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;");
50 TEST_F(FormatTest
, FormatsUnwrappedLinesAtFirstFormat
) {
51 verifyFormat("int i;", "int\ni;");
54 TEST_F(FormatTest
, FormatsNestedBlockStatements
) {
55 verifyFormat("{\n {\n {}\n }\n}", "{{{}}}");
58 TEST_F(FormatTest
, FormatsNestedCall
) {
59 verifyFormat("Method(f1, f2(f3));");
60 verifyFormat("Method(f1(f2, f3()));");
61 verifyFormat("Method(f1(f2, (f3())));");
64 TEST_F(FormatTest
, NestedNameSpecifiers
) {
65 verifyFormat("vector<::Type> v;");
66 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
67 verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
68 verifyFormat("static constexpr bool Bar = typeof(bar())::value;");
69 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;");
70 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;");
71 verifyFormat("bool a = 2 < ::SomeFunction();");
72 verifyFormat("ALWAYS_INLINE ::std::string getName();");
73 verifyFormat("some::string getName();");
76 TEST_F(FormatTest
, OnlyGeneratesNecessaryReplacements
) {
77 verifyFormat("if (a) {\n"
81 EXPECT_EQ(4, ReplacementCount
);
82 verifyNoChange("if (a) {\n"
85 EXPECT_EQ(0, ReplacementCount
);
86 verifyNoChange("/*\r\n"
89 EXPECT_EQ(0, ReplacementCount
);
92 TEST_F(FormatTest
, RemovesEmptyLines
) {
93 verifyFormat("class C {\n"
101 // Don't remove empty lines at the start of namespaces or extern "C" blocks.
102 verifyFormat("namespace N {\n"
111 verifyFormat("/* something */ namespace N {\n"
115 "/* something */ namespace N {\n"
120 verifyFormat("inline namespace N {\n"
124 "inline namespace N {\n"
129 verifyFormat("/* something */ inline namespace N {\n"
133 "/* something */ inline namespace N {\n"
138 verifyFormat("export namespace N {\n"
142 "export namespace N {\n"
147 verifyFormat("extern /**/ \"C\" /**/ {\n"
151 "extern /**/ \"C\" /**/ {\n"
157 auto CustomStyle
= getLLVMStyle();
158 CustomStyle
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
159 CustomStyle
.BraceWrapping
.AfterNamespace
= true;
160 CustomStyle
.KeepEmptyLinesAtTheStartOfBlocks
= false;
161 verifyFormat("namespace N\n"
173 verifyFormat("/* something */ namespace N\n"
178 "/* something */ namespace N {\n"
184 verifyFormat("inline namespace N\n"
189 "inline namespace N\n"
196 verifyFormat("/* something */ inline namespace N\n"
201 "/* something */ inline namespace N\n"
207 verifyFormat("export namespace N\n"
212 "export namespace N\n"
218 verifyFormat("namespace a\n"
239 verifyFormat("namespace A /* comment */\n"
243 "namespace A /* comment */ { class B {} }", CustomStyle
);
244 verifyFormat("namespace A\n"
248 "namespace A {/* comment */ class B {} }", CustomStyle
);
249 verifyFormat("namespace A\n"
256 "namespace A { /* comment */\n"
264 verifyFormat("namespace A /* comment */\n"
270 "namespace A/* comment */ {\n"
279 // ...but do keep inlining and removing empty lines for non-block extern "C"
281 verifyGoogleFormat("extern \"C\" int f() { return 42; }");
282 verifyFormat("extern \"C\" int f() {\n"
286 "extern \"C\" int f() {\n"
293 // Remove empty lines at the beginning and end of blocks.
294 verifyFormat("void f() {\n"
310 verifyFormat("void f() {\n"
326 // Don't remove empty lines in more complex control statements.
327 verifyFormat("void f() {\n"
346 // Don't remove empty lines before namespace endings.
347 FormatStyle LLVMWithNoNamespaceFix
= getLLVMStyle();
348 LLVMWithNoNamespaceFix
.FixNamespaceComments
= false;
349 verifyNoChange("namespace {\n"
353 LLVMWithNoNamespaceFix
);
354 verifyFormat("namespace {\n"
357 LLVMWithNoNamespaceFix
);
358 verifyNoChange("namespace {\n"
362 LLVMWithNoNamespaceFix
);
363 verifyFormat("namespace {\n"
366 LLVMWithNoNamespaceFix
);
367 verifyNoChange("namespace {\n"
371 verifyFormat("namespace {\n"
380 FormatStyle Style
= getLLVMStyle();
381 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
382 Style
.MaxEmptyLinesToKeep
= 2;
383 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
384 Style
.BraceWrapping
.AfterClass
= true;
385 Style
.BraceWrapping
.AfterFunction
= true;
386 Style
.KeepEmptyLinesAtTheStartOfBlocks
= false;
388 verifyFormat("class Foo\n"
405 TEST_F(FormatTest
, RecognizesBinaryOperatorKeywords
) {
406 verifyFormat("x = (a) and (b);");
407 verifyFormat("x = (a) or (b);");
408 verifyFormat("x = (a) bitand (b);");
409 verifyFormat("x = (a) bitor (b);");
410 verifyFormat("x = (a) not_eq (b);");
411 verifyFormat("x = (a) and_eq (b);");
412 verifyFormat("x = (a) or_eq (b);");
413 verifyFormat("x = (a) xor (b);");
416 TEST_F(FormatTest
, RecognizesUnaryOperatorKeywords
) {
417 verifyFormat("x = compl(a);");
418 verifyFormat("x = not(a);");
419 verifyFormat("x = bitand(a);");
420 // Unary operator must not be merged with the next identifier
421 verifyFormat("x = compl a;");
422 verifyFormat("x = not a;");
423 verifyFormat("x = bitand a;");
426 //===----------------------------------------------------------------------===//
427 // Tests for control statements.
428 //===----------------------------------------------------------------------===//
430 TEST_F(FormatTest
, FormatIfWithoutCompoundStatement
) {
431 verifyFormat("if (true)\n f();\ng();");
432 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
433 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
434 verifyFormat("if constexpr (true)\n"
436 verifyFormat("if CONSTEXPR (true)\n"
438 verifyFormat("if constexpr (a)\n"
439 " if constexpr (b)\n"
440 " if constexpr (c)\n"
443 verifyFormat("if CONSTEXPR (a)\n"
444 " if CONSTEXPR (b)\n"
445 " if CONSTEXPR (c)\n"
448 verifyFormat("if constexpr (a)\n"
449 " if constexpr (b) {\n"
453 verifyFormat("if CONSTEXPR (a)\n"
454 " if CONSTEXPR (b) {\n"
459 verifyFormat("if consteval {\n}");
460 verifyFormat("if !consteval {\n}");
461 verifyFormat("if not consteval {\n}");
462 verifyFormat("if consteval {\n} else {\n}");
463 verifyFormat("if !consteval {\n} else {\n}");
464 verifyFormat("if consteval {\n"
467 verifyFormat("if !consteval {\n"
470 verifyFormat("if consteval {\n"
475 verifyFormat("if CONSTEVAL {\n"
478 verifyFormat("if !CONSTEVAL {\n"
482 verifyFormat("if (a)\n"
484 verifyFormat("if (a) {\n"
487 verifyFormat("if (a)\n"
491 verifyFormat("if (a) {\n"
495 verifyFormat("if (a)\n"
500 verifyFormat("if (a) {\n"
505 verifyFormat("if (a)\n"
511 verifyFormat("if (a) {\n"
517 verifyFormat("if (a)\n"
523 verifyFormat("if (a)\n"
530 verifyFormat("if (a)\n"
537 verifyFormat("if (a) {\n"
545 FormatStyle AllowsMergedIf
= getLLVMStyle();
546 AllowsMergedIf
.IfMacros
.push_back("MYIF");
547 AllowsMergedIf
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
548 AllowsMergedIf
.AllowShortIfStatementsOnASingleLine
=
549 FormatStyle::SIS_WithoutElse
;
550 verifyFormat("if (a)\n"
560 verifyFormat("#define A \\\n"
565 verifyFormat("if (a)\n"
568 verifyFormat("if (a)\n"
572 verifyFormat("if (a) // Can't merge this\n"
575 verifyFormat("if (a) /* still don't merge */\n"
578 verifyFormat("if (a) { // Never merge this\n"
582 verifyFormat("if (a) { /* Never merge this */\n"
586 verifyFormat("MYIF (a)\n"
596 verifyFormat("#define A \\\n"
601 verifyFormat("MYIF (a)\n"
604 verifyFormat("MYIF (a)\n"
608 verifyFormat("MYIF (a) // Can't merge this\n"
611 verifyFormat("MYIF (a) /* still don't merge */\n"
614 verifyFormat("MYIF (a) { // Never merge this\n"
618 verifyFormat("MYIF (a) { /* Never merge this */\n"
623 AllowsMergedIf
.ColumnLimit
= 14;
624 // Where line-lengths matter, a 2-letter synonym that maintains line length.
625 // Not IF to avoid any confusion that IF is somehow special.
626 AllowsMergedIf
.IfMacros
.push_back("FI");
627 verifyFormat("if (a) return;", AllowsMergedIf
);
628 verifyFormat("if (aaaaaaaaa)\n"
631 verifyFormat("FI (a) return;", AllowsMergedIf
);
632 verifyFormat("FI (aaaaaaaaa)\n"
636 AllowsMergedIf
.ColumnLimit
= 13;
637 verifyFormat("if (a)\n return;", AllowsMergedIf
);
638 verifyFormat("FI (a)\n return;", AllowsMergedIf
);
640 FormatStyle AllowsMergedIfElse
= getLLVMStyle();
641 AllowsMergedIfElse
.IfMacros
.push_back("MYIF");
642 AllowsMergedIfElse
.AllowShortIfStatementsOnASingleLine
=
643 FormatStyle::SIS_AllIfsAndElse
;
644 verifyFormat("if (a)\n"
660 verifyFormat("if (a)\n"
665 verifyFormat("if (a) {\n"
669 verifyFormat("if (a) return;\n"
670 "else if (b) return;\n"
673 verifyFormat("if (a) {\n"
676 verifyFormat("if (a) {\n"
677 "} else if (b) return;\n"
680 verifyFormat("if (a) return;\n"
684 verifyFormat("if (a)\n"
688 verifyFormat("if constexpr (a)\n"
689 " if constexpr (b) return;\n"
690 " else if constexpr (c) return;\n"
693 verifyFormat("MYIF (a)\n"
709 verifyFormat("MYIF (a)\n"
714 verifyFormat("MYIF (a) {\n"
718 verifyFormat("MYIF (a) return;\n"
719 "else MYIF (b) return;\n"
722 verifyFormat("MYIF (a) {\n"
725 verifyFormat("MYIF (a) {\n"
726 "} else MYIF (b) return;\n"
729 verifyFormat("MYIF (a) return;\n"
733 verifyFormat("MYIF (a)\n"
734 " MYIF (b) return;\n"
737 verifyFormat("MYIF constexpr (a)\n"
738 " MYIF constexpr (b) return;\n"
739 " else MYIF constexpr (c) return;\n"
744 TEST_F(FormatTest
, FormatIfWithoutCompoundStatementButElseWith
) {
745 FormatStyle AllowsMergedIf
= getLLVMStyle();
746 AllowsMergedIf
.IfMacros
.push_back("MYIF");
747 AllowsMergedIf
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
748 AllowsMergedIf
.AllowShortIfStatementsOnASingleLine
=
749 FormatStyle::SIS_WithoutElse
;
750 verifyFormat("if (a)\n"
756 verifyFormat("if (a)\n"
762 verifyFormat("if (a) g();", AllowsMergedIf
);
763 verifyFormat("if (a) {\n"
767 verifyFormat("if (a)\n"
772 verifyFormat("if (a) {\n"
777 verifyFormat("if (a)\n"
783 verifyFormat("if (a) {\n"
789 verifyFormat("if (a)\n"
796 verifyFormat("if (a) {\n"
803 verifyFormat("if (a)\n"
810 verifyFormat("if (a)\n"
818 verifyFormat("if (a)\n"
826 verifyFormat("if (a) {\n"
834 verifyFormat("MYIF (a)\n"
840 verifyFormat("MYIF (a)\n"
846 verifyFormat("MYIF (a) g();", AllowsMergedIf
);
847 verifyFormat("MYIF (a) {\n"
851 verifyFormat("MYIF (a)\n"
856 verifyFormat("MYIF (a) {\n"
861 verifyFormat("MYIF (a)\n"
867 verifyFormat("MYIF (a) {\n"
873 verifyFormat("MYIF (a)\n"
880 verifyFormat("MYIF (a)\n"
887 verifyFormat("MYIF (a) {\n"
894 verifyFormat("MYIF (a) {\n"
901 verifyFormat("MYIF (a)\n"
908 verifyFormat("MYIF (a)\n"
915 verifyFormat("MYIF (a)\n"
923 verifyFormat("MYIF (a)\n"
931 verifyFormat("MYIF (a)\n"
939 verifyFormat("MYIF (a)\n"
947 verifyFormat("MYIF (a) {\n"
949 "} else MYIF (b) {\n"
955 verifyFormat("MYIF (a) {\n"
964 AllowsMergedIf
.AllowShortIfStatementsOnASingleLine
=
965 FormatStyle::SIS_OnlyFirstIf
;
967 verifyFormat("if (a) f();\n"
972 verifyFormat("if (a) f();\n"
982 verifyFormat("if (a) g();", AllowsMergedIf
);
983 verifyFormat("if (a) {\n"
987 verifyFormat("if (a) g();\n"
991 verifyFormat("if (a) {\n"
996 verifyFormat("if (a) g();\n"
1001 verifyFormat("if (a) {\n"
1007 verifyFormat("if (a) g();\n"
1013 verifyFormat("if (a) {\n"
1020 verifyFormat("if (a) g();\n"
1026 verifyFormat("if (a) g();\n"
1033 verifyFormat("if (a) g();\n"
1040 verifyFormat("if (a) {\n"
1048 verifyFormat("MYIF (a) f();\n"
1053 verifyFormat("MYIF (a) f();\n"
1063 verifyFormat("MYIF (a) g();", AllowsMergedIf
);
1064 verifyFormat("MYIF (a) {\n"
1068 verifyFormat("MYIF (a) g();\n"
1072 verifyFormat("MYIF (a) {\n"
1077 verifyFormat("MYIF (a) g();\n"
1082 verifyFormat("MYIF (a) {\n"
1088 verifyFormat("MYIF (a) g();\n"
1094 verifyFormat("MYIF (a) g();\n"
1100 verifyFormat("MYIF (a) {\n"
1107 verifyFormat("MYIF (a) {\n"
1114 verifyFormat("MYIF (a) g();\n"
1120 verifyFormat("MYIF (a) g();\n"
1126 verifyFormat("MYIF (a) g();\n"
1133 verifyFormat("MYIF (a) g();\n"
1140 verifyFormat("MYIF (a) g();\n"
1147 verifyFormat("MYIF (a) g();\n"
1154 verifyFormat("MYIF (a) {\n"
1156 "} else MYIF (b) {\n"
1162 verifyFormat("MYIF (a) {\n"
1171 AllowsMergedIf
.AllowShortIfStatementsOnASingleLine
=
1172 FormatStyle::SIS_AllIfsAndElse
;
1174 verifyFormat("if (a) f();\n"
1179 verifyFormat("if (a) f();\n"
1189 verifyFormat("if (a) g();", AllowsMergedIf
);
1190 verifyFormat("if (a) {\n"
1194 verifyFormat("if (a) g();\n"
1197 verifyFormat("if (a) {\n"
1201 verifyFormat("if (a) g();\n"
1206 verifyFormat("if (a) {\n"
1212 verifyFormat("if (a) g();\n"
1213 "else if (b) g();\n"
1216 verifyFormat("if (a) {\n"
1218 "} else if (b) g();\n"
1221 verifyFormat("if (a) g();\n"
1226 verifyFormat("if (a) g();\n"
1227 "else if (b) g();\n"
1232 verifyFormat("if (a) g();\n"
1239 verifyFormat("if (a) {\n"
1247 verifyFormat("MYIF (a) f();\n"
1252 verifyFormat("MYIF (a) f();\n"
1262 verifyFormat("MYIF (a) g();", AllowsMergedIf
);
1263 verifyFormat("MYIF (a) {\n"
1267 verifyFormat("MYIF (a) g();\n"
1270 verifyFormat("MYIF (a) {\n"
1274 verifyFormat("MYIF (a) g();\n"
1279 verifyFormat("MYIF (a) {\n"
1285 verifyFormat("MYIF (a) g();\n"
1286 "else MYIF (b) g();\n"
1289 verifyFormat("MYIF (a) g();\n"
1290 "else if (b) g();\n"
1293 verifyFormat("MYIF (a) {\n"
1295 "} else MYIF (b) g();\n"
1298 verifyFormat("MYIF (a) {\n"
1300 "} else if (b) g();\n"
1303 verifyFormat("MYIF (a) g();\n"
1308 verifyFormat("MYIF (a) g();\n"
1313 verifyFormat("MYIF (a) g();\n"
1314 "else MYIF (b) g();\n"
1319 verifyFormat("MYIF (a) g();\n"
1320 "else if (b) g();\n"
1325 verifyFormat("MYIF (a) g();\n"
1332 verifyFormat("MYIF (a) g();\n"
1339 verifyFormat("MYIF (a) {\n"
1341 "} else MYIF (b) {\n"
1347 verifyFormat("MYIF (a) {\n"
1357 TEST_F(FormatTest
, FormatLoopsWithoutCompoundStatement
) {
1358 FormatStyle AllowsMergedLoops
= getLLVMStyle();
1359 AllowsMergedLoops
.AllowShortLoopsOnASingleLine
= true;
1360 verifyFormat("while (true) continue;", AllowsMergedLoops
);
1361 verifyFormat("for (;;) continue;", AllowsMergedLoops
);
1362 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops
);
1363 verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops
);
1364 verifyFormat("while (true)\n"
1367 verifyFormat("for (;;)\n"
1370 verifyFormat("for (;;)\n"
1371 " for (;;) continue;",
1373 verifyFormat("for (;;)\n"
1374 " while (true) continue;",
1376 verifyFormat("while (true)\n"
1377 " for (;;) continue;",
1379 verifyFormat("BOOST_FOREACH (int &v, vec)\n"
1380 " for (;;) continue;",
1382 verifyFormat("for (;;)\n"
1383 " BOOST_FOREACH (int &v, vec) continue;",
1385 verifyFormat("for (;;) // Can't merge this\n"
1388 verifyFormat("for (;;) /* still don't merge */\n"
1391 verifyFormat("do a++;\n"
1394 verifyFormat("do /* Don't merge */\n"
1398 verifyFormat("do // Don't merge\n"
1407 // Without braces labels are interpreted differently.
1417 TEST_F(FormatTest
, FormatShortBracedStatements
) {
1418 FormatStyle AllowSimpleBracedStatements
= getLLVMStyle();
1419 EXPECT_EQ(AllowSimpleBracedStatements
.AllowShortBlocksOnASingleLine
, false);
1420 EXPECT_EQ(AllowSimpleBracedStatements
.AllowShortIfStatementsOnASingleLine
,
1421 FormatStyle::SIS_Never
);
1422 EXPECT_EQ(AllowSimpleBracedStatements
.AllowShortLoopsOnASingleLine
, false);
1423 EXPECT_EQ(AllowSimpleBracedStatements
.BraceWrapping
.AfterFunction
, false);
1424 verifyFormat("for (;;) {\n"
1427 verifyFormat("/*comment*/ for (;;) {\n"
1430 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1433 verifyFormat("/*comment*/ BOOST_FOREACH (int v, vec) {\n"
1436 verifyFormat("while (true) {\n"
1439 verifyFormat("/*comment*/ while (true) {\n"
1442 verifyFormat("if (true) {\n"
1445 verifyFormat("/*comment*/ if (true) {\n"
1449 AllowSimpleBracedStatements
.AllowShortBlocksOnASingleLine
=
1450 FormatStyle::SBS_Empty
;
1451 AllowSimpleBracedStatements
.AllowShortIfStatementsOnASingleLine
=
1452 FormatStyle::SIS_WithoutElse
;
1453 verifyFormat("if (true) {}", AllowSimpleBracedStatements
);
1454 verifyFormat("if (i) break;", AllowSimpleBracedStatements
);
1455 verifyFormat("if (i > 0) {\n"
1458 AllowSimpleBracedStatements
);
1460 AllowSimpleBracedStatements
.IfMacros
.push_back("MYIF");
1461 // Where line-lengths matter, a 2-letter synonym that maintains line length.
1462 // Not IF to avoid any confusion that IF is somehow special.
1463 AllowSimpleBracedStatements
.IfMacros
.push_back("FI");
1464 AllowSimpleBracedStatements
.ColumnLimit
= 40;
1465 AllowSimpleBracedStatements
.AllowShortBlocksOnASingleLine
=
1466 FormatStyle::SBS_Always
;
1467 AllowSimpleBracedStatements
.AllowShortLoopsOnASingleLine
= true;
1468 AllowSimpleBracedStatements
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
1469 AllowSimpleBracedStatements
.BraceWrapping
.AfterFunction
= true;
1470 AllowSimpleBracedStatements
.BraceWrapping
.SplitEmptyRecord
= false;
1472 verifyFormat("if (true) {}", AllowSimpleBracedStatements
);
1473 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements
);
1474 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements
);
1475 verifyFormat("if consteval {}", AllowSimpleBracedStatements
);
1476 verifyFormat("if !consteval {}", AllowSimpleBracedStatements
);
1477 verifyFormat("if CONSTEVAL {}", AllowSimpleBracedStatements
);
1478 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements
);
1479 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements
);
1480 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements
);
1481 verifyFormat("while (true) {}", AllowSimpleBracedStatements
);
1482 verifyFormat("for (;;) {}", AllowSimpleBracedStatements
);
1483 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements
);
1484 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements
);
1485 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements
);
1486 verifyFormat("if consteval { f(); }", AllowSimpleBracedStatements
);
1487 verifyFormat("if CONSTEVAL { f(); }", AllowSimpleBracedStatements
);
1488 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements
);
1489 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements
);
1490 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements
);
1491 verifyFormat("MYIF consteval { f(); }", AllowSimpleBracedStatements
);
1492 verifyFormat("MYIF CONSTEVAL { f(); }", AllowSimpleBracedStatements
);
1493 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements
);
1494 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements
);
1495 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1496 AllowSimpleBracedStatements
);
1497 verifyFormat("if (true) {\n"
1498 " ffffffffffffffffffffffff();\n"
1500 AllowSimpleBracedStatements
);
1501 verifyFormat("if (true) {\n"
1502 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1504 AllowSimpleBracedStatements
);
1505 verifyFormat("if (true) { //\n"
1508 AllowSimpleBracedStatements
);
1509 verifyFormat("if (true) {\n"
1513 AllowSimpleBracedStatements
);
1514 verifyFormat("if (true) {\n"
1519 AllowSimpleBracedStatements
);
1520 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1521 AllowSimpleBracedStatements
);
1522 verifyFormat("MYIF (true) {\n"
1523 " ffffffffffffffffffffffff();\n"
1525 AllowSimpleBracedStatements
);
1526 verifyFormat("MYIF (true) {\n"
1527 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1529 AllowSimpleBracedStatements
);
1530 verifyFormat("MYIF (true) { //\n"
1533 AllowSimpleBracedStatements
);
1534 verifyFormat("MYIF (true) {\n"
1538 AllowSimpleBracedStatements
);
1539 verifyFormat("MYIF (true) {\n"
1544 AllowSimpleBracedStatements
);
1546 verifyFormat("struct A2 {\n"
1549 AllowSimpleBracedStatements
);
1550 verifyFormat("typedef struct A2 {\n"
1553 AllowSimpleBracedStatements
);
1554 verifyFormat("template <int> struct A2 {\n"
1557 AllowSimpleBracedStatements
);
1559 AllowSimpleBracedStatements
.AllowShortIfStatementsOnASingleLine
=
1560 FormatStyle::SIS_Never
;
1561 verifyFormat("if (true) {}", AllowSimpleBracedStatements
);
1562 verifyFormat("if (true) {\n"
1565 AllowSimpleBracedStatements
);
1566 verifyFormat("if (true) {\n"
1571 AllowSimpleBracedStatements
);
1572 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements
);
1573 verifyFormat("MYIF (true) {\n"
1576 AllowSimpleBracedStatements
);
1577 verifyFormat("MYIF (true) {\n"
1582 AllowSimpleBracedStatements
);
1584 AllowSimpleBracedStatements
.AllowShortLoopsOnASingleLine
= false;
1585 verifyFormat("while (true) {}", AllowSimpleBracedStatements
);
1586 verifyFormat("while (true) {\n"
1589 AllowSimpleBracedStatements
);
1590 verifyFormat("for (;;) {}", AllowSimpleBracedStatements
);
1591 verifyFormat("for (;;) {\n"
1594 AllowSimpleBracedStatements
);
1595 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements
);
1596 verifyFormat("BOOST_FOREACH (int v, vec) {\n"
1599 AllowSimpleBracedStatements
);
1601 AllowSimpleBracedStatements
.AllowShortIfStatementsOnASingleLine
=
1602 FormatStyle::SIS_WithoutElse
;
1603 AllowSimpleBracedStatements
.AllowShortLoopsOnASingleLine
= true;
1604 AllowSimpleBracedStatements
.BraceWrapping
.AfterControlStatement
=
1605 FormatStyle::BWACS_Always
;
1607 verifyFormat("if (true) {}", AllowSimpleBracedStatements
);
1608 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements
);
1609 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements
);
1610 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements
);
1611 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements
);
1612 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements
);
1613 verifyFormat("while (true) {}", AllowSimpleBracedStatements
);
1614 verifyFormat("for (;;) {}", AllowSimpleBracedStatements
);
1615 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements
);
1616 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements
);
1617 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements
);
1618 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements
);
1619 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements
);
1620 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements
);
1621 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements
);
1622 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements
);
1623 verifyFormat("if (true) { fffffffffffffffffffffff(); }",
1624 AllowSimpleBracedStatements
);
1625 verifyFormat("if (true)\n"
1627 " ffffffffffffffffffffffff();\n"
1629 AllowSimpleBracedStatements
);
1630 verifyFormat("if (true)\n"
1632 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1634 AllowSimpleBracedStatements
);
1635 verifyFormat("if (true)\n"
1639 AllowSimpleBracedStatements
);
1640 verifyFormat("if (true)\n"
1645 AllowSimpleBracedStatements
);
1646 verifyFormat("if (true)\n"
1653 AllowSimpleBracedStatements
);
1654 verifyFormat("FI (true) { fffffffffffffffffffffff(); }",
1655 AllowSimpleBracedStatements
);
1656 verifyFormat("MYIF (true)\n"
1658 " ffffffffffffffffffffffff();\n"
1660 AllowSimpleBracedStatements
);
1661 verifyFormat("MYIF (true)\n"
1663 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
1665 AllowSimpleBracedStatements
);
1666 verifyFormat("MYIF (true)\n"
1670 AllowSimpleBracedStatements
);
1671 verifyFormat("MYIF (true)\n"
1676 AllowSimpleBracedStatements
);
1677 verifyFormat("MYIF (true)\n"
1684 AllowSimpleBracedStatements
);
1686 AllowSimpleBracedStatements
.AllowShortIfStatementsOnASingleLine
=
1687 FormatStyle::SIS_Never
;
1688 verifyFormat("if (true) {}", AllowSimpleBracedStatements
);
1689 verifyFormat("if (true)\n"
1693 AllowSimpleBracedStatements
);
1694 verifyFormat("if (true)\n"
1701 AllowSimpleBracedStatements
);
1702 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements
);
1703 verifyFormat("MYIF (true)\n"
1707 AllowSimpleBracedStatements
);
1708 verifyFormat("MYIF (true)\n"
1715 AllowSimpleBracedStatements
);
1717 AllowSimpleBracedStatements
.AllowShortLoopsOnASingleLine
= false;
1718 verifyFormat("while (true) {}", AllowSimpleBracedStatements
);
1719 verifyFormat("while (true)\n"
1723 AllowSimpleBracedStatements
);
1724 verifyFormat("for (;;) {}", AllowSimpleBracedStatements
);
1725 verifyFormat("for (;;)\n"
1729 AllowSimpleBracedStatements
);
1730 verifyFormat("BOOST_FOREACH (int v, vec) {}", AllowSimpleBracedStatements
);
1731 verifyFormat("BOOST_FOREACH (int v, vec)\n"
1735 AllowSimpleBracedStatements
);
1737 FormatStyle Style
= getLLVMStyle();
1738 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
1739 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
1741 verifyFormat("while (i > 0)\n"
1747 verifyFormat("if (a)\n"
1753 verifyFormat("if (a)\n"
1762 verifyFormat("if (a)\n"
1774 Style
.BraceWrapping
.BeforeElse
= true;
1776 verifyFormat("if (a)\n"
1786 verifyFormat("if (a)\n"
1801 TEST_F(FormatTest
, UnderstandsMacros
) {
1802 verifyFormat("#define A (parentheses)");
1803 verifyFormat("/* comment */ #define A (parentheses)");
1804 verifyFormat("/* comment */ /* another comment */ #define A (parentheses)");
1805 // Even the partial code should never be merged.
1806 verifyNoChange("/* comment */ #define A (parentheses)\n"
1808 verifyFormat("/* comment */ #define A (parentheses)\n"
1810 verifyFormat("/* comment */ #define A (parentheses)\n"
1811 "#define B (parentheses)");
1812 verifyFormat("#define true ((int)1)");
1813 verifyFormat("#define and(x)");
1814 verifyFormat("#define if(x) x");
1815 verifyFormat("#define return(x) (x)");
1816 verifyFormat("#define while(x) for (; x;)");
1817 verifyFormat("#define xor(x) (^(x))");
1818 verifyFormat("#define __except(x)");
1819 verifyFormat("#define __try(x)");
1821 // https://llvm.org/PR54348.
1828 FormatStyle Style
= getLLVMStyle();
1829 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
1830 Style
.BraceWrapping
.AfterFunction
= true;
1831 // Test that a macro definition never gets merged with the following
1833 // FIXME: The AAA macro definition probably should not be split into 3 lines.
1834 verifyFormat("#define AAA "
1841 // verifyFormat("#define AAA N { //", Style);
1843 verifyFormat("MACRO(return)");
1844 verifyFormat("MACRO(co_await)");
1845 verifyFormat("MACRO(co_return)");
1846 verifyFormat("MACRO(co_yield)");
1847 verifyFormat("MACRO(return, something)");
1848 verifyFormat("MACRO(co_return, something)");
1849 verifyFormat("MACRO(something##something)");
1850 verifyFormat("MACRO(return##something)");
1851 verifyFormat("MACRO(co_return##something)");
1854 TEST_F(FormatTest
, ShortBlocksInMacrosDontMergeWithCodeAfterMacro
) {
1855 FormatStyle Style
= getLLVMStyleWithColumns(60);
1856 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Always
;
1857 Style
.AllowShortIfStatementsOnASingleLine
= FormatStyle::SIS_WithoutElse
;
1858 Style
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
1859 verifyFormat("#define A \\\n"
1860 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n"
1862 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1866 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n"
1867 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n"
1873 TEST_F(FormatTest
, ParseIfElse
) {
1874 verifyFormat("if (true)\n"
1884 verifyFormat("if (true)\n"
1897 verifyFormat("if (true)\n"
1898 " if constexpr (true)\n"
1900 " if constexpr (true)\n"
1910 verifyFormat("if (true)\n"
1911 " if CONSTEXPR (true)\n"
1913 " if CONSTEXPR (true)\n"
1923 verifyFormat("void f() {\n"
1930 TEST_F(FormatTest
, ElseIf
) {
1931 verifyFormat("if (a) {\n} else if (b) {\n}");
1932 verifyFormat("if (a)\n"
1938 verifyFormat("if (a)\n"
1945 verifyFormat("if constexpr (a)\n"
1947 "else if constexpr (b)\n"
1951 verifyFormat("if CONSTEXPR (a)\n"
1953 "else if CONSTEXPR (b)\n"
1957 verifyFormat("if (a) {\n"
1965 verifyFormat("if (a) {\n"
1966 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1967 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1969 verifyFormat("if (a) {\n"
1970 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1971 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1973 verifyFormat("if (a) {\n"
1974 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1975 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
1977 verifyFormat("if (a) {\n"
1979 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1981 getLLVMStyleWithColumns(62));
1982 verifyFormat("if (a) {\n"
1983 "} else if constexpr (\n"
1984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1986 getLLVMStyleWithColumns(62));
1987 verifyFormat("if (a) {\n"
1988 "} else if CONSTEXPR (\n"
1989 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
1991 getLLVMStyleWithColumns(62));
1994 TEST_F(FormatTest
, SeparatePointerReferenceAlignment
) {
1995 FormatStyle Style
= getLLVMStyle();
1996 EXPECT_EQ(Style
.PointerAlignment
, FormatStyle::PAS_Right
);
1997 EXPECT_EQ(Style
.ReferenceAlignment
, FormatStyle::RAS_Pointer
);
1998 verifyFormat("int *f1(int *a, int &b, int &&c);", Style
);
1999 verifyFormat("int &f2(int &&c, int *a, int &b);", Style
);
2000 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style
);
2001 verifyFormat("int *f1(int &a) const &;", Style
);
2002 verifyFormat("int *f1(int &a) const & = 0;", Style
);
2003 verifyFormat("int *a = f1();", Style
);
2004 verifyFormat("int &b = f2();", Style
);
2005 verifyFormat("int &&c = f3();", Style
);
2006 verifyFormat("int f3() { return sizeof(Foo &); }", Style
);
2007 verifyFormat("int f4() { return sizeof(Foo &&); }", Style
);
2008 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style
);
2009 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style
);
2010 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style
);
2011 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style
);
2012 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style
);
2013 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style
);
2014 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style
);
2015 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style
);
2016 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style
);
2017 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style
);
2018 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style
);
2019 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style
);
2020 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style
);
2021 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style
);
2022 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style
);
2023 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style
);
2024 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style
);
2025 verifyFormat("for (f(); int &c : {1, 2, 3})", Style
);
2027 "function<int(int &)> res1 = [](int &a) { return 0000000000000; },\n"
2028 " res2 = [](int &a) { return 0000000000000; };",
2031 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
2032 verifyFormat("Const unsigned int *c;\n"
2033 "const unsigned int *d;\n"
2034 "Const unsigned int &e;\n"
2035 "const unsigned int &f;\n"
2036 "const unsigned &&g;\n"
2037 "Const unsigned h;",
2040 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
2041 Style
.ReferenceAlignment
= FormatStyle::RAS_Pointer
;
2042 verifyFormat("int* f1(int* a, int& b, int&& c);", Style
);
2043 verifyFormat("int& f2(int&& c, int* a, int& b);", Style
);
2044 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style
);
2045 verifyFormat("int* f1(int& a) const& = 0;", Style
);
2046 verifyFormat("int* a = f1();", Style
);
2047 verifyFormat("int& b = f2();", Style
);
2048 verifyFormat("int&& c = f3();", Style
);
2049 verifyFormat("int f3() { return sizeof(Foo&); }", Style
);
2050 verifyFormat("int f4() { return sizeof(Foo&&); }", Style
);
2051 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style
);
2052 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style
);
2053 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style
);
2054 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style
);
2055 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style
);
2056 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style
);
2057 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style
);
2058 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style
);
2059 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style
);
2060 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style
);
2061 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style
);
2062 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style
);
2063 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style
);
2064 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style
);
2065 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style
);
2066 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style
);
2067 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style
);
2068 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style
);
2069 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style
);
2070 verifyFormat("for (f(); int& c : {1, 2, 3})", Style
);
2072 "function<int(int&)> res1 = [](int& a) { return 0000000000000; },\n"
2073 " res2 = [](int& a) { return 0000000000000; };",
2076 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
2077 verifyFormat("Const unsigned int* c;\n"
2078 "const unsigned int* d;\n"
2079 "Const unsigned int& e;\n"
2080 "const unsigned int& f;\n"
2081 "const unsigned&& g;\n"
2082 "Const unsigned h;",
2085 Style
.PointerAlignment
= FormatStyle::PAS_Right
;
2086 Style
.ReferenceAlignment
= FormatStyle::RAS_Left
;
2087 verifyFormat("int *f1(int *a, int& b, int&& c);", Style
);
2088 verifyFormat("int& f2(int&& c, int *a, int& b);", Style
);
2089 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style
);
2090 verifyFormat("int *a = f1();", Style
);
2091 verifyFormat("int& b = f2();", Style
);
2092 verifyFormat("int&& c = f3();", Style
);
2093 verifyFormat("int f3() { return sizeof(Foo&); }", Style
);
2094 verifyFormat("int f4() { return sizeof(Foo&&); }", Style
);
2095 verifyFormat("void f5() { int f6(Foo&, Bar&); }", Style
);
2096 verifyFormat("void f5() { int f6(Foo&&, Bar&&); }", Style
);
2097 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style
);
2098 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style
);
2099 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style
);
2101 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
2102 verifyFormat("Const unsigned int *c;\n"
2103 "const unsigned int *d;\n"
2104 "Const unsigned int& e;\n"
2105 "const unsigned int& f;\n"
2106 "const unsigned g;\n"
2107 "Const unsigned h;",
2110 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
2111 Style
.ReferenceAlignment
= FormatStyle::RAS_Middle
;
2112 verifyFormat("int* f1(int* a, int & b, int && c);", Style
);
2113 verifyFormat("int & f2(int && c, int* a, int & b);", Style
);
2114 verifyFormat("int && f3(int & b, int && c, int* a);", Style
);
2115 verifyFormat("int* a = f1();", Style
);
2116 verifyFormat("int & b = f2();", Style
);
2117 verifyFormat("int && c = f3();", Style
);
2118 verifyFormat("int f3() { return sizeof(Foo &); }", Style
);
2119 verifyFormat("int f4() { return sizeof(Foo &&); }", Style
);
2120 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style
);
2121 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style
);
2122 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style
);
2123 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style
);
2124 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style
);
2125 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style
);
2126 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style
);
2127 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style
);
2128 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style
);
2129 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style
);
2130 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style
);
2131 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style
);
2132 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style
);
2133 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style
);
2134 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style
);
2135 verifyFormat("for (f(); int & c : {1, 2, 3})", Style
);
2137 "function<int(int &)> res1 = [](int & a) { return 0000000000000; },\n"
2138 " res2 = [](int & a) { return 0000000000000; };",
2141 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
2142 verifyFormat("Const unsigned int* c;\n"
2143 "const unsigned int* d;\n"
2144 "Const unsigned int & e;\n"
2145 "const unsigned int & f;\n"
2146 "const unsigned && g;\n"
2147 "Const unsigned h;",
2150 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
2151 Style
.ReferenceAlignment
= FormatStyle::RAS_Right
;
2152 verifyFormat("int * f1(int * a, int &b, int &&c);", Style
);
2153 verifyFormat("int &f2(int &&c, int * a, int &b);", Style
);
2154 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style
);
2155 verifyFormat("int * a = f1();", Style
);
2156 verifyFormat("int &b = f2();", Style
);
2157 verifyFormat("int &&c = f3();", Style
);
2158 verifyFormat("int f3() { return sizeof(Foo &); }", Style
);
2159 verifyFormat("int f4() { return sizeof(Foo &&); }", Style
);
2160 verifyFormat("void f5() { int f6(Foo &, Bar &); }", Style
);
2161 verifyFormat("void f5() { int f6(Foo &&, Bar &&); }", Style
);
2162 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style
);
2163 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style
);
2164 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style
);
2166 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
2167 verifyFormat("Const unsigned int * c;\n"
2168 "const unsigned int * d;\n"
2169 "Const unsigned int &e;\n"
2170 "const unsigned int &f;\n"
2171 "const unsigned &&g;\n"
2172 "Const unsigned h;",
2175 // FIXME: we don't handle this yet, so output may be arbitrary until it's
2176 // specifically handled
2177 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style);
2180 TEST_F(FormatTest
, FormatsForLoop
) {
2182 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
2183 " ++VeryVeryLongLoopVariable)\n"
2185 verifyFormat("for (;;)\n"
2187 verifyFormat("for (;;) {\n}");
2188 verifyFormat("for (;;) {\n"
2191 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
2194 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2195 " E = UnwrappedLines.end();\n"
2196 " I != E; ++I) {\n}");
2199 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
2201 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
2202 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
2203 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
2204 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
2205 " I = FD->getDeclsInPrototypeScope().begin(),\n"
2206 " E = FD->getDeclsInPrototypeScope().end();\n"
2207 " I != E; ++I) {\n}");
2208 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
2209 " I = Container.begin(),\n"
2210 " E = Container.end();\n"
2211 " I != E; ++I) {\n}",
2212 getLLVMStyleWithColumns(76));
2215 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
2217 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2218 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2219 " ++aaaaaaaaaaa) {\n}");
2220 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2221 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
2223 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
2224 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2226 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
2228 " iter; ++iter) {\n"
2230 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2231 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
2232 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
2233 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
2235 // These should not be formatted as Objective-C for-in loops.
2236 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}");
2237 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}");
2238 verifyFormat("Foo *x;\nfor (x in y) {\n}");
2240 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}");
2242 FormatStyle NoBinPacking
= getLLVMStyle();
2243 NoBinPacking
.BinPackParameters
= false;
2244 verifyFormat("for (int aaaaaaaaaaa = 1;\n"
2245 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
2246 " aaaaaaaaaaaaaaaa,\n"
2247 " aaaaaaaaaaaaaaaa,\n"
2248 " aaaaaaaaaaaaaaaa);\n"
2249 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
2253 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
2254 " E = UnwrappedLines.end();\n"
2259 FormatStyle AlignLeft
= getLLVMStyle();
2260 AlignLeft
.PointerAlignment
= FormatStyle::PAS_Left
;
2261 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft
);
2264 TEST_F(FormatTest
, RangeBasedForLoops
) {
2265 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
2266 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2267 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
2268 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
2269 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
2270 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
2271 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
2272 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
2275 TEST_F(FormatTest
, ForEachLoops
) {
2276 FormatStyle Style
= getLLVMStyle();
2277 EXPECT_EQ(Style
.AllowShortBlocksOnASingleLine
, FormatStyle::SBS_Never
);
2278 EXPECT_EQ(Style
.AllowShortLoopsOnASingleLine
, false);
2279 verifyFormat("void f() {\n"
2282 " foreach (Item *item, itemlist) {\n"
2284 " Q_FOREACH (Item *item, itemlist) {\n"
2286 " BOOST_FOREACH (Item *item, itemlist) {\n"
2288 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2291 verifyFormat("void f() {\n"
2294 " Q_FOREACH (int v, vec)\n"
2299 " Q_FOREACH (int v, vec) {\n"
2305 FormatStyle ShortBlocks
= getLLVMStyle();
2306 ShortBlocks
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Always
;
2307 EXPECT_EQ(ShortBlocks
.AllowShortLoopsOnASingleLine
, false);
2308 verifyFormat("void f() {\n"
2311 " Q_FOREACH (int &v, vec)\n"
2316 " Q_FOREACH (int &v, vec) {\n"
2322 FormatStyle ShortLoops
= getLLVMStyle();
2323 ShortLoops
.AllowShortLoopsOnASingleLine
= true;
2324 EXPECT_EQ(ShortLoops
.AllowShortBlocksOnASingleLine
, FormatStyle::SBS_Never
);
2325 verifyFormat("void f() {\n"
2326 " for (;;) int j = 1;\n"
2327 " Q_FOREACH (int &v, vec) int j = 1;\n"
2331 " Q_FOREACH (int &v, vec) {\n"
2337 FormatStyle ShortBlocksAndLoops
= getLLVMStyle();
2338 ShortBlocksAndLoops
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Always
;
2339 ShortBlocksAndLoops
.AllowShortLoopsOnASingleLine
= true;
2340 verifyFormat("void f() {\n"
2341 " for (;;) int j = 1;\n"
2342 " Q_FOREACH (int &v, vec) int j = 1;\n"
2343 " for (;;) { int j = 1; }\n"
2344 " Q_FOREACH (int &v, vec) { int j = 1; }\n"
2346 ShortBlocksAndLoops
);
2348 Style
.SpaceBeforeParens
=
2349 FormatStyle::SBPO_ControlStatementsExceptControlMacros
;
2350 verifyFormat("void f() {\n"
2353 " foreach(Item *item, itemlist) {\n"
2355 " Q_FOREACH(Item *item, itemlist) {\n"
2357 " BOOST_FOREACH(Item *item, itemlist) {\n"
2359 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n"
2363 // As function-like macros.
2364 verifyFormat("#define foreach(x, y)\n"
2365 "#define Q_FOREACH(x, y)\n"
2366 "#define BOOST_FOREACH(x, y)\n"
2367 "#define UNKNOWN_FOREACH(x, y)");
2369 // Not as function-like macros.
2370 verifyFormat("#define foreach (x, y)\n"
2371 "#define Q_FOREACH (x, y)\n"
2372 "#define BOOST_FOREACH (x, y)\n"
2373 "#define UNKNOWN_FOREACH (x, y)");
2375 // handle microsoft non standard extension
2376 verifyFormat("for each (char c in x->MyStringProperty)");
2379 TEST_F(FormatTest
, FormatsWhileLoop
) {
2380 verifyFormat("while (true) {\n}");
2381 verifyFormat("while (true)\n"
2383 verifyFormat("while () {\n}");
2384 verifyFormat("while () {\n"
2389 TEST_F(FormatTest
, FormatsDoWhile
) {
2390 verifyFormat("do {\n"
2391 " do_something();\n"
2392 "} while (something());");
2394 " do_something();\n"
2395 "while (something());");
2398 TEST_F(FormatTest
, FormatsSwitchStatement
) {
2399 verifyFormat("switch (x) {\n"
2411 verifyFormat("switch (x) {\n"
2420 verifyFormat("switch (x) {\n"
2430 verifyFormat("switch (x) {\n"
2440 verifyFormat("switch (x) {\n"
2446 verifyFormat("switch (test)\n"
2448 verifyFormat("switch (x) {\n"
2453 verifyFormat("switch (x) {\n"
2459 verifyFormat("switch (x) {\n"
2461 " // Do amazing stuff\n"
2468 verifyFormat("#define A \\\n"
2469 " switch (x) { \\\n"
2473 getLLVMStyleWithColumns(20));
2474 verifyFormat("#define OPERATION_CASE(name) \\\n"
2475 " case OP_name: \\\n"
2476 " return operations::Operation##name",
2477 getLLVMStyleWithColumns(40));
2478 verifyFormat("switch (x) {\n"
2484 verifyGoogleFormat("switch (x) {\n"
2496 verifyGoogleFormat("switch (x) {\n"
2502 verifyGoogleFormat("switch (test)\n"
2505 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
2506 " case OP_name: \\\n"
2507 " return operations::Operation##name");
2508 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
2509 " // Get the correction operation class.\n"
2510 " switch (OpCode) {\n"
2512 " CASE(Subtract);\n"
2514 " return operations::Unknown;\n"
2516 "#undef OPERATION_CASE\n"
2518 verifyFormat("DEBUG({\n"
2529 verifyNoChange("DEBUG({\n"
2540 verifyFormat("switch (n) {\n"
2557 verifyFormat("switch (a) {\n"
2562 verifyFormat("switch (a) {\n"
2563 "case some_namespace::\n"
2567 getLLVMStyleWithColumns(34));
2569 verifyFormat("switch (a) {\n"
2570 "[[likely]] case 1:\n"
2573 verifyFormat("switch (a) {\n"
2574 "[[likely]] [[other::likely]] case 1:\n"
2577 verifyFormat("switch (x) {\n"
2580 "[[likely]] case 2:\n"
2583 verifyFormat("switch (a) {\n"
2585 "[[likely]] case 2:\n"
2588 FormatStyle Attributes
= getLLVMStyle();
2589 Attributes
.AttributeMacros
.push_back("LIKELY");
2590 Attributes
.AttributeMacros
.push_back("OTHER_LIKELY");
2591 verifyFormat("switch (a) {\n"
2596 verifyFormat("switch (a) {\n"
2597 "LIKELY OTHER_LIKELY() case b:\n"
2601 verifyFormat("switch (a) {\n"
2608 verifyFormat("switch (a) {\n"
2615 FormatStyle Style
= getLLVMStyle();
2616 Style
.IndentCaseLabels
= true;
2617 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Never
;
2618 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
2619 Style
.BraceWrapping
.AfterCaseLabel
= true;
2620 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
2621 verifyFormat("switch (n)\n"
2641 Style
.BraceWrapping
.AfterCaseLabel
= false;
2642 verifyFormat("switch (n)\n"
2662 Style
.IndentCaseLabels
= false;
2663 Style
.IndentCaseBlocks
= true;
2664 verifyFormat("switch (n)\n"
2688 Style
.IndentCaseLabels
= true;
2689 Style
.IndentCaseBlocks
= true;
2690 verifyFormat("switch (n)\n"
2716 TEST_F(FormatTest
, CaseRanges
) {
2717 verifyFormat("switch (x) {\n"
2718 "case 'A' ... 'Z':\n"
2725 TEST_F(FormatTest
, ShortEnums
) {
2726 FormatStyle Style
= getLLVMStyle();
2727 EXPECT_TRUE(Style
.AllowShortEnumsOnASingleLine
);
2728 EXPECT_FALSE(Style
.BraceWrapping
.AfterEnum
);
2729 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style
);
2730 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style
);
2731 Style
.AllowShortEnumsOnASingleLine
= false;
2732 verifyFormat("enum {\n"
2736 "} ShortEnum1, ShortEnum2;",
2738 verifyFormat("typedef enum {\n"
2742 "} ShortEnum1, ShortEnum2;",
2744 verifyFormat("enum {\n"
2746 "} ShortEnum1, ShortEnum2;",
2748 verifyFormat("typedef enum {\n"
2750 "} ShortEnum1, ShortEnum2;",
2752 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
2753 Style
.BraceWrapping
.AfterEnum
= true;
2754 verifyFormat("enum\n"
2759 "} ShortEnum1, ShortEnum2;",
2761 verifyFormat("typedef enum\n"
2766 "} ShortEnum1, ShortEnum2;",
2770 TEST_F(FormatTest
, ShortCompoundRequirement
) {
2771 FormatStyle Style
= getLLVMStyle();
2772 EXPECT_TRUE(Style
.AllowShortCompoundRequirementOnASingleLine
);
2773 verifyFormat("template <typename T>\n"
2774 "concept c = requires(T x) {\n"
2775 " { x + 1 } -> std::same_as<int>;\n"
2778 verifyFormat("template <typename T>\n"
2779 "concept c = requires(T x) {\n"
2780 " { x + 1 } -> std::same_as<int>;\n"
2781 " { x + 2 } -> std::same_as<int>;\n"
2784 Style
.AllowShortCompoundRequirementOnASingleLine
= false;
2785 verifyFormat("template <typename T>\n"
2786 "concept c = requires(T x) {\n"
2789 " } -> std::same_as<int>;\n"
2792 verifyFormat("template <typename T>\n"
2793 "concept c = requires(T x) {\n"
2796 " } -> std::same_as<int>;\n"
2799 " } -> std::same_as<int>;\n"
2804 TEST_F(FormatTest
, ShortCaseLabels
) {
2805 FormatStyle Style
= getLLVMStyle();
2806 Style
.AllowShortCaseLabelsOnASingleLine
= true;
2807 verifyFormat("switch (a) {\n"
2808 "case 1: x = 1; break;\n"
2813 "case 6: // comment\n"
2819 " x = 8; // comment\n"
2821 "default: y = 1; break;\n"
2824 verifyFormat("switch (a) {\n"
2825 "case 0: return; // comment\n"
2826 "case 1: break; // comment\n"
2833 "case 4: break; /* comment */\n"
2837 "case 6: /* comment */ x = 1; break;\n"
2838 "case 7: x = /* comment */ 1; break;\n"
2840 " x = 1; /* comment */\n"
2843 " break; // comment line 1\n"
2844 " // comment line 2\n"
2847 verifyFormat("switch (a) {\n"
2850 " // fall through\n"
2854 " return; /* comment line 1\n"
2855 " * comment line 2 */\n"
2857 "// something else\n"
2864 " // fall through\n"
2869 " return; /* comment line 1\n"
2870 " * comment line 2 */\n"
2873 "// something else\n"
2879 verifyFormat("switch (a) {\n"
2881 " return; // long long long long long long long long long long "
2882 "long long comment\n"
2886 "case 0: return; // long long long long long long long long "
2887 "long long long long comment line\n"
2890 verifyFormat("switch (a) {\n"
2892 " return; /* long long long long long long long long long long "
2893 "long long comment\n"
2897 "case 0: return; /* long long long long long long long long "
2898 "long long long long comment line */\n"
2901 verifyFormat("switch (a) {\n"
2903 "case 0: return 0;\n"
2907 verifyFormat("switch (a) {\n"
2922 Style
.ColumnLimit
= 21;
2923 verifyFormat("#define X \\\n"
2927 verifyFormat("switch (a) {\n"
2928 "case 1: x = 1; break;\n"
2938 Style
.ColumnLimit
= 80;
2939 Style
.AllowShortCaseLabelsOnASingleLine
= false;
2940 Style
.IndentCaseLabels
= true;
2941 verifyFormat("switch (n) {\n"
2942 " default /*comments*/:\n"
2948 "default/*comments*/:\n"
2954 Style
.AllowShortCaseLabelsOnASingleLine
= true;
2955 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
2956 Style
.BraceWrapping
.AfterCaseLabel
= true;
2957 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
2958 verifyFormat("switch (n)\n"
2981 TEST_F(FormatTest
, FormatsLabels
) {
2982 verifyFormat("void f() {\n"
2985 " some_other_code();\n"
2987 " some_more_code();\n"
2989 " some_more_code();\n"
2995 " some_other_code();\n"
3004 "test_label: { some_other_code(); }\n"
3009 " some_other_code();\n"
3010 " some_other_code();\n"
3016 "[[bar]] [[baz]] L2:\n"
3021 "[[bar]] [[baz]] L2:\n"
3029 " [[bar]] [[baz]] L2:\n"
3033 FormatStyle Style
= getLLVMStyle();
3034 Style
.IndentGotoLabels
= false;
3035 verifyFormat("void f() {\n"
3038 " some_other_code();\n"
3040 " some_more_code();\n"
3042 " some_more_code();\n"
3049 " some_other_code();\n"
3060 "test_label: { some_other_code(); }\n"
3067 "[[bar]] [[baz]] L2:\n"
3072 // The opening brace may either be on the same unwrapped line as the colon or
3073 // on a separate one. The formatter should recognize both.
3074 Style
= getLLVMStyle();
3075 Style
.BreakBeforeBraces
= FormatStyle::BraceBreakingStyle::BS_Allman
;
3080 " some_other_code();\n"
3087 "[[bar]] [[baz]] L2:\n"
3094 TEST_F(FormatTest
, MultiLineControlStatements
) {
3095 FormatStyle Style
= getLLVMStyleWithColumns(20);
3096 Style
.BreakBeforeBraces
= FormatStyle::BraceBreakingStyle::BS_Custom
;
3097 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_MultiLine
;
3098 // Short lines should keep opening brace on same line.
3099 verifyFormat("if (foo) {\n"
3102 "if(foo){bar();}", Style
);
3103 verifyFormat("if (foo) {\n"
3108 "if(foo){bar();}else{baz();}", Style
);
3109 verifyFormat("if (foo && bar) {\n"
3112 "if(foo&&bar){baz();}", Style
);
3113 verifyFormat("if (foo) {\n"
3115 "} else if (baz) {\n"
3118 "if(foo){bar();}else if(baz){quux();}", Style
);
3119 verifyFormat("if (foo) {\n"
3121 "} else if (baz) {\n"
3126 "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style
);
3127 verifyFormat("for (;;) {\n"
3131 verifyFormat("while (1) {\n"
3134 "while(1){foo();}", Style
);
3135 verifyFormat("switch (foo) {\n"
3139 "switch(foo){case bar:return;}", Style
);
3140 verifyFormat("try {\n"
3145 "try{foo();}catch(...){bar();}", Style
);
3146 verifyFormat("do {\n"
3150 "do{foo();}while(bar&&baz);", Style
);
3151 // Long lines should put opening brace on new line.
3152 verifyFormat("void f() {\n"
3153 " if (a1 && a2 &&\n"
3159 "void f(){if(a1&&a2&&a3){quux();}}", Style
);
3160 verifyFormat("if (foo && bar &&\n"
3165 "if(foo&&bar&&baz){quux();}", Style
);
3166 verifyFormat("if (foo && bar &&\n"
3171 "if (foo && bar &&\n"
3176 verifyFormat("if (foo) {\n"
3178 "} else if (baz ||\n"
3183 "if(foo){bar();}else if(baz||quux){foobar();}", Style
);
3184 verifyFormat("if (foo) {\n"
3186 "} else if (baz ||\n"
3193 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3195 verifyFormat("for (int i = 0;\n"
3200 "for(int i=0;i<10;++i){foo();}", Style
);
3201 verifyFormat("foreach (int i,\n"
3206 "foreach(int i, list){foo();}", Style
);
3208 40; // to concentrate at brace wrapping, not line wrap due to column limit
3209 verifyFormat("foreach (int i, list) {\n"
3212 "foreach(int i, list){foo();}", Style
);
3214 20; // to concentrate at brace wrapping, not line wrap due to column limit
3215 verifyFormat("while (foo || bar ||\n"
3220 "while(foo||bar||baz){quux();}", Style
);
3221 verifyFormat("switch (\n"
3227 "switch(foo=barbaz){case quux:return;}", Style
);
3228 verifyFormat("try {\n"
3231 " Exception &bar)\n"
3235 "try{foo();}catch(Exception&bar){baz();}", Style
);
3237 40; // to concentrate at brace wrapping, not line wrap due to column limit
3238 verifyFormat("try {\n"
3240 "} catch (Exception &bar) {\n"
3243 "try{foo();}catch(Exception&bar){baz();}", Style
);
3245 20; // to concentrate at brace wrapping, not line wrap due to column limit
3247 Style
.BraceWrapping
.BeforeElse
= true;
3248 verifyFormat("if (foo) {\n"
3259 "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}",
3262 Style
.BraceWrapping
.BeforeCatch
= true;
3263 verifyFormat("try {\n"
3269 "try{foo();}catch(...){baz();}", Style
);
3271 Style
.BraceWrapping
.AfterFunction
= true;
3272 Style
.BraceWrapping
.AfterStruct
= false;
3273 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_MultiLine
;
3274 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
3275 Style
.ColumnLimit
= 80;
3276 verifyFormat("void shortfunction() { bar(); }", Style
);
3277 verifyFormat("struct T shortfunction() { return bar(); }", Style
);
3278 verifyFormat("struct T {};", Style
);
3280 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
3281 verifyFormat("void shortfunction()\n"
3286 verifyFormat("struct T shortfunction()\n"
3291 verifyFormat("struct T {};", Style
);
3293 Style
.BraceWrapping
.AfterFunction
= false;
3294 Style
.BraceWrapping
.AfterStruct
= true;
3295 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
3296 verifyFormat("void shortfunction() { bar(); }", Style
);
3297 verifyFormat("struct T shortfunction() { return bar(); }", Style
);
3298 verifyFormat("struct T\n"
3303 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
3304 verifyFormat("void shortfunction() {\n"
3308 verifyFormat("struct T shortfunction() {\n"
3312 verifyFormat("struct T\n"
3318 TEST_F(FormatTest
, BeforeWhile
) {
3319 FormatStyle Style
= getLLVMStyle();
3320 Style
.BreakBeforeBraces
= FormatStyle::BraceBreakingStyle::BS_Custom
;
3322 verifyFormat("do {\n"
3326 Style
.BraceWrapping
.BeforeWhile
= true;
3327 verifyFormat("do {\n"
3334 //===----------------------------------------------------------------------===//
3335 // Tests for classes, namespaces, etc.
3336 //===----------------------------------------------------------------------===//
3338 TEST_F(FormatTest
, DoesNotBreakSemiAfterClassDecl
) {
3339 verifyFormat("class A {};");
3342 TEST_F(FormatTest
, UnderstandsAccessSpecifiers
) {
3343 verifyFormat("class A {\n"
3345 "public: // comment\n"
3350 verifyFormat("export class A {\n"
3352 "public: // comment\n"
3357 verifyGoogleFormat("class A {\n"
3363 verifyGoogleFormat("export class A {\n"
3369 verifyFormat("class A {\n"
3374 "protected slots:\n"
3376 "protected Q_SLOTS:\n"
3380 "private Q_SLOTS:\n"
3388 // Don't interpret 'signals' the wrong way.
3389 verifyFormat("signals.set();");
3390 verifyFormat("for (Signals signals : f()) {\n}");
3392 " signals.set(); // This needs indentation.\n"
3394 verifyFormat("void f() {\n"
3398 verifyFormat("private[1];");
3399 verifyFormat("testArray[public] = 1;");
3400 verifyFormat("public();");
3401 verifyFormat("myFunc(public);");
3402 verifyFormat("std::vector<int> testVec = {private};");
3403 verifyFormat("private.p = 1;");
3404 verifyFormat("void function(private...){};");
3405 verifyFormat("if (private && public)");
3406 verifyFormat("private &= true;");
3407 verifyFormat("int x = private * public;");
3408 verifyFormat("public *= private;");
3409 verifyFormat("int x = public + private;");
3410 verifyFormat("private++;");
3411 verifyFormat("++private;");
3412 verifyFormat("public += private;");
3413 verifyFormat("public = public - private;");
3414 verifyFormat("public->foo();");
3415 verifyFormat("private--;");
3416 verifyFormat("--private;");
3417 verifyFormat("public -= 1;");
3418 verifyFormat("if (!private && !public)");
3419 verifyFormat("public != private;");
3420 verifyFormat("int x = public / private;");
3421 verifyFormat("public /= 2;");
3422 verifyFormat("public = public % 2;");
3423 verifyFormat("public %= 2;");
3424 verifyFormat("if (public < private)");
3425 verifyFormat("public << private;");
3426 verifyFormat("public <<= private;");
3427 verifyFormat("if (public > private)");
3428 verifyFormat("public >> private;");
3429 verifyFormat("public >>= private;");
3430 verifyFormat("public ^ private;");
3431 verifyFormat("public ^= private;");
3432 verifyFormat("public | private;");
3433 verifyFormat("public |= private;");
3434 verifyFormat("auto x = private ? 1 : 2;");
3435 verifyFormat("if (public == private)");
3436 verifyFormat("void foo(public, private)");
3437 verifyFormat("public::foo();");
3439 verifyFormat("class A {\n"
3441 " std::unique_ptr<int *[]> b() { return nullptr; }\n"
3448 " std::unique_ptr<int *[] /* okay */> b() { return nullptr; }\n"
3455 TEST_F(FormatTest
, SeparatesLogicalBlocks
) {
3456 verifyFormat("class A {\n"
3475 verifyFormat("class A {\n"
3488 // Even ensure proper spacing inside macros.
3489 verifyFormat("#define B \\\n"
3504 // But don't remove empty lines after macros ending in access specifiers.
3505 verifyFormat("#define A private:\n"
3508 "#define A private:\n"
3513 TEST_F(FormatTest
, FormatsClasses
) {
3514 verifyFormat("class A : public B {};");
3515 verifyFormat("class A : public ::B {};");
3518 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3519 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3520 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3521 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3522 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
3524 "class A : public B, public C, public D, public E, public F {};");
3525 verifyFormat("class AAAAAAAAAAAA : public B,\n"
3532 verifyFormat("class\n"
3533 " ReallyReallyLongClassName {\n"
3536 getLLVMStyleWithColumns(32));
3537 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3538 " aaaaaaaaaaaaaaaa> {};");
3539 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
3540 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
3541 " aaaaaaaaaaaaaaaaaaaaaa> {};");
3542 verifyFormat("template <class R, class C>\n"
3543 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
3544 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
3545 verifyFormat("class ::A::B {};");
3548 TEST_F(FormatTest
, BreakInheritanceStyle
) {
3549 FormatStyle StyleWithInheritanceBreakBeforeComma
= getLLVMStyle();
3550 StyleWithInheritanceBreakBeforeComma
.BreakInheritanceList
=
3551 FormatStyle::BILS_BeforeComma
;
3552 verifyFormat("class MyClass : public X {};",
3553 StyleWithInheritanceBreakBeforeComma
);
3554 verifyFormat("class MyClass\n"
3557 StyleWithInheritanceBreakBeforeComma
);
3558 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n"
3559 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n"
3560 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3561 StyleWithInheritanceBreakBeforeComma
);
3562 verifyFormat("struct aaaaaaaaaaaaa\n"
3563 " : public aaaaaaaaaaaaaaaaaaa< // break\n"
3564 " aaaaaaaaaaaaaaaa> {};",
3565 StyleWithInheritanceBreakBeforeComma
);
3567 FormatStyle StyleWithInheritanceBreakAfterColon
= getLLVMStyle();
3568 StyleWithInheritanceBreakAfterColon
.BreakInheritanceList
=
3569 FormatStyle::BILS_AfterColon
;
3570 verifyFormat("class MyClass : public X {};",
3571 StyleWithInheritanceBreakAfterColon
);
3572 verifyFormat("class MyClass : public X, public Y {};",
3573 StyleWithInheritanceBreakAfterColon
);
3574 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n"
3575 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3576 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};",
3577 StyleWithInheritanceBreakAfterColon
);
3578 verifyFormat("struct aaaaaaaaaaaaa :\n"
3579 " public aaaaaaaaaaaaaaaaaaa< // break\n"
3580 " aaaaaaaaaaaaaaaa> {};",
3581 StyleWithInheritanceBreakAfterColon
);
3583 FormatStyle StyleWithInheritanceBreakAfterComma
= getLLVMStyle();
3584 StyleWithInheritanceBreakAfterComma
.BreakInheritanceList
=
3585 FormatStyle::BILS_AfterComma
;
3586 verifyFormat("class MyClass : public X {};",
3587 StyleWithInheritanceBreakAfterComma
);
3588 verifyFormat("class MyClass : public X,\n"
3590 StyleWithInheritanceBreakAfterComma
);
3592 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
3593 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC "
3595 StyleWithInheritanceBreakAfterComma
);
3596 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
3597 " aaaaaaaaaaaaaaaa> {};",
3598 StyleWithInheritanceBreakAfterComma
);
3599 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
3600 " : public OnceBreak,\n"
3601 " public AlwaysBreak,\n"
3602 " EvenBasesFitInOneLine {};",
3603 StyleWithInheritanceBreakAfterComma
);
3606 TEST_F(FormatTest
, FormatsVariableDeclarationsAfterRecord
) {
3607 verifyFormat("class A {\n} a, b;");
3608 verifyFormat("struct A {\n} a, b;");
3609 verifyFormat("union A {\n} a, b;");
3611 verifyFormat("constexpr class A {\n} a, b;");
3612 verifyFormat("constexpr struct A {\n} a, b;");
3613 verifyFormat("constexpr union A {\n} a, b;");
3615 verifyFormat("namespace {\nclass A {\n} a, b;\n} // namespace");
3616 verifyFormat("namespace {\nstruct A {\n} a, b;\n} // namespace");
3617 verifyFormat("namespace {\nunion A {\n} a, b;\n} // namespace");
3619 verifyFormat("namespace {\nconstexpr class A {\n} a, b;\n} // namespace");
3620 verifyFormat("namespace {\nconstexpr struct A {\n} a, b;\n} // namespace");
3621 verifyFormat("namespace {\nconstexpr union A {\n} a, b;\n} // namespace");
3623 verifyFormat("namespace ns {\n"
3626 "} // namespace ns");
3627 verifyFormat("namespace ns {\n"
3630 "} // namespace ns");
3631 verifyFormat("namespace ns {\n"
3632 "constexpr class C {\n"
3634 "} // namespace ns");
3635 verifyFormat("namespace ns {\n"
3636 "class { /* comment */\n"
3638 "} // namespace ns");
3639 verifyFormat("namespace ns {\n"
3640 "const class { /* comment */\n"
3642 "} // namespace ns");
3645 TEST_F(FormatTest
, FormatsEnum
) {
3646 verifyFormat("enum {\n"
3650 " Three = (One + Two),\n"
3651 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3652 " Five = (One, Two, Three, Four, 5)\n"
3654 verifyGoogleFormat("enum {\n"
3658 " Three = (One + Two),\n"
3659 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3660 " Five = (One, Two, Three, Four, 5)\n"
3662 verifyFormat("enum Enum {};");
3663 verifyFormat("enum {};");
3664 verifyFormat("enum X E {} d;");
3665 verifyFormat("enum __attribute__((...)) E {} d;");
3666 verifyFormat("enum __declspec__((...)) E {} d;");
3667 verifyFormat("enum [[nodiscard]] E {} d;");
3668 verifyFormat("enum {\n"
3669 " Bar = Foo<int, int>::value\n"
3671 getLLVMStyleWithColumns(30));
3673 verifyFormat("enum ShortEnum { A, B, C };");
3674 verifyGoogleFormat("enum ShortEnum { A, B, C };");
3676 verifyFormat("enum KeepEmptyLines {\n"
3683 "enum KeepEmptyLines {\n"
3691 verifyFormat("enum E { // comment\n"
3697 FormatStyle EightIndent
= getLLVMStyle();
3698 EightIndent
.IndentWidth
= 8;
3699 verifyFormat("enum {\n"
3714 verifyFormat("enum [[nodiscard]] E {\n"
3718 verifyFormat("enum [[nodiscard]] E {\n"
3726 verifyFormat("enum X f() {\n"
3730 verifyFormat("enum X Type::f() {\n"
3734 verifyFormat("enum ::X f() {\n"
3738 verifyFormat("enum ns::X f() {\n"
3744 TEST_F(FormatTest
, FormatsEnumsWithErrors
) {
3745 verifyFormat("enum Type {\n"
3746 " One = 0; // These semicolons should be commas.\n"
3749 verifyFormat("namespace n {\n"
3752 " Two, // missing };\n"
3758 TEST_F(FormatTest
, FormatsEnumStruct
) {
3759 verifyFormat("enum struct {\n"
3763 " Three = (One + Two),\n"
3764 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3765 " Five = (One, Two, Three, Four, 5)\n"
3767 verifyFormat("enum struct Enum {};");
3768 verifyFormat("enum struct {};");
3769 verifyFormat("enum struct X E {} d;");
3770 verifyFormat("enum struct __attribute__((...)) E {} d;");
3771 verifyFormat("enum struct __declspec__((...)) E {} d;");
3772 verifyFormat("enum struct [[nodiscard]] E {} d;");
3773 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
3775 verifyFormat("enum struct [[nodiscard]] E {\n"
3779 verifyFormat("enum struct [[nodiscard]] E {\n"
3787 TEST_F(FormatTest
, FormatsEnumClass
) {
3788 verifyFormat("enum class {\n"
3792 " Three = (One + Two),\n"
3793 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
3794 " Five = (One, Two, Three, Four, 5)\n"
3796 verifyFormat("enum class Enum {};");
3797 verifyFormat("enum class {};");
3798 verifyFormat("enum class X E {} d;");
3799 verifyFormat("enum class __attribute__((...)) E {} d;");
3800 verifyFormat("enum class __declspec__((...)) E {} d;");
3801 verifyFormat("enum class [[nodiscard]] E {} d;");
3802 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
3804 verifyFormat("enum class [[nodiscard]] E {\n"
3808 verifyFormat("enum class [[nodiscard]] E {\n"
3816 TEST_F(FormatTest
, FormatsEnumTypes
) {
3817 verifyFormat("enum X : int {\n"
3818 " A, // Force multiple lines.\n"
3821 verifyFormat("enum X : int { A, B };");
3822 verifyFormat("enum X : std::uint32_t { A, B };");
3825 TEST_F(FormatTest
, FormatsTypedefEnum
) {
3826 FormatStyle Style
= getLLVMStyleWithColumns(40);
3827 verifyFormat("typedef enum {} EmptyEnum;");
3828 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3829 verifyFormat("typedef enum {\n"
3836 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
3837 Style
.BraceWrapping
.AfterEnum
= true;
3838 verifyFormat("typedef enum {} EmptyEnum;");
3839 verifyFormat("typedef enum { A, B, C } ShortEnum;");
3840 verifyFormat("typedef enum\n"
3850 TEST_F(FormatTest
, FormatsNSEnums
) {
3851 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
3853 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }");
3854 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
3855 " // Information about someDecentlyLongValue.\n"
3856 " someDecentlyLongValue,\n"
3857 " // Information about anotherDecentlyLongValue.\n"
3858 " anotherDecentlyLongValue,\n"
3859 " // Information about aThirdDecentlyLongValue.\n"
3860 " aThirdDecentlyLongValue\n"
3862 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n"
3863 " // Information about someDecentlyLongValue.\n"
3864 " someDecentlyLongValue,\n"
3865 " // Information about anotherDecentlyLongValue.\n"
3866 " anotherDecentlyLongValue,\n"
3867 " // Information about aThirdDecentlyLongValue.\n"
3868 " aThirdDecentlyLongValue\n"
3870 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
3875 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
3880 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n"
3885 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
3892 TEST_F(FormatTest
, FormatsBitfields
) {
3893 verifyFormat("struct Bitfields {\n"
3894 " unsigned sClass : 8;\n"
3895 " unsigned ValueKind : 2;\n"
3897 verifyFormat("struct A {\n"
3898 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
3899 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
3901 verifyFormat("struct MyStruct {\n"
3907 FormatStyle Style
= getLLVMStyle();
3908 Style
.BitFieldColonSpacing
= FormatStyle::BFCS_None
;
3909 verifyFormat("struct Bitfields {\n"
3910 " unsigned sClass:8;\n"
3911 " unsigned ValueKind:2;\n"
3915 verifyFormat("struct A {\n"
3916 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n"
3917 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n"
3920 Style
.BitFieldColonSpacing
= FormatStyle::BFCS_Before
;
3921 verifyFormat("struct Bitfields {\n"
3922 " unsigned sClass :8;\n"
3923 " unsigned ValueKind :2;\n"
3927 Style
.BitFieldColonSpacing
= FormatStyle::BFCS_After
;
3928 verifyFormat("struct Bitfields {\n"
3929 " unsigned sClass: 8;\n"
3930 " unsigned ValueKind: 2;\n"
3936 TEST_F(FormatTest
, FormatsNamespaces
) {
3937 FormatStyle LLVMWithNoNamespaceFix
= getLLVMStyle();
3938 LLVMWithNoNamespaceFix
.FixNamespaceComments
= false;
3940 verifyFormat("namespace some_namespace {\n"
3942 "void f() { f(); }\n"
3944 LLVMWithNoNamespaceFix
);
3945 verifyFormat("#define M(x) x##x\n"
3946 "namespace M(x) {\n"
3948 "void f() { f(); }\n"
3950 LLVMWithNoNamespaceFix
);
3951 verifyFormat("#define M(x) x##x\n"
3952 "namespace N::inline M(x) {\n"
3954 "void f() { f(); }\n"
3956 LLVMWithNoNamespaceFix
);
3957 verifyFormat("#define M(x) x##x\n"
3958 "namespace M(x)::inline N {\n"
3960 "void f() { f(); }\n"
3962 LLVMWithNoNamespaceFix
);
3963 verifyFormat("#define M(x) x##x\n"
3964 "namespace N::M(x) {\n"
3966 "void f() { f(); }\n"
3968 LLVMWithNoNamespaceFix
);
3969 verifyFormat("#define M(x) x##x\n"
3970 "namespace M::N(x) {\n"
3972 "void f() { f(); }\n"
3974 LLVMWithNoNamespaceFix
);
3975 verifyFormat("namespace N::inline D {\n"
3977 "void f() { f(); }\n"
3979 LLVMWithNoNamespaceFix
);
3980 verifyFormat("namespace N::inline D::E {\n"
3982 "void f() { f(); }\n"
3984 LLVMWithNoNamespaceFix
);
3985 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n"
3987 "void f() { f(); }\n"
3989 LLVMWithNoNamespaceFix
);
3990 verifyFormat("/* something */ namespace some_namespace {\n"
3992 "void f() { f(); }\n"
3994 LLVMWithNoNamespaceFix
);
3995 verifyFormat("namespace {\n"
3997 "void f() { f(); }\n"
3999 LLVMWithNoNamespaceFix
);
4000 verifyFormat("/* something */ namespace {\n"
4002 "void f() { f(); }\n"
4004 LLVMWithNoNamespaceFix
);
4005 verifyFormat("inline namespace X {\n"
4007 "void f() { f(); }\n"
4009 LLVMWithNoNamespaceFix
);
4010 verifyFormat("/* something */ inline namespace X {\n"
4012 "void f() { f(); }\n"
4014 LLVMWithNoNamespaceFix
);
4015 verifyFormat("export namespace X {\n"
4017 "void f() { f(); }\n"
4019 LLVMWithNoNamespaceFix
);
4020 verifyFormat("using namespace some_namespace;\n"
4022 "void f() { f(); }",
4023 LLVMWithNoNamespaceFix
);
4025 // This code is more common than we thought; if we
4026 // layout this correctly the semicolon will go into
4027 // its own line, which is undesirable.
4028 verifyFormat("namespace {};", LLVMWithNoNamespaceFix
);
4029 verifyFormat("namespace {\n"
4032 LLVMWithNoNamespaceFix
);
4034 verifyFormat("namespace {\n"
4035 "int SomeVariable = 0; // comment\n"
4037 LLVMWithNoNamespaceFix
);
4038 verifyFormat("#ifndef HEADER_GUARD\n"
4039 "#define HEADER_GUARD\n"
4040 "namespace my_namespace {\n"
4042 "} // my_namespace\n"
4043 "#endif // HEADER_GUARD",
4044 "#ifndef HEADER_GUARD\n"
4045 " #define HEADER_GUARD\n"
4046 " namespace my_namespace {\n"
4048 "} // my_namespace\n"
4049 "#endif // HEADER_GUARD",
4050 LLVMWithNoNamespaceFix
);
4052 verifyFormat("namespace A::B {\n"
4055 LLVMWithNoNamespaceFix
);
4057 FormatStyle Style
= getLLVMStyle();
4058 Style
.NamespaceIndentation
= FormatStyle::NI_All
;
4059 verifyFormat("namespace out {\n"
4063 " } // namespace in\n"
4064 "} // namespace out",
4069 "} // namespace in\n"
4070 "} // namespace out",
4073 FormatStyle ShortInlineFunctions
= getLLVMStyle();
4074 ShortInlineFunctions
.NamespaceIndentation
= FormatStyle::NI_All
;
4075 ShortInlineFunctions
.AllowShortFunctionsOnASingleLine
=
4076 FormatStyle::SFS_Inline
;
4077 verifyFormat("namespace {\n"
4082 ShortInlineFunctions
);
4083 verifyFormat("namespace { /* comment */\n"
4088 ShortInlineFunctions
);
4089 verifyFormat("namespace { // comment\n"
4094 ShortInlineFunctions
);
4095 verifyFormat("namespace {\n"
4101 ShortInlineFunctions
);
4102 verifyFormat("namespace interface {\n"
4106 "} // namespace interface",
4107 ShortInlineFunctions
);
4108 verifyFormat("namespace {\n"
4110 " void f() { return; }\n"
4113 ShortInlineFunctions
);
4114 verifyFormat("namespace {\n"
4115 " class X { /* comment */\n"
4116 " void f() { return; }\n"
4119 ShortInlineFunctions
);
4120 verifyFormat("namespace {\n"
4121 " class X { // comment\n"
4122 " void f() { return; }\n"
4125 ShortInlineFunctions
);
4126 verifyFormat("namespace {\n"
4128 " void f() { return; }\n"
4131 ShortInlineFunctions
);
4132 verifyFormat("namespace {\n"
4134 " void f() { return; }\n"
4137 ShortInlineFunctions
);
4138 verifyFormat("extern \"C\" {\n"
4143 ShortInlineFunctions
);
4144 verifyFormat("namespace {\n"
4146 " void f() { return; }\n"
4149 ShortInlineFunctions
);
4150 verifyFormat("namespace {\n"
4151 " [[nodiscard]] class X {\n"
4152 " void f() { return; }\n"
4155 ShortInlineFunctions
);
4156 verifyFormat("namespace {\n"
4157 " static class X {\n"
4158 " void f() { return; }\n"
4161 ShortInlineFunctions
);
4162 verifyFormat("namespace {\n"
4163 " constexpr class X {\n"
4164 " void f() { return; }\n"
4167 ShortInlineFunctions
);
4169 ShortInlineFunctions
.IndentExternBlock
= FormatStyle::IEBS_Indent
;
4170 verifyFormat("extern \"C\" {\n"
4175 ShortInlineFunctions
);
4177 Style
.NamespaceIndentation
= FormatStyle::NI_Inner
;
4178 verifyFormat("namespace out {\n"
4182 "} // namespace in\n"
4183 "} // namespace out",
4188 "} // namespace in\n"
4189 "} // namespace out",
4192 Style
.NamespaceIndentation
= FormatStyle::NI_None
;
4193 verifyFormat("template <class T>\n"
4194 "concept a_concept = X<>;\n"
4196 "struct b_struct {};\n"
4199 verifyFormat("template <int I>\n"
4200 "constexpr void foo()\n"
4201 " requires(I == 42)\n"
4205 "} // namespace ns",
4208 FormatStyle LLVMWithCompactInnerNamespace
= getLLVMStyle();
4209 LLVMWithCompactInnerNamespace
.CompactNamespaces
= true;
4210 LLVMWithCompactInnerNamespace
.NamespaceIndentation
= FormatStyle::NI_Inner
;
4211 verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n"
4212 "// block for debug mode\n"
4215 "}}} // namespace ns1::ns2::ns3",
4216 LLVMWithCompactInnerNamespace
);
4219 TEST_F(FormatTest
, NamespaceMacros
) {
4220 FormatStyle Style
= getLLVMStyle();
4221 Style
.NamespaceMacros
.push_back("TESTSUITE");
4223 verifyFormat("TESTSUITE(A) {\n"
4225 "} // TESTSUITE(A)",
4228 verifyFormat("TESTSUITE(A, B) {\n"
4230 "} // TESTSUITE(A)",
4233 // Properly indent according to NamespaceIndentation style
4234 Style
.NamespaceIndentation
= FormatStyle::NI_All
;
4235 verifyFormat("TESTSUITE(A) {\n"
4237 "} // TESTSUITE(A)",
4239 verifyFormat("TESTSUITE(A) {\n"
4242 " } // namespace B\n"
4243 "} // TESTSUITE(A)",
4245 verifyFormat("namespace A {\n"
4248 " } // TESTSUITE(B)\n"
4252 Style
.NamespaceIndentation
= FormatStyle::NI_Inner
;
4253 verifyFormat("TESTSUITE(A) {\n"
4256 "} // TESTSUITE(B)\n"
4257 "} // TESTSUITE(A)",
4259 verifyFormat("TESTSUITE(A) {\n"
4262 "} // namespace B\n"
4263 "} // TESTSUITE(A)",
4265 verifyFormat("namespace A {\n"
4268 "} // TESTSUITE(B)\n"
4272 // Properly merge namespace-macros blocks in CompactNamespaces mode
4273 Style
.NamespaceIndentation
= FormatStyle::NI_None
;
4274 Style
.CompactNamespaces
= true;
4275 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n"
4276 "}} // TESTSUITE(A::B)",
4279 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4280 "}} // TESTSUITE(out::in)",
4281 "TESTSUITE(out) {\n"
4283 "} // TESTSUITE(in)\n"
4284 "} // TESTSUITE(out)",
4287 verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n"
4288 "}} // TESTSUITE(out::in)",
4289 "TESTSUITE(out) {\n"
4291 "} // TESTSUITE(in)\n"
4292 "} // TESTSUITE(out)",
4295 // Do not merge different namespaces/macros
4296 verifyFormat("namespace out {\n"
4298 "} // TESTSUITE(in)\n"
4299 "} // namespace out",
4301 verifyFormat("TESTSUITE(out) {\n"
4303 "} // namespace in\n"
4304 "} // TESTSUITE(out)",
4306 Style
.NamespaceMacros
.push_back("FOOBAR");
4307 verifyFormat("TESTSUITE(out) {\n"
4310 "} // TESTSUITE(out)",
4314 TEST_F(FormatTest
, FormatsCompactNamespaces
) {
4315 FormatStyle Style
= getLLVMStyle();
4316 Style
.CompactNamespaces
= true;
4317 Style
.NamespaceMacros
.push_back("TESTSUITE");
4319 verifyFormat("namespace A { namespace B {\n"
4320 "}} // namespace A::B",
4323 verifyFormat("namespace out { namespace in {\n"
4324 "}} // namespace out::in",
4327 "} // namespace in\n"
4328 "} // namespace out",
4331 // Only namespaces which have both consecutive opening and end get compacted
4332 verifyFormat("namespace out {\n"
4334 "} // namespace in1\n"
4336 "} // namespace in2\n"
4337 "} // namespace out",
4340 verifyFormat("namespace out {\n"
4344 "} // namespace in\n"
4346 "} // namespace out",
4347 "namespace out { int i;\n"
4348 "namespace in { int j; } // namespace in\n"
4349 "int k; } // namespace out",
4352 verifyFormat("namespace A { namespace B { namespace C {\n"
4353 "}}} // namespace A::B::C",
4354 "namespace A { namespace B {\n"
4356 "}} // namespace B::C\n"
4360 Style
.ColumnLimit
= 40;
4361 verifyFormat("namespace aaaaaaaaaa {\n"
4362 "namespace bbbbbbbbbb {\n"
4363 "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
4364 "namespace aaaaaaaaaa {\n"
4365 "namespace bbbbbbbbbb {\n"
4366 "} // namespace bbbbbbbbbb\n"
4367 "} // namespace aaaaaaaaaa",
4370 verifyFormat("namespace aaaaaa { namespace bbbbbb {\n"
4371 "namespace cccccc {\n"
4372 "}}} // namespace aaaaaa::bbbbbb::cccccc",
4373 "namespace aaaaaa {\n"
4374 "namespace bbbbbb {\n"
4375 "namespace cccccc {\n"
4376 "} // namespace cccccc\n"
4377 "} // namespace bbbbbb\n"
4378 "} // namespace aaaaaa",
4380 Style
.ColumnLimit
= 80;
4382 // Extra semicolon after 'inner' closing brace prevents merging
4383 verifyFormat("namespace out { namespace in {\n"
4384 "}; } // namespace out::in",
4387 "}; // namespace in\n"
4388 "} // namespace out",
4391 // Extra semicolon after 'outer' closing brace is conserved
4392 verifyFormat("namespace out { namespace in {\n"
4393 "}}; // namespace out::in",
4396 "} // namespace in\n"
4397 "}; // namespace out",
4400 Style
.NamespaceIndentation
= FormatStyle::NI_All
;
4401 verifyFormat("namespace out { namespace in {\n"
4403 "}} // namespace out::in",
4407 "} // namespace in\n"
4408 "} // namespace out",
4410 verifyFormat("namespace out { namespace mid {\n"
4413 " } // namespace in\n"
4415 "}} // namespace out::mid",
4416 "namespace out { namespace mid {\n"
4417 "namespace in { int j; } // namespace in\n"
4418 "int k; }} // namespace out::mid",
4421 verifyFormat("namespace A { namespace B { namespace C {\n"
4423 "}}} // namespace A::B::C\n"
4428 "namespace A { namespace B {\n"
4431 "}} // namespace B::C\n"
4432 "} // namespace A\n"
4439 verifyFormat("namespace A { namespace B { namespace C {\n"
4443 "}}} // namespace A::B::C\n"
4448 "namespace A { namespace B {\n"
4453 "}} // namespace B::C\n"
4454 "} // namespace A\n"
4461 Style
.NamespaceIndentation
= FormatStyle::NI_Inner
;
4462 verifyFormat("namespace out { namespace in {\n"
4464 "}} // namespace out::in",
4468 "} // namespace in\n"
4469 "} // namespace out",
4471 verifyFormat("namespace out { namespace mid { namespace in {\n"
4473 "}}} // namespace out::mid::in",
4478 "} // namespace in\n"
4479 "} // namespace mid\n"
4480 "} // namespace out",
4483 Style
.CompactNamespaces
= true;
4484 Style
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_None
;
4485 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
4486 Style
.BraceWrapping
.BeforeLambdaBody
= true;
4487 verifyFormat("namespace out { namespace in {\n"
4488 "}} // namespace out::in",
4490 verifyFormat("namespace out { namespace in {\n"
4491 "}} // namespace out::in",
4494 "} // namespace in\n"
4495 "} // namespace out",
4499 TEST_F(FormatTest
, FormatsExternC
) {
4500 verifyFormat("extern \"C\" {\nint a;");
4501 verifyFormat("extern \"C\" {}");
4502 verifyFormat("extern \"C\" {\n"
4505 verifyFormat("extern \"C\" int foo() {}");
4506 verifyFormat("extern \"C\" int foo();");
4507 verifyFormat("extern \"C\" int foo() {\n"
4512 FormatStyle Style
= getLLVMStyle();
4513 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
4514 Style
.BraceWrapping
.AfterFunction
= true;
4515 verifyFormat("extern \"C\" int foo() {}", Style
);
4516 verifyFormat("extern \"C\" int foo();", Style
);
4517 verifyFormat("extern \"C\" int foo()\n"
4524 Style
.BraceWrapping
.AfterExternBlock
= true;
4525 Style
.BraceWrapping
.SplitEmptyRecord
= false;
4526 verifyFormat("extern \"C\"\n"
4529 verifyFormat("extern \"C\"\n"
4536 TEST_F(FormatTest
, IndentExternBlockStyle
) {
4537 FormatStyle Style
= getLLVMStyle();
4538 Style
.IndentWidth
= 2;
4540 Style
.IndentExternBlock
= FormatStyle::IEBS_Indent
;
4541 verifyFormat("extern \"C\" { /*9*/\n"
4544 verifyFormat("extern \"C\" {\n"
4549 Style
.IndentExternBlock
= FormatStyle::IEBS_NoIndent
;
4550 verifyFormat("extern \"C\" { /*11*/\n"
4553 verifyFormat("extern \"C\" {\n"
4558 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
4559 Style
.BraceWrapping
.AfterExternBlock
= true;
4560 Style
.IndentExternBlock
= FormatStyle::IEBS_Indent
;
4561 verifyFormat("extern \"C\"\n"
4565 verifyFormat("extern \"C\"\n{\n"
4570 Style
.BraceWrapping
.AfterExternBlock
= false;
4571 Style
.IndentExternBlock
= FormatStyle::IEBS_NoIndent
;
4572 verifyFormat("extern \"C\" { /*15*/\n"
4575 verifyFormat("extern \"C\" {\n"
4580 Style
.BraceWrapping
.AfterExternBlock
= true;
4581 verifyFormat("extern \"C\"\n"
4585 verifyFormat("extern \"C\"\n"
4591 Style
.IndentExternBlock
= FormatStyle::IEBS_Indent
;
4592 verifyFormat("extern \"C\"\n"
4596 verifyFormat("extern \"C\"\n"
4603 TEST_F(FormatTest
, FormatsInlineASM
) {
4604 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
4605 verifyFormat("asm(\"nop\" ::: \"memory\");");
4607 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
4608 " \"cpuid\\n\\t\"\n"
4609 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
4610 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
4611 " : \"a\"(value));");
4613 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4615 " mov edx,[that] // vtable in edx\n"
4616 " mov eax,methodIndex\n"
4617 " call [edx][eax*4] // stdcall\n"
4620 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
4622 " mov edx,[that] // vtable in edx\n"
4623 " mov eax,methodIndex\n"
4624 " call [edx][eax*4] // stdcall\n"
4627 verifyNoChange("_asm {\n"
4631 verifyFormat("void function() {\n"
4635 verifyFormat("__asm {\n"
4642 auto Style
= getLLVMStyleWithColumns(0);
4643 const StringRef Code1
{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"};
4644 const StringRef Code2
{"asm(\"xyz\"\n"
4645 " : \"=a\"(a), \"=d\"(b)\n"
4646 " : \"a\"(data));"};
4647 const StringRef Code3
{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b)\n"
4648 " : \"a\"(data));"};
4650 Style
.BreakBeforeInlineASMColon
= FormatStyle::BBIAS_OnlyMultiline
;
4651 verifyFormat(Code1
, Style
);
4652 verifyNoChange(Code2
, Style
);
4653 verifyNoChange(Code3
, Style
);
4655 Style
.BreakBeforeInlineASMColon
= FormatStyle::BBIAS_Always
;
4656 verifyFormat(Code2
, Code1
, Style
);
4657 verifyNoChange(Code2
, Style
);
4658 verifyFormat(Code2
, Code3
, Style
);
4661 TEST_F(FormatTest
, FormatTryCatch
) {
4662 verifyFormat("try {\n"
4664 "} catch (int a) {\n"
4670 // Function-level try statements.
4671 verifyFormat("int f() try { return 4; } catch (...) {\n"
4674 verifyFormat("class A {\n"
4676 " A() try : a(0) {\n"
4677 " } catch (...) {\n"
4681 verifyFormat("class A {\n"
4683 " A() try : a(0), b{1} {\n"
4684 " } catch (...) {\n"
4688 verifyFormat("class A {\n"
4690 " A() try : a(0), b{1}, c{2} {\n"
4691 " } catch (...) {\n"
4695 verifyFormat("class A {\n"
4697 " A() try : a(0), b{1}, c{2} {\n"
4698 " { // New scope.\n"
4700 " } catch (...) {\n"
4705 // Incomplete try-catch blocks.
4706 verifyIncompleteFormat("try {} catch (");
4709 TEST_F(FormatTest
, FormatTryAsAVariable
) {
4710 verifyFormat("int try;");
4711 verifyFormat("int try, size;");
4712 verifyFormat("try = foo();");
4713 verifyFormat("if (try < size) {\n return true;\n}");
4715 verifyFormat("int catch;");
4716 verifyFormat("int catch, size;");
4717 verifyFormat("catch = foo();");
4718 verifyFormat("if (catch < size) {\n return true;\n}");
4720 FormatStyle Style
= getLLVMStyle();
4721 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
4722 Style
.BraceWrapping
.AfterFunction
= true;
4723 Style
.BraceWrapping
.BeforeCatch
= true;
4724 verifyFormat("try {\n"
4731 verifyFormat("#if NO_EX\n"
4740 verifyFormat("try /* abc */ {\n"
4747 verifyFormat("try\n"
4758 TEST_F(FormatTest
, FormatSEHTryCatch
) {
4759 verifyFormat("__try {\n"
4761 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
4765 verifyFormat("__try {\n"
4771 verifyFormat("DEBUG({\n"
4778 TEST_F(FormatTest
, IncompleteTryCatchBlocks
) {
4779 verifyFormat("try {\n"
4784 verifyFormat("try {\n"
4786 "} catch (A a) MACRO(x) {\n"
4788 "} catch (B b) MACRO(x) {\n"
4793 TEST_F(FormatTest
, FormatTryCatchBraceStyles
) {
4794 FormatStyle Style
= getLLVMStyle();
4795 for (auto BraceStyle
: {FormatStyle::BS_Attach
, FormatStyle::BS_Mozilla
,
4796 FormatStyle::BS_WebKit
}) {
4797 Style
.BreakBeforeBraces
= BraceStyle
;
4798 verifyFormat("try {\n"
4805 Style
.BreakBeforeBraces
= FormatStyle::BS_Stroustrup
;
4806 verifyFormat("try {\n"
4813 verifyFormat("__try {\n"
4820 verifyFormat("@try {\n"
4827 Style
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
4828 verifyFormat("try\n"
4837 Style
.BreakBeforeBraces
= FormatStyle::BS_Whitesmiths
;
4838 verifyFormat("try\n"
4840 " // something white\n"
4844 " // something white\n"
4847 Style
.BreakBeforeBraces
= FormatStyle::BS_GNU
;
4848 verifyFormat("try\n"
4857 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
4858 Style
.BraceWrapping
.BeforeCatch
= true;
4859 verifyFormat("try {\n"
4868 TEST_F(FormatTest
, StaticInitializers
) {
4869 verifyFormat("static SomeClass SC = {1, 'a'};");
4871 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
4873 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
4875 // Here, everything other than the "}" would fit on a line.
4876 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
4877 " 10000000000000000000000000};");
4878 verifyFormat("S s = {a,\n"
4887 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
4888 // line. However, the formatting looks a bit off and this probably doesn't
4889 // happen often in practice.
4890 verifyFormat("static int Variable[1] = {\n"
4891 " {1000000000000000000000000000000000000}};",
4892 getLLVMStyleWithColumns(40));
4895 TEST_F(FormatTest
, DesignatedInitializers
) {
4896 verifyFormat("const struct A a = {.a = 1, .b = 2};");
4897 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
4898 " .bbbbbbbbbb = 2,\n"
4899 " .cccccccccc = 3,\n"
4900 " .dddddddddd = 4,\n"
4901 " .eeeeeeeeee = 5};");
4902 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4903 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
4904 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
4905 " .ccccccccccccccccccccccccccc = 3,\n"
4906 " .ddddddddddddddddddddddddddd = 4,\n"
4907 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
4909 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
4911 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
4912 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
4913 " [2] = bbbbbbbbbb,\n"
4914 " [3] = cccccccccc,\n"
4915 " [4] = dddddddddd,\n"
4916 " [5] = eeeeeeeeee};");
4917 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
4918 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4919 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
4920 " [3] = cccccccccccccccccccccccccccccccccccccc,\n"
4921 " [4] = dddddddddddddddddddddddddddddddddddddd,\n"
4922 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
4925 TEST_F(FormatTest
, BracedInitializerIndentWidth
) {
4926 auto Style
= getLLVMStyleWithColumns(60);
4927 Style
.BinPackArguments
= true;
4928 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
4929 Style
.BracedInitializerIndentWidth
= 6;
4931 // Non-initializing braces are unaffected by BracedInitializerIndentWidth.
4932 verifyFormat("enum class {\n"
4937 verifyFormat("class Foo {\n"
4942 verifyFormat("void foo() {\n"
4943 " auto bar = baz;\n"
4947 verifyFormat("auto foo = [&] {\n"
4948 " auto bar = baz;\n"
4953 " auto bar = baz;\n"
4957 // Non-brace initialization is unaffected by BracedInitializerIndentWidth.
4958 verifyFormat("SomeClass clazz(\n"
4959 " \"xxxxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyyyy\",\n"
4960 " \"zzzzzzzzzzzzzzzzzz\");",
4963 // The following types of initialization are all affected by
4964 // BracedInitializerIndentWidth. Aggregate initialization.
4965 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
4966 " 10000000, 20000000};",
4968 verifyFormat("SomeStruct s{\n"
4969 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
4970 " \"zzzzzzzzzzzzzzzz\"};",
4972 // Designated initializers.
4973 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
4974 " [0] = 10000000, [1] = 20000000};",
4976 verifyFormat("SomeStruct s{\n"
4977 " .foo = \"xxxxxxxxxxxxx\",\n"
4978 " .bar = \"yyyyyyyyyyyyy\",\n"
4979 " .baz = \"zzzzzzzzzzzzz\"};",
4981 // List initialization.
4982 verifyFormat("SomeStruct s{\n"
4983 " \"xxxxxxxxxxxxx\",\n"
4984 " \"yyyyyyyyyyyyy\",\n"
4985 " \"zzzzzzzzzzzzz\",\n"
4988 verifyFormat("SomeStruct{\n"
4989 " \"xxxxxxxxxxxxx\",\n"
4990 " \"yyyyyyyyyyyyy\",\n"
4991 " \"zzzzzzzzzzzzz\",\n"
4994 verifyFormat("new SomeStruct{\n"
4995 " \"xxxxxxxxxxxxx\",\n"
4996 " \"yyyyyyyyyyyyy\",\n"
4997 " \"zzzzzzzzzzzzz\",\n"
5000 // Member initializer.
5001 verifyFormat("class SomeClass {\n"
5003 " \"xxxxxxxxxxxxx\",\n"
5004 " \"yyyyyyyyyyyyy\",\n"
5005 " \"zzzzzzzzzzzzz\",\n"
5009 // Constructor member initializer.
5010 verifyFormat("SomeClass::SomeClass : strct{\n"
5011 " \"xxxxxxxxxxxxx\",\n"
5012 " \"yyyyyyyyyyyyy\",\n"
5013 " \"zzzzzzzzzzzzz\",\n"
5016 // Copy initialization.
5017 verifyFormat("SomeStruct s = SomeStruct{\n"
5018 " \"xxxxxxxxxxxxx\",\n"
5019 " \"yyyyyyyyyyyyy\",\n"
5020 " \"zzzzzzzzzzzzz\",\n"
5023 // Copy list initialization.
5024 verifyFormat("SomeStruct s = {\n"
5025 " \"xxxxxxxxxxxxx\",\n"
5026 " \"yyyyyyyyyyyyy\",\n"
5027 " \"zzzzzzzzzzzzz\",\n"
5030 // Assignment operand initialization.
5031 verifyFormat("s = {\n"
5032 " \"xxxxxxxxxxxxx\",\n"
5033 " \"yyyyyyyyyyyyy\",\n"
5034 " \"zzzzzzzzzzzzz\",\n"
5037 // Returned object initialization.
5038 verifyFormat("return {\n"
5039 " \"xxxxxxxxxxxxx\",\n"
5040 " \"yyyyyyyyyyyyy\",\n"
5041 " \"zzzzzzzzzzzzz\",\n"
5044 // Initializer list.
5045 verifyFormat("auto initializerList = {\n"
5046 " \"xxxxxxxxxxxxx\",\n"
5047 " \"yyyyyyyyyyyyy\",\n"
5048 " \"zzzzzzzzzzzzz\",\n"
5051 // Function parameter initialization.
5052 verifyFormat("func({\n"
5053 " \"xxxxxxxxxxxxx\",\n"
5054 " \"yyyyyyyyyyyyy\",\n"
5055 " \"zzzzzzzzzzzzz\",\n"
5058 // Nested init lists.
5059 verifyFormat("SomeStruct s = {\n"
5060 " {{init1, init2, init3, init4, init5},\n"
5061 " {init1, init2, init3, init4, init5}}};",
5063 verifyFormat("SomeStruct s = {\n"
5071 " {init1, init2, init3, init4, init5}}};",
5073 verifyFormat("SomeArrayT a[3] = {\n"
5085 verifyFormat("SomeArrayT a[3] = {\n"
5103 // Aligning after open braces unaffected by BracedInitializerIndentWidth.
5104 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_Align
;
5105 verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n"
5106 " \"zzzzzzzzzzzzz\"};",
5110 TEST_F(FormatTest
, NestedStaticInitializers
) {
5111 verifyFormat("static A x = {{{}}};");
5112 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
5113 " {init1, init2, init3, init4}}};",
5114 getLLVMStyleWithColumns(50));
5116 verifyFormat("somes Status::global_reps[3] = {\n"
5117 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5118 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5119 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
5120 getLLVMStyleWithColumns(60));
5121 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
5122 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
5123 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
5124 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
5125 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
5126 " {rect.fRight - rect.fLeft, rect.fBottom - "
5130 "SomeArrayOfSomeType a = {\n"
5133 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
5134 " 333333333333333333333333333333},\n"
5138 "SomeArrayOfSomeType a = {\n"
5141 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
5142 " 333333333333333333333333333333}},\n"
5146 verifyFormat("struct {\n"
5148 " const char *const name;\n"
5149 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
5150 " {kOsWin, \"Windows\"},\n"
5151 " {kOsLinux, \"Linux\"},\n"
5152 " {kOsCrOS, \"Chrome OS\"}};");
5153 verifyFormat("struct {\n"
5155 " const char *const name;\n"
5156 "} kBitsToOs[] = {\n"
5157 " {kOsMac, \"Mac\"},\n"
5158 " {kOsWin, \"Windows\"},\n"
5159 " {kOsLinux, \"Linux\"},\n"
5160 " {kOsCrOS, \"Chrome OS\"},\n"
5164 TEST_F(FormatTest
, FormatsSmallMacroDefinitionsInSingleLine
) {
5165 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
5167 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
5170 TEST_F(FormatTest
, DoesNotBreakPureVirtualFunctionDefinition
) {
5171 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
5172 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
5174 // Do break defaulted and deleted functions.
5175 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5177 getLLVMStyleWithColumns(40));
5178 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
5180 getLLVMStyleWithColumns(40));
5183 TEST_F(FormatTest
, BreaksStringLiteralsOnlyInDefine
) {
5184 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
5185 getLLVMStyleWithColumns(40));
5186 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5187 getLLVMStyleWithColumns(40));
5188 verifyFormat("#define Q \\\n"
5189 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
5190 " \"aaaaaaaa.cpp\"",
5191 "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
5192 getLLVMStyleWithColumns(40));
5195 TEST_F(FormatTest
, UnderstandsLinePPDirective
) {
5196 verifyFormat("# 123 \"A string literal\"",
5197 " # 123 \"A string literal\"");
5200 TEST_F(FormatTest
, LayoutUnknownPPDirective
) {
5202 verifyFormat("#\n;\n;\n;");
5205 TEST_F(FormatTest
, UnescapedEndOfLineEndsPPDirective
) {
5206 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5207 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B",
5208 getLLVMStyleWithColumns(12));
5211 TEST_F(FormatTest
, EndOfFileEndsPPDirective
) {
5212 verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\"");
5213 verifyFormat("#define A B", "# \\\n define \\\n A \\\n B");
5216 TEST_F(FormatTest
, DoesntRemoveUnknownTokens
) {
5217 verifyFormat("#define A \\x20");
5218 verifyFormat("#define A \\ x20");
5219 verifyFormat("#define A \\ x20", "#define A \\ x20");
5220 verifyFormat("#define A ''");
5221 verifyFormat("#define A ''qqq");
5222 verifyFormat("#define A `qqq");
5223 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
5224 verifyFormat("const char *c = STRINGIFY(\n"
5226 "const char * c = STRINGIFY(\n"
5229 verifyFormat("a\r\\");
5230 verifyFormat("a\v\\");
5231 verifyFormat("a\f\\");
5234 TEST_F(FormatTest
, IndentsPPDirectiveWithPPIndentWidth
) {
5235 FormatStyle style
= getChromiumStyle(FormatStyle::LK_Cpp
);
5236 style
.IndentWidth
= 4;
5237 style
.PPIndentWidth
= 1;
5239 style
.IndentPPDirectives
= FormatStyle::PPDIS_None
;
5240 verifyFormat("#ifdef __linux__\n"
5251 style
.IndentPPDirectives
= FormatStyle::PPDIS_AfterHash
;
5252 verifyFormat("#ifdef __linux__\n"
5256 "# define FOO foo\n"
5263 style
.IndentPPDirectives
= FormatStyle::PPDIS_BeforeHash
;
5264 verifyFormat("#ifdef __linux__\n"
5268 " #define FOO foo\n"
5274 verifyFormat("#if 1\n"
5275 " // some comments\n"
5278 "// not a define comment\n"
5284 "// some comments\n"
5287 "// not a define comment\n"
5294 style
.IndentPPDirectives
= FormatStyle::PPDIS_None
;
5295 verifyFormat("#ifdef foo\n"
5296 "#define bar() \\\n"
5303 verifyFormat("if (emacs) {\n"
5307 " return duh(); \\\n"
5312 verifyFormat("#if abc\n"
5314 "#define bar() \\\n"
5324 verifyFormat("#ifndef foo\n"
5330 " return duh(); \\\n"
5336 verifyFormat("#if 1\n"
5344 verifyFormat("#define X \\\n"
5351 style
.PPIndentWidth
= 2;
5352 verifyFormat("#ifdef foo\n"
5353 "#define bar() \\\n"
5360 style
.IndentWidth
= 8;
5361 verifyFormat("#ifdef foo\n"
5362 "#define bar() \\\n"
5370 style
.IndentWidth
= 1;
5371 style
.PPIndentWidth
= 4;
5372 verifyFormat("#if 1\n"
5380 verifyFormat("#define X \\\n"
5387 style
.IndentWidth
= 4;
5388 style
.PPIndentWidth
= 1;
5389 style
.IndentPPDirectives
= FormatStyle::PPDIS_AfterHash
;
5390 verifyFormat("#ifdef foo\n"
5391 "# define bar() \\\n"
5398 verifyFormat("#if abc\n"
5400 "# define bar() \\\n"
5410 verifyFormat("#ifndef foo\n"
5416 " return duh(); \\\n"
5422 verifyFormat("#define X \\\n"
5429 style
.PPIndentWidth
= 2;
5430 style
.IndentWidth
= 8;
5431 verifyFormat("#ifdef foo\n"
5432 "# define bar() \\\n"
5440 style
.PPIndentWidth
= 4;
5441 style
.IndentWidth
= 1;
5442 verifyFormat("#define X \\\n"
5449 style
.IndentWidth
= 4;
5450 style
.PPIndentWidth
= 1;
5451 style
.IndentPPDirectives
= FormatStyle::PPDIS_BeforeHash
;
5452 verifyFormat("if (emacs) {\n"
5456 " return duh(); \\\n"
5461 verifyFormat("#if abc\n"
5463 " #define bar() \\\n"
5471 verifyFormat("#if 1\n"
5480 style
.PPIndentWidth
= 2;
5481 verifyFormat("#ifdef foo\n"
5482 " #define bar() \\\n"
5490 style
.PPIndentWidth
= 4;
5491 style
.IndentWidth
= 1;
5492 verifyFormat("#if 1\n"
5502 TEST_F(FormatTest
, IndentsPPDirectiveInReducedSpace
) {
5503 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
5504 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
5505 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
5506 // FIXME: We never break before the macro name.
5507 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
5509 verifyFormat("#define A A\n#define A A");
5510 verifyFormat("#define A(X) A\n#define A A");
5512 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
5513 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
5516 TEST_F(FormatTest
, HandlePreprocessorDirectiveContext
) {
5517 verifyFormat("// somecomment\n"
5518 "#include \"a.h\"\n"
5521 "#include \"b.h\"\n"
5524 " #include \"a.h\"\n"
5527 " #include \"b.h\"\n"
5529 getLLVMStyleWithColumns(13));
5532 TEST_F(FormatTest
, LayoutSingleHash
) { verifyFormat("#\na;"); }
5534 TEST_F(FormatTest
, LayoutCodeInMacroDefinitions
) {
5535 verifyFormat("#define A \\\n"
5541 getLLVMStyleWithColumns(14));
5544 TEST_F(FormatTest
, LayoutRemainingTokens
) { verifyFormat("{}"); }
5546 TEST_F(FormatTest
, MacroDefinitionInsideStatement
) {
5547 verifyFormat("int x,\n"
5550 "int x,\n#define A\ny;");
5553 TEST_F(FormatTest
, HashInMacroDefinition
) {
5554 verifyFormat("#define A(c) L#c");
5555 verifyFormat("#define A(c) u#c");
5556 verifyFormat("#define A(c) U#c");
5557 verifyFormat("#define A(c) u8#c");
5558 verifyFormat("#define A(c) LR#c");
5559 verifyFormat("#define A(c) uR#c");
5560 verifyFormat("#define A(c) UR#c");
5561 verifyFormat("#define A(c) u8R#c");
5562 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
5563 verifyFormat("#define A \\\n"
5567 getLLVMStyleWithColumns(11));
5569 verifyFormat("#define A(X) \\\n"
5570 " void function##X()",
5571 getLLVMStyleWithColumns(22));
5573 verifyFormat("#define A(a, b, c) \\\n"
5575 getLLVMStyleWithColumns(22));
5577 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
5580 TEST_F(FormatTest
, RespectWhitespaceInMacroDefinitions
) {
5581 verifyFormat("#define A (x)");
5582 verifyFormat("#define A(x)");
5584 FormatStyle Style
= getLLVMStyle();
5585 Style
.SpaceBeforeParens
= FormatStyle::SBPO_Never
;
5586 verifyFormat("#define true ((foo)1)", Style
);
5587 Style
.SpaceBeforeParens
= FormatStyle::SBPO_Always
;
5588 verifyFormat("#define false((foo)0)", Style
);
5591 TEST_F(FormatTest
, EmptyLinesInMacroDefinitions
) {
5592 verifyFormat("#define A b;",
5596 getLLVMStyleWithColumns(25));
5597 verifyNoChange("#define A \\\n"
5601 getLLVMStyleWithColumns(11));
5602 verifyNoChange("#define A \\\n"
5606 getLLVMStyleWithColumns(11));
5609 TEST_F(FormatTest
, MacroDefinitionsWithIncompleteCode
) {
5610 verifyIncompleteFormat("#define A :");
5611 verifyFormat("#define SOMECASES \\\n"
5614 getLLVMStyleWithColumns(20));
5615 verifyFormat("#define MACRO(a) \\\n"
5620 getLLVMStyleWithColumns(18));
5621 verifyFormat("#define A template <typename T>");
5622 verifyIncompleteFormat("#define STR(x) #x\n"
5623 "f(STR(this_is_a_string_literal{));");
5624 verifyFormat("#pragma omp threadprivate( \\\n"
5625 " y)), // expected-warning",
5626 getLLVMStyleWithColumns(28));
5627 verifyFormat("#d, = };");
5628 verifyFormat("#if \"a");
5629 verifyIncompleteFormat("({\n"
5634 getLLVMStyleWithColumns(15));
5635 verifyFormat("#define A \\\n"
5641 getLLVMStyleWithColumns(15));
5642 verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
5643 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
5644 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
5645 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
5646 verifyNoCrash("#else\n"
5650 verifyNoCrash("#else\n"
5654 verifyNoCrash("#else\n"
5658 verifyNoCrash("#if X\n"
5663 verifyNoCrash("#if X\n"
5668 verifyNoCrash("#endif\n"
5670 verifyNoCrash("#endif\n"
5672 verifyNoCrash("#endif\n"
5676 TEST_F(FormatTest
, MacrosWithoutTrailingSemicolon
) {
5677 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
5678 verifyFormat("class A : public QObject {\n"
5683 "class A : public QObject {\n"
5688 verifyFormat("MACRO\n"
5689 "/*static*/ int i;",
5691 " /*static*/ int i;");
5692 verifyFormat("SOME_MACRO\n"
5700 // Only if the identifier contains at least 5 characters.
5701 verifyFormat("HTTP f();", "HTTP\nf();");
5702 verifyNoChange("MACRO\nf();");
5703 // Only if everything is upper case.
5704 verifyFormat("class A : public QObject {\n"
5705 " Q_Object A() {}\n"
5707 "class A : public QObject {\n"
5712 // Only if the next line can actually start an unwrapped line.
5713 verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n"
5716 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
5718 getChromiumStyle(FormatStyle::LK_Cpp
));
5721 verifyNoChange("/**/ FOO(a)\n"
5725 TEST_F(FormatTest
, MacroCallsWithoutTrailingSemicolon
) {
5726 verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5727 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5728 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5730 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5731 "int *createScopDetectionPass() { return 0; }",
5732 " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
5733 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
5734 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
5736 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
5737 " int *createScopDetectionPass() { return 0; }");
5738 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
5739 // braces, so that inner block is indented one level more.
5740 verifyFormat("int q() {\n"
5741 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5742 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5743 " IPC_END_MESSAGE_MAP()\n"
5746 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
5747 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
5748 " IPC_END_MESSAGE_MAP()\n"
5751 // Same inside macros.
5752 verifyFormat("#define LIST(L) \\\n"
5756 "#define LIST(L) \\\n"
5762 // These must not be recognized as macros.
5763 verifyFormat("int q() {\n"
5781 " LOG(INFO) << x;\n"
5782 " ifstream(x) >> x;\n"
5802 " LOG(INFO)\n << x;\n"
5803 " ifstream(x)\n >> x;\n"
5805 verifyFormat("int q() {\n"
5817 " } catch (...) {\n"
5828 "try { Q(); } catch (...) {}\n"
5830 verifyFormat("class A {\n"
5832 " A(int i) noexcept() : {}\n"
5833 " A(X x)\n" // FIXME: function-level try blocks are broken.
5835 " } catch (...) {\n"
5839 " A()\n : t(0) {}\n"
5840 " A(int i)\n noexcept() : {}\n"
5842 " try : t(0) {} catch (...) {}\n"
5844 FormatStyle Style
= getLLVMStyle();
5845 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
5846 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
5847 Style
.BraceWrapping
.AfterFunction
= true;
5848 verifyFormat("void f()\n"
5855 verifyFormat("class SomeClass {\n"
5857 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5859 "class SomeClass {\n"
5862 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5864 verifyFormat("class SomeClass {\n"
5867 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5869 "class SomeClass {\n"
5872 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
5874 getLLVMStyleWithColumns(40));
5876 verifyFormat("MACRO(>)");
5878 // Some macros contain an implicit semicolon.
5879 Style
= getLLVMStyle();
5880 Style
.StatementMacros
.push_back("FOO");
5881 verifyFormat("FOO(a) int b = 0;");
5882 verifyFormat("FOO(a)\n"
5885 verifyFormat("FOO(a);\n"
5888 verifyFormat("FOO(argc, argv, \"4.0.2\")\n"
5891 verifyFormat("FOO()\n"
5894 verifyFormat("FOO\n"
5897 verifyFormat("void f() {\n"
5902 verifyFormat("FOO(a)\n"
5905 verifyFormat("int a = 0;\n"
5909 verifyFormat("int a = 0;\n"
5913 verifyFormat("void foo(int a) { FOO(a) }\n"
5914 "uint32_t bar() {}",
5918 TEST_F(FormatTest
, FormatsMacrosWithZeroColumnWidth
) {
5919 FormatStyle ZeroColumn
= getLLVMStyleWithColumns(0);
5921 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()",
5925 TEST_F(FormatTest
, LayoutMacroDefinitionsStatementsSpanningBlocks
) {
5926 verifyFormat("#define A \\\n"
5930 getLLVMStyleWithColumns(11));
5933 TEST_F(FormatTest
, IndentPreprocessorDirectives
) {
5934 FormatStyle Style
= getLLVMStyleWithColumns(40);
5935 Style
.IndentPPDirectives
= FormatStyle::PPDIS_None
;
5936 verifyFormat("#ifdef _WIN32\n"
5940 "#include <someheader.h>\n"
5941 "#define MACRO \\\n"
5942 " some_very_long_func_aaaaaaaaaa();\n"
5948 Style
.IndentPPDirectives
= FormatStyle::PPDIS_AfterHash
;
5949 verifyFormat("#if 1\n"
5950 "# define __STR(x) #x\n"
5953 verifyFormat("#ifdef _WIN32\n"
5957 "# include <someheader.h>\n"
5958 "# define MACRO \\\n"
5959 " some_very_long_func_aaaaaaaaaa();\n"
5965 verifyFormat("#if A\n"
5966 "# define MACRO \\\n"
5967 " void a(int x) { \\\n"
5976 // Comments before include guard.
5977 verifyFormat("// file comment\n"
5979 "#ifndef HEADER_H\n"
5980 "#define HEADER_H\n"
5984 // Test with include guards.
5985 verifyFormat("#ifndef HEADER_H\n"
5986 "#define HEADER_H\n"
5990 // Include guards must have a #define with the same variable immediately
5992 verifyFormat("#ifndef NOT_GUARD\n"
5998 // Include guards must cover the entire file.
5999 verifyFormat("code();\n"
6001 "#ifndef NOT_GUARD\n"
6002 "# define NOT_GUARD\n"
6006 verifyFormat("#ifndef NOT_GUARD\n"
6007 "# define NOT_GUARD\n"
6012 // Test with trailing blank lines.
6013 verifyFormat("#ifndef HEADER_H\n"
6014 "#define HEADER_H\n"
6018 // Include guards don't have #else.
6019 verifyFormat("#ifndef NOT_GUARD\n"
6020 "# define NOT_GUARD\n"
6025 verifyFormat("#ifndef NOT_GUARD\n"
6026 "# define NOT_GUARD\n"
6031 // Non-identifier #define after potential include guard.
6032 verifyFormat("#ifndef FOO\n"
6036 // #if closes past last non-preprocessor line.
6037 verifyFormat("#ifndef FOO\n"
6045 // Don't crash if there is an #elif directive without a condition.
6046 verifyFormat("#if 1\n"
6054 // FIXME: This doesn't handle the case where there's code between the
6055 // #ifndef and #define but all other conditions hold. This is because when
6056 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
6057 // previous code line yet, so we can't detect it.
6058 verifyFormat("#ifndef NOT_GUARD\n"
6060 "#define NOT_GUARD\n"
6063 "#ifndef NOT_GUARD\n"
6065 "# define NOT_GUARD\n"
6069 // FIXME: This doesn't handle cases where legitimate preprocessor lines may
6070 // be outside an include guard. Examples are #pragma once and
6071 // #pragma GCC diagnostic, or anything else that does not change the meaning
6072 // of the file if it's included multiple times.
6073 verifyFormat("#ifdef WIN32\n"
6076 "#ifndef HEADER_H\n"
6077 "# define HEADER_H\n"
6083 "#ifndef HEADER_H\n"
6084 "#define HEADER_H\n"
6088 // FIXME: This does not detect when there is a single non-preprocessor line
6089 // in front of an include-guard-like structure where other conditions hold
6090 // because ScopedLineState hides the line.
6091 verifyFormat("code();\n"
6092 "#ifndef HEADER_H\n"
6093 "#define HEADER_H\n"
6097 "#ifndef HEADER_H\n"
6098 "# define HEADER_H\n"
6102 // Keep comments aligned with #, otherwise indent comments normally. These
6103 // tests cannot use verifyFormat because messUp manipulates leading
6106 const char *Expected
= ""
6109 "// Preprocessor aligned.\n"
6111 " // Code. Separated by blank line.\n"
6114 " // Code. Not aligned with #\n"
6117 const char *ToFormat
= ""
6120 "// Preprocessor aligned.\n"
6122 "// Code. Separated by blank line.\n"
6125 " // Code. Not aligned with #\n"
6128 verifyFormat(Expected
, ToFormat
, Style
);
6129 verifyNoChange(Expected
, Style
);
6131 // Keep block quotes aligned.
6133 const char *Expected
= ""
6136 "/* Preprocessor aligned. */\n"
6138 " /* Code. Separated by blank line. */\n"
6141 " /* Code. Not aligned with # */\n"
6144 const char *ToFormat
= ""
6147 "/* Preprocessor aligned. */\n"
6149 "/* Code. Separated by blank line. */\n"
6152 " /* Code. Not aligned with # */\n"
6155 verifyFormat(Expected
, ToFormat
, Style
);
6156 verifyNoChange(Expected
, Style
);
6158 // Keep comments aligned with un-indented directives.
6160 const char *Expected
= ""
6162 "// Preprocessor aligned.\n"
6164 " // Code. Separated by blank line.\n"
6167 " // Code. Not aligned with #\n"
6169 const char *ToFormat
= ""
6171 "// Preprocessor aligned.\n"
6173 "// Code. Separated by blank line.\n"
6176 " // Code. Not aligned with #\n"
6178 verifyFormat(Expected
, ToFormat
, Style
);
6179 verifyNoChange(Expected
, Style
);
6181 // Test AfterHash with tabs.
6183 FormatStyle Tabbed
= Style
;
6184 Tabbed
.UseTab
= FormatStyle::UT_Always
;
6185 Tabbed
.IndentWidth
= 8;
6186 Tabbed
.TabWidth
= 8;
6187 verifyFormat("#ifdef _WIN32\n"
6191 "#\t\tinclude <someheader.h>\n"
6192 "#\t\tdefine MACRO \\\n"
6193 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
6201 // Regression test: Multiline-macro inside include guards.
6202 verifyFormat("#ifndef HEADER_H\n"
6203 "#define HEADER_H\n"
6207 "#endif // HEADER_H",
6208 getLLVMStyleWithColumns(20));
6210 Style
.IndentPPDirectives
= FormatStyle::PPDIS_BeforeHash
;
6211 // Basic before hash indent tests
6212 verifyFormat("#ifdef _WIN32\n"
6216 " #include <someheader.h>\n"
6217 " #define MACRO \\\n"
6218 " some_very_long_func_aaaaaaaaaa();\n"
6224 verifyFormat("#if A\n"
6225 " #define MACRO \\\n"
6226 " void a(int x) { \\\n"
6235 // Keep comments aligned with indented directives. These
6236 // tests cannot use verifyFormat because messUp manipulates leading
6239 const char *Expected
= "void f() {\n"
6240 "// Aligned to preprocessor.\n"
6242 " // Aligned to code.\n"
6245 " // Aligned to preprocessor.\n"
6247 " // Aligned to code.\n"
6252 const char *ToFormat
= "void f() {\n"
6253 "// Aligned to preprocessor.\n"
6255 "// Aligned to code.\n"
6258 "// Aligned to preprocessor.\n"
6260 "// Aligned to code.\n"
6265 verifyFormat(Expected
, ToFormat
, Style
);
6266 verifyNoChange(Expected
, Style
);
6269 const char *Expected
= "void f() {\n"
6270 "/* Aligned to preprocessor. */\n"
6272 " /* Aligned to code. */\n"
6275 " /* Aligned to preprocessor. */\n"
6277 " /* Aligned to code. */\n"
6282 const char *ToFormat
= "void f() {\n"
6283 "/* Aligned to preprocessor. */\n"
6285 "/* Aligned to code. */\n"
6288 "/* Aligned to preprocessor. */\n"
6290 "/* Aligned to code. */\n"
6295 verifyFormat(Expected
, ToFormat
, Style
);
6296 verifyNoChange(Expected
, Style
);
6299 // Test single comment before preprocessor
6300 verifyFormat("// Comment\n"
6307 TEST_F(FormatTest
, FormatAlignInsidePreprocessorElseBlock
) {
6308 FormatStyle Style
= getLLVMStyle();
6309 Style
.AlignConsecutiveAssignments
.Enabled
= true;
6310 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
6312 // Test with just #if blocks.
6313 verifyFormat("void f1() {\n"
6316 " int foobar = 2;\n"
6324 " char *foobarbaz = \"foobarbaz\";\n"
6329 // Test with just #else blocks.
6330 verifyFormat("void f1() {\n"
6334 " int foobar = 2;\n"
6344 " char *foobarbaz = \"foobarbaz\";\n"
6349 // Test with a mix of #if and #else blocks.
6350 verifyFormat("void f1() {\n"
6354 " int foobar = 2;\n"
6363 " // prevent alignment with #else in f1\n"
6364 " char *foobarbaz = \"foobarbaz\";\n"
6369 // Test with nested #if and #else blocks.
6370 verifyFormat("void f1() {\n"
6376 " int foobar = 2;\n"
6390 " // prevent alignment with #else in f1\n"
6391 " char *foobarbaz = \"foobarbaz\";\n"
6398 verifyFormat("#if FOO\n"
6410 verifyFormat("void f() {\n"
6421 " bool abcd = true;\n"
6427 verifyFormat("void f() {\n"
6444 TEST_F(FormatTest
, FormatHashIfNotAtStartOfLine
) {
6445 verifyFormat("{\n { a #c; }\n}");
6448 TEST_F(FormatTest
, FormatUnbalancedStructuralElements
) {
6449 verifyFormat("#define A \\\n { \\\n {\nint i;",
6450 "#define A { {\nint i;", getLLVMStyleWithColumns(11));
6451 verifyFormat("#define A \\\n } \\\n }\nint i;",
6452 "#define A } }\nint i;", getLLVMStyleWithColumns(11));
6455 TEST_F(FormatTest
, EscapedNewlines
) {
6456 FormatStyle Narrow
= getLLVMStyleWithColumns(11);
6457 verifyFormat("#define A \\\n int i; \\\n int j;",
6458 "#define A \\\nint i;\\\n int j;", Narrow
);
6459 verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;");
6460 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6461 verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */");
6462 verifyNoChange("<a\n\\\\\n>");
6464 FormatStyle AlignLeft
= getLLVMStyle();
6465 AlignLeft
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
6466 verifyFormat("#define MACRO(x) \\\n"
6471 // CRLF line endings
6472 verifyFormat("#define A \\\r\n int i; \\\r\n int j;",
6473 "#define A \\\r\nint i;\\\r\n int j;", Narrow
);
6474 verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;");
6475 verifyFormat("template <class T> f();", "\\\ntemplate <class T> f();");
6476 verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */");
6477 verifyNoChange("<a\r\n\\\\\r\n>");
6478 verifyFormat("#define MACRO(x) \\\r\n"
6483 FormatStyle DontAlign
= getLLVMStyle();
6484 DontAlign
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
6485 DontAlign
.MaxEmptyLinesToKeep
= 3;
6486 // FIXME: can't use verifyFormat here because the newline before
6487 // "public:" is not inserted the first time it's reformatted
6488 verifyNoChange("#define A \\\n"
6500 TEST_F(FormatTest
, CalculateSpaceOnConsecutiveLinesInMacro
) {
6501 verifyFormat("#define A \\\n"
6505 getLLVMStyleWithColumns(11));
6508 TEST_F(FormatTest
, MixingPreprocessorDirectivesAndNormalCode
) {
6509 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
6511 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6513 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6514 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);",
6515 " #define ALooooooooooooooooooooooooooooooooooooooongMacro("
6517 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
6519 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
6520 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);");
6523 TEST_F(FormatTest
, LayoutStatementsAroundPreprocessorDirectives
) {
6524 verifyFormat("int\n"
6527 "int\n#define A\na;");
6528 verifyFormat("functionCallTo(\n"
6529 " someOtherFunction(\n"
6530 " withSomeParameters, whichInSequence,\n"
6531 " areLongerThanALine(andAnotherCall,\n"
6533 " withMoreParamters,\n"
6534 " whichStronglyInfluenceTheLayout),\n"
6535 " andMoreParameters),\n"
6537 getLLVMStyleWithColumns(69));
6538 verifyFormat("Foo::Foo()\n"
6544 verifyFormat("void f() {\n"
6554 verifyFormat("void f(param1, param2,\n"
6575 getLLVMStyleWithColumns(28));
6576 verifyFormat("#if 1\n"
6578 verifyFormat("#if 1\n"
6583 verifyFormat("DEBUG({\n"
6584 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6585 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
6591 verifyIncompleteFormat("void f(\n"
6597 // Verify that indentation is correct when there is an `#if 0` with an
6599 verifyFormat("#if 0\n"
6607 verifyFormat("#if 0\n"
6610 "int something_fairly_long; // Align here please\n"
6611 "#endif // Should be aligned");
6613 verifyFormat("#if 0\n"
6620 verifyFormat("void SomeFunction(int param1,\n"
6635 TEST_F(FormatTest
, GraciouslyHandleIncorrectPreprocessorConditions
) {
6636 verifyFormat("#endif\n"
6640 TEST_F(FormatTest
, FormatsJoinedLinesOnSubsequentRuns
) {
6641 FormatStyle SingleLine
= getLLVMStyle();
6642 SingleLine
.AllowShortIfStatementsOnASingleLine
= FormatStyle::SIS_WithoutElse
;
6643 verifyFormat("#if 0\n"
6647 " if (test) foo2();\n"
6652 TEST_F(FormatTest
, LayoutBlockInsideParens
) {
6653 verifyFormat("functionCall({ int i; });");
6654 verifyFormat("functionCall({\n"
6658 verifyFormat("functionCall(\n"
6663 " aaaa, bbbb, cccc);");
6664 verifyFormat("functionA(functionB({\n"
6668 " aaaa, bbbb, cccc);");
6669 verifyFormat("functionCall(\n"
6674 " aaaa, bbbb, // comment\n"
6676 verifyFormat("functionA(functionB({\n"
6680 " aaaa, bbbb, // comment\n"
6682 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
6683 verifyFormat("functionCall(aaaa, bbbb, {\n"
6688 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
6690 " int i; // break\n"
6692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6693 " ccccccccccccccccc));");
6694 verifyFormat("DEBUG({\n"
6700 TEST_F(FormatTest
, LayoutBlockInsideStatement
) {
6701 verifyFormat("SOME_MACRO { int i; }\n"
6703 " SOME_MACRO {int i;} int i;");
6706 TEST_F(FormatTest
, LayoutNestedBlocks
) {
6707 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
6711 " s kBitsToOs[] = {{10}};\n"
6712 " for (int i = 0; i < 10; ++i)\n"
6715 verifyFormat("call(parameter, {\n"
6717 " // Comment using all columns.\n"
6718 " somethingelse();\n"
6720 getLLVMStyleWithColumns(40));
6721 verifyFormat("DEBUG( //\n"
6723 verifyFormat("DEBUG( //\n"
6729 verifyFormat("call(parameter, {\n"
6732 " // looooooooooong.\n"
6733 " somethingElse();\n"
6735 "call(parameter, {\n"
6737 " // Comment too looooooooooong.\n"
6738 " somethingElse();\n"
6740 getLLVMStyleWithColumns(29));
6741 verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });");
6742 verifyFormat("DEBUG({ // comment\n"
6745 "DEBUG({ // comment\n"
6748 verifyFormat("DEBUG({\n"
6761 verifyFormat("DEBUG({\n"
6765 verifyGoogleFormat("DEBUG({\n"
6768 FormatStyle Style
= getGoogleStyle();
6769 Style
.ColumnLimit
= 45;
6770 verifyFormat("Debug(\n"
6773 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
6778 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
6780 verifyNoCrash("^{v^{a}}");
6783 TEST_F(FormatTest
, FormatNestedBlocksInMacros
) {
6784 verifyFormat("#define MACRO() \\\n"
6785 " Debug(aaa, /* force line break */ \\\n"
6790 "#define MACRO() Debug(aaa, /* force line break */ \\\n"
6791 " { int i; int j; })",
6794 verifyFormat("#define A \\\n"
6796 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6797 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
6799 "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
6800 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
6804 TEST_F(FormatTest
, PutEmptyBlocksIntoOneLine
) {
6806 verifyFormat("enum E {};");
6807 verifyFormat("enum E {}");
6808 FormatStyle Style
= getLLVMStyle();
6809 Style
.SpaceInEmptyBlock
= true;
6810 verifyFormat("void f() { }", "void f() {}", Style
);
6811 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Empty
;
6812 verifyFormat("while (true) { }", "while (true) {}", Style
);
6813 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
6814 Style
.BraceWrapping
.BeforeElse
= false;
6815 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
6816 verifyFormat("if (a)\n"
6823 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Never
;
6824 verifyFormat("if (a) {\n"
6829 Style
.BraceWrapping
.BeforeElse
= true;
6830 verifyFormat("if (a) { }\n"
6836 TEST_F(FormatTest
, FormatBeginBlockEndMacros
) {
6837 FormatStyle Style
= getLLVMStyle();
6838 Style
.MacroBlockBegin
= "^[A-Z_]+_BEGIN$";
6839 Style
.MacroBlockEnd
= "^[A-Z_]+_END$";
6840 verifyFormat("FOO_BEGIN\n"
6844 verifyFormat("FOO_BEGIN\n"
6845 " NESTED_FOO_BEGIN\n"
6846 " NESTED_FOO_ENTRY\n"
6850 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
6856 Style
.RemoveBracesLLVM
= true;
6857 verifyNoCrash("for (;;)\n"
6864 //===----------------------------------------------------------------------===//
6865 // Line break tests.
6866 //===----------------------------------------------------------------------===//
6868 TEST_F(FormatTest
, PreventConfusingIndents
) {
6871 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
6872 " parameter, parameter, parameter)),\n"
6873 " SecondLongCall(parameter));\n"
6876 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6877 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
6878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
6879 " aaaaaaaaaaaaaaaaaaaaaaaa);");
6881 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6882 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
6883 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6884 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
6886 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6887 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
6888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
6889 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
6890 verifyFormat("int a = bbbb && ccc &&\n"
6892 "#define A Just forcing a new line\n"
6896 TEST_F(FormatTest
, LineBreakingInBinaryExpressions
) {
6899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
6903 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
6906 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
6907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
6908 " ccccccccc == ddddddddddd;");
6909 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
6910 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
6911 " ccccccccc == ddddddddddd;");
6913 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
6914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
6915 " ccccccccc == ddddddddddd;");
6917 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
6919 " bbbbbb && cccccc;");
6920 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
6923 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
6924 " SourceMgr.getSpellingColumnNumber(\n"
6925 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
6928 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6929 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
6931 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6932 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
6934 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6935 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
6937 verifyFormat("b = a &&\n"
6941 // If the LHS of a comparison is not a binary expression itself, the
6942 // additional linebreak confuses many people.
6944 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
6948 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6949 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
6952 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
6953 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
6956 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6957 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n"
6959 // Even explicit parentheses stress the precedence enough to make the
6960 // additional break unnecessary.
6961 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
6964 // This cases is borderline, but with the indentation it is still readable.
6966 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6967 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6968 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
6970 getLLVMStyleWithColumns(75));
6972 // If the LHS is a binary expression, we should still use the additional break
6973 // as otherwise the formatting hides the operator precedence.
6974 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6975 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
6978 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
6979 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n"
6983 FormatStyle OnePerLine
= getLLVMStyle();
6984 OnePerLine
.BinPackParameters
= false;
6986 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6987 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6988 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
6991 verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
6992 " .aaa(aaaaaaaaaaaaa) *\n"
6995 getLLVMStyleWithColumns(40));
6998 TEST_F(FormatTest
, ExpressionIndentation
) {
6999 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7003 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
7004 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
7005 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7006 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
7007 " ccccccccccccccccccccccccccccccccccccccccc;");
7008 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7009 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7010 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7011 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7012 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7013 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7015 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7016 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
7017 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
7018 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7019 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
7020 verifyFormat("if () {\n"
7021 "} else if (aaaaa && bbbbb > // break\n"
7024 verifyFormat("if () {\n"
7025 "} else if constexpr (aaaaa && bbbbb > // break\n"
7028 verifyFormat("if () {\n"
7029 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n"
7032 verifyFormat("if () {\n"
7033 "} else if (aaaaa &&\n"
7034 " bbbbb > // break\n"
7039 // Presence of a trailing comment used to change indentation of b.
7040 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
7042 "return aaaaaaaaaaaaaaaaaaa +\n"
7044 getLLVMStyleWithColumns(30));
7047 TEST_F(FormatTest
, ExpressionIndentationBreakingBeforeOperators
) {
7048 // Not sure what the best system is here. Like this, the LHS can be found
7049 // immediately above an operator (everything with the same or a higher
7050 // indent). The RHS is aligned right of the operator and so compasses
7051 // everything until something with the same indent as the operator is found.
7052 // FIXME: Is this a good system?
7053 FormatStyle Style
= getLLVMStyle();
7054 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
7056 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7057 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7058 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7059 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7060 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7061 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7062 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7063 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7064 " > ccccccccccccccccccccccccccccccccccccccccc;",
7066 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7067 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7068 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7069 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7071 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7072 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7073 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7074 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7076 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7077 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7078 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7079 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7081 verifyFormat("if () {\n"
7082 "} else if (aaaaa\n"
7083 " && bbbbb // break\n"
7087 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7088 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7090 verifyFormat("return (a)\n"
7095 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7096 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7100 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7101 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7104 // Forced by comments.
7106 "unsigned ContentSize =\n"
7107 " sizeof(int16_t) // DWARF ARange version number\n"
7108 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7109 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7110 " + sizeof(int8_t); // Segment Size (in bytes)");
7112 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7113 " == boost::fusion::at_c<1>(iiii).second;",
7116 Style
.ColumnLimit
= 60;
7117 verifyFormat("zzzzzzzzzz\n"
7118 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7119 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7122 Style
.ColumnLimit
= 80;
7123 Style
.IndentWidth
= 4;
7125 Style
.UseTab
= FormatStyle::UT_Always
;
7126 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
7127 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
7128 verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
7129 "\t&& (someOtherLongishConditionPart1\n"
7130 "\t\t|| someOtherEvenLongerNestedConditionPart2);",
7131 "return someVeryVeryLongConditionThatBarelyFitsOnALine && "
7132 "(someOtherLongishConditionPart1 || "
7133 "someOtherEvenLongerNestedConditionPart2);",
7136 Style
= getLLVMStyleWithColumns(20);
7137 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
7138 Style
.BinPackParameters
= false;
7139 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7140 Style
.ContinuationIndentWidth
= 2;
7141 verifyFormat("struct Foo {\n"
7150 verifyFormat("return abc\n"
7160 TEST_F(FormatTest
, ExpressionIndentationStrictAlign
) {
7161 FormatStyle Style
= getLLVMStyle();
7162 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
7163 Style
.AlignOperands
= FormatStyle::OAS_AlignAfterOperator
;
7165 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7166 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7167 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7168 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7169 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7170 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7171 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7172 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7173 " > ccccccccccccccccccccccccccccccccccccccccc;",
7175 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7176 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7177 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7178 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7180 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7181 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7182 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7183 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7185 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7186 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7187 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7188 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
7190 verifyFormat("if () {\n"
7191 "} else if (aaaaa\n"
7192 " && bbbbb // break\n"
7196 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7197 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7199 verifyFormat("return (a)\n"
7204 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7205 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7208 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
7209 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7210 " : 3333333333333333;",
7213 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
7214 " : ccccccccccccccc ? dddddddddddddddddd\n"
7215 " : eeeeeeeeeeeeeeeeee)\n"
7216 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
7217 " : 3333333333333333;",
7219 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7220 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
7223 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
7224 " == boost::fusion::at_c<1>(iiii).second;",
7227 Style
.ColumnLimit
= 60;
7228 verifyFormat("zzzzzzzzzzzzz\n"
7229 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7230 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
7233 // Forced by comments.
7234 Style
.ColumnLimit
= 80;
7236 "unsigned ContentSize\n"
7237 " = sizeof(int16_t) // DWARF ARange version number\n"
7238 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7239 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7240 " + sizeof(int8_t); // Segment Size (in bytes)",
7243 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7245 "unsigned ContentSize =\n"
7246 " sizeof(int16_t) // DWARF ARange version number\n"
7247 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7248 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7249 " + sizeof(int8_t); // Segment Size (in bytes)",
7252 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
7254 "unsigned ContentSize =\n"
7255 " sizeof(int16_t) // DWARF ARange version number\n"
7256 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
7257 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
7258 " + sizeof(int8_t); // Segment Size (in bytes)",
7262 TEST_F(FormatTest
, EnforcedOperatorWraps
) {
7263 // Here we'd like to wrap after the || operators, but a comment is forcing an
7265 verifyFormat("bool x = aaaaa //\n"
7271 TEST_F(FormatTest
, NoOperandAlignment
) {
7272 FormatStyle Style
= getLLVMStyle();
7273 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
7274 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
7275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
7276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
7278 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7279 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7280 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7281 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7282 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7283 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7284 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7285 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7286 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7287 " > ccccccccccccccccccccccccccccccccccccccccc;",
7290 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7291 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7294 verifyFormat("int a = aa\n"
7295 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
7296 " * cccccccccccccccccccccccccccccccccccc;",
7299 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
7300 verifyFormat("return (a > b\n"
7307 TEST_F(FormatTest
, BreakingBeforeNonAssigmentOperators
) {
7308 FormatStyle Style
= getLLVMStyle();
7309 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7310 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7311 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7312 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7316 TEST_F(FormatTest
, AllowBinPackingInsideArguments
) {
7317 FormatStyle Style
= getLLVMStyleWithColumns(40);
7318 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7319 Style
.BinPackArguments
= false;
7320 verifyFormat("void test() {\n"
7322 " this + argument + is + quite\n"
7323 " + long + so + it + gets + wrapped\n"
7324 " + but + remains + bin - packed);\n"
7327 verifyFormat("void test() {\n"
7328 " someFunction(arg1,\n"
7329 " this + argument + is\n"
7330 " + quite + long + so\n"
7331 " + it + gets + wrapped\n"
7332 " + but + remains + bin\n"
7337 verifyFormat("void test() {\n"
7340 " this + argument + has\n"
7341 " + anotherFunc(nested,\n"
7347 " + to + being + bin - packed,\n"
7352 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
7353 verifyFormat("void test() {\n"
7356 " this + argument + has +\n"
7357 " anotherFunc(nested,\n"
7358 " calls + whose +\n"
7362 " in + addition) +\n"
7363 " to + being + bin - packed,\n"
7369 TEST_F(FormatTest
, BreakBinaryOperatorsInPresenceOfTemplates
) {
7370 auto Style
= getLLVMStyleWithColumns(45);
7371 EXPECT_EQ(Style
.BreakBeforeBinaryOperators
, FormatStyle::BOS_None
);
7372 verifyFormat("bool b =\n"
7373 " is_default_constructible_v<hash<T>> and\n"
7374 " is_copy_constructible_v<hash<T>> and\n"
7375 " is_move_constructible_v<hash<T>> and\n"
7376 " is_copy_assignable_v<hash<T>> and\n"
7377 " is_move_assignable_v<hash<T>> and\n"
7378 " is_destructible_v<hash<T>> and\n"
7379 " is_swappable_v<hash<T>> and\n"
7380 " is_callable_v<hash<T>(T)>;",
7383 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_NonAssignment
;
7384 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7385 " and is_copy_constructible_v<hash<T>>\n"
7386 " and is_move_constructible_v<hash<T>>\n"
7387 " and is_copy_assignable_v<hash<T>>\n"
7388 " and is_move_assignable_v<hash<T>>\n"
7389 " and is_destructible_v<hash<T>>\n"
7390 " and is_swappable_v<hash<T>>\n"
7391 " and is_callable_v<hash<T>(T)>;",
7394 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
7395 verifyFormat("bool b = is_default_constructible_v<hash<T>>\n"
7396 " and is_copy_constructible_v<hash<T>>\n"
7397 " and is_move_constructible_v<hash<T>>\n"
7398 " and is_copy_assignable_v<hash<T>>\n"
7399 " and is_move_assignable_v<hash<T>>\n"
7400 " and is_destructible_v<hash<T>>\n"
7401 " and is_swappable_v<hash<T>>\n"
7402 " and is_callable_v<hash<T>(T)>;",
7406 TEST_F(FormatTest
, ConstructorInitializers
) {
7407 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7408 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
7409 getLLVMStyleWithColumns(45));
7410 verifyFormat("Constructor()\n"
7411 " : Inttializer(FitsOnTheLine) {}",
7412 getLLVMStyleWithColumns(44));
7413 verifyFormat("Constructor()\n"
7414 " : Inttializer(FitsOnTheLine) {}",
7415 getLLVMStyleWithColumns(43));
7417 verifyFormat("template <typename T>\n"
7418 "Constructor() : Initializer(FitsOnTheLine) {}",
7419 getLLVMStyleWithColumns(45));
7422 "SomeClass::Constructor()\n"
7423 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7426 "SomeClass::Constructor()\n"
7427 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7428 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
7430 "SomeClass::Constructor()\n"
7431 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7432 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
7433 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7434 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
7435 " : aaaaaaaaaa(aaaaaa) {}");
7437 verifyFormat("Constructor()\n"
7438 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7439 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7440 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7441 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
7443 verifyFormat("Constructor()\n"
7444 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7447 verifyFormat("Constructor(int Parameter = 0)\n"
7448 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7449 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
7450 verifyFormat("Constructor()\n"
7451 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7453 getLLVMStyleWithColumns(60));
7454 verifyFormat("Constructor()\n"
7455 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7456 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
7458 // Here a line could be saved by splitting the second initializer onto two
7459 // lines, but that is not desirable.
7460 verifyFormat("Constructor()\n"
7461 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7462 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7463 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
7465 FormatStyle OnePerLine
= getLLVMStyle();
7466 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_Never
;
7467 verifyFormat("MyClass::MyClass()\n"
7472 verifyFormat("MyClass::MyClass()\n"
7473 " : a(a), // comment\n"
7477 verifyFormat("MyClass::MyClass(int a)\n"
7478 " : b(a), // comment\n"
7479 " c(a + 1) { // lined up\n"
7482 verifyFormat("Constructor()\n"
7485 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7486 OnePerLine
.AllowAllParametersOfDeclarationOnNextLine
= false;
7487 verifyFormat("SomeClass::Constructor()\n"
7488 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7489 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7490 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7492 verifyFormat("SomeClass::Constructor()\n"
7493 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
7494 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7495 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7497 verifyFormat("MyClass::MyClass(int var)\n"
7498 " : some_var_(var), // 4 space indent\n"
7499 " some_other_var_(var + 1) { // lined up\n"
7502 verifyFormat("Constructor()\n"
7503 " : aaaaa(aaaaaa),\n"
7507 " aaaaa(aaaaaa) {}",
7509 verifyFormat("Constructor()\n"
7510 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
7511 " aaaaaaaaaaaaaaaaaaaaaa) {}",
7513 OnePerLine
.BinPackParameters
= false;
7516 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
7517 " aaaaaaaaaaa().aaa(),\n"
7518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7520 OnePerLine
.ColumnLimit
= 60;
7521 verifyFormat("Constructor()\n"
7522 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7523 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
7526 verifyFormat("Constructor()\n"
7527 " : // Comment forcing unwanted break.\n"
7530 " // Comment forcing unwanted break.\n"
7534 TEST_F(FormatTest
, AllowAllConstructorInitializersOnNextLine
) {
7535 FormatStyle Style
= getLLVMStyleWithColumns(60);
7536 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
7537 Style
.BinPackParameters
= false;
7539 for (int i
= 0; i
< 4; ++i
) {
7540 // Test all combinations of parameters that should not have an effect.
7541 Style
.AllowAllParametersOfDeclarationOnNextLine
= i
& 1;
7542 Style
.AllowAllArgumentsOnNextLine
= i
& 2;
7544 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7545 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
7546 verifyFormat("Constructor()\n"
7547 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7549 verifyFormat("Constructor() : a(a), b(b) {}", Style
);
7551 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7552 verifyFormat("Constructor()\n"
7553 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7554 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7556 verifyFormat("Constructor() : a(a), b(b) {}", Style
);
7558 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7559 verifyFormat("Constructor()\n"
7560 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7562 verifyFormat("Constructor()\n"
7565 verifyFormat("Constructor()\n"
7566 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7567 " , bbbbbbbbbbbbbbbbbbbbb(b)\n"
7568 " , cccccccccccccccccccccc(c) {}",
7571 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeColon
;
7572 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7573 verifyFormat("Constructor()\n"
7574 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7577 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7578 verifyFormat("Constructor()\n"
7579 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7580 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7583 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7584 verifyFormat("Constructor()\n"
7585 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7587 verifyFormat("Constructor()\n"
7590 verifyFormat("Constructor()\n"
7591 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7592 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7593 " cccccccccccccccccccccc(c) {}",
7596 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
7597 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7598 verifyFormat("Constructor() :\n"
7599 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7602 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7603 verifyFormat("Constructor() :\n"
7604 " aaaaaaaaaaaaaaaaaa(a),\n"
7605 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7608 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7609 verifyFormat("Constructor() :\n"
7610 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7612 verifyFormat("Constructor() :\n"
7615 verifyFormat("Constructor() :\n"
7616 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7617 " bbbbbbbbbbbbbbbbbbbbb(b),\n"
7618 " cccccccccccccccccccccc(c) {}",
7622 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and
7623 // AllowAllConstructorInitializersOnNextLine in all
7624 // BreakConstructorInitializers modes
7625 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
7626 Style
.AllowAllParametersOfDeclarationOnNextLine
= true;
7627 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7628 verifyFormat("SomeClassWithALongName::Constructor(\n"
7629 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7630 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7631 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7634 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7635 verifyFormat("SomeClassWithALongName::Constructor(\n"
7636 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7637 " int bbbbbbbbbbbbb,\n"
7638 " int cccccccccccccccc)\n"
7639 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7642 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7643 verifyFormat("SomeClassWithALongName::Constructor(\n"
7644 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7645 " int bbbbbbbbbbbbb,\n"
7646 " int cccccccccccccccc)\n"
7647 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7650 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
7651 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7652 verifyFormat("SomeClassWithALongName::Constructor(\n"
7653 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7654 " int bbbbbbbbbbbbb)\n"
7655 " : aaaaaaaaaaaaaaaaaaaa(a)\n"
7656 " , bbbbbbbbbbbbbbbbbbbbb(b) {}",
7659 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeColon
;
7661 Style
.AllowAllParametersOfDeclarationOnNextLine
= true;
7662 verifyFormat("SomeClassWithALongName::Constructor(\n"
7663 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n"
7664 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7665 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7668 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7669 verifyFormat("SomeClassWithALongName::Constructor(\n"
7670 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7671 " int bbbbbbbbbbbbb,\n"
7672 " int cccccccccccccccc)\n"
7673 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7676 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7677 verifyFormat("SomeClassWithALongName::Constructor(\n"
7678 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7679 " int bbbbbbbbbbbbb,\n"
7680 " int cccccccccccccccc)\n"
7681 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7684 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
7685 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7686 verifyFormat("SomeClassWithALongName::Constructor(\n"
7687 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7688 " int bbbbbbbbbbbbb)\n"
7689 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
7690 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7693 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
7694 Style
.AllowAllParametersOfDeclarationOnNextLine
= true;
7695 verifyFormat("SomeClassWithALongName::Constructor(\n"
7696 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n"
7697 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7698 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7701 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7702 verifyFormat("SomeClassWithALongName::Constructor(\n"
7703 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7704 " int bbbbbbbbbbbbb,\n"
7705 " int cccccccccccccccc) :\n"
7706 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7709 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7710 verifyFormat("SomeClassWithALongName::Constructor(\n"
7711 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7712 " int bbbbbbbbbbbbb,\n"
7713 " int cccccccccccccccc) :\n"
7714 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}",
7717 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
7718 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7719 verifyFormat("SomeClassWithALongName::Constructor(\n"
7720 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7721 " int bbbbbbbbbbbbb) :\n"
7722 " aaaaaaaaaaaaaaaaaaaa(a),\n"
7723 " bbbbbbbbbbbbbbbbbbbbb(b) {}",
7726 Style
= getLLVMStyleWithColumns(0);
7727 Style
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7728 verifyFormat("Foo(Bar bar, Baz baz) : bar(bar), baz(baz) {}", Style
);
7729 verifyNoChange("Foo(Bar bar, Baz baz)\n"
7730 " : bar(bar), baz(baz) {}",
7734 TEST_F(FormatTest
, AllowAllArgumentsOnNextLine
) {
7735 FormatStyle Style
= getLLVMStyleWithColumns(60);
7736 Style
.BinPackArguments
= false;
7737 for (int i
= 0; i
< 4; ++i
) {
7738 // Test all combinations of parameters that should not have an effect.
7739 Style
.AllowAllParametersOfDeclarationOnNextLine
= i
& 1;
7740 Style
.PackConstructorInitializers
=
7741 i
& 2 ? FormatStyle::PCIS_BinPack
: FormatStyle::PCIS_Never
;
7743 Style
.AllowAllArgumentsOnNextLine
= true;
7744 verifyFormat("void foo() {\n"
7745 " FunctionCallWithReallyLongName(\n"
7746 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n"
7749 Style
.AllowAllArgumentsOnNextLine
= false;
7750 verifyFormat("void foo() {\n"
7751 " FunctionCallWithReallyLongName(\n"
7752 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7757 Style
.AllowAllArgumentsOnNextLine
= true;
7758 verifyFormat("void foo() {\n"
7759 " auto VariableWithReallyLongName = {\n"
7760 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n"
7763 Style
.AllowAllArgumentsOnNextLine
= false;
7764 verifyFormat("void foo() {\n"
7765 " auto VariableWithReallyLongName = {\n"
7766 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7772 // This parameter should not affect declarations.
7773 Style
.BinPackParameters
= false;
7774 Style
.AllowAllArgumentsOnNextLine
= false;
7775 Style
.AllowAllParametersOfDeclarationOnNextLine
= true;
7776 verifyFormat("void FunctionCallWithReallyLongName(\n"
7777 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);",
7779 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
7780 verifyFormat("void FunctionCallWithReallyLongName(\n"
7781 " int aaaaaaaaaaaaaaaaaaaaaaa,\n"
7782 " int bbbbbbbbbbbb);",
7786 TEST_F(FormatTest
, AllowAllArgumentsOnNextLineDontAlign
) {
7787 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
7789 FormatStyle Style
= getLLVMStyleWithColumns(35);
7790 StringRef Input
= "functionCall(paramA, paramB, paramC);\n"
7791 "void functionDecl(int A, int B, int C);";
7792 Style
.AllowAllArgumentsOnNextLine
= false;
7793 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
7794 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7796 "void functionDecl(int A, int B,\n"
7799 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_Align
;
7800 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7802 "void functionDecl(int A, int B,\n"
7805 // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
7806 // AllowAllArgumentsOnNextLine.
7807 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
7808 verifyFormat(StringRef("functionCall(\n"
7809 " paramA, paramB, paramC);\n"
7810 "void functionDecl(\n"
7811 " int A, int B, int C);"),
7813 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
7814 verifyFormat("functionCall(\n"
7815 " paramA, paramB, paramC\n"
7817 "void functionDecl(\n"
7818 " int A, int B, int C\n"
7822 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
7824 Style
.AllowAllArgumentsOnNextLine
= true;
7825 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
7826 verifyFormat(StringRef("functionCall(\n"
7827 " paramA, paramB, paramC);\n"
7828 "void functionDecl(\n"
7829 " int A, int B, int C);"),
7831 // It wouldn't fit on one line with aligned parameters so this setting
7832 // doesn't change anything for BAS_Align.
7833 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_Align
;
7834 verifyFormat(StringRef("functionCall(paramA, paramB,\n"
7836 "void functionDecl(int A, int B,\n"
7839 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
7840 verifyFormat(StringRef("functionCall(\n"
7841 " paramA, paramB, paramC);\n"
7842 "void functionDecl(\n"
7843 " int A, int B, int C);"),
7847 TEST_F(FormatTest
, BreakBeforeInlineASMColon
) {
7848 FormatStyle Style
= getLLVMStyle();
7849 Style
.BreakBeforeInlineASMColon
= FormatStyle::BBIAS_Never
;
7850 /* Test the behaviour with long lines */
7851 Style
.ColumnLimit
= 40;
7852 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
7855 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
7858 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
7859 " \"cpuid\\n\\t\"\n"
7860 " \"xchgq\\t%%rbx %%rsi\\n\\t\",\n"
7861 " : \"=a\" : \"a\");",
7863 Style
.ColumnLimit
= 80;
7864 verifyFormat("asm volatile(\"string\", : : val);", Style
);
7865 verifyFormat("asm volatile(\"string\", : val1 : val2);", Style
);
7867 Style
.BreakBeforeInlineASMColon
= FormatStyle::BBIAS_Always
;
7868 verifyFormat("asm volatile(\"string\",\n"
7872 verifyFormat("asm volatile(\"string\",\n"
7876 /* Test the behaviour with long lines */
7877 Style
.ColumnLimit
= 40;
7878 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
7879 " \"cpuid\\n\\t\"\n"
7880 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
7881 " : \"=a\"(*rEAX)\n"
7882 " : \"a\"(value));",
7884 verifyFormat("asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
7885 " \"cpuid\\n\\t\"\n"
7886 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
7888 " : \"a\"(value));",
7890 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
7894 verifyFormat("asm volatile(\"loooooooooooooooooooong\",\n"
7900 TEST_F(FormatTest
, BreakConstructorInitializersAfterColon
) {
7901 FormatStyle Style
= getLLVMStyle();
7902 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
7904 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
7905 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
7906 getStyleWithColumns(Style
, 45));
7907 verifyFormat("Constructor() :\n"
7908 " Initializer(FitsOnTheLine) {}",
7909 getStyleWithColumns(Style
, 44));
7910 verifyFormat("Constructor() :\n"
7911 " Initializer(FitsOnTheLine) {}",
7912 getStyleWithColumns(Style
, 43));
7914 verifyFormat("template <typename T>\n"
7915 "Constructor() : Initializer(FitsOnTheLine) {}",
7916 getStyleWithColumns(Style
, 50));
7918 "Class::Class(int some, int arguments, int loooooooooooooooooooong,\n"
7919 " int mooooooooooooore) noexcept :\n"
7920 " Super{some, arguments}, Member{5}, Member2{2} {}",
7922 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
7924 "SomeClass::Constructor() :\n"
7925 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7928 "SomeClass::Constructor() : // NOLINT\n"
7929 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7932 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
7934 "SomeClass::Constructor() :\n"
7935 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7938 "SomeClass::Constructor() : // NOLINT\n"
7939 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7942 Style
.PackConstructorInitializers
= FormatStyle::PCIS_BinPack
;
7944 "SomeClass::Constructor() :\n"
7945 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7949 "SomeClass::Constructor() :\n"
7950 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7951 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7954 "SomeClass::Constructor() :\n"
7955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7956 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
7959 "Ctor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7960 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) : aaaaaaaaaa(aaaaaa) {}",
7963 verifyFormat("Constructor() :\n"
7964 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7965 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7966 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
7967 " aaaaaaaaaaaaaaaaaaaaaaa() {}",
7970 verifyFormat("Constructor() :\n"
7971 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7972 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7975 verifyFormat("Constructor(int Parameter = 0) :\n"
7976 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
7977 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
7979 verifyFormat("Constructor() :\n"
7980 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
7982 getStyleWithColumns(Style
, 60));
7983 verifyFormat("Constructor() :\n"
7984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
7985 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
7988 // Here a line could be saved by splitting the second initializer onto two
7989 // lines, but that is not desirable.
7990 verifyFormat("Constructor() :\n"
7991 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
7992 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
7993 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
7996 FormatStyle OnePerLine
= Style
;
7997 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_CurrentLine
;
7998 verifyFormat("SomeClass::Constructor() :\n"
7999 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8000 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8001 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8003 verifyFormat("SomeClass::Constructor() :\n"
8004 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
8005 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8006 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8008 verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n"
8009 " i(i), // comment\n"
8012 verifyFormat("MyClass::MyClass(int var) :\n"
8013 " some_var_(var), // 4 space indent\n"
8014 " some_other_var_(var + 1) { // lined up\n"
8017 verifyFormat("Constructor() :\n"
8022 " aaaaa(aaaaaa) {}",
8024 verifyFormat("Constructor() :\n"
8025 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
8026 " aaaaaaaaaaaaaaaaaaaaaa) {}",
8028 OnePerLine
.BinPackParameters
= false;
8029 verifyFormat("Constructor() :\n"
8030 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
8031 " aaaaaaaaaaa().aaa(),\n"
8032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8034 OnePerLine
.ColumnLimit
= 60;
8035 verifyFormat("Constructor() :\n"
8036 " aaaaaaaaaaaaaaaaaaaa(a),\n"
8037 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
8040 verifyFormat("Constructor() :\n"
8041 " // Comment forcing unwanted break.\n"
8044 verifyFormat("Constructor() : // NOLINT\n"
8047 verifyFormat("Constructor() : // A very long trailing comment that cannot fit"
8051 "Constructor() : // A very long trailing comment that cannot fit"
8052 " on a single line.\n"
8056 Style
.ColumnLimit
= 0;
8057 verifyFormat("SomeClass::Constructor() :\n"
8060 verifyFormat("SomeClass::Constructor() noexcept :\n"
8063 verifyFormat("SomeClass::Constructor() :\n"
8064 " a(a), b(b), c(c) {}",
8066 verifyFormat("SomeClass::Constructor() :\n"
8073 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
8074 verifyFormat("SomeClass::Constructor() :\n"
8075 " a(a), b(b), c(c) {\n"
8078 verifyFormat("SomeClass::Constructor() :\n"
8083 Style
.ColumnLimit
= 80;
8084 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
8085 Style
.ConstructorInitializerIndentWidth
= 2;
8086 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style
);
8087 verifyFormat("SomeClass::Constructor() :\n"
8088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8089 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
8092 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as
8094 Style
.BreakInheritanceList
= FormatStyle::BILS_BeforeColon
;
8097 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8098 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8100 Style
.BreakInheritanceList
= FormatStyle::BILS_BeforeComma
;
8103 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8104 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8106 Style
.BreakInheritanceList
= FormatStyle::BILS_AfterColon
;
8108 "class SomeClass :\n"
8109 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8110 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8112 Style
.BreakInheritanceList
= FormatStyle::BILS_AfterComma
;
8115 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8116 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};",
8120 #ifndef EXPENSIVE_CHECKS
8121 // Expensive checks enables libstdc++ checking which includes validating the
8122 // state of ranges used in std::priority_queue - this blows out the
8123 // runtime/scalability of the function and makes this test unacceptably slow.
8124 TEST_F(FormatTest
, MemoizationTests
) {
8125 // This breaks if the memoization lookup does not take \c Indent and
8126 // \c LastSpace into account.
8128 "extern CFRunLoopTimerRef\n"
8129 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
8130 " CFTimeInterval interval, CFOptionFlags flags,\n"
8131 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
8132 " CFRunLoopTimerContext *context) {}");
8134 // Deep nesting somewhat works around our memoization.
8136 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8137 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8138 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8139 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
8140 " aaaaa())))))))))))))))))))))))))))))))))))))));",
8141 getLLVMStyleWithColumns(65));
8167 " aaaaa))))))))))));",
8168 getLLVMStyleWithColumns(65));
8170 "a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(a(), a), a), a), a),\n"
8188 getLLVMStyleWithColumns(65));
8190 // This test takes VERY long when memoization is broken.
8191 FormatStyle OnePerLine
= getLLVMStyle();
8192 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
8193 OnePerLine
.BinPackParameters
= false;
8194 std::string input
= "Constructor()\n"
8196 for (unsigned i
= 0, e
= 80; i
!= e
; ++i
)
8199 verifyFormat(input
, OnePerLine
);
8200 OnePerLine
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
8201 verifyFormat(input
, OnePerLine
);
8205 TEST_F(FormatTest
, BreaksAsHighAsPossible
) {
8208 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
8209 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
8212 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
8213 " Intervals[i - 1].getRange().getLast()) {\n}");
8216 TEST_F(FormatTest
, BreaksFunctionDeclarations
) {
8217 // Principially, we break function declarations in a certain order:
8218 // 1) break amongst arguments.
8219 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
8220 " Cccccccccccccc cccccccccccccc);");
8221 verifyFormat("template <class TemplateIt>\n"
8222 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
8223 " TemplateIt *stop) {}");
8225 // 2) break after return type.
8227 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8228 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);");
8230 // 3) break after (.
8232 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
8233 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);");
8235 // 4) break before after nested name specifiers.
8237 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8238 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
8239 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);");
8241 // However, there are exceptions, if a sufficient amount of lines can be
8243 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
8245 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8246 " Cccccccccccccc cccccccccc,\n"
8247 " Cccccccccccccc cccccccccc,\n"
8248 " Cccccccccccccc cccccccccc,\n"
8249 " Cccccccccccccc cccccccccc);");
8251 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8252 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8253 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8254 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8256 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
8257 " Cccccccccccccc cccccccccc,\n"
8258 " Cccccccccccccc cccccccccc,\n"
8259 " Cccccccccccccc cccccccccc,\n"
8260 " Cccccccccccccc cccccccccc,\n"
8261 " Cccccccccccccc cccccccccc,\n"
8262 " Cccccccccccccc cccccccccc);");
8263 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8264 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8265 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8266 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
8267 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
8269 // Break after multi-line parameters.
8270 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8271 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8274 verifyFormat("void SomeLoooooooooooongFunction(\n"
8275 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
8276 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8277 " int bbbbbbbbbbbbb);");
8279 // Treat overloaded operators like other functions.
8280 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8281 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
8282 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8283 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
8284 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
8285 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
8287 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
8288 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8290 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
8291 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
8292 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8293 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8294 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
8295 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
8297 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
8298 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8299 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
8300 verifyGoogleFormat("template <typename T>\n"
8301 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8302 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
8303 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
8305 FormatStyle Style
= getLLVMStyle();
8306 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
8307 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8308 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
8310 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
8311 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8315 TEST_F(FormatTest
, DontBreakBeforeQualifiedOperator
) {
8316 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
8317 // Prefer keeping `::` followed by `operator` together.
8318 verifyFormat("const aaaa::bbbbbbb &\n"
8319 "ccccccccc::operator++() {\n"
8322 "const aaaa::bbbbbbb\n"
8323 "&ccccccccc::operator++() { stuff(); }",
8324 getLLVMStyleWithColumns(40));
8327 TEST_F(FormatTest
, TrailingReturnType
) {
8328 verifyFormat("auto foo() -> int;");
8329 // correct trailing return type spacing
8330 verifyFormat("auto operator->() -> int;");
8331 verifyFormat("auto operator++(int) -> int;");
8333 verifyFormat("struct S {\n"
8334 " auto bar() const -> int;\n"
8336 verifyFormat("template <size_t Order, typename T>\n"
8337 "auto load_img(const std::string &filename)\n"
8338 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
8339 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
8340 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
8341 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
8342 verifyFormat("template <typename T>\n"
8343 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
8344 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
8346 FormatStyle Style
= getLLVMStyleWithColumns(60);
8347 verifyFormat("#define MAKE_DEF(NAME) \\\n"
8348 " auto NAME() -> int { return 42; }",
8351 // Not trailing return types.
8352 verifyFormat("void f() { auto a = b->c(); }");
8353 verifyFormat("auto a = p->foo();");
8354 verifyFormat("int a = p->foo();");
8355 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };");
8358 TEST_F(FormatTest
, DeductionGuides
) {
8359 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;");
8360 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;");
8361 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;");
8363 "template <class... T>\n"
8364 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;");
8365 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;");
8366 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;");
8367 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;");
8368 verifyFormat("template <class T> A() -> A<(3 < 2)>;");
8369 verifyFormat("template <class T> A() -> A<((3) < (2))>;");
8370 verifyFormat("template <class T> x() -> x<1>;");
8371 verifyFormat("template <class T> explicit x(T &) -> x<1>;");
8373 verifyFormat("A(const char *) -> A<string &>;");
8374 verifyFormat("A() -> A<int>;");
8376 // Ensure not deduction guides.
8377 verifyFormat("c()->f<int>();");
8378 verifyFormat("x()->foo<1>;");
8379 verifyFormat("x = p->foo<3>();");
8380 verifyFormat("x()->x<1>();");
8383 TEST_F(FormatTest
, BreaksFunctionDeclarationsWithTrailingTokens
) {
8384 // Avoid breaking before trailing 'const' or other trailing annotations, if
8385 // they are not function-like.
8386 FormatStyle Style
= getGoogleStyleWithColumns(47);
8387 verifyFormat("void someLongFunction(\n"
8388 " int someLoooooooooooooongParameter) const {\n}",
8389 getLLVMStyleWithColumns(47));
8390 verifyFormat("LoooooongReturnType\n"
8391 "someLoooooooongFunction() const {}",
8392 getLLVMStyleWithColumns(47));
8393 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
8396 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8397 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
8398 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8399 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
8400 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
8401 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
8402 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
8403 " aaaaaaaaaaa aaaaa) const override;");
8405 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8406 " const override;");
8408 // Even if the first parameter has to be wrapped.
8409 verifyFormat("void someLongFunction(\n"
8410 " int someLongParameter) const {}",
8411 getLLVMStyleWithColumns(46));
8412 verifyFormat("void someLongFunction(\n"
8413 " int someLongParameter) const {}",
8415 verifyFormat("void someLongFunction(\n"
8416 " int someLongParameter) override {}",
8418 verifyFormat("void someLongFunction(\n"
8419 " int someLongParameter) OVERRIDE {}",
8421 verifyFormat("void someLongFunction(\n"
8422 " int someLongParameter) final {}",
8424 verifyFormat("void someLongFunction(\n"
8425 " int someLongParameter) FINAL {}",
8427 verifyFormat("void someLongFunction(\n"
8428 " int parameter) const override {}",
8431 Style
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
8432 verifyFormat("void someLongFunction(\n"
8433 " int someLongParameter) const\n"
8438 Style
.BreakBeforeBraces
= FormatStyle::BS_Whitesmiths
;
8439 verifyFormat("void someLongFunction(\n"
8440 " int someLongParameter) const\n"
8445 // Unless these are unknown annotations.
8446 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
8447 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8448 " LONG_AND_UGLY_ANNOTATION;");
8450 // Breaking before function-like trailing annotations is fine to keep them
8451 // close to their arguments.
8452 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8453 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8454 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8455 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
8456 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
8457 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
8458 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
8459 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
8460 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
8463 "void aaaaaaaaaaaaaaaaaa()\n"
8464 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
8465 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
8466 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8467 " __attribute__((unused));");
8469 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8470 " GUARDED_BY(aaaaaaaaaaaa);");
8472 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8473 " GUARDED_BY(aaaaaaaaaaaa);");
8475 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8476 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8478 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
8479 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8482 TEST_F(FormatTest
, FunctionAnnotations
) {
8483 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8484 "int OldFunction(const string ¶meter) {}");
8485 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8486 "string OldFunction(const string ¶meter) {}");
8487 verifyFormat("template <typename T>\n"
8488 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
8489 "string OldFunction(const string ¶meter) {}");
8491 // Not function annotations.
8492 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8493 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
8494 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
8495 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
8496 verifyFormat("MACRO(abc).function() // wrap\n"
8498 verifyFormat("MACRO(abc)->function() // wrap\n"
8500 verifyFormat("MACRO(abc)::function() // wrap\n"
8504 TEST_F(FormatTest
, BreaksDesireably
) {
8505 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8506 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
8507 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
8508 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
8513 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
8516 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8521 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8522 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8524 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8525 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
8527 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8528 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8532 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
8533 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8536 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8537 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8539 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8540 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
8543 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
8546 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8547 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8550 // Indent consistently independent of call expression and unary operator.
8551 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8552 " dddddddddddddddddddddddddddddd));");
8553 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8554 " dddddddddddddddddddddddddddddd));");
8555 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
8556 " dddddddddddddddddddddddddddddd));");
8558 // This test case breaks on an incorrect memoization, i.e. an optimization not
8559 // taking into account the StopAt value.
8561 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8562 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8563 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
8564 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8566 verifyFormat("{\n {\n {\n"
8567 " Annotation.SpaceRequiredBefore =\n"
8568 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
8569 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
8572 // Break on an outer level if there was a break on an inner level.
8573 verifyFormat("f(g(h(a, // comment\n"
8577 "f(g(h(a, // comment\n"
8578 " b, c), d, e), x, y);");
8580 // Prefer breaking similar line breaks.
8582 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
8583 " NSTrackingMouseEnteredAndExited |\n"
8584 " NSTrackingActiveAlways;");
8587 TEST_F(FormatTest
, FormatsDeclarationsOnePerLine
) {
8588 FormatStyle NoBinPacking
= getGoogleStyle();
8589 NoBinPacking
.BinPackParameters
= false;
8590 NoBinPacking
.BinPackArguments
= true;
8591 verifyFormat("void f() {\n"
8592 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
8593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
8596 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
8597 " int aaaaaaaaaaaaaaaaaaaa,\n"
8598 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8601 NoBinPacking
.AllowAllParametersOfDeclarationOnNextLine
= false;
8602 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8603 " vector<int> bbbbbbbbbbbbbbb);",
8605 // FIXME: This behavior difference is probably not wanted. However, currently
8606 // we cannot distinguish BreakBeforeParameter being set because of the wrapped
8607 // template arguments from BreakBeforeParameter being set because of the
8608 // one-per-line formatting.
8610 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8611 " aaaaaaaaaa> aaaaaaaaaa);",
8614 "void fffffffffff(\n"
8615 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
8619 TEST_F(FormatTest
, FormatsOneParameterPerLineIfNecessary
) {
8620 FormatStyle NoBinPacking
= getGoogleStyle();
8621 NoBinPacking
.BinPackParameters
= false;
8622 NoBinPacking
.BinPackArguments
= false;
8623 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
8624 " aaaaaaaaaaaaaaaaaaaa,\n"
8625 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
8627 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
8629 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
8632 "aaaaaaaa(aaaaaaaaaaaaa,\n"
8633 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8634 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
8635 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8636 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
8638 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8639 " .aaaaaaaaaaaaaaaaaa();",
8641 verifyFormat("void f() {\n"
8642 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8643 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
8648 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
8653 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
8654 " ddddddddddddddddddddddddddddd),\n"
8658 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
8659 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
8660 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
8661 " aaaaaaaaaaaaaaaaaa;",
8663 verifyFormat("a(\"a\"\n"
8667 NoBinPacking
.AllowAllParametersOfDeclarationOnNextLine
= false;
8668 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
8670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8674 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
8679 "template <class SomeType, class SomeOtherType>\n"
8680 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
8684 TEST_F(FormatTest
, AdaptiveOnePerLineFormatting
) {
8685 FormatStyle Style
= getLLVMStyleWithColumns(15);
8686 Style
.ExperimentalAutoDetectBinPacking
= true;
8687 verifyFormat("aaa(aaaa,\n"
8693 "aaa(aaaa,\n" // one-per-line
8696 "aaa(aaaa, aaaa, aaaa);", // inconclusive
8698 verifyFormat("aaa(aaaa, aaaa,\n"
8702 "aaa(aaaa, aaaa,\n" // bin-packed
8704 "aaa(aaaa, aaaa, aaaa);", // inconclusive
8708 TEST_F(FormatTest
, FormatsBuilderPattern
) {
8709 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
8710 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
8711 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
8712 " .StartsWith(\".init\", ORDER_INIT)\n"
8713 " .StartsWith(\".fini\", ORDER_FINI)\n"
8714 " .StartsWith(\".hash\", ORDER_HASH)\n"
8715 " .Default(ORDER_TEXT);");
8717 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
8718 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
8719 verifyFormat("aaaaaaa->aaaaaaa\n"
8720 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8721 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8722 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
8724 "aaaaaaa->aaaaaaa\n"
8725 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8726 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
8728 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
8729 " aaaaaaaaaaaaaa);");
8731 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
8732 " aaaaaa->aaaaaaaaaaaa()\n"
8733 " ->aaaaaaaaaaaaaaaa(\n"
8734 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8735 " ->aaaaaaaaaaaaaaaaa();");
8738 " someo->Add((new util::filetools::Handler(dir))\n"
8739 " ->OnEvent1(NewPermanentCallback(\n"
8740 " this, &HandlerHolderClass::EventHandlerCBA))\n"
8741 " ->OnEvent2(NewPermanentCallback(\n"
8742 " this, &HandlerHolderClass::EventHandlerCBB))\n"
8743 " ->OnEvent3(NewPermanentCallback(\n"
8744 " this, &HandlerHolderClass::EventHandlerCBC))\n"
8745 " ->OnEvent5(NewPermanentCallback(\n"
8746 " this, &HandlerHolderClass::EventHandlerCBD))\n"
8747 " ->OnEvent6(NewPermanentCallback(\n"
8748 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
8752 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
8753 verifyFormat("aaaaaaaaaaaaaaa()\n"
8754 " .aaaaaaaaaaaaaaa()\n"
8755 " .aaaaaaaaaaaaaaa()\n"
8756 " .aaaaaaaaaaaaaaa()\n"
8757 " .aaaaaaaaaaaaaaa();");
8758 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
8759 " .aaaaaaaaaaaaaaa()\n"
8760 " .aaaaaaaaaaaaaaa()\n"
8761 " .aaaaaaaaaaaaaaa();");
8762 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
8763 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
8764 " .aaaaaaaaaaaaaaa();");
8765 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
8766 " ->aaaaaaaaaaaaaae(0)\n"
8767 " ->aaaaaaaaaaaaaaa();");
8769 // Don't linewrap after very short segments.
8770 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8771 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8772 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8773 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8774 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8775 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8776 verifyFormat("aaa()\n"
8777 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8778 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8779 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
8781 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
8782 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
8783 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
8784 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
8785 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
8786 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
8788 // Prefer not to break after empty parentheses.
8789 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
8790 " First->LastNewlineOffset);");
8792 // Prefer not to create "hanging" indents.
8794 "return !soooooooooooooome_map\n"
8795 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8798 "return aaaaaaaaaaaaaaaa\n"
8799 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
8800 " .aaaa(aaaaaaaaaaaaaa);");
8801 // No hanging indent here.
8802 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
8803 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8804 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
8805 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8806 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
8807 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8808 getLLVMStyleWithColumns(60));
8809 verifyFormat("aaaaaaaaaaaaaaaaaa\n"
8810 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
8811 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8812 getLLVMStyleWithColumns(59));
8813 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8814 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8815 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8817 // Dont break if only closing statements before member call
8818 verifyFormat("test() {\n"
8824 verifyFormat("test() {\n"
8833 verifyFormat("test() {\n"
8841 verifyFormat("test() {\n"
8846 " .foo(\"aaaaaaaaaaaaaaaaa\"\n"
8849 getLLVMStyleWithColumns(30));
8852 TEST_F(FormatTest
, BreaksAccordingToOperatorPrecedence
) {
8854 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
8855 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
8857 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
8858 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
8860 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
8861 " ccccccccccccccccccccccccc) {\n}");
8862 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
8863 " ccccccccccccccccccccccccc) {\n}");
8865 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
8866 " ccccccccccccccccccccccccc) {\n}");
8867 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
8868 " ccccccccccccccccccccccccc) {\n}");
8871 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
8872 " ccccccccccccccccccccccccc) {\n}");
8874 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
8875 " ccccccccccccccccccccccccc) {\n}");
8877 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
8878 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
8879 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
8880 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
8881 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
8882 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
8883 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
8884 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
8886 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
8887 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
8888 " aaaaaaaaaaaaaaa != aa) {\n}");
8889 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
8890 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
8891 " aaaaaaaaaaaaaaa != aa) {\n}");
8894 TEST_F(FormatTest
, BreaksAfterAssignments
) {
8897 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
8898 " SI->getPointerAddressSpaceee());");
8900 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
8901 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
8904 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
8905 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
8906 verifyFormat("unsigned OriginalStartColumn =\n"
8907 " SourceMgr.getSpellingColumnNumber(\n"
8908 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
8912 TEST_F(FormatTest
, ConfigurableBreakAssignmentPenalty
) {
8913 FormatStyle Style
= getLLVMStyle();
8914 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
8915 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
8918 Style
.PenaltyBreakAssignment
= 20;
8919 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
8920 " cccccccccccccccccccccccccc;",
8924 TEST_F(FormatTest
, AlignsAfterAssignments
) {
8926 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8927 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8929 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8930 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8932 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8933 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8935 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8936 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
8938 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
8939 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
8940 " aaaaaaaaaaaaaaaaaaaaaaaa;");
8943 TEST_F(FormatTest
, AlignsAfterReturn
) {
8945 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8946 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
8948 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
8949 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
8951 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
8952 " aaaaaaaaaaaaaaaaaaaaaa();");
8954 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
8955 " aaaaaaaaaaaaaaaaaaaaaa());");
8956 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8957 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
8958 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8959 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
8960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
8961 verifyFormat("return\n"
8962 " // true if code is one of a or b.\n"
8963 " code == a || code == b;");
8966 TEST_F(FormatTest
, AlignsAfterOpenBracket
) {
8968 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
8969 " aaaaaaaaa aaaaaaa) {}");
8971 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
8972 " aaaaaaaaaaa aaaaaaaaa);");
8974 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
8975 " aaaaaaaaaaaaaaaaaaaaa));");
8976 FormatStyle Style
= getLLVMStyle();
8977 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
8978 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
8979 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
8981 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
8982 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
8984 verifyFormat("SomeLongVariableName->someFunction(\n"
8985 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
8988 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
8989 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
8992 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
8993 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
8996 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
8997 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9000 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
9001 " ccccccc(aaaaaaaaaaaaaaaaa, //\n"
9005 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
9006 Style
.BinPackArguments
= false;
9007 Style
.BinPackParameters
= false;
9008 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9009 " aaaaaaaaaaa aaaaaaaa,\n"
9010 " aaaaaaaaa aaaaaaa,\n"
9011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
9013 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9014 " aaaaaaaaaaa aaaaaaaaa,\n"
9015 " aaaaaaaaaaa aaaaaaaaa,\n"
9016 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9018 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9019 " aaaaaaaaaaaaaaa,\n"
9020 " aaaaaaaaaaaaaaaaaaaaa,\n"
9021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
9024 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9025 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9028 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9029 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
9032 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9033 " aaaaaaaaaaaaaaaaaaaaa(\n"
9034 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
9035 " aaaaaaaaaaaaaaaa);",
9038 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9039 " aaaaaaaaaaaaaaaaaaaaa(\n"
9040 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
9041 " aaaaaaaaaaaaaaaa);",
9044 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
9045 Style
.BinPackArguments
= false;
9046 Style
.BinPackParameters
= false;
9047 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9048 " aaaaaaaaaaa aaaaaaaa,\n"
9049 " aaaaaaaaa aaaaaaa,\n"
9050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9053 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
9054 " aaaaaaaaaaa aaaaaaaaa,\n"
9055 " aaaaaaaaaaa aaaaaaaaa,\n"
9056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9059 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
9060 " aaaaaaaaaaaaaaa,\n"
9061 " aaaaaaaaaaaaaaaaaaaaa,\n"
9062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9065 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
9066 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9069 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
9070 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9074 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9075 " aaaaaaaaaaaaaaaaaaaaa(\n"
9076 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9078 " aaaaaaaaaaaaaaaa\n"
9082 "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
9083 " aaaaaaaaaaaaaaaaaaaaa(\n"
9084 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n"
9086 " aaaaaaaaaaaaaaaa\n"
9091 TEST_F(FormatTest
, ParenthesesAndOperandAlignment
) {
9092 FormatStyle Style
= getLLVMStyleWithColumns(40);
9093 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9094 " bbbbbbbbbbbbbbbbbbbbbb);",
9096 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_Align
;
9097 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
9098 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9099 " bbbbbbbbbbbbbbbbbbbbbb);",
9101 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
9102 Style
.AlignOperands
= FormatStyle::OAS_Align
;
9103 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9104 " bbbbbbbbbbbbbbbbbbbbbb);",
9106 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
9107 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
9108 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
9109 " bbbbbbbbbbbbbbbbbbbbbb);",
9113 TEST_F(FormatTest
, BreaksConditionalExpressions
) {
9115 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9116 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9117 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9119 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9120 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9121 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9123 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9124 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9125 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n"
9126 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9127 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9129 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
9130 " : aaaaaaaaaaaaa);");
9132 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9133 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9134 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9135 " aaaaaaaaaaaaa);");
9137 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9138 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9139 " aaaaaaaaaaaaa);");
9140 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9141 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9143 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9145 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9147 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9149 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9151 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9152 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9154 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9155 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9156 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
9157 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9158 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9159 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9160 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9162 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9163 " : aaaaaaaaaaaaaaaa;");
9165 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9166 " ? aaaaaaaaaaaaaaa\n"
9167 " : aaaaaaaaaaaaaaa;");
9168 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9172 verifyFormat("return aaaa == bbbb\n"
9176 verifyFormat("unsigned Indent =\n"
9177 " format(TheLine.First,\n"
9178 " IndentForLevel[TheLine.Level] >= 0\n"
9179 " ? IndentForLevel[TheLine.Level]\n"
9181 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9182 getLLVMStyleWithColumns(60));
9183 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9184 " ? aaaaaaaaaaaaaaa\n"
9185 " : bbbbbbbbbbbbbbb //\n"
9186 " ? ccccccccccccccc\n"
9187 " : ddddddddddddddd;");
9188 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
9189 " ? aaaaaaaaaaaaaaa\n"
9190 " : (bbbbbbbbbbbbbbb //\n"
9191 " ? ccccccccccccccc\n"
9192 " : ddddddddddddddd);");
9194 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9195 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
9196 " aaaaaaaaaaaaaaaaaaaaa +\n"
9197 " aaaaaaaaaaaaaaaaaaaaa\n"
9200 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9201 " : aaaaaaaaaaaaaaaaaaaaaa\n"
9202 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
9204 FormatStyle NoBinPacking
= getLLVMStyle();
9205 NoBinPacking
.BinPackArguments
= false;
9209 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9210 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9211 " ? aaaaaaaaaaaaaaa\n"
9212 " : aaaaaaaaaaaaaaa);\n"
9218 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
9219 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9220 " ?: aaaaaaaaaaaaaaa);\n"
9224 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
9226 " ccccccccccccccccccccccccccccccccccccccc\n"
9227 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9228 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
9230 // Assignments in conditional expressions. Apparently not uncommon :-(.
9231 verifyFormat("return a != b\n"
9235 verifyFormat("return a != b\n"
9242 verifyFormat("return a != b\n"
9250 // Chained conditionals
9251 FormatStyle Style
= getLLVMStyleWithColumns(70);
9252 Style
.AlignOperands
= FormatStyle::OAS_Align
;
9253 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9254 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9255 " : 3333333333333333;",
9257 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9258 " : bbbbbbbbbb ? 2222222222222222\n"
9259 " : 3333333333333333;",
9261 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n"
9262 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n"
9263 " : 3333333333333333;",
9265 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9266 " : bbbbbbbbbbbbbb ? 222222\n"
9269 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9270 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9271 " : cccccccccccccc ? 3333333333333333\n"
9272 " : 4444444444444444;",
9274 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n"
9275 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9276 " : 3333333333333333;",
9278 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9279 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9280 " : (aaa ? bbb : ccc);",
9283 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9284 " : cccccccccccccccccc)\n"
9285 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9286 " : 3333333333333333;",
9289 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9290 " : cccccccccccccccccc)\n"
9291 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9292 " : 3333333333333333;",
9295 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9296 " : dddddddddddddddddd)\n"
9297 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9298 " : 3333333333333333;",
9301 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9302 " : dddddddddddddddddd)\n"
9303 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9304 " : 3333333333333333;",
9307 "return aaaaaaaaa ? 1111111111111111\n"
9308 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9309 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9310 " : dddddddddddddddddd)",
9313 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n"
9314 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9315 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9316 " : cccccccccccccccccc);",
9319 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9320 " : ccccccccccccccc ? dddddddddddddddddd\n"
9321 " : eeeeeeeeeeeeeeeeee)\n"
9322 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9323 " : 3333333333333333;",
9326 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9327 " : ccccccccccccccc ? dddddddddddddddddd\n"
9328 " : eeeeeeeeeeeeeeeeee)\n"
9329 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9330 " : 3333333333333333;",
9333 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9334 " : cccccccccccc ? dddddddddddddddddd\n"
9335 " : eeeeeeeeeeeeeeeeee)\n"
9336 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9337 " : 3333333333333333;",
9340 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9341 " : cccccccccccccccccc\n"
9342 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9343 " : 3333333333333333;",
9346 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9347 " : cccccccccccccccc ? dddddddddddddddddd\n"
9348 " : eeeeeeeeeeeeeeeeee\n"
9349 " : bbbbbbbbbbbbbb ? 2222222222222222\n"
9350 " : 3333333333333333;",
9352 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n"
9353 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9354 " : cccccccccccccccccc ? dddddddddddddddddd\n"
9355 " : eeeeeeeeeeeeeeeeee)\n"
9356 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9357 " : 3333333333333333;",
9359 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n"
9360 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n"
9361 " : cccccccccccccccc ? dddddddddddddddddd\n"
9362 " : eeeeeeeeeeeeeeeeee\n"
9363 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n"
9364 " : 3333333333333333;",
9367 Style
.AlignOperands
= FormatStyle::OAS_DontAlign
;
9368 Style
.BreakBeforeTernaryOperators
= false;
9369 // FIXME: Aligning the question marks is weird given DontAlign.
9370 // Consider disabling this alignment in this case. Also check whether this
9371 // will render the adjustment from https://reviews.llvm.org/D82199
9373 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n"
9374 " bbbb ? cccccccccccccccccc :\n"
9379 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9384 " return JJJJJJJJJJJJJJ(\n"
9385 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9389 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n"
9394 " return JJJJJJJJJJJJJJ(\n"
9395 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n"
9399 getGoogleStyle(FormatStyle::LK_JavaScript
));
9402 TEST_F(FormatTest
, BreaksConditionalExpressionsAfterOperator
) {
9403 FormatStyle Style
= getLLVMStyleWithColumns(70);
9404 Style
.BreakBeforeTernaryOperators
= false;
9406 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9407 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9408 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9411 "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
9412 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9416 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9419 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n"
9420 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9424 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
9428 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9429 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9434 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9435 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9441 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9442 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9444 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
9448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9449 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9450 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9452 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
9453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
9454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
9456 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9458 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9459 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9460 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9462 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
9463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
9465 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
9468 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9469 " aaaaaaaaaaaaaaa :\n"
9470 " aaaaaaaaaaaaaaa;",
9472 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
9477 verifyFormat("unsigned Indent =\n"
9478 " format(TheLine.First,\n"
9479 " IndentForLevel[TheLine.Level] >= 0 ?\n"
9480 " IndentForLevel[TheLine.Level] :\n"
9482 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
9484 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9485 " aaaaaaaaaaaaaaa :\n"
9486 " bbbbbbbbbbbbbbb ? //\n"
9487 " ccccccccccccccc :\n"
9488 " ddddddddddddddd;",
9490 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
9491 " aaaaaaaaaaaaaaa :\n"
9492 " (bbbbbbbbbbbbbbb ? //\n"
9493 " ccccccccccccccc :\n"
9494 " ddddddddddddddd);",
9496 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9497 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
9498 " ccccccccccccccccccccccccccc;",
9500 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
9502 " bbbbbbbbbbbbbbb + cccccccccccccccc;",
9505 // Chained conditionals
9506 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9507 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9508 " 3333333333333333;",
9510 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9511 " bbbbbbbbbb ? 2222222222222222 :\n"
9512 " 3333333333333333;",
9514 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n"
9515 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9516 " 3333333333333333;",
9518 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9519 " bbbbbbbbbbbbbbbb ? 222222 :\n"
9522 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9523 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9524 " cccccccccccccccc ? 3333333333333333 :\n"
9525 " 4444444444444444;",
9527 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n"
9528 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9529 " 3333333333333333;",
9531 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9532 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9533 " (aaa ? bbb : ccc);",
9536 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9537 " cccccccccccccccccc) :\n"
9538 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9539 " 3333333333333333;",
9542 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9543 " cccccccccccccccccc) :\n"
9544 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9545 " 3333333333333333;",
9548 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9549 " dddddddddddddddddd) :\n"
9550 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9551 " 3333333333333333;",
9554 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9555 " dddddddddddddddddd) :\n"
9556 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9557 " 3333333333333333;",
9560 "return aaaaaaaaa ? 1111111111111111 :\n"
9561 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9562 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9563 " dddddddddddddddddd)",
9566 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n"
9567 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9568 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9569 " cccccccccccccccccc);",
9572 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9573 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9574 " eeeeeeeeeeeeeeeeee) :\n"
9575 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9576 " 3333333333333333;",
9579 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9580 " ccccccccccccc ? dddddddddddddddddd :\n"
9581 " eeeeeeeeeeeeeeeeee) :\n"
9582 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9583 " 3333333333333333;",
9586 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9587 " ccccccccccccccccc ? dddddddddddddddddd :\n"
9588 " eeeeeeeeeeeeeeeeee) :\n"
9589 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9590 " 3333333333333333;",
9593 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9594 " cccccccccccccccccc :\n"
9595 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9596 " 3333333333333333;",
9599 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9600 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9601 " eeeeeeeeeeeeeeeeee :\n"
9602 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9603 " 3333333333333333;",
9605 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9606 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9607 " cccccccccccccccccc ? dddddddddddddddddd :\n"
9608 " eeeeeeeeeeeeeeeeee) :\n"
9609 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9610 " 3333333333333333;",
9612 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n"
9613 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n"
9614 " cccccccccccccccccccc ? dddddddddddddddddd :\n"
9615 " eeeeeeeeeeeeeeeeee :\n"
9616 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n"
9617 " 3333333333333333;",
9621 TEST_F(FormatTest
, DeclarationsOfMultipleVariables
) {
9622 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
9623 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
9624 verifyFormat("bool a = true, b = false;");
9626 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
9627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
9628 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
9629 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
9631 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
9632 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
9634 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
9635 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
9636 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
9637 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
9638 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
9639 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
9641 FormatStyle Style
= getGoogleStyle();
9642 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
9643 Style
.DerivePointerAlignment
= false;
9644 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
9645 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
9646 " *b = bbbbbbbbbbbbbbbbbbb;",
9648 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
9649 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
9651 verifyFormat("vector<int*> a, b;", Style
);
9652 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style
);
9653 verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style
);
9654 verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style
);
9655 verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}",
9657 verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}",
9660 "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}",
9663 verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style
);
9664 verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style
);
9665 verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style
);
9666 verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style
);
9667 verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}",
9671 TEST_F(FormatTest
, ConditionalExpressionsInBrackets
) {
9672 verifyFormat("arr[foo ? bar : baz];");
9673 verifyFormat("f()[foo ? bar : baz];");
9674 verifyFormat("(a + b)[foo ? bar : baz];");
9675 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
9678 TEST_F(FormatTest
, AlignsStringLiterals
) {
9679 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
9680 " \"short literal\");");
9682 "looooooooooooooooooooooooongFunction(\n"
9683 " \"short literal\"\n"
9684 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
9685 verifyFormat("someFunction(\"Always break between multi-line\"\n"
9686 " \" string literals\",\n"
9687 " also, other, parameters);");
9688 verifyFormat("fun + \"1243\" /* comment */\n"
9690 "fun + \"1243\" /* comment */\n"
9692 getLLVMStyleWithColumns(28));
9694 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
9695 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
9696 " \"aaaaaaaaaaaaaaaa\";",
9698 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
9699 "aaaaaaaaaaaaaaaaaaaaa\" "
9700 "\"aaaaaaaaaaaaaaaa\";");
9701 verifyFormat("a = a + \"a\"\n"
9704 verifyFormat("f(\"a\", \"b\"\n"
9708 "#define LL_FORMAT \"ll\"\n"
9709 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
9710 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
9712 verifyFormat("#define A(X) \\\n"
9713 " \"aaaaa\" #X \"bbbbbb\" \\\n"
9715 getLLVMStyleWithColumns(23));
9716 verifyFormat("#define A \"def\"\n"
9717 "f(\"abc\" A \"ghi\"\n"
9720 verifyFormat("f(L\"a\"\n"
9722 verifyFormat("#define A(X) \\\n"
9723 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
9725 getLLVMStyleWithColumns(25));
9727 verifyFormat("f(@\"a\"\n"
9729 verifyFormat("NSString s = @\"a\"\n"
9732 verifyFormat("NSString s = @\"a\"\n"
9737 TEST_F(FormatTest
, ReturnTypeBreakingStyle
) {
9738 FormatStyle Style
= getLLVMStyle();
9739 // No declarations or definitions should be moved to own line.
9740 Style
.AlwaysBreakAfterReturnType
= FormatStyle::RTBS_None
;
9741 verifyFormat("class A {\n"
9742 " int f() { return 1; }\n"
9745 "int f() { return 1; }\n"
9749 // All declarations and definitions should have the return type moved to its
9751 Style
.AlwaysBreakAfterReturnType
= FormatStyle::RTBS_All
;
9752 Style
.TypenameMacros
= {"LIST"};
9753 verifyFormat("SomeType\n"
9754 "funcdecl(LIST(uint64_t));",
9756 verifyFormat("class E {\n"
9772 // Top-level definitions, and no kinds of declarations should have the
9773 // return type moved to its own line.
9774 Style
.AlwaysBreakAfterReturnType
= FormatStyle::RTBS_TopLevelDefinitions
;
9775 verifyFormat("class B {\n"
9776 " int f() { return 1; }\n"
9786 // Top-level definitions and declarations should have the return type moved
9788 Style
.AlwaysBreakAfterReturnType
= FormatStyle::RTBS_TopLevel
;
9789 verifyFormat("class C {\n"
9790 " int f() { return 1; }\n"
9801 // All definitions should have the return type moved to its own line, but no
9802 // kinds of declarations.
9803 Style
.AlwaysBreakAfterReturnType
= FormatStyle::RTBS_AllDefinitions
;
9804 verifyFormat("class D {\n"
9817 verifyFormat("const char *\n"
9818 "f(void) {\n" // Break here.
9821 "const char *bar(void);", // No break here.
9823 verifyFormat("template <class T>\n"
9825 "f(T &c) {\n" // Break here.
9828 "template <class T> T *f(T &c);", // No break here.
9830 verifyFormat("class C {\n"
9841 verifyFormat("void\n"
9842 "A::operator()() {}\n"
9844 "A::operator>>() {}\n"
9846 "A::operator+() {}\n"
9848 "A::operator*() {}\n"
9850 "A::operator->() {}\n"
9852 "A::operator void *() {}\n"
9854 "A::operator void &() {}\n"
9856 "A::operator void &&() {}\n"
9858 "A::operator char *() {}\n"
9860 "A::operator[]() {}\n"
9862 "A::operator!() {}\n"
9864 "A::operator**() {}\n"
9866 "A::operator<Foo> *() {}\n"
9868 "A::operator<Foo> **() {}\n"
9870 "A::operator<Foo> &() {}\n"
9872 "A::operator void **() {}",
9874 verifyFormat("constexpr auto\n"
9875 "operator()() const -> reference {}\n"
9877 "operator>>() const -> reference {}\n"
9879 "operator+() const -> reference {}\n"
9881 "operator*() const -> reference {}\n"
9883 "operator->() const -> reference {}\n"
9885 "operator++() const -> reference {}\n"
9887 "operator void *() const -> reference {}\n"
9889 "operator void **() const -> reference {}\n"
9891 "operator void *() const -> reference {}\n"
9893 "operator void &() const -> reference {}\n"
9895 "operator void &&() const -> reference {}\n"
9897 "operator char *() const -> reference {}\n"
9899 "operator!() const -> reference {}\n"
9901 "operator[]() const -> reference {}",
9903 verifyFormat("void *operator new(std::size_t s);", // No break here.
9905 verifyFormat("void *\n"
9906 "operator new(std::size_t s) {}",
9908 verifyFormat("void *\n"
9909 "operator delete[](void *ptr) {}",
9911 Style
.BreakBeforeBraces
= FormatStyle::BS_Stroustrup
;
9912 verifyFormat("const char *\n"
9913 "f(void)\n" // Break here.
9917 "const char *bar(void);", // No break here.
9919 verifyFormat("template <class T>\n"
9920 "T *\n" // Problem here: no line break
9921 "f(T &c)\n" // Break here.
9925 "template <class T> T *f(T &c);", // No break here.
9927 verifyFormat("int\n"
9933 verifyFormat("int\n"
9939 verifyFormat("int\n"
9940 "foo(A<B<bool>, 8> a)\n"
9945 verifyFormat("int\n"
9946 "foo(A<B<8>, bool> a)\n"
9951 verifyFormat("int\n"
9952 "foo(A<B<bool>, bool> a)\n"
9957 verifyFormat("int\n"
9958 "foo(A<B<8>, 8> a)\n"
9964 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
9965 Style
.BraceWrapping
.AfterFunction
= true;
9966 verifyFormat("int f(i);\n" // No break here.
9967 "int\n" // Break here.
9972 "int\n" // Break here.
9978 verifyFormat("int f(a, b, c);\n" // No break here.
9979 "int\n" // Break here.
9980 "f(a, b, c)\n" // Break here.
9984 " return a + b < c;\n"
9986 "int\n" // Break here.
9987 "f(a, b, c)\n" // Break here.
9991 " return a + b < c;\n"
9994 verifyFormat("byte *\n" // Break here.
9995 "f(a)\n" // Break here.
10001 verifyFormat("byte *\n"
10003 "byte /* K&R C */ a[];\n"
10009 "byte /* K&R C */ *p;\n"
10014 verifyFormat("bool f(int a, int) override;\n"
10015 "Bar g(int a, Bar) final;\n"
10016 "Bar h(a, Bar) final;",
10018 verifyFormat("int\n"
10021 verifyFormat("bool\n"
10022 "f(size_t = 0, bool b = false)\n"
10028 // The return breaking style doesn't affect:
10029 // * function and object definitions with attribute-like macros
10030 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10031 " ABSL_GUARDED_BY(mutex) = {};",
10032 getGoogleStyleWithColumns(40));
10033 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10034 " ABSL_GUARDED_BY(mutex); // comment",
10035 getGoogleStyleWithColumns(40));
10036 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n"
10037 " ABSL_GUARDED_BY(mutex1)\n"
10038 " ABSL_GUARDED_BY(mutex2);",
10039 getGoogleStyleWithColumns(40));
10040 verifyFormat("Tttttt f(int a, int b)\n"
10041 " ABSL_GUARDED_BY(mutex1)\n"
10042 " ABSL_GUARDED_BY(mutex2);",
10043 getGoogleStyleWithColumns(40));
10045 verifyGoogleFormat("typedef ATTR(X) char x;");
10047 Style
= getGNUStyle();
10049 // Test for comments at the end of function declarations.
10050 verifyFormat("void\n"
10051 "foo (int a, /*abc*/ int b) // def\n"
10056 verifyFormat("void\n"
10057 "foo (int a, /* abc */ int b) /* def */\n"
10062 // Definitions that should not break after return type
10063 verifyFormat("void foo (int a, int b); // def", Style
);
10064 verifyFormat("void foo (int a, int b); /* def */", Style
);
10065 verifyFormat("void foo (int a, int b);", Style
);
10068 TEST_F(FormatTest
, AlwaysBreakBeforeMultilineStrings
) {
10069 FormatStyle NoBreak
= getLLVMStyle();
10070 NoBreak
.AlwaysBreakBeforeMultilineStrings
= false;
10071 FormatStyle Break
= getLLVMStyle();
10072 Break
.AlwaysBreakBeforeMultilineStrings
= true;
10073 verifyFormat("aaaa = \"bbbb\"\n"
10076 verifyFormat("aaaa =\n"
10080 verifyFormat("aaaa(\"bbbb\"\n"
10083 verifyFormat("aaaa(\n"
10087 verifyFormat("aaaa(qqq, \"bbbb\"\n"
10090 verifyFormat("aaaa(qqq,\n"
10094 verifyFormat("aaaa(qqq,\n"
10098 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
10101 verifyFormat("string s = someFunction(\n"
10106 // As we break before unary operators, breaking right after them is bad.
10107 verifyFormat("string foo = abc ? \"x\"\n"
10108 " \"blah blah blah blah blah blah\"\n"
10112 // Don't break if there is no column gain.
10113 verifyFormat("f(\"aaaa\"\n"
10117 // Treat literals with escaped newlines like multi-line string literals.
10118 verifyNoChange("x = \"a\\\n"
10122 verifyFormat("xxxx =\n"
10131 verifyFormat("NSString *const kString =\n"
10134 "NSString *const kString = @\"aaaa\"\n"
10138 Break
.ColumnLimit
= 0;
10139 verifyFormat("const char *hello = \"hello llvm\";", Break
);
10142 TEST_F(FormatTest
, AlignsPipes
) {
10144 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10145 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10146 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10148 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
10149 " << aaaaaaaaaaaaaaaaaaaa;");
10151 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10152 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10154 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
10155 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10157 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
10158 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
10159 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
10161 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10162 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10163 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10164 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10165 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10167 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10168 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
10169 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
10171 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10172 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10174 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
10175 " aaaaaaaaaaaaaaaaaaaaaaaaaa);");
10177 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
10178 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
10179 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10180 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10181 " aaaaaaaaaaaaaaaaaaaaa)\n"
10182 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
10183 verifyFormat("LOG_IF(aaa == //\n"
10187 // But sometimes, breaking before the first "<<" is desirable.
10188 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10189 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
10190 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
10191 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10192 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10193 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
10194 " << BEF << IsTemplate << Description << E->getType();");
10195 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10196 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10197 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10198 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
10199 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10200 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10204 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10205 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10207 // Incomplete string literal.
10208 verifyFormat("llvm::errs() << \"\n"
10210 "llvm::errs() << \"\n<<a;");
10212 verifyFormat("void f() {\n"
10213 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
10214 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
10218 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
10219 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10220 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
10223 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
10224 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10225 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
10226 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
10227 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
10228 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
10229 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
10232 TEST_F(FormatTest
, KeepStringLabelValuePairsOnALine
) {
10233 verifyFormat("return out << \"somepacket = {\\n\"\n"
10234 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
10235 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
10236 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
10237 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
10240 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10241 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
10242 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
10244 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
10245 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
10246 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
10247 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
10248 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
10249 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
10250 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
10253 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
10254 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
10257 // Breaking before the first "<<" is generally not desirable.
10260 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10261 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10262 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10263 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10264 getLLVMStyleWithColumns(70));
10265 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10266 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10267 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10268 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10269 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
10270 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
10271 getLLVMStyleWithColumns(70));
10273 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10274 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
10275 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
10276 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10277 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
10278 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
10279 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
10281 getLLVMStyleWithColumns(40));
10282 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
10283 " (aaaaaaa + aaaaa));",
10284 getLLVMStyleWithColumns(40));
10286 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
10287 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
10288 " bbbbbbbbbbbbbbbbbbbbbbb);");
10291 TEST_F(FormatTest
, UnderstandsEquals
) {
10293 "aaaaaaaaaaaaaaaaa =\n"
10294 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
10296 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10297 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10301 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10302 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
10305 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
10306 " 100000000 + 10000000) {\n}");
10309 TEST_F(FormatTest
, WrapsAtFunctionCallsIfNecessary
) {
10310 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10311 " .looooooooooooooooooooooooooooooooooooooongFunction();");
10313 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
10314 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
10317 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
10321 "ShortObject->shortFunction(\n"
10322 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
10323 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
10325 verifyFormat("loooooooooooooongFunction(\n"
10326 " LoooooooooooooongObject->looooooooooooooooongFunction());");
10329 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
10330 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
10332 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10333 " .WillRepeatedly(Return(SomeValue));");
10334 verifyFormat("void f() {\n"
10335 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
10337 " .WillRepeatedly(Return(SomeValue));\n"
10339 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
10340 " ccccccccccccccccccccccc);");
10341 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10343 " .aaaaa(aaaaa),\n"
10344 " aaaaaaaaaaaaaaaaaaaaa);");
10345 verifyFormat("void f() {\n"
10346 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10347 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
10349 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10350 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10351 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10352 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10353 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10354 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10355 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10356 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10357 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
10360 // Here, it is not necessary to wrap at "." or "->".
10361 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
10362 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
10364 "aaaaaaaaaaa->aaaaaaaaa(\n"
10365 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10366 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));");
10369 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10370 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
10371 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
10372 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10373 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
10374 " aaaaaaaaa()->aaaaaa()->aaaaa());");
10376 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10380 FormatStyle NoBinPacking
= getLLVMStyle();
10381 NoBinPacking
.BinPackParameters
= false;
10382 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10383 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
10384 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
10385 " aaaaaaaaaaaaaaaaaaa,\n"
10386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10389 // If there is a subsequent call, change to hanging indentation.
10391 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10392 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
10393 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10395 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10396 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
10397 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10398 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10399 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10400 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10402 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10405 TEST_F(FormatTest
, WrapsTemplateDeclarations
) {
10406 verifyFormat("template <typename T>\n"
10407 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10408 verifyFormat("template <typename T>\n"
10409 "// T should be one of {A, B}.\n"
10410 "virtual void loooooooooooongFunction(int Param1, int Param2);");
10412 "template <typename T>\n"
10413 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
10414 verifyFormat("template <typename T>\n"
10415 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
10416 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
10418 "template <typename T>\n"
10419 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
10420 " int Paaaaaaaaaaaaaaaaaaaaram2);");
10422 "template <typename T>\n"
10423 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
10424 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
10425 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10426 verifyFormat("template <typename T>\n"
10427 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10428 " int aaaaaaaaaaaaaaaaaaaaaa);");
10430 "template <typename T1, typename T2 = char, typename T3 = char,\n"
10431 " typename T4 = char>\n"
10433 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
10434 " template <typename> class cccccccccccccccccccccc,\n"
10435 " typename ddddddddddddd>\n"
10438 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
10439 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
10441 verifyFormat("void f() {\n"
10442 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
10443 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
10446 verifyFormat("template <typename T> class C {};");
10447 verifyFormat("template <typename T> void f();");
10448 verifyFormat("template <typename T> void f() {}");
10450 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10452 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
10453 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
10454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
10456 " bbbbbbbbbbbbbbbbbbbbbbbb);",
10457 getLLVMStyleWithColumns(72));
10458 verifyFormat("static_cast<A< //\n"
10462 "static_cast<A<//\n"
10466 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10467 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
10469 FormatStyle AlwaysBreak
= getLLVMStyle();
10470 AlwaysBreak
.AlwaysBreakTemplateDeclarations
= FormatStyle::BTDS_Yes
;
10471 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak
);
10472 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak
);
10473 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak
);
10474 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10475 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10476 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
10477 verifyFormat("template <template <typename> class Fooooooo,\n"
10478 " template <typename> class Baaaaaaar>\n"
10481 verifyFormat("template <typename T> // T can be A, B or C.\n"
10484 verifyFormat("template <typename T>\n"
10487 verifyFormat("template <typename T>\n"
10488 "ClassName(T) noexcept;",
10490 verifyFormat("template <typename T>\n"
10491 "POOR_NAME(T) noexcept;",
10493 verifyFormat("template <enum E> class A {\n"
10498 FormatStyle NeverBreak
= getLLVMStyle();
10499 NeverBreak
.AlwaysBreakTemplateDeclarations
= FormatStyle::BTDS_No
;
10500 verifyFormat("template <typename T> class C {};", NeverBreak
);
10501 verifyFormat("template <typename T> void f();", NeverBreak
);
10502 verifyFormat("template <typename T> void f() {}", NeverBreak
);
10503 verifyFormat("template <typename T> C(T) noexcept;", NeverBreak
);
10504 verifyFormat("template <typename T> ClassName(T) noexcept;", NeverBreak
);
10505 verifyFormat("template <typename T> POOR_NAME(T) noexcept;", NeverBreak
);
10506 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10507 "bbbbbbbbbbbbbbbbbbbb) {}",
10509 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10510 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
10511 " ccccccccccccccccccccccccccccccccccccccccccccccc);",
10513 verifyFormat("template <template <typename> class Fooooooo,\n"
10514 " template <typename> class Baaaaaaar>\n"
10517 verifyFormat("template <typename T> // T can be A, B or C.\n"
10520 verifyFormat("template <enum E> class A {\n"
10525 NeverBreak
.PenaltyBreakTemplateDeclaration
= 100;
10526 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa "
10527 "bbbbbbbbbbbbbbbbbbbb) {}",
10531 TEST_F(FormatTest
, WrapsTemplateDeclarationsWithComments
) {
10532 FormatStyle Style
= getGoogleStyle(FormatStyle::LK_Cpp
);
10533 Style
.ColumnLimit
= 60;
10534 verifyFormat("// Baseline - no comments.\n"
10536 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n"
10540 verifyFormat("template <\n"
10541 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10544 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10550 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
10552 "template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n"
10556 verifyFormat("template <\n"
10557 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10561 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n"
10567 "template <typename aaaaaaaaaa<\n"
10568 " bbbbbbbbbbbb>::value> // trailing loooong\n"
10571 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n"
10576 TEST_F(FormatTest
, WrapsTemplateParameters
) {
10577 FormatStyle Style
= getLLVMStyle();
10578 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
10579 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
10581 "template <typename... a> struct q {};\n"
10582 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
10583 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
10586 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_DontAlign
;
10587 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
10589 "template <typename... a> struct r {};\n"
10590 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
10591 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
10594 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
10595 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
10596 verifyFormat("template <typename... a> struct s {};\n"
10598 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10599 "aaaaaaaaaaaaaaaaaaaaaa,\n"
10600 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10601 "aaaaaaaaaaaaaaaaaaaaaa>\n"
10604 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
10605 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
10606 verifyFormat("template <typename... a> struct t {};\n"
10608 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10609 "aaaaaaaaaaaaaaaaaaaaaa,\n"
10610 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, "
10611 "aaaaaaaaaaaaaaaaaaaaaa>\n"
10616 TEST_F(FormatTest
, WrapsAtNestedNameSpecifiers
) {
10618 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10619 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10621 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10622 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10623 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
10625 // FIXME: Should we have the extra indent after the second break?
10627 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10628 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10632 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
10633 " cccccccccccccccccccccccccccccccccccccccccccccc());");
10635 // Breaking at nested name specifiers is generally not desirable.
10637 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10638 " aaaaaaaaaaaaaaaaaaaaaaa);");
10640 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
10641 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10642 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
10643 " aaaaaaaaaaaaaaaaaaaaa);",
10644 getLLVMStyleWithColumns(74));
10646 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
10647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10648 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10651 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
10652 " AndAnotherLongClassNameToShowTheIssue() {}\n"
10653 "LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
10654 " ~AndAnotherLongClassNameToShowTheIssue() {}");
10657 TEST_F(FormatTest
, UnderstandsTemplateParameters
) {
10658 verifyFormat("A<int> a;");
10659 verifyFormat("A<A<A<int>>> a;");
10660 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
10661 verifyFormat("bool x = a < 1 || 2 > a;");
10662 verifyFormat("bool x = 5 < f<int>();");
10663 verifyFormat("bool x = f<int>() > 5;");
10664 verifyFormat("bool x = 5 < a<int>::x;");
10665 verifyFormat("bool x = a < 4 ? a > 2 : false;");
10666 verifyFormat("bool x = f() ? a < 2 : a > 2;");
10668 verifyGoogleFormat("A<A<int>> a;");
10669 verifyGoogleFormat("A<A<A<int>>> a;");
10670 verifyGoogleFormat("A<A<A<A<int>>>> a;");
10671 verifyGoogleFormat("A<A<int> > a;");
10672 verifyGoogleFormat("A<A<A<int> > > a;");
10673 verifyGoogleFormat("A<A<A<A<int> > > > a;");
10674 verifyGoogleFormat("A<::A<int>> a;");
10675 verifyGoogleFormat("A<::A> a;");
10676 verifyGoogleFormat("A< ::A> a;");
10677 verifyGoogleFormat("A< ::A<int> > a;");
10678 verifyFormat("A<A<A<A>>> a;", "A<A<A<A> >> a;", getGoogleStyle());
10679 verifyFormat("A<A<A<A>>> a;", "A<A<A<A>> > a;", getGoogleStyle());
10680 verifyFormat("A<::A<int>> a;", "A< ::A<int>> a;", getGoogleStyle());
10681 verifyFormat("A<::A<int>> a;", "A<::A<int> > a;", getGoogleStyle());
10682 verifyFormat("auto x = [] { A<A<A<A>>> a; };", "auto x=[]{A<A<A<A> >> a;};",
10685 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp
));
10687 // template closer followed by a token that starts with > or =
10688 verifyFormat("bool b = a<1> > 1;");
10689 verifyFormat("bool b = a<1> >= 1;");
10690 verifyFormat("int i = a<1> >> 1;");
10691 FormatStyle Style
= getLLVMStyle();
10692 Style
.SpaceBeforeAssignmentOperators
= false;
10693 verifyFormat("bool b= a<1> == 1;", Style
);
10694 verifyFormat("a<int> = 1;", Style
);
10695 verifyFormat("a<int> >>= 1;", Style
);
10697 verifyFormat("test < a | b >> c;");
10698 verifyFormat("test<test<a | b>> c;");
10699 verifyFormat("test >> a >> b;");
10700 verifyFormat("test << a >> b;");
10702 verifyFormat("f<int>();");
10703 verifyFormat("template <typename T> void f() {}");
10704 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
10705 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
10706 "sizeof(char)>::type>;");
10707 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
10708 verifyFormat("f(a.operator()<A>());");
10709 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
10710 " .template operator()<A>());",
10711 getLLVMStyleWithColumns(35));
10712 verifyFormat("bool_constant<a && noexcept(f())>;");
10713 verifyFormat("bool_constant<a || noexcept(f())>;");
10715 verifyFormat("if (std::tuple_size_v<T> > 0)");
10717 // Not template parameters.
10718 verifyFormat("return a < b && c > d;");
10719 verifyFormat("a < 0 ? b : a > 0 ? c : d;");
10720 verifyFormat("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
10721 verifyFormat("void f() {\n"
10722 " while (a < b && c > d) {\n"
10725 verifyFormat("template <typename... Types>\n"
10726 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
10728 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
10730 getLLVMStyleWithColumns(60));
10731 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
10732 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
10733 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
10734 verifyFormat("some_templated_type<decltype([](int i) { return i; })>");
10736 verifyFormat("#define FOO(typeName, realClass) \\\n"
10737 " { #typeName, foo<FooType>(new foo<realClass>(#typeName)) }",
10738 getLLVMStyleWithColumns(60));
10741 TEST_F(FormatTest
, UnderstandsShiftOperators
) {
10742 verifyFormat("if (i < x >> 1)");
10743 verifyFormat("while (i < x >> 1)");
10744 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)");
10745 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)");
10747 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)");
10748 verifyFormat("Foo.call<Bar<Function>>()");
10749 verifyFormat("if (Foo.call<Bar<Function>>() == 0)");
10750 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; "
10751 "++i, v = v >> 1)");
10752 verifyFormat("if (w<u<v<x>>, 1>::t)");
10755 TEST_F(FormatTest
, BitshiftOperatorWidth
) {
10756 verifyFormat("int a = 1 << 2; /* foo\n"
10758 "int a=1<<2; /* foo\n"
10761 verifyFormat("int b = 256 >> 1; /* foo\n"
10763 "int b =256>>1 ; /* foo\n"
10767 TEST_F(FormatTest
, UnderstandsBinaryOperators
) {
10768 verifyFormat("COMPARE(a, ==, b);");
10769 verifyFormat("auto s = sizeof...(Ts) - 1;");
10772 TEST_F(FormatTest
, UnderstandsPointersToMembers
) {
10773 verifyFormat("int A::*x;");
10774 verifyFormat("int (S::*func)(void *);");
10775 verifyFormat("void f() { int (S::*func)(void *); }");
10776 verifyFormat("typedef bool *(Class::*Member)() const;");
10777 verifyFormat("void f() {\n"
10784 verifyFormat("void f() {\n"
10785 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
10786 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
10789 "(aaaaaaaaaa->*bbbbbbb)(\n"
10790 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
10791 FormatStyle Style
= getLLVMStyle();
10792 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
10793 verifyFormat("typedef bool* (Class::*Member)() const;", Style
);
10796 TEST_F(FormatTest
, UnderstandsUnaryOperators
) {
10797 verifyFormat("int a = -2;");
10798 verifyFormat("f(-1, -2, -3);");
10799 verifyFormat("a[-1] = 5;");
10800 verifyFormat("int a = 5 + -2;");
10801 verifyFormat("if (i == -1) {\n}");
10802 verifyFormat("if (i != -1) {\n}");
10803 verifyFormat("if (i > -1) {\n}");
10804 verifyFormat("if (i < -1) {\n}");
10805 verifyFormat("++(a->f());");
10806 verifyFormat("--(a->f());");
10807 verifyFormat("(a->f())++;");
10808 verifyFormat("a[42]++;");
10809 verifyFormat("if (!(a->f())) {\n}");
10810 verifyFormat("if (!+i) {\n}");
10811 verifyFormat("~&a;");
10812 verifyFormat("for (x = 0; -10 < x; --x) {\n}");
10813 verifyFormat("sizeof -x");
10814 verifyFormat("sizeof +x");
10815 verifyFormat("sizeof *x");
10816 verifyFormat("sizeof &x");
10817 verifyFormat("delete +x;");
10818 verifyFormat("co_await +x;");
10819 verifyFormat("case *x:");
10820 verifyFormat("case &x:");
10822 verifyFormat("a-- > b;");
10823 verifyFormat("b ? -a : c;");
10824 verifyFormat("n * sizeof char16;");
10825 verifyGoogleFormat("n * alignof char16;");
10826 verifyFormat("sizeof(char);");
10827 verifyGoogleFormat("alignof(char);");
10829 verifyFormat("return -1;");
10830 verifyFormat("throw -1;");
10831 verifyFormat("switch (a) {\n"
10835 verifyFormat("#define X -1");
10836 verifyFormat("#define X -kConstant");
10838 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
10839 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
10841 verifyFormat("int a = /* confusing comment */ -1;");
10842 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
10843 verifyFormat("int a = i /* confusing comment */++;");
10845 verifyFormat("co_yield -1;");
10846 verifyFormat("co_return -1;");
10848 // Check that * is not treated as a binary operator when we set
10849 // PointerAlignment as PAS_Left after a keyword and not a declaration.
10850 FormatStyle PASLeftStyle
= getLLVMStyle();
10851 PASLeftStyle
.PointerAlignment
= FormatStyle::PAS_Left
;
10852 verifyFormat("co_return *a;", PASLeftStyle
);
10853 verifyFormat("co_await *a;", PASLeftStyle
);
10854 verifyFormat("co_yield *a", PASLeftStyle
);
10855 verifyFormat("return *a;", PASLeftStyle
);
10858 TEST_F(FormatTest
, DoesNotIndentRelativeToUnaryOperators
) {
10859 verifyFormat("if (!aaaaaaaaaa( // break\n"
10862 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
10864 verifyFormat("*aaa = aaaaaaa( // break\n"
10868 TEST_F(FormatTest
, UnderstandsOverloadedOperators
) {
10869 verifyFormat("bool operator<();");
10870 verifyFormat("bool operator>();");
10871 verifyFormat("bool operator=();");
10872 verifyFormat("bool operator==();");
10873 verifyFormat("bool operator!=();");
10874 verifyFormat("int operator+();");
10875 verifyFormat("int operator++();");
10876 verifyFormat("int operator++(int) volatile noexcept;");
10877 verifyFormat("bool operator,();");
10878 verifyFormat("bool operator();");
10879 verifyFormat("bool operator()();");
10880 verifyFormat("bool operator[]();");
10881 verifyFormat("operator bool();");
10882 verifyFormat("operator int();");
10883 verifyFormat("operator void *();");
10884 verifyFormat("operator SomeType<int>();");
10885 verifyFormat("operator SomeType<int, int>();");
10886 verifyFormat("operator SomeType<SomeType<int>>();");
10887 verifyFormat("operator< <>();");
10888 verifyFormat("operator<< <>();");
10889 verifyFormat("< <>");
10891 verifyFormat("void *operator new(std::size_t size);");
10892 verifyFormat("void *operator new[](std::size_t size);");
10893 verifyFormat("void operator delete(void *ptr);");
10894 verifyFormat("void operator delete[](void *ptr);");
10895 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
10896 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
10897 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
10898 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
10901 "ostream &operator<<(ostream &OutputStream,\n"
10902 " SomeReallyLongType WithSomeReallyLongValue);");
10903 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
10904 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
10905 " return left.group < right.group;\n"
10907 verifyFormat("SomeType &operator=(const SomeType &S);");
10908 verifyFormat("f.template operator()<int>();");
10910 verifyGoogleFormat("operator void*();");
10911 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
10912 verifyGoogleFormat("operator ::A();");
10914 verifyFormat("using A::operator+;");
10915 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
10918 // Calling an operator as a member function.
10919 verifyFormat("void f() { a.operator*(); }");
10920 verifyFormat("void f() { a.operator*(b & b); }");
10921 verifyFormat("void f() { a->operator&(a * b); }");
10922 verifyFormat("void f() { NS::a.operator+(*b * *b); }");
10923 // TODO: Calling an operator as a non-member function is hard to distinguish.
10924 // https://llvm.org/PR50629
10925 // verifyFormat("void f() { operator*(a & a); }");
10926 // verifyFormat("void f() { operator&(a, b * b); }");
10928 verifyFormat("void f() { return operator()(x) * b; }");
10929 verifyFormat("void f() { return operator[](x) * b; }");
10930 verifyFormat("void f() { return operator\"\"_a(x) * b; }");
10931 verifyFormat("void f() { return operator\"\" _a(x) * b; }");
10932 verifyFormat("void f() { return operator\"\"s(x) * b; }");
10933 verifyFormat("void f() { return operator\"\" s(x) * b; }");
10934 verifyFormat("void f() { return operator\"\"if(x) * b; }");
10936 verifyFormat("::operator delete(foo);");
10937 verifyFormat("::operator new(n * sizeof(foo));");
10938 verifyFormat("foo() { ::operator delete(foo); }");
10939 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }");
10942 TEST_F(FormatTest
, SpaceBeforeTemplateCloser
) {
10943 verifyFormat("C<&operator- > minus;");
10944 verifyFormat("C<&operator> > gt;");
10945 verifyFormat("C<&operator>= > ge;");
10946 verifyFormat("C<&operator<= > le;");
10947 verifyFormat("C<&operator< <X>> lt;");
10950 TEST_F(FormatTest
, UnderstandsFunctionRefQualification
) {
10951 verifyFormat("void A::b() && {}");
10952 verifyFormat("void A::b() && noexcept {}");
10953 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
10954 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
10955 verifyFormat("Deleted &operator=(const Deleted &) & noexcept = default;");
10956 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
10957 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
10958 verifyFormat("Deleted &operator=(const Deleted &) &;");
10959 verifyFormat("Deleted &operator=(const Deleted &) &&;");
10960 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
10961 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
10962 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
10963 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
10964 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
10965 verifyFormat("SomeType MemberFunction(const Deleted &) && noexcept {}");
10966 verifyFormat("void Fn(T const &) const &;");
10967 verifyFormat("void Fn(T const volatile &&) const volatile &&;");
10968 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;");
10969 verifyGoogleFormat("template <typename T>\n"
10970 "void F(T) && = delete;");
10971 verifyFormat("template <typename T> void operator=(T) &;");
10972 verifyFormat("template <typename T> void operator=(T) const &;");
10973 verifyFormat("template <typename T> void operator=(T) & noexcept;");
10974 verifyFormat("template <typename T> void operator=(T) & = default;");
10975 verifyFormat("template <typename T> void operator=(T) &&;");
10976 verifyFormat("template <typename T> void operator=(T) && = delete;");
10977 verifyFormat("template <typename T> void operator=(T) & {}");
10978 verifyFormat("template <typename T> void operator=(T) && {}");
10980 FormatStyle AlignLeft
= getLLVMStyle();
10981 AlignLeft
.PointerAlignment
= FormatStyle::PAS_Left
;
10982 verifyFormat("void A::b() && {}", AlignLeft
);
10983 verifyFormat("void A::b() && noexcept {}", AlignLeft
);
10984 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft
);
10985 verifyFormat("Deleted& operator=(const Deleted&) & noexcept = default;",
10987 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
10989 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft
);
10990 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft
);
10991 verifyFormat("auto Function(T t) & -> void {}", AlignLeft
);
10992 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft
);
10993 verifyFormat("auto Function(T) & -> void {}", AlignLeft
);
10994 verifyFormat("auto Function(T) & -> void;", AlignLeft
);
10995 verifyFormat("void Fn(T const&) const&;", AlignLeft
);
10996 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft
);
10997 verifyFormat("void Fn(T const volatile&&) const volatile&& noexcept;",
10999 verifyFormat("template <typename T> void operator=(T) &;", AlignLeft
);
11000 verifyFormat("template <typename T> void operator=(T) const&;", AlignLeft
);
11001 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11003 verifyFormat("template <typename T> void operator=(T) & = default;",
11005 verifyFormat("template <typename T> void operator=(T) &&;", AlignLeft
);
11006 verifyFormat("template <typename T> void operator=(T) && = delete;",
11008 verifyFormat("template <typename T> void operator=(T) & {}", AlignLeft
);
11009 verifyFormat("template <typename T> void operator=(T) && {}", AlignLeft
);
11011 FormatStyle AlignMiddle
= getLLVMStyle();
11012 AlignMiddle
.PointerAlignment
= FormatStyle::PAS_Middle
;
11013 verifyFormat("void A::b() && {}", AlignMiddle
);
11014 verifyFormat("void A::b() && noexcept {}", AlignMiddle
);
11015 verifyFormat("Deleted & operator=(const Deleted &) & = default;",
11017 verifyFormat("Deleted & operator=(const Deleted &) & noexcept = default;",
11019 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;",
11021 verifyFormat("Deleted & operator=(const Deleted &) &;", AlignMiddle
);
11022 verifyFormat("SomeType MemberFunction(const Deleted &) &;", AlignMiddle
);
11023 verifyFormat("auto Function(T t) & -> void {}", AlignMiddle
);
11024 verifyFormat("auto Function(T... t) & -> void {}", AlignMiddle
);
11025 verifyFormat("auto Function(T) & -> void {}", AlignMiddle
);
11026 verifyFormat("auto Function(T) & -> void;", AlignMiddle
);
11027 verifyFormat("void Fn(T const &) const &;", AlignMiddle
);
11028 verifyFormat("void Fn(T const volatile &&) const volatile &&;", AlignMiddle
);
11029 verifyFormat("void Fn(T const volatile &&) const volatile && noexcept;",
11031 verifyFormat("template <typename T> void operator=(T) &;", AlignMiddle
);
11032 verifyFormat("template <typename T> void operator=(T) const &;", AlignMiddle
);
11033 verifyFormat("template <typename T> void operator=(T) & noexcept;",
11035 verifyFormat("template <typename T> void operator=(T) & = default;",
11037 verifyFormat("template <typename T> void operator=(T) &&;", AlignMiddle
);
11038 verifyFormat("template <typename T> void operator=(T) && = delete;",
11040 verifyFormat("template <typename T> void operator=(T) & {}", AlignMiddle
);
11041 verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle
);
11043 FormatStyle Spaces
= getLLVMStyle();
11044 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
11045 Spaces
.SpacesInParensOptions
= {};
11046 Spaces
.SpacesInParensOptions
.InCStyleCasts
= true;
11047 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces
);
11048 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces
);
11049 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces
);
11050 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces
);
11052 Spaces
.SpacesInParensOptions
.InCStyleCasts
= false;
11053 Spaces
.SpacesInParensOptions
.Other
= true;
11054 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces
);
11055 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
11057 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces
);
11058 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces
);
11060 FormatStyle BreakTemplate
= getLLVMStyle();
11061 BreakTemplate
.AlwaysBreakTemplateDeclarations
= FormatStyle::BTDS_Yes
;
11063 verifyFormat("struct f {\n"
11064 " template <class T>\n"
11065 " int &foo(const std::string &str) & noexcept {}\n"
11069 verifyFormat("struct f {\n"
11070 " template <class T>\n"
11071 " int &foo(const std::string &str) && noexcept {}\n"
11075 verifyFormat("struct f {\n"
11076 " template <class T>\n"
11077 " int &foo(const std::string &str) const & noexcept {}\n"
11081 verifyFormat("struct f {\n"
11082 " template <class T>\n"
11083 " int &foo(const std::string &str) const & noexcept {}\n"
11087 verifyFormat("struct f {\n"
11088 " template <class T>\n"
11089 " auto foo(const std::string &str) && noexcept -> int & {}\n"
11093 FormatStyle AlignLeftBreakTemplate
= getLLVMStyle();
11094 AlignLeftBreakTemplate
.AlwaysBreakTemplateDeclarations
=
11095 FormatStyle::BTDS_Yes
;
11096 AlignLeftBreakTemplate
.PointerAlignment
= FormatStyle::PAS_Left
;
11098 verifyFormat("struct f {\n"
11099 " template <class T>\n"
11100 " int& foo(const std::string& str) & noexcept {}\n"
11102 AlignLeftBreakTemplate
);
11104 verifyFormat("struct f {\n"
11105 " template <class T>\n"
11106 " int& foo(const std::string& str) && noexcept {}\n"
11108 AlignLeftBreakTemplate
);
11110 verifyFormat("struct f {\n"
11111 " template <class T>\n"
11112 " int& foo(const std::string& str) const& noexcept {}\n"
11114 AlignLeftBreakTemplate
);
11116 verifyFormat("struct f {\n"
11117 " template <class T>\n"
11118 " int& foo(const std::string& str) const&& noexcept {}\n"
11120 AlignLeftBreakTemplate
);
11122 verifyFormat("struct f {\n"
11123 " template <class T>\n"
11124 " auto foo(const std::string& str) && noexcept -> int& {}\n"
11126 AlignLeftBreakTemplate
);
11128 // The `&` in `Type&` should not be confused with a trailing `&` of
11129 // DEPRECATED(reason) member function.
11130 verifyFormat("struct f {\n"
11131 " template <class T>\n"
11132 " DEPRECATED(reason)\n"
11133 " Type &foo(arguments) {}\n"
11137 verifyFormat("struct f {\n"
11138 " template <class T>\n"
11139 " DEPRECATED(reason)\n"
11140 " Type& foo(arguments) {}\n"
11142 AlignLeftBreakTemplate
);
11144 verifyFormat("void (*foopt)(int) = &func;");
11146 FormatStyle DerivePointerAlignment
= getLLVMStyle();
11147 DerivePointerAlignment
.DerivePointerAlignment
= true;
11148 // There's always a space between the function and its trailing qualifiers.
11149 // This isn't evidence for PAS_Right (or for PAS_Left).
11150 std::string Prefix
= "void a() &;\n"
11152 verifyFormat(Prefix
+ "int* x;", DerivePointerAlignment
);
11153 verifyFormat(Prefix
+ "int *x;", DerivePointerAlignment
);
11154 // Same if the function is an overloaded operator, and with &&.
11155 Prefix
= "void operator()() &&;\n"
11156 "void operator()() &&;\n";
11157 verifyFormat(Prefix
+ "int* x;", DerivePointerAlignment
);
11158 verifyFormat(Prefix
+ "int *x;", DerivePointerAlignment
);
11159 // However a space between cv-qualifiers and ref-qualifiers *is* evidence.
11160 Prefix
= "void a() const &;\n"
11161 "void b() const &;\n";
11162 verifyFormat(Prefix
+ "int *x;", Prefix
+ "int* x;", DerivePointerAlignment
);
11165 TEST_F(FormatTest
, PointerAlignmentFallback
) {
11166 FormatStyle Style
= getLLVMStyle();
11167 Style
.DerivePointerAlignment
= true;
11169 const StringRef
Code("int* p;\n"
11173 EXPECT_EQ(Style
.PointerAlignment
, FormatStyle::PAS_Right
);
11174 verifyFormat("int *p;\n"
11179 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
11180 verifyFormat("int* p;\n"
11185 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
11186 verifyFormat("int * p;\n"
11192 TEST_F(FormatTest
, UnderstandsNewAndDelete
) {
11193 verifyFormat("void f() {\n"
11195 " A *a = new (placement) A;\n"
11197 " delete (A *)a;\n"
11199 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11200 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11201 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
11202 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
11203 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
11204 verifyFormat("delete[] h->p;");
11205 verifyFormat("delete[] (void *)p;");
11207 verifyFormat("void operator delete(void *foo) ATTRIB;");
11208 verifyFormat("void operator new(void *foo) ATTRIB;");
11209 verifyFormat("void operator delete[](void *foo) ATTRIB;");
11210 verifyFormat("void operator delete(void *ptr) noexcept;");
11212 verifyFormat("void new(link p);\n"
11213 "void delete(link p);",
11214 "void new (link p);\n"
11215 "void delete (link p);");
11217 FormatStyle AfterPlacementOperator
= getLLVMStyle();
11218 AfterPlacementOperator
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
11220 AfterPlacementOperator
.SpaceBeforeParensOptions
.AfterPlacementOperator
,
11221 FormatStyle::SpaceBeforeParensCustom::APO_Leave
);
11222 verifyFormat("new (buf) int;", AfterPlacementOperator
);
11223 verifyFormat("new(buf) int;", AfterPlacementOperator
);
11225 AfterPlacementOperator
.SpaceBeforeParensOptions
.AfterPlacementOperator
=
11226 FormatStyle::SpaceBeforeParensCustom::APO_Never
;
11227 verifyFormat("struct A {\n"
11229 " A(int *p) : a(new(p) int) {\n"
11231 " int *b = new(p) int;\n"
11232 " int *c = new(p) int(3);\n"
11236 AfterPlacementOperator
);
11237 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator
);
11239 AfterPlacementOperator
.SpaceBeforeParensOptions
.AfterPlacementOperator
=
11240 FormatStyle::SpaceBeforeParensCustom::APO_Always
;
11241 verifyFormat("struct A {\n"
11243 " A(int *p) : a(new (p) int) {\n"
11245 " int *b = new (p) int;\n"
11246 " int *c = new (p) int(3);\n"
11250 AfterPlacementOperator
);
11251 verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator
);
11254 TEST_F(FormatTest
, UnderstandsUsesOfStarAndAmp
) {
11255 verifyFormat("int *f(int *a) {}");
11256 verifyFormat("int main(int argc, char **argv) {}");
11257 verifyFormat("Test::Test(int b) : a(b * b) {}");
11258 verifyIndependentOfContext("f(a, *a);");
11259 verifyFormat("void g() { f(*a); }");
11260 verifyIndependentOfContext("int a = b * 10;");
11261 verifyIndependentOfContext("int a = 10 * b;");
11262 verifyIndependentOfContext("int a = b * c;");
11263 verifyIndependentOfContext("int a += b * c;");
11264 verifyIndependentOfContext("int a -= b * c;");
11265 verifyIndependentOfContext("int a *= b * c;");
11266 verifyIndependentOfContext("int a /= b * c;");
11267 verifyIndependentOfContext("int a = *b;");
11268 verifyIndependentOfContext("int a = *b * c;");
11269 verifyIndependentOfContext("int a = b * *c;");
11270 verifyIndependentOfContext("int a = b * (10);");
11271 verifyIndependentOfContext("S << b * (10);");
11272 verifyIndependentOfContext("return 10 * b;");
11273 verifyIndependentOfContext("return *b * *c;");
11274 verifyIndependentOfContext("return a & ~b;");
11275 verifyIndependentOfContext("f(b ? *c : *d);");
11276 verifyIndependentOfContext("int a = b ? *c : *d;");
11277 verifyIndependentOfContext("*b = a;");
11278 verifyIndependentOfContext("a * ~b;");
11279 verifyIndependentOfContext("a * !b;");
11280 verifyIndependentOfContext("a * +b;");
11281 verifyIndependentOfContext("a * -b;");
11282 verifyIndependentOfContext("a * ++b;");
11283 verifyIndependentOfContext("a * --b;");
11284 verifyIndependentOfContext("a[4] * b;");
11285 verifyIndependentOfContext("a[a * a] = 1;");
11286 verifyIndependentOfContext("f() * b;");
11287 verifyIndependentOfContext("a * [self dostuff];");
11288 verifyIndependentOfContext("int x = a * (a + b);");
11289 verifyIndependentOfContext("(a *)(a + b);");
11290 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
11291 verifyIndependentOfContext("int *pa = (int *)&a;");
11292 verifyIndependentOfContext("return sizeof(int **);");
11293 verifyIndependentOfContext("return sizeof(int ******);");
11294 verifyIndependentOfContext("return (int **&)a;");
11295 verifyIndependentOfContext("f((*PointerToArray)[10]);");
11296 verifyFormat("void f(Type (*parameter)[10]) {}");
11297 verifyFormat("void f(Type (¶meter)[10]) {}");
11298 verifyGoogleFormat("return sizeof(int**);");
11299 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
11300 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
11301 verifyFormat("auto a = [](int **&, int ***) {};");
11302 verifyFormat("auto PointerBinding = [](const char *S) {};");
11303 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
11304 verifyFormat("[](const decltype(*a) &value) {}");
11305 verifyFormat("[](const typeof(*a) &value) {}");
11306 verifyFormat("[](const _Atomic(a *) &value) {}");
11307 verifyFormat("[](const __underlying_type(a) &value) {}");
11308 verifyFormat("decltype(a * b) F();");
11309 verifyFormat("typeof(a * b) F();");
11310 verifyFormat("#define MACRO() [](A *a) { return 1; }");
11311 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
11312 verifyIndependentOfContext("typedef void (*f)(int *a);");
11313 verifyIndependentOfContext("typedef void (*f)(Type *a);");
11314 verifyIndependentOfContext("int i{a * b};");
11315 verifyIndependentOfContext("aaa && aaa->f();");
11316 verifyIndependentOfContext("int x = ~*p;");
11317 verifyFormat("Constructor() : a(a), area(width * height) {}");
11318 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
11319 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
11320 verifyFormat("void f() { f(a, c * d); }");
11321 verifyFormat("void f() { f(new a(), c * d); }");
11322 verifyFormat("void f(const MyOverride &override);");
11323 verifyFormat("void f(const MyFinal &final);");
11324 verifyIndependentOfContext("bool a = f() && override.f();");
11325 verifyIndependentOfContext("bool a = f() && final.f();");
11327 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
11329 verifyIndependentOfContext("A<int *> a;");
11330 verifyIndependentOfContext("A<int **> a;");
11331 verifyIndependentOfContext("A<int *, int *> a;");
11332 verifyIndependentOfContext("A<int *[]> a;");
11333 verifyIndependentOfContext(
11334 "const char *const p = reinterpret_cast<const char *const>(q);");
11335 verifyIndependentOfContext("A<int **, int **> a;");
11336 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
11337 verifyFormat("for (char **a = b; *a; ++a) {\n}");
11338 verifyFormat("for (; a && b;) {\n}");
11339 verifyFormat("bool foo = true && [] { return false; }();");
11342 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
11343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
11345 verifyGoogleFormat("int const* a = &b;");
11346 verifyGoogleFormat("**outparam = 1;");
11347 verifyGoogleFormat("*outparam = a * b;");
11348 verifyGoogleFormat("int main(int argc, char** argv) {}");
11349 verifyGoogleFormat("A<int*> a;");
11350 verifyGoogleFormat("A<int**> a;");
11351 verifyGoogleFormat("A<int*, int*> a;");
11352 verifyGoogleFormat("A<int**, int**> a;");
11353 verifyGoogleFormat("f(b ? *c : *d);");
11354 verifyGoogleFormat("int a = b ? *c : *d;");
11355 verifyGoogleFormat("Type* t = **x;");
11356 verifyGoogleFormat("Type* t = *++*x;");
11357 verifyGoogleFormat("*++*x;");
11358 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
11359 verifyGoogleFormat("Type* t = x++ * y;");
11360 verifyGoogleFormat(
11361 "const char* const p = reinterpret_cast<const char* const>(q);");
11362 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
11363 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
11364 verifyGoogleFormat("template <typename T>\n"
11365 "void f(int i = 0, SomeType** temps = NULL);");
11367 FormatStyle Left
= getLLVMStyle();
11368 Left
.PointerAlignment
= FormatStyle::PAS_Left
;
11369 verifyFormat("x = *a(x) = *a(y);", Left
);
11370 verifyFormat("for (;; *a = b) {\n}", Left
);
11371 verifyFormat("return *this += 1;", Left
);
11372 verifyFormat("throw *x;", Left
);
11373 verifyFormat("delete *x;", Left
);
11374 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left
);
11375 verifyFormat("[](const decltype(*a)* ptr) {}", Left
);
11376 verifyFormat("[](const typeof(*a)* ptr) {}", Left
);
11377 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left
);
11378 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left
);
11379 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left
);
11380 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left
);
11381 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left
);
11382 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left
);
11384 verifyIndependentOfContext("a = *(x + y);");
11385 verifyIndependentOfContext("a = &(x + y);");
11386 verifyIndependentOfContext("*(x + y).call();");
11387 verifyIndependentOfContext("&(x + y)->call();");
11388 verifyFormat("void f() { &(*I).first; }");
11390 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
11391 verifyFormat("f(* /* confusing comment */ foo);");
11392 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)");
11393 verifyFormat("void foo(int * // this is the first paramters\n"
11396 verifyFormat("double term = a * // first\n"
11399 "int *MyValues = {\n"
11400 " *A, // Operator detection might be confused by the '{'\n"
11401 " *BB // Operator detection might be confused by previous comment\n"
11404 verifyIndependentOfContext("if (int *a = &b)");
11405 verifyIndependentOfContext("if (int &a = *b)");
11406 verifyIndependentOfContext("if (a & b[i])");
11407 verifyIndependentOfContext("if constexpr (a & b[i])");
11408 verifyIndependentOfContext("if CONSTEXPR (a & b[i])");
11409 verifyIndependentOfContext("if (a * (b * c))");
11410 verifyIndependentOfContext("if constexpr (a * (b * c))");
11411 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))");
11412 verifyIndependentOfContext("if (a::b::c::d & b[i])");
11413 verifyIndependentOfContext("if (*b[i])");
11414 verifyIndependentOfContext("if (int *a = (&b))");
11415 verifyIndependentOfContext("while (int *a = &b)");
11416 verifyIndependentOfContext("while (a * (b * c))");
11417 verifyIndependentOfContext("size = sizeof *a;");
11418 verifyIndependentOfContext("if (a && (b = c))");
11419 verifyFormat("void f() {\n"
11420 " for (const int &v : Values) {\n"
11423 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
11424 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
11425 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
11427 verifyFormat("#define A (!a * b)");
11428 verifyFormat("#define MACRO \\\n"
11429 " int *i = a * b; \\\n"
11431 getLLVMStyleWithColumns(19));
11433 verifyIndependentOfContext("A = new SomeType *[Length];");
11434 verifyIndependentOfContext("A = new SomeType *[Length]();");
11435 verifyIndependentOfContext("T **t = new T *;");
11436 verifyIndependentOfContext("T **t = new T *();");
11437 verifyGoogleFormat("A = new SomeType*[Length]();");
11438 verifyGoogleFormat("A = new SomeType*[Length];");
11439 verifyGoogleFormat("T** t = new T*;");
11440 verifyGoogleFormat("T** t = new T*();");
11442 verifyFormat("STATIC_ASSERT((a & b) == 0);");
11443 verifyFormat("STATIC_ASSERT(0 == (a & b));");
11444 verifyFormat("template <bool a, bool b> "
11445 "typename t::if<x && y>::type f() {}");
11446 verifyFormat("template <int *y> f() {}");
11447 verifyFormat("vector<int *> v;");
11448 verifyFormat("vector<int *const> v;");
11449 verifyFormat("vector<int *const **const *> v;");
11450 verifyFormat("vector<int *volatile> v;");
11451 verifyFormat("vector<a *_Nonnull> v;");
11452 verifyFormat("vector<a *_Nullable> v;");
11453 verifyFormat("vector<a *_Null_unspecified> v;");
11454 verifyFormat("vector<a *__ptr32> v;");
11455 verifyFormat("vector<a *__ptr64> v;");
11456 verifyFormat("vector<a *__capability> v;");
11457 FormatStyle TypeMacros
= getLLVMStyle();
11458 TypeMacros
.TypenameMacros
= {"LIST"};
11459 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros
);
11460 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros
);
11461 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros
);
11462 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros
);
11463 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros
); // multiplication
11465 FormatStyle CustomQualifier
= getLLVMStyle();
11466 // Add identifiers that should not be parsed as a qualifier by default.
11467 CustomQualifier
.AttributeMacros
.push_back("__my_qualifier");
11468 CustomQualifier
.AttributeMacros
.push_back("_My_qualifier");
11469 CustomQualifier
.AttributeMacros
.push_back("my_other_qualifier");
11470 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;");
11471 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier
);
11472 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;");
11473 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier
);
11474 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;");
11475 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier
);
11476 verifyFormat("vector<a * _NotAQualifier> v;");
11477 verifyFormat("vector<a * __not_a_qualifier> v;");
11478 verifyFormat("vector<a * b> v;");
11479 verifyFormat("foo<b && false>();");
11480 verifyFormat("foo<b & 1>();");
11481 verifyFormat("foo<b & (1)>();");
11482 verifyFormat("foo<b & (~0)>();");
11483 verifyFormat("foo<b & (true)>();");
11484 verifyFormat("foo<b & ((1))>();");
11485 verifyFormat("foo<b & (/*comment*/ 1)>();");
11486 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
11487 verifyFormat("typeof(*::std::declval<const T &>()) void F();");
11488 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();");
11489 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();");
11491 "template <class T, class = typename std::enable_if<\n"
11492 " std::is_integral<T>::value &&\n"
11493 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
11495 getLLVMStyleWithColumns(70));
11496 verifyFormat("template <class T,\n"
11497 " class = typename std::enable_if<\n"
11498 " std::is_integral<T>::value &&\n"
11499 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
11502 getLLVMStyleWithColumns(70));
11504 "template <class T,\n"
11505 " class = typename ::std::enable_if<\n"
11506 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
11508 getGoogleStyleWithColumns(68));
11510 FormatStyle Style
= getLLVMStyle();
11511 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
11512 verifyFormat("struct {\n"
11515 verifyFormat("union {\n"
11518 verifyFormat("class {\n"
11521 // Don't confuse a multiplication after a brace-initialized expression with
11522 // a class pointer.
11523 verifyFormat("int i = int{42} * 34;", Style
);
11524 verifyFormat("struct {\n"
11527 verifyFormat("union {\n"
11530 verifyFormat("class {\n"
11533 verifyFormat("bool b = 3 == int{3} && true;");
11535 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
11536 verifyFormat("struct {\n"
11539 verifyFormat("union {\n"
11542 verifyFormat("class {\n"
11545 verifyFormat("struct {\n"
11548 verifyFormat("union {\n"
11551 verifyFormat("class {\n"
11555 Style
.PointerAlignment
= FormatStyle::PAS_Right
;
11556 verifyFormat("struct {\n"
11559 verifyFormat("union {\n"
11562 verifyFormat("class {\n"
11565 verifyFormat("struct {\n"
11568 verifyFormat("union {\n"
11571 verifyFormat("class {\n"
11575 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
11576 verifyFormat("delete[] *ptr;", Style
);
11577 verifyFormat("delete[] **ptr;", Style
);
11578 verifyFormat("delete[] *(ptr);", Style
);
11580 verifyIndependentOfContext("MACRO(int *i);");
11581 verifyIndependentOfContext("MACRO(auto *a);");
11582 verifyIndependentOfContext("MACRO(const A *a);");
11583 verifyIndependentOfContext("MACRO(_Atomic(A) *a);");
11584 verifyIndependentOfContext("MACRO(decltype(A) *a);");
11585 verifyIndependentOfContext("MACRO(typeof(A) *a);");
11586 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);");
11587 verifyIndependentOfContext("MACRO(A *const a);");
11588 verifyIndependentOfContext("MACRO(A *restrict a);");
11589 verifyIndependentOfContext("MACRO(A *__restrict__ a);");
11590 verifyIndependentOfContext("MACRO(A *__restrict a);");
11591 verifyIndependentOfContext("MACRO(A *volatile a);");
11592 verifyIndependentOfContext("MACRO(A *__volatile a);");
11593 verifyIndependentOfContext("MACRO(A *__volatile__ a);");
11594 verifyIndependentOfContext("MACRO(A *_Nonnull a);");
11595 verifyIndependentOfContext("MACRO(A *_Nullable a);");
11596 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);");
11597 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);");
11598 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);");
11599 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);");
11600 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);");
11601 verifyIndependentOfContext("MACRO(A *__ptr32 a);");
11602 verifyIndependentOfContext("MACRO(A *__ptr64 a);");
11603 verifyIndependentOfContext("MACRO(A *__capability);");
11604 verifyIndependentOfContext("MACRO(A &__capability);");
11605 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration
11606 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication
11607 // If we add __my_qualifier to AttributeMacros it should always be parsed as
11608 // a type declaration:
11609 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier
);
11610 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier
);
11611 // Also check that TypenameMacros prevents parsing it as multiplication:
11612 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication
11613 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros
); // type
11615 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
11616 verifyFormat("void f() { f(float{1}, a * a); }");
11617 verifyFormat("void f() { f(float(1), a * a); }");
11619 verifyFormat("f((void (*)(int))g);");
11620 verifyFormat("f((void (&)(int))g);");
11621 verifyFormat("f((void (^)(int))g);");
11623 // FIXME: Is there a way to make this work?
11624 // verifyIndependentOfContext("MACRO(A *a);");
11625 verifyFormat("MACRO(A &B);");
11626 verifyFormat("MACRO(A *B);");
11627 verifyFormat("void f() { MACRO(A * B); }");
11628 verifyFormat("void f() { MACRO(A & B); }");
11630 // This lambda was mis-formatted after D88956 (treating it as a binop):
11631 verifyFormat("auto x = [](const decltype(x) &ptr) {};");
11632 verifyFormat("auto x = [](const decltype(x) *ptr) {};");
11633 verifyFormat("#define lambda [](const decltype(x) &ptr) {}");
11634 verifyFormat("#define lambda [](const decltype(x) *ptr) {}");
11636 verifyFormat("DatumHandle const *operator->() const { return input_; }");
11637 verifyFormat("return options != nullptr && operator==(*options);");
11639 verifyFormat("#define OP(x) \\\n"
11640 " ostream &operator<<(ostream &s, const A &a) { \\\n"
11641 " return s << a.DebugString(); \\\n"
11643 "#define OP(x) \\\n"
11644 " ostream &operator<<(ostream &s, const A &a) { \\\n"
11645 " return s << a.DebugString(); \\\n"
11647 getLLVMStyleWithColumns(50));
11649 verifyFormat("#define FOO \\\n"
11650 " void foo() { \\\n"
11651 " operator+(a * b); \\\n"
11653 getLLVMStyleWithColumns(25));
11655 // FIXME: We cannot handle this case yet; we might be able to figure out that
11656 // foo<x> d > v; doesn't make sense.
11657 verifyFormat("foo<a<b && c> d> v;");
11659 FormatStyle PointerMiddle
= getLLVMStyle();
11660 PointerMiddle
.PointerAlignment
= FormatStyle::PAS_Middle
;
11661 verifyFormat("delete *x;", PointerMiddle
);
11662 verifyFormat("int * x;", PointerMiddle
);
11663 verifyFormat("int *[] x;", PointerMiddle
);
11664 verifyFormat("template <int * y> f() {}", PointerMiddle
);
11665 verifyFormat("int * f(int * a) {}", PointerMiddle
);
11666 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle
);
11667 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle
);
11668 verifyFormat("A<int *> a;", PointerMiddle
);
11669 verifyFormat("A<int **> a;", PointerMiddle
);
11670 verifyFormat("A<int *, int *> a;", PointerMiddle
);
11671 verifyFormat("A<int *[]> a;", PointerMiddle
);
11672 verifyFormat("A = new SomeType *[Length]();", PointerMiddle
);
11673 verifyFormat("A = new SomeType *[Length];", PointerMiddle
);
11674 verifyFormat("T ** t = new T *;", PointerMiddle
);
11676 // Member function reference qualifiers aren't binary operators.
11677 verifyFormat("string // break\n"
11678 "operator()() & {}");
11679 verifyFormat("string // break\n"
11680 "operator()() && {}");
11681 verifyGoogleFormat("template <typename T>\n"
11682 "auto x() & -> int {}");
11684 // Should be binary operators when used as an argument expression (overloaded
11685 // operator invoked as a member function).
11686 verifyFormat("void f() { a.operator()(a * a); }");
11687 verifyFormat("void f() { a->operator()(a & a); }");
11688 verifyFormat("void f() { a.operator()(*a & *a); }");
11689 verifyFormat("void f() { a->operator()(*a * *a); }");
11691 verifyFormat("int operator()(T (&&)[N]) { return 1; }");
11692 verifyFormat("int operator()(T (&)[N]) { return 0; }");
11694 verifyFormat("val1 & val2;");
11695 verifyFormat("val1 & val2 & val3;");
11696 verifyFormat("class c {\n"
11697 " void func(type &a) { a & member; }\n"
11698 " anotherType &member;\n"
11702 TEST_F(FormatTest
, UnderstandsAttributes
) {
11703 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
11704 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
11705 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
11706 verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
11707 FormatStyle AfterType
= getLLVMStyle();
11708 AfterType
.AlwaysBreakAfterReturnType
= FormatStyle::RTBS_All
;
11709 verifyFormat("__attribute__((nodebug)) void\n"
11712 verifyFormat("__unused void\n"
11716 FormatStyle CustomAttrs
= getLLVMStyle();
11717 CustomAttrs
.AttributeMacros
.push_back("__unused");
11718 CustomAttrs
.AttributeMacros
.push_back("__attr1");
11719 CustomAttrs
.AttributeMacros
.push_back("__attr2");
11720 CustomAttrs
.AttributeMacros
.push_back("no_underscore_attr");
11721 verifyFormat("vector<SomeType *__attribute((foo))> v;");
11722 verifyFormat("vector<SomeType *__attribute__((foo))> v;");
11723 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;");
11724 // Check that it is parsed as a multiplication without AttributeMacros and
11725 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros.
11726 verifyFormat("vector<SomeType * __attr1> v;");
11727 verifyFormat("vector<SomeType __attr1 *> v;");
11728 verifyFormat("vector<SomeType __attr1 *const> v;");
11729 verifyFormat("vector<SomeType __attr1 * __attr2> v;");
11730 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs
);
11731 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs
);
11732 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs
);
11733 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs
);
11734 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs
);
11735 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs
);
11736 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs
);
11737 verifyFormat("__attr1 ::qualified_type f();", CustomAttrs
);
11738 verifyFormat("__attr1() ::qualified_type f();", CustomAttrs
);
11739 verifyFormat("__attr1(nodebug) ::qualified_type f();", CustomAttrs
);
11741 // Check that these are not parsed as function declarations:
11742 CustomAttrs
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
11743 CustomAttrs
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
11744 verifyFormat("SomeType s(InitValue);", CustomAttrs
);
11745 verifyFormat("SomeType s{InitValue};", CustomAttrs
);
11746 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs
);
11747 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs
);
11748 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs
);
11749 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs
);
11750 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs
);
11751 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs
);
11754 TEST_F(FormatTest
, UnderstandsPointerQualifiersInCast
) {
11755 // Check that qualifiers on pointers don't break parsing of casts.
11756 verifyFormat("x = (foo *const)*v;");
11757 verifyFormat("x = (foo *volatile)*v;");
11758 verifyFormat("x = (foo *restrict)*v;");
11759 verifyFormat("x = (foo *__attribute__((foo)))*v;");
11760 verifyFormat("x = (foo *_Nonnull)*v;");
11761 verifyFormat("x = (foo *_Nullable)*v;");
11762 verifyFormat("x = (foo *_Null_unspecified)*v;");
11763 verifyFormat("x = (foo *_Nonnull)*v;");
11764 verifyFormat("x = (foo *[[clang::attr]])*v;");
11765 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;");
11766 verifyFormat("x = (foo *__ptr32)*v;");
11767 verifyFormat("x = (foo *__ptr64)*v;");
11768 verifyFormat("x = (foo *__capability)*v;");
11770 // Check that we handle multiple trailing qualifiers and skip them all to
11771 // determine that the expression is a cast to a pointer type.
11772 FormatStyle LongPointerRight
= getLLVMStyleWithColumns(999);
11773 FormatStyle LongPointerLeft
= getLLVMStyleWithColumns(999);
11774 LongPointerLeft
.PointerAlignment
= FormatStyle::PAS_Left
;
11775 StringRef AllQualifiers
=
11776 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified "
11777 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability";
11778 verifyFormat(("x = (foo *" + AllQualifiers
+ ")*v;").str(), LongPointerRight
);
11779 verifyFormat(("x = (foo* " + AllQualifiers
+ ")*v;").str(), LongPointerLeft
);
11781 // Also check that address-of is not parsed as a binary bitwise-and:
11782 verifyFormat("x = (foo *const)&v;");
11783 verifyFormat(("x = (foo *" + AllQualifiers
+ ")&v;").str(), LongPointerRight
);
11784 verifyFormat(("x = (foo* " + AllQualifiers
+ ")&v;").str(), LongPointerLeft
);
11786 // Check custom qualifiers:
11787 FormatStyle CustomQualifier
= getLLVMStyleWithColumns(999);
11788 CustomQualifier
.AttributeMacros
.push_back("__my_qualifier");
11789 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier.
11790 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier
);
11791 verifyFormat(("x = (foo *" + AllQualifiers
+ " __my_qualifier)*v;").str(),
11793 verifyFormat(("x = (foo *" + AllQualifiers
+ " __my_qualifier)&v;").str(),
11796 // Check that unknown identifiers result in binary operator parsing:
11797 verifyFormat("x = (foo * __unknown_qualifier) * v;");
11798 verifyFormat("x = (foo * __unknown_qualifier) & v;");
11801 TEST_F(FormatTest
, UnderstandsSquareAttributes
) {
11802 verifyFormat("SomeType s [[unused]] (InitValue);");
11803 verifyFormat("SomeType s [[gnu::unused]] (InitValue);");
11804 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);");
11805 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}");
11806 verifyFormat("void f() [[deprecated(\"so sorry\")]];");
11807 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
11808 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);");
11809 verifyFormat("[[nodiscard]] bool f() { return false; }");
11810 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}");
11811 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}");
11812 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}");
11813 verifyFormat("[[nodiscard]] ::qualified_type f();");
11815 // Make sure we do not mistake attributes for array subscripts.
11816 verifyFormat("int a() {}\n"
11817 "[[unused]] int b() {}");
11818 verifyFormat("NSArray *arr;\n"
11819 "arr[[Foo() bar]];");
11821 // On the other hand, we still need to correctly find array subscripts.
11822 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];");
11824 // Make sure that we do not mistake Objective-C method inside array literals
11825 // as attributes, even if those method names are also keywords.
11826 verifyFormat("@[ [foo bar] ];");
11827 verifyFormat("@[ [NSArray class] ];");
11828 verifyFormat("@[ [foo enum] ];");
11830 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }");
11832 // Make sure we do not parse attributes as lambda introducers.
11833 FormatStyle MultiLineFunctions
= getLLVMStyle();
11834 MultiLineFunctions
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
11835 verifyFormat("[[unused]] int b() {\n"
11838 MultiLineFunctions
);
11841 TEST_F(FormatTest
, AttributeClass
) {
11842 FormatStyle Style
= getChromiumStyle(FormatStyle::LK_Cpp
);
11843 verifyFormat("class S {\n"
11844 " S(S&&) = default;\n"
11847 verifyFormat("class [[nodiscard]] S {\n"
11848 " S(S&&) = default;\n"
11851 verifyFormat("class __attribute((maybeunused)) S {\n"
11852 " S(S&&) = default;\n"
11855 verifyFormat("struct S {\n"
11856 " S(S&&) = default;\n"
11859 verifyFormat("struct [[nodiscard]] S {\n"
11860 " S(S&&) = default;\n"
11865 TEST_F(FormatTest
, AttributesAfterMacro
) {
11866 FormatStyle Style
= getLLVMStyle();
11867 verifyFormat("MACRO;\n"
11868 "__attribute__((maybe_unused)) int foo() {\n"
11872 verifyFormat("MACRO;\n"
11873 "[[nodiscard]] int foo() {\n"
11877 verifyNoChange("MACRO\n\n"
11878 "__attribute__((maybe_unused)) int foo() {\n"
11882 verifyNoChange("MACRO\n\n"
11883 "[[nodiscard]] int foo() {\n"
11888 TEST_F(FormatTest
, AttributePenaltyBreaking
) {
11889 FormatStyle Style
= getLLVMStyle();
11890 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
11891 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
11893 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
11894 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
11896 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
11897 "shared_ptr<ALongTypeName> &C d) {\n}",
11901 TEST_F(FormatTest
, UnderstandsEllipsis
) {
11902 FormatStyle Style
= getLLVMStyle();
11903 verifyFormat("int printf(const char *fmt, ...);");
11904 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
11905 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}");
11907 verifyFormat("template <int *...PP> a;", Style
);
11909 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
11910 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style
);
11912 verifyFormat("template <int*... PP> a;", Style
);
11914 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
11915 verifyFormat("template <int *... PP> a;", Style
);
11918 TEST_F(FormatTest
, AdaptivelyFormatsPointersAndReferences
) {
11919 verifyFormat("int *a;\n"
11926 verifyFormat("int* a;\n"
11933 verifyFormat("int *a;\n"
11940 verifyFormat("auto x = [] {\n"
11945 "auto x=[]{int *a;\n"
11951 TEST_F(FormatTest
, UnderstandsRvalueReferences
) {
11952 verifyFormat("int f(int &&a) {}");
11953 verifyFormat("int f(int a, char &&b) {}");
11954 verifyFormat("void f() { int &&a = b; }");
11955 verifyGoogleFormat("int f(int a, char&& b) {}");
11956 verifyGoogleFormat("void f() { int&& a = b; }");
11958 verifyIndependentOfContext("A<int &&> a;");
11959 verifyIndependentOfContext("A<int &&, int &&> a;");
11960 verifyGoogleFormat("A<int&&> a;");
11961 verifyGoogleFormat("A<int&&, int&&> a;");
11963 // Not rvalue references:
11964 verifyFormat("template <bool B, bool C> class A {\n"
11965 " static_assert(B && C, \"Something is wrong\");\n"
11967 verifyFormat("template <typename T> void swap() noexcept(Bar<T> && Foo<T>);");
11968 verifyFormat("template <typename T> struct S {\n"
11969 " explicit(Bar<T> && Foo<T>) S(const S &);\n"
11971 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
11972 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
11973 verifyFormat("#define A(a, b) (a && b)");
11976 TEST_F(FormatTest
, FormatsBinaryOperatorsPrecedingEquals
) {
11977 verifyFormat("void f() {\n"
11981 getLLVMStyleWithColumns(15));
11984 TEST_F(FormatTest
, FormatsCasts
) {
11985 verifyFormat("Type *A = static_cast<Type *>(P);");
11986 verifyFormat("static_cast<Type *>(P);");
11987 verifyFormat("static_cast<Type &>(Fun)(Args);");
11988 verifyFormat("static_cast<Type &>(*Fun)(Args);");
11989 verifyFormat("if (static_cast<int>(A) + B >= 0)\n ;");
11990 // Check that static_cast<...>(...) does not require the next token to be on
11992 verifyFormat("some_loooong_output << something_something__ << "
11993 "static_cast<const void *>(R)\n"
11995 verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
11996 verifyFormat("const_cast<Type &>(*Fun)(Args);");
11997 verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
11998 verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
11999 verifyFormat("Type *A = (Type *)P;");
12000 verifyFormat("Type *A = (vector<Type *, int *>)P;");
12001 verifyFormat("int a = (int)(2.0f);");
12002 verifyFormat("int a = (int)2.0f;");
12003 verifyFormat("x[(int32)y];");
12004 verifyFormat("x = (int32)y;");
12005 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
12006 verifyFormat("int a = (int)*b;");
12007 verifyFormat("int a = (int)2.0f;");
12008 verifyFormat("int a = (int)~0;");
12009 verifyFormat("int a = (int)++a;");
12010 verifyFormat("int a = (int)sizeof(int);");
12011 verifyFormat("int a = (int)+2;");
12012 verifyFormat("my_int a = (my_int)2.0f;");
12013 verifyFormat("my_int a = (my_int)sizeof(int);");
12014 verifyFormat("return (my_int)aaa;");
12015 verifyFormat("throw (my_int)aaa;");
12016 verifyFormat("#define x ((int)-1)");
12017 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
12018 verifyFormat("#define p(q) ((int *)&q)");
12019 verifyFormat("fn(a)(b) + 1;");
12021 verifyFormat("void f() { my_int a = (my_int)*b; }");
12022 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
12023 verifyFormat("my_int a = (my_int)~0;");
12024 verifyFormat("my_int a = (my_int)++a;");
12025 verifyFormat("my_int a = (my_int)-2;");
12026 verifyFormat("my_int a = (my_int)1;");
12027 verifyFormat("my_int a = (my_int *)1;");
12028 verifyFormat("my_int a = (const my_int)-1;");
12029 verifyFormat("my_int a = (const my_int *)-1;");
12030 verifyFormat("my_int a = (my_int)(my_int)-1;");
12031 verifyFormat("my_int a = (ns::my_int)-2;");
12032 verifyFormat("case (my_int)ONE:");
12033 verifyFormat("auto x = (X)this;");
12034 // Casts in Obj-C style calls used to not be recognized as such.
12035 verifyGoogleFormat("int a = [(type*)[((type*)val) arg] arg];");
12037 // FIXME: single value wrapped with paren will be treated as cast.
12038 verifyFormat("void f(int i = (kValue)*kMask) {}");
12040 verifyFormat("{ (void)F; }");
12042 // Don't break after a cast's
12043 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
12044 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
12045 " bbbbbbbbbbbbbbbbbbbbbb);");
12047 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)");
12048 verifyFormat("#define CONF_BOOL(x) (bool *)(x)");
12049 verifyFormat("#define CONF_BOOL(x) (bool)(x)");
12050 verifyFormat("bool *y = (bool *)(void *)(x);");
12051 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)");
12052 verifyFormat("bool *y = (bool *)(void *)(int)(x);");
12053 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)");
12054 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);");
12056 // These are not casts.
12057 verifyFormat("void f(int *) {}");
12058 verifyFormat("f(foo)->b;");
12059 verifyFormat("f(foo).b;");
12060 verifyFormat("f(foo)(b);");
12061 verifyFormat("f(foo)[b];");
12062 verifyFormat("[](foo) { return 4; }(bar);");
12063 verifyFormat("(*funptr)(foo)[4];");
12064 verifyFormat("funptrs[4](foo)[4];");
12065 verifyFormat("void f(int *);");
12066 verifyFormat("void f(int *) = 0;");
12067 verifyFormat("void f(SmallVector<int>) {}");
12068 verifyFormat("void f(SmallVector<int>);");
12069 verifyFormat("void f(SmallVector<int>) = 0;");
12070 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
12071 verifyFormat("int a = sizeof(int) * b;");
12072 verifyGoogleFormat("int a = alignof(int) * b;");
12073 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
12074 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
12075 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
12077 // These are not casts, but at some point were confused with casts.
12078 verifyFormat("virtual void foo(int *) override;");
12079 verifyFormat("virtual void foo(char &) const;");
12080 verifyFormat("virtual void foo(int *a, char *) const;");
12081 verifyFormat("int a = sizeof(int *) + b;");
12082 verifyGoogleFormat("int a = alignof(int *) + b;");
12083 verifyFormat("bool b = f(g<int>) && c;");
12084 verifyFormat("typedef void (*f)(int i) func;");
12085 verifyFormat("void operator++(int) noexcept;");
12086 verifyFormat("void operator++(int &) noexcept;");
12087 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t "
12090 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;");
12091 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;");
12092 verifyFormat("void operator delete(std::nothrow_t &) noexcept;");
12093 verifyFormat("void operator delete(nothrow_t &) noexcept;");
12094 verifyFormat("void operator delete(foo &) noexcept;");
12095 verifyFormat("void operator delete(foo) noexcept;");
12096 verifyFormat("void operator delete(int) noexcept;");
12097 verifyFormat("void operator delete(int &) noexcept;");
12098 verifyFormat("void operator delete(int &) volatile noexcept;");
12099 verifyFormat("void operator delete(int &) const");
12100 verifyFormat("void operator delete(int &) = default");
12101 verifyFormat("void operator delete(int &) = delete");
12102 verifyFormat("void operator delete(int &) [[noreturn]]");
12103 verifyFormat("void operator delete(int &) throw();");
12104 verifyFormat("void operator delete(int &) throw(int);");
12105 verifyFormat("auto operator delete(int &) -> int;");
12106 verifyFormat("auto operator delete(int &) override");
12107 verifyFormat("auto operator delete(int &) final");
12109 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
12110 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
12111 // FIXME: The indentation here is not ideal.
12113 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12114 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
12115 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
12118 TEST_F(FormatTest
, FormatsFunctionTypes
) {
12119 verifyFormat("A<bool()> a;");
12120 verifyFormat("A<SomeType()> a;");
12121 verifyFormat("A<void (*)(int, std::string)> a;");
12122 verifyFormat("A<void *(int)>;");
12123 verifyFormat("void *(*a)(int *, SomeType *);");
12124 verifyFormat("int (*func)(void *);");
12125 verifyFormat("void f() { int (*func)(void *); }");
12126 verifyFormat("template <class CallbackClass>\n"
12127 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
12129 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
12130 verifyGoogleFormat("void* (*a)(int);");
12131 verifyGoogleFormat(
12132 "template <class CallbackClass>\n"
12133 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
12135 // Other constructs can look somewhat like function types:
12136 verifyFormat("A<sizeof(*x)> a;");
12137 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
12138 verifyFormat("some_var = function(*some_pointer_var)[0];");
12139 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
12140 verifyFormat("int x = f(&h)();");
12141 verifyFormat("returnsFunction(¶m1, ¶m2)(param);");
12142 verifyFormat("std::function<\n"
12143 " LooooooooooongTemplatedType<\n"
12145 " LooooooooooooooooongType type)>\n"
12147 getGoogleStyleWithColumns(40));
12150 TEST_F(FormatTest
, FormatsPointersToArrayTypes
) {
12151 verifyFormat("A (*foo_)[6];");
12152 verifyFormat("vector<int> (*foo_)[6];");
12155 TEST_F(FormatTest
, BreaksLongVariableDeclarations
) {
12156 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12157 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12158 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
12159 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
12160 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12161 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
12163 // Different ways of ()-initializiation.
12164 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12165 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
12166 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12167 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
12168 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12169 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
12170 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
12171 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
12173 // Lambdas should not confuse the variable declaration heuristic.
12174 verifyFormat("LooooooooooooooooongType\n"
12175 " variable(nullptr, [](A *a) {});",
12176 getLLVMStyleWithColumns(40));
12179 TEST_F(FormatTest
, BreaksLongDeclarations
) {
12180 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
12181 " AnotherNameForTheLongType;");
12182 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
12183 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
12184 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12185 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12186 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
12187 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
12188 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12189 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12190 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
12191 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12192 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12193 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12194 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12195 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12196 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n"
12197 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12198 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n"
12199 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12200 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n"
12201 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
12202 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12203 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
12204 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12205 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
12206 FormatStyle Indented
= getLLVMStyle();
12207 Indented
.IndentWrappedFunctionNames
= true;
12208 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12209 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
12212 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
12213 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12216 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
12217 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12220 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
12221 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
12224 // FIXME: Without the comment, this breaks after "(".
12225 verifyGoogleFormat(
12226 "LoooooooooooooooooooooooooooooooooooooooongType // break\n"
12227 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();");
12229 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
12230 " int LoooooooooooooooooooongParam2) {}");
12232 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
12233 " SourceLocation L, IdentifierIn *II,\n"
12235 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
12236 "ReallyReaaallyLongFunctionName(\n"
12237 " const std::string &SomeParameter,\n"
12238 " const SomeType<string, SomeOtherTemplateParameter>\n"
12239 " &ReallyReallyLongParameterName,\n"
12240 " const SomeType<string, SomeOtherTemplateParameter>\n"
12241 " &AnotherLongParameterName) {}");
12242 verifyFormat("template <typename A>\n"
12243 "SomeLoooooooooooooooooooooongType<\n"
12244 " typename some_namespace::SomeOtherType<A>::Type>\n"
12247 verifyGoogleFormat(
12248 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
12249 " aaaaaaaaaaaaaaaaaaaaaaa;");
12250 verifyGoogleFormat(
12251 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
12252 " SourceLocation L) {}");
12253 verifyGoogleFormat(
12254 "some_namespace::LongReturnType\n"
12255 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
12256 " int first_long_parameter, int second_parameter) {}");
12258 verifyGoogleFormat("template <typename T>\n"
12259 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12260 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
12261 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
12262 " int aaaaaaaaaaaaaaaaaaaaaaa);");
12264 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
12265 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12266 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12267 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12268 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
12269 " aaaaaaaaaaaaaaaaaaaaaaaa);");
12270 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
12271 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
12272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
12273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
12275 verifyFormat("template <typename T> // Templates on own line.\n"
12276 "static int // Some comment.\n"
12277 "MyFunction(int a);");
12280 TEST_F(FormatTest
, FormatsAccessModifiers
) {
12281 FormatStyle Style
= getLLVMStyle();
12282 EXPECT_EQ(Style
.EmptyLineBeforeAccessModifier
,
12283 FormatStyle::ELBAMS_LogicalBlock
);
12284 verifyFormat("struct foo {\n"
12295 verifyFormat("struct foo {\n"
12314 verifyFormat("struct foo { /* comment */\n"
12322 verifyFormat("struct foo {\n"
12333 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
12334 verifyFormat("struct foo {\n"
12343 verifyFormat("struct foo {\n"
12363 verifyFormat("struct foo { /* comment */\n"
12370 "struct foo { /* comment */\n"
12380 verifyFormat("struct foo {\n"
12403 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
12404 verifyFormat("struct foo {\n"
12415 verifyFormat("struct foo {\n"
12434 verifyFormat("struct foo { /* comment */\n"
12443 verifyFormat("struct foo {\n"
12466 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Leave
;
12467 verifyNoChange("struct foo {\n"
12479 verifyFormat("struct foo {\n"
12488 verifyNoChange("struct foo { /* comment */\n"
12498 verifyFormat("struct foo { /* comment */\n"
12506 verifyNoChange("struct foo {\n"
12519 verifyFormat("struct foo {\n"
12531 FormatStyle NoEmptyLines
= getLLVMStyle();
12532 NoEmptyLines
.MaxEmptyLinesToKeep
= 0;
12533 verifyFormat("struct foo {\n"
12546 NoEmptyLines
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
12547 verifyFormat("struct foo {\n"
12558 NoEmptyLines
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
12559 verifyFormat("struct foo {\n"
12574 TEST_F(FormatTest
, FormatsAfterAccessModifiers
) {
12576 FormatStyle Style
= getLLVMStyle();
12577 EXPECT_EQ(Style
.EmptyLineAfterAccessModifier
, FormatStyle::ELAAMS_Never
);
12578 verifyFormat("struct foo {\n"
12590 // Check if lines are removed.
12591 verifyFormat("struct foo {\n"
12616 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
12617 verifyFormat("struct foo {\n"
12632 // Check if lines are added.
12633 verifyFormat("struct foo {\n"
12658 // Leave tests rely on the code layout, test::messUp can not be used.
12659 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
12660 Style
.MaxEmptyLinesToKeep
= 0u;
12661 verifyFormat("struct foo {\n"
12673 // Check if MaxEmptyLinesToKeep is respected.
12674 verifyFormat("struct foo {\n"
12699 Style
.MaxEmptyLinesToKeep
= 1u;
12700 verifyNoChange("struct foo {\n"
12714 // Check if no lines are kept.
12715 verifyFormat("struct foo {\n"
12726 // Check if MaxEmptyLinesToKeep is respected.
12727 verifyFormat("struct foo {\n"
12755 Style
.MaxEmptyLinesToKeep
= 10u;
12756 verifyNoChange("struct foo {\n"
12771 // Test with comments.
12772 Style
= getLLVMStyle();
12773 verifyFormat("struct foo {\n"
12778 "private: /* comment */\n"
12782 verifyFormat("struct foo {\n"
12787 "private: /* comment */\n"
12796 "private: /* comment */\n"
12802 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
12803 verifyFormat("struct foo {\n"
12809 "private: /* comment */\n"
12818 "private: /* comment */\n"
12822 verifyFormat("struct foo {\n"
12828 "private: /* comment */\n"
12834 // Test with preprocessor defines.
12835 Style
= getLLVMStyle();
12836 verifyFormat("struct foo {\n"
12843 verifyFormat("struct foo {\n"
12857 verifyNoChange("struct foo {\n"
12865 verifyFormat("struct foo {\n"
12881 verifyFormat("struct foo {\n"
12896 verifyFormat("struct foo {\n"
12916 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
12917 verifyFormat("struct foo {\n"
12931 verifyFormat("struct foo {\n"
12941 TEST_F(FormatTest
, FormatsAfterAndBeforeAccessModifiersInteraction
) {
12942 // Combined tests of EmptyLineAfterAccessModifier and
12943 // EmptyLineBeforeAccessModifier.
12944 FormatStyle Style
= getLLVMStyle();
12945 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
12946 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
12947 verifyFormat("struct foo {\n"
12954 Style
.MaxEmptyLinesToKeep
= 10u;
12955 // Both remove all new lines.
12956 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
12957 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Never
;
12958 verifyFormat("struct foo {\n"
12969 // Leave tests rely on the code layout, test::messUp can not be used.
12970 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Leave
;
12971 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
12972 Style
.MaxEmptyLinesToKeep
= 10u;
12973 verifyNoChange("struct foo {\n"
12979 Style
.MaxEmptyLinesToKeep
= 3u;
12980 verifyNoChange("struct foo {\n"
12986 Style
.MaxEmptyLinesToKeep
= 1u;
12987 verifyNoChange("struct foo {\n"
12992 Style
); // Based on new lines in original document and not
12995 Style
.MaxEmptyLinesToKeep
= 10u;
12996 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
12997 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
12998 // Newlines are kept if they are greater than zero,
12999 // test::messUp removes all new lines which changes the logic
13000 verifyNoChange("struct foo {\n"
13007 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Leave
;
13008 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13009 // test::messUp removes all new lines which changes the logic
13010 verifyNoChange("struct foo {\n"
13017 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Leave
;
13018 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Never
;
13019 verifyNoChange("struct foo {\n"
13024 Style
); // test::messUp removes all new lines which changes
13027 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
13028 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
13029 verifyFormat("struct foo {\n"
13040 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Always
;
13041 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Never
;
13042 verifyNoChange("struct foo {\n"
13047 Style
); // test::messUp removes all new lines which changes
13050 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_Never
;
13051 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13052 verifyFormat("struct foo {\n"
13063 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_LogicalBlock
;
13064 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Always
;
13065 verifyFormat("struct foo {\n"
13076 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_LogicalBlock
;
13077 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Leave
;
13078 verifyFormat("struct foo {\n"
13089 Style
.EmptyLineBeforeAccessModifier
= FormatStyle::ELBAMS_LogicalBlock
;
13090 Style
.EmptyLineAfterAccessModifier
= FormatStyle::ELAAMS_Never
;
13091 verifyFormat("struct foo {\n"
13103 TEST_F(FormatTest
, FormatsArrays
) {
13104 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13105 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
13106 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
13107 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
13108 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
13109 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
13110 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13111 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13112 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13113 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
13114 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
13115 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13116 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
13118 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
13119 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
13120 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
13121 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
13122 " .aaaaaaaaaaaaaaaaaaaaaa();");
13124 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
13125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
13127 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
13129 " .aaaaaaaaaaaaaaaaaaaaaa();");
13130 verifyFormat("a[::b::c];");
13132 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
13134 FormatStyle NoColumnLimit
= getLLVMStyleWithColumns(0);
13135 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit
);
13138 TEST_F(FormatTest
, LineStartsWithSpecialCharacter
) {
13139 verifyFormat("(a)->b();");
13140 verifyFormat("--a;");
13143 TEST_F(FormatTest
, HandlesIncludeDirectives
) {
13144 verifyFormat("#include <string>\n"
13145 "#include <a/b/c.h>\n"
13146 "#include \"a/b/string\"\n"
13147 "#include \"string.h\"\n"
13148 "#include \"string.h\"\n"
13150 "#include < path with space >\n"
13151 "#include_next <test.h>"
13152 "#include \"abc.h\" // this is included for ABC\n"
13153 "#include \"some long include\" // with a comment\n"
13154 "#include \"some very long include path\"\n"
13155 "#include <some/very/long/include/path>",
13156 getLLVMStyleWithColumns(35));
13157 verifyFormat("#include \"a.h\"", "#include \"a.h\"");
13158 verifyFormat("#include <a>", "#include<a>");
13160 verifyFormat("#import <string>");
13161 verifyFormat("#import <a/b/c.h>");
13162 verifyFormat("#import \"a/b/string\"");
13163 verifyFormat("#import \"string.h\"");
13164 verifyFormat("#import \"string.h\"");
13165 verifyFormat("#if __has_include(<strstream>)\n"
13166 "#include <strstream>\n"
13169 verifyFormat("#define MY_IMPORT <a/b>");
13171 verifyFormat("#if __has_include(<a/b>)");
13172 verifyFormat("#if __has_include_next(<a/b>)");
13173 verifyFormat("#define F __has_include(<a/b>)");
13174 verifyFormat("#define F __has_include_next(<a/b>)");
13176 // Protocol buffer definition or missing "#".
13177 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
13178 getLLVMStyleWithColumns(30));
13180 FormatStyle Style
= getLLVMStyle();
13181 Style
.AlwaysBreakBeforeMultilineStrings
= true;
13182 Style
.ColumnLimit
= 0;
13183 verifyFormat("#import \"abc.h\"", Style
);
13185 // But 'import' might also be a regular C++ namespace.
13186 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13187 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
13188 verifyFormat("import::Bar foo(val ? 2 : 1);");
13191 //===----------------------------------------------------------------------===//
13192 // Error recovery tests.
13193 //===----------------------------------------------------------------------===//
13195 TEST_F(FormatTest
, IncompleteParameterLists
) {
13196 FormatStyle NoBinPacking
= getLLVMStyle();
13197 NoBinPacking
.BinPackParameters
= false;
13198 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
13199 " double *min_x,\n"
13200 " double *max_x,\n"
13201 " double *min_y,\n"
13202 " double *max_y,\n"
13203 " double *min_z,\n"
13204 " double *max_z, ) {}",
13208 TEST_F(FormatTest
, IncorrectCodeTrailingStuff
) {
13209 verifyFormat("void f() { return; }\n42");
13210 verifyFormat("void f() {\n"
13215 verifyFormat("void f() { return }\n42");
13216 verifyFormat("void f() {\n"
13223 TEST_F(FormatTest
, IncorrectCodeMissingSemicolon
) {
13224 verifyFormat("void f() { return }", "void f ( ) { return }");
13225 verifyFormat("void f() {\n"
13229 "void f ( ) { if ( a ) return }");
13230 verifyFormat("namespace N {\n"
13233 "namespace N { void f() }");
13234 verifyFormat("namespace N {\n"
13237 "} // namespace N",
13238 "namespace N { void f( ) { } void g( ) }");
13241 TEST_F(FormatTest
, IndentationWithinColumnLimitNotPossible
) {
13242 verifyFormat("int aaaaaaaa =\n"
13243 " // Overlylongcomment\n"
13245 getLLVMStyleWithColumns(20));
13246 verifyFormat("function(\n"
13247 " ShortArgument,\n"
13248 " LoooooooooooongArgument);",
13249 getLLVMStyleWithColumns(20));
13252 TEST_F(FormatTest
, IncorrectAccessSpecifier
) {
13253 verifyFormat("public:");
13254 verifyFormat("class A {\n"
13258 verifyFormat("public\n"
13260 verifyFormat("public\n"
13262 verifyFormat("public\n"
13264 verifyFormat("public\n"
13268 TEST_F(FormatTest
, IncorrectCodeUnbalancedBraces
) {
13270 verifyFormat("#})");
13271 verifyNoCrash("(/**/[:!] ?[).");
13274 TEST_F(FormatTest
, IncorrectUnbalancedBracesInMacrosWithUnicode
) {
13275 // Found by oss-fuzz:
13276 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212
13277 FormatStyle Style
= getGoogleStyle(FormatStyle::LK_Cpp
);
13278 Style
.ColumnLimit
= 60;
13280 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20"
13281 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20"
13282 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a",
13286 TEST_F(FormatTest
, IncorrectCodeDoNoWhile
) {
13287 verifyFormat("do {\n}");
13288 verifyFormat("do {\n}\n"
13290 verifyFormat("do {\n}\n"
13292 verifyFormat("do {\n"
13297 TEST_F(FormatTest
, IncorrectCodeMissingParens
) {
13298 verifyFormat("if {\n foo;\n foo();\n}");
13299 verifyFormat("switch {\n foo;\n foo();\n}");
13300 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
13301 verifyIncompleteFormat("ERROR: for target;");
13302 verifyFormat("while {\n foo;\n foo();\n}");
13303 verifyFormat("do {\n foo;\n foo();\n} while;");
13306 TEST_F(FormatTest
, DoesNotTouchUnwrappedLinesWithErrors
) {
13307 verifyIncompleteFormat("namespace {\n"
13308 "class Foo { Foo (\n"
13313 TEST_F(FormatTest
, IncorrectCodeErrorDetection
) {
13314 verifyFormat("{\n {}", "{\n{\n}");
13315 verifyFormat("{\n {}", "{\n {\n}");
13316 verifyFormat("{\n {}", "{\n {\n }");
13317 verifyFormat("{\n {}\n}\n}", "{\n {\n }\n }\n}");
13328 getLLVMStyleWithColumns(10));
13331 TEST_F(FormatTest
, LayoutCallsInsideBraceInitializers
) {
13332 verifyFormat("int x = {\n"
13334 " b(alongervariable)};",
13335 getLLVMStyleWithColumns(25));
13338 TEST_F(FormatTest
, LayoutBraceInitializersInReturnStatement
) {
13339 verifyFormat("return (a)(b){1, 2, 3};");
13342 TEST_F(FormatTest
, LayoutCxx11BraceInitializers
) {
13343 verifyFormat("vector<int> x{1, 2, 3, 4};");
13344 verifyFormat("vector<int> x{\n"
13350 verifyFormat("vector<T> x{{}, {}, {}, {}};");
13351 verifyFormat("f({1, 2});");
13352 verifyFormat("auto v = Foo{-1};");
13353 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
13354 verifyFormat("Class::Class : member{1, 2, 3} {}");
13355 verifyFormat("new vector<int>{1, 2, 3};");
13356 verifyFormat("new int[3]{1, 2, 3};");
13357 verifyFormat("new int{1};");
13358 verifyFormat("return {arg1, arg2};");
13359 verifyFormat("return {arg1, SomeType{parameter}};");
13360 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
13361 verifyFormat("new T{arg1, arg2};");
13362 verifyFormat("f(MyMap[{composite, key}]);");
13363 verifyFormat("class Class {\n"
13364 " T member = {arg1, arg2};\n"
13366 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
13367 verifyFormat("const struct A a = {.a = 1, .b = 2};");
13368 verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
13369 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
13370 verifyFormat("int a = std::is_integral<int>{} + 0;");
13372 verifyFormat("int foo(int i) { return fo1{}(i); }");
13373 verifyFormat("int foo(int i) { return fo1{}(i); }");
13374 verifyFormat("auto i = decltype(x){};");
13375 verifyFormat("auto i = typeof(x){};");
13376 verifyFormat("auto i = _Atomic(x){};");
13377 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
13378 verifyFormat("Node n{1, Node{1000}, //\n"
13380 verifyFormat("Aaaa aaaaaaa{\n"
13385 verifyFormat("class C : public D {\n"
13386 " SomeClass SC{2};\n"
13388 verifyFormat("class C : public A {\n"
13389 " class D : public B {\n"
13390 " void f() { int i{2}; }\n"
13393 verifyFormat("#define A {a, a},");
13394 // Don't confuse braced list initializers with compound statements.
13398 " A() : Base<int>{} {}\n"
13399 " A() : Base<Foo<int>>{} {}\n"
13400 " A(int b) : b(b) {}\n"
13401 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n"
13403 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n"
13404 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} "
13408 // Avoid breaking between equal sign and opening brace
13409 FormatStyle AvoidBreakingFirstArgument
= getLLVMStyle();
13410 AvoidBreakingFirstArgument
.PenaltyBreakBeforeFirstCallParameter
= 200;
13411 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
13412 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n"
13413 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n"
13414 " {\"ccccccccccccccccccccc\", 2}};",
13415 AvoidBreakingFirstArgument
);
13417 // Binpacking only if there is no trailing comma
13418 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
13419 " cccccccccc, dddddddddd};",
13420 getLLVMStyleWithColumns(50));
13421 verifyFormat("const Aaaaaa aaaaa = {\n"
13427 getLLVMStyleWithColumns(50));
13429 // Cases where distinguising braced lists and blocks is hard.
13430 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
13431 verifyFormat("void f() {\n"
13432 " return; // comment\n"
13435 verifyFormat("void f() {\n"
13442 // In combination with BinPackArguments = false.
13443 FormatStyle NoBinPacking
= getLLVMStyle();
13444 NoBinPacking
.BinPackArguments
= false;
13445 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
13457 verifyFormat("const Aaaaaa aaaaa = {\n"
13472 "const Aaaaaa aaaaa = {\n"
13473 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
13474 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
13475 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
13479 NoBinPacking
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
13480 verifyFormat("static uint8 CddDp83848Reg[] = {\n"
13481 " CDDDP83848_BMCR_REGISTER,\n"
13482 " CDDDP83848_BMSR_REGISTER,\n"
13483 " CDDDP83848_RBR_REGISTER};",
13484 "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n"
13485 " CDDDP83848_BMSR_REGISTER,\n"
13486 " CDDDP83848_RBR_REGISTER};",
13489 // FIXME: The alignment of these trailing comments might be bad. Then again,
13490 // this might be utterly useless in real code.
13491 verifyFormat("Constructor::Constructor()\n"
13492 " : some_value{ //\n"
13496 // In braced lists, the first comment is always assumed to belong to the
13497 // first element. Thus, it can be moved to the next or previous line as
13499 verifyFormat("function({// First element:\n"
13501 " // Second element:\n"
13504 " // First element:\n"
13506 " // Second element:\n"
13508 verifyFormat("std::vector<int> MyNumbers{\n"
13509 " // First element:\n"
13511 " // Second element:\n"
13513 "std::vector<int> MyNumbers{// First element:\n"
13515 " // Second element:\n"
13517 getLLVMStyleWithColumns(30));
13518 // A trailing comma should still lead to an enforced line break and no
13520 verifyFormat("vector<int> SomeVector = {\n"
13525 "vector<int> SomeVector = { // aaa\n"
13528 // C++11 brace initializer list l-braces should not be treated any differently
13529 // when breaking before lambda bodies is enabled
13530 FormatStyle BreakBeforeLambdaBody
= getLLVMStyle();
13531 BreakBeforeLambdaBody
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
13532 BreakBeforeLambdaBody
.BraceWrapping
.BeforeLambdaBody
= true;
13533 BreakBeforeLambdaBody
.AlwaysBreakBeforeMultilineStrings
= true;
13535 "std::runtime_error{\n"
13536 " \"Long string which will force a break onto the next line...\"};",
13537 BreakBeforeLambdaBody
);
13539 FormatStyle ExtraSpaces
= getLLVMStyle();
13540 ExtraSpaces
.Cpp11BracedListStyle
= false;
13541 ExtraSpaces
.ColumnLimit
= 75;
13542 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces
);
13543 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces
);
13544 verifyFormat("f({ 1, 2 });", ExtraSpaces
);
13545 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces
);
13546 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces
);
13547 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces
);
13548 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces
);
13549 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces
);
13550 verifyFormat("return { arg1, arg2 };", ExtraSpaces
);
13551 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces
);
13552 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces
);
13553 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces
);
13554 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces
);
13555 verifyFormat("class Class {\n"
13556 " T member = { arg1, arg2 };\n"
13560 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13561 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
13562 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
13563 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
13565 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces
);
13566 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
13569 "someFunction(OtherParam,\n"
13570 " BracedList{ // comment 1 (Forcing interesting break)\n"
13571 " param1, param2,\n"
13573 " param3, param4 });",
13576 "std::this_thread::sleep_for(\n"
13577 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
13579 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
13583 " aaaaaaaaaaaaaaa,\n"
13587 " aaaaaaaaaaaaaaaaaaaaa,\n"
13589 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
13592 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces
);
13593 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces
);
13594 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces
);
13596 // Avoid breaking between initializer/equal sign and opening brace
13597 ExtraSpaces
.PenaltyBreakBeforeFirstCallParameter
= 200;
13598 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n"
13599 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
13600 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
13601 " { \"ccccccccccccccccccccc\", 2 }\n"
13604 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n"
13605 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n"
13606 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n"
13607 " { \"ccccccccccccccccccccc\", 2 }\n"
13611 FormatStyle SpaceBeforeBrace
= getLLVMStyle();
13612 SpaceBeforeBrace
.SpaceBeforeCpp11BracedList
= true;
13613 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace
);
13614 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace
);
13616 FormatStyle SpaceBetweenBraces
= getLLVMStyle();
13617 SpaceBetweenBraces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
13618 SpaceBetweenBraces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
13619 SpaceBetweenBraces
.SpacesInParensOptions
.Other
= true;
13620 SpaceBetweenBraces
.SpacesInSquareBrackets
= true;
13621 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces
);
13622 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces
);
13623 verifyFormat("vector< int > x{ // comment 1\n"
13625 SpaceBetweenBraces
);
13626 SpaceBetweenBraces
.ColumnLimit
= 20;
13627 verifyFormat("vector< int > x{\n"
13629 "vector<int>x{1,2,3,4};", SpaceBetweenBraces
);
13630 SpaceBetweenBraces
.ColumnLimit
= 24;
13631 verifyFormat("vector< int > x{ 1, 2,\n"
13633 "vector<int>x{1,2,3,4};", SpaceBetweenBraces
);
13634 verifyFormat("vector< int > x{\n"
13640 "vector<int>x{1,2,3,4,};", SpaceBetweenBraces
);
13641 verifyFormat("vector< int > x{};", SpaceBetweenBraces
);
13642 SpaceBetweenBraces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
13643 SpaceBetweenBraces
.SpacesInParensOptions
.InEmptyParentheses
= true;
13644 verifyFormat("vector< int > x{ };", SpaceBetweenBraces
);
13647 TEST_F(FormatTest
, FormatsBracedListsInColumnLayout
) {
13648 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13649 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13650 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13651 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13652 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13653 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
13654 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
13655 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13656 " 1, 22, 333, 4444, 55555, //\n"
13657 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13658 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
13660 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13661 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13662 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
13663 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
13664 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
13665 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
13667 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
13668 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
13669 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
13670 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
13671 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
13672 " // Separating comment.\n"
13673 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
13674 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
13675 " // Leading comment\n"
13676 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
13677 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
13678 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
13680 getLLVMStyleWithColumns(39));
13681 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
13683 getLLVMStyleWithColumns(38));
13684 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
13685 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
13686 getLLVMStyleWithColumns(43));
13688 "static unsigned SomeValues[10][3] = {\n"
13689 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
13690 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
13691 verifyFormat("static auto fields = new vector<string>{\n"
13692 " \"aaaaaaaaaaaaa\",\n"
13693 " \"aaaaaaaaaaaaa\",\n"
13694 " \"aaaaaaaaaaaa\",\n"
13695 " \"aaaaaaaaaaaaaa\",\n"
13696 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
13697 " \"aaaaaaaaaaaa\",\n"
13698 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
13700 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
13701 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
13702 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
13703 " 3, cccccccccccccccccccccc};",
13704 getLLVMStyleWithColumns(60));
13706 // Trailing commas.
13707 verifyFormat("vector<int> x = {\n"
13708 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
13710 getLLVMStyleWithColumns(39));
13711 verifyFormat("vector<int> x = {\n"
13712 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
13714 getLLVMStyleWithColumns(39));
13715 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
13718 getLLVMStyleWithColumns(39));
13720 // Trailing comment in the first line.
13721 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
13722 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
13723 " 111111111, 222222222, 3333333333, 444444444, //\n"
13724 " 11111111, 22222222, 333333333, 44444444};");
13725 // Trailing comment in the last line.
13726 verifyFormat("int aaaaa[] = {\n"
13727 " 1, 2, 3, // comment\n"
13728 " 4, 5, 6 // comment\n"
13731 // With nested lists, we should either format one item per line or all nested
13732 // lists one on line.
13733 // FIXME: For some nested lists, we can do better.
13734 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
13735 " {aaaaaaaaaaaaaaaaaaa},\n"
13736 " {aaaaaaaaaaaaaaaaaaaaa},\n"
13737 " {aaaaaaaaaaaaaaaaa}};",
13738 getLLVMStyleWithColumns(60));
13740 "SomeStruct my_struct_array = {\n"
13741 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
13742 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
13745 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
13746 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
13747 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
13749 // No column layout should be used here.
13750 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
13751 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
13753 verifyNoCrash("a<,");
13755 // No braced initializer here.
13756 verifyFormat("void f() {\n"
13757 " struct Dummy {};\n"
13760 verifyFormat("void foo() {\n"
13768 verifyFormat("namespace n {\n"
13779 "} // namespace n");
13781 // Long lists should be formatted in columns even if they are nested.
13783 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13784 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13785 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13786 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13787 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
13788 " 1, 22, 333, 4444, 55555, 666666, 7777777});");
13790 // Allow "single-column" layout even if that violates the column limit. There
13791 // isn't going to be a better way.
13792 verifyFormat("std::vector<int> a = {\n"
13799 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
13800 getLLVMStyleWithColumns(30));
13801 verifyFormat("vector<int> aaaa = {\n"
13802 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13803 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
13804 " aaaaaa.aaaaaaa,\n"
13805 " aaaaaa.aaaaaaa,\n"
13806 " aaaaaa.aaaaaaa,\n"
13807 " aaaaaa.aaaaaaa,\n"
13810 // Don't create hanging lists.
13811 verifyFormat("someFunction(Param, {List1, List2,\n"
13813 getLLVMStyleWithColumns(35));
13814 verifyFormat("someFunction(Param, Param,\n"
13815 " {List1, List2,\n"
13817 getLLVMStyleWithColumns(35));
13818 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
13819 " aaaaaaaaaaaaaaaaaaaaaaa);");
13822 TEST_F(FormatTest
, PullTrivialFunctionDefinitionsIntoSingleLine
) {
13823 FormatStyle DoNotMerge
= getLLVMStyle();
13824 DoNotMerge
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
13826 verifyFormat("void f() { return 42; }");
13827 verifyFormat("void f() {\n"
13831 verifyFormat("void f() {\n"
13842 verifyFormat("void f() {} // comment");
13843 verifyFormat("void f() { int a; } // comment");
13844 verifyFormat("void f() {\n"
13847 verifyFormat("void f() {\n"
13851 verifyFormat("void f() {\n"
13853 getLLVMStyleWithColumns(15));
13855 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
13856 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
13858 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
13859 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
13860 verifyGoogleFormat("class C {\n"
13862 " : iiiiiiii(nullptr),\n"
13863 " kkkkkkk(nullptr),\n"
13864 " mmmmmmm(nullptr),\n"
13865 " nnnnnnn(nullptr) {}\n"
13868 FormatStyle NoColumnLimit
= getLLVMStyleWithColumns(0);
13869 verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit
);
13870 verifyFormat("class C {\n"
13873 "class C{A():b(0){}};", NoColumnLimit
);
13874 verifyFormat("A()\n"
13877 "A()\n:b(0)\n{\n}", NoColumnLimit
);
13879 FormatStyle NoColumnLimitWrapAfterFunction
= NoColumnLimit
;
13880 NoColumnLimitWrapAfterFunction
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
13881 NoColumnLimitWrapAfterFunction
.BraceWrapping
.AfterFunction
= true;
13882 verifyFormat("class C {\n"
13884 " int foo { return 0; }\n"
13886 NoColumnLimitWrapAfterFunction
);
13887 verifyFormat("class C {\n"
13891 NoColumnLimitWrapAfterFunction
);
13893 FormatStyle DoNotMergeNoColumnLimit
= NoColumnLimit
;
13894 DoNotMergeNoColumnLimit
.AllowShortFunctionsOnASingleLine
=
13895 FormatStyle::SFS_None
;
13896 verifyFormat("A()\n"
13899 "A():b(0){}", DoNotMergeNoColumnLimit
);
13900 verifyFormat("A()\n"
13903 "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit
);
13905 verifyFormat("#define A \\\n"
13909 getLLVMStyleWithColumns(20));
13910 verifyFormat("#define A \\\n"
13911 " void f() { int i; }",
13912 getLLVMStyleWithColumns(21));
13913 verifyFormat("#define A \\\n"
13918 getLLVMStyleWithColumns(22));
13919 verifyFormat("#define A \\\n"
13920 " void f() { int i; } \\\n"
13922 getLLVMStyleWithColumns(23));
13925 TEST_F(FormatTest
, PullEmptyFunctionDefinitionsIntoSingleLine
) {
13926 FormatStyle MergeEmptyOnly
= getLLVMStyle();
13927 MergeEmptyOnly
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_Empty
;
13928 verifyFormat("class C {\n"
13932 verifyFormat("class C {\n"
13938 verifyFormat("int f() {}", MergeEmptyOnly
);
13939 verifyFormat("int f() {\n"
13944 // Also verify behavior when BraceWrapping.AfterFunction = true
13945 MergeEmptyOnly
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
13946 MergeEmptyOnly
.BraceWrapping
.AfterFunction
= true;
13947 verifyFormat("int f() {}", MergeEmptyOnly
);
13948 verifyFormat("class C {\n"
13954 TEST_F(FormatTest
, PullInlineFunctionDefinitionsIntoSingleLine
) {
13955 FormatStyle MergeInlineOnly
= getLLVMStyle();
13956 MergeInlineOnly
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_Inline
;
13957 verifyFormat("class C {\n"
13958 " int f() { return 42; }\n"
13961 verifyFormat("int f() {\n"
13966 // SFS_Inline implies SFS_Empty
13967 verifyFormat("class C {\n"
13971 verifyFormat("int f() {}", MergeInlineOnly
);
13972 // https://llvm.org/PR54147
13973 verifyFormat("auto lambda = []() {\n"
13980 verifyFormat("class C {\n"
13982 " int f() { return 42; }\n"
13987 verifyFormat("struct S {\n"
13990 " int foo() { bar(); }\n"
13995 // Also verify behavior when BraceWrapping.AfterFunction = true
13996 MergeInlineOnly
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
13997 MergeInlineOnly
.BraceWrapping
.AfterFunction
= true;
13998 verifyFormat("class C {\n"
13999 " int f() { return 42; }\n"
14002 verifyFormat("int f()\n"
14008 // SFS_Inline implies SFS_Empty
14009 verifyFormat("int f() {}", MergeInlineOnly
);
14010 verifyFormat("class C {\n"
14015 MergeInlineOnly
.BraceWrapping
.AfterClass
= true;
14016 MergeInlineOnly
.BraceWrapping
.AfterStruct
= true;
14017 verifyFormat("class C\n"
14019 " int f() { return 42; }\n"
14022 verifyFormat("struct C\n"
14024 " int f() { return 42; }\n"
14027 verifyFormat("int f()\n"
14032 verifyFormat("int f() {}", MergeInlineOnly
);
14033 verifyFormat("class C\n"
14035 " int f() { return 42; }\n"
14038 verifyFormat("struct C\n"
14040 " int f() { return 42; }\n"
14043 verifyFormat("struct C\n"
14048 " int f() { return 42; }\n"
14051 verifyFormat("/* comment */ struct C\n"
14053 " int f() { return 42; }\n"
14058 TEST_F(FormatTest
, PullInlineOnlyFunctionDefinitionsIntoSingleLine
) {
14059 FormatStyle MergeInlineOnly
= getLLVMStyle();
14060 MergeInlineOnly
.AllowShortFunctionsOnASingleLine
=
14061 FormatStyle::SFS_InlineOnly
;
14062 verifyFormat("class C {\n"
14063 " int f() { return 42; }\n"
14066 verifyFormat("int f() {\n"
14071 // SFS_InlineOnly does not imply SFS_Empty
14072 verifyFormat("class C {\n"
14076 verifyFormat("int f() {\n"
14080 // Also verify behavior when BraceWrapping.AfterFunction = true
14081 MergeInlineOnly
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14082 MergeInlineOnly
.BraceWrapping
.AfterFunction
= true;
14083 verifyFormat("class C {\n"
14084 " int f() { return 42; }\n"
14087 verifyFormat("int f()\n"
14093 // SFS_InlineOnly does not imply SFS_Empty
14094 verifyFormat("int f()\n"
14098 verifyFormat("class C {\n"
14104 TEST_F(FormatTest
, SplitEmptyFunction
) {
14105 FormatStyle Style
= getLLVMStyleWithColumns(40);
14106 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
14107 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14108 Style
.BraceWrapping
.AfterFunction
= true;
14109 Style
.BraceWrapping
.SplitEmptyFunction
= false;
14111 verifyFormat("int f()\n"
14114 verifyFormat("int f()\n"
14119 verifyFormat("int f()\n"
14121 " // some comment\n"
14125 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_Empty
;
14126 verifyFormat("int f() {}", Style
);
14127 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14130 verifyFormat("int f()\n"
14136 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_Inline
;
14137 verifyFormat("class Foo {\n"
14141 verifyFormat("class Foo {\n"
14142 " int f() { return 0; }\n"
14145 verifyFormat("class Foo {\n"
14146 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14150 verifyFormat("class Foo {\n"
14151 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14158 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
14159 verifyFormat("int f() {}", Style
);
14160 verifyFormat("int f() { return 0; }", Style
);
14161 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14164 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
14171 TEST_F(FormatTest
, SplitEmptyFunctionButNotRecord
) {
14172 FormatStyle Style
= getLLVMStyleWithColumns(40);
14173 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
14174 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14175 Style
.BraceWrapping
.AfterFunction
= true;
14176 Style
.BraceWrapping
.SplitEmptyFunction
= true;
14177 Style
.BraceWrapping
.SplitEmptyRecord
= false;
14179 verifyFormat("class C {};", Style
);
14180 verifyFormat("struct C {};", Style
);
14181 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14182 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14186 verifyFormat("class C {\n"
14188 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n"
14189 " bbbbbbbbbbbbbbbbbbb()\n"
14193 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
14194 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
14201 TEST_F(FormatTest
, KeepShortFunctionAfterPPElse
) {
14202 FormatStyle Style
= getLLVMStyle();
14203 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
14204 verifyFormat("#ifdef A\n"
14212 TEST_F(FormatTest
, SplitEmptyClass
) {
14213 FormatStyle Style
= getLLVMStyle();
14214 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14215 Style
.BraceWrapping
.AfterClass
= true;
14216 Style
.BraceWrapping
.SplitEmptyRecord
= false;
14218 verifyFormat("class Foo\n"
14221 verifyFormat("/* something */ class Foo\n"
14224 verifyFormat("template <typename X> class Foo\n"
14227 verifyFormat("class Foo\n"
14232 verifyFormat("typedef class Foo\n"
14237 Style
.BraceWrapping
.SplitEmptyRecord
= true;
14238 Style
.BraceWrapping
.AfterStruct
= true;
14239 verifyFormat("class rep\n"
14243 verifyFormat("struct rep\n"
14247 verifyFormat("template <typename T> class rep\n"
14251 verifyFormat("template <typename T> struct rep\n"
14255 verifyFormat("class rep\n"
14260 verifyFormat("struct rep\n"
14265 verifyFormat("template <typename T> class rep\n"
14270 verifyFormat("template <typename T> struct rep\n"
14275 verifyFormat("template <typename T> class rep // Foo\n"
14280 verifyFormat("template <typename T> struct rep // Bar\n"
14286 verifyFormat("template <typename T> class rep<T>\n"
14292 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14297 verifyFormat("template <typename T> class rep<std::complex<T>>\n"
14302 verifyFormat("#include \"stdint.h\"\n"
14303 "namespace rep {}",
14305 verifyFormat("#include <stdint.h>\n"
14306 "namespace rep {}",
14308 verifyFormat("#include <stdint.h>\n"
14309 "namespace rep {}",
14310 "#include <stdint.h>\n"
14311 "namespace rep {\n"
14318 TEST_F(FormatTest
, SplitEmptyStruct
) {
14319 FormatStyle Style
= getLLVMStyle();
14320 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14321 Style
.BraceWrapping
.AfterStruct
= true;
14322 Style
.BraceWrapping
.SplitEmptyRecord
= false;
14324 verifyFormat("struct Foo\n"
14327 verifyFormat("/* something */ struct Foo\n"
14330 verifyFormat("template <typename X> struct Foo\n"
14333 verifyFormat("struct Foo\n"
14338 verifyFormat("typedef struct Foo\n"
14342 // typedef struct Bar {} Bar_t;
14345 TEST_F(FormatTest
, SplitEmptyUnion
) {
14346 FormatStyle Style
= getLLVMStyle();
14347 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14348 Style
.BraceWrapping
.AfterUnion
= true;
14349 Style
.BraceWrapping
.SplitEmptyRecord
= false;
14351 verifyFormat("union Foo\n"
14354 verifyFormat("/* something */ union Foo\n"
14357 verifyFormat("union Foo\n"
14362 verifyFormat("typedef union Foo\n"
14368 TEST_F(FormatTest
, SplitEmptyNamespace
) {
14369 FormatStyle Style
= getLLVMStyle();
14370 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14371 Style
.BraceWrapping
.AfterNamespace
= true;
14372 Style
.BraceWrapping
.SplitEmptyNamespace
= false;
14374 verifyFormat("namespace Foo\n"
14377 verifyFormat("/* something */ namespace Foo\n"
14380 verifyFormat("inline namespace Foo\n"
14383 verifyFormat("/* something */ inline namespace Foo\n"
14386 verifyFormat("export namespace Foo\n"
14389 verifyFormat("namespace Foo\n"
14396 TEST_F(FormatTest
, NeverMergeShortRecords
) {
14397 FormatStyle Style
= getLLVMStyle();
14399 verifyFormat("class Foo {\n"
14403 verifyFormat("typedef class Foo {\n"
14407 verifyFormat("struct Foo {\n"
14411 verifyFormat("typedef struct Foo {\n"
14415 verifyFormat("union Foo {\n"
14419 verifyFormat("typedef union Foo {\n"
14423 verifyFormat("namespace Foo {\n"
14428 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
14429 Style
.BraceWrapping
.AfterClass
= true;
14430 Style
.BraceWrapping
.AfterStruct
= true;
14431 Style
.BraceWrapping
.AfterUnion
= true;
14432 Style
.BraceWrapping
.AfterNamespace
= true;
14433 verifyFormat("class Foo\n"
14438 verifyFormat("typedef class Foo\n"
14443 verifyFormat("struct Foo\n"
14448 verifyFormat("typedef struct Foo\n"
14453 verifyFormat("union Foo\n"
14458 verifyFormat("typedef union Foo\n"
14463 verifyFormat("namespace Foo\n"
14470 TEST_F(FormatTest
, UnderstandContextOfRecordTypeKeywords
) {
14471 // Elaborate type variable declarations.
14472 verifyFormat("struct foo a = {bar};\nint n;");
14473 verifyFormat("class foo a = {bar};\nint n;");
14474 verifyFormat("union foo a = {bar};\nint n;");
14476 // Elaborate types inside function definitions.
14477 verifyFormat("struct foo f() {}\nint n;");
14478 verifyFormat("class foo f() {}\nint n;");
14479 verifyFormat("union foo f() {}\nint n;");
14482 verifyFormat("template <class X> void f() {}\nint n;");
14483 verifyFormat("template <struct X> void f() {}\nint n;");
14484 verifyFormat("template <union X> void f() {}\nint n;");
14486 // Actual definitions...
14487 verifyFormat("struct {\n} n;");
14489 "template <template <class T, class Y>, class Z> class X {\n} n;");
14490 verifyFormat("union Z {\n int n;\n} x;");
14491 verifyFormat("class MACRO Z {\n} n;");
14492 verifyFormat("class MACRO(X) Z {\n} n;");
14493 verifyFormat("class __attribute__(X) Z {\n} n;");
14494 verifyFormat("class __declspec(X) Z {\n} n;");
14495 verifyFormat("class A##B##C {\n} n;");
14496 verifyFormat("class alignas(16) Z {\n} n;");
14497 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
14498 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
14500 // Redefinition from nested context:
14501 verifyFormat("class A::B::C {\n} n;");
14503 // Template definitions.
14505 "template <typename F>\n"
14506 "Matcher(const Matcher<F> &Other,\n"
14507 " typename enable_if_c<is_base_of<F, T>::value &&\n"
14508 " !is_same<F, T>::value>::type * = 0)\n"
14509 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
14511 // FIXME: This is still incorrectly handled at the formatter side.
14512 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
14513 verifyFormat("int i = SomeFunction(a<b, a> b);");
14516 // This now gets parsed incorrectly as class definition.
14517 // verifyFormat("class A<int> f() {\n}\nint n;");
14519 // Elaborate types where incorrectly parsing the structural element would
14520 // break the indent.
14521 verifyFormat("if (true)\n"
14526 // This is simply incomplete. Formatting is not important, but must not crash.
14527 verifyFormat("class A:");
14530 TEST_F(FormatTest
, DoNotInterfereWithErrorAndWarning
) {
14531 verifyNoChange("#error Leave all white!!!!! space* alone!");
14532 verifyNoChange("#warning Leave all white!!!!! space* alone!");
14533 verifyFormat("#error 1", " # error 1");
14534 verifyFormat("#warning 1", " # warning 1");
14537 TEST_F(FormatTest
, FormatHashIfExpressions
) {
14538 verifyFormat("#if AAAA && BBBB");
14539 verifyFormat("#if (AAAA && BBBB)");
14540 verifyFormat("#elif (AAAA && BBBB)");
14541 // FIXME: Come up with a better indentation for #elif.
14543 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
14544 " defined(BBBBBBBB)\n"
14545 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
14546 " defined(BBBBBBBB)\n"
14548 getLLVMStyleWithColumns(65));
14551 TEST_F(FormatTest
, MergeHandlingInTheFaceOfPreprocessorDirectives
) {
14552 FormatStyle AllowsMergedIf
= getGoogleStyle();
14553 AllowsMergedIf
.AllowShortIfStatementsOnASingleLine
=
14554 FormatStyle::SIS_WithoutElse
;
14555 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf
);
14556 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf
);
14557 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf
);
14558 verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf
);
14559 FormatStyle ShortMergedIf
= AllowsMergedIf
;
14560 ShortMergedIf
.ColumnLimit
= 25;
14561 verifyFormat("#define A \\\n"
14562 " if (true) return 42;",
14564 verifyFormat("#define A \\\n"
14569 verifyFormat("#define A \\\n"
14577 " if (true) continue;\n"
14580 " if (true) continue;\n"
14583 ShortMergedIf
.ColumnLimit
= 33;
14584 verifyFormat("#define A \\\n"
14585 " if constexpr (true) return 42;",
14587 verifyFormat("#define A \\\n"
14588 " if CONSTEXPR (true) return 42;",
14590 ShortMergedIf
.ColumnLimit
= 29;
14591 verifyFormat("#define A \\\n"
14592 " if (aaaaaaaaaa) return 1; \\\n"
14595 ShortMergedIf
.ColumnLimit
= 28;
14596 verifyFormat("#define A \\\n"
14597 " if (aaaaaaaaaa) \\\n"
14601 verifyFormat("#define A \\\n"
14602 " if constexpr (aaaaaaa) \\\n"
14606 verifyFormat("#define A \\\n"
14607 " if CONSTEXPR (aaaaaaa) \\\n"
14612 verifyFormat("//\n"
14616 getChromiumStyle(FormatStyle::LK_Cpp
));
14619 TEST_F(FormatTest
, FormatStarDependingOnContext
) {
14620 verifyFormat("void f(int *a);");
14621 verifyFormat("void f() { f(fint * b); }");
14622 verifyFormat("class A {\n void f(int *a);\n};");
14623 verifyFormat("class A {\n int *a;\n};");
14624 verifyFormat("namespace a {\n"
14630 "} // namespace b\n"
14631 "} // namespace a");
14634 TEST_F(FormatTest
, SpecialTokensAtEndOfLine
) {
14635 verifyFormat("while");
14636 verifyFormat("operator");
14639 TEST_F(FormatTest
, SkipsDeeplyNestedLines
) {
14640 // This code would be painfully slow to format if we didn't skip it.
14641 std::string
Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" // 20x
14642 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
14643 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
14644 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
14645 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
14647 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
14648 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14649 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14650 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14651 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14652 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14653 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14654 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14655 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
14656 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
14657 // Deeply nested part is untouched, rest is formatted.
14658 EXPECT_EQ(std::string("int i;") + Code
+ "int j;",
14659 format(std::string("int i;") + Code
+ "int j;",
14660 getLLVMStyle(), SC_ExpectIncomplete
));
14663 //===----------------------------------------------------------------------===//
14664 // Objective-C tests.
14665 //===----------------------------------------------------------------------===//
14667 TEST_F(FormatTest
, FormatForObjectiveCMethodDecls
) {
14668 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
14669 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;",
14670 "-(NSUInteger)indexOfObject:(id)anObject;");
14671 verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;");
14672 verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;");
14673 verifyFormat("- (NSInteger)Method3:(id)anObject;",
14674 "-(NSInteger)Method3:(id)anObject;");
14675 verifyFormat("- (NSInteger)Method4:(id)anObject;",
14676 "-(NSInteger)Method4:(id)anObject;");
14677 verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
14678 "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;");
14679 verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;");
14680 verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject "
14681 "forAllCells:(BOOL)flag;");
14683 // Very long objectiveC method declaration.
14684 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
14685 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
14686 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
14687 " inRange:(NSRange)range\n"
14688 " outRange:(NSRange)out_range\n"
14689 " outRange1:(NSRange)out_range1\n"
14690 " outRange2:(NSRange)out_range2\n"
14691 " outRange3:(NSRange)out_range3\n"
14692 " outRange4:(NSRange)out_range4\n"
14693 " outRange5:(NSRange)out_range5\n"
14694 " outRange6:(NSRange)out_range6\n"
14695 " outRange7:(NSRange)out_range7\n"
14696 " outRange8:(NSRange)out_range8\n"
14697 " outRange9:(NSRange)out_range9;");
14699 // When the function name has to be wrapped.
14700 FormatStyle Style
= getLLVMStyle();
14701 // ObjC ignores IndentWrappedFunctionNames when wrapping methods
14702 // and always indents instead.
14703 Style
.IndentWrappedFunctionNames
= false;
14704 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
14705 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
14706 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
14709 Style
.IndentWrappedFunctionNames
= true;
14710 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
14711 " veryLooooooooooongName:(NSString)cccccccccccccc\n"
14712 " anotherName:(NSString)dddddddddddddd {\n"
14716 verifyFormat("- (int)sum:(vector<int>)numbers;");
14717 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
14718 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
14719 // protocol lists (but not for template classes):
14720 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
14722 verifyFormat("- (int (*)())foo:(int (*)())f;");
14723 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
14725 // If there's no return type (very rare in practice!), LLVM and Google style
14727 verifyFormat("- foo;");
14728 verifyFormat("- foo:(int)f;");
14729 verifyGoogleFormat("- foo:(int)foo;");
14732 TEST_F(FormatTest
, BreaksStringLiterals
) {
14733 // FIXME: unstable test case
14734 EXPECT_EQ("\"some text \"\n"
14736 format("\"some text other\";", getLLVMStyleWithColumns(12)));
14737 // FIXME: unstable test case
14738 EXPECT_EQ("\"some text \"\n"
14740 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
14741 verifyFormat("#define A \\\n"
14745 "#define A \"some text other\";", getLLVMStyleWithColumns(12));
14746 verifyFormat("#define A \\\n"
14750 "#define A \"so text other\";", getLLVMStyleWithColumns(12));
14752 verifyFormat("\"some text\"", getLLVMStyleWithColumns(1));
14753 verifyFormat("\"some text\"", getLLVMStyleWithColumns(11));
14754 // FIXME: unstable test case
14755 EXPECT_EQ("\"some \"\n"
14757 format("\"some text\"", getLLVMStyleWithColumns(10)));
14758 // FIXME: unstable test case
14759 EXPECT_EQ("\"some \"\n"
14761 format("\"some text\"", getLLVMStyleWithColumns(7)));
14762 // FIXME: unstable test case
14763 EXPECT_EQ("\"some\"\n"
14766 format("\"some text\"", getLLVMStyleWithColumns(6)));
14767 // FIXME: unstable test case
14768 EXPECT_EQ("\"some\"\n"
14771 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
14772 // FIXME: unstable test case
14773 EXPECT_EQ("\"some\"\n"
14776 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
14778 verifyFormat("variable =\n"
14779 " \"long string \"\n"
14781 "variable = \"long string literal\";",
14782 getLLVMStyleWithColumns(20));
14784 verifyFormat("variable = f(\n"
14785 " \"long string \"\n"
14788 " loooooooooooooooooooong);",
14789 "variable = f(\"long string literal\", short, "
14790 "loooooooooooooooooooong);",
14791 getLLVMStyleWithColumns(20));
14793 verifyFormat("f(g(\"long string \"\n"
14796 "f(g(\"long string literal\"), b);",
14797 getLLVMStyleWithColumns(20));
14798 verifyFormat("f(g(\"long string \"\n"
14802 "f(g(\"long string literal\", a), b);",
14803 getLLVMStyleWithColumns(20));
14804 verifyFormat("f(\"one two\".split(\n"
14806 "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20));
14807 verifyFormat("f(\"one two three four five six \"\n"
14808 " \"seven\".split(\n"
14809 " really_looooong_variable));",
14810 "f(\"one two three four five six seven\"."
14811 "split(really_looooong_variable));",
14812 getLLVMStyleWithColumns(33));
14814 verifyFormat("f(\"some \"\n"
14817 "f(\"some text\", other);", getLLVMStyleWithColumns(10));
14819 // Only break as a last resort.
14821 "aaaaaaaaaaaaaaaaaaaa(\n"
14822 " aaaaaaaaaaaaaaaaaaaa,\n"
14823 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
14825 // FIXME: unstable test case
14826 EXPECT_EQ("\"splitmea\"\n"
14829 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
14831 // FIXME: unstable test case
14832 EXPECT_EQ("\"split/\"\n"
14835 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
14837 // FIXME: unstable test case
14838 EXPECT_EQ("\"split/\"\n"
14841 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
14842 // FIXME: unstable test case
14843 EXPECT_EQ("\"split at \"\n"
14845 "\"slashes.at.any$\"\n"
14846 "\"non-alphanumeric%\"\n"
14847 "\"1111111111characte\"\n"
14849 format("\"split at "
14854 "1111111111characte"
14856 getLLVMStyleWithColumns(20)));
14858 // Verify that splitting the strings understands
14859 // Style::AlwaysBreakBeforeMultilineStrings.
14860 verifyFormat("aaaaaaaaaaaa(\n"
14861 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
14862 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
14863 "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
14864 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
14865 "aaaaaaaaaaaaaaaaaaaaaa\");",
14867 verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
14868 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
14869 "return \"aaaaaaaaaaaaaaaaaaaaaa "
14870 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
14871 "aaaaaaaaaaaaaaaaaaaaaa\";",
14873 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
14874 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
14876 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
14877 "aaaaaaaaaaaaaaaaaaa\";");
14878 verifyFormat("ffff(\n"
14879 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
14880 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
14881 "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
14882 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
14885 FormatStyle Style
= getLLVMStyleWithColumns(12);
14886 Style
.BreakStringLiterals
= false;
14887 verifyFormat("\"some text other\";", Style
);
14889 FormatStyle AlignLeft
= getLLVMStyleWithColumns(12);
14890 AlignLeft
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
14891 verifyFormat("#define A \\\n"
14895 "#define A \"some text other\";", AlignLeft
);
14898 TEST_F(FormatTest
, BreaksStringLiteralsAtColumnLimit
) {
14899 verifyFormat("C a = \"some more \"\n"
14901 "C a = \"some more text\";", getLLVMStyleWithColumns(18));
14904 TEST_F(FormatTest
, FullyRemoveEmptyLines
) {
14905 FormatStyle NoEmptyLines
= getLLVMStyleWithColumns(80);
14906 NoEmptyLines
.MaxEmptyLinesToKeep
= 0;
14907 verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);",
14911 TEST_F(FormatTest
, BreaksStringLiteralsWithTabs
) {
14912 // FIXME: unstable test case
14914 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
14917 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
14922 TEST_F(FormatTest
, BreaksWideAndNSStringLiterals
) {
14923 // FIXME: unstable test case
14925 "u8\"utf8 string \"\n"
14927 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
14928 // FIXME: unstable test case
14930 "u\"utf16 string \"\n"
14932 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
14933 // FIXME: unstable test case
14935 "U\"utf32 string \"\n"
14937 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
14938 // FIXME: unstable test case
14939 EXPECT_EQ("L\"wide string \"\n"
14941 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
14942 verifyFormat("@\"NSString \"\n"
14944 "@\"NSString literal\";", getGoogleStyleWithColumns(19));
14945 verifyFormat(R
"(NSString *s = @"那那那那
";)", getLLVMStyleWithColumns(26));
14947 // This input makes clang-format try to split the incomplete unicode escape
14948 // sequence, which used to lead to a crasher.
14950 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
14951 getLLVMStyleWithColumns(60));
14954 TEST_F(FormatTest
, DoesNotBreakRawStringLiterals
) {
14955 FormatStyle Style
= getGoogleStyleWithColumns(15);
14956 verifyFormat("R\"x(raw literal)x\";", Style
);
14957 verifyFormat("uR\"x(raw literal)x\";", Style
);
14958 verifyFormat("LR\"x(raw literal)x\";", Style
);
14959 verifyFormat("UR\"x(raw literal)x\";", Style
);
14960 verifyFormat("u8R\"x(raw literal)x\";", Style
);
14963 TEST_F(FormatTest
, BreaksStringLiteralsWithin_TMacro
) {
14964 FormatStyle Style
= getLLVMStyleWithColumns(20);
14965 // FIXME: unstable test case
14967 "_T(\"aaaaaaaaaaaaaa\")\n"
14968 "_T(\"aaaaaaaaaaaaaa\")\n"
14969 "_T(\"aaaaaaaaaaaa\")",
14970 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style
));
14971 verifyFormat("f(x,\n"
14972 " _T(\"aaaaaaaaaaaa\")\n"
14975 "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style
);
14977 // FIXME: Handle embedded spaces in one iteration.
14978 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
14979 // "_T(\"aaaaaaaaaaaaa\")\n"
14980 // "_T(\"aaaaaaaaaaaaa\")\n"
14982 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
14983 // getLLVMStyleWithColumns(20)));
14984 verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
14985 " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style
);
14986 verifyFormat("f(\n"
14988 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
14993 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
14996 verifyFormat("f(\n"
14998 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
15001 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));");
15002 // Regression test for accessing tokens past the end of a vector in the
15004 verifyNoCrash(R
"(_T(
15010 TEST_F(FormatTest, BreaksStringLiteralOperands) {
15011 // In a function call with two operands, the second can be broken with no line
15012 // break before it.
15013 verifyFormat("func(a
, \"long long \"\n"
15014 " \"long long\");",
15015 "func(a
, \"long long long long\");",
15016 getLLVMStyleWithColumns(24));
15017 // In a function call with three operands, the second must be broken with a
15018 // line break before it.
15019 verifyFormat("func(a
,\n"
15020 " \"long long long \"\n"
15023 "func(a
, \"long long long long\", c
);",
15024 getLLVMStyleWithColumns(24));
15025 // In a function call with three operands, the third must be broken with a
15026 // line break before it.
15027 verifyFormat("func(a
, b
,\n"
15028 " \"long long long \"\n"
15030 "func(a
, b
, \"long long long long\");",
15031 getLLVMStyleWithColumns(24));
15032 // In a function call with three operands, both the second and the third must
15033 // be broken with a line break before them.
15034 verifyFormat("func(a
,\n"
15035 " \"long long long \"\n"
15037 " \"long long long \"\n"
15039 "func(a
, \"long long long long\", \"long long long long\");",
15040 getLLVMStyleWithColumns(24));
15041 // In a chain of << with two operands, the second can be broken with no line
15042 // break before it.
15043 verifyFormat("a
<< \"line line
\"\n"
15045 "a
<< \"line line line
\";", getLLVMStyleWithColumns(20));
15046 // In a chain of << with three operands, the second can be broken with no line
15047 // break before it.
15048 verifyFormat("abcde
<< \"line
\"\n"
15051 "abcde
<< \"line line line
\" << c
;",
15052 getLLVMStyleWithColumns(20));
15053 // In a chain of << with three operands, the third must be broken with a line
15054 // break before it.
15055 verifyFormat("a
<< b
\n"
15056 " << \"line line
\"\n"
15058 "a
<< b
<< \"line line line
\";", getLLVMStyleWithColumns(20));
15059 // In a chain of << with three operands, the second can be broken with no line
15060 // break before it and the third must be broken with a line break before it.
15061 verifyFormat("abcd
<< \"line line
\"\n"
15063 " << \"line line
\"\n"
15065 "abcd
<< \"line line line
\" << \"line line line
\";",
15066 getLLVMStyleWithColumns(20));
15067 // In a chain of binary operators with two operands, the second can be broken
15068 // with no line break before it.
15069 verifyFormat("abcd
+ \"line line
\"\n"
15071 "abcd
+ \"line line line line
\";", getLLVMStyleWithColumns(20));
15072 // In a chain of binary operators with three operands, the second must be
15073 // broken with a line break before it.
15074 verifyFormat("abcd
+\n"
15075 " \"line line
\"\n"
15076 " \"line line
\" +\n"
15078 "abcd
+ \"line line line line
\" + e
;",
15079 getLLVMStyleWithColumns(20));
15080 // In a function call with two operands, with AlignAfterOpenBracket enabled,
15081 // the first must be broken with a line break before it.
15082 FormatStyle Style = getLLVMStyleWithColumns(25);
15083 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
15084 verifyFormat("someFunction(\n"
15085 " \"long long long \"\n"
15088 "someFunction(\"long long long long\", a
);", Style);
15089 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
15090 verifyFormat("someFunction(\n"
15091 " \"long long long \"\n"
15098 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
15099 verifyFormat("aaaaaaaaaaa
= \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\\\n"
15100 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\\\n"
15101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\";",
15102 "aaaaaaaaaaa
= \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\\\n"
15103 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\\\n"
15104 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\";");
15107 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
15108 verifyFormat("f(g(R
\"x(raw literal
)x
\", a
), b
);",
15109 "f(g(R
\"x(raw literal
)x
\", a
), b
);", getGoogleStyle());
15110 verifyFormat("fffffffffff(g(R
\"x(\n"
15111 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15115 "fffffffffff(g(R
\"x(\n"
15116 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15118 getGoogleStyleWithColumns(20));
15119 verifyFormat("fffffffffff(\n"
15121 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15125 "fffffffffff(g(R
\"x(qqq
\n"
15126 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15128 getGoogleStyleWithColumns(20));
15130 verifyNoChange("fffffffffff(R
\"x(\n"
15131 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15133 getGoogleStyleWithColumns(20));
15134 verifyFormat("fffffffffff(R
\"x(\n"
15135 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15137 "fffffffffff(R
\"x(\n"
15138 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15140 getGoogleStyleWithColumns(20));
15141 verifyFormat("fffffffffff(\n"
15143 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15148 "multiline raw string literal xxxxxxxxxxxxxx
\n"
15150 getGoogleStyleWithColumns(20));
15151 verifyFormat("fffffffffff(R
\"(single line raw string
)\" + bbbbbb
);",
15153 " R
\"(single line raw string
)\" + bbbbbb
);");
15156 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
15157 verifyFormat("string a
= \"unterminated
;");
15158 verifyFormat("function(\"unterminated
,\n"
15159 " OtherParameter
);",
15160 "function( \"unterminated
,\n"
15161 " OtherParameter
);");
15164 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
15165 FormatStyle Style = getLLVMStyle();
15166 Style.Standard = FormatStyle::LS_Cpp03;
15167 verifyFormat("#define x(_a) printf(\"foo\" _a);",
15168 "#define x(_a) printf(\"foo\"_a);", Style);
15171 TEST_F(FormatTest, CppLexVersion) {
15172 FormatStyle Style = getLLVMStyle();
15173 // Formatting of x * y differs if x is a type.
15174 verifyFormat("void foo() { MACRO(a * b); }", Style);
15175 verifyFormat("void foo() { MACRO(int *b); }", Style);
15177 // LLVM style uses latest lexer.
15178 verifyFormat("void foo() { MACRO(char8_t *b); }", Style);
15179 Style.Standard = FormatStyle::LS_Cpp17;
15180 // But in c++17, char8_t isn't a keyword.
15181 verifyFormat("void foo() { MACRO(char8_t * b); }", Style);
15184 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
15186 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
15187 verifyFormat("someFunction(\"aaabbbcccd\"\n"
15189 "someFunction(\"aaabbbcccdddeeefff\");",
15190 getLLVMStyleWithColumns(25));
15191 verifyFormat("someFunction1234567890(\n"
15192 " \"aaabbbcccdddeeefff\");",
15193 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15194 getLLVMStyleWithColumns(26));
15195 verifyFormat("someFunction1234567890(\n"
15196 " \"aaabbbcccdddeeeff\"\n"
15198 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15199 getLLVMStyleWithColumns(25));
15200 verifyFormat("someFunction1234567890(\n"
15201 " \"aaabbbcccdddeeeff\"\n"
15203 "someFunction1234567890(\"aaabbbcccdddeeefff\");",
15204 getLLVMStyleWithColumns(24));
15205 verifyFormat("someFunction(\n"
15206 " \"aaabbbcc ddde \"\n"
15208 "someFunction(\"aaabbbcc ddde efff\");",
15209 getLLVMStyleWithColumns(25));
15210 verifyFormat("someFunction(\"aaabbbccc \"\n"
15212 "someFunction(\"aaabbbccc ddeeefff\");",
15213 getLLVMStyleWithColumns(25));
15214 verifyFormat("someFunction1234567890(\n"
15216 " \"cccdddeeefff\");",
15217 "someFunction1234567890(\"aaabb cccdddeeefff\");",
15218 getLLVMStyleWithColumns(25));
15219 verifyFormat("#define A \\\n"
15221 " \"123456789\" \\\n"
15224 "#define A string s = \"1234567890\"; int i;",
15225 getLLVMStyleWithColumns(20));
15226 verifyFormat("someFunction(\n"
15228 " \"dddeeefff\");",
15229 "someFunction(\"aaabbbcc dddeeefff\");",
15230 getLLVMStyleWithColumns(25));
15233 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
15234 verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3));
15235 verifyFormat("\"\\\"", getLLVMStyleWithColumns(2));
15236 // FIXME: unstable test case
15237 EXPECT_EQ("\"test\"\n"
15239 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
15240 // FIXME: unstable test case
15241 EXPECT_EQ("\"tes\\\\\"\n"
15243 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
15244 // FIXME: unstable test case
15245 EXPECT_EQ("\"\\\\\\\\\"\n"
15247 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
15248 verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7));
15249 // FIXME: unstable test case
15250 EXPECT_EQ("\"\\uff01\"\n"
15252 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
15253 verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11));
15254 // FIXME: unstable test case
15255 EXPECT_EQ("\"\\x000000000001\"\n"
15257 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
15258 verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15));
15259 verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7));
15260 // FIXME: unstable test case
15261 EXPECT_EQ("\"test\"\n"
15264 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
15265 // FIXME: unstable test case
15266 EXPECT_EQ("\"test\\000\"\n"
15269 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
15272 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
15273 verifyFormat("void f() {\n"
15276 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
15281 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
15283 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
15286 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
15287 verifyFormat("class X {\n"
15291 getLLVMStyleWithColumns(12));
15294 TEST_F(FormatTest, ConfigurableIndentWidth) {
15295 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
15296 EightIndent.IndentWidth = 8;
15297 EightIndent.ContinuationIndentWidth = 8;
15298 verifyFormat("void f() {\n"
15299 " someFunction();\n"
15305 verifyFormat("class X {\n"
15310 verifyFormat("int x[] = {\n"
15316 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
15317 verifyFormat("double\n"
15319 getLLVMStyleWithColumns(8));
15322 TEST_F(FormatTest, ConfigurableUseOfTab) {
15323 FormatStyle Tab = getLLVMStyleWithColumns(42);
15324 Tab.IndentWidth = 8;
15325 Tab.UseTab = FormatStyle::UT_Always;
15326 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
15328 verifyFormat("if (aaaaaaaa && // q\n"
15331 "if (aaaaaaaa &&// q\n"
15335 verifyFormat("if (aaa && bbb) // w\n"
15337 "if(aaa&&bbb)// w\n"
15341 verifyFormat("class X {\n"
15343 "\t\tsomeFunction(parameter1,\n"
15344 "\t\t\t parameter2);\n"
15348 verifyFormat("#define A \\\n"
15349 "\tvoid f() { \\\n"
15350 "\t\tsomeFunction( \\\n"
15351 "\t\t parameter1, \\\n"
15352 "\t\t parameter2); \\\n"
15355 verifyFormat("int a;\t // x\n"
15356 "int bbbbbbbb; // x",
15359 FormatStyle TabAlignment
= Tab
;
15360 TabAlignment
.AlignConsecutiveDeclarations
.Enabled
= true;
15361 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Left
;
15362 verifyFormat("unsigned long long big;\n"
15365 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Middle
;
15366 verifyFormat("unsigned long long big;\n"
15369 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Right
;
15370 verifyFormat("unsigned long long big;\n"
15375 Tab
.IndentWidth
= 8;
15376 verifyFormat("class TabWidth4Indent8 {\n"
15378 "\t\t\t\tsomeFunction(parameter1,\n"
15379 "\t\t\t\t\t\t\t parameter2);\n"
15385 Tab
.IndentWidth
= 4;
15386 verifyFormat("class TabWidth4Indent4 {\n"
15388 "\t\tsomeFunction(parameter1,\n"
15389 "\t\t\t\t\t parameter2);\n"
15395 Tab
.IndentWidth
= 4;
15396 verifyFormat("class TabWidth8Indent4 {\n"
15398 "\tsomeFunction(parameter1,\n"
15399 "\t\t parameter2);\n"
15405 Tab
.IndentWidth
= 8;
15406 verifyFormat("/*\n"
15407 "\t a\t\tcomment\n"
15408 "\t in multiple lines\n"
15411 " \t \t a\t\tcomment\t \t\n"
15412 " \t \t in multiple lines\t\n"
15416 TabAlignment
.UseTab
= FormatStyle::UT_ForIndentation
;
15417 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Left
;
15418 verifyFormat("void f() {\n"
15419 "\tunsigned long long big;\n"
15423 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Middle
;
15424 verifyFormat("void f() {\n"
15425 "\tunsigned long long big;\n"
15429 TabAlignment
.PointerAlignment
= FormatStyle::PAS_Right
;
15430 verifyFormat("void f() {\n"
15431 "\tunsigned long long big;\n"
15436 Tab
.UseTab
= FormatStyle::UT_ForIndentation
;
15438 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15439 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15440 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15441 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15442 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15443 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15446 verifyFormat("enum AA {\n"
15447 "\ta1, // Force multiple lines\n"
15452 verifyFormat("if (aaaaaaaa && // q\n"
15455 "if (aaaaaaaa &&// q\n"
15459 verifyFormat("class X {\n"
15461 "\t\tsomeFunction(parameter1,\n"
15462 "\t\t parameter2);\n"
15470 "\t\t someFunction(aaaaaaaa,\n"
15487 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15488 "\t bbbbbbbbbbbbb\n"
15493 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15498 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15499 "\t// bbbbbbbbbbbbb\n"
15502 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15507 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15508 "\t bbbbbbbbbbbbb\n"
15513 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15517 verifyNoChange("{\n"
15523 verifyNoChange("{\n"
15530 verifyFormat("void f() {\n"
15531 "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
15532 "\t : bbbbbbbbbbbbbbbbbb\n"
15535 FormatStyle TabNoBreak
= Tab
;
15536 TabNoBreak
.BreakBeforeTernaryOperators
= false;
15537 verifyFormat("void f() {\n"
15538 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
15539 "\t bbbbbbbbbbbbbbbbbb\n"
15542 verifyFormat("void f() {\n"
15543 "\treturn true ?\n"
15544 "\t aaaaaaaaaaaaaaaaaaaa :\n"
15545 "\t bbbbbbbbbbbbbbbbbbbb\n"
15549 Tab
.UseTab
= FormatStyle::UT_Never
;
15550 verifyFormat("/*\n"
15552 " in multiple lines\n"
15555 " \t \t a\t\tcomment\t \t\n"
15556 " \t \t in multiple lines\t\n"
15559 verifyFormat("/* some\n"
15562 " \t \t comment */",
15564 verifyFormat("int a; /* some\n"
15566 " \t \t int a; /* some\n"
15567 " \t \t comment */",
15570 verifyFormat("int a; /* some\n"
15572 " \t \t int\ta; /* some\n"
15573 " \t \t comment */",
15575 verifyFormat("f(\"\t\t\"); /* some\n"
15577 " \t \t f(\"\t\t\"); /* some\n"
15578 " \t \t comment */",
15594 Tab
.UseTab
= FormatStyle::UT_ForContinuationAndIndentation
;
15596 Tab
.IndentWidth
= 8;
15597 verifyFormat("if (aaaaaaaa && // q\n"
15600 "if (aaaaaaaa &&// q\n"
15604 verifyFormat("if (aaa && bbb) // w\n"
15606 "if(aaa&&bbb)// w\n"
15609 verifyFormat("class X {\n"
15611 "\t\tsomeFunction(parameter1,\n"
15612 "\t\t\t parameter2);\n"
15616 verifyFormat("#define A \\\n"
15617 "\tvoid f() { \\\n"
15618 "\t\tsomeFunction( \\\n"
15619 "\t\t parameter1, \\\n"
15620 "\t\t parameter2); \\\n"
15624 Tab
.IndentWidth
= 8;
15625 verifyFormat("class TabWidth4Indent8 {\n"
15627 "\t\t\t\tsomeFunction(parameter1,\n"
15628 "\t\t\t\t\t\t\t parameter2);\n"
15633 Tab
.IndentWidth
= 4;
15634 verifyFormat("class TabWidth4Indent4 {\n"
15636 "\t\tsomeFunction(parameter1,\n"
15637 "\t\t\t\t\t parameter2);\n"
15642 Tab
.IndentWidth
= 4;
15643 verifyFormat("class TabWidth8Indent4 {\n"
15645 "\tsomeFunction(parameter1,\n"
15646 "\t\t parameter2);\n"
15651 Tab
.IndentWidth
= 8;
15652 verifyFormat("/*\n"
15653 "\t a\t\tcomment\n"
15654 "\t in multiple lines\n"
15657 " \t \t a\t\tcomment\t \t\n"
15658 " \t \t in multiple lines\t\n"
15662 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15663 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15664 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15665 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15666 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15667 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15670 verifyFormat("enum AA {\n"
15671 "\ta1, // Force multiple lines\n"
15676 verifyFormat("if (aaaaaaaa && // q\n"
15679 "if (aaaaaaaa &&// q\n"
15683 verifyFormat("class X {\n"
15685 "\t\tsomeFunction(parameter1,\n"
15686 "\t\t\t parameter2);\n"
15694 "\t\t someFunction(aaaaaaaa,\n"
15695 "\t\t\t\t bbbbbbb);\n"
15711 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15712 "\t bbbbbbbbbbbbb\n"
15717 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15722 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15723 "\t// bbbbbbbbbbbbb\n"
15726 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15731 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15732 "\t bbbbbbbbbbbbb\n"
15737 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15741 verifyNoChange("{\n"
15747 verifyNoChange("{\n"
15753 verifyFormat("/* some\n"
15756 " \t \t comment */",
15758 verifyFormat("int a; /* some\n"
15760 " \t \t int a; /* some\n"
15761 " \t \t comment */",
15763 verifyFormat("int a; /* some\n"
15765 " \t \t int\ta; /* some\n"
15766 " \t \t comment */",
15768 verifyFormat("f(\"\t\t\"); /* some\n"
15770 " \t \t f(\"\t\t\"); /* some\n"
15771 " \t \t comment */",
15787 Tab
.IndentWidth
= 2;
15799 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15800 "\t\tbbbbbbbbbbbbb\n"
15805 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15809 Tab
.AlignConsecutiveAssignments
.Enabled
= true;
15810 Tab
.AlignConsecutiveDeclarations
.Enabled
= true;
15812 Tab
.IndentWidth
= 4;
15813 verifyFormat("class Assign {\n"
15815 "\t\tint x = 123;\n"
15816 "\t\tint random = 4;\n"
15817 "\t\tstd::string alphabet =\n"
15818 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
15823 Tab
.UseTab
= FormatStyle::UT_AlignWithSpaces
;
15825 Tab
.IndentWidth
= 8;
15826 verifyFormat("if (aaaaaaaa && // q\n"
15829 "if (aaaaaaaa &&// q\n"
15833 verifyFormat("if (aaa && bbb) // w\n"
15835 "if(aaa&&bbb)// w\n"
15838 verifyFormat("class X {\n"
15840 "\t\tsomeFunction(parameter1,\n"
15841 "\t\t parameter2);\n"
15845 verifyFormat("#define A \\\n"
15846 "\tvoid f() { \\\n"
15847 "\t\tsomeFunction( \\\n"
15848 "\t\t parameter1, \\\n"
15849 "\t\t parameter2); \\\n"
15853 Tab
.IndentWidth
= 8;
15854 verifyFormat("class TabWidth4Indent8 {\n"
15856 "\t\t\t\tsomeFunction(parameter1,\n"
15857 "\t\t\t\t parameter2);\n"
15862 Tab
.IndentWidth
= 4;
15863 verifyFormat("class TabWidth4Indent4 {\n"
15865 "\t\tsomeFunction(parameter1,\n"
15866 "\t\t parameter2);\n"
15871 Tab
.IndentWidth
= 4;
15872 verifyFormat("class TabWidth8Indent4 {\n"
15874 "\tsomeFunction(parameter1,\n"
15875 "\t parameter2);\n"
15880 Tab
.IndentWidth
= 8;
15881 verifyFormat("/*\n"
15883 " in multiple lines\n"
15886 " \t \t a\t\tcomment\t \t\n"
15887 " \t \t in multiple lines\t\n"
15891 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15892 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15893 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15894 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15895 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15896 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
15899 verifyFormat("enum AA {\n"
15900 "\ta1, // Force multiple lines\n"
15905 verifyFormat("if (aaaaaaaa && // q\n"
15908 "if (aaaaaaaa &&// q\n"
15912 verifyFormat("class X {\n"
15914 "\t\tsomeFunction(parameter1,\n"
15915 "\t\t parameter2);\n"
15923 "\t\t someFunction(aaaaaaaa,\n"
15940 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15941 "\t bbbbbbbbbbbbb\n"
15946 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15951 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15952 "\t// bbbbbbbbbbbbb\n"
15955 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15960 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
15961 "\t bbbbbbbbbbbbb\n"
15966 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
15970 verifyNoChange("{\n"
15976 verifyNoChange("{\n"
15982 verifyFormat("/* some\n"
15985 " \t \t comment */",
15987 verifyFormat("int a; /* some\n"
15989 " \t \t int a; /* some\n"
15990 " \t \t comment */",
15992 verifyFormat("int a; /* some\n"
15994 " \t \t int\ta; /* some\n"
15995 " \t \t comment */",
15997 verifyFormat("f(\"\t\t\"); /* some\n"
15999 " \t \t f(\"\t\t\"); /* some\n"
16000 " \t \t comment */",
16016 Tab
.IndentWidth
= 2;
16028 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
16029 "\t bbbbbbbbbbbbb\n"
16034 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
16038 Tab
.AlignConsecutiveAssignments
.Enabled
= true;
16039 Tab
.AlignConsecutiveDeclarations
.Enabled
= true;
16041 Tab
.IndentWidth
= 4;
16042 verifyFormat("class Assign {\n"
16044 "\t\tint x = 123;\n"
16045 "\t\tint random = 4;\n"
16046 "\t\tstd::string alphabet =\n"
16047 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
16051 Tab
.AlignOperands
= FormatStyle::OAS_Align
;
16052 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n"
16053 " cccccccccccccccccccc;",
16056 verifyFormat("int aaaaaaaaaa =\n"
16057 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
16059 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n"
16060 " : bbbbbbbbbbbbbb ? 222222222222222\n"
16061 " : 333333333333333;",
16063 Tab
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
16064 Tab
.AlignOperands
= FormatStyle::OAS_AlignAfterOperator
;
16065 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n"
16066 " + cccccccccccccccccccc;",
16070 TEST_F(FormatTest
, ZeroTabWidth
) {
16071 FormatStyle Tab
= getLLVMStyleWithColumns(42);
16072 Tab
.IndentWidth
= 8;
16073 Tab
.UseTab
= FormatStyle::UT_Never
;
16075 verifyFormat("void a(){\n"
16076 " // line starts with '\t'\n"
16079 "\t// line starts with '\t'\n"
16083 verifyFormat("void a(){\n"
16084 " // line starts with '\t'\n"
16087 "\t\t// line starts with '\t'\n"
16091 Tab
.UseTab
= FormatStyle::UT_ForIndentation
;
16092 verifyFormat("void a(){\n"
16093 " // line starts with '\t'\n"
16096 "\t// line starts with '\t'\n"
16100 verifyFormat("void a(){\n"
16101 " // line starts with '\t'\n"
16104 "\t\t// line starts with '\t'\n"
16108 Tab
.UseTab
= FormatStyle::UT_ForContinuationAndIndentation
;
16109 verifyFormat("void a(){\n"
16110 " // line starts with '\t'\n"
16113 "\t// line starts with '\t'\n"
16117 verifyFormat("void a(){\n"
16118 " // line starts with '\t'\n"
16121 "\t\t// line starts with '\t'\n"
16125 Tab
.UseTab
= FormatStyle::UT_AlignWithSpaces
;
16126 verifyFormat("void a(){\n"
16127 " // line starts with '\t'\n"
16130 "\t// line starts with '\t'\n"
16134 verifyFormat("void a(){\n"
16135 " // line starts with '\t'\n"
16138 "\t\t// line starts with '\t'\n"
16142 Tab
.UseTab
= FormatStyle::UT_Always
;
16143 verifyFormat("void a(){\n"
16144 "// line starts with '\t'\n"
16147 "\t// line starts with '\t'\n"
16151 verifyFormat("void a(){\n"
16152 "// line starts with '\t'\n"
16155 "\t\t// line starts with '\t'\n"
16160 TEST_F(FormatTest
, CalculatesOriginalColumn
) {
16161 verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16164 " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16167 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16170 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
16173 verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16177 "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16181 verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16184 " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
16189 TEST_F(FormatTest
, ConfigurableSpaceBeforeParens
) {
16190 FormatStyle NoSpace
= getLLVMStyle();
16191 NoSpace
.SpaceBeforeParens
= FormatStyle::SBPO_Never
;
16193 verifyFormat("while(true)\n"
16196 verifyFormat("for(;;)\n"
16199 verifyFormat("if(true)\n"
16204 verifyFormat("do {\n"
16205 " do_something();\n"
16206 "} while(something());",
16208 verifyFormat("switch(x) {\n"
16213 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace
);
16214 verifyFormat("size_t x = sizeof(x);", NoSpace
);
16215 verifyFormat("auto f(int x) -> decltype(x);", NoSpace
);
16216 verifyFormat("auto f(int x) -> typeof(x);", NoSpace
);
16217 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace
);
16218 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace
);
16219 verifyFormat("int f(T x) noexcept(x.create());", NoSpace
);
16220 verifyFormat("alignas(128) char a[128];", NoSpace
);
16221 verifyFormat("size_t x = alignof(MyType);", NoSpace
);
16222 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace
);
16223 verifyFormat("int f() throw(Deprecated);", NoSpace
);
16224 verifyFormat("typedef void (*cb)(int);", NoSpace
);
16225 verifyFormat("T A::operator()();", NoSpace
);
16226 verifyFormat("X A::operator++(T);", NoSpace
);
16227 verifyFormat("auto lambda = []() { return 0; };", NoSpace
);
16229 FormatStyle Space
= getLLVMStyle();
16230 Space
.SpaceBeforeParens
= FormatStyle::SBPO_Always
;
16232 verifyFormat("int f ();", Space
);
16233 verifyFormat("bool operator< ();", Space
);
16234 verifyFormat("bool operator> ();", Space
);
16235 verifyFormat("void f (int a, T b) {\n"
16240 verifyFormat("if (true)\n"
16245 verifyFormat("do {\n"
16246 " do_something ();\n"
16247 "} while (something ());",
16249 verifyFormat("switch (x) {\n"
16254 verifyFormat("A::A () : a (1) {}", Space
);
16255 verifyFormat("void f () __attribute__ ((asdf));", Space
);
16256 verifyFormat("*(&a + 1);\n"
16258 "a[(b + c) * d];\n"
16259 "(((a + 1) * 2) + 3) * 4;",
16261 verifyFormat("#define A(x) x", Space
);
16262 verifyFormat("#define A (x) x", Space
);
16263 verifyFormat("#if defined(x)\n"
16266 verifyFormat("auto i = std::make_unique<int> (5);", Space
);
16267 verifyFormat("size_t x = sizeof (x);", Space
);
16268 verifyFormat("auto f (int x) -> decltype (x);", Space
);
16269 verifyFormat("auto f (int x) -> typeof (x);", Space
);
16270 verifyFormat("auto f (int x) -> _Atomic (x);", Space
);
16271 verifyFormat("auto f (int x) -> __underlying_type (x);", Space
);
16272 verifyFormat("int f (T x) noexcept (x.create ());", Space
);
16273 verifyFormat("alignas (128) char a[128];", Space
);
16274 verifyFormat("size_t x = alignof (MyType);", Space
);
16275 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space
);
16276 verifyFormat("int f () throw (Deprecated);", Space
);
16277 verifyFormat("typedef void (*cb) (int);", Space
);
16278 // FIXME these tests regressed behaviour.
16279 // verifyFormat("T A::operator() ();", Space);
16280 // verifyFormat("X A::operator++ (T);", Space);
16281 verifyFormat("auto lambda = [] () { return 0; };", Space
);
16282 verifyFormat("int x = int (y);", Space
);
16283 verifyFormat("#define F(...) __VA_OPT__ (__VA_ARGS__)", Space
);
16284 verifyFormat("__builtin_LINE ()", Space
);
16285 verifyFormat("__builtin_UNKNOWN ()", Space
);
16287 FormatStyle SomeSpace
= getLLVMStyle();
16288 SomeSpace
.SpaceBeforeParens
= FormatStyle::SBPO_NonEmptyParentheses
;
16290 verifyFormat("[]() -> float {}", SomeSpace
);
16291 verifyFormat("[] (auto foo) {}", SomeSpace
);
16292 verifyFormat("[foo]() -> int {}", SomeSpace
);
16293 verifyFormat("int f();", SomeSpace
);
16294 verifyFormat("void f (int a, T b) {\n"
16299 verifyFormat("if (true)\n"
16304 verifyFormat("do {\n"
16305 " do_something();\n"
16306 "} while (something());",
16308 verifyFormat("switch (x) {\n"
16313 verifyFormat("A::A() : a (1) {}", SomeSpace
);
16314 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace
);
16315 verifyFormat("*(&a + 1);\n"
16317 "a[(b + c) * d];\n"
16318 "(((a + 1) * 2) + 3) * 4;",
16320 verifyFormat("#define A(x) x", SomeSpace
);
16321 verifyFormat("#define A (x) x", SomeSpace
);
16322 verifyFormat("#if defined(x)\n"
16325 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace
);
16326 verifyFormat("size_t x = sizeof (x);", SomeSpace
);
16327 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace
);
16328 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace
);
16329 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace
);
16330 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace
);
16331 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace
);
16332 verifyFormat("alignas (128) char a[128];", SomeSpace
);
16333 verifyFormat("size_t x = alignof (MyType);", SomeSpace
);
16334 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16336 verifyFormat("int f() throw (Deprecated);", SomeSpace
);
16337 verifyFormat("typedef void (*cb) (int);", SomeSpace
);
16338 verifyFormat("T A::operator()();", SomeSpace
);
16339 // FIXME these tests regressed behaviour.
16340 // verifyFormat("X A::operator++ (T);", SomeSpace);
16341 verifyFormat("int x = int (y);", SomeSpace
);
16342 verifyFormat("auto lambda = []() { return 0; };", SomeSpace
);
16344 FormatStyle SpaceControlStatements
= getLLVMStyle();
16345 SpaceControlStatements
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
16346 SpaceControlStatements
.SpaceBeforeParensOptions
.AfterControlStatements
= true;
16348 verifyFormat("while (true)\n"
16350 SpaceControlStatements
);
16351 verifyFormat("if (true)\n"
16355 SpaceControlStatements
);
16356 verifyFormat("for (;;) {\n"
16357 " do_something();\n"
16359 SpaceControlStatements
);
16360 verifyFormat("do {\n"
16361 " do_something();\n"
16362 "} while (something());",
16363 SpaceControlStatements
);
16364 verifyFormat("switch (x) {\n"
16368 SpaceControlStatements
);
16370 FormatStyle SpaceFuncDecl
= getLLVMStyle();
16371 SpaceFuncDecl
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
16372 SpaceFuncDecl
.SpaceBeforeParensOptions
.AfterFunctionDeclarationName
= true;
16374 verifyFormat("int f ();", SpaceFuncDecl
);
16375 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl
);
16376 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl
);
16377 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl
);
16378 verifyFormat("#define A(x) x", SpaceFuncDecl
);
16379 verifyFormat("#define A (x) x", SpaceFuncDecl
);
16380 verifyFormat("#if defined(x)\n"
16383 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl
);
16384 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl
);
16385 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl
);
16386 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl
);
16387 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl
);
16388 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl
);
16389 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl
);
16390 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl
);
16391 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl
);
16392 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16394 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl
);
16395 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl
);
16396 // FIXME these tests regressed behaviour.
16397 // verifyFormat("T A::operator() ();", SpaceFuncDecl);
16398 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl);
16399 verifyFormat("T A::operator()() {}", SpaceFuncDecl
);
16400 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl
);
16401 verifyFormat("int x = int(y);", SpaceFuncDecl
);
16402 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
16405 FormatStyle SpaceFuncDef
= getLLVMStyle();
16406 SpaceFuncDef
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
16407 SpaceFuncDef
.SpaceBeforeParensOptions
.AfterFunctionDefinitionName
= true;
16409 verifyFormat("int f();", SpaceFuncDef
);
16410 verifyFormat("void f (int a, T b) {}", SpaceFuncDef
);
16411 verifyFormat("A::A() : a(1) {}", SpaceFuncDef
);
16412 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef
);
16413 verifyFormat("#define A(x) x", SpaceFuncDef
);
16414 verifyFormat("#define A (x) x", SpaceFuncDef
);
16415 verifyFormat("#if defined(x)\n"
16418 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef
);
16419 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef
);
16420 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef
);
16421 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef
);
16422 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef
);
16423 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef
);
16424 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef
);
16425 verifyFormat("alignas(128) char a[128];", SpaceFuncDef
);
16426 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef
);
16427 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");",
16429 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef
);
16430 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef
);
16431 verifyFormat("T A::operator()();", SpaceFuncDef
);
16432 verifyFormat("X A::operator++(T);", SpaceFuncDef
);
16433 // verifyFormat("T A::operator() () {}", SpaceFuncDef);
16434 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef
);
16435 verifyFormat("int x = int(y);", SpaceFuncDef
);
16436 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
16439 FormatStyle SpaceIfMacros
= getLLVMStyle();
16440 SpaceIfMacros
.IfMacros
.clear();
16441 SpaceIfMacros
.IfMacros
.push_back("MYIF");
16442 SpaceIfMacros
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
16443 SpaceIfMacros
.SpaceBeforeParensOptions
.AfterIfMacros
= true;
16444 verifyFormat("MYIF (a)\n return;", SpaceIfMacros
);
16445 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros
);
16446 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros
);
16448 FormatStyle SpaceForeachMacros
= getLLVMStyle();
16449 EXPECT_EQ(SpaceForeachMacros
.AllowShortBlocksOnASingleLine
,
16450 FormatStyle::SBS_Never
);
16451 EXPECT_EQ(SpaceForeachMacros
.AllowShortLoopsOnASingleLine
, false);
16452 SpaceForeachMacros
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
16453 SpaceForeachMacros
.SpaceBeforeParensOptions
.AfterForeachMacros
= true;
16454 verifyFormat("for (;;) {\n"
16456 SpaceForeachMacros
);
16457 verifyFormat("foreach (Item *item, itemlist) {\n"
16459 SpaceForeachMacros
);
16460 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n"
16462 SpaceForeachMacros
);
16463 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n"
16465 SpaceForeachMacros
);
16466 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros
);
16468 FormatStyle SomeSpace2
= getLLVMStyle();
16469 SomeSpace2
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
16470 SomeSpace2
.SpaceBeforeParensOptions
.BeforeNonEmptyParentheses
= true;
16471 verifyFormat("[]() -> float {}", SomeSpace2
);
16472 verifyFormat("[] (auto foo) {}", SomeSpace2
);
16473 verifyFormat("[foo]() -> int {}", SomeSpace2
);
16474 verifyFormat("int f();", SomeSpace2
);
16475 verifyFormat("void f (int a, T b) {\n"
16480 verifyFormat("if (true)\n"
16485 verifyFormat("do {\n"
16486 " do_something();\n"
16487 "} while (something());",
16489 verifyFormat("switch (x) {\n"
16494 verifyFormat("A::A() : a (1) {}", SomeSpace2
);
16495 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2
);
16496 verifyFormat("*(&a + 1);\n"
16498 "a[(b + c) * d];\n"
16499 "(((a + 1) * 2) + 3) * 4;",
16501 verifyFormat("#define A(x) x", SomeSpace2
);
16502 verifyFormat("#define A (x) x", SomeSpace2
);
16503 verifyFormat("#if defined(x)\n"
16506 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2
);
16507 verifyFormat("size_t x = sizeof (x);", SomeSpace2
);
16508 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2
);
16509 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2
);
16510 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2
);
16511 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2
);
16512 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2
);
16513 verifyFormat("alignas (128) char a[128];", SomeSpace2
);
16514 verifyFormat("size_t x = alignof (MyType);", SomeSpace2
);
16515 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");",
16517 verifyFormat("int f() throw (Deprecated);", SomeSpace2
);
16518 verifyFormat("typedef void (*cb) (int);", SomeSpace2
);
16519 verifyFormat("T A::operator()();", SomeSpace2
);
16520 // verifyFormat("X A::operator++ (T);", SomeSpace2);
16521 verifyFormat("int x = int (y);", SomeSpace2
);
16522 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2
);
16524 FormatStyle SpaceAfterOverloadedOperator
= getLLVMStyle();
16525 SpaceAfterOverloadedOperator
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
16526 SpaceAfterOverloadedOperator
.SpaceBeforeParensOptions
16527 .AfterOverloadedOperator
= true;
16529 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator
);
16530 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator
);
16531 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator
);
16532 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator
);
16534 SpaceAfterOverloadedOperator
.SpaceBeforeParensOptions
16535 .AfterOverloadedOperator
= false;
16537 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator
);
16538 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator
);
16539 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator
);
16540 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator
);
16542 auto SpaceAfterRequires
= getLLVMStyle();
16543 SpaceAfterRequires
.SpaceBeforeParens
= FormatStyle::SBPO_Custom
;
16545 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInClause
);
16547 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInExpression
);
16548 verifyFormat("void f(auto x)\n"
16549 " requires requires(int i) { x + i; }\n"
16551 SpaceAfterRequires
);
16552 verifyFormat("void f(auto x)\n"
16553 " requires(requires(int i) { x + i; })\n"
16555 SpaceAfterRequires
);
16556 verifyFormat("if (requires(int i) { x + i; })\n"
16558 SpaceAfterRequires
);
16559 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires
);
16560 verifyFormat("template <typename T>\n"
16561 " requires(Foo<T>)\n"
16563 SpaceAfterRequires
);
16565 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInClause
= true;
16566 verifyFormat("void f(auto x)\n"
16567 " requires requires(int i) { x + i; }\n"
16569 SpaceAfterRequires
);
16570 verifyFormat("void f(auto x)\n"
16571 " requires (requires(int i) { x + i; })\n"
16573 SpaceAfterRequires
);
16574 verifyFormat("if (requires(int i) { x + i; })\n"
16576 SpaceAfterRequires
);
16577 verifyFormat("bool b = requires(int i) { x + i; };", SpaceAfterRequires
);
16578 verifyFormat("template <typename T>\n"
16579 " requires (Foo<T>)\n"
16581 SpaceAfterRequires
);
16583 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInClause
= false;
16584 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInExpression
= true;
16585 verifyFormat("void f(auto x)\n"
16586 " requires requires (int i) { x + i; }\n"
16588 SpaceAfterRequires
);
16589 verifyFormat("void f(auto x)\n"
16590 " requires(requires (int i) { x + i; })\n"
16592 SpaceAfterRequires
);
16593 verifyFormat("if (requires (int i) { x + i; })\n"
16595 SpaceAfterRequires
);
16596 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires
);
16597 verifyFormat("template <typename T>\n"
16598 " requires(Foo<T>)\n"
16600 SpaceAfterRequires
);
16602 SpaceAfterRequires
.SpaceBeforeParensOptions
.AfterRequiresInClause
= true;
16603 verifyFormat("void f(auto x)\n"
16604 " requires requires (int i) { x + i; }\n"
16606 SpaceAfterRequires
);
16607 verifyFormat("void f(auto x)\n"
16608 " requires (requires (int i) { x + i; })\n"
16610 SpaceAfterRequires
);
16611 verifyFormat("if (requires (int i) { x + i; })\n"
16613 SpaceAfterRequires
);
16614 verifyFormat("bool b = requires (int i) { x + i; };", SpaceAfterRequires
);
16615 verifyFormat("template <typename T>\n"
16616 " requires (Foo<T>)\n"
16618 SpaceAfterRequires
);
16621 TEST_F(FormatTest
, SpaceAfterLogicalNot
) {
16622 FormatStyle Spaces
= getLLVMStyle();
16623 Spaces
.SpaceAfterLogicalNot
= true;
16625 verifyFormat("bool x = ! y", Spaces
);
16626 verifyFormat("if (! isFailure())", Spaces
);
16627 verifyFormat("if (! (a && b))", Spaces
);
16628 verifyFormat("\"Error!\"", Spaces
);
16629 verifyFormat("! ! x", Spaces
);
16632 TEST_F(FormatTest
, ConfigurableSpacesInParens
) {
16633 FormatStyle Spaces
= getLLVMStyle();
16635 verifyFormat("do_something(::globalVar);", Spaces
);
16636 verifyFormat("call(x, y, z);", Spaces
);
16637 verifyFormat("call();", Spaces
);
16638 verifyFormat("std::function<void(int, int)> callback;", Spaces
);
16639 verifyFormat("void inFunction() { std::function<void(int, int)> fct; }",
16641 verifyFormat("while ((bool)1)\n"
16644 verifyFormat("for (;;)\n"
16647 verifyFormat("if (true)\n"
16652 verifyFormat("do {\n"
16653 " do_something((int)i);\n"
16654 "} while (something());",
16656 verifyFormat("switch (x) {\n"
16661 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
16662 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces
);
16663 verifyFormat("void f() __attribute__((asdf));", Spaces
);
16665 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
16666 Spaces
.SpacesInParensOptions
= {};
16667 Spaces
.SpacesInParensOptions
.Other
= true;
16668 Spaces
.SpacesInParensOptions
.InConditionalStatements
= true;
16669 verifyFormat("do_something( ::globalVar );", Spaces
);
16670 verifyFormat("call( x, y, z );", Spaces
);
16671 verifyFormat("call();", Spaces
);
16672 verifyFormat("std::function<void( int, int )> callback;", Spaces
);
16673 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
16675 verifyFormat("while ( (bool)1 )\n"
16678 verifyFormat("for ( ;; )\n"
16681 verifyFormat("if ( true )\n"
16683 "else if ( true )\n"
16686 verifyFormat("do {\n"
16687 " do_something( (int)i );\n"
16688 "} while ( something() );",
16690 verifyFormat("switch ( x ) {\n"
16695 verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces
);
16696 verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces
);
16697 verifyFormat("void f() __attribute__( ( asdf ) );", Spaces
);
16699 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
16700 Spaces
.SpacesInParensOptions
= {};
16701 Spaces
.SpacesInParensOptions
.InCStyleCasts
= true;
16702 verifyFormat("Type *A = ( Type * )P;", Spaces
);
16703 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces
);
16704 verifyFormat("x = ( int32 )y;", Spaces
);
16705 verifyFormat("throw ( int32 )x;", Spaces
);
16706 verifyFormat("int a = ( int )(2.0f);", Spaces
);
16707 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces
);
16708 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces
);
16709 verifyFormat("#define x (( int )-1)", Spaces
);
16711 // Run the first set of tests again with:
16712 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
16713 Spaces
.SpacesInParensOptions
= {};
16714 Spaces
.SpacesInParensOptions
.InEmptyParentheses
= true;
16715 Spaces
.SpacesInParensOptions
.InCStyleCasts
= true;
16716 verifyFormat("call(x, y, z);", Spaces
);
16717 verifyFormat("call( );", Spaces
);
16718 verifyFormat("std::function<void(int, int)> callback;", Spaces
);
16719 verifyFormat("while (( bool )1)\n"
16722 verifyFormat("for (;;)\n"
16725 verifyFormat("if (true)\n"
16730 verifyFormat("do {\n"
16731 " do_something(( int )i);\n"
16732 "} while (something( ));",
16734 verifyFormat("switch (x) {\n"
16739 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
16740 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces
);
16741 verifyFormat("void f( ) __attribute__((asdf));", Spaces
);
16743 // Run the first set of tests again with:
16744 Spaces
.SpaceAfterCStyleCast
= true;
16745 verifyFormat("call(x, y, z);", Spaces
);
16746 verifyFormat("call( );", Spaces
);
16747 verifyFormat("std::function<void(int, int)> callback;", Spaces
);
16748 verifyFormat("while (( bool ) 1)\n"
16751 verifyFormat("for (;;)\n"
16754 verifyFormat("if (true)\n"
16759 verifyFormat("do {\n"
16760 " do_something(( int ) i);\n"
16761 "} while (something( ));",
16763 verifyFormat("switch (x) {\n"
16768 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces
);
16769 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces
);
16770 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces
);
16771 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces
);
16772 verifyFormat("bool *y = ( bool * ) (x);", Spaces
);
16773 verifyFormat("throw ( int32 ) x;", Spaces
);
16774 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
16775 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces
);
16776 verifyFormat("void f( ) __attribute__((asdf));", Spaces
);
16778 // Run subset of tests again with:
16779 Spaces
.SpacesInParensOptions
.InCStyleCasts
= false;
16780 Spaces
.SpaceAfterCStyleCast
= true;
16781 verifyFormat("while ((bool) 1)\n"
16784 verifyFormat("do {\n"
16785 " do_something((int) i);\n"
16786 "} while (something( ));",
16789 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces
);
16790 verifyFormat("size_t idx = (size_t) a;", Spaces
);
16791 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces
);
16792 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces
);
16793 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces
);
16794 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces
);
16795 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces
);
16796 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces
);
16797 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces
);
16798 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces
);
16799 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces
);
16800 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces
);
16801 verifyFormat("throw (int32) x;", Spaces
);
16802 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
16803 verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces
);
16804 verifyFormat("void f( ) __attribute__((asdf));", Spaces
);
16806 Spaces
.ColumnLimit
= 80;
16807 Spaces
.IndentWidth
= 4;
16808 Spaces
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
16809 verifyFormat("void foo( ) {\n"
16810 " size_t foo = (*(function))(\n"
16811 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
16812 "BarrrrrrrrrrrrLong,\n"
16813 " FoooooooooLooooong);\n"
16816 Spaces
.SpaceAfterCStyleCast
= false;
16817 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces
);
16818 verifyFormat("size_t idx = (size_t)a;", Spaces
);
16819 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces
);
16820 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces
);
16821 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces
);
16822 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces
);
16823 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces
);
16825 verifyFormat("void foo( ) {\n"
16826 " size_t foo = (*(function))(\n"
16827 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
16828 "BarrrrrrrrrrrrLong,\n"
16829 " FoooooooooLooooong);\n"
16833 Spaces
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
16834 verifyFormat("void foo( ) {\n"
16835 " size_t foo = (*(function))(\n"
16836 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
16837 "BarrrrrrrrrrrrLong,\n"
16838 " FoooooooooLooooong\n"
16842 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces
);
16843 verifyFormat("size_t idx = (size_t)a;", Spaces
);
16844 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces
);
16845 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces
);
16846 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces
);
16847 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces
);
16848 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces
);
16851 TEST_F(FormatTest
, ConfigurableSpacesInSquareBrackets
) {
16852 verifyFormat("int a[5];");
16853 verifyFormat("a[3] += 42;");
16855 FormatStyle Spaces
= getLLVMStyle();
16856 Spaces
.SpacesInSquareBrackets
= true;
16858 verifyFormat("int a[ 5 ];", Spaces
);
16859 verifyFormat("a[ 3 ] += 42;", Spaces
);
16860 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces
);
16861 verifyFormat("double &operator[](int i) { return 0; }\n"
16864 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces
);
16865 verifyFormat("int i = a[ a ][ a ]->f();", Spaces
);
16866 verifyFormat("int i = (*b)[ a ]->f();", Spaces
);
16868 verifyFormat("int c = []() -> int { return 2; }();", Spaces
);
16869 verifyFormat("return [ i, args... ] {};", Spaces
);
16870 verifyFormat("int foo = [ &bar ]() {};", Spaces
);
16871 verifyFormat("int foo = [ = ]() {};", Spaces
);
16872 verifyFormat("int foo = [ & ]() {};", Spaces
);
16873 verifyFormat("int foo = [ =, &bar ]() {};", Spaces
);
16874 verifyFormat("int foo = [ &bar, = ]() {};", Spaces
);
16877 TEST_F(FormatTest
, ConfigurableSpaceBeforeBrackets
) {
16878 FormatStyle NoSpaceStyle
= getLLVMStyle();
16879 verifyFormat("int a[5];", NoSpaceStyle
);
16880 verifyFormat("a[3] += 42;", NoSpaceStyle
);
16882 verifyFormat("int a[1];", NoSpaceStyle
);
16883 verifyFormat("int 1 [a];", NoSpaceStyle
);
16884 verifyFormat("int a[1][2];", NoSpaceStyle
);
16885 verifyFormat("a[7] = 5;", NoSpaceStyle
);
16886 verifyFormat("int a = (f())[23];", NoSpaceStyle
);
16887 verifyFormat("f([] {})", NoSpaceStyle
);
16889 FormatStyle Space
= getLLVMStyle();
16890 Space
.SpaceBeforeSquareBrackets
= true;
16891 verifyFormat("int c = []() -> int { return 2; }();", Space
);
16892 verifyFormat("return [i, args...] {};", Space
);
16894 verifyFormat("int a [5];", Space
);
16895 verifyFormat("a [3] += 42;", Space
);
16896 verifyFormat("constexpr char hello []{\"hello\"};", Space
);
16897 verifyFormat("double &operator[](int i) { return 0; }\n"
16900 verifyFormat("std::unique_ptr<int []> foo() {}", Space
);
16901 verifyFormat("int i = a [a][a]->f();", Space
);
16902 verifyFormat("int i = (*b) [a]->f();", Space
);
16904 verifyFormat("int a [1];", Space
);
16905 verifyFormat("int 1 [a];", Space
);
16906 verifyFormat("int a [1][2];", Space
);
16907 verifyFormat("a [7] = 5;", Space
);
16908 verifyFormat("int a = (f()) [23];", Space
);
16909 verifyFormat("f([] {})", Space
);
16912 TEST_F(FormatTest
, ConfigurableSpaceBeforeAssignmentOperators
) {
16913 verifyFormat("int a = 5;");
16914 verifyFormat("a += 42;");
16915 verifyFormat("a or_eq 8;");
16917 FormatStyle Spaces
= getLLVMStyle();
16918 Spaces
.SpaceBeforeAssignmentOperators
= false;
16919 verifyFormat("int a= 5;", Spaces
);
16920 verifyFormat("a+= 42;", Spaces
);
16921 verifyFormat("a or_eq 8;", Spaces
);
16924 TEST_F(FormatTest
, ConfigurableSpaceBeforeColon
) {
16925 verifyFormat("class Foo : public Bar {};");
16926 verifyFormat("Foo::Foo() : foo(1) {}");
16927 verifyFormat("for (auto a : b) {\n}");
16928 verifyFormat("int x = a ? b : c;");
16933 verifyFormat("switch (x) {\n"
16937 verifyFormat("switch (allBraces) {\n"
16942 " [[fallthrough]];\n"
16949 FormatStyle CtorInitializerStyle
= getLLVMStyleWithColumns(30);
16950 CtorInitializerStyle
.SpaceBeforeCtorInitializerColon
= false;
16951 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle
);
16952 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle
);
16953 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle
);
16954 verifyFormat("int x = a ? b : c;", CtorInitializerStyle
);
16959 CtorInitializerStyle
);
16960 verifyFormat("switch (x) {\n"
16964 CtorInitializerStyle
);
16965 verifyFormat("switch (allBraces) {\n"
16970 " [[fallthrough]];\n"
16976 CtorInitializerStyle
);
16977 CtorInitializerStyle
.BreakConstructorInitializers
=
16978 FormatStyle::BCIS_AfterColon
;
16979 verifyFormat("Fooooooooooo::Fooooooooooo():\n"
16980 " aaaaaaaaaaaaaaaa(1),\n"
16981 " bbbbbbbbbbbbbbbb(2) {}",
16982 CtorInitializerStyle
);
16983 CtorInitializerStyle
.BreakConstructorInitializers
=
16984 FormatStyle::BCIS_BeforeComma
;
16985 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
16986 " : aaaaaaaaaaaaaaaa(1)\n"
16987 " , bbbbbbbbbbbbbbbb(2) {}",
16988 CtorInitializerStyle
);
16989 CtorInitializerStyle
.BreakConstructorInitializers
=
16990 FormatStyle::BCIS_BeforeColon
;
16991 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
16992 " : aaaaaaaaaaaaaaaa(1),\n"
16993 " bbbbbbbbbbbbbbbb(2) {}",
16994 CtorInitializerStyle
);
16995 CtorInitializerStyle
.ConstructorInitializerIndentWidth
= 0;
16996 verifyFormat("Fooooooooooo::Fooooooooooo()\n"
16997 ": aaaaaaaaaaaaaaaa(1),\n"
16998 " bbbbbbbbbbbbbbbb(2) {}",
16999 CtorInitializerStyle
);
17001 FormatStyle InheritanceStyle
= getLLVMStyleWithColumns(30);
17002 InheritanceStyle
.SpaceBeforeInheritanceColon
= false;
17003 verifyFormat("class Foo: public Bar {};", InheritanceStyle
);
17004 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle
);
17005 verifyFormat("for (auto a : b) {\n}", InheritanceStyle
);
17006 verifyFormat("int x = a ? b : c;", InheritanceStyle
);
17012 verifyFormat("switch (x) {\n"
17017 verifyFormat("switch (allBraces) {\n"
17022 " [[fallthrough]];\n"
17029 InheritanceStyle
.BreakInheritanceList
= FormatStyle::BILS_AfterComma
;
17030 verifyFormat("class Foooooooooooooooooooooo\n"
17031 " : public aaaaaaaaaaaaaaaaaa,\n"
17032 " public bbbbbbbbbbbbbbbbbb {\n"
17035 InheritanceStyle
.BreakInheritanceList
= FormatStyle::BILS_AfterColon
;
17036 verifyFormat("class Foooooooooooooooooooooo:\n"
17037 " public aaaaaaaaaaaaaaaaaa,\n"
17038 " public bbbbbbbbbbbbbbbbbb {\n"
17041 InheritanceStyle
.BreakInheritanceList
= FormatStyle::BILS_BeforeComma
;
17042 verifyFormat("class Foooooooooooooooooooooo\n"
17043 " : public aaaaaaaaaaaaaaaaaa\n"
17044 " , public bbbbbbbbbbbbbbbbbb {\n"
17047 InheritanceStyle
.BreakInheritanceList
= FormatStyle::BILS_BeforeColon
;
17048 verifyFormat("class Foooooooooooooooooooooo\n"
17049 " : public aaaaaaaaaaaaaaaaaa,\n"
17050 " public bbbbbbbbbbbbbbbbbb {\n"
17053 InheritanceStyle
.ConstructorInitializerIndentWidth
= 0;
17054 verifyFormat("class Foooooooooooooooooooooo\n"
17055 ": public aaaaaaaaaaaaaaaaaa,\n"
17056 " public bbbbbbbbbbbbbbbbbb {}",
17059 FormatStyle ForLoopStyle
= getLLVMStyle();
17060 ForLoopStyle
.SpaceBeforeRangeBasedForLoopColon
= false;
17061 verifyFormat("class Foo : public Bar {};", ForLoopStyle
);
17062 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle
);
17063 verifyFormat("for (auto a: b) {\n}", ForLoopStyle
);
17064 verifyFormat("int x = a ? b : c;", ForLoopStyle
);
17070 verifyFormat("switch (x) {\n"
17075 verifyFormat("switch (allBraces) {\n"
17080 " [[fallthrough]];\n"
17088 FormatStyle CaseStyle
= getLLVMStyle();
17089 CaseStyle
.SpaceBeforeCaseColon
= true;
17090 verifyFormat("class Foo : public Bar {};", CaseStyle
);
17091 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle
);
17092 verifyFormat("for (auto a : b) {\n}", CaseStyle
);
17093 verifyFormat("int x = a ? b : c;", CaseStyle
);
17094 verifyFormat("switch (x) {\n"
17099 verifyFormat("switch (allBraces) {\n"
17104 " [[fallthrough]];\n"
17111 // Goto labels should not be affected.
17112 verifyFormat("switch (x) {\n"
17117 verifyFormat("switch (x) {\n"
17118 "goto_label: { break; }\n"
17125 FormatStyle NoSpaceStyle
= getLLVMStyle();
17126 EXPECT_EQ(NoSpaceStyle
.SpaceBeforeCaseColon
, false);
17127 NoSpaceStyle
.SpaceBeforeCtorInitializerColon
= false;
17128 NoSpaceStyle
.SpaceBeforeInheritanceColon
= false;
17129 NoSpaceStyle
.SpaceBeforeRangeBasedForLoopColon
= false;
17130 verifyFormat("class Foo: public Bar {};", NoSpaceStyle
);
17131 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle
);
17132 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle
);
17133 verifyFormat("int x = a ? b : c;", NoSpaceStyle
);
17139 verifyFormat("switch (x) {\n"
17144 verifyFormat("switch (allBraces) {\n"
17149 " [[fallthrough]];\n"
17157 FormatStyle InvertedSpaceStyle
= getLLVMStyle();
17158 InvertedSpaceStyle
.SpaceBeforeCaseColon
= true;
17159 InvertedSpaceStyle
.SpaceBeforeCtorInitializerColon
= false;
17160 InvertedSpaceStyle
.SpaceBeforeInheritanceColon
= false;
17161 InvertedSpaceStyle
.SpaceBeforeRangeBasedForLoopColon
= false;
17162 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle
);
17163 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle
);
17164 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle
);
17165 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle
);
17170 InvertedSpaceStyle
);
17171 verifyFormat("switch (x) {\n"
17179 InvertedSpaceStyle
);
17180 verifyFormat("switch (allBraces) {\n"
17185 " [[fallthrough]];\n"
17191 InvertedSpaceStyle
);
17194 TEST_F(FormatTest
, ConfigurableSpaceAroundPointerQualifiers
) {
17195 FormatStyle Style
= getLLVMStyle();
17197 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
17198 Style
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_Default
;
17199 verifyFormat("void* const* x = NULL;", Style
);
17201 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \
17203 Style.PointerAlignment = FormatStyle::Pointers; \
17204 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \
17205 verifyFormat(Code, Style); \
17208 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left
, SAPQ_Default
);
17209 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right
, SAPQ_Default
);
17210 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle
, SAPQ_Default
);
17212 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left
, SAPQ_Before
);
17213 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right
, SAPQ_Before
);
17214 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle
, SAPQ_Before
);
17216 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left
, SAPQ_After
);
17217 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right
, SAPQ_After
);
17218 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle
, SAPQ_After
);
17220 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left
, SAPQ_Both
);
17221 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right
, SAPQ_Both
);
17222 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle
, SAPQ_Both
);
17224 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left
, SAPQ_Default
);
17225 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right
,
17227 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle
,
17230 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left
, SAPQ_Before
);
17231 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right
,
17233 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle
,
17236 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left
, SAPQ_After
);
17237 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right
, SAPQ_After
);
17238 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle
,
17241 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left
, SAPQ_Both
);
17242 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right
, SAPQ_Both
);
17243 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle
, SAPQ_Both
);
17245 #undef verifyQualifierSpaces
17247 FormatStyle Spaces
= getLLVMStyle();
17248 Spaces
.AttributeMacros
.push_back("qualified");
17249 Spaces
.PointerAlignment
= FormatStyle::PAS_Right
;
17250 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_Default
;
17251 verifyFormat("SomeType *volatile *a = NULL;", Spaces
);
17252 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces
);
17253 verifyFormat("std::vector<SomeType *const *> x;", Spaces
);
17254 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces
);
17255 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
17256 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_Before
;
17257 verifyFormat("SomeType * volatile *a = NULL;", Spaces
);
17258 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces
);
17259 verifyFormat("std::vector<SomeType * const *> x;", Spaces
);
17260 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces
);
17261 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
17263 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left.
17264 Spaces
.PointerAlignment
= FormatStyle::PAS_Left
;
17265 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_Before
;
17266 verifyFormat("SomeType* volatile* a = NULL;", Spaces
);
17267 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces
);
17268 verifyFormat("std::vector<SomeType* const*> x;", Spaces
);
17269 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces
);
17270 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
17271 // However, setting it to SAPQ_After should add spaces after __attribute, etc.
17272 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_After
;
17273 verifyFormat("SomeType* volatile * a = NULL;", Spaces
);
17274 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces
);
17275 verifyFormat("std::vector<SomeType* const *> x;", Spaces
);
17276 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces
);
17277 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
17279 // PAS_Middle should not have any noticeable changes even for SAPQ_Both
17280 Spaces
.PointerAlignment
= FormatStyle::PAS_Middle
;
17281 Spaces
.SpaceAroundPointerQualifiers
= FormatStyle::SAPQ_After
;
17282 verifyFormat("SomeType * volatile * a = NULL;", Spaces
);
17283 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces
);
17284 verifyFormat("std::vector<SomeType * const *> x;", Spaces
);
17285 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces
);
17286 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces
);
17289 TEST_F(FormatTest
, AlignConsecutiveMacros
) {
17290 FormatStyle Style
= getLLVMStyle();
17291 Style
.AlignConsecutiveAssignments
.Enabled
= true;
17292 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
17294 verifyFormat("#define a 3\n"
17299 verifyFormat("#define f(x) (x * x)\n"
17300 "#define fff(x, y, z) (x * y + z)\n"
17301 "#define ffff(x, y) (x - y)",
17304 verifyFormat("#define foo(x, y) (x + y)\n"
17305 "#define bar (5, 6)(2 + 2)",
17308 verifyFormat("#define a 3\n"
17310 "#define ccc (5)\n"
17311 "#define f(x) (x * x)\n"
17312 "#define fff(x, y, z) (x * y + z)\n"
17313 "#define ffff(x, y) (x - y)",
17316 Style
.AlignConsecutiveMacros
.Enabled
= true;
17317 verifyFormat("#define a 3\n"
17322 verifyFormat("#define true 1\n"
17326 verifyFormat("#define f(x) (x * x)\n"
17327 "#define fff(x, y, z) (x * y + z)\n"
17328 "#define ffff(x, y) (x - y)",
17331 verifyFormat("#define foo(x, y) (x + y)\n"
17332 "#define bar (5, 6)(2 + 2)",
17335 verifyFormat("#define a 3\n"
17337 "#define ccc (5)\n"
17338 "#define f(x) (x * x)\n"
17339 "#define fff(x, y, z) (x * y + z)\n"
17340 "#define ffff(x, y) (x - y)",
17343 verifyFormat("#define a 5\n"
17344 "#define foo(x, y) (x + y)\n"
17345 "#define CCC (6)\n"
17346 "auto lambda = []() {\n"
17360 Style
.AlignConsecutiveMacros
.Enabled
= false;
17361 Style
.ColumnLimit
= 20;
17363 verifyFormat("#define a \\\n"
17364 " \"aabbbbbbbbbbbb\"\n"
17366 " \"aabbbbbbbbbbbb\" \\\n"
17367 " \"ccddeeeeeeeee\"\n"
17369 " \"QQQQQQQQQQQQQ\" \\\n"
17370 " \"FFFFFFFFFFFFF\" \\\n"
17374 Style
.AlignConsecutiveMacros
.Enabled
= true;
17375 verifyFormat("#define a \\\n"
17376 " \"aabbbbbbbbbbbb\"\n"
17378 " \"aabbbbbbbbbbbb\" \\\n"
17379 " \"ccddeeeeeeeee\"\n"
17381 " \"QQQQQQQQQQQQQ\" \\\n"
17382 " \"FFFFFFFFFFFFF\" \\\n"
17386 // Test across comments
17387 Style
.MaxEmptyLinesToKeep
= 10;
17388 Style
.ReflowComments
= false;
17389 Style
.AlignConsecutiveMacros
.AcrossComments
= true;
17390 verifyFormat("#define a 3\n"
17391 "// line comment\n"
17395 "// line comment\n"
17400 verifyFormat("#define a 3\n"
17401 "/* block comment */\n"
17405 "/* block comment */\n"
17410 verifyFormat("#define a 3\n"
17411 "/* multi-line *\n"
17412 " * block comment */\n"
17416 "/* multi-line *\n"
17417 " * block comment */\n"
17422 verifyFormat("#define a 3\n"
17423 "// multi-line line comment\n"
17428 "// multi-line line comment\n"
17434 verifyFormat("#define a 3\n"
17435 "// empty lines still break.\n"
17440 "// empty lines still break.\n"
17446 // Test across empty lines
17447 Style
.AlignConsecutiveMacros
.AcrossComments
= false;
17448 Style
.AlignConsecutiveMacros
.AcrossEmptyLines
= true;
17449 verifyFormat("#define a 3\n"
17459 verifyFormat("#define a 3\n"
17473 verifyFormat("#define a 3\n"
17474 "// comments should break alignment\n"
17479 "// comments should break alignment\n"
17485 // Test across empty lines and comments
17486 Style
.AlignConsecutiveMacros
.AcrossComments
= true;
17487 verifyFormat("#define a 3\n"
17489 "// line comment\n"
17494 verifyFormat("#define a 3\n"
17497 "/* multi-line *\n"
17498 " * block comment */\n"
17506 "/* multi-line *\n"
17507 " * block comment */\n"
17514 verifyFormat("#define a 3\n"
17517 "/* multi-line *\n"
17518 " * block comment */\n"
17526 "/* multi-line *\n"
17527 " * block comment */\n"
17535 TEST_F(FormatTest
, AlignConsecutiveAssignmentsAcrossEmptyLines
) {
17536 FormatStyle Alignment
= getLLVMStyle();
17537 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
17538 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
17539 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= true;
17541 Alignment
.MaxEmptyLinesToKeep
= 10;
17542 /* Test alignment across empty lines */
17543 verifyFormat("int a = 5;\n"
17545 "int oneTwoThree = 123;",
17548 "int oneTwoThree= 123;",
17550 verifyFormat("int a = 5;\n"
17553 "int oneTwoThree = 123;",
17557 "int oneTwoThree = 123;",
17559 verifyFormat("int a = 5;\n"
17562 "int oneTwoThree = 123;\n"
17563 "int oneTwo = 12;",
17567 "int oneTwoThree = 123;\n"
17568 "int oneTwo = 12;",
17571 /* Test across comments */
17572 verifyFormat("int a = 5;\n"
17573 "/* block comment */\n"
17574 "int oneTwoThree = 123;",
17576 "/* block comment */\n"
17577 "int oneTwoThree=123;",
17580 verifyFormat("int a = 5;\n"
17581 "// line comment\n"
17582 "int oneTwoThree = 123;",
17584 "// line comment\n"
17585 "int oneTwoThree=123;",
17588 /* Test across comments and newlines */
17589 verifyFormat("int a = 5;\n"
17591 "/* block comment */\n"
17592 "int oneTwoThree = 123;",
17595 "/* block comment */\n"
17596 "int oneTwoThree=123;",
17599 verifyFormat("int a = 5;\n"
17601 "// line comment\n"
17602 "int oneTwoThree = 123;",
17605 "// line comment\n"
17606 "int oneTwoThree=123;",
17610 TEST_F(FormatTest
, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments
) {
17611 FormatStyle Alignment
= getLLVMStyle();
17612 Alignment
.AlignConsecutiveDeclarations
.Enabled
= true;
17613 Alignment
.AlignConsecutiveDeclarations
.AcrossEmptyLines
= true;
17614 Alignment
.AlignConsecutiveDeclarations
.AcrossComments
= true;
17616 Alignment
.MaxEmptyLinesToKeep
= 10;
17617 /* Test alignment across empty lines */
17618 verifyFormat("int a = 5;\n"
17620 "float const oneTwoThree = 123;",
17623 "float const oneTwoThree = 123;",
17625 verifyFormat("int a = 5;\n"
17626 "float const one = 1;\n"
17628 "int oneTwoThree = 123;",
17630 "float const one = 1;\n"
17632 "int oneTwoThree = 123;",
17635 /* Test across comments */
17636 verifyFormat("float const a = 5;\n"
17637 "/* block comment */\n"
17638 "int oneTwoThree = 123;",
17639 "float const a = 5;\n"
17640 "/* block comment */\n"
17641 "int oneTwoThree=123;",
17644 verifyFormat("float const a = 5;\n"
17645 "// line comment\n"
17646 "int oneTwoThree = 123;",
17647 "float const a = 5;\n"
17648 "// line comment\n"
17649 "int oneTwoThree=123;",
17652 /* Test across comments and newlines */
17653 verifyFormat("float const a = 5;\n"
17655 "/* block comment */\n"
17656 "int oneTwoThree = 123;",
17657 "float const a = 5;\n"
17659 "/* block comment */\n"
17660 "int oneTwoThree=123;",
17663 verifyFormat("float const a = 5;\n"
17665 "// line comment\n"
17666 "int oneTwoThree = 123;",
17667 "float const a = 5;\n"
17669 "// line comment\n"
17670 "int oneTwoThree=123;",
17674 TEST_F(FormatTest
, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments
) {
17675 FormatStyle Alignment
= getLLVMStyle();
17676 Alignment
.AlignConsecutiveBitFields
.Enabled
= true;
17677 Alignment
.AlignConsecutiveBitFields
.AcrossEmptyLines
= true;
17678 Alignment
.AlignConsecutiveBitFields
.AcrossComments
= true;
17680 Alignment
.MaxEmptyLinesToKeep
= 10;
17681 /* Test alignment across empty lines */
17682 verifyFormat("int a : 5;\n"
17684 "int longbitfield : 6;",
17687 "int longbitfield : 6;",
17689 verifyFormat("int a : 5;\n"
17692 "int longbitfield : 6;",
17696 "int longbitfield : 6;",
17699 /* Test across comments */
17700 verifyFormat("int a : 5;\n"
17701 "/* block comment */\n"
17702 "int longbitfield : 6;",
17704 "/* block comment */\n"
17705 "int longbitfield : 6;",
17707 verifyFormat("int a : 5;\n"
17709 "// line comment\n"
17710 "int longbitfield : 6;",
17713 "// line comment\n"
17714 "int longbitfield : 6;",
17717 /* Test across comments and newlines */
17718 verifyFormat("int a : 5;\n"
17719 "/* block comment */\n"
17721 "int longbitfield : 6;",
17723 "/* block comment */\n"
17725 "int longbitfield : 6;",
17727 verifyFormat("int a : 5;\n"
17730 "// line comment\n"
17732 "int longbitfield : 6;",
17736 "// line comment \n"
17738 "int longbitfield : 6;",
17742 TEST_F(FormatTest
, AlignConsecutiveAssignmentsAcrossComments
) {
17743 FormatStyle Alignment
= getLLVMStyle();
17744 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
17745 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
17746 Alignment
.AlignConsecutiveAssignments
.AcrossComments
= true;
17748 Alignment
.MaxEmptyLinesToKeep
= 10;
17749 /* Test alignment across empty lines */
17750 verifyFormat("int a = 5;\n"
17752 "int oneTwoThree = 123;",
17755 "int oneTwoThree= 123;",
17757 verifyFormat("int a = 5;\n"
17760 "int oneTwoThree = 123;",
17764 "int oneTwoThree = 123;",
17767 /* Test across comments */
17768 verifyFormat("int a = 5;\n"
17769 "/* block comment */\n"
17770 "int oneTwoThree = 123;",
17772 "/* block comment */\n"
17773 "int oneTwoThree=123;",
17776 verifyFormat("int a = 5;\n"
17777 "// line comment\n"
17778 "int oneTwoThree = 123;",
17780 "// line comment\n"
17781 "int oneTwoThree=123;",
17784 verifyFormat("int a = 5;\n"
17786 " * multi-line block comment\n"
17788 "int oneTwoThree = 123;",
17791 " * multi-line block comment\n"
17793 "int oneTwoThree=123;",
17796 verifyFormat("int a = 5;\n"
17798 "// multi-line line comment\n"
17800 "int oneTwoThree = 123;",
17803 "// multi-line line comment\n"
17805 "int oneTwoThree=123;",
17808 /* Test across comments and newlines */
17809 verifyFormat("int a = 5;\n"
17811 "/* block comment */\n"
17812 "int oneTwoThree = 123;",
17815 "/* block comment */\n"
17816 "int oneTwoThree=123;",
17819 verifyFormat("int a = 5;\n"
17821 "// line comment\n"
17822 "int oneTwoThree = 123;",
17825 "// line comment\n"
17826 "int oneTwoThree=123;",
17830 TEST_F(FormatTest
, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments
) {
17831 FormatStyle Alignment
= getLLVMStyle();
17832 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
17833 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
17834 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= true;
17835 Alignment
.AlignConsecutiveAssignments
.AcrossComments
= true;
17836 verifyFormat("int a = 5;\n"
17837 "int oneTwoThree = 123;",
17839 verifyFormat("int a = method();\n"
17840 "int oneTwoThree = 133;",
17842 verifyFormat("a &= 5;\n"
17848 "sfdbddfbdfbb ^= 5;\n"
17850 "int dsvvdvsdvvv = 123;",
17852 verifyFormat("int i = 1, j = 10;\n"
17853 "something = 2000;",
17855 verifyFormat("something = 2000;\n"
17856 "int i = 1, j = 10;",
17858 verifyFormat("something = 2000;\n"
17860 "int i = 1, j = 10;\n"
17864 verifyFormat("int a = 5;\n"
17867 "int oneTwoThree = 123;\n"
17868 "int oneTwo = 12;",
17870 verifyFormat("int oneTwoThree = 123;\n"
17871 "int oneTwo = 12;\n"
17874 verifyFormat("int oneTwoThree = 123; // comment\n"
17875 "int oneTwo = 12; // comment",
17879 /* Uncomment when fixed
17880 verifyFormat("#if A\n"
17882 "int aaaaaaaa = 12;\n"
17889 verifyFormat("enum foo {\n"
17892 " aaaaaaaa = 12;\n"
17902 Alignment
.MaxEmptyLinesToKeep
= 10;
17903 /* Test alignment across empty lines */
17904 verifyFormat("int a = 5;\n"
17906 "int oneTwoThree = 123;",
17909 "int oneTwoThree= 123;",
17911 verifyFormat("int a = 5;\n"
17914 "int oneTwoThree = 123;",
17918 "int oneTwoThree = 123;",
17920 verifyFormat("int a = 5;\n"
17923 "int oneTwoThree = 123;\n"
17924 "int oneTwo = 12;",
17928 "int oneTwoThree = 123;\n"
17929 "int oneTwo = 12;",
17932 /* Test across comments */
17933 verifyFormat("int a = 5;\n"
17934 "/* block comment */\n"
17935 "int oneTwoThree = 123;",
17937 "/* block comment */\n"
17938 "int oneTwoThree=123;",
17941 verifyFormat("int a = 5;\n"
17942 "// line comment\n"
17943 "int oneTwoThree = 123;",
17945 "// line comment\n"
17946 "int oneTwoThree=123;",
17949 /* Test across comments and newlines */
17950 verifyFormat("int a = 5;\n"
17952 "/* block comment */\n"
17953 "int oneTwoThree = 123;",
17956 "/* block comment */\n"
17957 "int oneTwoThree=123;",
17960 verifyFormat("int a = 5;\n"
17962 "// line comment\n"
17963 "int oneTwoThree = 123;",
17966 "// line comment\n"
17967 "int oneTwoThree=123;",
17970 verifyFormat("int a = 5;\n"
17972 "// multi-line line comment\n"
17974 "int oneTwoThree = 123;",
17977 "// multi-line line comment\n"
17979 "int oneTwoThree=123;",
17982 verifyFormat("int a = 5;\n"
17984 " * multi-line block comment\n"
17986 "int oneTwoThree = 123;",
17989 " * multi-line block comment\n"
17991 "int oneTwoThree=123;",
17994 verifyFormat("int a = 5;\n"
17996 "/* block comment */\n"
18000 "int oneTwoThree = 123;",
18003 "/* block comment */\n"
18007 "int oneTwoThree=123;",
18010 verifyFormat("int a = 5;\n"
18012 "// line comment\n"
18016 "int oneTwoThree = 123;",
18019 "// line comment\n"
18023 "int oneTwoThree=123;",
18026 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
18027 verifyFormat("#define A \\\n"
18028 " int aaaa = 12; \\\n"
18029 " int b = 23; \\\n"
18030 " int ccc = 234; \\\n"
18031 " int dddddddddd = 2345;",
18033 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
18034 verifyFormat("#define A \\\n"
18035 " int aaaa = 12; \\\n"
18036 " int b = 23; \\\n"
18037 " int ccc = 234; \\\n"
18038 " int dddddddddd = 2345;",
18040 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Right
;
18041 verifyFormat("#define A "
18049 " int dddddddddd = 2345;",
18051 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18052 "k = 4, int l = 5,\n"
18055 " otherThing = 1;\n"
18058 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18061 " int big = 10000;\n"
18064 verifyFormat("class C {\n"
18067 " virtual void f() = 0;\n"
18070 verifyFormat("int i = 1;\n"
18071 "if (SomeType t = getSomething()) {\n"
18074 "int big = 10000;",
18076 verifyFormat("int j = 7;\n"
18077 "for (int k = 0; k < N; ++k) {\n"
18080 "int big = 10000;\n"
18083 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
18084 verifyFormat("int i = 1;\n"
18085 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18086 " = someLooooooooooooooooongFunction();\n"
18089 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
18090 verifyFormat("int i = 1;\n"
18091 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18092 " someLooooooooooooooooongFunction();\n"
18096 verifyFormat("auto lambda = []() {\n"
18110 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18111 " loooooooooooooooooooooongParameterB);\n"
18115 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18116 " typename B = very_long_type_name_1,\n"
18117 " typename T_2 = very_long_type_name_2>\n"
18120 verifyFormat("int a, b = 1;\n"
18124 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18125 "float b[1][] = {{3.f}};",
18127 verifyFormat("for (int i = 0; i < 1; i++)\n"
18130 verifyFormat("for (i = 0; i < 1; i++)\n"
18135 Alignment
.ReflowComments
= true;
18136 Alignment
.ColumnLimit
= 50;
18137 verifyFormat("int x = 0;\n"
18138 "int yy = 1; /// specificlennospace\n"
18141 "int yy = 1; ///specificlennospace\n"
18146 TEST_F(FormatTest
, AlignCompoundAssignments
) {
18147 FormatStyle Alignment
= getLLVMStyle();
18148 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18149 Alignment
.AlignConsecutiveAssignments
.AlignCompound
= true;
18150 Alignment
.AlignConsecutiveAssignments
.PadOperators
= false;
18151 verifyFormat("sfdbddfbdfbb = 5;\n"
18153 "int dsvvdvsdvvv = 123;",
18155 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18157 "int dsvvdvsdvvv = 123;",
18159 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18161 "int dsvvdvsdvvv = 123;",
18163 verifyFormat("int xxx = 5;\n"
18170 verifyFormat("int xxx = 5;\n"
18177 // Test that `<=` is not treated as a compound assignment.
18178 verifyFormat("aa &= 5;\n"
18182 Alignment
.AlignConsecutiveAssignments
.PadOperators
= true;
18183 verifyFormat("sfdbddfbdfbb = 5;\n"
18185 "int dsvvdvsdvvv = 123;",
18187 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18189 "int dsvvdvsdvvv = 123;",
18191 verifyFormat("sfdbddfbdfbb ^= 5;\n"
18193 "int dsvvdvsdvvv = 123;",
18195 verifyFormat("a += 5;\n"
18198 "oneTwoThree = 123;",
18202 "oneTwoThree = 123;",
18204 verifyFormat("a += 5;\n"
18207 "oneTwoThree = 123;",
18211 "oneTwoThree = 123;",
18213 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= true;
18214 verifyFormat("a += 5;\n"
18217 "oneTwoThree = 123;",
18221 "oneTwoThree = 123;",
18223 verifyFormat("a += 5;\n"
18226 "oneTwoThree = 123;",
18230 "oneTwoThree = 123;",
18232 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= false;
18233 Alignment
.AlignConsecutiveAssignments
.AcrossComments
= true;
18234 verifyFormat("a += 5;\n"
18237 "oneTwoThree = 123;",
18241 "oneTwoThree = 123;",
18243 verifyFormat("a += 5;\n"
18246 "oneTwoThree = 123;",
18250 "oneTwoThree = 123;",
18252 Alignment
.AlignConsecutiveAssignments
.AcrossEmptyLines
= true;
18253 verifyFormat("a += 5;\n"
18256 "oneTwoThree = 123;",
18260 "oneTwoThree = 123;",
18262 verifyFormat("a += 5;\n"
18265 "oneTwoThree <<= 123;",
18269 "oneTwoThree <<= 123;",
18273 TEST_F(FormatTest
, AlignConsecutiveAssignments
) {
18274 FormatStyle Alignment
= getLLVMStyle();
18275 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
18276 verifyFormat("int a = 5;\n"
18277 "int oneTwoThree = 123;",
18279 verifyFormat("int a = 5;\n"
18280 "int oneTwoThree = 123;",
18283 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18284 verifyFormat("int a = 5;\n"
18285 "int oneTwoThree = 123;",
18287 verifyFormat("int a = method();\n"
18288 "int oneTwoThree = 133;",
18290 verifyFormat("aa <= 5;\n"
18297 "sfdbddfbdfbb ^= 5;\n"
18299 "int dsvvdvsdvvv = 123;",
18301 verifyFormat("int i = 1, j = 10;\n"
18302 "something = 2000;",
18304 verifyFormat("something = 2000;\n"
18305 "int i = 1, j = 10;",
18307 verifyFormat("something = 2000;\n"
18309 "int i = 1, j = 10;\n"
18313 verifyFormat("int a = 5;\n"
18316 "int oneTwoThree = 123;\n"
18317 "int oneTwo = 12;",
18319 verifyFormat("int oneTwoThree = 123;\n"
18320 "int oneTwo = 12;\n"
18323 verifyFormat("int oneTwoThree = 123; // comment\n"
18324 "int oneTwo = 12; // comment",
18326 verifyFormat("int f() = default;\n"
18327 "int &operator() = default;\n"
18328 "int &operator=() {",
18330 verifyFormat("int f() = delete;\n"
18331 "int &operator() = delete;\n"
18332 "int &operator=() {",
18334 verifyFormat("int f() = default; // comment\n"
18335 "int &operator() = default; // comment\n"
18336 "int &operator=() {",
18338 verifyFormat("int f() = default;\n"
18339 "int &operator() = default;\n"
18340 "int &operator==() {",
18342 verifyFormat("int f() = default;\n"
18343 "int &operator() = default;\n"
18344 "int &operator<=() {",
18346 verifyFormat("int f() = default;\n"
18347 "int &operator() = default;\n"
18348 "int &operator!=() {",
18350 verifyFormat("int f() = default;\n"
18351 "int &operator() = default;\n"
18352 "int &operator=();",
18354 verifyFormat("int f() = delete;\n"
18355 "int &operator() = delete;\n"
18356 "int &operator=();",
18358 verifyFormat("/* long long padding */ int f() = default;\n"
18359 "int &operator() = default;\n"
18360 "int &operator/**/ =();",
18362 // https://llvm.org/PR33697
18363 FormatStyle AlignmentWithPenalty
= getLLVMStyle();
18364 AlignmentWithPenalty
.AlignConsecutiveAssignments
.Enabled
= true;
18365 AlignmentWithPenalty
.PenaltyReturnTypeOnItsOwnLine
= 5000;
18366 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n"
18367 " void f() = delete;\n"
18368 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n"
18369 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n"
18371 AlignmentWithPenalty
);
18374 /* Uncomment when fixed
18375 verifyFormat("#if A\n"
18377 "int aaaaaaaa = 12;\n"
18384 verifyFormat("enum foo {\n"
18387 " aaaaaaaa = 12;\n"
18397 verifyFormat("int a = 5;\n"
18399 "int oneTwoThree = 123;",
18402 "int oneTwoThree= 123;",
18404 verifyFormat("int a = 5;\n"
18407 "int oneTwoThree = 123;",
18411 "int oneTwoThree = 123;",
18413 verifyFormat("int a = 5;\n"
18416 "int oneTwoThree = 123;\n"
18417 "int oneTwo = 12;",
18421 "int oneTwoThree = 123;\n"
18422 "int oneTwo = 12;",
18424 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
18425 verifyFormat("#define A \\\n"
18426 " int aaaa = 12; \\\n"
18427 " int b = 23; \\\n"
18428 " int ccc = 234; \\\n"
18429 " int dddddddddd = 2345;",
18431 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
18432 verifyFormat("#define A \\\n"
18433 " int aaaa = 12; \\\n"
18434 " int b = 23; \\\n"
18435 " int ccc = 234; \\\n"
18436 " int dddddddddd = 2345;",
18438 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Right
;
18439 verifyFormat("#define A "
18447 " int dddddddddd = 2345;",
18449 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
18450 "k = 4, int l = 5,\n"
18453 " otherThing = 1;\n"
18456 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18459 " int big = 10000;\n"
18462 verifyFormat("class C {\n"
18465 " virtual void f() = 0;\n"
18468 verifyFormat("int i = 1;\n"
18469 "if (SomeType t = getSomething()) {\n"
18472 "int big = 10000;",
18474 verifyFormat("int j = 7;\n"
18475 "for (int k = 0; k < N; ++k) {\n"
18478 "int big = 10000;\n"
18481 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
18482 verifyFormat("int i = 1;\n"
18483 "LooooooooooongType loooooooooooooooooooooongVariable\n"
18484 " = someLooooooooooooooooongFunction();\n"
18487 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
18488 verifyFormat("int i = 1;\n"
18489 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
18490 " someLooooooooooooooooongFunction();\n"
18494 verifyFormat("auto lambda = []() {\n"
18508 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
18509 " loooooooooooooooooooooongParameterB);\n"
18513 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
18514 " typename B = very_long_type_name_1,\n"
18515 " typename T_2 = very_long_type_name_2>\n"
18518 verifyFormat("int a, b = 1;\n"
18522 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
18523 "float b[1][] = {{3.f}};",
18525 verifyFormat("for (int i = 0; i < 1; i++)\n"
18528 verifyFormat("for (i = 0; i < 1; i++)\n"
18533 EXPECT_EQ(Alignment
.ReflowComments
, true);
18534 Alignment
.ColumnLimit
= 50;
18535 verifyFormat("int x = 0;\n"
18536 "int yy = 1; /// specificlennospace\n"
18539 "int yy = 1; ///specificlennospace\n"
18543 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18549 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18550 "auto b = g([] {\n"
18555 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18556 "auto b = g(param, [] {\n"
18561 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n"
18563 " if (condition) {\n"
18569 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
18570 " ccc ? aaaaa : bbbbb,\n"
18571 " dddddddddddddddddddddddddd);",
18573 // FIXME: https://llvm.org/PR53497
18574 // verifyFormat("auto aaaaaaaaaaaa = f();\n"
18575 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
18576 // " ccc ? aaaaa : bbbbb,\n"
18577 // " dddddddddddddddddddddddddd);",
18580 // Confirm proper handling of AlignConsecutiveAssignments with
18581 // BinPackArguments.
18582 // See https://llvm.org/PR55360
18583 Alignment
= getLLVMStyleWithColumns(50);
18584 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18585 Alignment
.BinPackArguments
= false;
18586 verifyFormat("int a_long_name = 1;\n"
18587 "auto b = B({a_long_name, a_long_name},\n"
18588 " {a_longer_name_for_wrap,\n"
18589 " a_longer_name_for_wrap});",
18591 verifyFormat("int a_long_name = 1;\n"
18592 "auto b = B{{a_long_name, a_long_name},\n"
18593 " {a_longer_name_for_wrap,\n"
18594 " a_longer_name_for_wrap}};",
18597 Alignment
= getLLVMStyleWithColumns(60);
18598 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18599 verifyFormat("using II = typename TI<T, std::tuple<Types...>>::I;\n"
18600 "using I = std::conditional_t<II::value >= 0,\n"
18601 " std::ic<int, II::value + 1>,\n"
18602 " std::ic<int, -1>>;",
18604 verifyFormat("SomeName = Foo;\n"
18605 "X = func<Type, Type>(looooooooooooooooooooooooong,\n"
18610 TEST_F(FormatTest
, AlignConsecutiveBitFields
) {
18611 FormatStyle Alignment
= getLLVMStyle();
18612 Alignment
.AlignConsecutiveBitFields
.Enabled
= true;
18613 verifyFormat("int const a : 5;\n"
18614 "int oneTwoThree : 23;",
18617 // Initializers are allowed starting with c++2a
18618 verifyFormat("int const a : 5 = 1;\n"
18619 "int oneTwoThree : 23 = 0;",
18622 Alignment
.AlignConsecutiveDeclarations
.Enabled
= true;
18623 verifyFormat("int const a : 5;\n"
18624 "int oneTwoThree : 23;",
18627 verifyFormat("int const a : 5; // comment\n"
18628 "int oneTwoThree : 23; // comment",
18631 verifyFormat("int const a : 5 = 1;\n"
18632 "int oneTwoThree : 23 = 0;",
18635 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18636 verifyFormat("int const a : 5 = 1;\n"
18637 "int oneTwoThree : 23 = 0;",
18639 verifyFormat("int const a : 5 = {1};\n"
18640 "int oneTwoThree : 23 = 0;",
18643 Alignment
.BitFieldColonSpacing
= FormatStyle::BFCS_None
;
18644 verifyFormat("int const a :5;\n"
18645 "int oneTwoThree:23;",
18648 Alignment
.BitFieldColonSpacing
= FormatStyle::BFCS_Before
;
18649 verifyFormat("int const a :5;\n"
18650 "int oneTwoThree :23;",
18653 Alignment
.BitFieldColonSpacing
= FormatStyle::BFCS_After
;
18654 verifyFormat("int const a : 5;\n"
18655 "int oneTwoThree: 23;",
18658 // Known limitations: ':' is only recognized as a bitfield colon when
18659 // followed by a number.
18661 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n"
18667 TEST_F(FormatTest
, AlignConsecutiveDeclarations
) {
18668 FormatStyle Alignment
= getLLVMStyle();
18669 Alignment
.AlignConsecutiveMacros
.Enabled
= true;
18670 Alignment
.PointerAlignment
= FormatStyle::PAS_Right
;
18671 verifyFormat("float const a = 5;\n"
18672 "int oneTwoThree = 123;",
18674 verifyFormat("int a = 5;\n"
18675 "float const oneTwoThree = 123;",
18678 Alignment
.AlignConsecutiveDeclarations
.Enabled
= true;
18679 verifyFormat("float const a = 5;\n"
18680 "int oneTwoThree = 123;",
18682 verifyFormat("int a = method();\n"
18683 "float const oneTwoThree = 133;",
18685 verifyFormat("int i = 1, j = 10;\n"
18686 "something = 2000;",
18688 verifyFormat("something = 2000;\n"
18689 "int i = 1, j = 10;",
18691 verifyFormat("float something = 2000;\n"
18692 "double another = 911;\n"
18693 "int i = 1, j = 10;\n"
18694 "const int *oneMore = 1;\n"
18697 verifyFormat("float a = 5;\n"
18700 "const double oneTwoThree = 123;\n"
18701 "const unsigned int oneTwo = 12;",
18703 verifyFormat("int oneTwoThree{0}; // comment\n"
18704 "unsigned oneTwo; // comment",
18706 verifyFormat("unsigned int *a;\n"
18708 "unsigned int Const *c;\n"
18709 "unsigned int const *d;\n"
18710 "unsigned int Const &e;\n"
18711 "unsigned int const &f;",
18713 verifyFormat("Const unsigned int *c;\n"
18714 "const unsigned int *d;\n"
18715 "Const unsigned int &e;\n"
18716 "const unsigned int &f;\n"
18717 "const unsigned g;\n"
18718 "Const unsigned h;",
18720 verifyFormat("float const a = 5;\n"
18722 "int oneTwoThree = 123;",
18723 "float const a = 5;\n"
18725 "int oneTwoThree= 123;",
18727 verifyFormat("float a = 5;\n"
18730 "unsigned oneTwoThree = 123;",
18734 "unsigned oneTwoThree = 123;",
18736 verifyFormat("float a = 5;\n"
18739 "unsigned oneTwoThree = 123;\n"
18740 "int oneTwo = 12;",
18744 "unsigned oneTwoThree = 123;\n"
18745 "int oneTwo = 12;",
18747 // Function prototype alignment
18748 verifyFormat("int a();\n"
18751 verifyFormat("int a(int x);\n"
18754 verifyFormat("struct Test {\n"
18755 " Test(const Test &) = default;\n"
18756 " ~Test() = default;\n"
18757 " Test &operator=(const Test &) = default;\n"
18760 unsigned OldColumnLimit
= Alignment
.ColumnLimit
;
18761 // We need to set ColumnLimit to zero, in order to stress nested alignments,
18762 // otherwise the function parameters will be re-flowed onto a single line.
18763 Alignment
.ColumnLimit
= 0;
18764 verifyFormat("int a(int x,\n"
18766 "double b(int x,\n"
18770 "double b(int x,\n"
18773 // This ensures that function parameters of function declarations are
18774 // correctly indented when their owning functions are indented.
18775 // The failure case here is for 'double y' to not be indented enough.
18776 verifyFormat("double a(int x);\n"
18779 "double a(int x);\n"
18783 // Set ColumnLimit low so that we induce wrapping immediately after
18784 // the function name and opening paren.
18785 Alignment
.ColumnLimit
= 13;
18786 verifyFormat("int function(\n"
18790 Alignment
.ColumnLimit
= OldColumnLimit
;
18791 // Ensure function pointers don't screw up recursive alignment
18792 verifyFormat("int a(int x, void (*fp)(int y));\n"
18795 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
18796 verifyFormat("struct Test {\n"
18797 " Test(const Test &) = default;\n"
18798 " ~Test() = default;\n"
18799 " Test &operator=(const Test &) = default;\n"
18802 // Ensure recursive alignment is broken by function braces, so that the
18803 // "a = 1" does not align with subsequent assignments inside the function
18805 verifyFormat("int func(int a = 1) {\n"
18810 verifyFormat("float something = 2000;\n"
18811 "double another = 911;\n"
18812 "int i = 1, j = 10;\n"
18813 "const int *oneMore = 1;\n"
18816 verifyFormat("int oneTwoThree = {0}; // comment\n"
18817 "unsigned oneTwo = 0; // comment",
18819 // Make sure that scope is correctly tracked, in the absence of braces
18820 verifyFormat("for (int i = 0; i < n; i++)\n"
18824 verifyFormat("if (int i = 0)\n"
18828 // Ensure operator[] and operator() are comprehended
18829 verifyFormat("struct test {\n"
18830 " long long int foo();\n"
18831 " int operator[](int a);\n"
18835 verifyFormat("struct test {\n"
18836 " long long int foo();\n"
18837 " int operator()(int a);\n"
18841 // http://llvm.org/PR52914
18842 verifyFormat("char *a[] = {\"a\", // comment\n"
18844 "int bbbbbbb = 0;",
18848 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18849 " int const i = 1;\n"
18851 " int big = 10000;\n"
18853 " unsigned oneTwoThree = 123;\n"
18854 " int oneTwo = 12;\n"
18857 " int ll = 10000;\n"
18859 "void SomeFunction(int parameter= 0) {\n"
18860 " int const i= 1;\n"
18862 " int big = 10000;\n"
18864 "unsigned oneTwoThree =123;\n"
18865 "int oneTwo = 12;\n"
18871 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18872 " int const i = 1;\n"
18873 " int **j = 2, ***k;\n"
18875 " int &&l = i + j;\n"
18876 " int big = 10000;\n"
18878 " unsigned oneTwoThree = 123;\n"
18879 " int oneTwo = 12;\n"
18882 " int ll = 10000;\n"
18884 "void SomeFunction(int parameter= 0) {\n"
18885 " int const i= 1;\n"
18886 " int **j=2,***k;\n"
18889 " int big = 10000;\n"
18891 "unsigned oneTwoThree =123;\n"
18892 "int oneTwo = 12;\n"
18898 // variables are aligned at their name, pointers are at the right most
18900 verifyFormat("int *a;\n"
18907 FormatStyle AlignmentLeft
= Alignment
;
18908 AlignmentLeft
.PointerAlignment
= FormatStyle::PAS_Left
;
18909 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18910 " int const i = 1;\n"
18912 " int big = 10000;\n"
18914 " unsigned oneTwoThree = 123;\n"
18915 " int oneTwo = 12;\n"
18918 " int ll = 10000;\n"
18920 "void SomeFunction(int parameter= 0) {\n"
18921 " int const i= 1;\n"
18923 " int big = 10000;\n"
18925 "unsigned oneTwoThree =123;\n"
18926 "int oneTwo = 12;\n"
18932 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18933 " int const i = 1;\n"
18936 " int&& l = i + j;\n"
18937 " int big = 10000;\n"
18939 " unsigned oneTwoThree = 123;\n"
18940 " int oneTwo = 12;\n"
18943 " int ll = 10000;\n"
18945 "void SomeFunction(int parameter= 0) {\n"
18946 " int const i= 1;\n"
18950 " int big = 10000;\n"
18952 "unsigned oneTwoThree =123;\n"
18953 "int oneTwo = 12;\n"
18959 // variables are aligned at their name, pointers are at the left most position
18960 verifyFormat("int* a;\n"
18967 FormatStyle AlignmentMiddle
= Alignment
;
18968 AlignmentMiddle
.PointerAlignment
= FormatStyle::PAS_Middle
;
18969 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18970 " int const i = 1;\n"
18972 " int big = 10000;\n"
18974 " unsigned oneTwoThree = 123;\n"
18975 " int oneTwo = 12;\n"
18978 " int ll = 10000;\n"
18980 "void SomeFunction(int parameter= 0) {\n"
18981 " int const i= 1;\n"
18983 " int big = 10000;\n"
18985 "unsigned oneTwoThree =123;\n"
18986 "int oneTwo = 12;\n"
18992 verifyFormat("void SomeFunction(int parameter = 0) {\n"
18993 " int const i = 1;\n"
18994 " int ** j = 2, ***k;\n"
18996 " int && l = i + j;\n"
18997 " int big = 10000;\n"
18999 " unsigned oneTwoThree = 123;\n"
19000 " int oneTwo = 12;\n"
19003 " int ll = 10000;\n"
19005 "void SomeFunction(int parameter= 0) {\n"
19006 " int const i= 1;\n"
19007 " int **j=2,***k;\n"
19010 " int big = 10000;\n"
19012 "unsigned oneTwoThree =123;\n"
19013 "int oneTwo = 12;\n"
19019 // variables are aligned at their name, pointers are in the middle
19020 verifyFormat("int * a;\n"
19026 Alignment
.AlignConsecutiveAssignments
.Enabled
= false;
19027 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_DontAlign
;
19028 verifyFormat("#define A \\\n"
19029 " int aaaa = 12; \\\n"
19030 " float b = 23; \\\n"
19031 " const int ccc = 234; \\\n"
19032 " unsigned dddddddddd = 2345;",
19034 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
19035 verifyFormat("#define A \\\n"
19036 " int aaaa = 12; \\\n"
19037 " float b = 23; \\\n"
19038 " const int ccc = 234; \\\n"
19039 " unsigned dddddddddd = 2345;",
19041 Alignment
.AlignEscapedNewlines
= FormatStyle::ENAS_Right
;
19042 Alignment
.ColumnLimit
= 30;
19043 verifyFormat("#define A \\\n"
19044 " int aaaa = 12; \\\n"
19045 " float b = 23; \\\n"
19046 " const int ccc = 234; \\\n"
19047 " int dddddddddd = 2345;",
19049 Alignment
.ColumnLimit
= 80;
19050 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
19051 "k = 4, int l = 5,\n"
19053 " const int j = 10;\n"
19054 " otherThing = 1;\n"
19057 verifyFormat("void SomeFunction(int parameter = 0) {\n"
19058 " int const i = 1;\n"
19060 " int big = 10000;\n"
19063 verifyFormat("class C {\n"
19066 " virtual void f() = 0;\n"
19069 verifyFormat("float i = 1;\n"
19070 "if (SomeType t = getSomething()) {\n"
19072 "const unsigned j = 2;\n"
19073 "int big = 10000;",
19075 verifyFormat("float j = 7;\n"
19076 "for (int k = 0; k < N; ++k) {\n"
19078 "unsigned j = 2;\n"
19079 "int big = 10000;\n"
19082 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
19083 verifyFormat("float i = 1;\n"
19084 "LooooooooooongType loooooooooooooooooooooongVariable\n"
19085 " = someLooooooooooooooooongFunction();\n"
19088 Alignment
.BreakBeforeBinaryOperators
= FormatStyle::BOS_None
;
19089 verifyFormat("int i = 1;\n"
19090 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
19091 " someLooooooooooooooooongFunction();\n"
19095 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19096 verifyFormat("auto lambda = []() {\n"
19109 Alignment
.AlignConsecutiveAssignments
.Enabled
= false;
19113 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
19114 " loooooooooooooooooooooongParameterB);\n"
19118 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
19119 // We expect declarations and assignments to align, as long as it doesn't
19120 // exceed the column limit, starting a new alignment sequence whenever it
19122 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19123 Alignment
.ColumnLimit
= 30;
19124 verifyFormat("float ii = 1;\n"
19125 "unsigned j = 2;\n"
19126 "int someVerylongVariable = 1;\n"
19127 "AnotherLongType ll = 123456;\n"
19128 "VeryVeryLongType k = 2;\n"
19131 Alignment
.ColumnLimit
= 80;
19132 Alignment
.AlignConsecutiveAssignments
.Enabled
= false;
19135 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
19136 " typename LongType, typename B>\n"
19139 verifyFormat("float a, b = 1;\n"
19143 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19144 "float b[1][] = {{3.f}};",
19146 Alignment
.AlignConsecutiveAssignments
.Enabled
= true;
19147 verifyFormat("float a, b = 1;\n"
19151 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
19152 "float b[1][] = {{3.f}};",
19154 Alignment
.AlignConsecutiveAssignments
.Enabled
= false;
19156 Alignment
.ColumnLimit
= 30;
19157 Alignment
.BinPackParameters
= false;
19158 verifyFormat("void foo(float a,\n"
19161 " uint32_t *d) {\n"
19166 "void bar(ino_t a,\n"
19171 Alignment
.BinPackParameters
= true;
19172 Alignment
.ColumnLimit
= 80;
19175 Alignment
.PointerAlignment
= FormatStyle::PAS_Middle
;
19177 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
19178 " static const Version verVs2017;\n"
19182 Alignment
.PointerAlignment
= FormatStyle::PAS_Right
;
19184 // See llvm.org/PR35641
19185 Alignment
.AlignConsecutiveDeclarations
.Enabled
= true;
19186 verifyFormat("int func() { //\n"
19193 FormatStyle Style
= getMozillaStyle();
19194 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
19195 verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n"
19197 "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style
);
19199 Alignment
.PointerAlignment
= FormatStyle::PAS_Left
;
19200 verifyFormat("unsigned int* a;\n"
19202 "unsigned int Const* c;\n"
19203 "unsigned int const* d;\n"
19204 "unsigned int Const& e;\n"
19205 "unsigned int const& f;",
19207 verifyFormat("Const unsigned int* c;\n"
19208 "const unsigned int* d;\n"
19209 "Const unsigned int& e;\n"
19210 "const unsigned int& f;\n"
19211 "const unsigned g;\n"
19212 "Const unsigned h;",
19215 Alignment
.PointerAlignment
= FormatStyle::PAS_Middle
;
19216 verifyFormat("unsigned int * a;\n"
19218 "unsigned int Const * c;\n"
19219 "unsigned int const * d;\n"
19220 "unsigned int Const & e;\n"
19221 "unsigned int const & f;",
19223 verifyFormat("Const unsigned int * c;\n"
19224 "const unsigned int * d;\n"
19225 "Const unsigned int & e;\n"
19226 "const unsigned int & f;\n"
19227 "const unsigned g;\n"
19228 "Const unsigned h;",
19232 FormatStyle BracedAlign
= getLLVMStyle();
19233 BracedAlign
.AlignConsecutiveDeclarations
.Enabled
= true;
19234 verifyFormat("const auto result{[]() {\n"
19235 " const auto something = 1;\n"
19239 verifyFormat("int foo{[]() {\n"
19244 BracedAlign
.Cpp11BracedListStyle
= false;
19245 verifyFormat("const auto result{ []() {\n"
19246 " const auto something = 1;\n"
19250 verifyFormat("int foo{ []() {\n"
19257 TEST_F(FormatTest
, AlignConsecutiveShortCaseStatements
) {
19258 FormatStyle Alignment
= getLLVMStyle();
19259 Alignment
.AllowShortCaseLabelsOnASingleLine
= true;
19260 Alignment
.AlignConsecutiveShortCaseStatements
.Enabled
= true;
19262 verifyFormat("switch (level) {\n"
19263 "case log::info: return \"info\";\n"
19264 "case log::warning: return \"warning\";\n"
19265 "default: return \"default\";\n"
19269 verifyFormat("switch (level) {\n"
19270 "case log::info: return \"info\";\n"
19271 "case log::warning: return \"warning\";\n"
19273 "switch (level) {\n"
19274 "case log::info: return \"info\";\n"
19275 "case log::warning:\n"
19276 " return \"warning\";\n"
19280 // Empty case statements push out the alignment, but non-short case labels
19282 verifyFormat("switch (level) {\n"
19283 "case log::info: return \"info\";\n"
19284 "case log::critical:\n"
19285 "case log::warning:\n"
19286 "case log::severe: return \"severe\";\n"
19287 "case log::extra_severe:\n"
19289 " return \"extra_severe\";\n"
19293 // Verify comments and empty lines break the alignment.
19294 verifyNoChange("switch (level) {\n"
19295 "case log::info: return \"info\";\n"
19296 "case log::warning: return \"warning\";\n"
19298 "case log::critical: return \"critical\";\n"
19299 "default: return \"default\";\n"
19301 "case log::severe: return \"severe\";\n"
19305 // Empty case statements don't break the alignment, and potentially push it
19307 verifyFormat("switch (level) {\n"
19308 "case log::info: return \"info\";\n"
19309 "case log::warning:\n"
19310 "case log::critical:\n"
19311 "default: return \"default\";\n"
19315 // Implicit fallthrough cases can be aligned with either a comment or
19317 verifyFormat("switch (level) {\n"
19318 "case log::info: return \"info\";\n"
19319 "case log::warning: // fallthrough\n"
19320 "case log::error: return \"error\";\n"
19321 "case log::critical: /*fallthrough*/\n"
19322 "case log::severe: return \"severe\";\n"
19323 "case log::diag: [[fallthrough]];\n"
19324 "default: return \"default\";\n"
19328 // Verify trailing comment that needs a reflow also gets aligned properly.
19329 verifyFormat("switch (level) {\n"
19330 "case log::info: return \"info\";\n"
19331 "case log::warning: // fallthrough\n"
19332 "case log::error: return \"error\";\n"
19334 "switch (level) {\n"
19335 "case log::info: return \"info\";\n"
19336 "case log::warning: //fallthrough\n"
19337 "case log::error: return \"error\";\n"
19341 // Verify adjacent non-short case statements don't change the alignment, and
19342 // properly break the set of consecutive statements.
19343 verifyFormat("switch (level) {\n"
19344 "case log::critical:\n"
19346 " return \"critical\";\n"
19347 "case log::info: return \"info\";\n"
19348 "case log::warning: return \"warning\";\n"
19352 "case log::error: return \"error\";\n"
19353 "case log::severe: return \"severe\";\n"
19354 "case log::extra_critical:\n"
19356 " return \"extra critical\";\n"
19360 Alignment
.SpaceBeforeCaseColon
= true;
19361 verifyFormat("switch (level) {\n"
19362 "case log::info : return \"info\";\n"
19363 "case log::warning : return \"warning\";\n"
19364 "default : return \"default\";\n"
19367 Alignment
.SpaceBeforeCaseColon
= false;
19369 // Make sure we don't incorrectly align correctly across nested switch cases.
19370 verifyFormat("switch (level) {\n"
19371 "case log::info: return \"info\";\n"
19372 "case log::warning: return \"warning\";\n"
19373 "case log::other:\n"
19374 " switch (sublevel) {\n"
19375 " case log::info: return \"info\";\n"
19376 " case log::warning: return \"warning\";\n"
19379 "case log::error: return \"error\";\n"
19380 "default: return \"default\";\n"
19382 "switch (level) {\n"
19383 "case log::info: return \"info\";\n"
19384 "case log::warning: return \"warning\";\n"
19385 "case log::other: switch (sublevel) {\n"
19386 " case log::info: return \"info\";\n"
19387 " case log::warning: return \"warning\";\n"
19390 "case log::error: return \"error\";\n"
19391 "default: return \"default\";\n"
19395 Alignment
.AlignConsecutiveShortCaseStatements
.AcrossEmptyLines
= true;
19397 verifyFormat("switch (level) {\n"
19398 "case log::info: return \"info\";\n"
19400 "case log::warning: return \"warning\";\n"
19402 "switch (level) {\n"
19403 "case log::info: return \"info\";\n"
19405 "case log::warning: return \"warning\";\n"
19409 Alignment
.AlignConsecutiveShortCaseStatements
.AcrossComments
= true;
19411 verifyNoChange("switch (level) {\n"
19412 "case log::info: return \"info\";\n"
19414 "/* block comment */\n"
19416 "// line comment\n"
19417 "case log::warning: return \"warning\";\n"
19421 Alignment
.AlignConsecutiveShortCaseStatements
.AcrossEmptyLines
= false;
19423 verifyFormat("switch (level) {\n"
19424 "case log::info: return \"info\";\n"
19426 "case log::warning: return \"warning\";\n"
19430 Alignment
.AlignConsecutiveShortCaseStatements
.AlignCaseColons
= true;
19432 verifyFormat("switch (level) {\n"
19433 "case log::info : return \"info\";\n"
19434 "case log::warning: return \"warning\";\n"
19435 "default : return \"default\";\n"
19439 // With AlignCaseColons, empty case statements don't break alignment of
19440 // consecutive case statements (and are aligned).
19441 verifyFormat("switch (level) {\n"
19442 "case log::info : return \"info\";\n"
19443 "case log::warning :\n"
19444 "case log::critical:\n"
19445 "default : return \"default\";\n"
19449 // Final non-short case labels shouldn't have their colon aligned
19450 verifyFormat("switch (level) {\n"
19451 "case log::info : return \"info\";\n"
19452 "case log::warning :\n"
19453 "case log::critical:\n"
19454 "case log::severe : return \"severe\";\n"
19457 " return \"default\";\n"
19461 // Verify adjacent non-short case statements break the set of consecutive
19462 // alignments and aren't aligned with adjacent non-short case statements if
19463 // AlignCaseColons is set.
19464 verifyFormat("switch (level) {\n"
19465 "case log::critical:\n"
19467 " return \"critical\";\n"
19468 "case log::info : return \"info\";\n"
19469 "case log::warning: return \"warning\";\n"
19473 "case log::error : return \"error\";\n"
19474 "case log::severe: return \"severe\";\n"
19475 "case log::extra_critical:\n"
19477 " return \"extra critical\";\n"
19481 Alignment
.SpaceBeforeCaseColon
= true;
19482 verifyFormat("switch (level) {\n"
19483 "case log::info : return \"info\";\n"
19484 "case log::warning : return \"warning\";\n"
19485 "case log::error :\n"
19486 "default : return \"default\";\n"
19491 TEST_F(FormatTest
, AlignWithLineBreaks
) {
19492 auto Style
= getLLVMStyleWithColumns(120);
19494 EXPECT_EQ(Style
.AlignConsecutiveAssignments
,
19495 FormatStyle::AlignConsecutiveStyle(
19496 {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
19497 /*AcrossComments=*/false, /*AlignCompound=*/false,
19498 /*PadOperators=*/true}));
19499 EXPECT_EQ(Style
.AlignConsecutiveDeclarations
,
19500 FormatStyle::AlignConsecutiveStyle({}));
19501 verifyFormat("void foo() {\n"
19502 " int myVar = 5;\n"
19503 " double x = 3.14;\n"
19504 " auto str = \"Hello \"\n"
19506 " auto s = \"Hello \"\n"
19511 // clang-format off
19512 verifyFormat("void foo() {\n"
19513 " const int capacityBefore = Entries.capacity();\n"
19514 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19515 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19516 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19517 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19522 Style
.AlignConsecutiveAssignments
.Enabled
= true;
19523 verifyFormat("void foo() {\n"
19524 " int myVar = 5;\n"
19525 " double x = 3.14;\n"
19526 " auto str = \"Hello \"\n"
19528 " auto s = \"Hello \"\n"
19533 // clang-format off
19534 verifyFormat("void foo() {\n"
19535 " const int capacityBefore = Entries.capacity();\n"
19536 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19537 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19538 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19539 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19544 Style
.AlignConsecutiveAssignments
.Enabled
= false;
19545 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
19546 verifyFormat("void foo() {\n"
19547 " int myVar = 5;\n"
19548 " double x = 3.14;\n"
19549 " auto str = \"Hello \"\n"
19551 " auto s = \"Hello \"\n"
19556 // clang-format off
19557 verifyFormat("void foo() {\n"
19558 " const int capacityBefore = Entries.capacity();\n"
19559 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19560 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19561 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19562 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19567 Style
.AlignConsecutiveAssignments
.Enabled
= true;
19568 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
19570 verifyFormat("void foo() {\n"
19571 " int myVar = 5;\n"
19572 " double x = 3.14;\n"
19573 " auto str = \"Hello \"\n"
19575 " auto s = \"Hello \"\n"
19580 // clang-format off
19581 verifyFormat("void foo() {\n"
19582 " const int capacityBefore = Entries.capacity();\n"
19583 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19584 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19585 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n"
19586 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n"
19591 Style
= getLLVMStyleWithColumns(20);
19592 Style
.AlignConsecutiveAssignments
.Enabled
= true;
19593 Style
.IndentWidth
= 4;
19595 verifyFormat("void foo() {\n"
19604 verifyFormat("unsigned i = 0;\n"
19610 Style
.ColumnLimit
= 120;
19612 // clang-format off
19613 verifyFormat("void SomeFunc() {\n"
19614 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
19615 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
19616 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
19617 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
19618 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n"
19619 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
19624 Style
.BinPackArguments
= false;
19626 // clang-format off
19627 verifyFormat("void SomeFunc() {\n"
19628 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n"
19629 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
19630 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n"
19631 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
19632 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n"
19633 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n"
19639 TEST_F(FormatTest
, AlignWithInitializerPeriods
) {
19640 auto Style
= getLLVMStyleWithColumns(60);
19642 verifyFormat("void foo1(void) {\n"
19643 " BYTE p[1] = 1;\n"
19644 " A B = {.one_foooooooooooooooo = 2,\n"
19645 " .two_fooooooooooooo = 3,\n"
19646 " .three_fooooooooooooo = 4};\n"
19647 " BYTE payload = 2;\n"
19651 Style
.AlignConsecutiveAssignments
.Enabled
= true;
19652 Style
.AlignConsecutiveDeclarations
.Enabled
= false;
19653 verifyFormat("void foo2(void) {\n"
19654 " BYTE p[1] = 1;\n"
19655 " A B = {.one_foooooooooooooooo = 2,\n"
19656 " .two_fooooooooooooo = 3,\n"
19657 " .three_fooooooooooooo = 4};\n"
19658 " BYTE payload = 2;\n"
19662 Style
.AlignConsecutiveAssignments
.Enabled
= false;
19663 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
19664 verifyFormat("void foo3(void) {\n"
19665 " BYTE p[1] = 1;\n"
19666 " A B = {.one_foooooooooooooooo = 2,\n"
19667 " .two_fooooooooooooo = 3,\n"
19668 " .three_fooooooooooooo = 4};\n"
19669 " BYTE payload = 2;\n"
19673 Style
.AlignConsecutiveAssignments
.Enabled
= true;
19674 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
19675 verifyFormat("void foo4(void) {\n"
19676 " BYTE p[1] = 1;\n"
19677 " A B = {.one_foooooooooooooooo = 2,\n"
19678 " .two_fooooooooooooo = 3,\n"
19679 " .three_fooooooooooooo = 4};\n"
19680 " BYTE payload = 2;\n"
19685 TEST_F(FormatTest
, LinuxBraceBreaking
) {
19686 FormatStyle LinuxBraceStyle
= getLLVMStyle();
19687 LinuxBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Linux
;
19688 verifyFormat("namespace a\n"
19701 " void g() { return; }\n"
19706 "} // namespace a",
19708 verifyFormat("enum X {\n"
19712 verifyFormat("struct S {\n"
19720 " MyFavoriteType Value;\n"
19726 TEST_F(FormatTest
, MozillaBraceBreaking
) {
19727 FormatStyle MozillaBraceStyle
= getLLVMStyle();
19728 MozillaBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Mozilla
;
19729 MozillaBraceStyle
.FixNamespaceComments
= false;
19730 verifyFormat("namespace a {\n"
19740 " void g() { return; }\n"
19754 MozillaBraceStyle
);
19755 verifyFormat("struct S\n"
19765 " MyFavoriteType Value;\n"
19768 MozillaBraceStyle
);
19771 TEST_F(FormatTest
, StroustrupBraceBreaking
) {
19772 FormatStyle StroustrupBraceStyle
= getLLVMStyle();
19773 StroustrupBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Stroustrup
;
19774 verifyFormat("namespace a {\n"
19783 " void g() { return; }\n"
19788 "} // namespace a",
19789 StroustrupBraceStyle
);
19791 verifyFormat("void foo()\n"
19800 StroustrupBraceStyle
);
19802 verifyFormat("#ifdef _DEBUG\n"
19803 "int foo(int i = 0)\n"
19805 "int foo(int i = 5)\n"
19810 StroustrupBraceStyle
);
19812 verifyFormat("void foo() {}\n"
19822 StroustrupBraceStyle
);
19824 verifyFormat("void foobar() { int i = 5; }\n"
19828 "void bar() { foobar(); }\n"
19830 StroustrupBraceStyle
);
19833 TEST_F(FormatTest
, AllmanBraceBreaking
) {
19834 FormatStyle AllmanBraceStyle
= getLLVMStyle();
19835 AllmanBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Allman
;
19837 verifyFormat("namespace a\n"
19841 "} // namespace a",
19849 verifyFormat("namespace a\n"
19861 " void g() { return; }\n"
19870 "} // namespace a",
19873 verifyFormat("void f()\n"
19879 " else if (false)\n"
19890 verifyFormat("void f()\n"
19892 " for (int i = 0; i < 10; ++i)\n"
19903 " } while (false)\n"
19907 verifyFormat("void f(int a)\n"
19927 verifyFormat("enum X\n"
19932 verifyFormat("enum X\n"
19938 verifyFormat("@interface BSApplicationController ()\n"
19941 " id _extraIvar;\n"
19946 verifyFormat("#ifdef _DEBUG\n"
19947 "int foo(int i = 0)\n"
19949 "int foo(int i = 5)\n"
19956 verifyFormat("void foo() {}\n"
19968 verifyFormat("void foobar() { int i = 5; }\n"
19972 "void bar() { foobar(); }\n"
19976 EXPECT_EQ(AllmanBraceStyle
.AllowShortLambdasOnASingleLine
,
19977 FormatStyle::SLS_All
);
19979 verifyFormat("[](int i) { return i + 2; };\n"
19980 "[](int i, int j)\n"
19982 " auto x = i + j;\n"
19983 " auto y = i * j;\n"
19988 " auto shortLambda = [](int i) { return i + 2; };\n"
19989 " auto longLambda = [](int i, int j)\n"
19991 " auto x = i + j;\n"
19992 " auto y = i * j;\n"
19998 AllmanBraceStyle
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_None
;
20000 verifyFormat("[](int i)\n"
20004 "[](int i, int j)\n"
20006 " auto x = i + j;\n"
20007 " auto y = i * j;\n"
20012 " auto shortLambda = [](int i)\n"
20016 " auto longLambda = [](int i, int j)\n"
20018 " auto x = i + j;\n"
20019 " auto y = i * j;\n"
20026 AllmanBraceStyle
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_All
;
20028 // This shouldn't affect ObjC blocks..
20029 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
20034 verifyFormat("void (^block)(void) = ^{\n"
20039 // .. or dict literals.
20040 verifyFormat("void f()\n"
20043 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
20046 verifyFormat("void f()\n"
20049 " [object someMethod:@{a : @\"b\"}];\n"
20052 verifyFormat("int f()\n"
20058 AllmanBraceStyle
.ColumnLimit
= 19;
20059 verifyFormat("void f() { int i; }", AllmanBraceStyle
);
20060 AllmanBraceStyle
.ColumnLimit
= 18;
20061 verifyFormat("void f()\n"
20066 AllmanBraceStyle
.ColumnLimit
= 80;
20068 FormatStyle BreakBeforeBraceShortIfs
= AllmanBraceStyle
;
20069 BreakBeforeBraceShortIfs
.AllowShortIfStatementsOnASingleLine
=
20070 FormatStyle::SIS_WithoutElse
;
20071 BreakBeforeBraceShortIfs
.AllowShortLoopsOnASingleLine
= true;
20072 verifyFormat("void f(bool b)\n"
20079 BreakBeforeBraceShortIfs
);
20080 verifyFormat("void f(bool b)\n"
20082 " if constexpr (b)\n"
20087 BreakBeforeBraceShortIfs
);
20088 verifyFormat("void f(bool b)\n"
20090 " if CONSTEXPR (b)\n"
20095 BreakBeforeBraceShortIfs
);
20096 verifyFormat("void f(bool b)\n"
20098 " if (b) return;\n"
20100 BreakBeforeBraceShortIfs
);
20101 verifyFormat("void f(bool b)\n"
20103 " if constexpr (b) return;\n"
20105 BreakBeforeBraceShortIfs
);
20106 verifyFormat("void f(bool b)\n"
20108 " if CONSTEXPR (b) return;\n"
20110 BreakBeforeBraceShortIfs
);
20111 verifyFormat("void f(bool b)\n"
20118 BreakBeforeBraceShortIfs
);
20121 TEST_F(FormatTest
, WhitesmithsBraceBreaking
) {
20122 FormatStyle WhitesmithsBraceStyle
= getLLVMStyleWithColumns(0);
20123 WhitesmithsBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_Whitesmiths
;
20125 // Make a few changes to the style for testing purposes
20126 WhitesmithsBraceStyle
.AllowShortFunctionsOnASingleLine
=
20127 FormatStyle::SFS_Empty
;
20128 WhitesmithsBraceStyle
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_None
;
20130 // FIXME: this test case can't decide whether there should be a blank line
20131 // after the ~D() line or not. It adds one if one doesn't exist in the test
20132 // and it removes the line if one exists.
20134 verifyFormat("class A;\n"
20150 " } // namespace B",
20151 WhitesmithsBraceStyle);
20154 WhitesmithsBraceStyle
.NamespaceIndentation
= FormatStyle::NI_None
;
20155 verifyFormat("namespace a\n"
20176 " } // namespace a",
20177 WhitesmithsBraceStyle
);
20179 verifyFormat("namespace a\n"
20202 " } // namespace b\n"
20203 " } // namespace a",
20204 WhitesmithsBraceStyle
);
20206 WhitesmithsBraceStyle
.NamespaceIndentation
= FormatStyle::NI_Inner
;
20207 verifyFormat("namespace a\n"
20230 " } // namespace b\n"
20231 " } // namespace a",
20232 WhitesmithsBraceStyle
);
20234 WhitesmithsBraceStyle
.NamespaceIndentation
= FormatStyle::NI_All
;
20235 verifyFormat("namespace a\n"
20258 " } // namespace b\n"
20259 " } // namespace a",
20260 WhitesmithsBraceStyle
);
20262 verifyFormat("void f()\n"
20268 " else if (false)\n"
20277 WhitesmithsBraceStyle
);
20279 verifyFormat("void f()\n"
20281 " for (int i = 0; i < 10; ++i)\n"
20292 " } while (false)\n"
20294 WhitesmithsBraceStyle
);
20296 WhitesmithsBraceStyle
.IndentCaseLabels
= true;
20297 verifyFormat("void switchTest1(int a)\n"
20307 WhitesmithsBraceStyle
);
20309 verifyFormat("void switchTest2(int a)\n"
20327 WhitesmithsBraceStyle
);
20329 verifyFormat("void switchTest3(int a)\n"
20345 WhitesmithsBraceStyle
);
20347 WhitesmithsBraceStyle
.IndentCaseLabels
= false;
20349 verifyFormat("void switchTest4(int a)\n"
20359 WhitesmithsBraceStyle
);
20361 verifyFormat("void switchTest5(int a)\n"
20380 WhitesmithsBraceStyle
);
20382 verifyFormat("void switchTest6(int a)\n"
20398 WhitesmithsBraceStyle
);
20400 verifyFormat("enum X\n"
20402 " Y = 0, // testing\n"
20404 WhitesmithsBraceStyle
);
20406 verifyFormat("enum X\n"
20410 WhitesmithsBraceStyle
);
20411 verifyFormat("enum X\n"
20416 WhitesmithsBraceStyle
);
20418 verifyFormat("@interface BSApplicationController ()\n"
20421 " id _extraIvar;\n"
20424 WhitesmithsBraceStyle
);
20426 verifyFormat("#ifdef _DEBUG\n"
20427 "int foo(int i = 0)\n"
20429 "int foo(int i = 5)\n"
20434 WhitesmithsBraceStyle
);
20436 verifyFormat("void foo() {}\n"
20446 WhitesmithsBraceStyle
);
20448 verifyFormat("void foobar()\n"
20460 WhitesmithsBraceStyle
);
20462 // This shouldn't affect ObjC blocks..
20463 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
20467 WhitesmithsBraceStyle
);
20468 verifyFormat("void (^block)(void) = ^{\n"
20472 WhitesmithsBraceStyle
);
20473 // .. or dict literals.
20474 verifyFormat("void f()\n"
20476 " [object someMethod:@{@\"a\" : @\"b\"}];\n"
20478 WhitesmithsBraceStyle
);
20480 verifyFormat("int f()\n"
20484 WhitesmithsBraceStyle
);
20486 FormatStyle BreakBeforeBraceShortIfs
= WhitesmithsBraceStyle
;
20487 BreakBeforeBraceShortIfs
.AllowShortIfStatementsOnASingleLine
=
20488 FormatStyle::SIS_OnlyFirstIf
;
20489 BreakBeforeBraceShortIfs
.AllowShortLoopsOnASingleLine
= true;
20490 verifyFormat("void f(bool b)\n"
20497 BreakBeforeBraceShortIfs
);
20498 verifyFormat("void f(bool b)\n"
20500 " if (b) return;\n"
20502 BreakBeforeBraceShortIfs
);
20503 verifyFormat("void f(bool b)\n"
20510 BreakBeforeBraceShortIfs
);
20513 TEST_F(FormatTest
, GNUBraceBreaking
) {
20514 FormatStyle GNUBraceStyle
= getLLVMStyle();
20515 GNUBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_GNU
;
20516 verifyFormat("namespace a\n"
20532 " void g() { return; }\n"
20534 "} // namespace a",
20537 verifyFormat("void f()\n"
20543 " else if (false)\n"
20554 verifyFormat("void f()\n"
20556 " for (int i = 0; i < 10; ++i)\n"
20568 " while (false);\n"
20572 verifyFormat("void f(int a)\n"
20592 verifyFormat("enum X\n"
20598 verifyFormat("@interface BSApplicationController ()\n"
20601 " id _extraIvar;\n"
20606 verifyFormat("#ifdef _DEBUG\n"
20607 "int foo(int i = 0)\n"
20609 "int foo(int i = 5)\n"
20616 verifyFormat("void foo() {}\n"
20628 verifyFormat("void foobar() { int i = 5; }\n"
20632 "void bar() { foobar(); }\n"
20637 TEST_F(FormatTest
, WebKitBraceBreaking
) {
20638 FormatStyle WebKitBraceStyle
= getLLVMStyle();
20639 WebKitBraceStyle
.BreakBeforeBraces
= FormatStyle::BS_WebKit
;
20640 WebKitBraceStyle
.FixNamespaceComments
= false;
20641 verifyFormat("namespace a {\n"
20650 " void g() { return; }\n"
20663 verifyFormat("struct S {\n"
20670 " MyFavoriteType Value;\n"
20676 TEST_F(FormatTest
, CatchExceptionReferenceBinding
) {
20677 verifyFormat("void f() {\n"
20679 " } catch (const Exception &e) {\n"
20684 TEST_F(FormatTest
, CatchAlignArrayOfStructuresRightAlignment
) {
20685 auto Style
= getLLVMStyle();
20686 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Right
;
20687 Style
.AlignConsecutiveAssignments
.Enabled
= true;
20688 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
20689 verifyFormat("struct test demo[] = {\n"
20690 " {56, 23, \"hello\"},\n"
20691 " {-1, 93463, \"world\"},\n"
20692 " { 7, 5, \"!!\"}\n"
20696 verifyFormat("struct test demo[] = {\n"
20697 " {56, 23, \"hello\"}, // first line\n"
20698 " {-1, 93463, \"world\"}, // second line\n"
20699 " { 7, 5, \"!!\"} // third line\n"
20703 verifyFormat("struct test demo[4] = {\n"
20704 " { 56, 23, 21, \"oh\"}, // first line\n"
20705 " { -1, 93463, 22, \"my\"}, // second line\n"
20706 " { 7, 5, 1, \"goodness\"} // third line\n"
20707 " {234, 5, 1, \"gracious\"} // fourth line\n"
20711 verifyFormat("struct test demo[3] = {\n"
20712 " {56, 23, \"hello\"},\n"
20713 " {-1, 93463, \"world\"},\n"
20714 " { 7, 5, \"!!\"}\n"
20718 verifyFormat("struct test demo[3] = {\n"
20719 " {int{56}, 23, \"hello\"},\n"
20720 " {int{-1}, 93463, \"world\"},\n"
20721 " { int{7}, 5, \"!!\"}\n"
20725 verifyFormat("struct test demo[] = {\n"
20726 " {56, 23, \"hello\"},\n"
20727 " {-1, 93463, \"world\"},\n"
20728 " { 7, 5, \"!!\"},\n"
20732 verifyFormat("test demo[] = {\n"
20733 " {56, 23, \"hello\"},\n"
20734 " {-1, 93463, \"world\"},\n"
20735 " { 7, 5, \"!!\"},\n"
20739 verifyFormat("demo = std::array<struct test, 3>{\n"
20740 " test{56, 23, \"hello\"},\n"
20741 " test{-1, 93463, \"world\"},\n"
20742 " test{ 7, 5, \"!!\"},\n"
20746 verifyFormat("test demo[] = {\n"
20747 " {56, 23, \"hello\"},\n"
20749 " {-1, 93463, \"world\"},\n"
20751 " { 7, 5, \"!!\"}\n"
20756 "test demo[] = {\n"
20758 " \"hello world i am a very long line that really, in any\"\n"
20759 " \"just world, ought to be split over multiple lines\"},\n"
20760 " {-1, 93463, \"world\"},\n"
20761 " {56, 5, \"!!\"}\n"
20765 verifyFormat("return GradForUnaryCwise(g, {\n"
20766 " {{\"sign\"}, \"Sign\", "
20767 " {\"x\", \"dy\"}},\n"
20768 " { {\"dx\"}, \"Mul\", {\"dy\""
20773 Style
.Cpp11BracedListStyle
= false;
20774 verifyFormat("struct test demo[] = {\n"
20775 " { 56, 23, \"hello\" },\n"
20776 " { -1, 93463, \"world\" },\n"
20777 " { 7, 5, \"!!\" }\n"
20780 Style
.Cpp11BracedListStyle
= true;
20782 Style
.ColumnLimit
= 0;
20784 "test demo[] = {\n"
20785 " {56, 23, \"hello world i am a very long line that really, "
20786 "in any just world, ought to be split over multiple lines\"},\n"
20792 "test demo[] = {{56, 23, \"hello world i am a very long line "
20793 "that really, in any just world, ought to be split over multiple "
20794 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
20797 Style
.ColumnLimit
= 80;
20798 verifyFormat("test demo[] = {\n"
20799 " {56, 23, /* a comment */ \"hello\"},\n"
20800 " {-1, 93463, \"world\"},\n"
20801 " { 7, 5, \"!!\"}\n"
20805 verifyFormat("test demo[] = {\n"
20806 " {56, 23, \"hello\"},\n"
20807 " {-1, 93463, \"world\" /* comment here */},\n"
20808 " { 7, 5, \"!!\"}\n"
20812 verifyFormat("test demo[] = {\n"
20813 " {56, /* a comment */ 23, \"hello\"},\n"
20814 " {-1, 93463, \"world\"},\n"
20815 " { 7, 5, \"!!\"}\n"
20819 Style
.ColumnLimit
= 20;
20820 verifyFormat("demo = std::array<\n"
20821 " struct test, 3>{\n"
20826 " \"am a very \"\n"
20827 " \"long line \"\n"
20838 " test{-1, 93463,\n"
20843 "demo = std::array<struct test, 3>{test{56, 23, \"hello world "
20844 "i am a very long line that really, in any just world, ought "
20845 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
20846 "test{7, 5, \"!!\"},};",
20848 // This caused a core dump by enabling Alignment in the LLVMStyle globally
20849 Style
= getLLVMStyleWithColumns(50);
20850 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Right
;
20851 verifyFormat("static A x = {\n"
20852 " {{init1, init2, init3, init4},\n"
20853 " {init1, init2, init3, init4}}\n"
20856 // TODO: Fix the indentations below when this option is fully functional.
20857 verifyFormat("int a[][] = {\n"
20864 Style
.ColumnLimit
= 100;
20866 "test demo[] = {\n"
20868 " \"hello world i am a very long line that really, in any just world"
20869 ", ought to be split over \"\n"
20870 " \"multiple lines\" },\n"
20871 " {-1, 93463, \"world\"},\n"
20872 " { 7, 5, \"!!\"},\n"
20874 "test demo[] = {{56, 23, \"hello world i am a very long line "
20875 "that really, in any just world, ought to be split over multiple "
20876 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
20879 Style
= getLLVMStyleWithColumns(50);
20880 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Right
;
20881 verifyFormat("struct test demo[] = {\n"
20882 " {56, 23, \"hello\"},\n"
20883 " {-1, 93463, \"world\"},\n"
20884 " { 7, 5, \"!!\"}\n"
20887 " {{init1, init2, init3, init4},\n"
20888 " {init1, init2, init3, init4}}\n"
20891 Style
.ColumnLimit
= 100;
20892 Style
.AlignConsecutiveAssignments
.AcrossComments
= true;
20893 Style
.AlignConsecutiveDeclarations
.AcrossComments
= true;
20894 verifyFormat("struct test demo[] = {\n"
20895 " {56, 23, \"hello\"},\n"
20896 " {-1, 93463, \"world\"},\n"
20897 " { 7, 5, \"!!\"}\n"
20899 "struct test demo[4] = {\n"
20900 " { 56, 23, 21, \"oh\"}, // first line\n"
20901 " { -1, 93463, 22, \"my\"}, // second line\n"
20902 " { 7, 5, 1, \"goodness\"} // third line\n"
20903 " {234, 5, 1, \"gracious\"} // fourth line\n"
20907 "test demo[] = {\n"
20909 " \"hello world i am a very long line that really, in any just world"
20910 ", ought to be split over \"\n"
20911 " \"multiple lines\", 23},\n"
20912 " {-1, \"world\", 93463},\n"
20913 " { 7, \"!!\", 5},\n"
20915 "test demo[] = {{56, \"hello world i am a very long line "
20916 "that really, in any just world, ought to be split over multiple "
20917 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};",
20921 TEST_F(FormatTest
, CatchAlignArrayOfStructuresLeftAlignment
) {
20922 auto Style
= getLLVMStyle();
20923 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Left
;
20924 /* FIXME: This case gets misformatted.
20925 verifyFormat("auto foo = Items{\n"
20926 " Section{0, bar(), },\n"
20927 " Section{1, boo() }\n"
20931 verifyFormat("auto foo = Items{\n"
20937 verifyFormat("struct test demo[] = {\n"
20938 " {56, 23, \"hello\"},\n"
20939 " {-1, 93463, \"world\"},\n"
20940 " {7, 5, \"!!\" }\n"
20943 verifyFormat("struct test demo[] = {\n"
20944 " {56, 23, \"hello\"}, // first line\n"
20945 " {-1, 93463, \"world\"}, // second line\n"
20946 " {7, 5, \"!!\" } // third line\n"
20949 verifyFormat("struct test demo[4] = {\n"
20950 " {56, 23, 21, \"oh\" }, // first line\n"
20951 " {-1, 93463, 22, \"my\" }, // second line\n"
20952 " {7, 5, 1, \"goodness\"} // third line\n"
20953 " {234, 5, 1, \"gracious\"} // fourth line\n"
20956 verifyFormat("struct test demo[3] = {\n"
20957 " {56, 23, \"hello\"},\n"
20958 " {-1, 93463, \"world\"},\n"
20959 " {7, 5, \"!!\" }\n"
20963 verifyFormat("struct test demo[3] = {\n"
20964 " {int{56}, 23, \"hello\"},\n"
20965 " {int{-1}, 93463, \"world\"},\n"
20966 " {int{7}, 5, \"!!\" }\n"
20969 verifyFormat("struct test demo[] = {\n"
20970 " {56, 23, \"hello\"},\n"
20971 " {-1, 93463, \"world\"},\n"
20972 " {7, 5, \"!!\" },\n"
20975 verifyFormat("test demo[] = {\n"
20976 " {56, 23, \"hello\"},\n"
20977 " {-1, 93463, \"world\"},\n"
20978 " {7, 5, \"!!\" },\n"
20981 verifyFormat("demo = std::array<struct test, 3>{\n"
20982 " test{56, 23, \"hello\"},\n"
20983 " test{-1, 93463, \"world\"},\n"
20984 " test{7, 5, \"!!\" },\n"
20987 verifyFormat("test demo[] = {\n"
20988 " {56, 23, \"hello\"},\n"
20990 " {-1, 93463, \"world\"},\n"
20992 " {7, 5, \"!!\" }\n"
20996 "test demo[] = {\n"
20998 " \"hello world i am a very long line that really, in any\"\n"
20999 " \"just world, ought to be split over multiple lines\"},\n"
21000 " {-1, 93463, \"world\" },\n"
21001 " {56, 5, \"!!\" }\n"
21005 verifyFormat("return GradForUnaryCwise(g, {\n"
21006 " {{\"sign\"}, \"Sign\", {\"x\", "
21008 " {{\"dx\"}, \"Mul\", "
21009 "{\"dy\", \"sign\"}},\n"
21013 Style
.Cpp11BracedListStyle
= false;
21014 verifyFormat("struct test demo[] = {\n"
21015 " { 56, 23, \"hello\" },\n"
21016 " { -1, 93463, \"world\" },\n"
21017 " { 7, 5, \"!!\" }\n"
21020 Style
.Cpp11BracedListStyle
= true;
21022 Style
.ColumnLimit
= 0;
21024 "test demo[] = {\n"
21025 " {56, 23, \"hello world i am a very long line that really, in any "
21026 "just world, ought to be split over multiple lines\"},\n"
21027 " {-1, 93463, \"world\" "
21032 "test demo[] = {{56, 23, \"hello world i am a very long line "
21033 "that really, in any just world, ought to be split over multiple "
21034 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21037 Style
.ColumnLimit
= 80;
21038 verifyFormat("test demo[] = {\n"
21039 " {56, 23, /* a comment */ \"hello\"},\n"
21040 " {-1, 93463, \"world\" },\n"
21041 " {7, 5, \"!!\" }\n"
21045 verifyFormat("test demo[] = {\n"
21046 " {56, 23, \"hello\" },\n"
21047 " {-1, 93463, \"world\" /* comment here */},\n"
21048 " {7, 5, \"!!\" }\n"
21052 verifyFormat("test demo[] = {\n"
21053 " {56, /* a comment */ 23, \"hello\"},\n"
21054 " {-1, 93463, \"world\"},\n"
21055 " {7, 5, \"!!\" }\n"
21059 Style
.ColumnLimit
= 20;
21060 // FIXME: unstable test case
21062 "demo = std::array<\n"
21063 " struct test, 3>{\n"
21068 " \"am a very \"\n"
21069 " \"long line \"\n"
21080 " test{-1, 93463,\n"
21085 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world "
21086 "i am a very long line that really, in any just world, ought "
21087 "to be split over multiple lines\"},test{-1, 93463, \"world\"},"
21088 "test{7, 5, \"!!\"},};",
21091 // This caused a core dump by enabling Alignment in the LLVMStyle globally
21092 Style
= getLLVMStyleWithColumns(50);
21093 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Left
;
21094 verifyFormat("static A x = {\n"
21095 " {{init1, init2, init3, init4},\n"
21096 " {init1, init2, init3, init4}}\n"
21099 Style
.ColumnLimit
= 100;
21101 "test demo[] = {\n"
21103 " \"hello world i am a very long line that really, in any just world"
21104 ", ought to be split over \"\n"
21105 " \"multiple lines\" },\n"
21106 " {-1, 93463, \"world\"},\n"
21107 " {7, 5, \"!!\" },\n"
21109 "test demo[] = {{56, 23, \"hello world i am a very long line "
21110 "that really, in any just world, ought to be split over multiple "
21111 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
21115 TEST_F(FormatTest
, UnderstandsPragmas
) {
21116 verifyFormat("#pragma omp reduction(| : var)");
21117 verifyFormat("#pragma omp reduction(+ : var)");
21119 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21120 "(including parentheses).",
21121 "#pragma mark Any non-hyphenated or hyphenated string "
21122 "(including parentheses).");
21124 verifyFormat("#pragma mark Any non-hyphenated or hyphenated string "
21125 "(including parentheses).",
21126 "#pragma mark Any non-hyphenated or hyphenated string "
21127 "(including parentheses).");
21129 verifyFormat("#pragma comment(linker, \\\n"
21130 " \"argument\" \\\n"
21132 "#pragma comment(linker, \\\n"
21133 " \"argument\" \\\n"
21135 getStyleWithColumns(
21136 getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp
), 32));
21139 TEST_F(FormatTest
, UnderstandsPragmaOmpTarget
) {
21140 verifyFormat("#pragma omp target map(to : var)");
21141 verifyFormat("#pragma omp target map(to : var[ : N])");
21142 verifyFormat("#pragma omp target map(to : var[0 : N])");
21143 verifyFormat("#pragma omp target map(always, to : var[0 : N])");
21146 "#pragma omp target \\\n"
21147 " reduction(+ : var) \\\n"
21148 " map(to : A[0 : N]) \\\n"
21149 " map(to : B[0 : N]) \\\n"
21150 " map(from : C[0 : N]) \\\n"
21151 " firstprivate(i) \\\n"
21152 " firstprivate(j) \\\n"
21153 " firstprivate(k)",
21154 "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) "
21155 "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)",
21156 getLLVMStyleWithColumns(26));
21159 TEST_F(FormatTest
, UnderstandPragmaOption
) {
21160 verifyFormat("#pragma option -C -A");
21162 verifyFormat("#pragma option -C -A", "#pragma option -C -A");
21165 TEST_F(FormatTest
, UnderstandPragmaRegion
) {
21166 auto Style
= getLLVMStyleWithColumns(0);
21167 verifyFormat("#pragma region TEST(FOO : BAR)", Style
);
21168 verifyFormat("#pragma region TEST(FOO: NOSPACE)", Style
);
21171 TEST_F(FormatTest
, OptimizeBreakPenaltyVsExcess
) {
21172 FormatStyle Style
= getLLVMStyleWithColumns(20);
21175 verifyFormat("/*\n"
21180 " *\t9012345 /8901\n"
21183 verifyFormat("/*\n"
21188 " *345678\t/8901\n"
21192 verifyFormat("int a; // the\n"
21195 verifyNoChange("int a; /* first line\n"
21201 verifyFormat("int a; // first line\n"
21205 "int a; // first line\n"
21206 " // second line\n"
21210 Style
.PenaltyExcessCharacter
= 90;
21211 verifyFormat("int a; // the comment", Style
);
21212 verifyFormat("int a; // the comment\n"
21214 "int a; // the comment aaa", Style
);
21215 verifyNoChange("int a; /* first line\n"
21220 verifyFormat("int a; // first line\n"
21221 " // second line\n"
21224 // FIXME: Investigate why this is not getting the same layout as the test
21226 verifyFormat("int a; /* first line\n"
21230 "int a; /* first line second line third line"
21234 verifyFormat("// foo bar baz bazfoo\n"
21235 "// foo bar foo bar",
21236 "// foo bar baz bazfoo\n"
21237 "// foo bar foo bar",
21239 verifyFormat("// foo bar baz bazfoo\n"
21240 "// foo bar foo bar",
21241 "// foo bar baz bazfoo\n"
21242 "// foo bar foo bar",
21245 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the
21247 verifyFormat("// foo bar baz bazfoo\n"
21249 "// foo bar baz bazfoo bar\n"
21253 // FIXME: unstable test case
21254 EXPECT_EQ("// foo bar baz bazfoo\n"
21255 "// foo bar baz bazfoo\n"
21257 format("// foo bar baz bazfoo\n"
21258 "// foo bar baz bazfoo bar\n"
21262 // FIXME: unstable test case
21263 EXPECT_EQ("// foo bar baz bazfoo\n"
21264 "// foo bar baz bazfoo\n"
21266 format("// foo bar baz bazfoo\n"
21267 "// foo bar baz bazfoo bar\n"
21271 // Make sure we do not keep protruding characters if strict mode reflow is
21272 // cheaper than keeping protruding characters.
21273 Style
.ColumnLimit
= 21;
21274 verifyFormat("// foo foo foo foo\n"
21275 "// foo foo foo foo\n"
21276 "// foo foo foo foo",
21277 "// foo foo foo foo foo foo foo foo foo foo foo foo", Style
);
21279 verifyFormat("int a = /* long block\n"
21282 "int a = /* long block comment */ 42;", Style
);
21285 TEST_F(FormatTest
, BreakPenaltyAfterLParen
) {
21286 FormatStyle Style
= getLLVMStyle();
21287 Style
.ColumnLimit
= 8;
21288 Style
.PenaltyExcessCharacter
= 15;
21289 verifyFormat("int foo(\n"
21290 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
21292 Style
.PenaltyBreakOpenParenthesis
= 200;
21293 verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);",
21295 " int aaaaaaaaaaaaaaaaaaaaaaaa);",
21299 TEST_F(FormatTest
, BreakPenaltyAfterCastLParen
) {
21300 FormatStyle Style
= getLLVMStyle();
21301 Style
.ColumnLimit
= 5;
21302 Style
.PenaltyExcessCharacter
= 150;
21303 verifyFormat("foo((\n"
21304 " int)aaaaaaaaaaaaaaaaaaaaaaaa);",
21307 Style
.PenaltyBreakOpenParenthesis
= 100000;
21308 verifyFormat("foo((int)\n"
21309 " aaaaaaaaaaaaaaaaaaaaaaaa);",
21311 "int)aaaaaaaaaaaaaaaaaaaaaaaa);",
21315 TEST_F(FormatTest
, BreakPenaltyAfterForLoopLParen
) {
21316 FormatStyle Style
= getLLVMStyle();
21317 Style
.ColumnLimit
= 4;
21318 Style
.PenaltyExcessCharacter
= 100;
21319 verifyFormat("for (\n"
21320 " int iiiiiiiiiiiiiiiii =\n"
21322 " iiiiiiiiiiiiiiiii <\n"
21324 " iiiiiiiiiiiiiiiii++) {\n"
21328 Style
.PenaltyBreakOpenParenthesis
= 1250;
21329 verifyFormat("for (int iiiiiiiiiiiiiiiii =\n"
21331 " iiiiiiiiiiiiiiiii <\n"
21333 " iiiiiiiiiiiiiiiii++) {\n"
21336 " int iiiiiiiiiiiiiiiii =\n"
21338 " iiiiiiiiiiiiiiiii <\n"
21340 " iiiiiiiiiiiiiiiii++) {\n"
21345 TEST_F(FormatTest
, WorksFor8bitEncodings
) {
21346 // FIXME: unstable test case
21347 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
21348 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
21349 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
21350 "\"\xef\xee\xf0\xf3...\"",
21351 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
21352 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
21353 "\xef\xee\xf0\xf3...\"",
21354 getLLVMStyleWithColumns(12)));
21357 TEST_F(FormatTest
, HandlesUTF8BOM
) {
21358 verifyFormat("\xef\xbb\xbf");
21359 verifyFormat("\xef\xbb\xbf#include <iostream>");
21360 verifyFormat("\xef\xbb\xbf\n#include <iostream>");
21363 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
21364 #if !defined(_MSC_VER)
21366 TEST_F(FormatTest
, CountsUTF8CharactersProperly
) {
21367 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
21368 getLLVMStyleWithColumns(35));
21369 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
21370 getLLVMStyleWithColumns(31));
21371 verifyFormat("// Однажды в студёную зимнюю пору...",
21372 getLLVMStyleWithColumns(36));
21373 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
21374 verifyFormat("/* Однажды в студёную зимнюю пору... */",
21375 getLLVMStyleWithColumns(39));
21376 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
21377 getLLVMStyleWithColumns(35));
21380 TEST_F(FormatTest
, SplitsUTF8Strings
) {
21381 // Non-printable characters' width is currently considered to be the length in
21382 // bytes in UTF8. The characters can be displayed in very different manner
21383 // (zero-width, single width with a substitution glyph, expanded to their code
21384 // (e.g. "<8d>"), so there's no single correct way to handle them.
21385 // FIXME: unstable test case
21386 EXPECT_EQ("\"aaaaÄ\"\n"
21388 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
21389 // FIXME: unstable test case
21390 EXPECT_EQ("\"aaaaaaaÄ\"\n"
21392 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
21393 // FIXME: unstable test case
21394 EXPECT_EQ("\"Однажды, в \"\n"
21398 format("\"Однажды, в студёную зимнюю пору,\"",
21399 getLLVMStyleWithColumns(13)));
21400 // FIXME: unstable test case
21406 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
21407 // FIXME: unstable test case
21408 EXPECT_EQ("\"一\t\"\n"
21415 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
21416 getLLVMStyleWithColumns(11)));
21418 // UTF8 character in an escape sequence.
21419 // FIXME: unstable test case
21420 EXPECT_EQ("\"aaaaaa\"\n"
21422 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
21425 TEST_F(FormatTest
, HandlesDoubleWidthCharsInMultiLineStrings
) {
21426 verifyFormat("const char *sssss =\n"
21429 "const char *sssss = \"一二三四五六七八\\\n"
21431 getLLVMStyleWithColumns(30));
21434 TEST_F(FormatTest
, SplitsUTF8LineComments
) {
21435 verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
21436 verifyFormat("// Я из лесу\n"
21440 "// Я из лесу вышел; был сильный мороз.",
21441 getLLVMStyleWithColumns(13));
21442 verifyFormat("// 一二三\n"
21446 "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9));
21449 TEST_F(FormatTest
, SplitsUTF8BlockComments
) {
21450 verifyFormat("/* Гляжу,\n"
21458 "/* Гляжу, поднимается медленно в гору\n"
21459 " * Лошадка, везущая хворосту воз. */",
21460 getLLVMStyleWithColumns(13));
21461 verifyFormat("/* 一二三\n"
21465 "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9));
21466 verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
21469 "/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12));
21474 TEST_F(FormatTest
, ConstructorInitializerIndentWidth
) {
21475 FormatStyle Style
= getLLVMStyle();
21477 Style
.ConstructorInitializerIndentWidth
= 4;
21479 "SomeClass::Constructor()\n"
21480 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
21481 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
21484 Style
.ConstructorInitializerIndentWidth
= 2;
21486 "SomeClass::Constructor()\n"
21487 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
21488 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
21491 Style
.ConstructorInitializerIndentWidth
= 0;
21493 "SomeClass::Constructor()\n"
21494 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
21495 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
21497 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
21499 "SomeLongTemplateVariableName<\n"
21500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
21502 verifyFormat("bool smaller = 1 < "
21503 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
21505 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
21508 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
21509 verifyFormat("SomeClass::Constructor() :\n"
21510 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n"
21511 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}",
21515 TEST_F(FormatTest
, BreakConstructorInitializersBeforeComma
) {
21516 FormatStyle Style
= getLLVMStyle();
21517 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
21518 Style
.ConstructorInitializerIndentWidth
= 4;
21519 verifyFormat("SomeClass::Constructor()\n"
21524 verifyFormat("SomeClass::Constructor()\n"
21528 Style
.ColumnLimit
= 0;
21529 verifyFormat("SomeClass::Constructor()\n"
21532 verifyFormat("SomeClass::Constructor() noexcept\n"
21535 verifyFormat("SomeClass::Constructor()\n"
21540 verifyFormat("SomeClass::Constructor()\n"
21547 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
21548 verifyFormat("SomeClass::Constructor()\n"
21553 verifyFormat("SomeClass::Constructor()\n"
21557 Style
.ColumnLimit
= 80;
21558 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_All
;
21559 Style
.ConstructorInitializerIndentWidth
= 2;
21560 verifyFormat("SomeClass::Constructor()\n"
21566 Style
.ConstructorInitializerIndentWidth
= 0;
21567 verifyFormat("SomeClass::Constructor()\n"
21573 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
21574 Style
.ConstructorInitializerIndentWidth
= 4;
21575 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style
);
21577 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
21580 "SomeClass::Constructor()\n"
21581 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
21583 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
21584 verifyFormat("SomeClass::Constructor()\n"
21585 " : aaaaaaaa(aaaaaaaa) {}",
21587 verifyFormat("SomeClass::Constructor()\n"
21588 " : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)",
21591 "SomeClass::Constructor()\n"
21592 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
21595 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLine
;
21596 Style
.ConstructorInitializerIndentWidth
= 4;
21597 Style
.ColumnLimit
= 60;
21598 verifyFormat("SomeClass::Constructor()\n"
21599 " : aaaaaaaa(aaaaaaaa)\n"
21600 " , aaaaaaaa(aaaaaaaa)\n"
21601 " , aaaaaaaa(aaaaaaaa) {}",
21603 Style
.PackConstructorInitializers
= FormatStyle::PCIS_NextLineOnly
;
21604 verifyFormat("SomeClass::Constructor()\n"
21605 " : aaaaaaaa(aaaaaaaa)\n"
21606 " , aaaaaaaa(aaaaaaaa)\n"
21607 " , aaaaaaaa(aaaaaaaa) {}",
21611 TEST_F(FormatTest
, ConstructorInitializersWithPreprocessorDirective
) {
21612 FormatStyle Style
= getLLVMStyle();
21613 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeComma
;
21614 Style
.ConstructorInitializerIndentWidth
= 4;
21615 verifyFormat("SomeClass::Constructor()\n"
21619 verifyFormat("SomeClass::Constructor()\n"
21626 Style
.ConstructorInitializerIndentWidth
= 2;
21627 verifyFormat("SomeClass::Constructor()\n"
21634 Style
.ConstructorInitializerIndentWidth
= 0;
21635 verifyFormat("SomeClass::Constructor()\n"
21637 "#ifdef CONDITION\n"
21644 Style
.ConstructorInitializerIndentWidth
= 4;
21645 verifyFormat("SomeClass::Constructor()\n"
21662 verifyFormat("SomeClass::Constructor()\n"
21683 TEST_F(FormatTest
, Destructors
) {
21684 verifyFormat("void F(int &i) { i.~int(); }");
21685 verifyFormat("void F(int &i) { i->~int(); }");
21688 TEST_F(FormatTest
, FormatsWithWebKitStyle
) {
21689 FormatStyle Style
= getWebKitStyle();
21691 // Don't indent in outer namespaces.
21692 verifyFormat("namespace outer {\n"
21694 "namespace inner {\n"
21696 "} // namespace inner\n"
21697 "} // namespace outer\n"
21698 "namespace other_outer {\n"
21703 // Don't indent case labels.
21704 verifyFormat("switch (variable) {\n"
21707 " doSomething();\n"
21714 // Wrap before binary operators.
21718 " if (aaaaaaaaaaaaaaaa\n"
21719 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
21720 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
21724 "if (aaaaaaaaaaaaaaaa\n"
21725 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
21726 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
21731 // Allow functions on a single line.
21732 verifyFormat("void f() { return; }", Style
);
21734 // Allow empty blocks on a single line and insert a space in empty blocks.
21735 verifyFormat("void f() { }", "void f() {}", Style
);
21736 verifyFormat("while (true) { }", "while (true) {}", Style
);
21737 // However, don't merge non-empty short loops.
21738 verifyFormat("while (true) {\n"
21741 "while (true) { continue; }", Style
);
21743 // Constructor initializers are formatted one per line with the "," on the
21745 verifyFormat("Constructor()\n"
21746 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
21747 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
21748 " aaaaaaaaaaaaaa)\n"
21749 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
21753 verifyFormat("SomeClass::Constructor()\n"
21758 verifyFormat("SomeClass::Constructor()\n"
21762 "SomeClass::Constructor():a(a){}", Style
);
21763 verifyFormat("SomeClass::Constructor()\n"
21770 verifyFormat("SomeClass::Constructor()\n"
21778 // Access specifiers should be aligned left.
21779 verifyFormat("class C {\n"
21785 // Do not align comments.
21786 verifyFormat("int a; // Do not\n"
21787 "double b; // align comments.",
21790 // Do not align operands.
21791 verifyFormat("ASSERT(aaaa\n"
21793 "ASSERT ( aaaa\n||bbbb);", Style
);
21795 // Accept input's line breaks.
21796 verifyFormat("if (aaaaaaaaaaaaaaa\n"
21797 " || bbbbbbbbbbbbbbb) {\n"
21800 "if (aaaaaaaaaaaaaaa\n"
21801 "|| bbbbbbbbbbbbbbb) { i++; }",
21803 verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
21806 "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style
);
21808 // Don't automatically break all macro definitions (llvm.org/PR17842).
21809 verifyFormat("#define aNumber 10", Style
);
21810 // However, generally keep the line breaks that the user authored.
21811 verifyFormat("#define aNumber \\\n"
21813 "#define aNumber \\\n"
21817 // Keep empty and one-element array literals on a single line.
21818 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
21819 " copyItems:YES];",
21820 "NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
21823 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
21824 " copyItems:YES];",
21825 "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
21826 " copyItems:YES];",
21828 // FIXME: This does not seem right, there should be more indentation before
21829 // the array literal's entries. Nested blocks have the same problem.
21830 verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
21834 " copyItems:YES];",
21835 "NSArray* a = [[NSArray alloc] initWithArray:@[\n"
21839 " copyItems:YES];",
21842 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
21843 " copyItems:YES];",
21844 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
21845 " copyItems:YES];",
21848 verifyFormat("[self.a b:c c:d];", Style
);
21849 verifyFormat("[self.a b:c\n"
21856 TEST_F(FormatTest
, FormatsLambdas
) {
21857 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();");
21859 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();");
21860 verifyFormat("int c = [&] { [=] { return b++; }(); }();");
21861 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();");
21862 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();");
21863 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}");
21864 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}");
21865 verifyFormat("auto c = [a = [b = 42] {}] {};");
21866 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};");
21867 verifyFormat("int x = f(*+[] {});");
21868 verifyFormat("void f() {\n"
21869 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
21871 verifyFormat("void f() {\n"
21872 " other(x.begin(), //\n"
21874 " [&](int, int) { return 1; });\n"
21876 verifyFormat("void f() {\n"
21877 " other.other.other.other.other(\n"
21878 " x.begin(), x.end(),\n"
21879 " [something, rather](int, int, int, int, int, int, int) { "
21884 " other.other.other.other.other(\n"
21885 " x.begin(), x.end(),\n"
21886 " [something, rather](int, int, int, int, int, int, int) {\n"
21890 verifyFormat("SomeFunction([]() { // A cool function...\n"
21893 verifyFormat("SomeFunction([]() {\n"
21897 "SomeFunction([](){\n"
21901 verifyFormat("void f() {\n"
21902 " SomeFunction([](decltype(x), A *a) {});\n"
21903 " SomeFunction([](typeof(x), A *a) {});\n"
21904 " SomeFunction([](_Atomic(x), A *a) {});\n"
21905 " SomeFunction([](__underlying_type(x), A *a) {});\n"
21907 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21908 " [](const aaaaaaaaaa &a) { return a; });");
21909 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
21910 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
21912 verifyFormat("Constructor()\n"
21913 " : Field([] { // comment\n"
21916 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
21917 " return some_parameter.size();\n"
21919 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
21920 " [](const string &s) { return s; };");
21921 verifyFormat("int i = aaaaaa ? 1 //\n"
21925 verifyFormat("llvm::errs() << \"number of twos is \"\n"
21926 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
21927 " return x == 2; // force break\n"
21929 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
21930 " [=](int iiiiiiiiiiii) {\n"
21931 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
21932 " aaaaaaaaaaaaaaaaaaaaaaa;\n"
21934 getLLVMStyleWithColumns(60));
21936 verifyFormat("SomeFunction({[&] {\n"
21942 verifyFormat("SomeFunction({[&] {\n"
21946 "virtual aaaaaaaaaaaaaaaa(\n"
21947 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n"
21948 " aaaaa aaaaaaaaa);");
21950 // Lambdas with return types.
21951 verifyFormat("int c = []() -> int { return 2; }();");
21952 verifyFormat("int c = []() -> int * { return 2; }();");
21953 verifyFormat("int c = []() -> vector<int> { return {2}; }();");
21954 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
21955 verifyFormat("foo([]() noexcept -> int {});");
21956 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
21957 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
21958 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
21959 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
21960 verifyFormat("[a, a]() -> a<1> {};");
21961 verifyFormat("[]() -> foo<5 + 2> { return {}; };");
21962 verifyFormat("[]() -> foo<5 - 2> { return {}; };");
21963 verifyFormat("[]() -> foo<5 / 2> { return {}; };");
21964 verifyFormat("[]() -> foo<5 * 2> { return {}; };");
21965 verifyFormat("[]() -> foo<5 % 2> { return {}; };");
21966 verifyFormat("[]() -> foo<5 << 2> { return {}; };");
21967 verifyFormat("[]() -> foo<!5> { return {}; };");
21968 verifyFormat("[]() -> foo<~5> { return {}; };");
21969 verifyFormat("[]() -> foo<5 | 2> { return {}; };");
21970 verifyFormat("[]() -> foo<5 || 2> { return {}; };");
21971 verifyFormat("[]() -> foo<5 & 2> { return {}; };");
21972 verifyFormat("[]() -> foo<5 && 2> { return {}; };");
21973 verifyFormat("[]() -> foo<5 == 2> { return {}; };");
21974 verifyFormat("[]() -> foo<5 != 2> { return {}; };");
21975 verifyFormat("[]() -> foo<5 >= 2> { return {}; };");
21976 verifyFormat("[]() -> foo<5 <= 2> { return {}; };");
21977 verifyFormat("[]() -> foo<5 < 2> { return {}; };");
21978 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };");
21979 verifyFormat("namespace bar {\n"
21981 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n"
21982 "} // namespace bar");
21983 verifyFormat("namespace bar {\n"
21985 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n"
21986 "} // namespace bar");
21987 verifyFormat("namespace bar {\n"
21989 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n"
21990 "} // namespace bar");
21991 verifyFormat("namespace bar {\n"
21993 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n"
21994 "} // namespace bar");
21995 verifyFormat("namespace bar {\n"
21997 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n"
21998 "} // namespace bar");
21999 verifyFormat("namespace bar {\n"
22001 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n"
22002 "} // namespace bar");
22003 verifyFormat("namespace bar {\n"
22005 "auto foo{[]() -> foo<!5> { return {}; }};\n"
22006 "} // namespace bar");
22007 verifyFormat("namespace bar {\n"
22009 "auto foo{[]() -> foo<~5> { return {}; }};\n"
22010 "} // namespace bar");
22011 verifyFormat("namespace bar {\n"
22013 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n"
22014 "} // namespace bar");
22015 verifyFormat("namespace bar {\n"
22017 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n"
22018 "} // namespace bar");
22019 verifyFormat("namespace bar {\n"
22021 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n"
22022 "} // namespace bar");
22023 verifyFormat("namespace bar {\n"
22025 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n"
22026 "} // namespace bar");
22027 verifyFormat("namespace bar {\n"
22029 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n"
22030 "} // namespace bar");
22031 verifyFormat("namespace bar {\n"
22033 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n"
22034 "} // namespace bar");
22035 verifyFormat("namespace bar {\n"
22037 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n"
22038 "} // namespace bar");
22039 verifyFormat("namespace bar {\n"
22041 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n"
22042 "} // namespace bar");
22043 verifyFormat("namespace bar {\n"
22045 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n"
22046 "} // namespace bar");
22047 verifyFormat("namespace bar {\n"
22049 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n"
22050 "} // namespace bar");
22051 verifyFormat("[]() -> a<1> {};");
22052 verifyFormat("[]() -> a<1> { ; };");
22053 verifyFormat("[]() -> a<1> { ; }();");
22054 verifyFormat("[a, a]() -> a<true> {};");
22055 verifyFormat("[]() -> a<true> {};");
22056 verifyFormat("[]() -> a<true> { ; };");
22057 verifyFormat("[]() -> a<true> { ; }();");
22058 verifyFormat("[a, a]() -> a<false> {};");
22059 verifyFormat("[]() -> a<false> {};");
22060 verifyFormat("[]() -> a<false> { ; };");
22061 verifyFormat("[]() -> a<false> { ; }();");
22062 verifyFormat("auto foo{[]() -> foo<false> { ; }};");
22063 verifyFormat("namespace bar {\n"
22064 "auto foo{[]() -> foo<false> { ; }};\n"
22065 "} // namespace bar");
22066 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
22067 " int j) -> int {\n"
22068 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
22071 "aaaaaaaaaaaaaaaaaaaaaa(\n"
22072 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
22073 " return aaaaaaaaaaaaaaaaa;\n"
22075 getLLVMStyleWithColumns(70));
22076 verifyFormat("[]() //\n"
22080 verifyFormat("[]() -> Void<T...> {};");
22081 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };");
22082 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});");
22083 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
22084 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
22085 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
22086 verifyFormat("return int{[x = x]() { return x; }()};");
22088 // Lambdas with explicit template argument lists.
22090 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};");
22091 verifyFormat("auto L = []<class T>(T) {\n"
22097 verifyFormat("auto L = []<class... T>(T...) {\n"
22103 verifyFormat("auto L = []<typename... T>(T...) {\n"
22109 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n"
22115 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n"
22121 verifyFormat("auto L = []<int... T>(T...) {\n"
22127 verifyFormat("auto L = []<Foo... T>(T...) {\n"
22134 // Lambdas that fit on a single line within an argument list are not forced
22136 verifyFormat("SomeFunction([] {});");
22137 verifyFormat("SomeFunction(0, [] {});");
22138 verifyFormat("SomeFunction([] {}, 0);");
22139 verifyFormat("SomeFunction(0, [] {}, 0);");
22140 verifyFormat("SomeFunction([] { return 0; }, 0);");
22141 verifyFormat("SomeFunction(a, [] { return 0; }, b);");
22142 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; });");
22143 verifyFormat("SomeFunction([] { return 0; }, [] { return 0; }, b);");
22144 verifyFormat("auto loooooooooooooooooooooooooooong =\n"
22145 " SomeFunction([] { return 0; }, [] { return 0; }, b);");
22146 // Exceeded column limit. We need to break.
22147 verifyFormat("auto loooooooooooooooooooooooooooongName = SomeFunction(\n"
22148 " [] { return anotherLooooooooooonoooooooongName; }, [] { "
22149 "return 0; }, b);");
22151 // Multiple multi-line lambdas in the same parentheses change indentation
22152 // rules. These lambdas are always forced to start on new lines.
22153 verifyFormat("SomeFunction(\n"
22161 // A multi-line lambda passed as arg0 is always pushed to the next line.
22162 verifyFormat("SomeFunction(\n"
22168 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like
22169 // the arg0 case above.
22170 auto Style
= getGoogleStyle();
22171 Style
.BinPackArguments
= false;
22172 verifyFormat("SomeFunction(\n"
22179 verifyFormat("SomeFunction(\n"
22186 // A lambda with a very long line forces arg0 to be pushed out irrespective of
22187 // the BinPackArguments value (as long as the code is wide enough).
22189 "something->SomeFunction(\n"
22193 "D0000000000000000000000000000000000000000000000000000000000001();\n"
22197 // A multi-line lambda is pulled up as long as the introducer fits on the
22198 // previous line and there are no further args.
22199 verifyFormat("function(1, [this, that] {\n"
22202 verifyFormat("function([this, that] {\n"
22205 // FIXME: this format is not ideal and we should consider forcing the first
22206 // arg onto its own line.
22207 verifyFormat("function(a, b, c, //\n"
22208 " d, [this, that] {\n"
22212 // Multiple lambdas are treated correctly even when there is a short arg0.
22213 verifyFormat("SomeFunction(\n"
22223 // More complex introducers.
22224 verifyFormat("return [i, args...] {};");
22227 verifyFormat("constexpr char hello[]{\"hello\"};");
22228 verifyFormat("double &operator[](int i) { return 0; }\n"
22230 verifyFormat("std::unique_ptr<int[]> foo() {}");
22231 verifyFormat("int i = a[a][a]->f();");
22232 verifyFormat("int i = (*b)[a]->f();");
22234 // Other corner cases.
22235 verifyFormat("void f() {\n"
22236 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
22239 verifyFormat("auto k = *[](int *j) { return j; }(&i);");
22241 // Lambdas created through weird macros.
22242 verifyFormat("void f() {\n"
22243 " MACRO((const AA &a) { return 1; });\n"
22244 " MACRO((AA &a) { return 1; });\n"
22247 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
22252 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
22257 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n"
22262 verifyFormat("auto lambda = []() {\n"
22270 // Lambdas with complex multiline introducers.
22272 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
22273 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
22274 " -> ::std::unordered_set<\n"
22275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
22279 FormatStyle DoNotMerge
= getLLVMStyle();
22280 DoNotMerge
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_None
;
22281 verifyFormat("auto c = []() {\n"
22284 "auto c = []() { return b; };", DoNotMerge
);
22285 verifyFormat("auto c = []() {\n"
22287 " auto c = []() {};", DoNotMerge
);
22289 FormatStyle MergeEmptyOnly
= getLLVMStyle();
22290 MergeEmptyOnly
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_Empty
;
22291 verifyFormat("auto c = []() {\n"
22294 "auto c = []() {\n"
22298 verifyFormat("auto c = []() {};",
22299 "auto c = []() {\n"
22303 FormatStyle MergeInline
= getLLVMStyle();
22304 MergeInline
.AllowShortLambdasOnASingleLine
= FormatStyle::SLS_Inline
;
22305 verifyFormat("auto c = []() {\n"
22308 "auto c = []() { return b; };", MergeInline
);
22309 verifyFormat("function([]() { return b; })", MergeInline
);
22310 verifyFormat("function([]() { return b; }, a)", MergeInline
);
22311 verifyFormat("function(a, []() { return b; })", MergeInline
);
22313 // Check option "BraceWrapping.BeforeLambdaBody" and different state of
22314 // AllowShortLambdasOnASingleLine
22315 FormatStyle LLVMWithBeforeLambdaBody
= getLLVMStyle();
22316 LLVMWithBeforeLambdaBody
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
22317 LLVMWithBeforeLambdaBody
.BraceWrapping
.BeforeLambdaBody
= true;
22318 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
22319 FormatStyle::ShortLambdaStyle::SLS_None
;
22320 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n"
22325 LLVMWithBeforeLambdaBody
);
22326 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n"
22330 LLVMWithBeforeLambdaBody
);
22331 verifyFormat("auto fct_SLS_None = []()\n"
22335 LLVMWithBeforeLambdaBody
);
22336 verifyFormat("TwoNestedLambdas_SLS_None(\n"
22345 LLVMWithBeforeLambdaBody
);
22346 verifyFormat("void Fct() {\n"
22352 LLVMWithBeforeLambdaBody
);
22354 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
22355 FormatStyle::ShortLambdaStyle::SLS_Empty
;
22356 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"
22361 LLVMWithBeforeLambdaBody
);
22362 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});",
22363 LLVMWithBeforeLambdaBody
);
22364 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL"
22365 "ongFunctionName_SLS_Empty(\n"
22367 LLVMWithBeforeLambdaBody
);
22368 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n"
22373 LLVMWithBeforeLambdaBody
);
22374 verifyFormat("auto fct_SLS_Empty = []()\n"
22378 LLVMWithBeforeLambdaBody
);
22379 verifyFormat("TwoNestedLambdas_SLS_Empty(\n"
22382 " return Call([]() {});\n"
22384 LLVMWithBeforeLambdaBody
);
22385 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n"
22388 " return Call([]() {});\n"
22390 LLVMWithBeforeLambdaBody
);
22392 "FctWithLongLineInLambda_SLS_Empty(\n"
22395 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
22396 " AndShouldNotBeConsiderAsInline,\n"
22397 " LambdaBodyMustBeBreak);\n"
22399 LLVMWithBeforeLambdaBody
);
22401 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
22402 FormatStyle::ShortLambdaStyle::SLS_Inline
;
22403 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });",
22404 LLVMWithBeforeLambdaBody
);
22405 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});",
22406 LLVMWithBeforeLambdaBody
);
22407 verifyFormat("auto fct_SLS_Inline = []()\n"
22411 LLVMWithBeforeLambdaBody
);
22412 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return "
22414 LLVMWithBeforeLambdaBody
);
22416 "FctWithLongLineInLambda_SLS_Inline(\n"
22419 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
22420 " AndShouldNotBeConsiderAsInline,\n"
22421 " LambdaBodyMustBeBreak);\n"
22423 LLVMWithBeforeLambdaBody
);
22424 verifyFormat("FctWithMultipleParams_SLS_Inline("
22425 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
22426 " []() { return 17; });",
22427 LLVMWithBeforeLambdaBody
);
22429 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });",
22430 LLVMWithBeforeLambdaBody
);
22432 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
22433 FormatStyle::ShortLambdaStyle::SLS_All
;
22434 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });",
22435 LLVMWithBeforeLambdaBody
);
22436 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});",
22437 LLVMWithBeforeLambdaBody
);
22438 verifyFormat("auto fct_SLS_All = []() { return 17; };",
22439 LLVMWithBeforeLambdaBody
);
22440 verifyFormat("FctWithOneParam_SLS_All(\n"
22443 " // A cool function...\n"
22446 LLVMWithBeforeLambdaBody
);
22447 verifyFormat("FctWithMultipleParams_SLS_All("
22448 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n"
22449 " []() { return 17; });",
22450 LLVMWithBeforeLambdaBody
);
22451 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });",
22452 LLVMWithBeforeLambdaBody
);
22453 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });",
22454 LLVMWithBeforeLambdaBody
);
22456 "FctWithLongLineInLambda_SLS_All(\n"
22459 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
22460 " AndShouldNotBeConsiderAsInline,\n"
22461 " LambdaBodyMustBeBreak);\n"
22463 LLVMWithBeforeLambdaBody
);
22465 "auto fct_SLS_All = []()\n"
22467 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
22468 " AndShouldNotBeConsiderAsInline,\n"
22469 " LambdaBodyMustBeBreak);\n"
22471 LLVMWithBeforeLambdaBody
);
22472 LLVMWithBeforeLambdaBody
.BinPackParameters
= false;
22473 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);",
22474 LLVMWithBeforeLambdaBody
);
22476 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n"
22481 LLVMWithBeforeLambdaBody
);
22482 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
22484 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n"
22489 LLVMWithBeforeLambdaBody
);
22491 "FctWithLongLineInLambda_SLS_All(FirstParam,\n"
22495 " []() { return SomeValueNotSoLong; });",
22496 LLVMWithBeforeLambdaBody
);
22497 verifyFormat("FctWithLongLineInLambda_SLS_All(\n"
22501 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB"
22502 "eConsiderAsInline;\n"
22504 LLVMWithBeforeLambdaBody
);
22506 "FctWithLongLineInLambda_SLS_All(\n"
22509 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n"
22510 " AndShouldNotBeConsiderAsInline,\n"
22511 " LambdaBodyMustBeBreak);\n"
22513 LLVMWithBeforeLambdaBody
);
22514 verifyFormat("FctWithTwoParams_SLS_All(\n"
22517 " // A cool function...\n"
22521 LLVMWithBeforeLambdaBody
);
22522 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
22523 LLVMWithBeforeLambdaBody
);
22524 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
22525 LLVMWithBeforeLambdaBody
);
22527 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });",
22528 LLVMWithBeforeLambdaBody
);
22529 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; "
22531 LLVMWithBeforeLambdaBody
);
22532 verifyFormat("TwoNestedLambdas_SLS_All(\n"
22535 " // A cool function...\n"
22536 " return Call([]() { return 17; });\n"
22538 LLVMWithBeforeLambdaBody
);
22539 verifyFormat("TwoNestedLambdas_SLS_All(\n"
22545 " // A cool function...\n"
22549 LLVMWithBeforeLambdaBody
);
22551 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
22552 FormatStyle::ShortLambdaStyle::SLS_None
;
22554 verifyFormat("auto select = [this]() -> const Library::Object *\n"
22556 " return MyAssignment::SelectFromList(this);\n"
22558 LLVMWithBeforeLambdaBody
);
22560 verifyFormat("auto select = [this]() -> const Library::Object &\n"
22562 " return MyAssignment::SelectFromList(this);\n"
22564 LLVMWithBeforeLambdaBody
);
22566 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n"
22568 " return MyAssignment::SelectFromList(this);\n"
22570 LLVMWithBeforeLambdaBody
);
22572 verifyFormat("namespace test {\n"
22575 " Test() = default;\n"
22577 "} // namespace test",
22578 LLVMWithBeforeLambdaBody
);
22580 // Lambdas with different indentation styles.
22581 Style
= getLLVMStyleWithColumns(60);
22582 verifyFormat("Result doSomething(Promise promise) {\n"
22583 " return promise.then(\n"
22584 " [this, obj = std::move(s)](int bar) mutable {\n"
22585 " return someObject.startAsyncAction().then(\n"
22586 " [this, &obj](Result result) mutable {\n"
22587 " result.processMore();\n"
22592 Style
.LambdaBodyIndentation
= FormatStyle::LBI_OuterScope
;
22593 verifyFormat("Result doSomething(Promise promise) {\n"
22594 " return promise.then(\n"
22595 " [this, obj = std::move(s)](int bar) mutable {\n"
22596 " return obj.startAsyncAction().then(\n"
22597 " [this, &obj](Result result) mutable {\n"
22598 " result.processMore();\n"
22603 verifyFormat("Result doSomething(Promise promise) {\n"
22604 " return promise.then([this, obj = std::move(s)] {\n"
22605 " return obj.startAsyncAction().then(\n"
22606 " [this, &obj](Result result) mutable {\n"
22607 " result.processMore();\n"
22612 verifyFormat("void test() {\n"
22613 " ([]() -> auto {\n"
22619 verifyFormat("void test() {\n"
22620 " []() -> auto {\n"
22626 verifyFormat("void test() {\n"
22627 " std::sort(v.begin(), v.end(),\n"
22628 " [](const auto &foo, const auto &bar) {\n"
22629 " return foo.baz < bar.baz;\n"
22633 verifyFormat("void test() {\n"
22635 " []() -> auto {\n"
22642 verifyFormat("void test() {\n"
22643 " ([]() -> auto {\n"
22651 verifyFormat("#define A \\\n"
22653 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
22654 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
22657 verifyFormat("#define SORT(v) \\\n"
22658 " std::sort(v.begin(), v.end(), \\\n"
22659 " [](const auto &foo, const auto &bar) { \\\n"
22660 " return foo.baz < bar.baz; \\\n"
22663 verifyFormat("void foo() {\n"
22664 " aFunction(1, b(c(foo, bar, baz, [](d) {\n"
22665 " auto f = e(d);\n"
22670 verifyFormat("void foo() {\n"
22671 " aFunction(1, b(c(foo, Bar{}, baz, [](d) -> Foo {\n"
22672 " auto f = e(foo, [&] {\n"
22675 " }, qux, [&] -> Bar {\n"
22683 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
22684 " AnotherLongClassName baz)\n"
22685 " : baz{baz}, func{[&] {\n"
22686 " auto qux = bar;\n"
22687 " return aFunkyFunctionCall(qux);\n"
22690 verifyFormat("void foo() {\n"
22694 " : qux{[](int quux) {\n"
22695 " auto tmp = quux;\n"
22700 " std::function<void(int quux)> qux;\n"
22704 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_AfterColon
;
22705 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
22706 " AnotherLongClassName baz) :\n"
22707 " baz{baz}, func{[&] {\n"
22708 " auto qux = bar;\n"
22709 " return aFunkyFunctionCall(qux);\n"
22712 Style
.PackConstructorInitializers
= FormatStyle::PCIS_Never
;
22713 verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
22714 " AnotherLongClassName baz) :\n"
22717 " auto qux = bar;\n"
22718 " return aFunkyFunctionCall(qux);\n"
22721 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_AlwaysBreak
;
22722 // FIXME: The following test should pass, but fails at the time of writing.
22724 // As long as all the non-lambda arguments fit on a single line, AlwaysBreak
22725 // doesn't force an initial line break, even if lambdas span multiple lines.
22726 verifyFormat("void foo() {\n"
22728 " [](d) -> Foo {\n"
22729 " auto f = e(d);\n"
22731 " }, foo, Bar{}, [] {\n"
22738 // A long non-lambda argument forces arguments to span multiple lines and thus
22739 // forces an initial line break when using AlwaysBreak.
22740 verifyFormat("void foo() {\n"
22743 " [](d) -> Foo {\n"
22744 " auto f = e(d);\n"
22746 " }, foo, Bar{},\n"
22751 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
22754 Style
.BinPackArguments
= false;
22755 verifyFormat("void foo() {\n"
22758 " [](d) -> Foo {\n"
22759 " auto f = e(d);\n"
22769 " quuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux);\n"
22772 Style
.BinPackArguments
= true;
22773 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
22774 Style
.BraceWrapping
.BeforeLambdaBody
= true;
22775 verifyFormat("void foo() {\n"
22777 " 1, b(c(foo, Bar{}, baz, [](d) -> Foo\n"
22784 " }, qux, [&] -> Bar\n"
22795 TEST_F(FormatTest
, LambdaWithLineComments
) {
22796 FormatStyle LLVMWithBeforeLambdaBody
= getLLVMStyle();
22797 LLVMWithBeforeLambdaBody
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
22798 LLVMWithBeforeLambdaBody
.BraceWrapping
.BeforeLambdaBody
= true;
22799 LLVMWithBeforeLambdaBody
.AllowShortLambdasOnASingleLine
=
22800 FormatStyle::ShortLambdaStyle::SLS_All
;
22802 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody
);
22803 verifyFormat("auto k = []() // comment\n"
22805 LLVMWithBeforeLambdaBody
);
22806 verifyFormat("auto k = []() /* comment */ { return; }",
22807 LLVMWithBeforeLambdaBody
);
22808 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
22809 LLVMWithBeforeLambdaBody
);
22810 verifyFormat("auto k = []() // X\n"
22812 LLVMWithBeforeLambdaBody
);
22814 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
22816 LLVMWithBeforeLambdaBody
);
22818 LLVMWithBeforeLambdaBody
.ColumnLimit
= 0;
22820 verifyFormat("foo([]()\n"
22823 " return 1; // comment\n"
22827 " return 1; // comment\n"
22829 LLVMWithBeforeLambdaBody
);
22830 verifyFormat("foo(\n"
22833 " bar(); // comment\n"
22837 " 1, MACRO { baz(); bar(); // comment\n"
22840 LLVMWithBeforeLambdaBody
);
22843 TEST_F(FormatTest
, EmptyLinesInLambdas
) {
22844 verifyFormat("auto lambda = []() {\n"
22847 "auto lambda = []() {\n"
22854 TEST_F(FormatTest
, FormatsBlocks
) {
22855 FormatStyle ShortBlocks
= getLLVMStyle();
22856 ShortBlocks
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Always
;
22857 verifyFormat("int (^Block)(int, int);", ShortBlocks
);
22858 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks
);
22859 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks
);
22860 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks
);
22861 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks
);
22862 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks
);
22864 verifyFormat("foo(^{ bar(); });", ShortBlocks
);
22865 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks
);
22866 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks
);
22868 verifyFormat("[operation setCompletionBlock:^{\n"
22869 " [self onOperationDone];\n"
22871 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
22872 " [self onOperationDone];\n"
22874 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
22877 verifyFormat("int a = [operation block:^int(int *i) {\n"
22880 verifyFormat("[myObject doSomethingWith:arg1\n"
22881 " aaa:^int(int *a) {\n"
22884 " bbb:f(a * bbbbbbbb)];");
22886 verifyFormat("[operation setCompletionBlock:^{\n"
22887 " [self.delegate newDataAvailable];\n"
22889 getLLVMStyleWithColumns(60));
22890 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
22891 " NSString *path = [self sessionFilePath];\n"
22896 verifyFormat("[[SessionService sharedService]\n"
22897 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
22899 " [self windowDidLoad:window];\n"
22901 " [self errorLoadingWindow];\n"
22904 verifyFormat("void (^largeBlock)(void) = ^{\n"
22907 getLLVMStyleWithColumns(40));
22908 verifyFormat("[[SessionService sharedService]\n"
22909 " loadWindowWithCompletionBlock: //\n"
22910 " ^(SessionWindow *window) {\n"
22912 " [self windowDidLoad:window];\n"
22914 " [self errorLoadingWindow];\n"
22917 getLLVMStyleWithColumns(60));
22918 verifyFormat("[myObject doSomethingWith:arg1\n"
22919 " firstBlock:^(Foo *a) {\n"
22923 " secondBlock:^(Bar *b) {\n"
22927 " thirdBlock:^Foo(Bar *b) {\n"
22931 verifyFormat("[myObject doSomethingWith:arg1\n"
22933 " secondBlock:^(Bar *b) {\n"
22938 verifyFormat("f(^{\n"
22939 " @autoreleasepool {\n"
22945 verifyFormat("Block b = ^int *(A *a, B *b) {\n"
22947 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
22950 FormatStyle FourIndent
= getLLVMStyle();
22951 FourIndent
.ObjCBlockIndentWidth
= 4;
22952 verifyFormat("[operation setCompletionBlock:^{\n"
22953 " [self onOperationDone];\n"
22958 TEST_F(FormatTest
, FormatsBlocksWithZeroColumnWidth
) {
22959 FormatStyle ZeroColumn
= getLLVMStyleWithColumns(0);
22961 verifyFormat("[[SessionService sharedService] "
22962 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
22964 " [self windowDidLoad:window];\n"
22966 " [self errorLoadingWindow];\n"
22970 verifyFormat("[[SessionService sharedService]\n"
22971 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
22973 " [self windowDidLoad:window];\n"
22975 " [self errorLoadingWindow];\n"
22978 "[[SessionService sharedService]\n"
22979 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
22981 " [self windowDidLoad:window];\n"
22983 " [self errorLoadingWindow];\n"
22987 verifyFormat("[myObject doSomethingWith:arg1\n"
22988 " firstBlock:^(Foo *a) {\n"
22992 " secondBlock:^(Bar *b) {\n"
22996 " thirdBlock:^Foo(Bar *b) {\n"
23001 verifyFormat("f(^{\n"
23002 " @autoreleasepool {\n"
23009 verifyFormat("void (^largeBlock)(void) = ^{\n"
23014 ZeroColumn
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Always
;
23015 verifyFormat("void (^largeBlock)(void) = ^{ int i; };",
23016 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn
);
23017 ZeroColumn
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Never
;
23018 verifyFormat("void (^largeBlock)(void) = ^{\n"
23021 "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn
);
23024 TEST_F(FormatTest
, SupportsCRLF
) {
23025 verifyFormat("int a;\r\n"
23031 verifyFormat("int a;\r\n"
23037 verifyFormat("int a;\n"
23043 // FIXME: unstable test case
23044 EXPECT_EQ("\"aaaaaaa \"\r\n"
23045 "\"bbbbbbb\";\r\n",
23046 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
23047 verifyFormat("#define A \\\r\n"
23056 verifyNoChange("/*\r\n"
23057 "multi line block comments\r\n"
23058 "should not introduce\r\n"
23059 "an extra carriage return\r\n"
23061 verifyFormat("/*\r\n"
23068 FormatStyle style
= getLLVMStyle();
23070 EXPECT_EQ(style
.LineEnding
, FormatStyle::LE_DeriveLF
);
23071 verifyFormat("union FooBarBazQux {\n"
23076 "union FooBarBazQux {\r\n"
23082 style
.LineEnding
= FormatStyle::LE_DeriveCRLF
;
23083 verifyFormat("union FooBarBazQux {\r\n"
23088 "union FooBarBazQux {\r\n"
23095 style
.LineEnding
= FormatStyle::LE_LF
;
23096 verifyFormat("union FooBarBazQux {\n"
23102 "union FooBarBazQux {\r\n"
23109 style
.LineEnding
= FormatStyle::LE_CRLF
;
23110 verifyFormat("union FooBarBazQux {\r\n"
23116 "union FooBarBazQux {\r\n"
23124 style
.LineEnding
= FormatStyle::LE_DeriveLF
;
23125 verifyFormat("union FooBarBazQux {\r\n"
23131 "union FooBarBazQux {\r\n"
23138 style
.LineEnding
= FormatStyle::LE_DeriveCRLF
;
23139 verifyFormat("union FooBarBazQux {\n"
23145 "union FooBarBazQux {\r\n"
23154 TEST_F(FormatTest
, MunchSemicolonAfterBlocks
) {
23155 verifyFormat("MY_CLASS(C) {\n"
23161 TEST_F(FormatTest
, ConfigurableContinuationIndentWidth
) {
23162 FormatStyle TwoIndent
= getLLVMStyleWithColumns(15);
23163 TwoIndent
.ContinuationIndentWidth
= 2;
23165 verifyFormat("int i =\n"
23168 "int i = longFunction(arg);", TwoIndent
);
23170 FormatStyle SixIndent
= getLLVMStyleWithColumns(20);
23171 SixIndent
.ContinuationIndentWidth
= 6;
23173 verifyFormat("int i =\n"
23176 "int i = longFunction(arg);", SixIndent
);
23179 TEST_F(FormatTest
, WrappedClosingParenthesisIndent
) {
23180 FormatStyle Style
= getLLVMStyle();
23181 verifyFormat("int Foo::getter(\n"
23187 verifyFormat("void Foo::setter(\n"
23195 TEST_F(FormatTest
, SpacesInAngles
) {
23196 FormatStyle Spaces
= getLLVMStyle();
23197 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
23199 verifyFormat("vector< ::std::string > x1;", Spaces
);
23200 verifyFormat("Foo< int, Bar > x2;", Spaces
);
23201 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces
);
23203 verifyFormat("static_cast< int >(arg);", Spaces
);
23204 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces
);
23205 verifyFormat("f< int, float >();", Spaces
);
23206 verifyFormat("template <> g() {}", Spaces
);
23207 verifyFormat("template < std::vector< int > > f() {}", Spaces
);
23208 verifyFormat("std::function< void(int, int) > fct;", Spaces
);
23209 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
23212 Spaces
.Standard
= FormatStyle::LS_Cpp03
;
23213 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
23214 verifyFormat("A< A< int > >();", Spaces
);
23216 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Never
;
23217 verifyFormat("A<A<int> >();", Spaces
);
23219 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Leave
;
23220 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;",
23222 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;",
23225 verifyFormat("A<A<int> >();", Spaces
);
23226 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces
);
23227 verifyFormat("A< A< int > >();", Spaces
);
23229 Spaces
.Standard
= FormatStyle::LS_Cpp11
;
23230 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
23231 verifyFormat("A< A< int > >();", Spaces
);
23233 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Never
;
23234 verifyFormat("vector<::std::string> x4;", Spaces
);
23235 verifyFormat("vector<int> x5;", Spaces
);
23236 verifyFormat("Foo<int, Bar> x6;", Spaces
);
23237 verifyFormat("Foo<::int, ::Bar> x7;", Spaces
);
23239 verifyFormat("A<A<int>>();", Spaces
);
23241 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Leave
;
23242 verifyFormat("vector<::std::string> x4;", Spaces
);
23243 verifyFormat("vector< ::std::string > x4;", Spaces
);
23244 verifyFormat("vector<int> x5;", Spaces
);
23245 verifyFormat("vector< int > x5;", Spaces
);
23246 verifyFormat("Foo<int, Bar> x6;", Spaces
);
23247 verifyFormat("Foo< int, Bar > x6;", Spaces
);
23248 verifyFormat("Foo<::int, ::Bar> x7;", Spaces
);
23249 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces
);
23251 verifyFormat("A<A<int>>();", Spaces
);
23252 verifyFormat("A< A< int > >();", Spaces
);
23253 verifyFormat("A<A<int > >();", Spaces
);
23254 verifyFormat("A< A< int>>();", Spaces
);
23256 Spaces
.SpacesInAngles
= FormatStyle::SIAS_Always
;
23257 verifyFormat("// clang-format off\n"
23258 "foo<<<1, 1>>>();\n"
23259 "// clang-format on",
23261 verifyFormat("// clang-format off\n"
23262 "foo< < <1, 1> > >();\n"
23263 "// clang-format on",
23267 TEST_F(FormatTest
, SpaceAfterTemplateKeyword
) {
23268 FormatStyle Style
= getLLVMStyle();
23269 Style
.SpaceAfterTemplateKeyword
= false;
23270 verifyFormat("template<int> void foo();", Style
);
23273 TEST_F(FormatTest
, TripleAngleBrackets
) {
23274 verifyFormat("f<<<1, 1>>>();");
23275 verifyFormat("f<<<1, 1, 1, s>>>();");
23276 verifyFormat("f<<<a, b, c, d>>>();");
23277 verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();");
23278 verifyFormat("f<param><<<1, 1>>>();");
23279 verifyFormat("f<1><<<1, 1>>>();");
23280 verifyFormat("f<param><<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();");
23281 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23282 "aaaaaaaaaaa<<<\n 1, 1>>>();");
23283 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
23284 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
23287 TEST_F(FormatTest
, MergeLessLessAtEnd
) {
23288 verifyFormat("<<");
23289 verifyFormat("< < <", "\\\n<<<");
23290 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23291 "aaallvm::outs() <<");
23292 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23293 "aaaallvm::outs()\n <<");
23296 TEST_F(FormatTest
, HandleUnbalancedImplicitBracesAcrossPPBranches
) {
23297 std::string code
= "#if A\n"
23307 verifyFormat(code
);
23310 TEST_F(FormatTest
, HandleConflictMarkers
) {
23311 // Git/SVN conflict markers.
23312 verifyFormat("int a;\n"
23314 " callme(some(parameter1,\n"
23315 "<<<<<<< text by the vcs\n"
23317 "||||||| text by the vcs\n"
23320 "======= text by the vcs\n"
23321 " parameter2, parameter3),\n"
23322 ">>>>>>> text by the vcs\n"
23323 " otherparameter);",
23326 " callme(some(parameter1,\n"
23327 "<<<<<<< text by the vcs\n"
23329 "||||||| text by the vcs\n"
23332 "======= text by the vcs\n"
23335 ">>>>>>> text by the vcs\n"
23336 " otherparameter);");
23338 // Perforce markers.
23339 verifyFormat("void f() {\n"
23341 ">>>> text by the vcs\n"
23343 "==== text by the vcs\n"
23345 "==== text by the vcs\n"
23347 "<<<< text by the vcs\n"
23351 ">>>> text by the vcs\n"
23353 "==== text by the vcs\n"
23355 "==== text by the vcs\n"
23357 "<<<< text by the vcs\n"
23360 verifyNoChange("<<<<<<<\n"
23365 verifyNoChange("<<<<<<<\n"
23371 // FIXME: Handle parsing of macros around conflict markers correctly:
23372 verifyFormat("#define Macro \\\n"
23381 "#define Macro \\\n"
23392 verifyFormat(R
"(====
23401 TEST_F(FormatTest
, DisableRegions
) {
23402 verifyFormat("int i;\n"
23403 "// clang-format off\n"
23405 "// clang-format on\n"
23408 " // clang-format off\n"
23410 " // clang-format on\n"
23412 verifyFormat("int i;\n"
23413 "/* clang-format off */\n"
23415 "/* clang-format on */\n"
23418 " /* clang-format off */\n"
23420 " /* clang-format on */\n"
23423 // Don't reflow comments within disabled regions.
23424 verifyFormat("// clang-format off\n"
23425 "// long long long long long long line\n"
23426 "/* clang-format on */\n"
23427 "/* long long long\n"
23428 " * long long long\n"
23431 "/* clang-format off */\n"
23432 "/* long long long long long long line */",
23433 "// clang-format off\n"
23434 "// long long long long long long line\n"
23435 "/* clang-format on */\n"
23436 "/* long long long long long long line */\n"
23438 "/* clang-format off */\n"
23439 "/* long long long long long long line */",
23440 getLLVMStyleWithColumns(20));
23442 verifyFormat("int *i;\n"
23443 "// clang-format off:\n"
23445 "// clang-format on: 1\n"
23448 "// clang-format off:\n"
23450 "// clang-format on: 1\n"
23453 verifyFormat("int *i;\n"
23454 "// clang-format off:0\n"
23456 "// clang-format only\n"
23459 "// clang-format off:0\n"
23461 "// clang-format only\n"
23464 verifyNoChange("// clang-format off\n"
23466 " #if SHOULD_STAY_INDENTED\n"
23469 "// clang-format on");
23472 TEST_F(FormatTest
, DoNotCrashOnInvalidInput
) {
23474 verifyNoCrash("#define a\\\n /**/}");
23477 TEST_F(FormatTest
, FormatsTableGenCode
) {
23478 FormatStyle Style
= getLLVMStyle();
23479 Style
.Language
= FormatStyle::LK_TableGen
;
23480 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style
);
23483 TEST_F(FormatTest
, ArrayOfTemplates
) {
23484 verifyFormat("auto a = new unique_ptr<int>[10];",
23485 "auto a = new unique_ptr<int > [ 10];");
23487 FormatStyle Spaces
= getLLVMStyle();
23488 Spaces
.SpacesInSquareBrackets
= true;
23489 verifyFormat("auto a = new unique_ptr<int>[ 10 ];",
23490 "auto a = new unique_ptr<int > [10];", Spaces
);
23493 TEST_F(FormatTest
, ArrayAsTemplateType
) {
23494 verifyFormat("auto a = unique_ptr<Foo<Bar>[10]>;",
23495 "auto a = unique_ptr < Foo < Bar>[ 10]> ;");
23497 FormatStyle Spaces
= getLLVMStyle();
23498 Spaces
.SpacesInSquareBrackets
= true;
23499 verifyFormat("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
23500 "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces
);
23503 TEST_F(FormatTest
, NoSpaceAfterSuper
) { verifyFormat("__super::FooBar();"); }
23505 TEST_F(FormatTest
, FormatSortsUsingDeclarations
) {
23506 verifyFormat("using std::cin;\n"
23507 "using std::cout;",
23508 "using std::cout;\n"
23513 TEST_F(FormatTest
, UTF8CharacterLiteralCpp03
) {
23514 FormatStyle Style
= getLLVMStyle();
23515 Style
.Standard
= FormatStyle::LS_Cpp03
;
23516 // cpp03 recognize this string as identifier u8 and literal character 'a'
23517 verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style
);
23520 TEST_F(FormatTest
, UTF8CharacterLiteralCpp11
) {
23521 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
23522 // all modes, including C++11, C++14 and C++17
23523 verifyFormat("auto c = u8'a';");
23526 TEST_F(FormatTest
, DoNotFormatLikelyXml
) {
23527 verifyGoogleFormat("<!-- ;> -->");
23528 verifyNoChange(" <!-- >; -->", getGoogleStyle());
23531 TEST_F(FormatTest
, StructuredBindings
) {
23532 // Structured bindings is a C++17 feature.
23533 // all modes, including C++11, C++14 and C++17
23534 verifyFormat("auto [a, b] = f();");
23535 verifyFormat("auto [a, b] = f();", "auto[a, b] = f();");
23536 verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();");
23537 verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();");
23538 verifyFormat("auto const volatile [a, b] = f();",
23539 "auto const volatile[a, b] = f();");
23540 verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();");
23541 verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();");
23542 verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();");
23543 verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();");
23544 verifyFormat("auto const volatile &&[a, b] = f();",
23545 "auto const volatile &&[a, b] = f();");
23546 verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();");
23547 verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();");
23548 verifyFormat("const auto volatile &&[a, b] = f();",
23549 "const auto volatile &&[a, b] = f();");
23550 verifyFormat("volatile const auto &&[a, b] = f();",
23551 "volatile const auto &&[a, b] = f();");
23552 verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();");
23554 // Make sure we don't mistake structured bindings for lambdas.
23555 FormatStyle PointerMiddle
= getLLVMStyle();
23556 PointerMiddle
.PointerAlignment
= FormatStyle::PAS_Middle
;
23557 verifyGoogleFormat("auto [a1, b]{A * i};");
23558 verifyFormat("auto [a2, b]{A * i};");
23559 verifyFormat("auto [a3, b]{A * i};", PointerMiddle
);
23560 verifyGoogleFormat("auto const [a1, b]{A * i};");
23561 verifyFormat("auto const [a2, b]{A * i};");
23562 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle
);
23563 verifyGoogleFormat("auto const& [a1, b]{A * i};");
23564 verifyFormat("auto const &[a2, b]{A * i};");
23565 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle
);
23566 verifyGoogleFormat("auto const&& [a1, b]{A * i};");
23567 verifyFormat("auto const &&[a2, b]{A * i};");
23568 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle
);
23570 verifyFormat("for (const auto &&[a, b] : some_range) {\n}",
23571 "for (const auto && [a, b] : some_range) {\n}");
23572 verifyFormat("for (const auto &[a, b] : some_range) {\n}",
23573 "for (const auto & [a, b] : some_range) {\n}");
23574 verifyFormat("for (const auto [a, b] : some_range) {\n}",
23575 "for (const auto[a, b] : some_range) {\n}");
23576 verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);");
23577 verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);");
23578 verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);");
23579 verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);");
23580 verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);");
23581 verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};");
23582 verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};");
23583 verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};");
23585 FormatStyle Spaces
= getLLVMStyle();
23586 Spaces
.SpacesInSquareBrackets
= true;
23587 verifyFormat("auto [ a, b ] = f();", Spaces
);
23588 verifyFormat("auto &&[ a, b ] = f();", Spaces
);
23589 verifyFormat("auto &[ a, b ] = f();", Spaces
);
23590 verifyFormat("auto const &&[ a, b ] = f();", Spaces
);
23591 verifyFormat("auto const &[ a, b ] = f();", Spaces
);
23594 TEST_F(FormatTest
, FileAndCode
) {
23595 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.cc", ""));
23596 EXPECT_EQ(FormatStyle::LK_ObjC
, guessLanguage("foo.m", ""));
23597 EXPECT_EQ(FormatStyle::LK_ObjC
, guessLanguage("foo.mm", ""));
23598 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", ""));
23599 EXPECT_EQ(FormatStyle::LK_ObjC
,
23600 guessLanguage("foo.h", "@interface Foo\n@end"));
23602 FormatStyle::LK_ObjC
,
23603 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
23604 EXPECT_EQ(FormatStyle::LK_ObjC
,
23605 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
23606 EXPECT_EQ(FormatStyle::LK_ObjC
, guessLanguage("foo.h", "@class Foo;"));
23607 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo", ""));
23608 EXPECT_EQ(FormatStyle::LK_ObjC
, guessLanguage("foo", "@interface Foo\n@end"));
23609 EXPECT_EQ(FormatStyle::LK_ObjC
,
23610 guessLanguage("foo.h", "int DoStuff(CGRect rect);"));
23611 EXPECT_EQ(FormatStyle::LK_ObjC
,
23613 "foo.h", "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));"));
23615 FormatStyle::LK_Cpp
,
23616 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;"));
23617 // Only one of the two preprocessor regions has ObjC-like code.
23618 EXPECT_EQ(FormatStyle::LK_ObjC
,
23619 guessLanguage("foo.h", "#if A\n"
23622 "#define B() [NSString a:@\"\"]\n"
23626 TEST_F(FormatTest
, GuessLanguageWithCpp11AttributeSpecifiers
) {
23627 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", "[[noreturn]];"));
23628 EXPECT_EQ(FormatStyle::LK_ObjC
,
23629 guessLanguage("foo.h", "array[[calculator getIndex]];"));
23630 EXPECT_EQ(FormatStyle::LK_Cpp
,
23631 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];"));
23633 FormatStyle::LK_Cpp
,
23634 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];"));
23635 EXPECT_EQ(FormatStyle::LK_ObjC
,
23636 guessLanguage("foo.h", "[[noreturn foo] bar];"));
23637 EXPECT_EQ(FormatStyle::LK_Cpp
,
23638 guessLanguage("foo.h", "[[clang::fallthrough]];"));
23639 EXPECT_EQ(FormatStyle::LK_ObjC
,
23640 guessLanguage("foo.h", "[[clang:fallthrough] foo];"));
23641 EXPECT_EQ(FormatStyle::LK_Cpp
,
23642 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];"));
23643 EXPECT_EQ(FormatStyle::LK_Cpp
,
23644 guessLanguage("foo.h", "[[using clang: fallthrough]];"));
23645 EXPECT_EQ(FormatStyle::LK_ObjC
,
23646 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];"));
23647 EXPECT_EQ(FormatStyle::LK_Cpp
,
23648 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];"));
23650 FormatStyle::LK_Cpp
,
23651 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)"));
23653 FormatStyle::LK_Cpp
,
23654 guessLanguage("foo.h",
23655 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]"));
23656 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", "[[foo::bar, ...]]"));
23659 TEST_F(FormatTest
, GuessLanguageWithCaret
) {
23660 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", "FOO(^);"));
23661 EXPECT_EQ(FormatStyle::LK_Cpp
, guessLanguage("foo.h", "FOO(^, Bar);"));
23662 EXPECT_EQ(FormatStyle::LK_ObjC
,
23663 guessLanguage("foo.h", "int(^)(char, float);"));
23664 EXPECT_EQ(FormatStyle::LK_ObjC
,
23665 guessLanguage("foo.h", "int(^foo)(char, float);"));
23666 EXPECT_EQ(FormatStyle::LK_ObjC
,
23667 guessLanguage("foo.h", "int(^foo[10])(char, float);"));
23668 EXPECT_EQ(FormatStyle::LK_ObjC
,
23669 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);"));
23671 FormatStyle::LK_ObjC
,
23672 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
23675 TEST_F(FormatTest
, GuessLanguageWithPragmas
) {
23676 EXPECT_EQ(FormatStyle::LK_Cpp
,
23677 guessLanguage("foo.h", "__pragma(warning(disable:))"));
23678 EXPECT_EQ(FormatStyle::LK_Cpp
,
23679 guessLanguage("foo.h", "#pragma(warning(disable:))"));
23680 EXPECT_EQ(FormatStyle::LK_Cpp
,
23681 guessLanguage("foo.h", "_Pragma(warning(disable:))"));
23684 TEST_F(FormatTest
, FormatsInlineAsmSymbolicNames
) {
23685 // ASM symbolic names are identifiers that must be surrounded by [] without
23686 // space in between:
23687 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands
23689 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108.
23691 asm volatile("mrs
%x
[result
], FPCR
" : [result] "=r
"(result));
23694 // A list of several ASM symbolic names.
23695 verifyFormat(R
"(asm("mov
%[e
], %[d
]" : [d] "=rm
"(d), [e] "rm
"(*e));)");
23697 // ASM symbolic names in inline ASM with inputs and outputs.
23699 asm("cmoveq
%1, %2, %[result
]"
23700 : [result] "=r
"(result)
23701 : "r
"(test), "r
"(new), "[result
]"(old));
23704 // ASM symbolic names in inline ASM with no outputs.
23705 verifyFormat(R
"(asm("mov
%[e
], %[d
]" : : [d] "=rm
"(d), [e] "rm
"(*e));)");
23708 TEST_F(FormatTest
, GuessedLanguageWithInlineAsmClobbers
) {
23709 EXPECT_EQ(FormatStyle::LK_Cpp
,
23710 guessLanguage("foo.h", "void f() {\n"
23711 " asm (\"mov %[e], %[d]\"\n"
23712 " : [d] \"=rm\" (d)\n"
23713 " [e] \"rm\" (*e));\n"
23715 EXPECT_EQ(FormatStyle::LK_Cpp
,
23716 guessLanguage("foo.h", "void f() {\n"
23717 " _asm (\"mov %[e], %[d]\"\n"
23718 " : [d] \"=rm\" (d)\n"
23719 " [e] \"rm\" (*e));\n"
23721 EXPECT_EQ(FormatStyle::LK_Cpp
,
23722 guessLanguage("foo.h", "void f() {\n"
23723 " __asm (\"mov %[e], %[d]\"\n"
23724 " : [d] \"=rm\" (d)\n"
23725 " [e] \"rm\" (*e));\n"
23727 EXPECT_EQ(FormatStyle::LK_Cpp
,
23728 guessLanguage("foo.h", "void f() {\n"
23729 " __asm__ (\"mov %[e], %[d]\"\n"
23730 " : [d] \"=rm\" (d)\n"
23731 " [e] \"rm\" (*e));\n"
23733 EXPECT_EQ(FormatStyle::LK_Cpp
,
23734 guessLanguage("foo.h", "void f() {\n"
23735 " asm (\"mov %[e], %[d]\"\n"
23736 " : [d] \"=rm\" (d),\n"
23737 " [e] \"rm\" (*e));\n"
23739 EXPECT_EQ(FormatStyle::LK_Cpp
,
23740 guessLanguage("foo.h", "void f() {\n"
23741 " asm volatile (\"mov %[e], %[d]\"\n"
23742 " : [d] \"=rm\" (d)\n"
23743 " [e] \"rm\" (*e));\n"
23747 TEST_F(FormatTest
, GuessLanguageWithChildLines
) {
23748 EXPECT_EQ(FormatStyle::LK_Cpp
,
23749 guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
23750 EXPECT_EQ(FormatStyle::LK_ObjC
,
23751 guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
23753 FormatStyle::LK_Cpp
,
23754 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
23756 FormatStyle::LK_ObjC
,
23757 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
23760 TEST_F(FormatTest
, TypenameMacros
) {
23761 std::vector
<std::string
> TypenameMacros
= {"STACK_OF", "LIST", "TAILQ_ENTRY"};
23763 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353
23764 FormatStyle Google
= getGoogleStyleWithColumns(0);
23765 Google
.TypenameMacros
= TypenameMacros
;
23766 verifyFormat("struct foo {\n"
23768 " TAILQ_ENTRY(a) bleh;\n"
23772 FormatStyle Macros
= getLLVMStyle();
23773 Macros
.TypenameMacros
= TypenameMacros
;
23775 verifyFormat("STACK_OF(int) a;", Macros
);
23776 verifyFormat("STACK_OF(int) *a;", Macros
);
23777 verifyFormat("STACK_OF(int const *) *a;", Macros
);
23778 verifyFormat("STACK_OF(int *const) *a;", Macros
);
23779 verifyFormat("STACK_OF(int, string) a;", Macros
);
23780 verifyFormat("STACK_OF(LIST(int)) a;", Macros
);
23781 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros
);
23782 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros
);
23783 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros
);
23784 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros
);
23785 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros
);
23787 Macros
.PointerAlignment
= FormatStyle::PAS_Left
;
23788 verifyFormat("STACK_OF(int)* a;", Macros
);
23789 verifyFormat("STACK_OF(int*)* a;", Macros
);
23790 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros
);
23791 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros
);
23792 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros
);
23795 TEST_F(FormatTest
, AtomicQualifier
) {
23796 // Check that we treate _Atomic as a type and not a function call
23797 FormatStyle Google
= getGoogleStyleWithColumns(0);
23798 verifyFormat("struct foo {\n"
23800 " _Atomic(a) a2;\n"
23801 " _Atomic(_Atomic(int) *const) a3;\n"
23804 verifyFormat("_Atomic(uint64_t) a;");
23805 verifyFormat("_Atomic(uint64_t) *a;");
23806 verifyFormat("_Atomic(uint64_t const *) *a;");
23807 verifyFormat("_Atomic(uint64_t *const) *a;");
23808 verifyFormat("_Atomic(const uint64_t *) *a;");
23809 verifyFormat("_Atomic(uint64_t) a;");
23810 verifyFormat("_Atomic(_Atomic(uint64_t)) a;");
23811 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;");
23812 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}");
23813 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);");
23815 verifyFormat("_Atomic(uint64_t) *s(InitValue);");
23816 verifyFormat("_Atomic(uint64_t) *s{InitValue};");
23817 FormatStyle Style
= getLLVMStyle();
23818 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
23819 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style
);
23820 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style
);
23821 verifyFormat("_Atomic(int)* a;", Style
);
23822 verifyFormat("_Atomic(int*)* a;", Style
);
23823 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style
);
23825 Style
.SpacesInParens
= FormatStyle::SIPO_Custom
;
23826 Style
.SpacesInParensOptions
.InCStyleCasts
= true;
23827 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style
);
23828 Style
.SpacesInParensOptions
.InCStyleCasts
= false;
23829 Style
.SpacesInParensOptions
.Other
= true;
23830 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style
);
23831 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style
);
23834 TEST_F(FormatTest
, C11Generic
) {
23835 verifyFormat("_Generic(x, int: 1, default: 0)");
23836 verifyFormat("#define cbrt(X) _Generic((X), float: cbrtf, default: cbrt)(X)");
23837 verifyFormat("_Generic(x, const char *: 1, char *const: 16, int: 8);");
23838 verifyFormat("_Generic(x, int: f1, const int: f2)();");
23839 verifyFormat("_Generic(x, struct A: 1, void (*)(void): 2);");
23841 verifyFormat("_Generic(x,\n"
23844 " long double: ld,\n"
23845 " float _Complex: fc,\n"
23846 " double _Complex: dc,\n"
23847 " long double _Complex: ldc)");
23849 FormatStyle Style
= getLLVMStyle();
23850 Style
.ColumnLimit
= 40;
23851 verifyFormat("#define LIMIT_MAX(T) \\\n"
23852 " _Generic(((T)0), \\\n"
23853 " unsigned int: UINT_MAX, \\\n"
23854 " unsigned long: ULONG_MAX, \\\n"
23855 " unsigned long long: ULLONG_MAX)",
23857 verifyFormat("_Generic(x,\n"
23859 " void (*)(void): 2);",
23862 Style
.ContinuationIndentWidth
= 2;
23863 verifyFormat("_Generic(x,\n"
23865 " void (*)(void): 2);",
23869 TEST_F(FormatTest
, AmbersandInLamda
) {
23870 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899
23871 FormatStyle AlignStyle
= getLLVMStyle();
23872 AlignStyle
.PointerAlignment
= FormatStyle::PAS_Left
;
23873 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle
);
23874 AlignStyle
.PointerAlignment
= FormatStyle::PAS_Right
;
23875 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle
);
23878 TEST_F(FormatTest
, TrailingReturnTypeAuto
) {
23879 FormatStyle Style
= getLLVMStyle();
23880 verifyFormat("[]() -> auto { return Val; }", Style
);
23881 verifyFormat("[]() -> auto * { return Val; }", Style
);
23882 verifyFormat("[]() -> auto & { return Val; }", Style
);
23883 verifyFormat("auto foo() -> auto { return Val; }", Style
);
23884 verifyFormat("auto foo() -> auto * { return Val; }", Style
);
23885 verifyFormat("auto foo() -> auto & { return Val; }", Style
);
23888 TEST_F(FormatTest
, SpacesInConditionalStatement
) {
23889 FormatStyle Spaces
= getLLVMStyle();
23890 Spaces
.IfMacros
.clear();
23891 Spaces
.IfMacros
.push_back("MYIF");
23892 Spaces
.SpacesInParens
= FormatStyle::SIPO_Custom
;
23893 Spaces
.SpacesInParensOptions
.InConditionalStatements
= true;
23894 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces
);
23895 verifyFormat("if ( !a )\n return;", Spaces
);
23896 verifyFormat("if ( a )\n return;", Spaces
);
23897 verifyFormat("if constexpr ( a )\n return;", Spaces
);
23898 verifyFormat("MYIF ( a )\n return;", Spaces
);
23899 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces
);
23900 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces
);
23901 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces
);
23902 verifyFormat("while ( a )\n return;", Spaces
);
23903 verifyFormat("while ( (a && b) )\n return;", Spaces
);
23904 verifyFormat("do {\n} while ( 1 != 0 );", Spaces
);
23905 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces
);
23906 // Check that space on the left of "::" is inserted as expected at beginning
23908 verifyFormat("while ( ::func() )\n return;", Spaces
);
23910 // Check impact of ControlStatementsExceptControlMacros is honored.
23911 Spaces
.SpaceBeforeParens
=
23912 FormatStyle::SBPO_ControlStatementsExceptControlMacros
;
23913 verifyFormat("MYIF( a )\n return;", Spaces
);
23914 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces
);
23915 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces
);
23918 TEST_F(FormatTest
, AlternativeOperators
) {
23919 // Test case for ensuring alternate operators are not
23920 // combined with their right most neighbour.
23921 verifyFormat("int a and b;");
23922 verifyFormat("int a and_eq b;");
23923 verifyFormat("int a bitand b;");
23924 verifyFormat("int a bitor b;");
23925 verifyFormat("int a compl b;");
23926 verifyFormat("int a not b;");
23927 verifyFormat("int a not_eq b;");
23928 verifyFormat("int a or b;");
23929 verifyFormat("int a xor b;");
23930 verifyFormat("int a xor_eq b;");
23931 verifyFormat("return this not_eq bitand other;");
23932 verifyFormat("bool operator not_eq(const X bitand other)");
23934 verifyFormat("int a and 5;");
23935 verifyFormat("int a and_eq 5;");
23936 verifyFormat("int a bitand 5;");
23937 verifyFormat("int a bitor 5;");
23938 verifyFormat("int a compl 5;");
23939 verifyFormat("int a not 5;");
23940 verifyFormat("int a not_eq 5;");
23941 verifyFormat("int a or 5;");
23942 verifyFormat("int a xor 5;");
23943 verifyFormat("int a xor_eq 5;");
23945 verifyFormat("int a compl(5);");
23946 verifyFormat("int a not(5);");
23948 /* FIXME handle alternate tokens
23949 * https://en.cppreference.com/w/cpp/language/operator_alternative
23950 // alternative tokens
23951 verifyFormat("compl foo();"); // ~foo();
23952 verifyFormat("foo() <%%>;"); // foo();
23953 verifyFormat("void foo() <%%>;"); // void foo(){}
23954 verifyFormat("int a <:1:>;"); // int a[1];[
23955 verifyFormat("%:define ABC abc"); // #define ABC abc
23956 verifyFormat("%:%:"); // ##
23960 TEST_F(FormatTest
, STLWhileNotDefineChed
) {
23961 verifyFormat("#if defined(while)\n"
23962 "#define while EMIT WARNING C4005\n"
23963 "#endif // while");
23966 TEST_F(FormatTest
, OperatorSpacing
) {
23967 FormatStyle Style
= getLLVMStyle();
23968 Style
.PointerAlignment
= FormatStyle::PAS_Right
;
23969 verifyFormat("Foo::operator*();", Style
);
23970 verifyFormat("Foo::operator void *();", Style
);
23971 verifyFormat("Foo::operator void **();", Style
);
23972 verifyFormat("Foo::operator void *&();", Style
);
23973 verifyFormat("Foo::operator void *&&();", Style
);
23974 verifyFormat("Foo::operator void const *();", Style
);
23975 verifyFormat("Foo::operator void const **();", Style
);
23976 verifyFormat("Foo::operator void const *&();", Style
);
23977 verifyFormat("Foo::operator void const *&&();", Style
);
23978 verifyFormat("Foo::operator()(void *);", Style
);
23979 verifyFormat("Foo::operator*(void *);", Style
);
23980 verifyFormat("Foo::operator*();", Style
);
23981 verifyFormat("Foo::operator**();", Style
);
23982 verifyFormat("Foo::operator&();", Style
);
23983 verifyFormat("Foo::operator<int> *();", Style
);
23984 verifyFormat("Foo::operator<Foo> *();", Style
);
23985 verifyFormat("Foo::operator<int> **();", Style
);
23986 verifyFormat("Foo::operator<Foo> **();", Style
);
23987 verifyFormat("Foo::operator<int> &();", Style
);
23988 verifyFormat("Foo::operator<Foo> &();", Style
);
23989 verifyFormat("Foo::operator<int> &&();", Style
);
23990 verifyFormat("Foo::operator<Foo> &&();", Style
);
23991 verifyFormat("Foo::operator<int> *&();", Style
);
23992 verifyFormat("Foo::operator<Foo> *&();", Style
);
23993 verifyFormat("Foo::operator<int> *&&();", Style
);
23994 verifyFormat("Foo::operator<Foo> *&&();", Style
);
23995 verifyFormat("operator*(int (*)(), class Foo);", Style
);
23997 verifyFormat("Foo::operator&();", Style
);
23998 verifyFormat("Foo::operator void &();", Style
);
23999 verifyFormat("Foo::operator void const &();", Style
);
24000 verifyFormat("Foo::operator()(void &);", Style
);
24001 verifyFormat("Foo::operator&(void &);", Style
);
24002 verifyFormat("Foo::operator&();", Style
);
24003 verifyFormat("operator&(int (&)(), class Foo);", Style
);
24004 verifyFormat("operator&&(int (&)(), class Foo);", Style
);
24006 verifyFormat("Foo::operator&&();", Style
);
24007 verifyFormat("Foo::operator**();", Style
);
24008 verifyFormat("Foo::operator void &&();", Style
);
24009 verifyFormat("Foo::operator void const &&();", Style
);
24010 verifyFormat("Foo::operator()(void &&);", Style
);
24011 verifyFormat("Foo::operator&&(void &&);", Style
);
24012 verifyFormat("Foo::operator&&();", Style
);
24013 verifyFormat("operator&&(int (&&)(), class Foo);", Style
);
24014 verifyFormat("operator const nsTArrayRight<E> &()", Style
);
24015 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()",
24017 verifyFormat("operator void **()", Style
);
24018 verifyFormat("operator const FooRight<Object> &()", Style
);
24019 verifyFormat("operator const FooRight<Object> *()", Style
);
24020 verifyFormat("operator const FooRight<Object> **()", Style
);
24021 verifyFormat("operator const FooRight<Object> *&()", Style
);
24022 verifyFormat("operator const FooRight<Object> *&&()", Style
);
24024 Style
.PointerAlignment
= FormatStyle::PAS_Left
;
24025 verifyFormat("Foo::operator*();", Style
);
24026 verifyFormat("Foo::operator**();", Style
);
24027 verifyFormat("Foo::operator void*();", Style
);
24028 verifyFormat("Foo::operator void**();", Style
);
24029 verifyFormat("Foo::operator void*&();", Style
);
24030 verifyFormat("Foo::operator void*&&();", Style
);
24031 verifyFormat("Foo::operator void const*();", Style
);
24032 verifyFormat("Foo::operator void const**();", Style
);
24033 verifyFormat("Foo::operator void const*&();", Style
);
24034 verifyFormat("Foo::operator void const*&&();", Style
);
24035 verifyFormat("Foo::operator/*comment*/ void*();", Style
);
24036 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style
);
24037 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style
);
24038 verifyFormat("Foo::operator()(void*);", Style
);
24039 verifyFormat("Foo::operator*(void*);", Style
);
24040 verifyFormat("Foo::operator*();", Style
);
24041 verifyFormat("Foo::operator<int>*();", Style
);
24042 verifyFormat("Foo::operator<Foo>*();", Style
);
24043 verifyFormat("Foo::operator<int>**();", Style
);
24044 verifyFormat("Foo::operator<Foo>**();", Style
);
24045 verifyFormat("Foo::operator<Foo>*&();", Style
);
24046 verifyFormat("Foo::operator<int>&();", Style
);
24047 verifyFormat("Foo::operator<Foo>&();", Style
);
24048 verifyFormat("Foo::operator<int>&&();", Style
);
24049 verifyFormat("Foo::operator<Foo>&&();", Style
);
24050 verifyFormat("Foo::operator<int>*&();", Style
);
24051 verifyFormat("Foo::operator<Foo>*&();", Style
);
24052 verifyFormat("operator*(int (*)(), class Foo);", Style
);
24054 verifyFormat("Foo::operator&();", Style
);
24055 verifyFormat("Foo::operator void&();", Style
);
24056 verifyFormat("Foo::operator void const&();", Style
);
24057 verifyFormat("Foo::operator/*comment*/ void&();", Style
);
24058 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style
);
24059 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style
);
24060 verifyFormat("Foo::operator()(void&);", Style
);
24061 verifyFormat("Foo::operator&(void&);", Style
);
24062 verifyFormat("Foo::operator&();", Style
);
24063 verifyFormat("operator&(int (&)(), class Foo);", Style
);
24064 verifyFormat("operator&(int (&&)(), class Foo);", Style
);
24065 verifyFormat("operator&&(int (&&)(), class Foo);", Style
);
24067 verifyFormat("Foo::operator&&();", Style
);
24068 verifyFormat("Foo::operator void&&();", Style
);
24069 verifyFormat("Foo::operator void const&&();", Style
);
24070 verifyFormat("Foo::operator/*comment*/ void&&();", Style
);
24071 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style
);
24072 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style
);
24073 verifyFormat("Foo::operator()(void&&);", Style
);
24074 verifyFormat("Foo::operator&&(void&&);", Style
);
24075 verifyFormat("Foo::operator&&();", Style
);
24076 verifyFormat("operator&&(int (&&)(), class Foo);", Style
);
24077 verifyFormat("operator const nsTArrayLeft<E>&()", Style
);
24078 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()",
24080 verifyFormat("operator void**()", Style
);
24081 verifyFormat("operator const FooLeft<Object>&()", Style
);
24082 verifyFormat("operator const FooLeft<Object>*()", Style
);
24083 verifyFormat("operator const FooLeft<Object>**()", Style
);
24084 verifyFormat("operator const FooLeft<Object>*&()", Style
);
24085 verifyFormat("operator const FooLeft<Object>*&&()", Style
);
24088 verifyFormat("operator Vector<String>&();", Style
);
24089 verifyFormat("operator const Vector<String>&();", Style
);
24090 verifyFormat("operator foo::Bar*();", Style
);
24091 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style
);
24092 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();",
24095 Style
.PointerAlignment
= FormatStyle::PAS_Middle
;
24096 verifyFormat("Foo::operator*();", Style
);
24097 verifyFormat("Foo::operator void *();", Style
);
24098 verifyFormat("Foo::operator()(void *);", Style
);
24099 verifyFormat("Foo::operator*(void *);", Style
);
24100 verifyFormat("Foo::operator*();", Style
);
24101 verifyFormat("operator*(int (*)(), class Foo);", Style
);
24103 verifyFormat("Foo::operator&();", Style
);
24104 verifyFormat("Foo::operator void &();", Style
);
24105 verifyFormat("Foo::operator void const &();", Style
);
24106 verifyFormat("Foo::operator()(void &);", Style
);
24107 verifyFormat("Foo::operator&(void &);", Style
);
24108 verifyFormat("Foo::operator&();", Style
);
24109 verifyFormat("operator&(int (&)(), class Foo);", Style
);
24111 verifyFormat("Foo::operator&&();", Style
);
24112 verifyFormat("Foo::operator void &&();", Style
);
24113 verifyFormat("Foo::operator void const &&();", Style
);
24114 verifyFormat("Foo::operator()(void &&);", Style
);
24115 verifyFormat("Foo::operator&&(void &&);", Style
);
24116 verifyFormat("Foo::operator&&();", Style
);
24117 verifyFormat("operator&&(int (&&)(), class Foo);", Style
);
24120 TEST_F(FormatTest
, OperatorPassedAsAFunctionPtr
) {
24121 FormatStyle Style
= getLLVMStyle();
24123 verifyFormat("foo(operator+, -42);", Style
);
24124 verifyFormat("foo(operator++, -42);", Style
);
24125 verifyFormat("foo(operator--, -42);", Style
);
24126 verifyFormat("foo(-42, operator--);", Style
);
24127 verifyFormat("foo(-42, operator, );", Style
);
24128 verifyFormat("foo(operator, , -42);", Style
);
24131 TEST_F(FormatTest
, WhitespaceSensitiveMacros
) {
24132 FormatStyle Style
= getLLVMStyle();
24133 Style
.WhitespaceSensitiveMacros
.push_back("FOO");
24135 // Newlines are important here.
24136 verifyNoChange("FOO(1+2 )\n", Style
);
24137 verifyNoChange("FOO(a:b:c)\n", Style
);
24139 // Don't use the helpers here, since 'mess up' will change the whitespace
24140 // and these are all whitespace sensitive by definition
24141 verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style
);
24142 verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style
);
24143 verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style
);
24144 verifyNoChange("FOO(String-ized&Messy+But,: :\n"
24145 " Still=Intentional);",
24147 Style
.AlignConsecutiveAssignments
.Enabled
= true;
24148 verifyNoChange("FOO(String-ized=&Messy+But,: :\n"
24149 " Still=Intentional);",
24152 Style
.ColumnLimit
= 21;
24153 verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style
);
24156 TEST_F(FormatTest
, VeryLongNamespaceCommentSplit
) {
24157 // These tests are not in NamespaceEndCommentsFixerTest because that doesn't
24158 // test its interaction with line wrapping
24159 FormatStyle Style
= getLLVMStyleWithColumns(80);
24160 verifyFormat("namespace {\n"
24166 verifyFormat("namespace AAA {\n"
24169 "} // namespace AAA",
24172 verifyFormat("namespace Averyveryveryverylongnamespace {\n"
24175 "} // namespace Averyveryveryverylongnamespace",
24176 "namespace Averyveryveryverylongnamespace {\n"
24184 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
24185 " went::mad::now {\n"
24190 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
24193 "would::it::save::you::a::lot::of::time::if_::i::"
24194 "just::gave::up::and_::went::mad::now {\n"
24200 // This used to duplicate the comment again and again on subsequent runs
24203 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n"
24204 " went::mad::now {\n"
24209 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::"
24212 "would::it::save::you::a::lot::of::time::if_::i::"
24213 "just::gave::up::and_::went::mad::now {\n"
24218 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::"
24219 "and_::went::mad::now",
24223 TEST_F(FormatTest
, LikelyUnlikely
) {
24224 FormatStyle Style
= getLLVMStyle();
24226 verifyFormat("if (argc > 5) [[unlikely]] {\n"
24231 verifyFormat("if (argc > 5) [[likely]] {\n"
24236 verifyFormat("if (argc > 5) [[unlikely]] {\n"
24238 "} else [[likely]] {\n"
24243 verifyFormat("if (argc > 5) [[unlikely]] {\n"
24245 "} else if (argc > 10) [[likely]] {\n"
24252 verifyFormat("if (argc > 5) [[gnu::unused]] {\n"
24257 verifyFormat("if (argc > 5) [[unlikely]]\n"
24260 verifyFormat("if (argc > 5) [[likely]]\n"
24264 verifyFormat("while (limit > 0) [[unlikely]] {\n"
24268 verifyFormat("for (auto &limit : limits) [[likely]] {\n"
24273 verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
24276 verifyFormat("while (limit > 0) [[likely]]\n"
24280 Style
.AttributeMacros
.push_back("UNLIKELY");
24281 Style
.AttributeMacros
.push_back("LIKELY");
24282 verifyFormat("if (argc > 5) UNLIKELY\n"
24286 verifyFormat("if (argc > 5) UNLIKELY {\n"
24290 verifyFormat("if (argc > 5) UNLIKELY {\n"
24292 "} else [[likely]] {\n"
24296 verifyFormat("if (argc > 5) UNLIKELY {\n"
24298 "} else LIKELY {\n"
24302 verifyFormat("if (argc > 5) [[unlikely]] {\n"
24304 "} else LIKELY {\n"
24309 verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
24313 verifyFormat("while (limit > 0) LIKELY {\n"
24318 verifyFormat("while (limit > 0) UNLIKELY\n"
24321 verifyFormat("for (auto &limit : limits) LIKELY\n"
24326 TEST_F(FormatTest
, PenaltyIndentedWhitespace
) {
24327 verifyFormat("Constructor()\n"
24328 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
24329 " aaaa(aaaaaaaaaaaaaaaaaa, "
24330 "aaaaaaaaaaaaaaaaaat))");
24331 verifyFormat("Constructor()\n"
24332 " : aaaaaaaaaaaaa(aaaaaa), "
24333 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)");
24335 FormatStyle StyleWithWhitespacePenalty
= getLLVMStyle();
24336 StyleWithWhitespacePenalty
.PenaltyIndentedWhitespace
= 5;
24337 verifyFormat("Constructor()\n"
24338 " : aaaaaa(aaaaaa),\n"
24339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
24340 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))",
24341 StyleWithWhitespacePenalty
);
24342 verifyFormat("Constructor()\n"
24343 " : aaaaaaaaaaaaa(aaaaaa), "
24344 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)",
24345 StyleWithWhitespacePenalty
);
24348 TEST_F(FormatTest
, LLVMDefaultStyle
) {
24349 FormatStyle Style
= getLLVMStyle();
24350 verifyFormat("extern \"C\" {\n"
24355 TEST_F(FormatTest
, GNUDefaultStyle
) {
24356 FormatStyle Style
= getGNUStyle();
24357 verifyFormat("extern \"C\"\n"
24363 TEST_F(FormatTest
, MozillaDefaultStyle
) {
24364 FormatStyle Style
= getMozillaStyle();
24365 verifyFormat("extern \"C\"\n"
24371 TEST_F(FormatTest
, GoogleDefaultStyle
) {
24372 FormatStyle Style
= getGoogleStyle();
24373 verifyFormat("extern \"C\" {\n"
24378 TEST_F(FormatTest
, ChromiumDefaultStyle
) {
24379 FormatStyle Style
= getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp
);
24380 verifyFormat("extern \"C\" {\n"
24385 TEST_F(FormatTest
, MicrosoftDefaultStyle
) {
24386 FormatStyle Style
= getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp
);
24387 verifyFormat("extern \"C\"\n"
24393 TEST_F(FormatTest
, WebKitDefaultStyle
) {
24394 FormatStyle Style
= getWebKitStyle();
24395 verifyFormat("extern \"C\" {\n"
24401 TEST_F(FormatTest
, Concepts
) {
24402 EXPECT_EQ(getLLVMStyle().BreakBeforeConceptDeclarations
,
24403 FormatStyle::BBCDS_Always
);
24405 // The default in LLVM style is REI_OuterScope, but these tests were written
24406 // when the default was REI_Keyword.
24407 FormatStyle Style
= getLLVMStyle();
24408 Style
.RequiresExpressionIndentation
= FormatStyle::REI_Keyword
;
24410 verifyFormat("template <typename T>\n"
24411 "concept True = true;");
24413 verifyFormat("template <typename T>\n"
24414 "concept C = ((false || foo()) && C2<T>) ||\n"
24415 " (std::trait<T>::value && Baz) || sizeof(T) >= 6;",
24416 getLLVMStyleWithColumns(60));
24418 verifyFormat("template <typename T>\n"
24419 "concept DelayedCheck = true && requires(T t) { t.bar(); } && "
24420 "sizeof(T) <= 8;");
24422 verifyFormat("template <typename T>\n"
24423 "concept DelayedCheck = true && requires(T t) {\n"
24426 " } && sizeof(T) <= 8;",
24429 verifyFormat("template <typename T>\n"
24430 "concept DelayedCheck = true && requires(T t) { // Comment\n"
24433 " } && sizeof(T) <= 8;",
24436 verifyFormat("template <typename T>\n"
24437 "concept DelayedCheck = false || requires(T t) { t.bar(); } && "
24438 "sizeof(T) <= 8;");
24440 verifyFormat("template <typename T>\n"
24441 "concept DelayedCheck = Unit<T> && !DerivedUnit<T>;");
24443 verifyFormat("template <typename T>\n"
24444 "concept DelayedCheck = Unit<T> && !(DerivedUnit<T>);");
24446 verifyFormat("template <typename T>\n"
24447 "concept DelayedCheck = Unit<T> && !!DerivedUnit<T>;");
24449 verifyFormat("template <typename T>\n"
24450 "concept DelayedCheck = !!false || requires(T t) { t.bar(); } "
24451 "&& sizeof(T) <= 8;");
24453 verifyFormat("template <typename T>\n"
24454 "concept DelayedCheck =\n"
24455 " static_cast<bool>(0) || requires(T t) { t.bar(); } && "
24456 "sizeof(T) <= 8;");
24458 verifyFormat("template <typename T>\n"
24459 "concept DelayedCheck = bool(0) || requires(T t) { t.bar(); } "
24460 "&& sizeof(T) <= 8;");
24463 "template <typename T>\n"
24464 "concept DelayedCheck =\n"
24465 " (bool)(0) || requires(T t) { t.bar(); } && sizeof(T) <= 8;");
24467 verifyFormat("template <typename T>\n"
24468 "concept DelayedCheck = (bool)0 || requires(T t) { t.bar(); } "
24469 "&& sizeof(T) <= 8;");
24471 verifyFormat("template <typename T>\n"
24472 "concept Size = sizeof(T) >= 5 && requires(T t) { t.bar(); } && "
24473 "sizeof(T) <= 8;");
24475 verifyFormat("template <typename T>\n"
24476 "concept Size = 2 < 5 && 2 <= 5 && 8 >= 5 && 8 > 5 &&\n"
24477 " requires(T t) {\n"
24480 " } && sizeof(T) <= 8 && !(4 < 3);",
24481 getLLVMStyleWithColumns(60));
24483 verifyFormat("template <typename T>\n"
24484 "concept TrueOrNot = IsAlwaysTrue || IsNeverTrue;");
24486 verifyFormat("template <typename T>\n"
24487 "concept C = foo();");
24489 verifyFormat("template <typename T>\n"
24490 "concept C = foo(T());");
24492 verifyFormat("template <typename T>\n"
24493 "concept C = foo(T{});");
24495 verifyFormat("template <typename T>\n"
24496 "concept Size = V<sizeof(T)>::Value > 5;");
24498 verifyFormat("template <typename T>\n"
24499 "concept True = S<T>::Value;");
24501 verifyFormat("template <S T>\n"
24502 "concept True = T.field;");
24505 "template <typename T>\n"
24506 "concept C = []() { return true; }() && requires(T t) { t.bar(); } &&\n"
24507 " sizeof(T) <= 8;");
24509 // FIXME: This is misformatted because the fake l paren starts at bool, not at
24510 // the lambda l square.
24511 verifyFormat("template <typename T>\n"
24512 "concept C = [] -> bool { return true; }() && requires(T t) { "
24514 " sizeof(T) <= 8;");
24517 "template <typename T>\n"
24518 "concept C = decltype([]() { return std::true_type{}; }())::value &&\n"
24519 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
24521 verifyFormat("template <typename T>\n"
24522 "concept C = decltype([]() { return std::true_type{}; "
24523 "}())::value && requires(T t) { t.bar(); } && sizeof(T) <= 8;",
24524 getLLVMStyleWithColumns(120));
24526 verifyFormat("template <typename T>\n"
24527 "concept C = decltype([]() -> std::true_type { return {}; "
24529 " requires(T t) { t.bar(); } && sizeof(T) <= 8;");
24531 verifyFormat("template <typename T>\n"
24532 "concept C = true;\n"
24535 verifyFormat("template <typename T>\n"
24536 "concept Hashable = requires(T a) {\n"
24537 " { std::hash<T>{}(a) } -> "
24538 "std::convertible_to<std::size_t>;\n"
24543 "template <typename T>\n"
24544 "concept EqualityComparable = requires(T a, T b) {\n"
24545 " { a == b } -> std::same_as<bool>;\n"
24550 "template <typename T>\n"
24551 "concept EqualityComparable = requires(T a, T b) {\n"
24552 " { a == b } -> std::same_as<bool>;\n"
24553 " { a != b } -> std::same_as<bool>;\n"
24557 verifyFormat("template <typename T>\n"
24558 "concept WeakEqualityComparable = requires(T a, T b) {\n"
24564 verifyFormat("template <typename T>\n"
24565 "concept HasSizeT = requires { typename T::size_t; };");
24567 verifyFormat("template <typename T>\n"
24568 "concept Semiregular =\n"
24569 " DefaultConstructible<T> && CopyConstructible<T> && "
24570 "CopyAssignable<T> &&\n"
24571 " requires(T a, std::size_t n) {\n"
24572 " requires Same<T *, decltype(&a)>;\n"
24573 " { a.~T() } noexcept;\n"
24574 " requires Same<T *, decltype(new T)>;\n"
24575 " requires Same<T *, decltype(new T[n])>;\n"
24576 " { delete new T; };\n"
24577 " { delete new T[n]; };\n"
24581 verifyFormat("template <typename T>\n"
24582 "concept Semiregular =\n"
24583 " requires(T a, std::size_t n) {\n"
24584 " requires Same<T *, decltype(&a)>;\n"
24585 " { a.~T() } noexcept;\n"
24586 " requires Same<T *, decltype(new T)>;\n"
24587 " requires Same<T *, decltype(new T[n])>;\n"
24588 " { delete new T; };\n"
24589 " { delete new T[n]; };\n"
24590 " { new T } -> std::same_as<T *>;\n"
24591 " } && DefaultConstructible<T> && CopyConstructible<T> && "
24592 "CopyAssignable<T>;",
24596 "template <typename T>\n"
24597 "concept Semiregular =\n"
24598 " DefaultConstructible<T> && requires(T a, std::size_t n) {\n"
24599 " requires Same<T *, decltype(&a)>;\n"
24600 " { a.~T() } noexcept;\n"
24601 " requires Same<T *, decltype(new T)>;\n"
24602 " requires Same<T *, decltype(new "
24604 " { delete new T; };\n"
24605 " { delete new T[n]; };\n"
24606 " } && CopyConstructible<T> && "
24607 "CopyAssignable<T>;",
24610 verifyFormat("template <typename T>\n"
24611 "concept Two = requires(T t) {\n"
24612 " { t.foo() } -> std::same_as<Bar>;\n"
24613 " } && requires(T &&t) {\n"
24614 " { t.foo() } -> std::same_as<Bar &&>;\n"
24619 "template <typename T>\n"
24620 "concept C = requires(T x) {\n"
24621 " { *x } -> std::convertible_to<typename T::inner>;\n"
24622 " { x + 1 } noexcept -> std::same_as<int>;\n"
24623 " { x * 1 } -> std::convertible_to<T>;\n"
24627 verifyFormat("template <typename T>\n"
24628 "concept C = requires(T x) {\n"
24630 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
24631 " } -> long_long_concept_name<T>;\n"
24633 " long_long_long_function_call(1, 2, 3, 4, 5)\n"
24634 " } noexcept -> long_long_concept_name<T>;\n"
24639 "template <typename T, typename U = T>\n"
24640 "concept Swappable = requires(T &&t, U &&u) {\n"
24641 " swap(std::forward<T>(t), std::forward<U>(u));\n"
24642 " swap(std::forward<U>(u), std::forward<T>(t));\n"
24646 verifyFormat("template <typename T, typename U>\n"
24647 "concept Common = requires(T &&t, U &&u) {\n"
24648 " typename CommonType<T, U>;\n"
24649 " { CommonType<T, U>(std::forward<T>(t)) };\n"
24653 verifyFormat("template <typename T, typename U>\n"
24654 "concept Common = requires(T &&t, U &&u) {\n"
24655 " typename CommonType<T, U>;\n"
24656 " { CommonType<T, U>{std::forward<T>(t)} };\n"
24661 "template <typename T>\n"
24662 "concept C = requires(T t) {\n"
24663 " requires Bar<T> && Foo<T>;\n"
24664 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
24668 verifyFormat("template <typename T>\n"
24669 "concept HasFoo = requires(T t) {\n"
24673 "template <typename T>\n"
24674 "concept HasBar = requires(T t) {\n"
24680 verifyFormat("template <typename T>\n"
24681 "concept Large = sizeof(T) > 10;");
24683 verifyFormat("template <typename T, typename U>\n"
24684 "concept FooableWith = requires(T t, U u) {\n"
24685 " typename T::foo_type;\n"
24686 " { t.foo(u) } -> typename T::foo_type;\n"
24689 "void doFoo(FooableWith<int> auto t) { t.foo(3); }",
24692 verifyFormat("template <typename T>\n"
24693 "concept Context = is_specialization_of_v<context, T>;");
24695 verifyFormat("template <typename T>\n"
24696 "concept Node = std::is_object_v<T>;");
24698 verifyFormat("template <class T>\n"
24699 "concept integral = __is_integral(T);");
24701 verifyFormat("template <class T>\n"
24702 "concept is2D = __array_extent(T, 1) == 2;");
24704 verifyFormat("template <class T>\n"
24705 "concept isRhs = __is_rvalue_expr(std::declval<T>() + 2)");
24707 verifyFormat("template <class T, class T2>\n"
24708 "concept Same = __is_same_as<T, T2>;");
24711 "template <class _InIt, class _OutIt>\n"
24712 "concept _Can_reread_dest =\n"
24713 " std::forward_iterator<_OutIt> &&\n"
24714 " std::same_as<std::iter_value_t<_InIt>, std::iter_value_t<_OutIt>>;");
24716 Style
.BreakBeforeConceptDeclarations
= FormatStyle::BBCDS_Allowed
;
24719 "template <typename T>\n"
24720 "concept C = requires(T t) {\n"
24721 " requires Bar<T> && Foo<T>;\n"
24722 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
24726 verifyFormat("template <typename T>\n"
24727 "concept HasFoo = requires(T t) {\n"
24731 "template <typename T>\n"
24732 "concept HasBar = requires(T t) {\n"
24738 verifyFormat("template <typename T> concept True = true;", Style
);
24740 verifyFormat("template <typename T>\n"
24741 "concept C = decltype([]() -> std::true_type { return {}; "
24743 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
24746 verifyFormat("template <typename T>\n"
24747 "concept Semiregular =\n"
24748 " DefaultConstructible<T> && CopyConstructible<T> && "
24749 "CopyAssignable<T> &&\n"
24750 " requires(T a, std::size_t n) {\n"
24751 " requires Same<T *, decltype(&a)>;\n"
24752 " { a.~T() } noexcept;\n"
24753 " requires Same<T *, decltype(new T)>;\n"
24754 " requires Same<T *, decltype(new T[n])>;\n"
24755 " { delete new T; };\n"
24756 " { delete new T[n]; };\n"
24760 Style
.BreakBeforeConceptDeclarations
= FormatStyle::BBCDS_Never
;
24762 verifyFormat("template <typename T> concept C =\n"
24763 " requires(T t) {\n"
24764 " requires Bar<T> && Foo<T>;\n"
24765 " requires((trait<T> && Baz) || (T2<T> && Foo<T>));\n"
24769 verifyFormat("template <typename T> concept HasFoo = requires(T t) {\n"
24773 "template <typename T> concept HasBar = requires(T t) {\n"
24779 verifyFormat("template <typename T> concept True = true;", Style
);
24782 "template <typename T> concept C =\n"
24783 " decltype([]() -> std::true_type { return {}; }())::value &&\n"
24784 " requires(T t) { t.bar(); } && sizeof(T) <= 8;",
24787 verifyFormat("template <typename T> concept Semiregular =\n"
24788 " DefaultConstructible<T> && CopyConstructible<T> && "
24789 "CopyAssignable<T> &&\n"
24790 " requires(T a, std::size_t n) {\n"
24791 " requires Same<T *, decltype(&a)>;\n"
24792 " { a.~T() } noexcept;\n"
24793 " requires Same<T *, decltype(new T)>;\n"
24794 " requires Same<T *, decltype(new T[n])>;\n"
24795 " { delete new T; };\n"
24796 " { delete new T[n]; };\n"
24800 // The following tests are invalid C++, we just want to make sure we don't
24802 verifyNoCrash("template <typename T>\n"
24803 "concept C = requires C2<T>;");
24805 verifyNoCrash("template <typename T>\n"
24806 "concept C = 5 + 4;");
24808 verifyNoCrash("template <typename T>\n"
24809 "concept C = class X;");
24811 verifyNoCrash("template <typename T>\n"
24812 "concept C = [] && true;");
24814 verifyNoCrash("template <typename T>\n"
24815 "concept C = [] && requires(T t) { typename T::size_type; };");
24818 TEST_F(FormatTest
, RequiresClausesPositions
) {
24819 auto Style
= getLLVMStyle();
24820 EXPECT_EQ(Style
.RequiresClausePosition
, FormatStyle::RCPS_OwnLine
);
24821 EXPECT_EQ(Style
.IndentRequiresClause
, true);
24823 // The default in LLVM style is REI_OuterScope, but these tests were written
24824 // when the default was REI_Keyword.
24825 Style
.RequiresExpressionIndentation
= FormatStyle::REI_Keyword
;
24827 verifyFormat("template <typename T>\n"
24828 " requires(Foo<T> && std::trait<T>)\n"
24832 verifyFormat("template <typename T>\n"
24833 " requires(Foo<T> && std::trait<T>)\n"
24842 "template <typename T>\n"
24843 " requires requires(T &&t) {\n"
24844 " typename T::I;\n"
24845 " requires(F<typename T::I> && std::trait<typename T::I>);\n"
24847 "Bar(T) -> Bar<typename T::I>;",
24850 verifyFormat("template <typename T>\n"
24851 " requires(Foo<T> && std::trait<T>)\n"
24852 "constexpr T MyGlobal;",
24855 verifyFormat("template <typename T>\n"
24856 " requires Foo<T> && requires(T t) {\n"
24857 " { t.baz() } -> std::same_as<bool>;\n"
24858 " requires std::same_as<T::Factor, int>;\n"
24860 "inline int bar(T t) {\n"
24861 " return t.baz() ? T::Factor : 5;\n"
24865 verifyFormat("template <typename T>\n"
24866 "inline int bar(T t)\n"
24867 " requires Foo<T> && requires(T t) {\n"
24868 " { t.baz() } -> std::same_as<bool>;\n"
24869 " requires std::same_as<T::Factor, int>;\n"
24872 " return t.baz() ? T::Factor : 5;\n"
24876 verifyFormat("template <typename T>\n"
24883 verifyFormat("template <typename T>\n"
24891 verifyFormat("template <typename T>\n"
24892 "int S::bar(T t) &&\n"
24899 verifyFormat("template <typename T>\n"
24904 Style
.IndentRequiresClause
= false;
24905 verifyFormat("template <typename T>\n"
24912 verifyFormat("template <typename T>\n"
24913 "int S::bar(T t) &&\n"
24920 verifyFormat("template <typename T>\n"
24928 Style
.RequiresClausePosition
= FormatStyle::RCPS_SingleLine
;
24929 verifyFormat("template <typename T> requires Foo<T> struct Bar {};\n"
24930 "template <typename T> requires Foo<T> void bar() {}\n"
24931 "template <typename T> void bar() requires Foo<T> {}\n"
24932 "template <typename T> void bar() requires Foo<T>;\n"
24933 "template <typename T> void S::bar() && requires Foo<T> {}\n"
24934 "template <typename T> requires Foo<T> Bar(T) -> Bar<T>;",
24937 auto ColumnStyle
= Style
;
24938 ColumnStyle
.ColumnLimit
= 40;
24939 verifyFormat("template <typename AAAAAAA>\n"
24940 "requires Foo<T> struct Bar {};\n"
24941 "template <typename AAAAAAA>\n"
24942 "requires Foo<T> void bar() {}\n"
24943 "template <typename AAAAAAA>\n"
24944 "void bar() requires Foo<T> {}\n"
24945 "template <typename T>\n"
24946 "void S::bar() && requires Foo<T> {}\n"
24947 "template <typename AAAAAAA>\n"
24948 "requires Foo<T> Baz(T) -> Baz<T>;",
24951 verifyFormat("template <typename T>\n"
24952 "requires Foo<AAAAAAA> struct Bar {};\n"
24953 "template <typename T>\n"
24954 "requires Foo<AAAAAAA> void bar() {}\n"
24955 "template <typename T>\n"
24956 "void bar() requires Foo<AAAAAAA> {}\n"
24957 "template <typename T>\n"
24958 "requires Foo<AAAAAAA> Bar(T) -> Bar<T>;",
24961 verifyFormat("template <typename AAAAAAA>\n"
24962 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24964 "template <typename AAAAAAA>\n"
24965 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24967 "template <typename AAAAAAA>\n"
24969 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
24970 "template <typename AAAAAAA>\n"
24971 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
24972 "template <typename AAAAAAA>\n"
24973 "requires Foo<AAAAAAAAAAAAAAAA>\n"
24974 "Bar(T) -> Bar<T>;",
24977 Style
.RequiresClausePosition
= FormatStyle::RCPS_WithFollowing
;
24978 ColumnStyle
.RequiresClausePosition
= FormatStyle::RCPS_WithFollowing
;
24980 verifyFormat("template <typename T>\n"
24981 "requires Foo<T> struct Bar {};\n"
24982 "template <typename T>\n"
24983 "requires Foo<T> void bar() {}\n"
24984 "template <typename T>\n"
24986 "requires Foo<T> {}\n"
24987 "template <typename T>\n"
24989 "requires Foo<T>;\n"
24990 "template <typename T>\n"
24991 "void S::bar() &&\n"
24992 "requires Foo<T> {}\n"
24993 "template <typename T>\n"
24994 "requires Foo<T> Bar(T) -> Bar<T>;",
24997 verifyFormat("template <typename AAAAAAA>\n"
24998 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25000 "template <typename AAAAAAA>\n"
25001 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25003 "template <typename AAAAAAA>\n"
25005 "requires Foo<AAAAAAAAAAAAAAAA> {}\n"
25006 "template <typename AAAAAAA>\n"
25007 "requires Foo<AAAAAAAA> Bar(T) -> Bar<T>;\n"
25008 "template <typename AAAAAAA>\n"
25009 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25010 "Bar(T) -> Bar<T>;",
25013 Style
.IndentRequiresClause
= true;
25014 ColumnStyle
.IndentRequiresClause
= true;
25016 verifyFormat("template <typename T>\n"
25017 " requires Foo<T> struct Bar {};\n"
25018 "template <typename T>\n"
25019 " requires Foo<T> void bar() {}\n"
25020 "template <typename T>\n"
25022 " requires Foo<T> {}\n"
25023 "template <typename T>\n"
25024 "void S::bar() &&\n"
25025 " requires Foo<T> {}\n"
25026 "template <typename T>\n"
25027 " requires Foo<T> Bar(T) -> Bar<T>;",
25030 verifyFormat("template <typename AAAAAAA>\n"
25031 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25033 "template <typename AAAAAAA>\n"
25034 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25036 "template <typename AAAAAAA>\n"
25038 " requires Foo<AAAAAAAAAAAAAAAA> {}\n"
25039 "template <typename AAAAAAA>\n"
25040 " requires Foo<AAAAAA> Bar(T) -> Bar<T>;\n"
25041 "template <typename AAAAAAA>\n"
25042 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25043 "Bar(T) -> Bar<T>;",
25046 Style
.RequiresClausePosition
= FormatStyle::RCPS_WithPreceding
;
25047 ColumnStyle
.RequiresClausePosition
= FormatStyle::RCPS_WithPreceding
;
25049 verifyFormat("template <typename T> requires Foo<T>\n"
25051 "template <typename T> requires Foo<T>\n"
25053 "template <typename T>\n"
25054 "void bar() requires Foo<T>\n"
25056 "template <typename T> void bar() requires Foo<T>;\n"
25057 "template <typename T>\n"
25058 "void S::bar() && requires Foo<T>\n"
25060 "template <typename T> requires Foo<T>\n"
25061 "Bar(T) -> Bar<T>;",
25064 verifyFormat("template <typename AAAAAAA>\n"
25065 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25067 "template <typename AAAAAAA>\n"
25068 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25070 "template <typename AAAAAAA>\n"
25072 " requires Foo<AAAAAAAAAAAAAAAA>\n"
25074 "template <typename AAAAAAA>\n"
25075 "requires Foo<AAAAAAAA>\n"
25076 "Bar(T) -> Bar<T>;\n"
25077 "template <typename AAAAAAA>\n"
25078 "requires Foo<AAAAAAAAAAAAAAAA>\n"
25079 "Bar(T) -> Bar<T>;",
25083 TEST_F(FormatTest
, RequiresClauses
) {
25084 verifyFormat("struct [[nodiscard]] zero_t {\n"
25085 " template <class T>\n"
25086 " requires requires { number_zero_v<T>; }\n"
25087 " [[nodiscard]] constexpr operator T() const {\n"
25088 " return number_zero_v<T>;\n"
25092 verifyFormat("template <class T>\n"
25093 " requires(std::same_as<int, T>)\n"
25094 "decltype(auto) fun() {}");
25096 auto Style
= getLLVMStyle();
25099 "template <typename T>\n"
25100 " requires is_default_constructible_v<hash<T>> and\n"
25101 " is_copy_constructible_v<hash<T>> and\n"
25102 " is_move_constructible_v<hash<T>> and\n"
25103 " is_copy_assignable_v<hash<T>> and "
25104 "is_move_assignable_v<hash<T>> and\n"
25105 " is_destructible_v<hash<T>> and is_swappable_v<hash<T>> and\n"
25106 " is_callable_v<hash<T>(T)> and\n"
25107 " is_same_v<size_t, decltype(hash<T>(declval<T>()))> and\n"
25108 " is_same_v<size_t, decltype(hash<T>(declval<T &>()))> and\n"
25109 " is_same_v<size_t, decltype(hash<T>(declval<const T &>()))>\n"
25113 Style
.BreakBeforeBinaryOperators
= FormatStyle::BOS_All
;
25115 "template <typename T>\n"
25116 " requires is_default_constructible_v<hash<T>>\n"
25117 " and is_copy_constructible_v<hash<T>>\n"
25118 " and is_move_constructible_v<hash<T>>\n"
25119 " and is_copy_assignable_v<hash<T>> and "
25120 "is_move_assignable_v<hash<T>>\n"
25121 " and is_destructible_v<hash<T>> and is_swappable_v<hash<T>>\n"
25122 " and is_callable_v<hash<T>(T)>\n"
25123 " and is_same_v<size_t, decltype(hash<T>(declval<T>()))>\n"
25124 " and is_same_v<size_t, decltype(hash<T>(declval<T &>()))>\n"
25125 " and is_same_v<size_t, decltype(hash<T>(declval<const T "
25130 Style
= getLLVMStyle();
25131 Style
.ConstructorInitializerIndentWidth
= 4;
25132 Style
.BreakConstructorInitializers
= FormatStyle::BCIS_BeforeColon
;
25133 Style
.PackConstructorInitializers
= FormatStyle::PCIS_Never
;
25134 verifyFormat("constexpr Foo(Foo const &other)\n"
25135 " requires std::is_copy_constructible<T>\n"
25136 " : value{other.value} {\n"
25138 " do_more_magic();\n"
25142 // Not a clause, but we once hit an assert.
25143 verifyFormat("#if 0\n"
25150 TEST_F(FormatTest
, RequiresExpressionIndentation
) {
25151 auto Style
= getLLVMStyle();
25152 EXPECT_EQ(Style
.RequiresExpressionIndentation
, FormatStyle::REI_OuterScope
);
25154 verifyFormat("template <typename T>\n"
25155 "concept C = requires(T t) {\n"
25156 " typename T::value;\n"
25157 " requires requires(typename T::value v) {\n"
25158 " { t == v } -> std::same_as<bool>;\n"
25163 verifyFormat("template <typename T>\n"
25165 " requires Foo<T> && requires(T t) {\n"
25166 " { t.foo() } -> std::same_as<int>;\n"
25167 " } && requires(T t) {\n"
25168 " { t.bar() } -> std::same_as<bool>;\n"
25173 verifyFormat("template <typename T>\n"
25174 " requires Foo<T> &&\n"
25175 " requires(T t) {\n"
25176 " { t.foo() } -> std::same_as<int>;\n"
25177 " } && requires(T t) {\n"
25178 " { t.bar() } -> std::same_as<bool>;\n"
25184 verifyFormat("template <typename T> void f() {\n"
25185 " if constexpr (requires(T t) {\n"
25186 " { t.bar() } -> std::same_as<bool>;\n"
25192 verifyFormat("template <typename T> void f() {\n"
25193 " if constexpr (condition && requires(T t) {\n"
25194 " { t.bar() } -> std::same_as<bool>;\n"
25200 verifyFormat("template <typename T> struct C {\n"
25202 " requires requires(T t) {\n"
25203 " { t.bar() } -> std::same_as<bool>;\n"
25208 Style
.RequiresExpressionIndentation
= FormatStyle::REI_Keyword
;
25210 verifyFormat("template <typename T>\n"
25211 "concept C = requires(T t) {\n"
25212 " typename T::value;\n"
25213 " requires requires(typename T::value v) {\n"
25214 " { t == v } -> std::same_as<bool>;\n"
25220 "template <typename T>\n"
25222 " requires Foo<T> && requires(T t) {\n"
25223 " { t.foo() } -> std::same_as<int>;\n"
25224 " } && requires(T t) {\n"
25225 " { t.bar() } -> std::same_as<bool>;\n"
25230 verifyFormat("template <typename T>\n"
25231 " requires Foo<T> &&\n"
25232 " requires(T t) {\n"
25233 " { t.foo() } -> std::same_as<int>;\n"
25234 " } && requires(T t) {\n"
25235 " { t.bar() } -> std::same_as<bool>;\n"
25241 verifyFormat("template <typename T> void f() {\n"
25242 " if constexpr (requires(T t) {\n"
25243 " { t.bar() } -> std::same_as<bool>;\n"
25250 "template <typename T> void f() {\n"
25251 " if constexpr (condition && requires(T t) {\n"
25252 " { t.bar() } -> std::same_as<bool>;\n"
25258 verifyFormat("template <typename T> struct C {\n"
25260 " requires requires(T t) {\n"
25261 " { t.bar() } -> std::same_as<bool>;\n"
25267 TEST_F(FormatTest
, StatementAttributeLikeMacros
) {
25268 FormatStyle Style
= getLLVMStyle();
25269 StringRef Source
= "void Foo::slot() {\n"
25270 " unsigned char MyChar = 'x';\n"
25271 " emit signal(MyChar);\n"
25272 " Q_EMIT signal(MyChar);\n"
25275 verifyFormat(Source
, Style
);
25277 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
25278 verifyFormat("void Foo::slot() {\n"
25279 " unsigned char MyChar = 'x';\n"
25280 " emit signal(MyChar);\n"
25281 " Q_EMIT signal(MyChar);\n"
25285 Style
.StatementAttributeLikeMacros
.push_back("emit");
25286 verifyFormat(Source
, Style
);
25288 Style
.StatementAttributeLikeMacros
= {};
25289 verifyFormat("void Foo::slot() {\n"
25290 " unsigned char MyChar = 'x';\n"
25291 " emit signal(MyChar);\n"
25292 " Q_EMIT signal(MyChar);\n"
25297 TEST_F(FormatTest
, IndentAccessModifiers
) {
25298 FormatStyle Style
= getLLVMStyle();
25299 Style
.IndentAccessModifiers
= true;
25300 // Members are *two* levels below the record;
25301 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation.
25302 verifyFormat("class C {\n"
25306 verifyFormat("union C {\n"
25311 // Access modifiers should be indented one level below the record.
25312 verifyFormat("class C {\n"
25317 verifyFormat("class C {\n"
25318 " public /* comment */:\n"
25322 verifyFormat("struct S {\n"
25335 // Enumerations are not records and should be unaffected.
25336 Style
.AllowShortEnumsOnASingleLine
= false;
25337 verifyFormat("enum class E {\n"
25342 // Test with a different indentation width;
25343 // also proves that the result is Style.AccessModifierOffset agnostic.
25344 Style
.IndentWidth
= 3;
25345 verifyFormat("class C {\n"
25350 verifyFormat("class C {\n"
25357 TEST_F(FormatTest
, LimitlessStringsAndComments
) {
25358 auto Style
= getLLVMStyleWithColumns(0);
25359 constexpr StringRef Code
=
25361 " * This is a multiline comment with quite some long lines, at least for "
25362 "the LLVM Style.\n"
25363 " * We will redo this with strings and line comments. Just to check if "
25364 "everything is working.\n"
25367 " /* Single line multi line comment. */\n"
25368 " const std::string String = \"This is a multiline string with quite "
25369 "some long lines, at least for the LLVM Style.\"\n"
25370 " \"We already did it with multi line "
25371 "comments, and we will do it with line comments. Just to check if "
25372 "everything is working.\";\n"
25373 " // This is a line comment (block) with quite some long lines, at "
25374 "least for the LLVM Style.\n"
25375 " // We already did this with multi line comments and strings. Just to "
25376 "check if everything is working.\n"
25377 " const std::string SmallString = \"Hello World\";\n"
25378 " // Small line comment\n"
25379 " return String.size() > SmallString.size();\n"
25381 verifyNoChange(Code
, Style
);
25384 TEST_F(FormatTest
, FormatDecayCopy
) {
25385 // error cases from unit tests
25386 verifyFormat("foo(auto())");
25387 verifyFormat("foo(auto{})");
25388 verifyFormat("foo(auto({}))");
25389 verifyFormat("foo(auto{{}})");
25391 verifyFormat("foo(auto(1))");
25392 verifyFormat("foo(auto{1})");
25393 verifyFormat("foo(new auto(1))");
25394 verifyFormat("foo(new auto{1})");
25395 verifyFormat("decltype(auto(1)) x;");
25396 verifyFormat("decltype(auto{1}) x;");
25397 verifyFormat("auto(x);");
25398 verifyFormat("auto{x};");
25399 verifyFormat("new auto{x};");
25400 verifyFormat("auto{x} = y;");
25401 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly
25402 // the user's own fault
25403 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is
25404 // clearly the user's own fault
25405 verifyFormat("auto (*p)() = f;");
25408 TEST_F(FormatTest
, Cpp20ModulesSupport
) {
25409 FormatStyle Style
= getLLVMStyle();
25410 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Never
;
25411 Style
.AllowShortFunctionsOnASingleLine
= FormatStyle::SFS_None
;
25413 verifyFormat("export import foo;", Style
);
25414 verifyFormat("export import foo:bar;", Style
);
25415 verifyFormat("export import foo.bar;", Style
);
25416 verifyFormat("export import foo.bar:baz;", Style
);
25417 verifyFormat("export import :bar;", Style
);
25418 verifyFormat("export module foo:bar;", Style
);
25419 verifyFormat("export module foo;", Style
);
25420 verifyFormat("export module foo.bar;", Style
);
25421 verifyFormat("export module foo.bar:baz;", Style
);
25422 verifyFormat("export import <string_view>;", Style
);
25423 verifyFormat("export import <Foo/Bar>;", Style
);
25425 verifyFormat("export type_name var;", Style
);
25426 verifyFormat("template <class T> export using A = B<T>;", Style
);
25427 verifyFormat("export using A = B;", Style
);
25428 verifyFormat("export int func() {\n"
25432 verifyFormat("export struct {\n"
25436 verifyFormat("export {\n"
25440 verifyFormat("export export char const *hello() { return \"hello\"; }");
25442 verifyFormat("import bar;", Style
);
25443 verifyFormat("import foo.bar;", Style
);
25444 verifyFormat("import foo:bar;", Style
);
25445 verifyFormat("import :bar;", Style
);
25446 verifyFormat("import /* module partition */ :bar;", Style
);
25447 verifyFormat("import <ctime>;", Style
);
25448 verifyFormat("import \"header\";", Style
);
25450 verifyFormat("module foo;", Style
);
25451 verifyFormat("module foo:bar;", Style
);
25452 verifyFormat("module foo.bar;", Style
);
25453 verifyFormat("module;", Style
);
25455 verifyFormat("export namespace hi {\n"
25456 "const char *sayhi();\n"
25460 verifyFormat("module :private;", Style
);
25461 verifyFormat("import <foo/bar.h>;", Style
);
25462 verifyFormat("import foo...bar;", Style
);
25463 verifyFormat("import ..........;", Style
);
25464 verifyFormat("module foo:private;", Style
);
25465 verifyFormat("import a", Style
);
25466 verifyFormat("module a", Style
);
25467 verifyFormat("export import a", Style
);
25468 verifyFormat("export module a", Style
);
25470 verifyFormat("import", Style
);
25471 verifyFormat("module", Style
);
25472 verifyFormat("export", Style
);
25474 verifyFormat("import /* not keyword */ = val ? 2 : 1;");
25477 TEST_F(FormatTest
, CoroutineForCoawait
) {
25478 FormatStyle Style
= getLLVMStyle();
25479 verifyFormat("for co_await (auto x : range())\n ;");
25480 verifyFormat("for (auto i : arr) {\n"
25483 verifyFormat("for co_await (auto i : arr) {\n"
25486 verifyFormat("for co_await (auto i : foo(T{})) {\n"
25491 TEST_F(FormatTest
, CoroutineCoAwait
) {
25492 verifyFormat("int x = co_await foo();");
25493 verifyFormat("int x = (co_await foo());");
25494 verifyFormat("co_await (42);");
25495 verifyFormat("void operator co_await(int);");
25496 verifyFormat("void operator co_await(a);");
25497 verifyFormat("co_await a;");
25498 verifyFormat("co_await missing_await_resume{};");
25499 verifyFormat("co_await a; // comment");
25500 verifyFormat("void test0() { co_await a; }");
25501 verifyFormat("co_await co_await co_await foo();");
25502 verifyFormat("co_await foo().bar();");
25503 verifyFormat("co_await [this]() -> Task { co_return x; }");
25504 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await "
25505 "foo(); }(x, y);");
25507 FormatStyle Style
= getLLVMStyleWithColumns(40);
25508 verifyFormat("co_await [this](int a, int b) -> Task {\n"
25509 " co_return co_await foo();\n"
25512 verifyFormat("co_await;");
25515 TEST_F(FormatTest
, CoroutineCoYield
) {
25516 verifyFormat("int x = co_yield foo();");
25517 verifyFormat("int x = (co_yield foo());");
25518 verifyFormat("co_yield (42);");
25519 verifyFormat("co_yield {42};");
25520 verifyFormat("co_yield 42;");
25521 verifyFormat("co_yield n++;");
25522 verifyFormat("co_yield ++n;");
25523 verifyFormat("co_yield;");
25526 TEST_F(FormatTest
, CoroutineCoReturn
) {
25527 verifyFormat("co_return (42);");
25528 verifyFormat("co_return;");
25529 verifyFormat("co_return {};");
25530 verifyFormat("co_return x;");
25531 verifyFormat("co_return co_await foo();");
25532 verifyFormat("co_return co_yield foo();");
25535 TEST_F(FormatTest
, EmptyShortBlock
) {
25536 auto Style
= getLLVMStyle();
25537 Style
.AllowShortBlocksOnASingleLine
= FormatStyle::SBS_Empty
;
25539 verifyFormat("try {\n"
25541 "} catch (Exception &e) {\n"
25542 " e.printStackTrace();\n"
25546 verifyFormat("try {\n"
25548 "} catch (Exception &e) {}",
25552 TEST_F(FormatTest
, ShortTemplatedArgumentLists
) {
25553 auto Style
= getLLVMStyle();
25555 verifyFormat("template <> struct S : Template<int (*)[]> {};", Style
);
25556 verifyFormat("template <> struct S : Template<int (*)[10]> {};", Style
);
25557 verifyFormat("struct Y : X<[] { return 0; }> {};", Style
);
25558 verifyFormat("struct Y<[] { return 0; }> {};", Style
);
25560 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style
);
25561 verifyFormat("template <int N> struct Foo<char[N]> {};", Style
);
25564 TEST_F(FormatTest
, MultilineLambdaInConditional
) {
25565 auto Style
= getLLVMStyleWithColumns(70);
25566 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
25573 "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n"
25579 Style
= getLLVMStyleWithColumns(60);
25580 verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
25587 verifyFormat("auto aLengthyIdentifier =\n"
25588 " oneExpressionSoThatWeBreak ? 2 : []() {\n"
25594 Style
= getLLVMStyleWithColumns(40);
25595 verifyFormat("auto aLengthyIdentifier =\n"
25596 " oneExpressionSoThatWeBreak ? []() {\n"
25602 verifyFormat("auto aLengthyIdentifier =\n"
25603 " oneExpressionSoThatWeBreak\n"
25612 TEST_F(FormatTest
, AlignAfterOpenBracketBlockIndent
) {
25613 auto Style
= getLLVMStyle();
25615 StringRef Short
= "functionCall(paramA, paramB, paramC);\n"
25616 "void functionDecl(int a, int b, int c);";
25618 StringRef Medium
= "functionCall(paramA, paramB, paramC, paramD, paramE, "
25619 "paramF, paramG, paramH, paramI);\n"
25620 "void functionDecl(int argumentA, int argumentB, int "
25621 "argumentC, int argumentD, int argumentE);";
25623 verifyFormat(Short
, Style
);
25625 StringRef NoBreak
= "functionCall(paramA, paramB, paramC, paramD, paramE, "
25626 "paramF, paramG, paramH,\n"
25628 "void functionDecl(int argumentA, int argumentB, int "
25629 "argumentC, int argumentD,\n"
25630 " int argumentE);";
25632 verifyFormat(NoBreak
, Medium
, Style
);
25633 verifyFormat(NoBreak
,
25645 "void functionDecl(\n"
25646 " int argumentA,\n"
25647 " int argumentB,\n"
25648 " int argumentC,\n"
25649 " int argumentD,\n"
25654 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
25655 " nestedLongFunctionCall(argument1, "
25656 "argument2, argument3,\n"
25661 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
25663 verifyFormat(Short
, Style
);
25666 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
25669 "void functionDecl(\n"
25670 " int argumentA, int argumentB, int argumentC, int argumentD, int "
25675 Style
.AllowAllArgumentsOnNextLine
= false;
25676 Style
.AllowAllParametersOfDeclarationOnNextLine
= false;
25678 verifyFormat(Short
, Style
);
25681 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
25684 "void functionDecl(\n"
25685 " int argumentA, int argumentB, int argumentC, int argumentD, int "
25690 Style
.BinPackArguments
= false;
25691 Style
.BinPackParameters
= false;
25693 verifyFormat(Short
, Style
);
25695 verifyFormat("functionCall(\n"
25706 "void functionDecl(\n"
25707 " int argumentA,\n"
25708 " int argumentB,\n"
25709 " int argumentC,\n"
25710 " int argumentD,\n"
25715 verifyFormat("outerFunctionCall(\n"
25716 " nestedFunctionCall(argument1),\n"
25717 " nestedLongFunctionCall(\n"
25727 verifyFormat("int a = (int)b;", Style
);
25728 verifyFormat("int a = (int)b;",
25734 verifyFormat("return (true);", Style
);
25735 verifyFormat("return (true);",
25741 verifyFormat("void foo();", Style
);
25742 verifyFormat("void foo();",
25747 verifyFormat("void foo() {}", Style
);
25748 verifyFormat("void foo() {}",
25754 verifyFormat("auto string = std::string();", Style
);
25755 verifyFormat("auto string = std::string();",
25756 "auto string = std::string(\n"
25760 verifyFormat("void (*functionPointer)() = nullptr;", Style
);
25761 verifyFormat("void (*functionPointer)() = nullptr;",
25763 " *functionPointer\n"
25770 TEST_F(FormatTest
, AlignAfterOpenBracketBlockIndentIfStatement
) {
25771 auto Style
= getLLVMStyle();
25773 verifyFormat("if (foo()) {\n"
25778 verifyFormat("if (quitelongarg !=\n"
25779 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
25785 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
25787 verifyFormat("if (foo()) {\n"
25792 verifyFormat("if (quitelongarg !=\n"
25793 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
25800 TEST_F(FormatTest
, AlignAfterOpenBracketBlockIndentForStatement
) {
25801 auto Style
= getLLVMStyle();
25803 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
25804 " doSomething();\n"
25808 verifyFormat("for (int myReallyLongCountVariable = 0; "
25809 "myReallyLongCountVariable < count;\n"
25810 " myReallyLongCountVariable++) {\n"
25811 " doSomething();\n"
25815 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
25817 verifyFormat("for (int i = 0; i < 5; ++i) {\n"
25818 " doSomething();\n"
25822 verifyFormat("for (int myReallyLongCountVariable = 0; "
25823 "myReallyLongCountVariable < count;\n"
25824 " myReallyLongCountVariable++) {\n"
25825 " doSomething();\n"
25830 TEST_F(FormatTest
, AlignAfterOpenBracketBlockIndentInitializers
) {
25831 auto Style
= getLLVMStyleWithColumns(60);
25832 Style
.AlignAfterOpenBracket
= FormatStyle::BAS_BlockIndent
;
25833 // Aggregate initialization.
25834 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
25835 " 10000000, 20000000\n"
25838 verifyFormat("SomeStruct s{\n"
25839 " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
25840 " \"zzzzzzzzzzzzzzzz\"\n"
25843 // Designated initializers.
25844 verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
25845 " [0] = 10000000, [1] = 20000000\n"
25848 verifyFormat("SomeStruct s{\n"
25849 " .foo = \"xxxxxxxxxxxxx\",\n"
25850 " .bar = \"yyyyyyyyyyyyy\",\n"
25851 " .baz = \"zzzzzzzzzzzzz\"\n"
25854 // List initialization.
25855 verifyFormat("SomeStruct s{\n"
25856 " \"xxxxxxxxxxxxx\",\n"
25857 " \"yyyyyyyyyyyyy\",\n"
25858 " \"zzzzzzzzzzzzz\",\n"
25861 verifyFormat("SomeStruct{\n"
25862 " \"xxxxxxxxxxxxx\",\n"
25863 " \"yyyyyyyyyyyyy\",\n"
25864 " \"zzzzzzzzzzzzz\",\n"
25867 verifyFormat("new SomeStruct{\n"
25868 " \"xxxxxxxxxxxxx\",\n"
25869 " \"yyyyyyyyyyyyy\",\n"
25870 " \"zzzzzzzzzzzzz\",\n"
25873 // Member initializer.
25874 verifyFormat("class SomeClass {\n"
25876 " \"xxxxxxxxxxxxx\",\n"
25877 " \"yyyyyyyyyyyyy\",\n"
25878 " \"zzzzzzzzzzzzz\",\n"
25882 // Constructor member initializer.
25883 verifyFormat("SomeClass::SomeClass : strct{\n"
25884 " \"xxxxxxxxxxxxx\",\n"
25885 " \"yyyyyyyyyyyyy\",\n"
25886 " \"zzzzzzzzzzzzz\",\n"
25889 // Copy initialization.
25890 verifyFormat("SomeStruct s = SomeStruct{\n"
25891 " \"xxxxxxxxxxxxx\",\n"
25892 " \"yyyyyyyyyyyyy\",\n"
25893 " \"zzzzzzzzzzzzz\",\n"
25896 // Copy list initialization.
25897 verifyFormat("SomeStruct s = {\n"
25898 " \"xxxxxxxxxxxxx\",\n"
25899 " \"yyyyyyyyyyyyy\",\n"
25900 " \"zzzzzzzzzzzzz\",\n"
25903 // Assignment operand initialization.
25904 verifyFormat("s = {\n"
25905 " \"xxxxxxxxxxxxx\",\n"
25906 " \"yyyyyyyyyyyyy\",\n"
25907 " \"zzzzzzzzzzzzz\",\n"
25910 // Returned object initialization.
25911 verifyFormat("return {\n"
25912 " \"xxxxxxxxxxxxx\",\n"
25913 " \"yyyyyyyyyyyyy\",\n"
25914 " \"zzzzzzzzzzzzz\",\n"
25917 // Initializer list.
25918 verifyFormat("auto initializerList = {\n"
25919 " \"xxxxxxxxxxxxx\",\n"
25920 " \"yyyyyyyyyyyyy\",\n"
25921 " \"zzzzzzzzzzzzz\",\n"
25924 // Function parameter initialization.
25925 verifyFormat("func({\n"
25926 " \"xxxxxxxxxxxxx\",\n"
25927 " \"yyyyyyyyyyyyy\",\n"
25928 " \"zzzzzzzzzzzzz\",\n"
25931 // Nested init lists.
25932 verifyFormat("SomeStruct s = {\n"
25933 " {{init1, init2, init3, init4, init5},\n"
25934 " {init1, init2, init3, init4, init5}}\n"
25937 verifyFormat("SomeStruct s = {\n"
25945 " {init1, init2, init3, init4, init5}}\n"
25948 verifyFormat("SomeArrayT a[3] = {\n"
25960 verifyFormat("SomeArrayT a[3] = {\n"
25979 TEST_F(FormatTest
, UnderstandsDigraphs
) {
25980 verifyFormat("int arr<:5:> = {};");
25981 verifyFormat("int arr[5] = <%%>;");
25982 verifyFormat("int arr<:::qualified_variable:> = {};");
25983 verifyFormat("int arr[::qualified_variable] = <%%>;");
25984 verifyFormat("%:include <header>");
25985 verifyFormat("%:define A x##y");
25986 verifyFormat("#define A x%:%:y");
25989 TEST_F(FormatTest
, AlignArrayOfStructuresLeftAlignmentNonSquare
) {
25990 auto Style
= getLLVMStyle();
25991 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Left
;
25992 Style
.AlignConsecutiveAssignments
.Enabled
= true;
25993 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
25995 // The AlignArray code is incorrect for non square Arrays and can cause
25996 // crashes, these tests assert that the array is not changed but will
25997 // also act as regression tests for when it is properly fixed
25998 verifyFormat("struct test demo[] = {\n"
26004 verifyFormat("struct test demo[] = {\n"
26005 " {1, 2, 3, 4, 5},\n"
26010 verifyFormat("struct test demo[] = {\n"
26011 " {1, 2, 3, 4, 5},\n"
26013 " {6, 7, 8, 9, 10, 11, 12}\n"
26016 verifyFormat("struct test demo[] = {\n"
26019 " {6, 7, 8, 9, 10, 11, 12}\n"
26023 verifyFormat("S{\n"
26029 verifyFormat("S{\n"
26035 verifyFormat("void foo() {\n"
26036 " auto thing = test{\n"
26038 " {13}, {something}, // A\n"
26043 " auto thing = test{\n"
26046 " {something}, // A\n"
26053 TEST_F(FormatTest
, AlignArrayOfStructuresRightAlignmentNonSquare
) {
26054 auto Style
= getLLVMStyle();
26055 Style
.AlignArrayOfStructures
= FormatStyle::AIAS_Right
;
26056 Style
.AlignConsecutiveAssignments
.Enabled
= true;
26057 Style
.AlignConsecutiveDeclarations
.Enabled
= true;
26059 // The AlignArray code is incorrect for non square Arrays and can cause
26060 // crashes, these tests assert that the array is not changed but will
26061 // also act as regression tests for when it is properly fixed
26062 verifyFormat("struct test demo[] = {\n"
26068 verifyFormat("struct test demo[] = {\n"
26069 " {1, 2, 3, 4, 5},\n"
26074 verifyFormat("struct test demo[] = {\n"
26075 " {1, 2, 3, 4, 5},\n"
26077 " {6, 7, 8, 9, 10, 11, 12}\n"
26080 verifyFormat("struct test demo[] = {\n"
26083 " {6, 7, 8, 9, 10, 11, 12}\n"
26087 verifyFormat("S{\n"
26093 verifyFormat("S{\n"
26099 verifyFormat("void foo() {\n"
26100 " auto thing = test{\n"
26102 " {13}, {something}, // A\n"
26107 " auto thing = test{\n"
26110 " {something}, // A\n"
26117 TEST_F(FormatTest
, FormatsVariableTemplates
) {
26118 verifyFormat("inline bool var = is_integral_v<int> && is_signed_v<int>;");
26119 verifyFormat("template <typename T> "
26120 "inline bool var = is_integral_v<T> && is_signed_v<T>;");
26123 TEST_F(FormatTest
, RemoveSemicolon
) {
26124 FormatStyle Style
= getLLVMStyle();
26125 Style
.RemoveSemicolon
= true;
26127 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
26128 "int max(int a, int b) { return a > b ? a : b; };", Style
);
26130 verifyFormat("int max(int a, int b) { return a > b ? a : b; }",
26131 "int max(int a, int b) { return a > b ? a : b; };;", Style
);
26133 verifyFormat("class Foo {\n"
26134 " int getSomething() const { return something; }\n"
26137 " int getSomething() const { return something; };\n"
26141 verifyFormat("class Foo {\n"
26142 " int getSomething() const { return something; }\n"
26145 " int getSomething() const { return something; };;\n"
26149 verifyFormat("for (;;) {\n"
26153 verifyFormat("class [[deprecated(\"\")]] C {\n"
26158 verifyFormat("struct EXPORT_MACRO [[nodiscard]] C {\n"
26163 verifyIncompleteFormat("class C final [[deprecated(l]] {});", Style
);
26165 // These tests are here to show a problem that may not be easily
26166 // solved, our implementation to remove semicolons is only as good
26167 // as our FunctionLBrace detection and this fails for empty braces
26168 // because we can't distringuish this from a bracelist.
26169 // We will enable when that is resolved.
26171 verifyFormat("void main() {}", "void main() {};", Style
);
26172 verifyFormat("void foo() {} //\n"
26174 "void foo() {}; //\n"
26180 TEST_F(FormatTest
, BreakAfterAttributes
) {
26181 FormatStyle Style
= getLLVMStyle();
26183 constexpr StringRef
Code("[[nodiscard]] inline int f(int &i);\n"
26184 "[[foo([[]])]] [[nodiscard]]\n"
26187 "inline int f(int &i) {\n"
26191 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
26196 EXPECT_EQ(Style
.BreakAfterAttributes
, FormatStyle::ABS_Leave
);
26197 verifyNoChange(Code
, Style
);
26199 Style
.BreakAfterAttributes
= FormatStyle::ABS_Never
;
26200 verifyFormat("[[nodiscard]] inline int f(int &i);\n"
26201 "[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
26202 "[[nodiscard]] inline int f(int &i) {\n"
26206 "[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
26212 Style
.BreakAfterAttributes
= FormatStyle::ABS_Always
;
26213 verifyFormat("[[nodiscard]]\n"
26214 "inline int f(int &i);\n"
26215 "[[foo([[]])]] [[nodiscard]]\n"
26218 "inline int f(int &i) {\n"
26222 "[[foo([[]])]] [[nodiscard]]\n"
26223 "int g(int &i) {\n"
26229 constexpr StringRef
CtorDtorCode("struct Foo {\n"
26230 " [[deprecated]] Foo();\n"
26231 " [[deprecated]] Foo() {}\n"
26232 " [[deprecated]] ~Foo();\n"
26233 " [[deprecated]] ~Foo() {}\n"
26234 " [[deprecated]] void f();\n"
26235 " [[deprecated]] void f() {}\n"
26237 "[[deprecated]] Bar::Bar() {}\n"
26238 "[[deprecated]] Bar::~Bar() {}\n"
26239 "[[deprecated]] void g() {}");
26240 verifyFormat("struct Foo {\n"
26241 " [[deprecated]]\n"
26243 " [[deprecated]]\n"
26245 " [[deprecated]]\n"
26247 " [[deprecated]]\n"
26249 " [[deprecated]]\n"
26251 " [[deprecated]]\n"
26260 CtorDtorCode
, Style
);
26262 Style
.BreakBeforeBraces
= FormatStyle::BS_Linux
;
26263 verifyFormat("struct Foo {\n"
26264 " [[deprecated]]\n"
26266 " [[deprecated]]\n"
26270 " [[deprecated]]\n"
26272 " [[deprecated]]\n"
26276 " [[deprecated]]\n"
26278 " [[deprecated]]\n"
26295 CtorDtorCode
, Style
);
26298 TEST_F(FormatTest
, InsertNewlineAtEOF
) {
26299 FormatStyle Style
= getLLVMStyle();
26300 Style
.InsertNewlineAtEOF
= true;
26302 verifyNoChange("int i;\n", Style
);
26303 verifyFormat("int i;\n", "int i;", Style
);
26306 TEST_F(FormatTest
, KeepEmptyLinesAtEOF
) {
26307 FormatStyle Style
= getLLVMStyle();
26308 Style
.KeepEmptyLinesAtEOF
= true;
26310 const StringRef Code
{"int i;\n\n"};
26311 verifyNoChange(Code
, Style
);
26312 verifyFormat(Code
, "int i;\n\n\n", Style
);
26315 TEST_F(FormatTest
, SpaceAfterUDL
) {
26316 verifyFormat("auto c = (4s).count();");
26317 verifyFormat("auto x = 5s .count() == 5;");
26320 TEST_F(FormatTest
, InterfaceAsClassMemberName
) {
26321 verifyFormat("class Foo {\n"
26322 " int interface;\n"
26323 " Foo::Foo(int iface) : interface{iface} {}\n"
26327 TEST_F(FormatTest
, PreprocessorOverlappingRegions
) {
26328 verifyFormat("#ifdef\n\n"
26339 TEST_F(FormatTest
, RemoveParentheses
) {
26340 FormatStyle Style
= getLLVMStyle();
26341 EXPECT_EQ(Style
.RemoveParentheses
, FormatStyle::RPS_Leave
);
26343 Style
.RemoveParentheses
= FormatStyle::RPS_MultipleParentheses
;
26344 verifyFormat("int x __attribute__((aligned(16))) = 0;", Style
);
26345 verifyFormat("decltype((foo->bar)) baz;", Style
);
26346 verifyFormat("class __declspec(dllimport) X {};",
26347 "class __declspec((dllimport)) X {};", Style
);
26348 verifyFormat("int x = (({ 0; }));", "int x = ((({ 0; })));", Style
);
26349 verifyFormat("while (a)\n"
26354 verifyFormat("while ((a = b))\n"
26356 "while (((a = b)))\n"
26359 verifyFormat("if (a)\n"
26364 verifyFormat("if constexpr ((a = b))\n"
26366 "if constexpr (((a = b)))\n"
26369 verifyFormat("if (({ a; }))\n"
26371 "if ((({ a; })))\n"
26374 verifyFormat("return (0);", "return (((0)));", Style
);
26375 verifyFormat("return (({ 0; }));", "return ((({ 0; })));", Style
);
26377 Style
.RemoveParentheses
= FormatStyle::RPS_ReturnStatement
;
26378 verifyFormat("return 0;", "return (0);", Style
);
26379 verifyFormat("co_return 0;", "co_return ((0));", Style
);
26380 verifyFormat("return 0;", "return (((0)));", Style
);
26381 verifyFormat("return ({ 0; });", "return ((({ 0; })));", Style
);
26382 verifyFormat("inline decltype(auto) f() {\n"
26388 "inline decltype(auto) f() {\n"
26395 verifyFormat("auto g() {\n"
26396 " decltype(auto) x = [] {\n"
26414 " decltype(auto) x = [] {\n"
26433 Style
.ColumnLimit
= 25;
26434 verifyFormat("return (a + b) - (c + d);",
26435 "return (((a + b)) -\n"
26440 TEST_F(FormatTest
, AllowBreakBeforeNoexceptSpecifier
) {
26441 auto Style
= getLLVMStyleWithColumns(35);
26443 EXPECT_EQ(Style
.AllowBreakBeforeNoexceptSpecifier
, FormatStyle::BBNSS_Never
);
26444 verifyFormat("void foo(int arg1,\n"
26445 " double arg2) noexcept;",
26448 // The following line does not fit within the 35 column limit, but that's what
26449 // happens with no break allowed.
26450 verifyFormat("void bar(int arg1, double arg2) noexcept(\n"
26451 " noexcept(baz(arg1)) &&\n"
26452 " noexcept(baz(arg2)));",
26455 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
26458 Style
.AllowBreakBeforeNoexceptSpecifier
= FormatStyle::BBNSS_Always
;
26459 verifyFormat("void foo(int arg1,\n"
26460 " double arg2) noexcept;",
26463 verifyFormat("void bar(int arg1, double arg2)\n"
26464 " noexcept(noexcept(baz(arg1)) &&\n"
26465 " noexcept(baz(arg2)));",
26468 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments()\n"
26472 Style
.AllowBreakBeforeNoexceptSpecifier
= FormatStyle::BBNSS_OnlyWithParen
;
26473 verifyFormat("void foo(int arg1,\n"
26474 " double arg2) noexcept;",
26477 verifyFormat("void bar(int arg1, double arg2)\n"
26478 " noexcept(noexcept(baz(arg1)) &&\n"
26479 " noexcept(baz(arg2)));",
26482 verifyFormat("void aVeryLongFunctionNameWithoutAnyArguments() noexcept;",
26486 TEST_F(FormatTest
, PPBranchesInBracedInit
) {
26487 verifyFormat("A a_{kFlag1,\n"
26505 TEST_F(FormatTest
, StreamOutputOperator
) {
26506 verifyFormat("std::cout << \"foo\" << \"bar\" << baz;");
26510 } // namespace test
26511 } // namespace format
26512 } // namespace clang