1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
15 #define DEBUG_TYPE "format-test"
21 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
23 class FormatTest : public ::testing::Test {
25 enum IncompleteCheck {
31 std::string format(llvm::StringRef Code,
32 const FormatStyle &Style = getLLVMStyle(),
33 IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
34 DEBUG(llvm::errs() << "---\n");
35 DEBUG(llvm::errs() << Code << "\n\n");
36 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
37 bool IncompleteFormat = false;
38 tooling::Replacements Replaces =
39 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
40 if (CheckIncomplete != IC_DoNotCheck) {
41 bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
42 EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
44 ReplacementCount = Replaces.size();
45 std::string Result = applyAllReplacements(Code, Replaces);
46 EXPECT_NE("", Result);
47 DEBUG(llvm::errs() << "\n" << Result << "\n\n");
51 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
52 FormatStyle Style = getLLVMStyle();
53 Style.ColumnLimit = ColumnLimit;
57 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
58 FormatStyle Style = getGoogleStyle();
59 Style.ColumnLimit = ColumnLimit;
63 void verifyFormat(llvm::StringRef Code,
64 const FormatStyle &Style = getLLVMStyle()) {
65 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
68 void verifyIncompleteFormat(llvm::StringRef Code,
69 const FormatStyle &Style = getLLVMStyle()) {
71 format(test::messUp(Code), Style, IC_ExpectIncomplete));
74 void verifyGoogleFormat(llvm::StringRef Code) {
75 verifyFormat(Code, getGoogleStyle());
78 void verifyIndependentOfContext(llvm::StringRef text) {
80 verifyFormat(llvm::Twine("void f() { " + text + " }").str());
83 /// \brief Verify that clang-format does not crash on the given input.
84 void verifyNoCrash(llvm::StringRef Code,
85 const FormatStyle &Style = getLLVMStyle()) {
86 format(Code, Style, IC_DoNotCheck);
92 TEST_F(FormatTest, MessUp) {
93 EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
94 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
95 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
96 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
97 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
100 //===----------------------------------------------------------------------===//
101 // Basic function tests.
102 //===----------------------------------------------------------------------===//
104 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
105 EXPECT_EQ(";", format(";"));
108 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
109 EXPECT_EQ("int i;", format(" int i;"));
110 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;"));
111 EXPECT_EQ("int i;\nint j;", format(" int i; int j;"));
112 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;"));
115 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
116 EXPECT_EQ("int i;", format("int\ni;"));
119 TEST_F(FormatTest, FormatsNestedBlockStatements) {
120 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}"));
123 TEST_F(FormatTest, FormatsNestedCall) {
124 verifyFormat("Method(f1, f2(f3));");
125 verifyFormat("Method(f1(f2, f3()));");
126 verifyFormat("Method(f1(f2, (f3())));");
129 TEST_F(FormatTest, NestedNameSpecifiers) {
130 verifyFormat("vector<::Type> v;");
131 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
132 verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
133 verifyFormat("bool a = 2 < ::SomeFunction();");
136 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
137 EXPECT_EQ("if (a) {\n"
140 format("if(a){f();}"));
141 EXPECT_EQ(4, ReplacementCount);
142 EXPECT_EQ("if (a) {\n"
148 EXPECT_EQ(0, ReplacementCount);
155 EXPECT_EQ(0, ReplacementCount);
158 TEST_F(FormatTest, RemovesEmptyLines) {
159 EXPECT_EQ("class C {\n"
167 // Don't remove empty lines at the start of namespaces or extern "C" blocks.
168 EXPECT_EQ("namespace N {\n"
172 format("namespace N {\n"
177 EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
181 format("extern /**/ \"C\" /**/ {\n"
187 // ...but do keep inlining and removing empty lines for non-block extern "C"
189 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
190 EXPECT_EQ("extern \"C\" int f() {\n"
194 format("extern \"C\" int f() {\n"
201 // Remove empty lines at the beginning and end of blocks.
202 EXPECT_EQ("void f() {\n"
209 format("void f() {\n"
219 EXPECT_EQ("void f() {\n"
224 format("void f() {\n"
235 // Don't remove empty lines in more complex control statements.
236 EXPECT_EQ("void f() {\n"
244 format("void f() {\n"
255 // FIXME: This is slightly inconsistent.
256 EXPECT_EQ("namespace {\n"
259 format("namespace {\n"
263 EXPECT_EQ("namespace {\n"
267 format("namespace {\n"
273 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
274 verifyFormat("x = (a) and (b);");
275 verifyFormat("x = (a) or (b);");
276 verifyFormat("x = (a) bitand (b);");
277 verifyFormat("x = (a) bitor (b);");
278 verifyFormat("x = (a) not_eq (b);");
279 verifyFormat("x = (a) and_eq (b);");
280 verifyFormat("x = (a) or_eq (b);");
281 verifyFormat("x = (a) xor (b);");
284 //===----------------------------------------------------------------------===//
285 // Tests for control statements.
286 //===----------------------------------------------------------------------===//
288 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
289 verifyFormat("if (true)\n f();\ng();");
290 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();");
291 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
293 FormatStyle AllowsMergedIf = getLLVMStyle();
294 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
295 verifyFormat("if (a)\n"
299 verifyFormat("if (a)\n"
302 verifyFormat("if (a)\n"
306 verifyFormat("if (a) // Can't merge this\n"
309 verifyFormat("if (a) /* still don't merge */\n"
312 verifyFormat("if (a) { // Never merge this\n"
316 verifyFormat("if (a) { /* Never merge this */\n"
321 AllowsMergedIf.ColumnLimit = 14;
322 verifyFormat("if (a) return;", AllowsMergedIf);
323 verifyFormat("if (aaaaaaaaa)\n"
327 AllowsMergedIf.ColumnLimit = 13;
328 verifyFormat("if (a)\n return;", AllowsMergedIf);
331 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
332 FormatStyle AllowsMergedLoops = getLLVMStyle();
333 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
334 verifyFormat("while (true) continue;", AllowsMergedLoops);
335 verifyFormat("for (;;) continue;", AllowsMergedLoops);
336 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
337 verifyFormat("while (true)\n"
340 verifyFormat("for (;;)\n"
343 verifyFormat("for (;;)\n"
344 " for (;;) continue;",
346 verifyFormat("for (;;) // Can't merge this\n"
349 verifyFormat("for (;;) /* still don't merge */\n"
354 TEST_F(FormatTest, FormatShortBracedStatements) {
355 FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
356 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
358 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
359 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
361 verifyFormat("if (true) {}", AllowSimpleBracedStatements);
362 verifyFormat("while (true) {}", AllowSimpleBracedStatements);
363 verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
364 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
365 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
366 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
367 verifyFormat("if (true) { //\n"
370 AllowSimpleBracedStatements);
371 verifyFormat("if (true) {\n"
375 AllowSimpleBracedStatements);
376 verifyFormat("if (true) {\n"
381 AllowSimpleBracedStatements);
383 verifyFormat("template <int> struct A2 {\n"
386 AllowSimpleBracedStatements);
388 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
389 verifyFormat("if (true) {\n"
392 AllowSimpleBracedStatements);
393 verifyFormat("if (true) {\n"
398 AllowSimpleBracedStatements);
400 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
401 verifyFormat("while (true) {\n"
404 AllowSimpleBracedStatements);
405 verifyFormat("for (;;) {\n"
408 AllowSimpleBracedStatements);
411 TEST_F(FormatTest, ParseIfElse) {
412 verifyFormat("if (true)\n"
422 verifyFormat("if (true)\n"
435 verifyFormat("void f() {\n"
442 TEST_F(FormatTest, ElseIf) {
443 verifyFormat("if (a) {\n} else if (b) {\n}");
444 verifyFormat("if (a)\n"
450 verifyFormat("if (a) {\n"
458 verifyFormat("if (a) {\n"
459 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
460 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
462 verifyFormat("if (a) {\n"
464 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
466 getLLVMStyleWithColumns(62));
469 TEST_F(FormatTest, FormatsForLoop) {
471 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
472 " ++VeryVeryLongLoopVariable)\n"
474 verifyFormat("for (;;)\n"
476 verifyFormat("for (;;) {\n}");
477 verifyFormat("for (;;) {\n"
480 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
483 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
484 " E = UnwrappedLines.end();\n"
485 " I != E; ++I) {\n}");
488 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
490 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
491 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
492 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
493 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
494 " I = FD->getDeclsInPrototypeScope().begin(),\n"
495 " E = FD->getDeclsInPrototypeScope().end();\n"
496 " I != E; ++I) {\n}");
497 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
498 " I = Container.begin(),\n"
499 " E = Container.end();\n"
500 " I != E; ++I) {\n}",
501 getLLVMStyleWithColumns(76));
504 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
508 " ++aaaaaaaaaaa) {\n}");
509 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
510 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
512 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
513 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
515 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
519 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
520 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
521 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
522 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
524 FormatStyle NoBinPacking = getLLVMStyle();
525 NoBinPacking.BinPackParameters = false;
526 verifyFormat("for (int aaaaaaaaaaa = 1;\n"
527 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
528 " aaaaaaaaaaaaaaaa,\n"
529 " aaaaaaaaaaaaaaaa,\n"
530 " aaaaaaaaaaaaaaaa);\n"
531 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
535 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
536 " E = UnwrappedLines.end();\n"
542 TEST_F(FormatTest, RangeBasedForLoops) {
543 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
545 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
546 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
547 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
549 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
550 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
553 TEST_F(FormatTest, ForEachLoops) {
554 verifyFormat("void f() {\n"
555 " foreach (Item *item, itemlist) {}\n"
556 " Q_FOREACH (Item *item, itemlist) {}\n"
557 " BOOST_FOREACH (Item *item, itemlist) {}\n"
558 " UNKNOWN_FORACH(Item * item, itemlist) {}\n"
561 // As function-like macros.
562 verifyFormat("#define foreach(x, y)\n"
563 "#define Q_FOREACH(x, y)\n"
564 "#define BOOST_FOREACH(x, y)\n"
565 "#define UNKNOWN_FOREACH(x, y)\n");
567 // Not as function-like macros.
568 verifyFormat("#define foreach (x, y)\n"
569 "#define Q_FOREACH (x, y)\n"
570 "#define BOOST_FOREACH (x, y)\n"
571 "#define UNKNOWN_FOREACH (x, y)\n");
574 TEST_F(FormatTest, FormatsWhileLoop) {
575 verifyFormat("while (true) {\n}");
576 verifyFormat("while (true)\n"
578 verifyFormat("while () {\n}");
579 verifyFormat("while () {\n"
584 TEST_F(FormatTest, FormatsDoWhile) {
585 verifyFormat("do {\n"
587 "} while (something());");
590 "while (something());");
593 TEST_F(FormatTest, FormatsSwitchStatement) {
594 verifyFormat("switch (x) {\n"
606 verifyFormat("switch (x) {\n"
615 verifyFormat("switch (x) {\n"
625 verifyFormat("switch (x) {\n"
635 verifyFormat("switch (x) {\n"
641 verifyFormat("switch (test)\n"
643 verifyFormat("switch (x) {\n"
648 verifyFormat("switch (x) {\n"
654 verifyFormat("switch (x) {\n"
656 " // Do amazing stuff\n"
663 verifyFormat("#define A \\\n"
668 getLLVMStyleWithColumns(20));
669 verifyFormat("#define OPERATION_CASE(name) \\\n"
670 " case OP_name: \\\n"
671 " return operations::Operation##name\n",
672 getLLVMStyleWithColumns(40));
673 verifyFormat("switch (x) {\n"
679 verifyGoogleFormat("switch (x) {\n"
691 verifyGoogleFormat("switch (x) {\n"
697 verifyGoogleFormat("switch (test)\n"
700 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
701 " case OP_name: \\\n"
702 " return operations::Operation##name\n");
703 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
704 " // Get the correction operation class.\n"
705 " switch (OpCode) {\n"
709 " return operations::Unknown;\n"
711 "#undef OPERATION_CASE\n"
713 verifyFormat("DEBUG({\n"
724 verifyFormat("switch (a) {\n"
729 verifyFormat("switch (a) {\n"
730 "case some_namespace::\n"
734 getLLVMStyleWithColumns(34));
737 TEST_F(FormatTest, CaseRanges) {
738 verifyFormat("switch (x) {\n"
739 "case 'A' ... 'Z':\n"
745 TEST_F(FormatTest, ShortCaseLabels) {
746 FormatStyle Style = getLLVMStyle();
747 Style.AllowShortCaseLabelsOnASingleLine = true;
748 verifyFormat("switch (a) {\n"
749 "case 1: x = 1; break;\n"
754 "case 6: // comment\n"
760 " x = 8; // comment\n"
762 "default: y = 1; break;\n"
765 verifyFormat("switch (a) {\n"
767 "case 0: return 0;\n"
771 verifyFormat("switch (a) {\n"
786 Style.ColumnLimit = 21;
787 verifyFormat("switch (a) {\n"
788 "case 1: x = 1; break;\n"
800 TEST_F(FormatTest, FormatsLabels) {
801 verifyFormat("void f() {\n"
804 " some_other_code();\n"
806 " some_more_code();\n"
808 " some_more_code();\n"
814 " some_other_code();\n"
823 //===----------------------------------------------------------------------===//
824 // Tests for comments.
825 //===----------------------------------------------------------------------===//
827 TEST_F(FormatTest, UnderstandsSingleLineComments) {
828 verifyFormat("//* */");
829 verifyFormat("// line 1\n"
833 verifyFormat("void f() {\n"
834 " // Doesn't do anything\n"
836 verifyFormat("SomeObject\n"
837 " // Calling someFunction on SomeObject\n"
838 " .someFunction();");
839 verifyFormat("auto result = SomeObject\n"
840 " // Calling someFunction on SomeObject\n"
841 " .someFunction();");
842 verifyFormat("void f(int i, // some comment (probably for i)\n"
843 " int j, // some comment (probably for j)\n"
844 " int k); // some comment (probably for k)");
845 verifyFormat("void f(int i,\n"
846 " // some comment (probably for j)\n"
848 " // some comment (probably for k)\n"
851 verifyFormat("int i // This is a fancy variable\n"
852 " = 5; // with nicely aligned comment.");
854 verifyFormat("// Leading comment.\n"
855 "int a; // Trailing comment.");
856 verifyFormat("int a; // Trailing comment\n"
860 verifyFormat("int a; // Trailing comment\n"
862 "// Leading comment.\n"
864 verifyFormat("int a; // Comment.\n"
865 " // More details.\n"
866 "int bbbb; // Another comment.");
868 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
869 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n"
870 "int cccccccccccccccccccccccccccccc; // comment\n"
871 "int ddd; // looooooooooooooooooooooooong comment\n"
872 "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
873 "int bbbbbbbbbbbbbbbbbbbbb; // comment\n"
874 "int ccccccccccccccccccc; // comment");
876 verifyFormat("#include \"a\" // comment\n"
877 "#include \"a/b/c\" // comment");
878 verifyFormat("#include <a> // comment\n"
879 "#include <a/b/c> // comment");
880 EXPECT_EQ("#include \"a\" // comment\n"
881 "#include \"a/b/c\" // comment",
882 format("#include \\\n"
883 " \"a\" // comment\n"
884 "#include \"a/b/c\" // comment"));
886 verifyFormat("enum E {\n"
888 " VAL_A, // comment\n"
893 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
894 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
895 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
896 " // Comment inside a statement.\n"
897 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
898 verifyFormat("SomeFunction(a,\n"
901 verifyFormat("SomeFunction(a, a,\n"
905 "bool aaaaaaaaaaaaa = // comment\n"
906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
907 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
909 verifyFormat("int aaaa; // aaaaa\n"
910 "int aa; // aaaaaaa",
911 getLLVMStyleWithColumns(20));
913 EXPECT_EQ("void f() { // This does something ..\n"
915 "int a; // This is unrelated",
916 format("void f() { // This does something ..\n"
918 "int a; // This is unrelated"));
919 EXPECT_EQ("class C {\n"
920 " void f() { // This does something ..\n"
923 " int a; // This is unrelated\n"
925 format("class C{void f() { // This does something ..\n"
928 "int a; // This is unrelated\n"
931 EXPECT_EQ("int i; // single line trailing comment",
932 format("int i;\\\n// single line trailing comment"));
934 verifyGoogleFormat("int a; // Trailing comment.");
936 verifyFormat("someFunction(anotherFunction( // Force break.\n"
939 verifyGoogleFormat("#endif // HEADER_GUARD");
941 verifyFormat("const char *test[] = {\n"
947 "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
948 " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
958 EXPECT_EQ("lineWith(); // comment\n"
961 format("lineWith(); // comment\n"
964 EXPECT_EQ("lineWith(); // comment\n"
967 format("lineWith(); // comment\n"
971 EXPECT_EQ("lineWith(); // comment\n"
973 "otherLine(); // comment",
974 format("lineWith(); // comment\n"
976 "otherLine(); // comment"));
977 EXPECT_EQ("lineWith();\n"
979 "otherLine(); // comment",
980 format("lineWith();\n"
982 "otherLine(); // comment"));
983 EXPECT_EQ("// first\n"
985 "otherLine(); // comment",
988 "otherLine(); // comment"));
992 "otherLine(); // comment",
996 "otherLine(); // comment"));
997 verifyFormat("f(); // comment\n"
1001 EXPECT_EQ("f(); // comment\n"
1005 format("f(); // comment\n"
1009 EXPECT_EQ("f(); // comment\n"
1013 format("f(); // comment\n"
1017 EXPECT_EQ("void f() {\n"
1018 " lineWith(); // comment\n"
1021 format("void f() {\n"
1022 " lineWith(); // comment\n"
1026 verifyFormat("#define A \\\n"
1027 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1028 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1029 getLLVMStyleWithColumns(60));
1032 " int i; /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1033 " int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1034 getLLVMStyleWithColumns(61));
1036 verifyFormat("if ( // This is some comment\n"
1039 EXPECT_EQ("if ( // This is some comment\n"
1040 " // spanning two lines\n"
1043 format("if( // This is some comment\n"
1044 " // spanning two lines\n"
1048 verifyNoCrash("/\\\n/");
1049 verifyNoCrash("/\\\n* */");
1050 // The 0-character somehow makes the lexer return a proper comment.
1051 verifyNoCrash(StringRef("/*\\\0\n/", 6));
1054 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
1055 EXPECT_EQ("SomeFunction(a,\n"
1058 format("SomeFunction(a,\n"
1061 EXPECT_EQ("SomeFunction(a, b,\n"
1064 format("SomeFunction(a,\n"
1068 EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
1070 format("SomeFunction(a, b, // comment (unclear relation)\n"
1072 EXPECT_EQ("SomeFunction(a, // comment\n"
1075 format("SomeFunction(a, // comment\n"
1077 " c); // comment"));
1080 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1081 EXPECT_EQ("// comment", format("// comment "));
1082 EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1083 format("int aaaaaaa, bbbbbbb; // comment ",
1084 getLLVMStyleWithColumns(33)));
1085 EXPECT_EQ("// comment\\\n", format("// comment\\\n \t \v \f "));
1086 EXPECT_EQ("// comment \\\n", format("// comment \\\n \t \v \f "));
1089 TEST_F(FormatTest, UnderstandsBlockComments) {
1090 verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1091 verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
1092 EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1093 " bbbbbbbbbbbbbbbbbbbbbbbbb);",
1094 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
1095 "/* Trailing comment for aa... */\n"
1096 " bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1098 "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1099 " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1100 format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
1101 "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1103 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1104 " aaaaaaaaaaaaaaaaaa,\n"
1105 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1107 format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1108 " aaaaaaaaaaaaaaaaaa ,\n"
1109 " aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1112 FormatStyle NoBinPacking = getLLVMStyle();
1113 NoBinPacking.BinPackParameters = false;
1114 verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1115 " /* parameter 2 */ aaaaaa,\n"
1116 " /* parameter 3 */ aaaaaa,\n"
1117 " /* parameter 4 */ aaaaaa);",
1120 // Aligning block comments in macros.
1121 verifyGoogleFormat("#define A \\\n"
1122 " int i; /*a*/ \\\n"
1126 TEST_F(FormatTest, AlignsBlockComments) {
1128 " * Really multi-line\n"
1133 " * Really multi-line\n"
1137 EXPECT_EQ("class C {\n"
1139 " * Another multi-line\n"
1144 format("class C {\n"
1146 " * Another multi-line\n"
1152 " 1. This is a comment with non-trivial formatting.\n"
1153 " 1.1. We have to indent/outdent all lines equally\n"
1154 " 1.1.1. to keep the formatting.\n"
1157 " 1. This is a comment with non-trivial formatting.\n"
1158 " 1.1. We have to indent/outdent all lines equally\n"
1159 " 1.1.1. to keep the formatting.\n"
1162 "Don't try to outdent if there's not enough indentation.\n"
1165 " Don't try to outdent if there's not enough indentation.\n"
1168 EXPECT_EQ("int i; /* Comment with empty...\n"
1171 format("int i; /* Comment with empty...\n"
1174 EXPECT_EQ("int foobar = 0; /* comment */\n"
1175 "int bar = 0; /* multiline\n"
1177 "int baz = 0; /* multiline\n"
1179 "int bzz = 0; /* multiline\n"
1181 format("int foobar = 0; /* comment */\n"
1182 "int bar = 0; /* multiline\n"
1184 "int baz = 0; /* multiline\n"
1186 "int bzz = 0; /* multiline\n"
1188 EXPECT_EQ("int foobar = 0; /* comment */\n"
1189 "int bar = 0; /* multiline\n"
1191 "int baz = 0; /* multiline\n"
1193 format("int foobar = 0; /* comment */\n"
1194 "int bar = 0; /* multiline\n"
1196 "int baz = 0; /* multiline\n"
1200 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
1201 FormatStyle Style = getLLVMStyleWithColumns(20);
1202 Style.ReflowComments = false;
1203 verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
1204 verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
1207 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1208 EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1209 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1210 format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1213 "void ffffffffffff(\n"
1214 " int aaaaaaaa, int bbbbbbbb,\n"
1215 " int cccccccccccc) { /*\n"
1222 format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1224 " aaaaaaaaaa aaaaaaaaaaaaa\n"
1225 " bbbbbbbbbbbbbb bbbbbbbbbb\n"
1228 getLLVMStyleWithColumns(40)));
1231 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1232 EXPECT_EQ("void ffffffffff(\n"
1233 " int aaaaa /* test */);",
1234 format("void ffffffffff(int aaaaa /* test */);",
1235 getLLVMStyleWithColumns(35)));
1238 TEST_F(FormatTest, SplitsLongCxxComments) {
1239 EXPECT_EQ("// A comment that\n"
1240 "// doesn't fit on\n"
1242 format("// A comment that doesn't fit on one line",
1243 getLLVMStyleWithColumns(20)));
1244 EXPECT_EQ("/// A comment that\n"
1245 "/// doesn't fit on\n"
1247 format("/// A comment that doesn't fit on one line",
1248 getLLVMStyleWithColumns(20)));
1249 EXPECT_EQ("//! A comment that\n"
1250 "//! doesn't fit on\n"
1252 format("//! A comment that doesn't fit on one line",
1253 getLLVMStyleWithColumns(20)));
1254 EXPECT_EQ("// a b c d\n"
1257 format("// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
1262 format("\\\n// a b c d e f g h i j k", getLLVMStyleWithColumns(10)));
1263 EXPECT_EQ("if (true) // A comment that\n"
1264 " // doesn't fit on\n"
1266 format("if (true) // A comment that doesn't fit on one line ",
1267 getLLVMStyleWithColumns(30)));
1268 EXPECT_EQ("// Don't_touch_leading_whitespace",
1269 format("// Don't_touch_leading_whitespace",
1270 getLLVMStyleWithColumns(20)));
1271 EXPECT_EQ("// Add leading\n"
1273 format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
1274 EXPECT_EQ("/// Add leading\n"
1276 format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
1277 EXPECT_EQ("//! Add leading\n"
1279 format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
1280 EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1281 EXPECT_EQ("// Even if it makes the line exceed the column\n"
1283 format("//Even if it makes the line exceed the column limit",
1284 getLLVMStyleWithColumns(51)));
1285 EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1287 EXPECT_EQ("// aa bb cc dd",
1288 format("// aa bb cc dd ",
1289 getLLVMStyleWithColumns(15)));
1291 EXPECT_EQ("// A comment before\n"
1295 format("// A comment before a macro definition\n"
1297 getLLVMStyleWithColumns(20)));
1298 EXPECT_EQ("void ffffff(\n"
1299 " int aaaaaaaaa, // wwww\n"
1300 " int bbbbbbbbbb, // xxxxxxx\n"
1302 " int c, int d, int e) {}",
1303 format("void ffffff(\n"
1304 " int aaaaaaaaa, // wwww\n"
1305 " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1306 " int c, int d, int e) {}",
1307 getLLVMStyleWithColumns(40)));
1308 EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1309 format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1310 getLLVMStyleWithColumns(20)));
1312 "#define XXX // a b c d\n"
1314 format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1316 "#define XXX // q w e r\n"
1318 format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1321 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1322 EXPECT_EQ("// A comment\n"
1326 format("// A comment that doesn't fit on one line",
1327 getLLVMStyleWithColumns(20)));
1328 EXPECT_EQ("/// A comment\n"
1329 "/// that doesn't\n"
1332 format("/// A comment that doesn't fit on one line",
1333 getLLVMStyleWithColumns(20)));
1336 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1337 EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1338 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1339 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1340 format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1341 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1342 "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1343 EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1344 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1345 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1346 format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1347 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1348 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1349 getLLVMStyleWithColumns(50)));
1350 // FIXME: One day we might want to implement adjustment of leading whitespace
1351 // of the consecutive lines in this kind of comment:
1352 EXPECT_EQ("double\n"
1353 " a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1354 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1355 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1356 format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1357 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1358 " // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1359 getLLVMStyleWithColumns(49)));
1362 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1363 FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1364 Pragmas.CommentPragmas = "^ IWYU pragma:";
1366 "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1367 format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1369 "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1370 format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1373 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1374 EXPECT_EQ("if (xxx ==\n"
1375 " yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1378 format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1380 getLLVMStyleWithColumns(40)));
1381 EXPECT_EQ("if (xxxxxxxxxx ==\n"
1382 " yyy && // aaaaaa bbbbbbbb cccc\n"
1385 format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1387 getLLVMStyleWithColumns(40)));
1388 EXPECT_EQ("if (xxxxxxxxxx &&\n"
1389 " yyy || // aaaaaa bbbbbbbb cccc\n"
1392 format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1394 getLLVMStyleWithColumns(40)));
1395 EXPECT_EQ("fffffffff(\n"
1396 " &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1398 format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1400 getLLVMStyleWithColumns(40)));
1403 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1404 EXPECT_EQ("#define A(x) /* \\\n"
1408 format("#define A(x) /* \\\n"
1412 getLLVMStyleWithColumns(17)));
1413 EXPECT_EQ("#define A( \\\n"
1418 format("#define A( \\\n"
1423 getLLVMStyleWithColumns(17)));
1426 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1427 EXPECT_EQ("namespace {}\n// Test\n#define A",
1428 format("namespace {}\n // Test\n#define A"));
1429 EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1430 format("namespace {}\n /* Test */\n#define A"));
1431 EXPECT_EQ("namespace {}\n/* Test */ #define A",
1432 format("namespace {}\n /* Test */ #define A"));
1435 TEST_F(FormatTest, SplitsLongLinesInComments) {
1436 EXPECT_EQ("/* This is a long\n"
1439 " * fit on one line.\n"
1445 "fit on one line. */",
1446 getLLVMStyleWithColumns(20)));
1452 format("/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
1458 format("\\\n/* a b c d e f g h i j k */", getLLVMStyleWithColumns(10)));
1461 "comment that doesn't\n"
1462 "fit on one line.\n"
1466 "comment that doesn't "
1467 "fit on one line. \n"
1469 getLLVMStyleWithColumns(20)));
1471 " * This is a long\n"
1473 " * doesn't fit on\n"
1477 " * This is a long "
1482 getLLVMStyleWithColumns(20)));
1484 " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1485 " * so_it_should_be_broken\n"
1486 " * wherever_a_space_occurs\n"
1489 " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1490 " so_it_should_be_broken "
1491 " wherever_a_space_occurs \n"
1493 getLLVMStyleWithColumns(20)));
1495 " * This_comment_can_not_be_broken_into_lines\n"
1498 " * This_comment_can_not_be_broken_into_lines\n"
1500 getLLVMStyleWithColumns(20)));
1503 " This is another\n"
1504 " long comment that\n"
1505 " doesn't fit on one\n"
1506 " line 1234567890\n"
1512 " long comment that "
1513 " doesn't fit on one"
1514 " line 1234567890\n"
1517 getLLVMStyleWithColumns(20)));
1521 " * another comment\n"
1522 " * t hat doesn' t\n"
1523 " * fit on one l i\n"
1536 getLLVMStyleWithColumns(20)));
1538 " * This is a long\n"
1540 " * doesn't fit on\n"
1544 " * This is a long comment that doesn't fit on one line\n"
1546 getLLVMStyleWithColumns(20)));
1548 " if (something) /* This is a\n"
1554 " if (something) /* This is a long comment */\n"
1557 getLLVMStyleWithColumns(30)));
1559 EXPECT_EQ("/* A comment before\n"
1561 " * definition */\n"
1563 format("/* A comment before a macro definition */\n"
1565 getLLVMStyleWithColumns(20)));
1567 EXPECT_EQ("/* some comment\n"
1570 " * another comment\n"
1571 "* we have to break\n"
1572 "* a left comment\n"
1574 format(" /* some comment\n"
1575 " * a comment that we break\n"
1576 " * another comment we have to break\n"
1577 "* a left comment\n"
1579 getLLVMStyleWithColumns(20)));
1582 " * multiline block\n"
1587 " * multiline block comment\n"
1590 getLLVMStyleWithColumns(20)));
1601 EXPECT_EQ("/* a a */",
1602 format("/* a a */", getLLVMStyleWithColumns(15)));
1603 EXPECT_EQ("/* a a bc */",
1604 format("/* a a bc */", getLLVMStyleWithColumns(15)));
1605 EXPECT_EQ("/* aaa aaa\n"
1607 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1608 EXPECT_EQ("/* aaa aaa\n"
1610 format("/* aaa aaa aaaaa */", getLLVMStyleWithColumns(15)));
1613 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1614 EXPECT_EQ("#define X \\\n"
1617 " Macro comment \\\n"
1622 format("#define X \\\n"
1625 " Macro comment with a long line\n"
1628 getLLVMStyleWithColumns(20)));
1629 EXPECT_EQ("#define X \\\n"
1630 " /* Macro comment \\\n"
1634 format("#define X \\\n"
1635 " /* Macro comment with a long\n"
1638 getLLVMStyleWithColumns(20)));
1639 EXPECT_EQ("#define X \\\n"
1640 " /* Macro comment \\\n"
1641 " * with a long \\\n"
1644 format("#define X \\\n"
1645 " /* Macro comment with a long line */ \\\n"
1647 getLLVMStyleWithColumns(20)));
1650 TEST_F(FormatTest, CommentsInStaticInitializers) {
1652 "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1653 " aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1654 " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1655 " aaaaaaaaaaaaaaaaaaaa, // comment\n"
1656 " aaaaaaaaaaaaaaaaaaaa};",
1657 format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n"
1658 " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n"
1659 " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n"
1660 " aaaaaaaaaaaaaaaaaaaa , // comment\n"
1661 " aaaaaaaaaaaaaaaaaaaa };"));
1662 verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1663 " bbbbbbbbbbb, ccccccccccc};");
1664 verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1665 " // comment for bb....\n"
1666 " bbbbbbbbbbb, ccccccccccc};");
1668 "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1669 " bbbbbbbbbbb, ccccccccccc};");
1670 verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1671 " // comment for bb....\n"
1672 " bbbbbbbbbbb, ccccccccccc};");
1674 verifyFormat("S s = {{a, b, c}, // Group #1\n"
1675 " {d, e, f}, // Group #2\n"
1676 " {g, h, i}}; // Group #3");
1677 verifyFormat("S s = {{// Group #1\n"
1684 EXPECT_EQ("S s = {\n"
1685 " // Some comment\n"
1688 " // Comment after empty line\n"
1691 " // Some comment\n"
1694 " // Comment after empty line\n"
1697 EXPECT_EQ("S s = {\n"
1698 " /* Some comment */\n"
1701 " /* Comment after empty line */\n"
1704 " /* Some comment */\n"
1707 " /* Comment after empty line */\n"
1710 verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1711 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1712 " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1713 " 0x00, 0x00, 0x00, 0x00}; // comment\n");
1716 TEST_F(FormatTest, IgnoresIf0Contents) {
1718 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1722 "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1725 EXPECT_EQ("#if false\n"
1729 format("#if false\n"
1732 "void g( ) { }\n"));
1733 EXPECT_EQ("enum E {\n"
1749 EXPECT_EQ("enum F {\n"
1771 EXPECT_EQ("enum G {\n"
1789 EXPECT_EQ("enum H {\n"
1811 EXPECT_EQ("enum I {\n"
1813 "#if /* test */ 0 || 1\n"
1821 "#if /* test */ 0 || 1\n"
1827 EXPECT_EQ("enum J {\n"
1853 //===----------------------------------------------------------------------===//
1854 // Tests for classes, namespaces, etc.
1855 //===----------------------------------------------------------------------===//
1857 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1858 verifyFormat("class A {};");
1861 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1862 verifyFormat("class A {\n"
1864 "public: // comment\n"
1869 verifyGoogleFormat("class A {\n"
1875 verifyFormat("class A {\n"
1880 "protected slots:\n"
1882 "protected Q_SLOTS:\n"
1886 "private Q_SLOTS:\n"
1894 // Don't interpret 'signals' the wrong way.
1895 verifyFormat("signals.set();");
1896 verifyFormat("for (Signals signals : f()) {\n}");
1898 " signals.set(); // This needs indentation.\n"
1902 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1903 EXPECT_EQ("class A {\n"
1913 format("class A {\n"
1922 EXPECT_EQ("class A {\n"
1927 format("class A {\n"
1935 // Even ensure proper spacing inside macros.
1936 EXPECT_EQ("#define B \\\n"
1942 format("#define B \\\n"
1951 // But don't remove empty lines after macros ending in access specifiers.
1952 EXPECT_EQ("#define A private:\n"
1955 format("#define A private:\n"
1960 TEST_F(FormatTest, FormatsClasses) {
1961 verifyFormat("class A : public B {};");
1962 verifyFormat("class A : public ::B {};");
1965 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1966 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1967 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1968 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1969 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1971 "class A : public B, public C, public D, public E, public F {};");
1972 verifyFormat("class AAAAAAAAAAAA : public B,\n"
1979 verifyFormat("class\n"
1980 " ReallyReallyLongClassName {\n"
1983 getLLVMStyleWithColumns(32));
1984 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1985 " aaaaaaaaaaaaaaaa> {};");
1986 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1987 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1988 " aaaaaaaaaaaaaaaaaaaaaa> {};");
1989 verifyFormat("template <class R, class C>\n"
1990 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1991 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1992 verifyFormat("class ::A::B {};");
1995 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1996 verifyFormat("class A {\n} a, b;");
1997 verifyFormat("struct A {\n} a, b;");
1998 verifyFormat("union A {\n} a;");
2001 TEST_F(FormatTest, FormatsEnum) {
2002 verifyFormat("enum {\n"
2006 " Three = (One + Two),\n"
2007 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
2008 " Five = (One, Two, Three, Four, 5)\n"
2010 verifyGoogleFormat("enum {\n"
2014 " Three = (One + Two),\n"
2015 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
2016 " Five = (One, Two, Three, Four, 5)\n"
2018 verifyFormat("enum Enum {};");
2019 verifyFormat("enum {};");
2020 verifyFormat("enum X E {} d;");
2021 verifyFormat("enum __attribute__((...)) E {} d;");
2022 verifyFormat("enum __declspec__((...)) E {} d;");
2023 verifyFormat("enum {\n"
2024 " Bar = Foo<int, int>::value\n"
2026 getLLVMStyleWithColumns(30));
2028 verifyFormat("enum ShortEnum { A, B, C };");
2029 verifyGoogleFormat("enum ShortEnum { A, B, C };");
2031 EXPECT_EQ("enum KeepEmptyLines {\n"
2038 format("enum KeepEmptyLines {\n"
2046 verifyFormat("enum E { // comment\n"
2052 verifyFormat("enum X f() {\n"
2056 verifyFormat("enum X Type::f() {\n"
2060 verifyFormat("enum ::X f() {\n"
2064 verifyFormat("enum ns::X f() {\n"
2070 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2071 verifyFormat("enum Type {\n"
2072 " One = 0; // These semicolons should be commas.\n"
2075 verifyFormat("namespace n {\n"
2078 " Two, // missing };\n"
2084 TEST_F(FormatTest, FormatsEnumStruct) {
2085 verifyFormat("enum struct {\n"
2089 " Three = (One + Two),\n"
2090 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
2091 " Five = (One, Two, Three, Four, 5)\n"
2093 verifyFormat("enum struct Enum {};");
2094 verifyFormat("enum struct {};");
2095 verifyFormat("enum struct X E {} d;");
2096 verifyFormat("enum struct __attribute__((...)) E {} d;");
2097 verifyFormat("enum struct __declspec__((...)) E {} d;");
2098 verifyFormat("enum struct X f() {\n a();\n return 42;\n}");
2101 TEST_F(FormatTest, FormatsEnumClass) {
2102 verifyFormat("enum class {\n"
2106 " Three = (One + Two),\n"
2107 " Four = (Zero && (One ^ Two)) | (One << Two),\n"
2108 " Five = (One, Two, Three, Four, 5)\n"
2110 verifyFormat("enum class Enum {};");
2111 verifyFormat("enum class {};");
2112 verifyFormat("enum class X E {} d;");
2113 verifyFormat("enum class __attribute__((...)) E {} d;");
2114 verifyFormat("enum class __declspec__((...)) E {} d;");
2115 verifyFormat("enum class X f() {\n a();\n return 42;\n}");
2118 TEST_F(FormatTest, FormatsEnumTypes) {
2119 verifyFormat("enum X : int {\n"
2120 " A, // Force multiple lines.\n"
2123 verifyFormat("enum X : int { A, B };");
2124 verifyFormat("enum X : std::uint32_t { A, B };");
2127 TEST_F(FormatTest, FormatsNSEnums) {
2128 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2129 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2130 " // Information about someDecentlyLongValue.\n"
2131 " someDecentlyLongValue,\n"
2132 " // Information about anotherDecentlyLongValue.\n"
2133 " anotherDecentlyLongValue,\n"
2134 " // Information about aThirdDecentlyLongValue.\n"
2135 " aThirdDecentlyLongValue\n"
2137 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2142 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2147 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2154 TEST_F(FormatTest, FormatsBitfields) {
2155 verifyFormat("struct Bitfields {\n"
2156 " unsigned sClass : 8;\n"
2157 " unsigned ValueKind : 2;\n"
2159 verifyFormat("struct A {\n"
2160 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2161 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2163 verifyFormat("struct MyStruct {\n"
2171 TEST_F(FormatTest, FormatsNamespaces) {
2172 verifyFormat("namespace some_namespace {\n"
2174 "void f() { f(); }\n"
2176 verifyFormat("namespace {\n"
2178 "void f() { f(); }\n"
2180 verifyFormat("inline namespace X {\n"
2182 "void f() { f(); }\n"
2184 verifyFormat("using namespace some_namespace;\n"
2186 "void f() { f(); }");
2188 // This code is more common than we thought; if we
2189 // layout this correctly the semicolon will go into
2190 // its own line, which is undesirable.
2191 verifyFormat("namespace {};");
2192 verifyFormat("namespace {\n"
2196 verifyFormat("namespace {\n"
2197 "int SomeVariable = 0; // comment\n"
2199 EXPECT_EQ("#ifndef HEADER_GUARD\n"
2200 "#define HEADER_GUARD\n"
2201 "namespace my_namespace {\n"
2203 "} // my_namespace\n"
2204 "#endif // HEADER_GUARD",
2205 format("#ifndef HEADER_GUARD\n"
2206 " #define HEADER_GUARD\n"
2207 " namespace my_namespace {\n"
2209 "} // my_namespace\n"
2210 "#endif // HEADER_GUARD"));
2212 EXPECT_EQ("namespace A::B {\n"
2215 format("namespace A::B {\n"
2219 FormatStyle Style = getLLVMStyle();
2220 Style.NamespaceIndentation = FormatStyle::NI_All;
2221 EXPECT_EQ("namespace out {\n"
2227 format("namespace out {\n"
2235 Style.NamespaceIndentation = FormatStyle::NI_Inner;
2236 EXPECT_EQ("namespace out {\n"
2242 format("namespace out {\n"
2251 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2253 TEST_F(FormatTest, FormatsInlineASM) {
2254 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2255 verifyFormat("asm(\"nop\" ::: \"memory\");");
2257 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2258 " \"cpuid\\n\\t\"\n"
2259 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2260 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2261 " : \"a\"(value));");
2263 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2265 " mov edx,[that] // vtable in edx\n"
2266 " mov eax,methodIndex\n"
2267 " call [edx][eax*4] // stdcall\n"
2270 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2272 " mov edx,[that] // vtable in edx\n"
2273 " mov eax,methodIndex\n"
2274 " call [edx][eax*4] // stdcall\n"
2277 EXPECT_EQ("_asm {\n"
2285 verifyFormat("void function() {\n"
2289 EXPECT_EQ("__asm {\n"
2297 TEST_F(FormatTest, FormatTryCatch) {
2298 verifyFormat("try {\n"
2300 "} catch (int a) {\n"
2306 // Function-level try statements.
2307 verifyFormat("int f() try { return 4; } catch (...) {\n"
2310 verifyFormat("class A {\n"
2312 " A() try : a(0) {\n"
2313 " } catch (...) {\n"
2318 // Incomplete try-catch blocks.
2319 verifyIncompleteFormat("try {} catch (");
2322 TEST_F(FormatTest, FormatSEHTryCatch) {
2323 verifyFormat("__try {\n"
2325 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2329 verifyFormat("__try {\n"
2335 verifyFormat("DEBUG({\n"
2342 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2343 verifyFormat("try {\n"
2348 verifyFormat("try {\n"
2350 "} catch (A a) MACRO(x) {\n"
2352 "} catch (B b) MACRO(x) {\n"
2357 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2358 FormatStyle Style = getLLVMStyle();
2359 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2360 FormatStyle::BS_WebKit}) {
2361 Style.BreakBeforeBraces = BraceStyle;
2362 verifyFormat("try {\n"
2369 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2370 verifyFormat("try {\n"
2377 verifyFormat("__try {\n"
2384 verifyFormat("@try {\n"
2391 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2392 verifyFormat("try\n"
2401 Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2402 verifyFormat("try\n"
2411 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2412 Style.BraceWrapping.BeforeCatch = true;
2413 verifyFormat("try {\n"
2422 TEST_F(FormatTest, FormatObjCTryCatch) {
2423 verifyFormat("@try {\n"
2425 "} @catch (NSException e) {\n"
2430 verifyFormat("DEBUG({\n"
2437 TEST_F(FormatTest, FormatObjCAutoreleasepool) {
2438 FormatStyle Style = getLLVMStyle();
2439 verifyFormat("@autoreleasepool {\n"
2442 "@autoreleasepool {\n"
2446 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2447 verifyFormat("@autoreleasepool\n"
2451 "@autoreleasepool\n"
2458 TEST_F(FormatTest, StaticInitializers) {
2459 verifyFormat("static SomeClass SC = {1, 'a'};");
2461 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2463 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2465 // Here, everything other than the "}" would fit on a line.
2466 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2467 " 10000000000000000000000000};");
2468 EXPECT_EQ("S s = {a,\n"
2477 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2478 // line. However, the formatting looks a bit off and this probably doesn't
2479 // happen often in practice.
2480 verifyFormat("static int Variable[1] = {\n"
2481 " {1000000000000000000000000000000000000}};",
2482 getLLVMStyleWithColumns(40));
2485 TEST_F(FormatTest, DesignatedInitializers) {
2486 verifyFormat("const struct A a = {.a = 1, .b = 2};");
2487 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2488 " .bbbbbbbbbb = 2,\n"
2489 " .cccccccccc = 3,\n"
2490 " .dddddddddd = 4,\n"
2491 " .eeeeeeeeee = 5};");
2492 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2493 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2494 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2495 " .ccccccccccccccccccccccccccc = 3,\n"
2496 " .ddddddddddddddddddddddddddd = 4,\n"
2497 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2499 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2502 TEST_F(FormatTest, NestedStaticInitializers) {
2503 verifyFormat("static A x = {{{}}};\n");
2504 verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2505 " {init1, init2, init3, init4}}};",
2506 getLLVMStyleWithColumns(50));
2508 verifyFormat("somes Status::global_reps[3] = {\n"
2509 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2510 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2511 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2512 getLLVMStyleWithColumns(60));
2513 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2514 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2515 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2516 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2517 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2518 " {rect.fRight - rect.fLeft, rect.fBottom - "
2522 "SomeArrayOfSomeType a = {\n"
2525 " {111111111111111111111111111111, 222222222222222222222222222222,\n"
2526 " 333333333333333333333333333333},\n"
2530 "SomeArrayOfSomeType a = {\n"
2533 " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2534 " 333333333333333333333333333333}},\n"
2538 verifyFormat("struct {\n"
2540 " const char *const name;\n"
2541 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2542 " {kOsWin, \"Windows\"},\n"
2543 " {kOsLinux, \"Linux\"},\n"
2544 " {kOsCrOS, \"Chrome OS\"}};");
2545 verifyFormat("struct {\n"
2547 " const char *const name;\n"
2548 "} kBitsToOs[] = {\n"
2549 " {kOsMac, \"Mac\"},\n"
2550 " {kOsWin, \"Windows\"},\n"
2551 " {kOsLinux, \"Linux\"},\n"
2552 " {kOsCrOS, \"Chrome OS\"},\n"
2556 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2557 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2559 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2562 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2563 verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2564 " OwningPtr<FileOutputBuffer> &buffer) = 0;");
2566 // Do break defaulted and deleted functions.
2567 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2569 getLLVMStyleWithColumns(40));
2570 verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2572 getLLVMStyleWithColumns(40));
2575 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2576 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2577 getLLVMStyleWithColumns(40));
2578 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2579 getLLVMStyleWithColumns(40));
2580 EXPECT_EQ("#define Q \\\n"
2581 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n"
2582 " \"aaaaaaaa.cpp\"",
2583 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2584 getLLVMStyleWithColumns(40)));
2587 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2588 EXPECT_EQ("# 123 \"A string literal\"",
2589 format(" # 123 \"A string literal\""));
2592 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2593 EXPECT_EQ("#;", format("#;"));
2594 verifyFormat("#\n;\n;\n;");
2597 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2598 EXPECT_EQ("#line 42 \"test\"\n",
2599 format("# \\\n line \\\n 42 \\\n \"test\"\n"));
2600 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n",
2601 getLLVMStyleWithColumns(12)));
2604 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2605 EXPECT_EQ("#line 42 \"test\"",
2606 format("# \\\n line \\\n 42 \\\n \"test\""));
2607 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B"));
2610 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2611 verifyFormat("#define A \\x20");
2612 verifyFormat("#define A \\ x20");
2613 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20"));
2614 verifyFormat("#define A ''");
2615 verifyFormat("#define A ''qqq");
2616 verifyFormat("#define A `qqq");
2617 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2618 EXPECT_EQ("const char *c = STRINGIFY(\n"
2620 format("const char * c = STRINGIFY(\n"
2623 verifyFormat("a\r\\");
2624 verifyFormat("a\v\\");
2625 verifyFormat("a\f\\");
2628 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2629 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2630 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12));
2631 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12));
2632 // FIXME: We never break before the macro name.
2633 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12));
2635 verifyFormat("#define A A\n#define A A");
2636 verifyFormat("#define A(X) A\n#define A A");
2638 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2639 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22));
2642 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2643 EXPECT_EQ("// somecomment\n"
2644 "#include \"a.h\"\n"
2647 "#include \"b.h\"\n"
2649 format(" // somecomment\n"
2650 " #include \"a.h\"\n"
2653 " #include \"b.h\"\n"
2654 " // somecomment\n",
2655 getLLVMStyleWithColumns(13)));
2658 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2660 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2661 EXPECT_EQ("#define A \\\n"
2665 format("#define A c; e;\n"
2667 getLLVMStyleWithColumns(14)));
2670 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2672 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2673 EXPECT_EQ("int x,\n"
2676 format("int x,\n#define A\ny;"));
2679 TEST_F(FormatTest, HashInMacroDefinition) {
2680 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2681 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
2682 verifyFormat("#define A \\\n"
2686 getLLVMStyleWithColumns(11));
2688 verifyFormat("#define A(X) \\\n"
2689 " void function##X()",
2690 getLLVMStyleWithColumns(22));
2692 verifyFormat("#define A(a, b, c) \\\n"
2694 getLLVMStyleWithColumns(22));
2696 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2699 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2700 EXPECT_EQ("#define A (x)", format("#define A (x)"));
2701 EXPECT_EQ("#define A(x)", format("#define A(x)"));
2704 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2705 EXPECT_EQ("#define A b;", format("#define A \\\n"
2708 getLLVMStyleWithColumns(25)));
2709 EXPECT_EQ("#define A \\\n"
2713 format("#define A \\\n"
2717 getLLVMStyleWithColumns(11)));
2718 EXPECT_EQ("#define A \\\n"
2722 format("#define A \\\n"
2726 getLLVMStyleWithColumns(11)));
2729 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2730 verifyIncompleteFormat("#define A :");
2731 verifyFormat("#define SOMECASES \\\n"
2734 getLLVMStyleWithColumns(20));
2735 verifyFormat("#define A template <typename T>");
2736 verifyIncompleteFormat("#define STR(x) #x\n"
2737 "f(STR(this_is_a_string_literal{));");
2738 verifyFormat("#pragma omp threadprivate( \\\n"
2739 " y)), // expected-warning",
2740 getLLVMStyleWithColumns(28));
2741 verifyFormat("#d, = };");
2742 verifyFormat("#if \"a");
2743 verifyIncompleteFormat("({\n"
2748 getLLVMStyleWithColumns(15));
2749 verifyFormat("#define A \\\n"
2755 getLLVMStyleWithColumns(15));
2756 verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2757 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2758 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2759 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}");
2762 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2763 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2764 EXPECT_EQ("class A : public QObject {\n"
2769 format("class A : public QObject {\n"
2775 "/*static*/ int i;",
2777 " /*static*/ int i;"));
2778 EXPECT_EQ("SOME_MACRO\n"
2782 format("SOME_MACRO\n"
2786 // Only if the identifier contains at least 5 characters.
2787 EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2788 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2789 // Only if everything is upper case.
2790 EXPECT_EQ("class A : public QObject {\n"
2791 " Q_Object A() {}\n"
2793 format("class A : public QObject {\n"
2798 // Only if the next line can actually start an unwrapped line.
2799 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2800 format("SOME_WEIRD_LOG_MACRO\n"
2803 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2805 getChromiumStyle(FormatStyle::LK_Cpp));
2808 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2809 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2810 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2811 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2813 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2814 "int *createScopDetectionPass() { return 0; }",
2815 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2816 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2817 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2819 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2820 " int *createScopDetectionPass() { return 0; }"));
2821 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2822 // braces, so that inner block is indented one level more.
2823 EXPECT_EQ("int q() {\n"
2824 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2825 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2826 " IPC_END_MESSAGE_MAP()\n"
2828 format("int q() {\n"
2829 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2830 " IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2831 " IPC_END_MESSAGE_MAP()\n"
2834 // Same inside macros.
2835 EXPECT_EQ("#define LIST(L) \\\n"
2839 format("#define LIST(L) \\\n"
2845 // These must not be recognized as macros.
2846 EXPECT_EQ("int q() {\n"
2864 " LOG(INFO) << x;\n"
2865 " ifstream(x) >> x;\n"
2867 format("int q() {\n"
2885 " LOG(INFO)\n << x;\n"
2886 " ifstream(x)\n >> x;\n"
2888 EXPECT_EQ("int q() {\n"
2900 " } catch (...) {\n"
2903 format("int q() {\n"
2911 "try { Q(); } catch (...) {}\n"
2913 EXPECT_EQ("class A {\n"
2915 " A(int i) noexcept() : {}\n"
2916 " A(X x)\n" // FIXME: function-level try blocks are broken.
2918 " } catch (...) {\n"
2921 format("class A {\n"
2922 " A()\n : t(0) {}\n"
2923 " A(int i)\n noexcept() : {}\n"
2925 " try : t(0) {} catch (...) {}\n"
2927 EXPECT_EQ("class SomeClass {\n"
2929 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2931 format("class SomeClass {\n"
2934 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2936 EXPECT_EQ("class SomeClass {\n"
2939 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2941 format("class SomeClass {\n"
2944 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2946 getLLVMStyleWithColumns(40)));
2948 verifyFormat("MACRO(>)");
2951 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2952 verifyFormat("#define A \\\n"
2956 getLLVMStyleWithColumns(11));
2959 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
2960 EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}"));
2963 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2964 verifyFormat("{\n { a #c; }\n}");
2967 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2968 EXPECT_EQ("#define A \\\n { \\\n {\nint i;",
2969 format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2970 EXPECT_EQ("#define A \\\n } \\\n }\nint i;",
2971 format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2974 TEST_F(FormatTest, EscapedNewlines) {
2976 "#define A \\\n int i; \\\n int j;",
2977 format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
2978 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2979 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2980 EXPECT_EQ("/* \\ \\ \\\n*/", format("\\\n/* \\ \\ \\\n*/"));
2981 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2984 TEST_F(FormatTest, DontCrashOnBlockComments) {
2986 "int xxxxxxxxx; /* "
2987 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2990 format("int xxxxxxxxx; /* "
2991 "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2995 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2996 verifyFormat("#define A \\\n"
3000 getLLVMStyleWithColumns(11));
3003 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3005 "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3007 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3009 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3010 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3011 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro("
3013 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3015 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3016 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3019 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3023 format("int\n#define A\na;"));
3024 verifyFormat("functionCallTo(\n"
3025 " someOtherFunction(\n"
3026 " withSomeParameters, whichInSequence,\n"
3027 " areLongerThanALine(andAnotherCall,\n"
3029 " withMoreParamters,\n"
3030 " whichStronglyInfluenceTheLayout),\n"
3031 " andMoreParameters),\n"
3033 getLLVMStyleWithColumns(69));
3034 verifyFormat("Foo::Foo()\n"
3040 verifyFormat("void f() {\n"
3050 verifyFormat("void f(param1, param2,\n"
3071 getLLVMStyleWithColumns(28));
3072 verifyFormat("#if 1\n"
3074 verifyFormat("#if 1\n"
3079 verifyFormat("DEBUG({\n"
3080 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3081 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3087 verifyIncompleteFormat("void f(\n"
3094 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3095 verifyFormat("#endif\n"
3099 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3100 FormatStyle SingleLine = getLLVMStyle();
3101 SingleLine.AllowShortIfStatementsOnASingleLine = true;
3102 verifyFormat("#if 0\n"
3106 " if (test) foo2();\n"
3111 TEST_F(FormatTest, LayoutBlockInsideParens) {
3112 verifyFormat("functionCall({ int i; });");
3113 verifyFormat("functionCall({\n"
3117 verifyFormat("functionCall(\n"
3122 " aaaa, bbbb, cccc);");
3123 verifyFormat("functionA(functionB({\n"
3127 " aaaa, bbbb, cccc);");
3128 verifyFormat("functionCall(\n"
3133 " aaaa, bbbb, // comment\n"
3135 verifyFormat("functionA(functionB({\n"
3139 " aaaa, bbbb, // comment\n"
3141 verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3142 verifyFormat("functionCall(aaaa, bbbb, {\n"
3147 "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3149 " int i; // break\n"
3151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3152 " ccccccccccccccccc));");
3153 verifyFormat("DEBUG({\n"
3159 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3160 EXPECT_EQ("SOME_MACRO { int i; }\n"
3162 format(" SOME_MACRO {int i;} int i;"));
3165 TEST_F(FormatTest, LayoutNestedBlocks) {
3166 verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3170 " s kBitsToOs[] = {{10}};\n"
3171 " for (int i = 0; i < 10; ++i)\n"
3174 verifyFormat("call(parameter, {\n"
3176 " // Comment using all columns.\n"
3177 " somethingelse();\n"
3179 getLLVMStyleWithColumns(40));
3180 verifyFormat("DEBUG( //\n"
3182 verifyFormat("DEBUG( //\n"
3188 EXPECT_EQ("call(parameter, {\n"
3191 " // looooooooooong.\n"
3192 " somethingElse();\n"
3194 format("call(parameter, {\n"
3196 " // Comment too looooooooooong.\n"
3197 " somethingElse();\n"
3199 getLLVMStyleWithColumns(29)));
3200 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });"));
3201 EXPECT_EQ("DEBUG({ // comment\n"
3204 format("DEBUG({ // comment\n"
3207 EXPECT_EQ("DEBUG({\n"
3220 verifyFormat("DEBUG({\n"
3224 verifyGoogleFormat("DEBUG({\n"
3227 FormatStyle Style = getGoogleStyle();
3228 Style.ColumnLimit = 45;
3229 verifyFormat("Debug(aaaaa,\n"
3231 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3236 verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3238 verifyNoCrash("^{v^{a}}");
3241 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3242 EXPECT_EQ("#define MACRO() \\\n"
3243 " Debug(aaa, /* force line break */ \\\n"
3248 format("#define MACRO() Debug(aaa, /* force line break */ \\\n"
3249 " { int i; int j; })",
3252 EXPECT_EQ("#define A \\\n"
3254 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3255 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3257 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3258 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3262 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3263 EXPECT_EQ("{}", format("{}"));
3264 verifyFormat("enum E {};");
3265 verifyFormat("enum E {}");
3268 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3269 FormatStyle Style = getLLVMStyle();
3270 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3271 Style.MacroBlockEnd = "^[A-Z_]+_END$";
3272 verifyFormat("FOO_BEGIN\n"
3275 verifyFormat("FOO_BEGIN\n"
3276 " NESTED_FOO_BEGIN\n"
3277 " NESTED_FOO_ENTRY\n"
3280 verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3283 "FOO_END(Baz)", Style);
3286 //===----------------------------------------------------------------------===//
3287 // Line break tests.
3288 //===----------------------------------------------------------------------===//
3290 TEST_F(FormatTest, PreventConfusingIndents) {
3293 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3294 " parameter, parameter, parameter)),\n"
3295 " SecondLongCall(parameter));\n"
3298 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3299 " aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3301 " aaaaaaaaaaaaaaaaaaaaaaaa);");
3303 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3304 " [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3305 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3306 " [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3308 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3309 " aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3310 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3311 " aaaaaaaaaaaaaaaaaaaaaaaa>;");
3312 verifyFormat("int a = bbbb && ccc && fffff(\n"
3313 "#define A Just forcing a new line\n"
3317 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3320 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3324 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3327 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3328 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3329 " ccccccccc == ddddddddddd;");
3330 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3331 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3332 " ccccccccc == ddddddddddd;");
3334 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3336 " ccccccccc == ddddddddddd;");
3338 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3340 " bbbbbb && cccccc;");
3341 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3344 verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3345 " SourceMgr.getSpellingColumnNumber(\n"
3346 " TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3349 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3350 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3352 verifyFormat("b = a &&\n"
3356 // If the LHS of a comparison is not a binary expression itself, the
3357 // additional linebreak confuses many people.
3359 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3363 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3364 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3367 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3370 // Even explicit parentheses stress the precedence enough to make the
3371 // additional break unnecessary.
3372 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3373 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3375 // This cases is borderline, but with the indentation it is still readable.
3377 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3378 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3381 getLLVMStyleWithColumns(75));
3383 // If the LHS is a binary expression, we should still use the additional break
3384 // as otherwise the formatting hides the operator precedence.
3385 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3390 FormatStyle OnePerLine = getLLVMStyle();
3391 OnePerLine.BinPackParameters = false;
3393 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3394 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3395 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3399 TEST_F(FormatTest, ExpressionIndentation) {
3400 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3402 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3403 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3404 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3405 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3407 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3408 " ccccccccccccccccccccccccccccccccccccccccc;");
3409 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3410 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3412 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3413 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3416 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3417 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3418 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3420 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3421 verifyFormat("if () {\n"
3422 "} else if (aaaaa &&\n"
3423 " bbbbb > // break\n"
3427 // Presence of a trailing comment used to change indentation of b.
3428 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3430 "return aaaaaaaaaaaaaaaaaaa +\n"
3432 getLLVMStyleWithColumns(30));
3435 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3436 // Not sure what the best system is here. Like this, the LHS can be found
3437 // immediately above an operator (everything with the same or a higher
3438 // indent). The RHS is aligned right of the operator and so compasses
3439 // everything until something with the same indent as the operator is found.
3440 // FIXME: Is this a good system?
3441 FormatStyle Style = getLLVMStyle();
3442 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3444 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3445 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3446 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3447 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3448 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3449 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3450 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3451 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3452 " > ccccccccccccccccccccccccccccccccccccccccc;",
3454 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3455 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3456 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3457 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3459 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3460 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3461 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3462 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3464 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3465 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3466 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3467 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3469 verifyFormat("if () {\n"
3470 "} else if (aaaaa\n"
3471 " && bbbbb // break\n"
3475 verifyFormat("return (a)\n"
3480 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3481 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3485 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3486 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3489 // Forced by comments.
3491 "unsigned ContentSize =\n"
3492 " sizeof(int16_t) // DWARF ARange version number\n"
3493 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3494 " + sizeof(int8_t) // Pointer Size (in bytes)\n"
3495 " + sizeof(int8_t); // Segment Size (in bytes)");
3497 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3498 " == boost::fusion::at_c<1>(iiii).second;",
3501 Style.ColumnLimit = 60;
3502 verifyFormat("zzzzzzzzzz\n"
3503 " = bbbbbbbbbbbbbbbbb\n"
3504 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3508 TEST_F(FormatTest, NoOperandAlignment) {
3509 FormatStyle Style = getLLVMStyle();
3510 Style.AlignOperands = false;
3511 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3512 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3513 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3514 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3515 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3516 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3517 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3518 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3519 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3520 " > ccccccccccccccccccccccccccccccccccccccccc;",
3523 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3524 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3527 verifyFormat("int a = aa\n"
3528 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3529 " * cccccccccccccccccccccccccccccccccccc;",
3532 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3533 verifyFormat("return (a > b\n"
3540 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3541 FormatStyle Style = getLLVMStyle();
3542 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3543 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3545 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3549 TEST_F(FormatTest, ConstructorInitializers) {
3550 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3551 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3552 getLLVMStyleWithColumns(45));
3553 verifyFormat("Constructor()\n"
3554 " : Inttializer(FitsOnTheLine) {}",
3555 getLLVMStyleWithColumns(44));
3556 verifyFormat("Constructor()\n"
3557 " : Inttializer(FitsOnTheLine) {}",
3558 getLLVMStyleWithColumns(43));
3560 verifyFormat("template <typename T>\n"
3561 "Constructor() : Initializer(FitsOnTheLine) {}",
3562 getLLVMStyleWithColumns(45));
3565 "SomeClass::Constructor()\n"
3566 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3569 "SomeClass::Constructor()\n"
3570 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3571 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3573 "SomeClass::Constructor()\n"
3574 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3575 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3576 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3577 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3578 " : aaaaaaaaaa(aaaaaa) {}");
3580 verifyFormat("Constructor()\n"
3581 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3582 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3583 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3584 " aaaaaaaaaaaaaaaaaaaaaaa() {}");
3586 verifyFormat("Constructor()\n"
3587 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3590 verifyFormat("Constructor(int Parameter = 0)\n"
3591 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3592 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3593 verifyFormat("Constructor()\n"
3594 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3596 getLLVMStyleWithColumns(60));
3597 verifyFormat("Constructor()\n"
3598 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3599 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3601 // Here a line could be saved by splitting the second initializer onto two
3602 // lines, but that is not desirable.
3603 verifyFormat("Constructor()\n"
3604 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3605 " aaaaaaaaaaa(aaaaaaaaaaa),\n"
3606 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3608 FormatStyle OnePerLine = getLLVMStyle();
3609 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3610 verifyFormat("SomeClass::Constructor()\n"
3611 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3612 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3613 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3615 verifyFormat("SomeClass::Constructor()\n"
3616 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3617 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3618 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3620 verifyFormat("MyClass::MyClass(int var)\n"
3621 " : some_var_(var), // 4 space indent\n"
3622 " some_other_var_(var + 1) { // lined up\n"
3625 verifyFormat("Constructor()\n"
3626 " : aaaaa(aaaaaa),\n"
3630 " aaaaa(aaaaaa) {}",
3632 verifyFormat("Constructor()\n"
3633 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3634 " aaaaaaaaaaaaaaaaaaaaaa) {}",
3636 OnePerLine.ColumnLimit = 60;
3637 verifyFormat("Constructor()\n"
3638 " : aaaaaaaaaaaaaaaaaaaa(a),\n"
3639 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3642 EXPECT_EQ("Constructor()\n"
3643 " : // Comment forcing unwanted break.\n"
3645 format("Constructor() :\n"
3646 " // Comment forcing unwanted break.\n"
3650 TEST_F(FormatTest, MemoizationTests) {
3651 // This breaks if the memoization lookup does not take \c Indent and
3652 // \c LastSpace into account.
3654 "extern CFRunLoopTimerRef\n"
3655 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3656 " CFTimeInterval interval, CFOptionFlags flags,\n"
3657 " CFIndex order, CFRunLoopTimerCallBack callout,\n"
3658 " CFRunLoopTimerContext *context) {}");
3660 // Deep nesting somewhat works around our memoization.
3662 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3663 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3664 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3665 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3666 " aaaaa())))))))))))))))))))))))))))))))))))))));",
3667 getLLVMStyleWithColumns(65));
3693 " aaaaa))))))))))));",
3694 getLLVMStyleWithColumns(65));
3696 "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"
3714 getLLVMStyleWithColumns(65));
3716 // This test takes VERY long when memoization is broken.
3717 FormatStyle OnePerLine = getLLVMStyle();
3718 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3719 OnePerLine.BinPackParameters = false;
3720 std::string input = "Constructor()\n"
3722 for (unsigned i = 0, e = 80; i != e; ++i) {
3726 verifyFormat(input, OnePerLine);
3729 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3732 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3733 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3736 verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3737 " Intervals[i - 1].getRange().getLast()) {\n}");
3740 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3741 // Principially, we break function declarations in a certain order:
3742 // 1) break amongst arguments.
3743 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3744 " Cccccccccccccc cccccccccccccc);");
3745 verifyFormat("template <class TemplateIt>\n"
3746 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3747 " TemplateIt *stop) {}");
3749 // 2) break after return type.
3751 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3752 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3755 // 3) break after (.
3757 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3758 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3761 // 4) break before after nested name specifiers.
3763 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3764 "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3765 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3768 // However, there are exceptions, if a sufficient amount of lines can be
3770 // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3772 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3773 " Cccccccccccccc cccccccccc,\n"
3774 " Cccccccccccccc cccccccccc,\n"
3775 " Cccccccccccccc cccccccccc,\n"
3776 " Cccccccccccccc cccccccccc);");
3778 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3779 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3780 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3781 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3784 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3785 " Cccccccccccccc cccccccccc,\n"
3786 " Cccccccccccccc cccccccccc,\n"
3787 " Cccccccccccccc cccccccccc,\n"
3788 " Cccccccccccccc cccccccccc,\n"
3789 " Cccccccccccccc cccccccccc,\n"
3790 " Cccccccccccccc cccccccccc);");
3791 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3792 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3793 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3794 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3795 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3797 // Break after multi-line parameters.
3798 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3802 verifyFormat("void SomeLoooooooooooongFunction(\n"
3803 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3804 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3805 " int bbbbbbbbbbbbb);");
3807 // Treat overloaded operators like other functions.
3808 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3809 "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3810 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3811 "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3812 verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3813 "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3815 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3816 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3818 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3819 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3820 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3821 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3822 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3823 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3825 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3826 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3827 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3829 FormatStyle Style = getLLVMStyle();
3830 Style.PointerAlignment = FormatStyle::PAS_Left;
3831 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3832 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3834 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3835 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3839 TEST_F(FormatTest, TrailingReturnType) {
3840 verifyFormat("auto foo() -> int;\n");
3841 verifyFormat("struct S {\n"
3842 " auto bar() const -> int;\n"
3844 verifyFormat("template <size_t Order, typename T>\n"
3845 "auto load_img(const std::string &filename)\n"
3846 " -> alias::tensor<Order, T, mem::tag::cpu> {}");
3847 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3848 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3849 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3850 verifyFormat("template <typename T>\n"
3851 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3852 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3854 // Not trailing return types.
3855 verifyFormat("void f() { auto a = b->c(); }");
3858 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3859 // Avoid breaking before trailing 'const' or other trailing annotations, if
3860 // they are not function-like.
3861 FormatStyle Style = getGoogleStyle();
3862 Style.ColumnLimit = 47;
3863 verifyFormat("void someLongFunction(\n"
3864 " int someLoooooooooooooongParameter) const {\n}",
3865 getLLVMStyleWithColumns(47));
3866 verifyFormat("LoooooongReturnType\n"
3867 "someLoooooooongFunction() const {}",
3868 getLLVMStyleWithColumns(47));
3869 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3872 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3873 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3874 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3875 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3876 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3877 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3878 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3879 " aaaaaaaaaaa aaaaa) const override;");
3881 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3882 " const override;");
3884 // Even if the first parameter has to be wrapped.
3885 verifyFormat("void someLongFunction(\n"
3886 " int someLongParameter) const {}",
3887 getLLVMStyleWithColumns(46));
3888 verifyFormat("void someLongFunction(\n"
3889 " int someLongParameter) const {}",
3891 verifyFormat("void someLongFunction(\n"
3892 " int someLongParameter) override {}",
3894 verifyFormat("void someLongFunction(\n"
3895 " int someLongParameter) OVERRIDE {}",
3897 verifyFormat("void someLongFunction(\n"
3898 " int someLongParameter) final {}",
3900 verifyFormat("void someLongFunction(\n"
3901 " int someLongParameter) FINAL {}",
3903 verifyFormat("void someLongFunction(\n"
3904 " int parameter) const override {}",
3907 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3908 verifyFormat("void someLongFunction(\n"
3909 " int someLongParameter) const\n"
3914 // Unless these are unknown annotations.
3915 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3916 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3917 " LONG_AND_UGLY_ANNOTATION;");
3919 // Breaking before function-like trailing annotations is fine to keep them
3920 // close to their arguments.
3921 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3922 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3923 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3924 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3925 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3926 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3927 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3928 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3929 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3932 "void aaaaaaaaaaaaaaaaaa()\n"
3933 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3934 " aaaaaaaaaaaaaaaaaaaaaaaaa));");
3935 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3936 " __attribute__((unused));");
3938 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3939 " GUARDED_BY(aaaaaaaaaaaa);");
3941 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3942 " GUARDED_BY(aaaaaaaaaaaa);");
3944 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3945 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3947 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3948 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
3951 TEST_F(FormatTest, FunctionAnnotations) {
3952 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3953 "int OldFunction(const string ¶meter) {}");
3954 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3955 "string OldFunction(const string ¶meter) {}");
3956 verifyFormat("template <typename T>\n"
3957 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3958 "string OldFunction(const string ¶meter) {}");
3960 // Not function annotations.
3961 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3962 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3963 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3964 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3967 TEST_F(FormatTest, BreaksDesireably) {
3968 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3969 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3970 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3971 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3972 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3976 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3977 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3979 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3980 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3981 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3984 "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3986 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3987 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3989 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3990 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3994 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3995 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3998 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3999 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4001 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4002 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4003 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4005 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4007 // Indent consistently independent of call expression and unary operator.
4008 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4009 " dddddddddddddddddddddddddddddd));");
4010 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4011 " dddddddddddddddddddddddddddddd));");
4012 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4013 " dddddddddddddddddddddddddddddd));");
4015 // This test case breaks on an incorrect memoization, i.e. an optimization not
4016 // taking into account the StopAt value.
4018 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4019 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4020 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4021 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4023 verifyFormat("{\n {\n {\n"
4024 " Annotation.SpaceRequiredBefore =\n"
4025 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4026 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4029 // Break on an outer level if there was a break on an inner level.
4030 EXPECT_EQ("f(g(h(a, // comment\n"
4034 format("f(g(h(a, // comment\n"
4035 " b, c), d, e), x, y);"));
4037 // Prefer breaking similar line breaks.
4039 "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4040 " NSTrackingMouseEnteredAndExited |\n"
4041 " NSTrackingActiveAlways;");
4044 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4045 FormatStyle NoBinPacking = getGoogleStyle();
4046 NoBinPacking.BinPackParameters = false;
4047 NoBinPacking.BinPackArguments = true;
4048 verifyFormat("void f() {\n"
4049 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4053 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4054 " int aaaaaaaaaaaaaaaaaaaa,\n"
4055 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4059 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4060 FormatStyle NoBinPacking = getGoogleStyle();
4061 NoBinPacking.BinPackParameters = false;
4062 NoBinPacking.BinPackArguments = false;
4063 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4064 " aaaaaaaaaaaaaaaaaaaa,\n"
4065 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4067 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4069 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4072 "aaaaaaaa(aaaaaaaaaaaaa,\n"
4073 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4075 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4078 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4079 " .aaaaaaaaaaaaaaaaaa();",
4081 verifyFormat("void f() {\n"
4082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4083 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4088 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4093 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4094 " ddddddddddddddddddddddddddddd),\n"
4098 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4099 " aaaaaaaaaaaaaaaaaaaaaaa,\n"
4100 " aaaaaaaaaaaaaaaaaaaaaaa>\n"
4101 " aaaaaaaaaaaaaaaaaa;",
4103 verifyFormat("a(\"a\"\n"
4107 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4108 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4114 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4119 "template <class SomeType, class SomeOtherType>\n"
4120 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4124 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4125 FormatStyle Style = getLLVMStyleWithColumns(15);
4126 Style.ExperimentalAutoDetectBinPacking = true;
4127 EXPECT_EQ("aaa(aaaa,\n"
4133 format("aaa(aaaa,\n" // one-per-line
4136 "aaa(aaaa, aaaa, aaaa);", // inconclusive
4138 EXPECT_EQ("aaa(aaaa, aaaa,\n"
4142 format("aaa(aaaa, aaaa,\n" // bin-packed
4144 "aaa(aaaa, aaaa, aaaa);", // inconclusive
4148 TEST_F(FormatTest, FormatsBuilderPattern) {
4149 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4150 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4151 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4152 " .StartsWith(\".init\", ORDER_INIT)\n"
4153 " .StartsWith(\".fini\", ORDER_FINI)\n"
4154 " .StartsWith(\".hash\", ORDER_HASH)\n"
4155 " .Default(ORDER_TEXT);\n");
4157 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4158 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4160 "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4162 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4164 "aaaaaaa->aaaaaaa\n"
4165 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4166 " ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4168 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4169 " aaaaaaaaaaaaaa);");
4171 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4172 " aaaaaa->aaaaaaaaaaaa()\n"
4173 " ->aaaaaaaaaaaaaaaa(\n"
4174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4175 " ->aaaaaaaaaaaaaaaaa();");
4178 " someo->Add((new util::filetools::Handler(dir))\n"
4179 " ->OnEvent1(NewPermanentCallback(\n"
4180 " this, &HandlerHolderClass::EventHandlerCBA))\n"
4181 " ->OnEvent2(NewPermanentCallback(\n"
4182 " this, &HandlerHolderClass::EventHandlerCBB))\n"
4183 " ->OnEvent3(NewPermanentCallback(\n"
4184 " this, &HandlerHolderClass::EventHandlerCBC))\n"
4185 " ->OnEvent5(NewPermanentCallback(\n"
4186 " this, &HandlerHolderClass::EventHandlerCBD))\n"
4187 " ->OnEvent6(NewPermanentCallback(\n"
4188 " this, &HandlerHolderClass::EventHandlerCBE)));\n"
4192 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4193 verifyFormat("aaaaaaaaaaaaaaa()\n"
4194 " .aaaaaaaaaaaaaaa()\n"
4195 " .aaaaaaaaaaaaaaa()\n"
4196 " .aaaaaaaaaaaaaaa()\n"
4197 " .aaaaaaaaaaaaaaa();");
4198 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4199 " .aaaaaaaaaaaaaaa()\n"
4200 " .aaaaaaaaaaaaaaa()\n"
4201 " .aaaaaaaaaaaaaaa();");
4202 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4203 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4204 " .aaaaaaaaaaaaaaa();");
4205 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4206 " ->aaaaaaaaaaaaaae(0)\n"
4207 " ->aaaaaaaaaaaaaaa();");
4209 // Don't linewrap after very short segments.
4210 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4211 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4212 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4213 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4214 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4215 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4216 verifyFormat("aaa()\n"
4217 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4218 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4219 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4221 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4222 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4223 " .has<bbbbbbbbbbbbbbbbbbbbb>();");
4224 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4225 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4226 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4228 // Prefer not to break after empty parentheses.
4229 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4230 " First->LastNewlineOffset);");
4232 // Prefer not to create "hanging" indents.
4234 "return !soooooooooooooome_map\n"
4235 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4239 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4241 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4242 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4244 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4245 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4247 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4248 " ccccccccccccccccccccccccc) {\n}");
4249 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4250 " ccccccccccccccccccccccccc) {\n}");
4252 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4253 " ccccccccccccccccccccccccc) {\n}");
4254 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4255 " ccccccccccccccccccccccccc) {\n}");
4258 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4259 " ccccccccccccccccccccccccc) {\n}");
4261 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4262 " ccccccccccccccccccccccccc) {\n}");
4264 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4265 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4266 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4267 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4268 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4269 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4270 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4271 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4273 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4274 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4275 " aaaaaaaaaaaaaaa != aa) {\n}");
4276 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4277 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4278 " aaaaaaaaaaaaaaa != aa) {\n}");
4281 TEST_F(FormatTest, BreaksAfterAssignments) {
4284 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4285 " SI->getPointerAddressSpaceee());\n");
4287 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4288 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4291 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4292 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4293 verifyFormat("unsigned OriginalStartColumn =\n"
4294 " SourceMgr.getSpellingColumnNumber(\n"
4295 " Current.FormatTok.getStartOfNonWhitespace()) -\n"
4299 TEST_F(FormatTest, AlignsAfterAssignments) {
4301 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4302 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
4304 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4305 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
4307 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4308 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
4310 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4311 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
4313 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4314 " aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4315 " aaaaaaaaaaaaaaaaaaaaaaaa;");
4318 TEST_F(FormatTest, AlignsAfterReturn) {
4320 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4321 " aaaaaaaaaaaaaaaaaaaaaaaaa;");
4323 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4324 " aaaaaaaaaaaaaaaaaaaaaaaaa);");
4326 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4327 " aaaaaaaaaaaaaaaaaaaaaa();");
4329 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4330 " aaaaaaaaaaaaaaaaaaaaaa());");
4331 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4332 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4333 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4336 verifyFormat("return\n"
4337 " // true if code is one of a or b.\n"
4338 " code == a || code == b;");
4341 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4343 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4344 " aaaaaaaaa aaaaaaa) {}");
4346 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4347 " aaaaaaaaaaa aaaaaaaaa);");
4349 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4350 " aaaaaaaaaaaaaaaaaaaaa));");
4351 FormatStyle Style = getLLVMStyle();
4352 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4353 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4354 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4356 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4357 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4359 verifyFormat("SomeLongVariableName->someFunction(\n"
4360 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4363 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4364 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4367 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4368 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4371 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4372 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4375 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4376 Style.BinPackArguments = false;
4377 Style.BinPackParameters = false;
4378 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4379 " aaaaaaaaaaa aaaaaaaa,\n"
4380 " aaaaaaaaa aaaaaaa,\n"
4381 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4383 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4384 " aaaaaaaaaaa aaaaaaaaa,\n"
4385 " aaaaaaaaaaa aaaaaaaaa,\n"
4386 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4388 verifyFormat("SomeLongVariableName->someFunction(\n"
4390 " aaaaaaaaaaaaaaa,\n"
4391 " aaaaaaaaaaaaaaaaaaaaa,\n"
4392 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4396 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4397 FormatStyle Style = getLLVMStyleWithColumns(40);
4398 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4399 " bbbbbbbbbbbbbbbbbbbbbb);",
4401 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4402 Style.AlignOperands = false;
4403 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4404 " bbbbbbbbbbbbbbbbbbbbbb);",
4406 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4407 Style.AlignOperands = true;
4408 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4409 " bbbbbbbbbbbbbbbbbbbbbb);",
4411 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4412 Style.AlignOperands = false;
4413 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4414 " bbbbbbbbbbbbbbbbbbbbbb);",
4418 TEST_F(FormatTest, BreaksConditionalExpressions) {
4420 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4421 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4422 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4424 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4425 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4427 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4428 " : aaaaaaaaaaaaa);");
4430 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4431 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4432 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4433 " aaaaaaaaaaaaa);");
4435 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4436 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4437 " aaaaaaaaaaaaa);");
4438 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4439 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4441 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4442 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4443 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4445 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4447 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4448 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4449 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4450 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4452 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4454 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4455 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4456 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4457 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4458 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4460 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4461 " : aaaaaaaaaaaaaaaa;");
4463 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4464 " ? aaaaaaaaaaaaaaa\n"
4465 " : aaaaaaaaaaaaaaa;");
4466 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4470 verifyFormat("return aaaa == bbbb\n"
4474 verifyFormat("unsigned Indent =\n"
4475 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
4476 " ? IndentForLevel[TheLine.Level]\n"
4478 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
4479 getLLVMStyleWithColumns(70));
4480 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4481 " ? aaaaaaaaaaaaaaa\n"
4482 " : bbbbbbbbbbbbbbb //\n"
4483 " ? ccccccccccccccc\n"
4484 " : ddddddddddddddd;");
4485 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4486 " ? aaaaaaaaaaaaaaa\n"
4487 " : (bbbbbbbbbbbbbbb //\n"
4488 " ? ccccccccccccccc\n"
4489 " : ddddddddddddddd);");
4491 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4492 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4493 " aaaaaaaaaaaaaaaaaaaaa +\n"
4494 " aaaaaaaaaaaaaaaaaaaaa\n"
4497 "aaaaaa = aaaaaaaaaaaa\n"
4498 " ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4499 " : aaaaaaaaaaaaaaaaaaaaaa\n"
4500 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4502 FormatStyle NoBinPacking = getLLVMStyle();
4503 NoBinPacking.BinPackArguments = false;
4507 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4509 " ? aaaaaaaaaaaaaaa\n"
4510 " : aaaaaaaaaaaaaaa);\n"
4516 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4517 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4518 " ?: aaaaaaaaaaaaaaa);\n"
4522 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4524 " ccccccccccccccccccccccccccccccccccccccc\n"
4525 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4526 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4528 // Assignments in conditional expressions. Apparently not uncommon :-(.
4529 verifyFormat("return a != b\n"
4533 verifyFormat("return a != b\n"
4540 verifyFormat("return a != b\n"
4549 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4550 FormatStyle Style = getLLVMStyle();
4551 Style.BreakBeforeTernaryOperators = false;
4552 Style.ColumnLimit = 70;
4554 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4555 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4559 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4560 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4563 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4567 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4568 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4573 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4574 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4577 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4579 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4581 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4583 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4584 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4585 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4586 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4589 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4591 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4592 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4594 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4595 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4597 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4598 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4599 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4601 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4602 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4607 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4608 " aaaaaaaaaaaaaaa :\n"
4609 " aaaaaaaaaaaaaaa;",
4611 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4617 "unsigned Indent =\n"
4618 " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
4619 " IndentForLevel[TheLine.Level] :\n"
4621 " TheLine.InPPDirective, PreviousEndOfLineColumn);",
4623 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4624 " aaaaaaaaaaaaaaa :\n"
4625 " bbbbbbbbbbbbbbb ? //\n"
4626 " ccccccccccccccc :\n"
4627 " ddddddddddddddd;",
4629 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4630 " aaaaaaaaaaaaaaa :\n"
4631 " (bbbbbbbbbbbbbbb ? //\n"
4632 " ccccccccccccccc :\n"
4633 " ddddddddddddddd);",
4637 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4638 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4639 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4640 verifyFormat("bool a = true, b = false;");
4642 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4643 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4644 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4645 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4647 "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4648 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4650 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4651 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4652 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4653 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4654 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4655 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4657 FormatStyle Style = getGoogleStyle();
4658 Style.PointerAlignment = FormatStyle::PAS_Left;
4659 Style.DerivePointerAlignment = false;
4660 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4661 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4662 " *b = bbbbbbbbbbbbbbbbbbb;",
4664 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4665 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4669 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4670 verifyFormat("arr[foo ? bar : baz];");
4671 verifyFormat("f()[foo ? bar : baz];");
4672 verifyFormat("(a + b)[foo ? bar : baz];");
4673 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4676 TEST_F(FormatTest, AlignsStringLiterals) {
4677 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4678 " \"short literal\");");
4680 "looooooooooooooooooooooooongFunction(\n"
4681 " \"short literal\"\n"
4682 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4683 verifyFormat("someFunction(\"Always break between multi-line\"\n"
4684 " \" string literals\",\n"
4685 " and, other, parameters);");
4686 EXPECT_EQ("fun + \"1243\" /* comment */\n"
4688 format("fun + \"1243\" /* comment */\n"
4690 getLLVMStyleWithColumns(28)));
4692 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4693 " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4694 " \"aaaaaaaaaaaaaaaa\";",
4696 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4697 "aaaaaaaaaaaaaaaaaaaaa\" "
4698 "\"aaaaaaaaaaaaaaaa\";"));
4699 verifyFormat("a = a + \"a\"\n"
4702 verifyFormat("f(\"a\", \"b\"\n"
4706 "#define LL_FORMAT \"ll\"\n"
4707 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4708 " \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4710 verifyFormat("#define A(X) \\\n"
4711 " \"aaaaa\" #X \"bbbbbb\" \\\n"
4713 getLLVMStyleWithColumns(23));
4714 verifyFormat("#define A \"def\"\n"
4715 "f(\"abc\" A \"ghi\"\n"
4718 verifyFormat("f(L\"a\"\n"
4720 verifyFormat("#define A(X) \\\n"
4721 " L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4723 getLLVMStyleWithColumns(25));
4725 verifyFormat("f(@\"a\"\n"
4727 verifyFormat("NSString s = @\"a\"\n"
4730 verifyFormat("NSString s = @\"a\"\n"
4735 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4736 FormatStyle Style = getLLVMStyle();
4737 // No declarations or definitions should be moved to own line.
4738 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4739 verifyFormat("class A {\n"
4740 " int f() { return 1; }\n"
4743 "int f() { return 1; }\n"
4747 // All declarations and definitions should have the return type moved to its
4750 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4751 verifyFormat("class E {\n"
4767 // Top-level definitions, and no kinds of declarations should have the
4768 // return type moved to its own line.
4769 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4770 verifyFormat("class B {\n"
4771 " int f() { return 1; }\n"
4781 // Top-level definitions and declarations should have the return type moved
4783 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4784 verifyFormat("class C {\n"
4785 " int f() { return 1; }\n"
4796 // All definitions should have the return type moved to its own line, but no
4797 // kinds of declarations.
4798 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4799 verifyFormat("class D {\n"
4812 verifyFormat("const char *\n"
4813 "f(void) {\n" // Break here.
4816 "const char *bar(void);\n", // No break here.
4818 verifyFormat("template <class T>\n"
4820 "f(T &c) {\n" // Break here.
4823 "template <class T> T *f(T &c);\n", // No break here.
4825 verifyFormat("class C {\n"
4836 verifyFormat("void\n"
4837 "A::operator()() {}\n"
4839 "A::operator>>() {}\n"
4841 "A::operator+() {}\n",
4843 verifyFormat("void *operator new(std::size_t s);", // No break here.
4845 verifyFormat("void *\n"
4846 "operator new(std::size_t s) {}",
4848 verifyFormat("void *\n"
4849 "operator delete[](void *ptr) {}",
4851 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4852 verifyFormat("const char *\n"
4853 "f(void)\n" // Break here.
4857 "const char *bar(void);\n", // No break here.
4859 verifyFormat("template <class T>\n"
4860 "T *\n" // Problem here: no line break
4861 "f(T &c)\n" // Break here.
4865 "template <class T> T *f(T &c);\n", // No break here.
4869 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4870 FormatStyle NoBreak = getLLVMStyle();
4871 NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4872 FormatStyle Break = getLLVMStyle();
4873 Break.AlwaysBreakBeforeMultilineStrings = true;
4874 verifyFormat("aaaa = \"bbbb\"\n"
4877 verifyFormat("aaaa =\n"
4881 verifyFormat("aaaa(\"bbbb\"\n"
4884 verifyFormat("aaaa(\n"
4888 verifyFormat("aaaa(qqq, \"bbbb\"\n"
4891 verifyFormat("aaaa(qqq,\n"
4895 verifyFormat("aaaa(qqq,\n"
4899 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4902 verifyFormat("string s = someFunction(\n"
4907 // As we break before unary operators, breaking right after them is bad.
4908 verifyFormat("string foo = abc ? \"x\"\n"
4909 " \"blah blah blah blah blah blah\"\n"
4913 // Don't break if there is no column gain.
4914 verifyFormat("f(\"aaaa\"\n"
4918 // Treat literals with escaped newlines like multi-line string literals.
4919 EXPECT_EQ("x = \"a\\\n"
4922 format("x = \"a\\\n"
4926 EXPECT_EQ("xxxx =\n"
4930 format("xxxx = \"a\\\n"
4935 // Exempt ObjC strings for now.
4936 EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
4938 format("NSString *const kString = @\"aaaa\"\n"
4942 Break.ColumnLimit = 0;
4943 verifyFormat("const char *hello = \"hello llvm\";", Break);
4946 TEST_F(FormatTest, AlignsPipes) {
4948 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4949 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4950 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4952 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4953 " << aaaaaaaaaaaaaaaaaaaa;");
4955 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4956 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4958 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4959 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4960 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4962 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4963 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4964 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4965 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4966 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4967 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4968 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4969 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
4970 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
4972 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4973 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4975 verifyFormat("return out << \"somepacket = {\\n\"\n"
4976 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4977 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4978 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4979 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4982 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4983 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4984 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4986 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4987 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4988 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4989 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4990 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4991 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4992 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4995 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4996 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4998 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4999 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5000 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5002 " aaaaaaaaaaaaaaaaaaaaa)\n"
5003 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5004 verifyFormat("LOG_IF(aaa == //\n"
5008 // Breaking before the first "<<" is generally not desirable.
5011 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5012 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5013 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5014 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5015 getLLVMStyleWithColumns(70));
5016 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5017 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5018 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5019 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5020 " << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5021 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5022 getLLVMStyleWithColumns(70));
5024 // But sometimes, breaking before the first "<<" is desirable.
5025 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5026 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5027 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5028 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5029 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5030 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5031 " << BEF << IsTemplate << Description << E->getType();");
5034 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5035 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5037 // Incomplete string literal.
5038 EXPECT_EQ("llvm::errs() << \"\n"
5040 format("llvm::errs() << \"\n<<a;"));
5042 verifyFormat("void f() {\n"
5043 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5044 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5048 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5049 " << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5050 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5053 TEST_F(FormatTest, UnderstandsEquals) {
5055 "aaaaaaaaaaaaaaaaa =\n"
5056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5058 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5063 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5067 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5068 " 100000000 + 10000000) {\n}");
5071 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5072 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5073 " .looooooooooooooooooooooooooooooooooooooongFunction();");
5075 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5076 " ->looooooooooooooooooooooooooooooooooooooongFunction();");
5079 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5083 "ShortObject->shortFunction(\n"
5084 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5085 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5087 verifyFormat("loooooooooooooongFunction(\n"
5088 " LoooooooooooooongObject->looooooooooooooooongFunction());");
5091 "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5092 " ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5094 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5095 " .WillRepeatedly(Return(SomeValue));");
5096 verifyFormat("void f() {\n"
5097 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5099 " .WillRepeatedly(Return(SomeValue));\n"
5101 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5102 " ccccccccccccccccccccccc);");
5103 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5104 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5106 " aaaaaaaaaaaaaaaaaaaaa);");
5107 verifyFormat("void f() {\n"
5108 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5109 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5111 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5113 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5114 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5115 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5116 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5117 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5118 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5119 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5122 // Here, it is not necessary to wrap at "." or "->".
5123 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5124 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5126 "aaaaaaaaaaa->aaaaaaaaa(\n"
5127 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5128 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5131 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5133 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5134 " aaaaaaaaa()->aaaaaa()->aaaaa());");
5135 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5136 " aaaaaaaaa()->aaaaaa()->aaaaa());");
5138 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5142 FormatStyle NoBinPacking = getLLVMStyle();
5143 NoBinPacking.BinPackParameters = false;
5144 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5145 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5146 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5147 " aaaaaaaaaaaaaaaaaaa,\n"
5148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5151 // If there is a subsequent call, change to hanging indentation.
5153 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5154 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5155 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5157 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5158 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5159 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5160 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5161 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5162 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5163 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5164 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5167 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5168 verifyFormat("template <typename T>\n"
5169 "virtual void loooooooooooongFunction(int Param1, int Param2);");
5170 verifyFormat("template <typename T>\n"
5171 "// T should be one of {A, B}.\n"
5172 "virtual void loooooooooooongFunction(int Param1, int Param2);");
5174 "template <typename T>\n"
5175 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5176 verifyFormat("template <typename T>\n"
5177 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5178 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5180 "template <typename T>\n"
5181 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5182 " int Paaaaaaaaaaaaaaaaaaaaram2);");
5184 "template <typename T>\n"
5185 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5186 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5187 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5188 verifyFormat("template <typename T>\n"
5189 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5190 " int aaaaaaaaaaaaaaaaaaaaaa);");
5192 "template <typename T1, typename T2 = char, typename T3 = char,\n"
5193 " typename T4 = char>\n"
5195 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5196 " template <typename> class cccccccccccccccccccccc,\n"
5197 " typename ddddddddddddd>\n"
5200 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5203 verifyFormat("void f() {\n"
5204 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5205 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5208 verifyFormat("template <typename T> class C {};");
5209 verifyFormat("template <typename T> void f();");
5210 verifyFormat("template <typename T> void f() {}");
5212 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5214 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5215 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5216 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5217 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5218 " bbbbbbbbbbbbbbbbbbbbbbbb);",
5219 getLLVMStyleWithColumns(72));
5220 EXPECT_EQ("static_cast<A< //\n"
5224 format("static_cast<A<//\n"
5228 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5229 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5231 FormatStyle AlwaysBreak = getLLVMStyle();
5232 AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5233 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5234 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5235 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5236 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5237 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5238 " ccccccccccccccccccccccccccccccccccccccccccccccc);");
5239 verifyFormat("template <template <typename> class Fooooooo,\n"
5240 " template <typename> class Baaaaaaar>\n"
5243 verifyFormat("template <typename T> // T can be A, B or C.\n"
5248 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5250 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5251 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5253 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5254 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5257 // FIXME: Should we have the extra indent after the second break?
5259 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5260 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5261 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5264 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5265 " cccccccccccccccccccccccccccccccccccccccccccccc());");
5267 // Breaking at nested name specifiers is generally not desirable.
5269 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5270 " aaaaaaaaaaaaaaaaaaaaaaa);");
5273 "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5274 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5275 " aaaaaaaaaaaaaaaaaaaaa);",
5276 getLLVMStyleWithColumns(74));
5278 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5280 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5283 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5284 verifyFormat("A<int> a;");
5285 verifyFormat("A<A<A<int>>> a;");
5286 verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5287 verifyFormat("bool x = a < 1 || 2 > a;");
5288 verifyFormat("bool x = 5 < f<int>();");
5289 verifyFormat("bool x = f<int>() > 5;");
5290 verifyFormat("bool x = 5 < a<int>::x;");
5291 verifyFormat("bool x = a < 4 ? a > 2 : false;");
5292 verifyFormat("bool x = f() ? a < 2 : a > 2;");
5294 verifyGoogleFormat("A<A<int>> a;");
5295 verifyGoogleFormat("A<A<A<int>>> a;");
5296 verifyGoogleFormat("A<A<A<A<int>>>> a;");
5297 verifyGoogleFormat("A<A<int> > a;");
5298 verifyGoogleFormat("A<A<A<int> > > a;");
5299 verifyGoogleFormat("A<A<A<A<int> > > > a;");
5300 verifyGoogleFormat("A<::A<int>> a;");
5301 verifyGoogleFormat("A<::A> a;");
5302 verifyGoogleFormat("A< ::A> a;");
5303 verifyGoogleFormat("A< ::A<int> > a;");
5304 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5305 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5306 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5307 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5308 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5309 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5311 verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5313 verifyFormat("test >> a >> b;");
5314 verifyFormat("test << a >> b;");
5316 verifyFormat("f<int>();");
5317 verifyFormat("template <typename T> void f() {}");
5318 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5319 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5320 "sizeof(char)>::type>;");
5321 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5323 // Not template parameters.
5324 verifyFormat("return a < b && c > d;");
5325 verifyFormat("void f() {\n"
5326 " while (a < b && c > d) {\n"
5329 verifyFormat("template <typename... Types>\n"
5330 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5332 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5334 getLLVMStyleWithColumns(60));
5335 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5336 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5337 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5340 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5341 verifyFormat("COMPARE(a, ==, b);");
5344 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5345 verifyFormat("int A::*x;");
5346 verifyFormat("int (S::*func)(void *);");
5347 verifyFormat("void f() { int (S::*func)(void *); }");
5348 verifyFormat("typedef bool *(Class::*Member)() const;");
5349 verifyFormat("void f() {\n"
5356 verifyFormat("void f() {\n"
5357 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5358 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5361 "(aaaaaaaaaa->*bbbbbbb)(\n"
5362 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5363 FormatStyle Style = getLLVMStyle();
5364 Style.PointerAlignment = FormatStyle::PAS_Left;
5365 verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5368 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5369 verifyFormat("int a = -2;");
5370 verifyFormat("f(-1, -2, -3);");
5371 verifyFormat("a[-1] = 5;");
5372 verifyFormat("int a = 5 + -2;");
5373 verifyFormat("if (i == -1) {\n}");
5374 verifyFormat("if (i != -1) {\n}");
5375 verifyFormat("if (i > -1) {\n}");
5376 verifyFormat("if (i < -1) {\n}");
5377 verifyFormat("++(a->f());");
5378 verifyFormat("--(a->f());");
5379 verifyFormat("(a->f())++;");
5380 verifyFormat("a[42]++;");
5381 verifyFormat("if (!(a->f())) {\n}");
5383 verifyFormat("a-- > b;");
5384 verifyFormat("b ? -a : c;");
5385 verifyFormat("n * sizeof char16;");
5386 verifyFormat("n * alignof char16;", getGoogleStyle());
5387 verifyFormat("sizeof(char);");
5388 verifyFormat("alignof(char);", getGoogleStyle());
5390 verifyFormat("return -1;");
5391 verifyFormat("switch (a) {\n"
5395 verifyFormat("#define X -1");
5396 verifyFormat("#define X -kConstant");
5398 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5399 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5401 verifyFormat("int a = /* confusing comment */ -1;");
5402 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5403 verifyFormat("int a = i /* confusing comment */++;");
5406 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5407 verifyFormat("if (!aaaaaaaaaa( // break\n"
5410 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5412 verifyFormat("*aaa = aaaaaaa( // break\n"
5416 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5417 verifyFormat("bool operator<();");
5418 verifyFormat("bool operator>();");
5419 verifyFormat("bool operator=();");
5420 verifyFormat("bool operator==();");
5421 verifyFormat("bool operator!=();");
5422 verifyFormat("int operator+();");
5423 verifyFormat("int operator++();");
5424 verifyFormat("bool operator();");
5425 verifyFormat("bool operator()();");
5426 verifyFormat("bool operator[]();");
5427 verifyFormat("operator bool();");
5428 verifyFormat("operator int();");
5429 verifyFormat("operator void *();");
5430 verifyFormat("operator SomeType<int>();");
5431 verifyFormat("operator SomeType<int, int>();");
5432 verifyFormat("operator SomeType<SomeType<int>>();");
5433 verifyFormat("void *operator new(std::size_t size);");
5434 verifyFormat("void *operator new[](std::size_t size);");
5435 verifyFormat("void operator delete(void *ptr);");
5436 verifyFormat("void operator delete[](void *ptr);");
5437 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5438 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5441 "ostream &operator<<(ostream &OutputStream,\n"
5442 " SomeReallyLongType WithSomeReallyLongValue);");
5443 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5444 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5445 " return left.group < right.group;\n"
5447 verifyFormat("SomeType &operator=(const SomeType &S);");
5448 verifyFormat("f.template operator()<int>();");
5450 verifyGoogleFormat("operator void*();");
5451 verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5452 verifyGoogleFormat("operator ::A();");
5454 verifyFormat("using A::operator+;");
5455 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5459 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5460 verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5461 verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5462 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5463 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5464 verifyFormat("Deleted &operator=(const Deleted &) &;");
5465 verifyFormat("Deleted &operator=(const Deleted &) &&;");
5466 verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5467 verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5468 verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5469 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5470 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5472 FormatStyle AlignLeft = getLLVMStyle();
5473 AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5474 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5475 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5477 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5478 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5480 FormatStyle Spaces = getLLVMStyle();
5481 Spaces.SpacesInCStyleCastParentheses = true;
5482 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5483 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5484 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5485 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5487 Spaces.SpacesInCStyleCastParentheses = false;
5488 Spaces.SpacesInParentheses = true;
5489 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5490 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5491 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5492 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5495 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5496 verifyFormat("void f() {\n"
5498 " A *a = new (placement) A;\n"
5502 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5503 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5504 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5505 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5506 " typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5507 verifyFormat("delete[] h->p;");
5510 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5511 verifyFormat("int *f(int *a) {}");
5512 verifyFormat("int main(int argc, char **argv) {}");
5513 verifyFormat("Test::Test(int b) : a(b * b) {}");
5514 verifyIndependentOfContext("f(a, *a);");
5515 verifyFormat("void g() { f(*a); }");
5516 verifyIndependentOfContext("int a = b * 10;");
5517 verifyIndependentOfContext("int a = 10 * b;");
5518 verifyIndependentOfContext("int a = b * c;");
5519 verifyIndependentOfContext("int a += b * c;");
5520 verifyIndependentOfContext("int a -= b * c;");
5521 verifyIndependentOfContext("int a *= b * c;");
5522 verifyIndependentOfContext("int a /= b * c;");
5523 verifyIndependentOfContext("int a = *b;");
5524 verifyIndependentOfContext("int a = *b * c;");
5525 verifyIndependentOfContext("int a = b * *c;");
5526 verifyIndependentOfContext("int a = b * (10);");
5527 verifyIndependentOfContext("S << b * (10);");
5528 verifyIndependentOfContext("return 10 * b;");
5529 verifyIndependentOfContext("return *b * *c;");
5530 verifyIndependentOfContext("return a & ~b;");
5531 verifyIndependentOfContext("f(b ? *c : *d);");
5532 verifyIndependentOfContext("int a = b ? *c : *d;");
5533 verifyIndependentOfContext("*b = a;");
5534 verifyIndependentOfContext("a * ~b;");
5535 verifyIndependentOfContext("a * !b;");
5536 verifyIndependentOfContext("a * +b;");
5537 verifyIndependentOfContext("a * -b;");
5538 verifyIndependentOfContext("a * ++b;");
5539 verifyIndependentOfContext("a * --b;");
5540 verifyIndependentOfContext("a[4] * b;");
5541 verifyIndependentOfContext("a[a * a] = 1;");
5542 verifyIndependentOfContext("f() * b;");
5543 verifyIndependentOfContext("a * [self dostuff];");
5544 verifyIndependentOfContext("int x = a * (a + b);");
5545 verifyIndependentOfContext("(a *)(a + b);");
5546 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5547 verifyIndependentOfContext("int *pa = (int *)&a;");
5548 verifyIndependentOfContext("return sizeof(int **);");
5549 verifyIndependentOfContext("return sizeof(int ******);");
5550 verifyIndependentOfContext("return (int **&)a;");
5551 verifyIndependentOfContext("f((*PointerToArray)[10]);");
5552 verifyFormat("void f(Type (*parameter)[10]) {}");
5553 verifyFormat("void f(Type (¶meter)[10]) {}");
5554 verifyGoogleFormat("return sizeof(int**);");
5555 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5556 verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5557 verifyFormat("auto a = [](int **&, int ***) {};");
5558 verifyFormat("auto PointerBinding = [](const char *S) {};");
5559 verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5560 verifyFormat("[](const decltype(*a) &value) {}");
5561 verifyFormat("decltype(a * b) F();");
5562 verifyFormat("#define MACRO() [](A *a) { return 1; }");
5563 verifyIndependentOfContext("typedef void (*f)(int *a);");
5564 verifyIndependentOfContext("int i{a * b};");
5565 verifyIndependentOfContext("aaa && aaa->f();");
5566 verifyIndependentOfContext("int x = ~*p;");
5567 verifyFormat("Constructor() : a(a), area(width * height) {}");
5568 verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5569 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5570 verifyFormat("void f() { f(a, c * d); }");
5571 verifyFormat("void f() { f(new a(), c * d); }");
5573 verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5575 verifyIndependentOfContext("A<int *> a;");
5576 verifyIndependentOfContext("A<int **> a;");
5577 verifyIndependentOfContext("A<int *, int *> a;");
5578 verifyIndependentOfContext("A<int *[]> a;");
5579 verifyIndependentOfContext(
5580 "const char *const p = reinterpret_cast<const char *const>(q);");
5581 verifyIndependentOfContext("A<int **, int **> a;");
5582 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5583 verifyFormat("for (char **a = b; *a; ++a) {\n}");
5584 verifyFormat("for (; a && b;) {\n}");
5585 verifyFormat("bool foo = true && [] { return false; }();");
5588 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5589 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5591 verifyGoogleFormat("**outparam = 1;");
5592 verifyGoogleFormat("*outparam = a * b;");
5593 verifyGoogleFormat("int main(int argc, char** argv) {}");
5594 verifyGoogleFormat("A<int*> a;");
5595 verifyGoogleFormat("A<int**> a;");
5596 verifyGoogleFormat("A<int*, int*> a;");
5597 verifyGoogleFormat("A<int**, int**> a;");
5598 verifyGoogleFormat("f(b ? *c : *d);");
5599 verifyGoogleFormat("int a = b ? *c : *d;");
5600 verifyGoogleFormat("Type* t = **x;");
5601 verifyGoogleFormat("Type* t = *++*x;");
5602 verifyGoogleFormat("*++*x;");
5603 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5604 verifyGoogleFormat("Type* t = x++ * y;");
5606 "const char* const p = reinterpret_cast<const char* const>(q);");
5607 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5608 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5609 verifyGoogleFormat("template <typename T>\n"
5610 "void f(int i = 0, SomeType** temps = NULL);");
5612 FormatStyle Left = getLLVMStyle();
5613 Left.PointerAlignment = FormatStyle::PAS_Left;
5614 verifyFormat("x = *a(x) = *a(y);", Left);
5615 verifyFormat("for (;; * = b) {\n}", Left);
5616 verifyFormat("return *this += 1;", Left);
5618 verifyIndependentOfContext("a = *(x + y);");
5619 verifyIndependentOfContext("a = &(x + y);");
5620 verifyIndependentOfContext("*(x + y).call();");
5621 verifyIndependentOfContext("&(x + y)->call();");
5622 verifyFormat("void f() { &(*I).first; }");
5624 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5626 "int *MyValues = {\n"
5627 " *A, // Operator detection might be confused by the '{'\n"
5628 " *BB // Operator detection might be confused by previous comment\n"
5631 verifyIndependentOfContext("if (int *a = &b)");
5632 verifyIndependentOfContext("if (int &a = *b)");
5633 verifyIndependentOfContext("if (a & b[i])");
5634 verifyIndependentOfContext("if (a::b::c::d & b[i])");
5635 verifyIndependentOfContext("if (*b[i])");
5636 verifyIndependentOfContext("if (int *a = (&b))");
5637 verifyIndependentOfContext("while (int *a = &b)");
5638 verifyIndependentOfContext("size = sizeof *a;");
5639 verifyIndependentOfContext("if (a && (b = c))");
5640 verifyFormat("void f() {\n"
5641 " for (const int &v : Values) {\n"
5644 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5645 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5646 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5648 verifyFormat("#define A (!a * b)");
5649 verifyFormat("#define MACRO \\\n"
5650 " int *i = a * b; \\\n"
5652 getLLVMStyleWithColumns(19));
5654 verifyIndependentOfContext("A = new SomeType *[Length];");
5655 verifyIndependentOfContext("A = new SomeType *[Length]();");
5656 verifyIndependentOfContext("T **t = new T *;");
5657 verifyIndependentOfContext("T **t = new T *();");
5658 verifyGoogleFormat("A = new SomeType*[Length]();");
5659 verifyGoogleFormat("A = new SomeType*[Length];");
5660 verifyGoogleFormat("T** t = new T*;");
5661 verifyGoogleFormat("T** t = new T*();");
5663 FormatStyle PointerLeft = getLLVMStyle();
5664 PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5665 verifyFormat("delete *x;", PointerLeft);
5666 verifyFormat("STATIC_ASSERT((a & b) == 0);");
5667 verifyFormat("STATIC_ASSERT(0 == (a & b));");
5668 verifyFormat("template <bool a, bool b> "
5669 "typename t::if<x && y>::type f() {}");
5670 verifyFormat("template <int *y> f() {}");
5671 verifyFormat("vector<int *> v;");
5672 verifyFormat("vector<int *const> v;");
5673 verifyFormat("vector<int *const **const *> v;");
5674 verifyFormat("vector<int *volatile> v;");
5675 verifyFormat("vector<a * b> v;");
5676 verifyFormat("foo<b && false>();");
5677 verifyFormat("foo<b & 1>();");
5678 verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5680 "template <class T, class = typename std::enable_if<\n"
5681 " std::is_integral<T>::value &&\n"
5682 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5684 getLLVMStyleWithColumns(76));
5686 "template <class T,\n"
5687 " class = typename ::std::enable_if<\n"
5688 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5690 getGoogleStyleWithColumns(68));
5692 verifyIndependentOfContext("MACRO(int *i);");
5693 verifyIndependentOfContext("MACRO(auto *a);");
5694 verifyIndependentOfContext("MACRO(const A *a);");
5695 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5696 // FIXME: Is there a way to make this work?
5697 // verifyIndependentOfContext("MACRO(A *a);");
5699 verifyFormat("DatumHandle const *operator->() const { return input_; }");
5700 verifyFormat("return options != nullptr && operator==(*options);");
5702 EXPECT_EQ("#define OP(x) \\\n"
5703 " ostream &operator<<(ostream &s, const A &a) { \\\n"
5704 " return s << a.DebugString(); \\\n"
5706 format("#define OP(x) \\\n"
5707 " ostream &operator<<(ostream &s, const A &a) { \\\n"
5708 " return s << a.DebugString(); \\\n"
5710 getLLVMStyleWithColumns(50)));
5712 // FIXME: We cannot handle this case yet; we might be able to figure out that
5713 // foo<x> d > v; doesn't make sense.
5714 verifyFormat("foo<a<b && c> d> v;");
5716 FormatStyle PointerMiddle = getLLVMStyle();
5717 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5718 verifyFormat("delete *x;", PointerMiddle);
5719 verifyFormat("int * x;", PointerMiddle);
5720 verifyFormat("template <int * y> f() {}", PointerMiddle);
5721 verifyFormat("int * f(int * a) {}", PointerMiddle);
5722 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5723 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5724 verifyFormat("A<int *> a;", PointerMiddle);
5725 verifyFormat("A<int **> a;", PointerMiddle);
5726 verifyFormat("A<int *, int *> a;", PointerMiddle);
5727 verifyFormat("A<int * []> a;", PointerMiddle);
5728 verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5729 verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5730 verifyFormat("T ** t = new T *;", PointerMiddle);
5732 // Member function reference qualifiers aren't binary operators.
5733 verifyFormat("string // break\n"
5734 "operator()() & {}");
5735 verifyFormat("string // break\n"
5736 "operator()() && {}");
5737 verifyGoogleFormat("template <typename T>\n"
5738 "auto x() & -> int {}");
5741 TEST_F(FormatTest, UnderstandsAttributes) {
5742 verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5743 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5744 "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5745 FormatStyle AfterType = getLLVMStyle();
5746 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5747 verifyFormat("__attribute__((nodebug)) void\n"
5752 TEST_F(FormatTest, UnderstandsEllipsis) {
5753 verifyFormat("int printf(const char *fmt, ...);");
5754 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5755 verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5757 FormatStyle PointersLeft = getLLVMStyle();
5758 PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5759 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5762 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5763 EXPECT_EQ("int *a;\n"
5770 EXPECT_EQ("int* a;\n"
5777 EXPECT_EQ("int *a;\n"
5784 EXPECT_EQ("auto x = [] {\n"
5789 format("auto x=[]{int *a;\n"
5795 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5796 verifyFormat("int f(int &&a) {}");
5797 verifyFormat("int f(int a, char &&b) {}");
5798 verifyFormat("void f() { int &&a = b; }");
5799 verifyGoogleFormat("int f(int a, char&& b) {}");
5800 verifyGoogleFormat("void f() { int&& a = b; }");
5802 verifyIndependentOfContext("A<int &&> a;");
5803 verifyIndependentOfContext("A<int &&, int &&> a;");
5804 verifyGoogleFormat("A<int&&> a;");
5805 verifyGoogleFormat("A<int&&, int&&> a;");
5807 // Not rvalue references:
5808 verifyFormat("template <bool B, bool C> class A {\n"
5809 " static_assert(B && C, \"Something is wrong\");\n"
5811 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5812 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5813 verifyFormat("#define A(a, b) (a && b)");
5816 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5817 verifyFormat("void f() {\n"
5821 getLLVMStyleWithColumns(15));
5824 TEST_F(FormatTest, FormatsCasts) {
5825 verifyFormat("Type *A = static_cast<Type *>(P);");
5826 verifyFormat("Type *A = (Type *)P;");
5827 verifyFormat("Type *A = (vector<Type *, int *>)P;");
5828 verifyFormat("int a = (int)(2.0f);");
5829 verifyFormat("int a = (int)2.0f;");
5830 verifyFormat("x[(int32)y];");
5831 verifyFormat("x = (int32)y;");
5832 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5833 verifyFormat("int a = (int)*b;");
5834 verifyFormat("int a = (int)2.0f;");
5835 verifyFormat("int a = (int)~0;");
5836 verifyFormat("int a = (int)++a;");
5837 verifyFormat("int a = (int)sizeof(int);");
5838 verifyFormat("int a = (int)+2;");
5839 verifyFormat("my_int a = (my_int)2.0f;");
5840 verifyFormat("my_int a = (my_int)sizeof(int);");
5841 verifyFormat("return (my_int)aaa;");
5842 verifyFormat("#define x ((int)-1)");
5843 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5844 verifyFormat("#define p(q) ((int *)&q)");
5845 verifyFormat("fn(a)(b) + 1;");
5847 verifyFormat("void f() { my_int a = (my_int)*b; }");
5848 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5849 verifyFormat("my_int a = (my_int)~0;");
5850 verifyFormat("my_int a = (my_int)++a;");
5851 verifyFormat("my_int a = (my_int)-2;");
5852 verifyFormat("my_int a = (my_int)1;");
5853 verifyFormat("my_int a = (my_int *)1;");
5854 verifyFormat("my_int a = (const my_int)-1;");
5855 verifyFormat("my_int a = (const my_int *)-1;");
5856 verifyFormat("my_int a = (my_int)(my_int)-1;");
5857 verifyFormat("my_int a = (ns::my_int)-2;");
5858 verifyFormat("case (my_int)ONE:");
5860 // FIXME: single value wrapped with paren will be treated as cast.
5861 verifyFormat("void f(int i = (kValue)*kMask) {}");
5863 verifyFormat("{ (void)F; }");
5865 // Don't break after a cast's
5866 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5867 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5868 " bbbbbbbbbbbbbbbbbbbbbb);");
5870 // These are not casts.
5871 verifyFormat("void f(int *) {}");
5872 verifyFormat("f(foo)->b;");
5873 verifyFormat("f(foo).b;");
5874 verifyFormat("f(foo)(b);");
5875 verifyFormat("f(foo)[b];");
5876 verifyFormat("[](foo) { return 4; }(bar);");
5877 verifyFormat("(*funptr)(foo)[4];");
5878 verifyFormat("funptrs[4](foo)[4];");
5879 verifyFormat("void f(int *);");
5880 verifyFormat("void f(int *) = 0;");
5881 verifyFormat("void f(SmallVector<int>) {}");
5882 verifyFormat("void f(SmallVector<int>);");
5883 verifyFormat("void f(SmallVector<int>) = 0;");
5884 verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5885 verifyFormat("int a = sizeof(int) * b;");
5886 verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5887 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5888 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5889 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5891 // These are not casts, but at some point were confused with casts.
5892 verifyFormat("virtual void foo(int *) override;");
5893 verifyFormat("virtual void foo(char &) const;");
5894 verifyFormat("virtual void foo(int *a, char *) const;");
5895 verifyFormat("int a = sizeof(int *) + b;");
5896 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5897 verifyFormat("bool b = f(g<int>) && c;");
5898 verifyFormat("typedef void (*f)(int i) func;");
5900 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5901 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5902 // FIXME: The indentation here is not ideal.
5904 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5905 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5906 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5909 TEST_F(FormatTest, FormatsFunctionTypes) {
5910 verifyFormat("A<bool()> a;");
5911 verifyFormat("A<SomeType()> a;");
5912 verifyFormat("A<void (*)(int, std::string)> a;");
5913 verifyFormat("A<void *(int)>;");
5914 verifyFormat("void *(*a)(int *, SomeType *);");
5915 verifyFormat("int (*func)(void *);");
5916 verifyFormat("void f() { int (*func)(void *); }");
5917 verifyFormat("template <class CallbackClass>\n"
5918 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5920 verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5921 verifyGoogleFormat("void* (*a)(int);");
5923 "template <class CallbackClass>\n"
5924 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5926 // Other constructs can look somewhat like function types:
5927 verifyFormat("A<sizeof(*x)> a;");
5928 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5929 verifyFormat("some_var = function(*some_pointer_var)[0];");
5930 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5931 verifyFormat("int x = f(&h)();");
5934 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5935 verifyFormat("A (*foo_)[6];");
5936 verifyFormat("vector<int> (*foo_)[6];");
5939 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5940 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5941 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
5942 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5943 " LoooooooooooooooooooooooooooooooooooooooongVariable;");
5944 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5945 " *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5947 // Different ways of ()-initializiation.
5948 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5949 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5950 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5951 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5952 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5953 " LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5954 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5955 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
5958 TEST_F(FormatTest, BreaksLongDeclarations) {
5959 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5960 " AnotherNameForTheLongType;");
5961 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5963 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5964 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5965 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5966 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5967 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5968 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5969 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5970 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5971 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5972 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5973 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5974 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5975 FormatStyle Indented = getLLVMStyle();
5976 Indented.IndentWrappedFunctionNames = true;
5977 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5978 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5981 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5982 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5985 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5986 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5989 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5990 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5993 // FIXME: Without the comment, this breaks after "(".
5994 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n"
5995 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5998 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5999 " int LoooooooooooooooooooongParam2) {}");
6001 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6002 " SourceLocation L, IdentifierIn *II,\n"
6004 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6005 "ReallyReaaallyLongFunctionName(\n"
6006 " const std::string &SomeParameter,\n"
6007 " const SomeType<string, SomeOtherTemplateParameter>\n"
6008 " &ReallyReallyLongParameterName,\n"
6009 " const SomeType<string, SomeOtherTemplateParameter>\n"
6010 " &AnotherLongParameterName) {}");
6011 verifyFormat("template <typename A>\n"
6012 "SomeLoooooooooooooooooooooongType<\n"
6013 " typename some_namespace::SomeOtherType<A>::Type>\n"
6017 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6018 " aaaaaaaaaaaaaaaaaaaaaaa;");
6020 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6021 " SourceLocation L) {}");
6023 "some_namespace::LongReturnType\n"
6024 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6025 " int first_long_parameter, int second_parameter) {}");
6027 verifyGoogleFormat("template <typename T>\n"
6028 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6029 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6030 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6031 " int aaaaaaaaaaaaaaaaaaaaaaa);");
6033 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6034 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6035 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6036 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6037 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6038 " aaaaaaaaaaaaaaaaaaaaaaaa);");
6039 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6040 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6045 TEST_F(FormatTest, FormatsArrays) {
6046 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6047 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6048 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6049 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6050 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6051 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6052 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6053 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6054 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6055 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6056 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6057 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6058 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6060 "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6061 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6062 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6064 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6067 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6069 " .aaaaaaaaaaaaaaaaaaaaaa();");
6071 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6074 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6075 verifyFormat("(a)->b();");
6076 verifyFormat("--a;");
6079 TEST_F(FormatTest, HandlesIncludeDirectives) {
6080 verifyFormat("#include <string>\n"
6081 "#include <a/b/c.h>\n"
6082 "#include \"a/b/string\"\n"
6083 "#include \"string.h\"\n"
6084 "#include \"string.h\"\n"
6086 "#include < path with space >\n"
6087 "#include_next <test.h>"
6088 "#include \"abc.h\" // this is included for ABC\n"
6089 "#include \"some long include\" // with a comment\n"
6090 "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
6091 getLLVMStyleWithColumns(35));
6092 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\""));
6093 EXPECT_EQ("#include <a>", format("#include<a>"));
6095 verifyFormat("#import <string>");
6096 verifyFormat("#import <a/b/c.h>");
6097 verifyFormat("#import \"a/b/string\"");
6098 verifyFormat("#import \"string.h\"");
6099 verifyFormat("#import \"string.h\"");
6100 verifyFormat("#if __has_include(<strstream>)\n"
6101 "#include <strstream>\n"
6104 verifyFormat("#define MY_IMPORT <a/b>");
6106 // Protocol buffer definition or missing "#".
6107 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6108 getLLVMStyleWithColumns(30));
6110 FormatStyle Style = getLLVMStyle();
6111 Style.AlwaysBreakBeforeMultilineStrings = true;
6112 Style.ColumnLimit = 0;
6113 verifyFormat("#import \"abc.h\"", Style);
6115 // But 'import' might also be a regular C++ namespace.
6116 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6117 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6120 //===----------------------------------------------------------------------===//
6121 // Error recovery tests.
6122 //===----------------------------------------------------------------------===//
6124 TEST_F(FormatTest, IncompleteParameterLists) {
6125 FormatStyle NoBinPacking = getLLVMStyle();
6126 NoBinPacking.BinPackParameters = false;
6127 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6133 " double *max_z, ) {}",
6137 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6138 verifyFormat("void f() { return; }\n42");
6139 verifyFormat("void f() {\n"
6144 verifyFormat("void f() { return }\n42");
6145 verifyFormat("void f() {\n"
6152 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6153 EXPECT_EQ("void f() { return }", format("void f ( ) { return }"));
6154 EXPECT_EQ("void f() {\n"
6158 format("void f ( ) { if ( a ) return }"));
6159 EXPECT_EQ("namespace N {\n"
6162 format("namespace N { void f() }"));
6163 EXPECT_EQ("namespace N {\n"
6167 format("namespace N { void f( ) { } void g( ) }"));
6170 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6171 verifyFormat("int aaaaaaaa =\n"
6172 " // Overlylongcomment\n"
6174 getLLVMStyleWithColumns(20));
6175 verifyFormat("function(\n"
6177 " LoooooooooooongArgument);\n",
6178 getLLVMStyleWithColumns(20));
6181 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6182 verifyFormat("public:");
6183 verifyFormat("class A {\n"
6187 verifyFormat("public\n"
6189 verifyFormat("public\n"
6191 verifyFormat("public\n"
6193 verifyFormat("public\n"
6197 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6199 verifyFormat("#})");
6200 verifyNoCrash("(/**/[:!] ?[).");
6203 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6204 verifyFormat("do {\n}");
6205 verifyFormat("do {\n}\n"
6207 verifyFormat("do {\n}\n"
6209 verifyFormat("do {\n"
6214 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6215 verifyFormat("if {\n foo;\n foo();\n}");
6216 verifyFormat("switch {\n foo;\n foo();\n}");
6217 verifyIncompleteFormat("for {\n foo;\n foo();\n}");
6218 verifyFormat("while {\n foo;\n foo();\n}");
6219 verifyFormat("do {\n foo;\n foo();\n} while;");
6222 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6223 verifyIncompleteFormat("namespace {\n"
6224 "class Foo { Foo (\n"
6229 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6230 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n"));
6231 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n"));
6232 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n"));
6233 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n"));
6244 getLLVMStyleWithColumns(10)));
6247 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6248 verifyFormat("int x = {\n"
6250 " b(alongervariable)};",
6251 getLLVMStyleWithColumns(25));
6254 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6255 verifyFormat("return (a)(b){1, 2, 3};");
6258 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6259 verifyFormat("vector<int> x{1, 2, 3, 4};");
6260 verifyFormat("vector<int> x{\n"
6263 verifyFormat("vector<T> x{{}, {}, {}, {}};");
6264 verifyFormat("f({1, 2});");
6265 verifyFormat("auto v = Foo{-1};");
6266 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6267 verifyFormat("Class::Class : member{1, 2, 3} {}");
6268 verifyFormat("new vector<int>{1, 2, 3};");
6269 verifyFormat("new int[3]{1, 2, 3};");
6270 verifyFormat("new int{1};");
6271 verifyFormat("return {arg1, arg2};");
6272 verifyFormat("return {arg1, SomeType{parameter}};");
6273 verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6274 verifyFormat("new T{arg1, arg2};");
6275 verifyFormat("f(MyMap[{composite, key}]);");
6276 verifyFormat("class Class {\n"
6277 " T member = {arg1, arg2};\n"
6279 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6280 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6281 verifyFormat("int a = std::is_integral<int>{} + 0;");
6283 verifyFormat("int foo(int i) { return fo1{}(i); }");
6284 verifyFormat("int foo(int i) { return fo1{}(i); }");
6285 verifyFormat("auto i = decltype(x){};");
6286 verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6287 verifyFormat("Node n{1, Node{1000}, //\n"
6289 verifyFormat("Aaaa aaaaaaa{\n"
6294 verifyFormat("class C : public D {\n"
6295 " SomeClass SC{2};\n"
6297 verifyFormat("class C : public A {\n"
6298 " class D : public B {\n"
6299 " void f() { int i{2}; }\n"
6302 verifyFormat("#define A {a, a},");
6304 // In combination with BinPackArguments = false.
6305 FormatStyle NoBinPacking = getLLVMStyle();
6306 NoBinPacking.BinPackArguments = false;
6307 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6319 verifyFormat("const Aaaaaa aaaaa = {\n"
6334 "const Aaaaaa aaaaa = {\n"
6335 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n"
6336 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n"
6337 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6341 // FIXME: The alignment of these trailing comments might be bad. Then again,
6342 // this might be utterly useless in real code.
6343 verifyFormat("Constructor::Constructor()\n"
6344 " : some_value{ //\n"
6348 // In braced lists, the first comment is always assumed to belong to the
6349 // first element. Thus, it can be moved to the next or previous line as
6351 EXPECT_EQ("function({// First element:\n"
6353 " // Second element:\n"
6355 format("function({\n"
6356 " // First element:\n"
6358 " // Second element:\n"
6360 EXPECT_EQ("std::vector<int> MyNumbers{\n"
6361 " // First element:\n"
6363 " // Second element:\n"
6365 format("std::vector<int> MyNumbers{// First element:\n"
6367 " // Second element:\n"
6369 getLLVMStyleWithColumns(30)));
6370 // A trailing comma should still lead to an enforced line break.
6371 EXPECT_EQ("vector<int> SomeVector = {\n"
6375 format("vector<int> SomeVector = { // aaa\n"
6378 FormatStyle ExtraSpaces = getLLVMStyle();
6379 ExtraSpaces.Cpp11BracedListStyle = false;
6380 ExtraSpaces.ColumnLimit = 75;
6381 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6382 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6383 verifyFormat("f({ 1, 2 });", ExtraSpaces);
6384 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6385 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6386 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6387 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6388 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6389 verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6390 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6391 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6392 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6393 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6394 verifyFormat("class Class {\n"
6395 " T member = { arg1, arg2 };\n"
6399 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6400 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6401 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6402 " bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6404 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6405 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6408 "someFunction(OtherParam,\n"
6409 " BracedList{ // comment 1 (Forcing interesting break)\n"
6410 " param1, param2,\n"
6412 " param3, param4 });",
6415 "std::this_thread::sleep_for(\n"
6416 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6418 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6422 " aaaaaaaaaaaaaaa,\n"
6426 " aaaaaaaaaaaaaaaaaaaaa,\n"
6428 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6431 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6434 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6435 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6436 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6437 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6438 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6439 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6440 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
6441 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6442 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6443 " 1, 22, 333, 4444, 55555, //\n"
6444 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6445 " 1, 22, 333, 4444, 55555, 666666, 7777777};");
6447 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6448 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6449 " 1, 22, 333, 4444, 55555, 666666, // comment\n"
6450 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
6451 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
6452 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n"
6454 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6455 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6456 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6457 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6458 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6459 " // Separating comment.\n"
6460 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6461 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6462 " // Leading comment\n"
6463 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6464 " X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6465 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6467 getLLVMStyleWithColumns(39));
6468 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6470 getLLVMStyleWithColumns(38));
6471 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6472 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6473 getLLVMStyleWithColumns(43));
6475 "static unsigned SomeValues[10][3] = {\n"
6476 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n"
6477 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6478 verifyFormat("static auto fields = new vector<string>{\n"
6479 " \"aaaaaaaaaaaaa\",\n"
6480 " \"aaaaaaaaaaaaa\",\n"
6481 " \"aaaaaaaaaaaa\",\n"
6482 " \"aaaaaaaaaaaaaa\",\n"
6483 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6484 " \"aaaaaaaaaaaa\",\n"
6485 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6487 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6488 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6489 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6490 " 3, cccccccccccccccccccccc};",
6491 getLLVMStyleWithColumns(60));
6494 verifyFormat("vector<int> x = {\n"
6495 " 1, 1, 1, 1, 1, 1, 1, 1,\n"
6497 getLLVMStyleWithColumns(39));
6498 verifyFormat("vector<int> x = {\n"
6499 " 1, 1, 1, 1, 1, 1, 1, 1, //\n"
6501 getLLVMStyleWithColumns(39));
6502 verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6505 getLLVMStyleWithColumns(39));
6507 // Trailing comment in the first line.
6508 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n"
6509 " 1111111111, 2222222222, 33333333333, 4444444444, //\n"
6510 " 111111111, 222222222, 3333333333, 444444444, //\n"
6511 " 11111111, 22222222, 333333333, 44444444};");
6512 // Trailing comment in the last line.
6513 verifyFormat("int aaaaa[] = {\n"
6514 " 1, 2, 3, // comment\n"
6515 " 4, 5, 6 // comment\n"
6518 // With nested lists, we should either format one item per line or all nested
6519 // lists one on line.
6520 // FIXME: For some nested lists, we can do better.
6521 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6522 " {aaaaaaaaaaaaaaaaaaa},\n"
6523 " {aaaaaaaaaaaaaaaaaaaaa},\n"
6524 " {aaaaaaaaaaaaaaaaa}};",
6525 getLLVMStyleWithColumns(60));
6527 "SomeStruct my_struct_array = {\n"
6528 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6529 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6532 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6533 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6534 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6536 // No column layout should be used here.
6537 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6538 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6540 verifyNoCrash("a<,");
6542 // No braced initializer here.
6543 verifyFormat("void f() {\n"
6544 " struct Dummy {};\n"
6549 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6550 FormatStyle DoNotMerge = getLLVMStyle();
6551 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6553 verifyFormat("void f() { return 42; }");
6554 verifyFormat("void f() {\n"
6558 verifyFormat("void f() {\n"
6569 verifyFormat("void f() {} // comment");
6570 verifyFormat("void f() { int a; } // comment");
6571 verifyFormat("void f() {\n"
6574 verifyFormat("void f() {\n"
6578 verifyFormat("void f() {\n"
6580 getLLVMStyleWithColumns(15));
6582 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6583 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22));
6585 verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6586 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6587 verifyFormat("class C {\n"
6589 " : iiiiiiii(nullptr),\n"
6590 " kkkkkkk(nullptr),\n"
6591 " mmmmmmm(nullptr),\n"
6592 " nnnnnnn(nullptr) {}\n"
6596 FormatStyle NoColumnLimit = getLLVMStyle();
6597 NoColumnLimit.ColumnLimit = 0;
6598 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6599 EXPECT_EQ("class C {\n"
6602 format("class C{A():b(0){}};", NoColumnLimit));
6606 format("A()\n:b(0)\n{\n}", NoColumnLimit));
6608 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6609 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6610 FormatStyle::SFS_None;
6614 format("A():b(0){}", DoNotMergeNoColumnLimit));
6618 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6620 verifyFormat("#define A \\\n"
6624 getLLVMStyleWithColumns(20));
6625 verifyFormat("#define A \\\n"
6626 " void f() { int i; }",
6627 getLLVMStyleWithColumns(21));
6628 verifyFormat("#define A \\\n"
6633 getLLVMStyleWithColumns(22));
6634 verifyFormat("#define A \\\n"
6635 " void f() { int i; } \\\n"
6637 getLLVMStyleWithColumns(23));
6640 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6641 FormatStyle MergeInlineOnly = getLLVMStyle();
6642 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6643 verifyFormat("class C {\n"
6644 " int f() { return 42; }\n"
6647 verifyFormat("int f() {\n"
6653 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6654 // Elaborate type variable declarations.
6655 verifyFormat("struct foo a = {bar};\nint n;");
6656 verifyFormat("class foo a = {bar};\nint n;");
6657 verifyFormat("union foo a = {bar};\nint n;");
6659 // Elaborate types inside function definitions.
6660 verifyFormat("struct foo f() {}\nint n;");
6661 verifyFormat("class foo f() {}\nint n;");
6662 verifyFormat("union foo f() {}\nint n;");
6665 verifyFormat("template <class X> void f() {}\nint n;");
6666 verifyFormat("template <struct X> void f() {}\nint n;");
6667 verifyFormat("template <union X> void f() {}\nint n;");
6669 // Actual definitions...
6670 verifyFormat("struct {\n} n;");
6672 "template <template <class T, class Y>, class Z> class X {\n} n;");
6673 verifyFormat("union Z {\n int n;\n} x;");
6674 verifyFormat("class MACRO Z {\n} n;");
6675 verifyFormat("class MACRO(X) Z {\n} n;");
6676 verifyFormat("class __attribute__(X) Z {\n} n;");
6677 verifyFormat("class __declspec(X) Z {\n} n;");
6678 verifyFormat("class A##B##C {\n} n;");
6679 verifyFormat("class alignas(16) Z {\n} n;");
6680 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6681 verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6683 // Redefinition from nested context:
6684 verifyFormat("class A::B::C {\n} n;");
6686 // Template definitions.
6688 "template <typename F>\n"
6689 "Matcher(const Matcher<F> &Other,\n"
6690 " typename enable_if_c<is_base_of<F, T>::value &&\n"
6691 " !is_same<F, T>::value>::type * = 0)\n"
6692 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6694 // FIXME: This is still incorrectly handled at the formatter side.
6695 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6696 verifyFormat("int i = SomeFunction(a<b, a> b);");
6699 // This now gets parsed incorrectly as class definition.
6700 // verifyFormat("class A<int> f() {\n}\nint n;");
6702 // Elaborate types where incorrectly parsing the structural element would
6703 // break the indent.
6704 verifyFormat("if (true)\n"
6709 // This is simply incomplete. Formatting is not important, but must not crash.
6710 verifyFormat("class A:");
6713 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6714 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
6715 format("#error Leave all white!!!!! space* alone!\n"));
6717 "#warning Leave all white!!!!! space* alone!\n",
6718 format("#warning Leave all white!!!!! space* alone!\n"));
6719 EXPECT_EQ("#error 1", format(" # error 1"));
6720 EXPECT_EQ("#warning 1", format(" # warning 1"));
6723 TEST_F(FormatTest, FormatHashIfExpressions) {
6724 verifyFormat("#if AAAA && BBBB");
6725 verifyFormat("#if (AAAA && BBBB)");
6726 verifyFormat("#elif (AAAA && BBBB)");
6727 // FIXME: Come up with a better indentation for #elif.
6729 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n"
6730 " defined(BBBBBBBB)\n"
6731 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n"
6732 " defined(BBBBBBBB)\n"
6734 getLLVMStyleWithColumns(65));
6737 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6738 FormatStyle AllowsMergedIf = getGoogleStyle();
6739 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6740 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6741 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6742 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf);
6743 EXPECT_EQ("if (true) return 42;",
6744 format("if (true)\nreturn 42;", AllowsMergedIf));
6745 FormatStyle ShortMergedIf = AllowsMergedIf;
6746 ShortMergedIf.ColumnLimit = 25;
6747 verifyFormat("#define A \\\n"
6748 " if (true) return 42;",
6750 verifyFormat("#define A \\\n"
6755 verifyFormat("#define A \\\n"
6763 " if (true) continue;\n"
6766 " if (true) continue;\n"
6769 ShortMergedIf.ColumnLimit = 29;
6770 verifyFormat("#define A \\\n"
6771 " if (aaaaaaaaaa) return 1; \\\n"
6774 ShortMergedIf.ColumnLimit = 28;
6775 verifyFormat("#define A \\\n"
6776 " if (aaaaaaaaaa) \\\n"
6782 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6783 verifyFormat("if (0) /* a comment in a strange place */ {\n"
6786 verifyFormat("if (0) /* a comment in a strange place */ {\n"
6788 "} /* another comment */ else /* comment #3 */ {\n"
6791 verifyFormat("while (0) /* a comment in a strange place */ {\n"
6794 verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6797 verifyFormat("do /* a comment in a strange place */ {\n"
6799 "} /* another comment */ while (0);");
6802 TEST_F(FormatTest, BlockComments) {
6803 EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6804 format("/* *//* */ /* */\n/* *//* */ /* */"));
6805 EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;"));
6806 EXPECT_EQ("#define A /*123*/ \\\n"
6811 format("#define A /*123*/ b\n"
6813 "someCall(parameter);",
6814 getLLVMStyleWithColumns(15)));
6816 EXPECT_EQ("#define A\n"
6819 format("#define A\n"
6820 "/* */someCall(parameter);",
6821 getLLVMStyleWithColumns(15)));
6822 EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
6830 " * aaaaaa aaaaaa\n"
6832 getLLVMStyleWithColumns(10)));
6842 getLLVMStyleWithColumns(10)));
6844 FormatStyle NoBinPacking = getLLVMStyle();
6845 NoBinPacking.BinPackParameters = false;
6846 EXPECT_EQ("someFunction(1, /* comment 1 */\n"
6847 " 2, /* comment 2 */\n"
6848 " 3, /* comment 3 */\n"
6851 format("someFunction (1, /* comment 1 */\n"
6852 " 2, /* comment 2 */ \n"
6853 " 3, /* comment 3 */\n"
6857 "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6860 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6861 " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
6864 "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6865 " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
6868 "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6869 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6870 "int cccccccccccccccccccccccccccccc; /* comment */\n",
6871 format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6872 "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6873 "int cccccccccccccccccccccccccccccc; /* comment */\n"));
6875 verifyFormat("void f(int * /* unused */) {}");
6905 TEST_F(FormatTest, BlockCommentsInMacros) {
6906 EXPECT_EQ("#define A \\\n"
6908 " /* one line */ \\\n"
6910 format("#define A { \\\n"
6911 " /* one line */ \\\n"
6913 getLLVMStyleWithColumns(20)));
6914 EXPECT_EQ("#define A \\\n"
6916 " /* previous */ \\\n"
6917 " /* one line */ \\\n"
6919 format("#define A { \\\n"
6920 " /* previous */ \\\n"
6921 " /* one line */ \\\n"
6923 getLLVMStyleWithColumns(20)));
6926 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
6930 format("a = {1111 /* */\n"
6932 getLLVMStyleWithColumns(15)));
6936 format("a = {1111 /* */\n"
6938 getLLVMStyleWithColumns(15)));
6940 // FIXME: The formatting is still wrong here.
6945 format("a = {1111 /* a */\n"
6947 getLLVMStyleWithColumns(15)));
6950 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
6951 // FIXME: This is not what we want...
6957 TEST_F(FormatTest, FormatStarDependingOnContext) {
6958 verifyFormat("void f(int *a);");
6959 verifyFormat("void f() { f(fint * b); }");
6960 verifyFormat("class A {\n void f(int *a);\n};");
6961 verifyFormat("class A {\n int *a;\n};");
6962 verifyFormat("namespace a {\n"
6972 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6973 verifyFormat("while");
6974 verifyFormat("operator");
6977 //===----------------------------------------------------------------------===//
6978 // Objective-C tests.
6979 //===----------------------------------------------------------------------===//
6981 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6982 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6983 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6984 format("-(NSUInteger)indexOfObject:(id)anObject;"));
6985 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6986 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6987 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6988 format("-(NSInteger)Method3:(id)anObject;"));
6989 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6990 format("-(NSInteger)Method4:(id)anObject;"));
6991 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6992 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6993 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6994 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6995 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6996 "forAllCells:(BOOL)flag;",
6997 format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6998 "forAllCells:(BOOL)flag;"));
7000 // Very long objectiveC method declaration.
7001 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7002 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7003 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7004 " inRange:(NSRange)range\n"
7005 " outRange:(NSRange)out_range\n"
7006 " outRange1:(NSRange)out_range1\n"
7007 " outRange2:(NSRange)out_range2\n"
7008 " outRange3:(NSRange)out_range3\n"
7009 " outRange4:(NSRange)out_range4\n"
7010 " outRange5:(NSRange)out_range5\n"
7011 " outRange6:(NSRange)out_range6\n"
7012 " outRange7:(NSRange)out_range7\n"
7013 " outRange8:(NSRange)out_range8\n"
7014 " outRange9:(NSRange)out_range9;");
7016 // When the function name has to be wrapped.
7017 FormatStyle Style = getLLVMStyle();
7018 Style.IndentWrappedFunctionNames = false;
7019 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7020 "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7021 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7024 Style.IndentWrappedFunctionNames = true;
7025 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7026 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7027 " anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7031 verifyFormat("- (int)sum:(vector<int>)numbers;");
7032 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7033 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7034 // protocol lists (but not for template classes):
7035 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7037 verifyFormat("- (int (*)())foo:(int (*)())f;");
7038 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7040 // If there's no return type (very rare in practice!), LLVM and Google style
7042 verifyFormat("- foo;");
7043 verifyFormat("- foo:(int)f;");
7044 verifyGoogleFormat("- foo:(int)foo;");
7047 TEST_F(FormatTest, FormatObjCInterface) {
7048 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
7061 verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
7074 verifyFormat("@interface /* wait for it */ Foo\n"
7076 "// Look, a comment!\n"
7077 "- (int)answerWith:(int)i;\n"
7080 verifyFormat("@interface Foo\n"
7085 verifyFormat("@interface Foo : Bar\n"
7089 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
7093 verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
7097 verifyFormat("@interface Foo (HackStuff)\n"
7101 verifyFormat("@interface Foo ()\n"
7105 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
7109 verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
7113 verifyFormat("@interface Foo {\n"
7119 verifyFormat("@interface Foo : Bar {\n"
7125 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
7131 verifyFormat("@interface Foo (HackStuff) {\n"
7137 verifyFormat("@interface Foo () {\n"
7143 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
7149 FormatStyle OnePerLine = getGoogleStyle();
7150 OnePerLine.BinPackParameters = false;
7151 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
7152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7154 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7155 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
7160 TEST_F(FormatTest, FormatObjCImplementation) {
7161 verifyFormat("@implementation Foo : NSObject {\n"
7174 verifyGoogleFormat("@implementation Foo : NSObject {\n"
7187 verifyFormat("@implementation Foo\n"
7192 "// Look, a comment!\n"
7193 "- (int)answerWith:(int)i {\n"
7196 "+ (int)answerWith:(int)i {\n"
7201 verifyFormat("@implementation Foo\n"
7203 "@implementation Bar\n"
7206 EXPECT_EQ("@implementation Foo : Bar\n"
7208 "- (void)foo {\n}\n"
7210 format("@implementation Foo : Bar\n"
7215 verifyFormat("@implementation Foo {\n"
7221 verifyFormat("@implementation Foo : Bar {\n"
7227 verifyFormat("@implementation Foo (HackStuff)\n"
7230 verifyFormat("@implementation ObjcClass\n"
7236 TEST_F(FormatTest, FormatObjCProtocol) {
7237 verifyFormat("@protocol Foo\n"
7238 "@property(weak) id delegate;\n"
7239 "- (NSUInteger)numberOfThings;\n"
7242 verifyFormat("@protocol MyProtocol <NSObject>\n"
7243 "- (NSUInteger)numberOfThings;\n"
7246 verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
7247 "- (NSUInteger)numberOfThings;\n"
7250 verifyFormat("@protocol Foo;\n"
7251 "@protocol Bar;\n");
7253 verifyFormat("@protocol Foo\n"
7258 verifyFormat("@protocol myProtocol\n"
7259 "- (void)mandatoryWithInt:(int)i;\n"
7261 "- (void)optional;\n"
7263 "- (void)required;\n"
7265 "@property(assign) int madProp;\n"
7268 verifyFormat("@property(nonatomic, assign, readonly)\n"
7269 " int *looooooooooooooooooooooooooooongNumber;\n"
7270 "@property(nonatomic, assign, readonly)\n"
7271 " NSString *looooooooooooooooooooooooooooongName;");
7273 verifyFormat("@implementation PR18406\n"
7278 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
7279 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
7280 " rect:(NSRect)theRect\n"
7281 " interval:(float)theInterval {\n"
7283 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7284 " longKeyword:(NSRect)theRect\n"
7285 " longerKeyword:(float)theInterval\n"
7286 " error:(NSError **)theError {\n"
7288 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7289 " longKeyword:(NSRect)theRect\n"
7290 " evenLongerKeyword:(float)theInterval\n"
7291 " error:(NSError **)theError {\n"
7293 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
7294 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
7295 " NS_DESIGNATED_INITIALIZER;",
7296 getLLVMStyleWithColumns(60));
7298 // Continuation indent width should win over aligning colons if the function
7300 FormatStyle continuationStyle = getGoogleStyle();
7301 continuationStyle.ColumnLimit = 40;
7302 continuationStyle.IndentWrappedFunctionNames = true;
7303 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7304 " dontAlignNamef:(NSRect)theRect {\n"
7308 // Make sure we don't break aligning for short parameter names.
7309 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7310 " aShortf:(NSRect)theRect {\n"
7315 TEST_F(FormatTest, FormatObjCMethodExpr) {
7316 verifyFormat("[foo bar:baz];");
7317 verifyFormat("return [foo bar:baz];");
7318 verifyFormat("return (a)[foo bar:baz];");
7319 verifyFormat("f([foo bar:baz]);");
7320 verifyFormat("f(2, [foo bar:baz]);");
7321 verifyFormat("f(2, a ? b : c);");
7322 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
7325 verifyFormat("int a = +[foo bar:baz];");
7326 verifyFormat("int a = -[foo bar:baz];");
7327 verifyFormat("int a = ![foo bar:baz];");
7328 verifyFormat("int a = ~[foo bar:baz];");
7329 verifyFormat("int a = ++[foo bar:baz];");
7330 verifyFormat("int a = --[foo bar:baz];");
7331 verifyFormat("int a = sizeof [foo bar:baz];");
7332 verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
7333 verifyFormat("int a = &[foo bar:baz];");
7334 verifyFormat("int a = *[foo bar:baz];");
7335 // FIXME: Make casts work, without breaking f()[4].
7336 // verifyFormat("int a = (int)[foo bar:baz];");
7337 // verifyFormat("return (int)[foo bar:baz];");
7338 // verifyFormat("(void)[foo bar:baz];");
7339 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
7341 // Binary operators.
7342 verifyFormat("[foo bar:baz], [foo bar:baz];");
7343 verifyFormat("[foo bar:baz] = [foo bar:baz];");
7344 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
7345 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
7346 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
7347 verifyFormat("[foo bar:baz] += [foo bar:baz];");
7348 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
7349 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
7350 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
7351 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
7352 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
7353 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
7354 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
7355 verifyFormat("[foo bar:baz] || [foo bar:baz];");
7356 verifyFormat("[foo bar:baz] && [foo bar:baz];");
7357 verifyFormat("[foo bar:baz] | [foo bar:baz];");
7358 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
7359 verifyFormat("[foo bar:baz] & [foo bar:baz];");
7360 verifyFormat("[foo bar:baz] == [foo bar:baz];");
7361 verifyFormat("[foo bar:baz] != [foo bar:baz];");
7362 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
7363 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
7364 verifyFormat("[foo bar:baz] > [foo bar:baz];");
7365 verifyFormat("[foo bar:baz] < [foo bar:baz];");
7366 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
7367 verifyFormat("[foo bar:baz] << [foo bar:baz];");
7368 verifyFormat("[foo bar:baz] - [foo bar:baz];");
7369 verifyFormat("[foo bar:baz] + [foo bar:baz];");
7370 verifyFormat("[foo bar:baz] * [foo bar:baz];");
7371 verifyFormat("[foo bar:baz] / [foo bar:baz];");
7372 verifyFormat("[foo bar:baz] % [foo bar:baz];");
7375 verifyFormat("return in[42];");
7376 verifyFormat("for (auto v : in[1]) {\n}");
7377 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
7378 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
7379 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
7380 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
7381 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
7382 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
7384 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
7385 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
7386 verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
7388 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
7389 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
7390 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
7391 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
7392 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
7393 verifyFormat("[button setAction:@selector(zoomOut:)];");
7394 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
7396 verifyFormat("arr[[self indexForFoo:a]];");
7397 verifyFormat("throw [self errorFor:a];");
7398 verifyFormat("@throw [self errorFor:a];");
7400 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
7401 verifyFormat("[(id)foo bar:(id) ? baz : quux];");
7402 verifyFormat("4 > 4 ? (id)a : (id)baz;");
7404 // This tests that the formatter doesn't break after "backing" but before ":",
7405 // which would be at 80 columns.
7408 " if ((self = [super initWithContentRect:contentRect\n"
7409 " styleMask:styleMask ?: otherMask\n"
7410 " backing:NSBackingStoreBuffered\n"
7414 "[foo checkThatBreakingAfterColonWorksOk:\n"
7415 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
7417 verifyFormat("[myObj short:arg1 // Force line break\n"
7418 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
7419 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
7423 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7424 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7425 " pos.width(), pos.height())\n"
7426 " styleMask:NSBorderlessWindowMask\n"
7427 " backing:NSBackingStoreBuffered\n"
7432 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
7433 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
7434 " pos.width(), pos.height())\n"
7435 " syeMask:NSBorderlessWindowMask\n"
7436 " bking:NSBackingStoreBuffered\n"
7439 getLLVMStyleWithColumns(70));
7442 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7443 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7444 " pos.width(), pos.height())\n"
7445 " styleMask:NSBorderlessWindowMask\n"
7446 " backing:NSBackingStoreBuffered\n"
7449 getChromiumStyle(FormatStyle::LK_Cpp));
7450 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
7451 " with:contentsNativeView];");
7454 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
7455 " owner:nillllll];");
7458 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7459 " forType:kBookmarkButtonDragType];");
7461 verifyFormat("[defaultCenter addObserver:self\n"
7462 " selector:@selector(willEnterFullscreen)\n"
7463 " name:kWillEnterFullscreenNotification\n"
7465 verifyFormat("[image_rep drawInRect:drawRect\n"
7466 " fromRect:NSZeroRect\n"
7467 " operation:NSCompositeCopy\n"
7469 " respectFlipped:NO\n"
7471 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7473 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7474 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7475 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
7476 " aaaaaaaaaaaaaaaaaaaaaa];");
7477 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
7478 " .aaaaaaaa.aaaaaaaa];", // FIXME: Indentation seems off.
7479 getLLVMStyleWithColumns(60));
7482 "scoped_nsobject<NSTextField> message(\n"
7483 " // The frame will be fixed up when |-setMessageText:| is called.\n"
7484 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7485 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7486 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7487 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7489 verifyFormat("[self param:function( //\n"
7492 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7493 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7494 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7496 // FIXME: This violates the column limit.
7498 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7499 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
7500 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
7501 getLLVMStyleWithColumns(60));
7503 // Variadic parameters.
7505 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7507 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7508 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7509 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7510 verifyFormat("[self // break\n"
7513 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7514 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7517 TEST_F(FormatTest, ObjCAt) {
7518 verifyFormat("@autoreleasepool");
7519 verifyFormat("@catch");
7520 verifyFormat("@class");
7521 verifyFormat("@compatibility_alias");
7522 verifyFormat("@defs");
7523 verifyFormat("@dynamic");
7524 verifyFormat("@encode");
7525 verifyFormat("@end");
7526 verifyFormat("@finally");
7527 verifyFormat("@implementation");
7528 verifyFormat("@import");
7529 verifyFormat("@interface");
7530 verifyFormat("@optional");
7531 verifyFormat("@package");
7532 verifyFormat("@private");
7533 verifyFormat("@property");
7534 verifyFormat("@protected");
7535 verifyFormat("@protocol");
7536 verifyFormat("@public");
7537 verifyFormat("@required");
7538 verifyFormat("@selector");
7539 verifyFormat("@synchronized");
7540 verifyFormat("@synthesize");
7541 verifyFormat("@throw");
7542 verifyFormat("@try");
7544 EXPECT_EQ("@interface", format("@ interface"));
7546 // The precise formatting of this doesn't matter, nobody writes code like
7548 verifyFormat("@ /*foo*/ interface");
7551 TEST_F(FormatTest, ObjCSnippets) {
7552 verifyFormat("@autoreleasepool {\n"
7555 verifyFormat("@class Foo, Bar;");
7556 verifyFormat("@compatibility_alias AliasName ExistingClass;");
7557 verifyFormat("@dynamic textColor;");
7558 verifyFormat("char *buf1 = @encode(int *);");
7559 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7560 verifyFormat("char *buf1 = @encode(int **);");
7561 verifyFormat("Protocol *proto = @protocol(p1);");
7562 verifyFormat("SEL s = @selector(foo:);");
7563 verifyFormat("@synchronized(self) {\n"
7567 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7568 verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7570 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7571 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7572 verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7573 verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7575 verifyFormat("@property BOOL editable;", getMozillaStyle());
7576 verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7578 verifyFormat("@property BOOL editable;", getWebKitStyle());
7580 verifyFormat("@import foo.bar;\n"
7584 TEST_F(FormatTest, ObjCForIn) {
7585 verifyFormat("- (void)test {\n"
7586 " for (NSString *n in arrayOfStrings) {\n"
7590 verifyFormat("- (void)test {\n"
7591 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
7597 TEST_F(FormatTest, ObjCLiterals) {
7598 verifyFormat("@\"String\"");
7600 verifyFormat("@+4.8");
7601 verifyFormat("@-4");
7602 verifyFormat("@1LL");
7603 verifyFormat("@.5");
7604 verifyFormat("@'c'");
7605 verifyFormat("@true");
7607 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7608 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7609 verifyFormat("NSNumber *favoriteColor = @(Green);");
7610 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7612 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7615 TEST_F(FormatTest, ObjCDictLiterals) {
7617 verifyFormat("@{}");
7618 verifyFormat("@{@\"one\" : @1}");
7619 verifyFormat("return @{@\"one\" : @1;");
7620 verifyFormat("@{@\"one\" : @1}");
7622 verifyFormat("@{@\"one\" : @{@2 : @1}}");
7624 " @\"one\" : @{@2 : @1},\n"
7627 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7628 verifyIncompleteFormat("[self setDict:@{}");
7629 verifyIncompleteFormat("[self setDict:@{@1 : @2}");
7630 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7632 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7634 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7636 verifyFormat("NSDictionary *d = @{\n"
7637 " @\"nam\" : NSUserNam(),\n"
7638 " @\"dte\" : [NSDate date],\n"
7639 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
7643 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7648 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7653 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7654 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7657 // We should try to be robust in case someone forgets the "@".
7658 verifyFormat("NSDictionary *d = {\n"
7659 " @\"nam\" : NSUserNam(),\n"
7660 " @\"dte\" : [NSDate date],\n"
7661 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
7663 verifyFormat("NSMutableDictionary *dictionary =\n"
7664 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
7665 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7666 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7667 " cccccccccccccccc : ccccccccccccccc\n"
7670 // Ensure that casts before the key are kept on the same line as the key.
7672 "NSDictionary *d = @{\n"
7673 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7674 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
7678 TEST_F(FormatTest, ObjCArrayLiterals) {
7679 verifyIncompleteFormat("@[");
7680 verifyFormat("@[]");
7682 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7683 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7684 verifyFormat("NSArray *array = @[ [foo description] ];");
7687 "NSArray *some_variable = @[\n"
7688 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7689 " @\"aaaaaaaaaaaaaaaaa\",\n"
7690 " @\"aaaaaaaaaaaaaaaaa\",\n"
7691 " @\"aaaaaaaaaaaaaaaaa\"\n"
7693 verifyFormat("NSArray *some_variable = @[\n"
7694 " @\"aaaaaaaaaaaaaaaaa\",\n"
7695 " @\"aaaaaaaaaaaaaaaaa\",\n"
7696 " @\"aaaaaaaaaaaaaaaaa\",\n"
7697 " @\"aaaaaaaaaaaaaaaaa\",\n"
7699 verifyGoogleFormat("NSArray *some_variable = @[\n"
7700 " @\"aaaaaaaaaaaaaaaaa\",\n"
7701 " @\"aaaaaaaaaaaaaaaaa\",\n"
7702 " @\"aaaaaaaaaaaaaaaaa\",\n"
7703 " @\"aaaaaaaaaaaaaaaaa\"\n"
7705 verifyFormat("NSArray *array = @[\n"
7707 " @\"a\",\n" // Trailing comma -> one per line.
7710 // We should try to be robust in case someone forgets the "@".
7711 verifyFormat("NSArray *some_variable = [\n"
7712 " @\"aaaaaaaaaaaaaaaaa\",\n"
7713 " @\"aaaaaaaaaaaaaaaaa\",\n"
7714 " @\"aaaaaaaaaaaaaaaaa\",\n"
7715 " @\"aaaaaaaaaaaaaaaaa\",\n"
7718 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7719 " index:(NSUInteger)index\n"
7720 " nonDigitAttributes:\n"
7721 " (NSDictionary *)noDigitAttributes;");
7722 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
7723 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
7727 TEST_F(FormatTest, BreaksStringLiterals) {
7728 EXPECT_EQ("\"some text \"\n"
7730 format("\"some text other\";", getLLVMStyleWithColumns(12)));
7731 EXPECT_EQ("\"some text \"\n"
7733 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7739 format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7745 format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7747 EXPECT_EQ("\"some text\"",
7748 format("\"some text\"", getLLVMStyleWithColumns(1)));
7749 EXPECT_EQ("\"some text\"",
7750 format("\"some text\"", getLLVMStyleWithColumns(11)));
7751 EXPECT_EQ("\"some \"\n"
7753 format("\"some text\"", getLLVMStyleWithColumns(10)));
7754 EXPECT_EQ("\"some \"\n"
7756 format("\"some text\"", getLLVMStyleWithColumns(7)));
7757 EXPECT_EQ("\"some\"\n"
7760 format("\"some text\"", getLLVMStyleWithColumns(6)));
7761 EXPECT_EQ("\"some\"\n"
7764 format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7765 EXPECT_EQ("\"some\"\n"
7768 format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7770 EXPECT_EQ("variable =\n"
7771 " \"long string \"\n"
7773 format("variable = \"long string literal\";",
7774 getLLVMStyleWithColumns(20)));
7776 EXPECT_EQ("variable = f(\n"
7777 " \"long string \"\n"
7780 " loooooooooooooooooooong);",
7781 format("variable = f(\"long string literal\", short, "
7782 "loooooooooooooooooooong);",
7783 getLLVMStyleWithColumns(20)));
7786 "f(g(\"long string \"\n"
7789 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7790 EXPECT_EQ("f(g(\"long string \"\n"
7794 format("f(g(\"long string literal\", a), b);",
7795 getLLVMStyleWithColumns(20)));
7797 "f(\"one two\".split(\n"
7799 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7800 EXPECT_EQ("f(\"one two three four five six \"\n"
7801 " \"seven\".split(\n"
7802 " really_looooong_variable));",
7803 format("f(\"one two three four five six seven\"."
7804 "split(really_looooong_variable));",
7805 getLLVMStyleWithColumns(33)));
7807 EXPECT_EQ("f(\"some \"\n"
7810 format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7812 // Only break as a last resort.
7814 "aaaaaaaaaaaaaaaaaaaa(\n"
7815 " aaaaaaaaaaaaaaaaaaaa,\n"
7816 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7818 EXPECT_EQ("\"splitmea\"\n"
7821 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7823 EXPECT_EQ("\"split/\"\n"
7826 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7828 EXPECT_EQ("\"split/\"\n"
7831 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7832 EXPECT_EQ("\"split at \"\n"
7834 "\"slashes.at.any$\"\n"
7835 "\"non-alphanumeric%\"\n"
7836 "\"1111111111characte\"\n"
7838 format("\"split at "
7843 "1111111111characte"
7845 getLLVMStyleWithColumns(20)));
7847 // Verify that splitting the strings understands
7848 // Style::AlwaysBreakBeforeMultilineStrings.
7851 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7852 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7853 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7854 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7855 "aaaaaaaaaaaaaaaaaaaaaa\");",
7857 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7858 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7859 format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7860 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7861 "aaaaaaaaaaaaaaaaaaaaaa\";",
7863 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7864 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7865 format("llvm::outs() << "
7866 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7867 "aaaaaaaaaaaaaaaaaaa\";"));
7869 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7870 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7871 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7872 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7875 FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7876 AlignLeft.AlignEscapedNewlinesLeft = true;
7877 EXPECT_EQ("#define A \\\n"
7881 format("#define A \"some text other\";", AlignLeft));
7884 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7885 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7886 NoEmptyLines.MaxEmptyLinesToKeep = 0;
7887 EXPECT_EQ("int i = a(b());",
7888 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7891 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7893 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7896 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7901 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7903 "u8\"utf8 string \"\n"
7905 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7907 "u\"utf16 string \"\n"
7909 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7911 "U\"utf32 string \"\n"
7913 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7914 EXPECT_EQ("L\"wide string \"\n"
7916 format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7917 EXPECT_EQ("@\"NSString \"\n"
7919 format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7921 // This input makes clang-format try to split the incomplete unicode escape
7922 // sequence, which used to lead to a crasher.
7924 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7925 getLLVMStyleWithColumns(60));
7928 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7929 FormatStyle Style = getGoogleStyleWithColumns(15);
7930 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7931 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7932 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7933 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7934 EXPECT_EQ("u8R\"x(raw literal)x\";",
7935 format("u8R\"x(raw literal)x\";", Style));
7938 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7939 FormatStyle Style = getLLVMStyleWithColumns(20);
7941 "_T(\"aaaaaaaaaaaaaa\")\n"
7942 "_T(\"aaaaaaaaaaaaaa\")\n"
7943 "_T(\"aaaaaaaaaaaa\")",
7944 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7945 EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
7946 " _T(\"aaaaaa\"),\n"
7948 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7950 // FIXME: Handle embedded spaces in one iteration.
7951 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7952 // "_T(\"aaaaaaaaaaaaa\")\n"
7953 // "_T(\"aaaaaaaaaaaaa\")\n"
7955 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7956 // getLLVMStyleWithColumns(20)));
7958 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7959 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7962 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7967 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7972 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7975 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7978 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7980 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7981 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7982 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7983 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7988 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7989 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7990 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()));
7991 EXPECT_EQ("fffffffffff(g(R\"x(\n"
7992 "multiline raw string literal xxxxxxxxxxxxxx\n"
7996 format("fffffffffff(g(R\"x(\n"
7997 "multiline raw string literal xxxxxxxxxxxxxx\n"
7999 getGoogleStyleWithColumns(20)));
8000 EXPECT_EQ("fffffffffff(\n"
8002 "multiline raw string literal xxxxxxxxxxxxxx\n"
8006 format("fffffffffff(g(R\"x(qqq\n"
8007 "multiline raw string literal xxxxxxxxxxxxxx\n"
8009 getGoogleStyleWithColumns(20)));
8011 EXPECT_EQ("fffffffffff(R\"x(\n"
8012 "multiline raw string literal xxxxxxxxxxxxxx\n"
8014 format("fffffffffff(R\"x(\n"
8015 "multiline raw string literal xxxxxxxxxxxxxx\n"
8017 getGoogleStyleWithColumns(20)));
8018 EXPECT_EQ("fffffffffff(R\"x(\n"
8019 "multiline raw string literal xxxxxxxxxxxxxx\n"
8021 format("fffffffffff(R\"x(\n"
8022 "multiline raw string literal xxxxxxxxxxxxxx\n"
8024 getGoogleStyleWithColumns(20)));
8025 EXPECT_EQ("fffffffffff(\n"
8027 "multiline raw string literal xxxxxxxxxxxxxx\n"
8030 format("fffffffffff(\n"
8032 "multiline raw string literal xxxxxxxxxxxxxx\n"
8034 getGoogleStyleWithColumns(20)));
8037 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8038 verifyFormat("string a = \"unterminated;");
8039 EXPECT_EQ("function(\"unterminated,\n"
8040 " OtherParameter);",
8041 format("function( \"unterminated,\n"
8042 " OtherParameter);"));
8045 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8046 FormatStyle Style = getLLVMStyle();
8047 Style.Standard = FormatStyle::LS_Cpp03;
8048 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8049 format("#define x(_a) printf(\"foo\"_a);", Style));
8052 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8054 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8055 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8057 format("someFunction(\"aaabbbcccdddeeefff\");",
8058 getLLVMStyleWithColumns(25)));
8059 EXPECT_EQ("someFunction1234567890(\n"
8060 " \"aaabbbcccdddeeefff\");",
8061 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8062 getLLVMStyleWithColumns(26)));
8063 EXPECT_EQ("someFunction1234567890(\n"
8064 " \"aaabbbcccdddeeeff\"\n"
8066 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8067 getLLVMStyleWithColumns(25)));
8068 EXPECT_EQ("someFunction1234567890(\n"
8069 " \"aaabbbcccdddeeeff\"\n"
8071 format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8072 getLLVMStyleWithColumns(24)));
8073 EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8076 format("someFunction(\"aaabbbcc ddde efff\");",
8077 getLLVMStyleWithColumns(25)));
8078 EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8080 format("someFunction(\"aaabbbccc ddeeefff\");",
8081 getLLVMStyleWithColumns(25)));
8082 EXPECT_EQ("someFunction1234567890(\n"
8084 " \"cccdddeeefff\");",
8085 format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8086 getLLVMStyleWithColumns(25)));
8087 EXPECT_EQ("#define A \\\n"
8089 " \"123456789\" \\\n"
8092 format("#define A string s = \"1234567890\"; int i;",
8093 getLLVMStyleWithColumns(20)));
8094 // FIXME: Put additional penalties on breaking at non-whitespace locations.
8095 EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8098 format("someFunction(\"aaabbbcc dddeeefff\");",
8099 getLLVMStyleWithColumns(25)));
8102 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8103 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8104 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8105 EXPECT_EQ("\"test\"\n"
8107 format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8108 EXPECT_EQ("\"tes\\\\\"\n"
8110 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8111 EXPECT_EQ("\"\\\\\\\\\"\n"
8113 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8114 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8115 EXPECT_EQ("\"\\uff01\"\n"
8117 format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8118 EXPECT_EQ("\"\\Uff01ff02\"",
8119 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8120 EXPECT_EQ("\"\\x000000000001\"\n"
8122 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8123 EXPECT_EQ("\"\\x000000000001next\"",
8124 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8125 EXPECT_EQ("\"\\x000000000001\"",
8126 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8127 EXPECT_EQ("\"test\"\n"
8130 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8131 EXPECT_EQ("\"test\\000\"\n"
8134 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8137 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8138 verifyFormat("void f() {\n"
8141 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8146 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8148 "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8151 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8152 verifyFormat("class X {\n"
8156 getLLVMStyleWithColumns(12));
8159 TEST_F(FormatTest, ConfigurableIndentWidth) {
8160 FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8161 EightIndent.IndentWidth = 8;
8162 EightIndent.ContinuationIndentWidth = 8;
8163 verifyFormat("void f() {\n"
8164 " someFunction();\n"
8170 verifyFormat("class X {\n"
8175 verifyFormat("int x[] = {\n"
8181 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8182 verifyFormat("double\n"
8184 getLLVMStyleWithColumns(8));
8187 TEST_F(FormatTest, ConfigurableUseOfTab) {
8188 FormatStyle Tab = getLLVMStyleWithColumns(42);
8189 Tab.IndentWidth = 8;
8190 Tab.UseTab = FormatStyle::UT_Always;
8191 Tab.AlignEscapedNewlinesLeft = true;
8193 EXPECT_EQ("if (aaaaaaaa && // q\n"
8196 format("if (aaaaaaaa &&// q\n"
8200 EXPECT_EQ("if (aaa && bbb) // w\n"
8202 format("if(aaa&&bbb)// w\n"
8206 verifyFormat("class X {\n"
8208 "\t\tsomeFunction(parameter1,\n"
8209 "\t\t\t parameter2);\n"
8213 verifyFormat("#define A \\\n"
8215 "\t\tsomeFunction( \\\n"
8216 "\t\t parameter1, \\\n"
8217 "\t\t parameter2); \\\n"
8222 Tab.IndentWidth = 8;
8223 verifyFormat("class TabWidth4Indent8 {\n"
8225 "\t\t\t\tsomeFunction(parameter1,\n"
8226 "\t\t\t\t\t\t\t parameter2);\n"
8232 Tab.IndentWidth = 4;
8233 verifyFormat("class TabWidth4Indent4 {\n"
8235 "\t\tsomeFunction(parameter1,\n"
8236 "\t\t\t\t\t parameter2);\n"
8242 Tab.IndentWidth = 4;
8243 verifyFormat("class TabWidth8Indent4 {\n"
8245 "\tsomeFunction(parameter1,\n"
8246 "\t\t parameter2);\n"
8252 Tab.IndentWidth = 8;
8255 "\t in multiple lines\n"
8257 format(" /*\t \t \n"
8258 " \t \t a\t\tcomment\t \t\n"
8259 " \t \t in multiple lines\t\n"
8263 Tab.UseTab = FormatStyle::UT_ForIndentation;
8265 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8266 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8267 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8268 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8269 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8270 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8273 verifyFormat("enum A {\n"
8274 "\ta1, // Force multiple lines\n"
8279 EXPECT_EQ("if (aaaaaaaa && // q\n"
8282 format("if (aaaaaaaa &&// q\n"
8286 verifyFormat("class X {\n"
8288 "\t\tsomeFunction(parameter1,\n"
8289 "\t\t parameter2);\n"
8297 "\t\t someFunction(aaaaaaaa,\n"
8314 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8315 "\t bbbbbbbbbbbbb\n"
8320 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8325 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8326 "\t// bbbbbbbbbbbbb\n"
8329 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8334 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8335 "\t bbbbbbbbbbbbb\n"
8340 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8367 Tab.UseTab = FormatStyle::UT_Never;
8370 " in multiple lines\n"
8372 format(" /*\t \t \n"
8373 " \t \t a\t\tcomment\t \t\n"
8374 " \t \t in multiple lines\t\n"
8377 EXPECT_EQ("/* some\n"
8379 format(" \t \t /* some\n"
8380 " \t \t comment */",
8382 EXPECT_EQ("int a; /* some\n"
8384 format(" \t \t int a; /* some\n"
8385 " \t \t comment */",
8388 EXPECT_EQ("int a; /* some\n"
8390 format(" \t \t int\ta; /* some\n"
8391 " \t \t comment */",
8393 EXPECT_EQ("f(\"\t\t\"); /* some\n"
8395 format(" \t \t f(\"\t\t\"); /* some\n"
8396 " \t \t comment */",
8412 TEST_F(FormatTest, CalculatesOriginalColumn) {
8413 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8416 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8420 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8423 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8427 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8431 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8436 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8439 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8445 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8446 FormatStyle NoSpace = getLLVMStyle();
8447 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8449 verifyFormat("while(true)\n"
8452 verifyFormat("for(;;)\n"
8455 verifyFormat("if(true)\n"
8460 verifyFormat("do {\n"
8461 " do_something();\n"
8462 "} while(something());",
8464 verifyFormat("switch(x) {\n"
8469 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8470 verifyFormat("size_t x = sizeof(x);", NoSpace);
8471 verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8472 verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8473 verifyFormat("alignas(128) char a[128];", NoSpace);
8474 verifyFormat("size_t x = alignof(MyType);", NoSpace);
8475 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8476 verifyFormat("int f() throw(Deprecated);", NoSpace);
8477 verifyFormat("typedef void (*cb)(int);", NoSpace);
8478 verifyFormat("T A::operator()();", NoSpace);
8479 verifyFormat("X A::operator++(T);", NoSpace);
8481 FormatStyle Space = getLLVMStyle();
8482 Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8484 verifyFormat("int f ();", Space);
8485 verifyFormat("void f (int a, T b) {\n"
8490 verifyFormat("if (true)\n"
8495 verifyFormat("do {\n"
8496 " do_something ();\n"
8497 "} while (something ());",
8499 verifyFormat("switch (x) {\n"
8504 verifyFormat("A::A () : a (1) {}", Space);
8505 verifyFormat("void f () __attribute__ ((asdf));", Space);
8506 verifyFormat("*(&a + 1);\n"
8509 "(((a + 1) * 2) + 3) * 4;",
8511 verifyFormat("#define A(x) x", Space);
8512 verifyFormat("#define A (x) x", Space);
8513 verifyFormat("#if defined(x)\n"
8516 verifyFormat("auto i = std::make_unique<int> (5);", Space);
8517 verifyFormat("size_t x = sizeof (x);", Space);
8518 verifyFormat("auto f (int x) -> decltype (x);", Space);
8519 verifyFormat("int f (T x) noexcept (x.create ());", Space);
8520 verifyFormat("alignas (128) char a[128];", Space);
8521 verifyFormat("size_t x = alignof (MyType);", Space);
8522 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8523 verifyFormat("int f () throw (Deprecated);", Space);
8524 verifyFormat("typedef void (*cb) (int);", Space);
8525 verifyFormat("T A::operator() ();", Space);
8526 verifyFormat("X A::operator++ (T);", Space);
8529 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8530 FormatStyle Spaces = getLLVMStyle();
8532 Spaces.SpacesInParentheses = true;
8533 verifyFormat("call( x, y, z );", Spaces);
8534 verifyFormat("call();", Spaces);
8535 verifyFormat("std::function<void( int, int )> callback;", Spaces);
8536 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8538 verifyFormat("while ( (bool)1 )\n"
8541 verifyFormat("for ( ;; )\n"
8544 verifyFormat("if ( true )\n"
8546 "else if ( true )\n"
8549 verifyFormat("do {\n"
8550 " do_something( (int)i );\n"
8551 "} while ( something() );",
8553 verifyFormat("switch ( x ) {\n"
8559 Spaces.SpacesInParentheses = false;
8560 Spaces.SpacesInCStyleCastParentheses = true;
8561 verifyFormat("Type *A = ( Type * )P;", Spaces);
8562 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8563 verifyFormat("x = ( int32 )y;", Spaces);
8564 verifyFormat("int a = ( int )(2.0f);", Spaces);
8565 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8566 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8567 verifyFormat("#define x (( int )-1)", Spaces);
8569 // Run the first set of tests again with:
8570 Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
8571 Spaces.SpacesInCStyleCastParentheses = true;
8572 verifyFormat("call(x, y, z);", Spaces);
8573 verifyFormat("call( );", Spaces);
8574 verifyFormat("std::function<void(int, int)> callback;", Spaces);
8575 verifyFormat("while (( bool )1)\n"
8578 verifyFormat("for (;;)\n"
8581 verifyFormat("if (true)\n"
8586 verifyFormat("do {\n"
8587 " do_something(( int )i);\n"
8588 "} while (something( ));",
8590 verifyFormat("switch (x) {\n"
8596 // Run the first set of tests again with:
8597 Spaces.SpaceAfterCStyleCast = true;
8598 verifyFormat("call(x, y, z);", Spaces);
8599 verifyFormat("call( );", Spaces);
8600 verifyFormat("std::function<void(int, int)> callback;", Spaces);
8601 verifyFormat("while (( bool ) 1)\n"
8604 verifyFormat("for (;;)\n"
8607 verifyFormat("if (true)\n"
8612 verifyFormat("do {\n"
8613 " do_something(( int ) i);\n"
8614 "} while (something( ));",
8616 verifyFormat("switch (x) {\n"
8622 // Run subset of tests again with:
8623 Spaces.SpacesInCStyleCastParentheses = false;
8624 Spaces.SpaceAfterCStyleCast = true;
8625 verifyFormat("while ((bool) 1)\n"
8628 verifyFormat("do {\n"
8629 " do_something((int) i);\n"
8630 "} while (something( ));",
8634 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8635 verifyFormat("int a[5];");
8636 verifyFormat("a[3] += 42;");
8638 FormatStyle Spaces = getLLVMStyle();
8639 Spaces.SpacesInSquareBrackets = true;
8640 // Lambdas unchanged.
8641 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8642 verifyFormat("return [i, args...] {};", Spaces);
8645 verifyFormat("int a[ 5 ];", Spaces);
8646 verifyFormat("a[ 3 ] += 42;", Spaces);
8647 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8648 verifyFormat("double &operator[](int i) { return 0; }\n"
8651 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8652 verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8653 verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8656 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8657 verifyFormat("int a = 5;");
8658 verifyFormat("a += 42;");
8659 verifyFormat("a or_eq 8;");
8661 FormatStyle Spaces = getLLVMStyle();
8662 Spaces.SpaceBeforeAssignmentOperators = false;
8663 verifyFormat("int a= 5;", Spaces);
8664 verifyFormat("a+= 42;", Spaces);
8665 verifyFormat("a or_eq 8;", Spaces);
8668 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8669 FormatStyle Alignment = getLLVMStyle();
8670 Alignment.AlignConsecutiveAssignments = false;
8671 verifyFormat("int a = 5;\n"
8672 "int oneTwoThree = 123;",
8674 verifyFormat("int a = 5;\n"
8675 "int oneTwoThree = 123;",
8678 Alignment.AlignConsecutiveAssignments = true;
8679 verifyFormat("int a = 5;\n"
8680 "int oneTwoThree = 123;",
8682 verifyFormat("int a = method();\n"
8683 "int oneTwoThree = 133;",
8685 verifyFormat("a &= 5;\n"
8691 "sfdbddfbdfbb ^= 5;\n"
8693 "int dsvvdvsdvvv = 123;",
8695 verifyFormat("int i = 1, j = 10;\n"
8696 "something = 2000;",
8698 verifyFormat("something = 2000;\n"
8699 "int i = 1, j = 10;\n",
8701 verifyFormat("something = 2000;\n"
8703 "int i = 1, j = 10;\n"
8707 verifyFormat("int a = 5;\n"
8710 "int oneTwoThree = 123;\n"
8713 verifyFormat("int oneTwoThree = 123;\n"
8714 "int oneTwo = 12;\n"
8717 verifyFormat("int oneTwoThree = 123; // comment\n"
8718 "int oneTwo = 12; // comment",
8720 EXPECT_EQ("int a = 5;\n"
8722 "int oneTwoThree = 123;",
8723 format("int a = 5;\n"
8725 "int oneTwoThree= 123;",
8727 EXPECT_EQ("int a = 5;\n"
8730 "int oneTwoThree = 123;",
8731 format("int a = 5;\n"
8734 "int oneTwoThree = 123;",
8736 EXPECT_EQ("int a = 5;\n"
8739 "int oneTwoThree = 123;\n"
8741 format("int a = 5;\n"
8744 "int oneTwoThree = 123;\n"
8747 Alignment.AlignEscapedNewlinesLeft = true;
8748 verifyFormat("#define A \\\n"
8749 " int aaaa = 12; \\\n"
8751 " int ccc = 234; \\\n"
8752 " int dddddddddd = 2345;",
8754 Alignment.AlignEscapedNewlinesLeft = false;
8755 verifyFormat("#define A "
8763 " int dddddddddd = 2345;",
8765 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8766 "k = 4, int l = 5,\n"
8769 " otherThing = 1;\n"
8772 verifyFormat("void SomeFunction(int parameter = 0) {\n"
8775 " int big = 10000;\n"
8778 verifyFormat("class C {\n"
8781 " virtual void f() = 0;\n"
8784 verifyFormat("int i = 1;\n"
8785 "if (SomeType t = getSomething()) {\n"
8790 verifyFormat("int j = 7;\n"
8791 "for (int k = 0; k < N; ++k) {\n"
8794 "int big = 10000;\n"
8797 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8798 verifyFormat("int i = 1;\n"
8799 "LooooooooooongType loooooooooooooooooooooongVariable\n"
8800 " = someLooooooooooooooooongFunction();\n"
8803 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8804 verifyFormat("int i = 1;\n"
8805 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8806 " someLooooooooooooooooongFunction();\n"
8810 verifyFormat("auto lambda = []() {\n"
8822 // FIXME: Should align all three assignments
8825 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8826 " loooooooooooooooooooooongParameterB);\n"
8830 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
8831 " typename B = very_long_type_name_1,\n"
8832 " typename T_2 = very_long_type_name_2>\n"
8835 verifyFormat("int a, b = 1;\n"
8839 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
8840 "float b[1][] = {{3.f}};\n",
8844 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
8845 FormatStyle Alignment = getLLVMStyle();
8846 Alignment.AlignConsecutiveDeclarations = false;
8847 verifyFormat("float const a = 5;\n"
8848 "int oneTwoThree = 123;",
8850 verifyFormat("int a = 5;\n"
8851 "float const oneTwoThree = 123;",
8854 Alignment.AlignConsecutiveDeclarations = true;
8855 verifyFormat("float const a = 5;\n"
8856 "int oneTwoThree = 123;",
8858 verifyFormat("int a = method();\n"
8859 "float const oneTwoThree = 133;",
8861 verifyFormat("int i = 1, j = 10;\n"
8862 "something = 2000;",
8864 verifyFormat("something = 2000;\n"
8865 "int i = 1, j = 10;\n",
8867 verifyFormat("float something = 2000;\n"
8868 "double another = 911;\n"
8869 "int i = 1, j = 10;\n"
8870 "const int *oneMore = 1;\n"
8873 verifyFormat("float a = 5;\n"
8876 "const double oneTwoThree = 123;\n"
8877 "const unsigned int oneTwo = 12;",
8879 verifyFormat("int oneTwoThree{0}; // comment\n"
8880 "unsigned oneTwo; // comment",
8882 EXPECT_EQ("float const a = 5;\n"
8884 "int oneTwoThree = 123;",
8885 format("float const a = 5;\n"
8887 "int oneTwoThree= 123;",
8889 EXPECT_EQ("float a = 5;\n"
8892 "unsigned oneTwoThree = 123;",
8893 format("float a = 5;\n"
8896 "unsigned oneTwoThree = 123;",
8898 EXPECT_EQ("float a = 5;\n"
8901 "unsigned oneTwoThree = 123;\n"
8903 format("float a = 5;\n"
8906 "unsigned oneTwoThree = 123;\n"
8909 Alignment.AlignConsecutiveAssignments = true;
8910 verifyFormat("float something = 2000;\n"
8911 "double another = 911;\n"
8912 "int i = 1, j = 10;\n"
8913 "const int *oneMore = 1;\n"
8916 verifyFormat("int oneTwoThree = {0}; // comment\n"
8917 "unsigned oneTwo = 0; // comment",
8919 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
8920 " int const i = 1;\n"
8922 " int big = 10000;\n"
8924 " unsigned oneTwoThree = 123;\n"
8925 " int oneTwo = 12;\n"
8928 " int ll = 10000;\n"
8930 format("void SomeFunction(int parameter= 0) {\n"
8931 " int const i= 1;\n"
8933 " int big = 10000;\n"
8935 "unsigned oneTwoThree =123;\n"
8936 "int oneTwo = 12;\n"
8942 Alignment.AlignConsecutiveAssignments = false;
8943 Alignment.AlignEscapedNewlinesLeft = true;
8944 verifyFormat("#define A \\\n"
8945 " int aaaa = 12; \\\n"
8946 " float b = 23; \\\n"
8947 " const int ccc = 234; \\\n"
8948 " unsigned dddddddddd = 2345;",
8950 Alignment.AlignEscapedNewlinesLeft = false;
8951 Alignment.ColumnLimit = 30;
8952 verifyFormat("#define A \\\n"
8953 " int aaaa = 12; \\\n"
8954 " float b = 23; \\\n"
8955 " const int ccc = 234; \\\n"
8956 " int dddddddddd = 2345;",
8958 Alignment.ColumnLimit = 80;
8959 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8960 "k = 4, int l = 5,\n"
8962 " const int j = 10;\n"
8963 " otherThing = 1;\n"
8966 verifyFormat("void SomeFunction(int parameter = 0) {\n"
8967 " int const i = 1;\n"
8969 " int big = 10000;\n"
8972 verifyFormat("class C {\n"
8975 " virtual void f() = 0;\n"
8978 verifyFormat("float i = 1;\n"
8979 "if (SomeType t = getSomething()) {\n"
8981 "const unsigned j = 2;\n"
8984 verifyFormat("float j = 7;\n"
8985 "for (int k = 0; k < N; ++k) {\n"
8988 "int big = 10000;\n"
8991 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8992 verifyFormat("float i = 1;\n"
8993 "LooooooooooongType loooooooooooooooooooooongVariable\n"
8994 " = someLooooooooooooooooongFunction();\n"
8997 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8998 verifyFormat("int i = 1;\n"
8999 "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9000 " someLooooooooooooooooongFunction();\n"
9004 Alignment.AlignConsecutiveAssignments = true;
9005 verifyFormat("auto lambda = []() {\n"
9018 Alignment.AlignConsecutiveAssignments = false;
9020 // FIXME: Should align all three declarations
9023 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9024 " loooooooooooooooooooooongParameterB);\n"
9028 // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9029 // We expect declarations and assignments to align, as long as it doesn't
9030 // exceed the column limit, starting a new alignemnt sequence whenever it
9032 Alignment.AlignConsecutiveAssignments = true;
9033 Alignment.ColumnLimit = 30;
9034 verifyFormat("float ii = 1;\n"
9036 "int someVerylongVariable = 1;\n"
9037 "AnotherLongType ll = 123456;\n"
9038 "VeryVeryLongType k = 2;\n"
9041 Alignment.ColumnLimit = 80;
9042 Alignment.AlignConsecutiveAssignments = false;
9045 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
9046 " typename LongType, typename B>\n"
9049 verifyFormat("float a, b = 1;\n"
9053 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
9054 "float b[1][] = {{3.f}};\n",
9056 Alignment.AlignConsecutiveAssignments = true;
9057 verifyFormat("float a, b = 1;\n"
9061 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n"
9062 "float b[1][] = {{3.f}};\n",
9064 Alignment.AlignConsecutiveAssignments = false;
9066 Alignment.ColumnLimit = 30;
9067 Alignment.BinPackParameters = false;
9068 verifyFormat("void foo(float a,\n"
9076 "void bar(ino_t a,\n"
9081 Alignment.BinPackParameters = true;
9082 Alignment.ColumnLimit = 80;
9085 TEST_F(FormatTest, LinuxBraceBreaking) {
9086 FormatStyle LinuxBraceStyle = getLLVMStyle();
9087 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
9088 verifyFormat("namespace a\n"
9101 " void g() { return; }\n"
9108 verifyFormat("enum X {\n"
9112 verifyFormat("struct S {\n"
9120 " MyFavoriteType Value;\n"
9126 TEST_F(FormatTest, MozillaBraceBreaking) {
9127 FormatStyle MozillaBraceStyle = getLLVMStyle();
9128 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9129 verifyFormat("namespace a {\n"
9139 " void g() { return; }\n"
9154 verifyFormat("struct S\n"
9164 " MyFavoriteType Value;\n"
9170 TEST_F(FormatTest, StroustrupBraceBreaking) {
9171 FormatStyle StroustrupBraceStyle = getLLVMStyle();
9172 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9173 verifyFormat("namespace a {\n"
9182 " void g() { return; }\n"
9188 StroustrupBraceStyle);
9190 verifyFormat("void foo()\n"
9199 StroustrupBraceStyle);
9201 verifyFormat("#ifdef _DEBUG\n"
9202 "int foo(int i = 0)\n"
9204 "int foo(int i = 5)\n"
9209 StroustrupBraceStyle);
9211 verifyFormat("void foo() {}\n"
9221 StroustrupBraceStyle);
9223 verifyFormat("void foobar() { int i = 5; }\n"
9227 "void bar() { foobar(); }\n"
9229 StroustrupBraceStyle);
9232 TEST_F(FormatTest, AllmanBraceBreaking) {
9233 FormatStyle AllmanBraceStyle = getLLVMStyle();
9234 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9235 verifyFormat("namespace a\n"
9247 " void g() { return; }\n"
9256 verifyFormat("void f()\n"
9262 " else if (false)\n"
9273 verifyFormat("void f()\n"
9275 " for (int i = 0; i < 10; ++i)\n"
9286 " } while (false)\n"
9290 verifyFormat("void f(int a)\n"
9310 verifyFormat("enum X\n"
9315 verifyFormat("enum X\n"
9321 verifyFormat("@interface BSApplicationController ()\n"
9329 verifyFormat("#ifdef _DEBUG\n"
9330 "int foo(int i = 0)\n"
9332 "int foo(int i = 5)\n"
9339 verifyFormat("void foo() {}\n"
9351 verifyFormat("void foobar() { int i = 5; }\n"
9355 "void bar() { foobar(); }\n"
9359 // This shouldn't affect ObjC blocks..
9360 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9365 verifyFormat("void (^block)(void) = ^{\n"
9370 // .. or dict literals.
9371 verifyFormat("void f()\n"
9373 " [object someMethod:@{ @\"a\" : @\"b\" }];\n"
9376 verifyFormat("int f()\n"
9382 AllmanBraceStyle.ColumnLimit = 19;
9383 verifyFormat("void f() { int i; }", AllmanBraceStyle);
9384 AllmanBraceStyle.ColumnLimit = 18;
9385 verifyFormat("void f()\n"
9390 AllmanBraceStyle.ColumnLimit = 80;
9392 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9393 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9394 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9395 verifyFormat("void f(bool b)\n"
9402 BreakBeforeBraceShortIfs);
9403 verifyFormat("void f(bool b)\n"
9407 BreakBeforeBraceShortIfs);
9408 verifyFormat("void f(bool b)\n"
9415 BreakBeforeBraceShortIfs);
9418 TEST_F(FormatTest, GNUBraceBreaking) {
9419 FormatStyle GNUBraceStyle = getLLVMStyle();
9420 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9421 verifyFormat("namespace a\n"
9437 " void g() { return; }\n"
9442 verifyFormat("void f()\n"
9448 " else if (false)\n"
9459 verifyFormat("void f()\n"
9461 " for (int i = 0; i < 10; ++i)\n"
9477 verifyFormat("void f(int a)\n"
9497 verifyFormat("enum X\n"
9503 verifyFormat("@interface BSApplicationController ()\n"
9511 verifyFormat("#ifdef _DEBUG\n"
9512 "int foo(int i = 0)\n"
9514 "int foo(int i = 5)\n"
9521 verifyFormat("void foo() {}\n"
9533 verifyFormat("void foobar() { int i = 5; }\n"
9537 "void bar() { foobar(); }\n"
9542 TEST_F(FormatTest, WebKitBraceBreaking) {
9543 FormatStyle WebKitBraceStyle = getLLVMStyle();
9544 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9545 verifyFormat("namespace a {\n"
9554 " void g() { return; }\n"
9567 verifyFormat("struct S {\n"
9574 " MyFavoriteType Value;\n"
9580 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9581 verifyFormat("void f() {\n"
9583 " } catch (const Exception &e) {\n"
9589 TEST_F(FormatTest, UnderstandsPragmas) {
9590 verifyFormat("#pragma omp reduction(| : var)");
9591 verifyFormat("#pragma omp reduction(+ : var)");
9593 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9594 "(including parentheses).",
9595 format("#pragma mark Any non-hyphenated or hyphenated string "
9596 "(including parentheses)."));
9599 TEST_F(FormatTest, UnderstandPragmaOption) {
9600 verifyFormat("#pragma option -C -A");
9602 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A"));
9605 #define EXPECT_ALL_STYLES_EQUAL(Styles) \
9606 for (size_t i = 1; i < Styles.size(); ++i) \
9607 EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9608 << " differs from Style #0"
9610 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9611 SmallVector<FormatStyle, 3> Styles;
9614 Styles[0] = getLLVMStyle();
9615 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9616 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9617 EXPECT_ALL_STYLES_EQUAL(Styles);
9619 Styles[0] = getGoogleStyle();
9620 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9621 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9622 EXPECT_ALL_STYLES_EQUAL(Styles);
9624 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9626 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9628 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9629 EXPECT_ALL_STYLES_EQUAL(Styles);
9631 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9632 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9633 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9634 EXPECT_ALL_STYLES_EQUAL(Styles);
9636 Styles[0] = getMozillaStyle();
9637 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9638 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9639 EXPECT_ALL_STYLES_EQUAL(Styles);
9641 Styles[0] = getWebKitStyle();
9642 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9643 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9644 EXPECT_ALL_STYLES_EQUAL(Styles);
9646 Styles[0] = getGNUStyle();
9647 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9648 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9649 EXPECT_ALL_STYLES_EQUAL(Styles);
9651 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9654 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9655 SmallVector<FormatStyle, 8> Styles;
9658 Styles[0] = getGoogleStyle();
9659 Styles[1] = getLLVMStyle();
9660 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9661 EXPECT_ALL_STYLES_EQUAL(Styles);
9664 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9665 Styles[1] = getLLVMStyle();
9666 Styles[1].Language = FormatStyle::LK_JavaScript;
9667 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9669 Styles[2] = getLLVMStyle();
9670 Styles[2].Language = FormatStyle::LK_JavaScript;
9671 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9672 "BasedOnStyle: Google",
9676 Styles[3] = getLLVMStyle();
9677 Styles[3].Language = FormatStyle::LK_JavaScript;
9678 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9679 "Language: JavaScript",
9683 Styles[4] = getLLVMStyle();
9684 Styles[4].Language = FormatStyle::LK_JavaScript;
9685 EXPECT_EQ(0, parseConfiguration("---\n"
9686 "BasedOnStyle: LLVM\n"
9687 "IndentWidth: 123\n"
9689 "BasedOnStyle: Google\n"
9690 "Language: JavaScript",
9693 EXPECT_ALL_STYLES_EQUAL(Styles);
9696 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \
9697 Style.FIELD = false; \
9698 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \
9699 EXPECT_TRUE(Style.FIELD); \
9700 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \
9701 EXPECT_FALSE(Style.FIELD);
9703 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9705 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \
9706 Style.STRUCT.FIELD = false; \
9708 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \
9710 EXPECT_TRUE(Style.STRUCT.FIELD); \
9712 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \
9714 EXPECT_FALSE(Style.STRUCT.FIELD);
9716 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \
9717 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
9719 #define CHECK_PARSE(TEXT, FIELD, VALUE) \
9720 EXPECT_NE(VALUE, Style.FIELD); \
9721 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \
9722 EXPECT_EQ(VALUE, Style.FIELD)
9724 TEST_F(FormatTest, ParsesConfigurationBools) {
9725 FormatStyle Style = {};
9726 Style.Language = FormatStyle::LK_Cpp;
9727 CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
9728 CHECK_PARSE_BOOL(AlignOperands);
9729 CHECK_PARSE_BOOL(AlignTrailingComments);
9730 CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9731 CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
9732 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9733 CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9734 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9735 CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9736 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9737 CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9738 CHECK_PARSE_BOOL(BinPackArguments);
9739 CHECK_PARSE_BOOL(BinPackParameters);
9740 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9741 CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
9742 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9743 CHECK_PARSE_BOOL(DerivePointerAlignment);
9744 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9745 CHECK_PARSE_BOOL(DisableFormat);
9746 CHECK_PARSE_BOOL(IndentCaseLabels);
9747 CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9748 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9749 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9750 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9751 CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9752 CHECK_PARSE_BOOL(ReflowComments);
9753 CHECK_PARSE_BOOL(SortIncludes);
9754 CHECK_PARSE_BOOL(SpacesInParentheses);
9755 CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9756 CHECK_PARSE_BOOL(SpacesInAngles);
9757 CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9758 CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9759 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9760 CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9761 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9763 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
9764 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
9765 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
9766 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
9767 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
9768 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
9769 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
9770 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
9771 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
9772 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
9773 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
9776 #undef CHECK_PARSE_BOOL
9778 TEST_F(FormatTest, ParsesConfiguration) {
9779 FormatStyle Style = {};
9780 Style.Language = FormatStyle::LK_Cpp;
9781 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9782 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9783 ConstructorInitializerIndentWidth, 1234u);
9784 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9785 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9786 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9787 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9788 PenaltyBreakBeforeFirstCallParameter, 1234u);
9789 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9790 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9791 PenaltyReturnTypeOnItsOwnLine, 1234u);
9792 CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9793 SpacesBeforeTrailingComments, 1234u);
9794 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9795 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9797 Style.PointerAlignment = FormatStyle::PAS_Middle;
9798 CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9799 FormatStyle::PAS_Left);
9800 CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9801 FormatStyle::PAS_Right);
9802 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9803 FormatStyle::PAS_Middle);
9804 // For backward compatibility:
9805 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9806 FormatStyle::PAS_Left);
9807 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9808 FormatStyle::PAS_Right);
9809 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9810 FormatStyle::PAS_Middle);
9812 Style.Standard = FormatStyle::LS_Auto;
9813 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9814 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9815 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9816 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9817 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9819 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9820 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9821 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9822 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9823 FormatStyle::BOS_None);
9824 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9825 FormatStyle::BOS_All);
9826 // For backward compatibility:
9827 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9828 FormatStyle::BOS_None);
9829 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9830 FormatStyle::BOS_All);
9832 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9833 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
9834 FormatStyle::BAS_Align);
9835 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
9836 FormatStyle::BAS_DontAlign);
9837 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
9838 FormatStyle::BAS_AlwaysBreak);
9839 // For backward compatibility:
9840 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
9841 FormatStyle::BAS_DontAlign);
9842 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
9843 FormatStyle::BAS_Align);
9845 Style.UseTab = FormatStyle::UT_ForIndentation;
9846 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9847 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9848 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9849 // For backward compatibility:
9850 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9851 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9853 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9854 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9855 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9856 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9857 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9858 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9859 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9860 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9861 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9862 // For backward compatibility:
9863 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9864 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9865 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9866 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9868 Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9869 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9870 FormatStyle::SBPO_Never);
9871 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9872 FormatStyle::SBPO_Always);
9873 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9874 FormatStyle::SBPO_ControlStatements);
9875 // For backward compatibility:
9876 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9877 FormatStyle::SBPO_Never);
9878 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9879 FormatStyle::SBPO_ControlStatements);
9881 Style.ColumnLimit = 123;
9882 FormatStyle BaseStyle = getLLVMStyle();
9883 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9884 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9886 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9887 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9888 FormatStyle::BS_Attach);
9889 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9890 FormatStyle::BS_Linux);
9891 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
9892 FormatStyle::BS_Mozilla);
9893 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9894 FormatStyle::BS_Stroustrup);
9895 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9896 FormatStyle::BS_Allman);
9897 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9898 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
9899 FormatStyle::BS_WebKit);
9900 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
9901 FormatStyle::BS_Custom);
9903 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9904 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
9905 FormatStyle::RTBS_None);
9906 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
9907 FormatStyle::RTBS_All);
9908 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
9909 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
9910 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
9911 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
9912 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
9913 AlwaysBreakAfterReturnType,
9914 FormatStyle::RTBS_TopLevelDefinitions);
9916 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
9917 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
9918 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
9919 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
9920 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
9921 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
9922 AlwaysBreakAfterDefinitionReturnType,
9923 FormatStyle::DRTBS_TopLevel);
9925 Style.NamespaceIndentation = FormatStyle::NI_All;
9926 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9927 FormatStyle::NI_None);
9928 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9929 FormatStyle::NI_Inner);
9930 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9931 FormatStyle::NI_All);
9933 // FIXME: This is required because parsing a configuration simply overwrites
9934 // the first N elements of the list instead of resetting it.
9935 Style.ForEachMacros.clear();
9936 std::vector<std::string> BoostForeach;
9937 BoostForeach.push_back("BOOST_FOREACH");
9938 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9939 std::vector<std::string> BoostAndQForeach;
9940 BoostAndQForeach.push_back("BOOST_FOREACH");
9941 BoostAndQForeach.push_back("Q_FOREACH");
9942 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9945 Style.IncludeCategories.clear();
9946 std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
9948 CHECK_PARSE("IncludeCategories:\n"
9949 " - Regex: abc/.*\n"
9953 IncludeCategories, ExpectedCategories);
9956 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9957 FormatStyle Style = {};
9958 Style.Language = FormatStyle::LK_Cpp;
9959 CHECK_PARSE("Language: Cpp\n"
9962 EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9965 ParseError::Unsuitable);
9966 EXPECT_EQ(12u, Style.IndentWidth);
9967 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9968 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9970 Style.Language = FormatStyle::LK_JavaScript;
9971 CHECK_PARSE("Language: JavaScript\n"
9974 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9975 EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9978 ParseError::Unsuitable);
9979 EXPECT_EQ(23u, Style.IndentWidth);
9980 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9981 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9983 CHECK_PARSE("BasedOnStyle: LLVM\n"
9988 "Language: JavaScript\n"
9996 Style.Language = FormatStyle::LK_Cpp;
9998 "Language: JavaScript\n"
10002 "IndentWidth: 34\n"
10005 CHECK_PARSE("---\n"
10006 "IndentWidth: 78\n"
10008 "Language: JavaScript\n"
10009 "IndentWidth: 56\n"
10013 Style.ColumnLimit = 123;
10014 Style.IndentWidth = 234;
10015 Style.BreakBeforeBraces = FormatStyle::BS_Linux;
10016 Style.TabWidth = 345;
10017 EXPECT_FALSE(parseConfiguration("---\n"
10018 "IndentWidth: 456\n"
10019 "BreakBeforeBraces: Allman\n"
10021 "Language: JavaScript\n"
10022 "IndentWidth: 111\n"
10026 "BreakBeforeBraces: Stroustrup\n"
10030 EXPECT_EQ(123u, Style.ColumnLimit);
10031 EXPECT_EQ(456u, Style.IndentWidth);
10032 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
10033 EXPECT_EQ(789u, Style.TabWidth);
10035 EXPECT_EQ(parseConfiguration("---\n"
10036 "Language: JavaScript\n"
10037 "IndentWidth: 56\n"
10039 "IndentWidth: 78\n"
10042 ParseError::Error);
10043 EXPECT_EQ(parseConfiguration("---\n"
10044 "Language: JavaScript\n"
10045 "IndentWidth: 56\n"
10047 "Language: JavaScript\n"
10048 "IndentWidth: 78\n"
10051 ParseError::Error);
10053 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10058 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
10059 FormatStyle Style = {};
10060 Style.Language = FormatStyle::LK_JavaScript;
10061 Style.BreakBeforeTernaryOperators = true;
10062 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
10063 EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10065 Style.BreakBeforeTernaryOperators = true;
10066 EXPECT_EQ(0, parseConfiguration("---\n"
10067 "BasedOnStyle: Google\n"
10069 "Language: JavaScript\n"
10070 "IndentWidth: 76\n"
10074 EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10075 EXPECT_EQ(76u, Style.IndentWidth);
10076 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10079 TEST_F(FormatTest, ConfigurationRoundTripTest) {
10080 FormatStyle Style = getLLVMStyle();
10081 std::string YAML = configurationAsText(Style);
10082 FormatStyle ParsedStyle = {};
10083 ParsedStyle.Language = FormatStyle::LK_Cpp;
10084 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
10085 EXPECT_EQ(Style, ParsedStyle);
10088 TEST_F(FormatTest, WorksFor8bitEncodings) {
10089 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
10090 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
10091 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
10092 "\"\xef\xee\xf0\xf3...\"",
10093 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
10094 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
10095 "\xef\xee\xf0\xf3...\"",
10096 getLLVMStyleWithColumns(12)));
10099 TEST_F(FormatTest, HandlesUTF8BOM) {
10100 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
10101 EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
10102 format("\xef\xbb\xbf#include <iostream>"));
10103 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
10104 format("\xef\xbb\xbf\n#include <iostream>"));
10107 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
10108 #if !defined(_MSC_VER)
10110 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
10111 verifyFormat("\"Однажды в студёную зимнюю пору...\"",
10112 getLLVMStyleWithColumns(35));
10113 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
10114 getLLVMStyleWithColumns(31));
10115 verifyFormat("// Однажды в студёную зимнюю пору...",
10116 getLLVMStyleWithColumns(36));
10117 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
10118 verifyFormat("/* Однажды в студёную зимнюю пору... */",
10119 getLLVMStyleWithColumns(39));
10120 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
10121 getLLVMStyleWithColumns(35));
10124 TEST_F(FormatTest, SplitsUTF8Strings) {
10125 // Non-printable characters' width is currently considered to be the length in
10126 // bytes in UTF8. The characters can be displayed in very different manner
10127 // (zero-width, single width with a substitution glyph, expanded to their code
10128 // (e.g. "<8d>"), so there's no single correct way to handle them.
10129 EXPECT_EQ("\"aaaaÄ\"\n"
10131 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10132 EXPECT_EQ("\"aaaaaaaÄ\"\n"
10134 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10135 EXPECT_EQ("\"Однажды, в \"\n"
10139 format("\"Однажды, в студёную зимнюю пору,\"",
10140 getLLVMStyleWithColumns(13)));
10146 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
10147 EXPECT_EQ("\"一\t二 \"\n"
10152 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
10153 getLLVMStyleWithColumns(11)));
10155 // UTF8 character in an escape sequence.
10156 EXPECT_EQ("\"aaaaaa\"\n"
10158 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
10161 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10162 EXPECT_EQ("const char *sssss =\n"
10165 format("const char *sssss = \"一二三四五六七八\\\n"
10167 getLLVMStyleWithColumns(30)));
10170 TEST_F(FormatTest, SplitsUTF8LineComments) {
10171 EXPECT_EQ("// aaaaÄ\xc2\x8d",
10172 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10173 EXPECT_EQ("// Я из лесу\n"
10177 format("// Я из лесу вышел; был сильный мороз.",
10178 getLLVMStyleWithColumns(13)));
10179 EXPECT_EQ("// 一二三\n"
10183 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)));
10186 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10187 EXPECT_EQ("/* Гляжу,\n"
10195 format("/* Гляжу, поднимается медленно в гору\n"
10196 " * Лошадка, везущая хворосту воз. */",
10197 getLLVMStyleWithColumns(13)));
10203 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)));
10204 EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
10207 format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
10212 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10213 FormatStyle Style = getLLVMStyle();
10215 Style.ConstructorInitializerIndentWidth = 4;
10217 "SomeClass::Constructor()\n"
10218 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10219 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10222 Style.ConstructorInitializerIndentWidth = 2;
10224 "SomeClass::Constructor()\n"
10225 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10226 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10229 Style.ConstructorInitializerIndentWidth = 0;
10231 "SomeClass::Constructor()\n"
10232 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10233 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10237 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10238 FormatStyle Style = getLLVMStyle();
10239 Style.BreakConstructorInitializersBeforeComma = true;
10240 Style.ConstructorInitializerIndentWidth = 4;
10241 verifyFormat("SomeClass::Constructor()\n"
10246 verifyFormat("SomeClass::Constructor()\n"
10250 Style.ColumnLimit = 0;
10251 verifyFormat("SomeClass::Constructor()\n"
10254 verifyFormat("SomeClass::Constructor()\n"
10259 verifyFormat("SomeClass::Constructor()\n"
10266 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10267 verifyFormat("SomeClass::Constructor()\n"
10272 verifyFormat("SomeClass::Constructor()\n"
10276 Style.ColumnLimit = 80;
10277 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10278 Style.ConstructorInitializerIndentWidth = 2;
10279 verifyFormat("SomeClass::Constructor()\n"
10285 Style.ConstructorInitializerIndentWidth = 0;
10286 verifyFormat("SomeClass::Constructor()\n"
10292 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10293 Style.ConstructorInitializerIndentWidth = 4;
10294 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10296 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10299 "SomeClass::Constructor()\n"
10300 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10302 Style.ConstructorInitializerIndentWidth = 4;
10303 Style.ColumnLimit = 60;
10304 verifyFormat("SomeClass::Constructor()\n"
10305 " : aaaaaaaa(aaaaaaaa)\n"
10306 " , aaaaaaaa(aaaaaaaa)\n"
10307 " , aaaaaaaa(aaaaaaaa) {}",
10311 TEST_F(FormatTest, Destructors) {
10312 verifyFormat("void F(int &i) { i.~int(); }");
10313 verifyFormat("void F(int &i) { i->~int(); }");
10316 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10317 FormatStyle Style = getWebKitStyle();
10319 // Don't indent in outer namespaces.
10320 verifyFormat("namespace outer {\n"
10322 "namespace inner {\n"
10324 "} // namespace inner\n"
10325 "} // namespace outer\n"
10326 "namespace other_outer {\n"
10331 // Don't indent case labels.
10332 verifyFormat("switch (variable) {\n"
10335 " doSomething();\n"
10342 // Wrap before binary operators.
10343 EXPECT_EQ("void f()\n"
10345 " if (aaaaaaaaaaaaaaaa\n"
10346 " && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10347 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10350 format("void f() {\n"
10351 "if (aaaaaaaaaaaaaaaa\n"
10352 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10353 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10358 // Allow functions on a single line.
10359 verifyFormat("void f() { return; }", Style);
10361 // Constructor initializers are formatted one per line with the "," on the
10363 verifyFormat("Constructor()\n"
10364 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10365 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10366 " aaaaaaaaaaaaaa)\n"
10367 " , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10371 verifyFormat("SomeClass::Constructor()\n"
10376 EXPECT_EQ("SomeClass::Constructor()\n"
10380 format("SomeClass::Constructor():a(a){}", Style));
10381 verifyFormat("SomeClass::Constructor()\n"
10388 verifyFormat("SomeClass::Constructor()\n"
10396 // Access specifiers should be aligned left.
10397 verifyFormat("class C {\n"
10403 // Do not align comments.
10404 verifyFormat("int a; // Do not\n"
10405 "double b; // align comments.",
10408 // Do not align operands.
10409 EXPECT_EQ("ASSERT(aaaa\n"
10411 format("ASSERT ( aaaa\n||bbbb);", Style));
10413 // Accept input's line breaks.
10414 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10415 " || bbbbbbbbbbbbbbb) {\n"
10418 format("if (aaaaaaaaaaaaaaa\n"
10419 "|| bbbbbbbbbbbbbbb) { i++; }",
10421 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10424 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10426 // Don't automatically break all macro definitions (llvm.org/PR17842).
10427 verifyFormat("#define aNumber 10", Style);
10428 // However, generally keep the line breaks that the user authored.
10429 EXPECT_EQ("#define aNumber \\\n"
10431 format("#define aNumber \\\n"
10435 // Keep empty and one-element array literals on a single line.
10436 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10437 " copyItems:YES];",
10438 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10441 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10442 " copyItems:YES];",
10443 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10444 " copyItems:YES];",
10446 // FIXME: This does not seem right, there should be more indentation before
10447 // the array literal's entries. Nested blocks have the same problem.
10448 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10452 " copyItems:YES];",
10453 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10457 " copyItems:YES];",
10460 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10461 " copyItems:YES];",
10462 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10463 " copyItems:YES];",
10466 verifyFormat("[self.a b:c c:d];", Style);
10467 EXPECT_EQ("[self.a b:c\n"
10469 format("[self.a b:c\n"
10474 TEST_F(FormatTest, FormatsLambdas) {
10475 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10476 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10477 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10478 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10479 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10480 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10481 verifyFormat("void f() {\n"
10482 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10484 verifyFormat("void f() {\n"
10485 " other(x.begin(), //\n"
10487 " [&](int, int) { return 1; });\n"
10489 verifyFormat("SomeFunction([]() { // A cool function...\n"
10492 EXPECT_EQ("SomeFunction([]() {\n"
10496 format("SomeFunction([](){\n"
10500 verifyFormat("void f() {\n"
10501 " SomeFunction([](decltype(x), A *a) {});\n"
10503 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10504 " [](const aaaaaaaaaa &a) { return a; });");
10505 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10506 " SomeOtherFunctioooooooooooooooooooooooooon();\n"
10508 verifyFormat("Constructor()\n"
10509 " : Field([] { // comment\n"
10512 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10513 " return some_parameter.size();\n"
10515 verifyFormat("int i = aaaaaa ? 1 //\n"
10519 verifyFormat("llvm::errs() << \"number of twos is \"\n"
10520 " << std::count_if(v.begin(), v.end(), [](int x) {\n"
10521 " return x == 2; // force break\n"
10523 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
10524 " int iiiiiiiiiiii) {\n"
10525 " return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
10527 getLLVMStyleWithColumns(60));
10528 verifyFormat("SomeFunction({[&] {\n"
10534 verifyFormat("SomeFunction({[&] {\n"
10537 verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10538 " [&]() { return true; },\n"
10539 " aaaaa aaaaaaaaa);");
10541 // Lambdas with return types.
10542 verifyFormat("int c = []() -> int { return 2; }();\n");
10543 verifyFormat("int c = []() -> int * { return 2; }();\n");
10544 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10545 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10546 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10547 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10548 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10549 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10550 verifyFormat("[a, a]() -> a<1> {};");
10551 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10552 " int j) -> int {\n"
10553 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10556 "aaaaaaaaaaaaaaaaaaaaaa(\n"
10557 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10558 " return aaaaaaaaaaaaaaaaa;\n"
10560 getLLVMStyleWithColumns(70));
10562 // Multiple lambdas in the same parentheses change indentation rules.
10563 verifyFormat("SomeFunction(\n"
10573 // More complex introducers.
10574 verifyFormat("return [i, args...] {};");
10577 verifyFormat("constexpr char hello[]{\"hello\"};");
10578 verifyFormat("double &operator[](int i) { return 0; }\n"
10580 verifyFormat("std::unique_ptr<int[]> foo() {}");
10581 verifyFormat("int i = a[a][a]->f();");
10582 verifyFormat("int i = (*b)[a]->f();");
10584 // Other corner cases.
10585 verifyFormat("void f() {\n"
10586 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
10590 // Lambdas created through weird macros.
10591 verifyFormat("void f() {\n"
10592 " MACRO((const AA &a) { return 1; });\n"
10595 verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
10600 verifyFormat("auto lambda = []() {\n"
10609 TEST_F(FormatTest, FormatsBlocks) {
10610 FormatStyle ShortBlocks = getLLVMStyle();
10611 ShortBlocks.AllowShortBlocksOnASingleLine = true;
10612 verifyFormat("int (^Block)(int, int);", ShortBlocks);
10613 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10614 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10615 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10616 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10617 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
10619 verifyFormat("foo(^{ bar(); });", ShortBlocks);
10620 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
10621 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
10623 verifyFormat("[operation setCompletionBlock:^{\n"
10624 " [self onOperationDone];\n"
10626 verifyFormat("int i = {[operation setCompletionBlock:^{\n"
10627 " [self onOperationDone];\n"
10629 verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
10632 verifyFormat("int a = [operation block:^int(int *i) {\n"
10635 verifyFormat("[myObject doSomethingWith:arg1\n"
10636 " aaa:^int(int *a) {\n"
10639 " bbb:f(a * bbbbbbbb)];");
10641 verifyFormat("[operation setCompletionBlock:^{\n"
10642 " [self.delegate newDataAvailable];\n"
10644 getLLVMStyleWithColumns(60));
10645 verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
10646 " NSString *path = [self sessionFilePath];\n"
10651 verifyFormat("[[SessionService sharedService]\n"
10652 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10654 " [self windowDidLoad:window];\n"
10656 " [self errorLoadingWindow];\n"
10659 verifyFormat("void (^largeBlock)(void) = ^{\n"
10662 getLLVMStyleWithColumns(40));
10663 verifyFormat("[[SessionService sharedService]\n"
10664 " loadWindowWithCompletionBlock: //\n"
10665 " ^(SessionWindow *window) {\n"
10667 " [self windowDidLoad:window];\n"
10669 " [self errorLoadingWindow];\n"
10672 getLLVMStyleWithColumns(60));
10673 verifyFormat("[myObject doSomethingWith:arg1\n"
10674 " firstBlock:^(Foo *a) {\n"
10678 " secondBlock:^(Bar *b) {\n"
10682 " thirdBlock:^Foo(Bar *b) {\n"
10686 verifyFormat("[myObject doSomethingWith:arg1\n"
10688 " secondBlock:^(Bar *b) {\n"
10693 verifyFormat("f(^{\n"
10694 " @autoreleasepool {\n"
10700 verifyFormat("Block b = ^int *(A *a, B *b) {}");
10702 FormatStyle FourIndent = getLLVMStyle();
10703 FourIndent.ObjCBlockIndentWidth = 4;
10704 verifyFormat("[operation setCompletionBlock:^{\n"
10705 " [self onOperationDone];\n"
10710 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
10711 FormatStyle ZeroColumn = getLLVMStyle();
10712 ZeroColumn.ColumnLimit = 0;
10714 verifyFormat("[[SessionService sharedService] "
10715 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10717 " [self windowDidLoad:window];\n"
10719 " [self errorLoadingWindow];\n"
10723 EXPECT_EQ("[[SessionService sharedService]\n"
10724 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10726 " [self windowDidLoad:window];\n"
10728 " [self errorLoadingWindow];\n"
10731 format("[[SessionService sharedService]\n"
10732 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10734 " [self windowDidLoad:window];\n"
10736 " [self errorLoadingWindow];\n"
10740 verifyFormat("[myObject doSomethingWith:arg1\n"
10741 " firstBlock:^(Foo *a) {\n"
10745 " secondBlock:^(Bar *b) {\n"
10749 " thirdBlock:^Foo(Bar *b) {\n"
10754 verifyFormat("f(^{\n"
10755 " @autoreleasepool {\n"
10762 verifyFormat("void (^largeBlock)(void) = ^{\n"
10767 ZeroColumn.AllowShortBlocksOnASingleLine = true;
10768 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10769 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
10770 ZeroColumn.AllowShortBlocksOnASingleLine = false;
10771 EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10774 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
10777 TEST_F(FormatTest, SupportsCRLF) {
10778 EXPECT_EQ("int a;\r\n"
10781 format("int a;\r\n"
10785 EXPECT_EQ("int a;\r\n"
10788 format("int a;\r\n"
10792 EXPECT_EQ("int a;\n"
10795 format("int a;\r\n"
10799 EXPECT_EQ("\"aaaaaaa \"\r\n"
10800 "\"bbbbbbb\";\r\n",
10801 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10802 EXPECT_EQ("#define A \\\r\n"
10806 format("#define A \\\r\n"
10809 getGoogleStyle()));
10812 "multi line block comments\r\n"
10813 "should not introduce\r\n"
10814 "an extra carriage return\r\n"
10817 "multi line block comments\r\n"
10818 "should not introduce\r\n"
10819 "an extra carriage return\r\n"
10823 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10824 verifyFormat("MY_CLASS(C) {\n"
10830 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10831 FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10832 TwoIndent.ContinuationIndentWidth = 2;
10834 EXPECT_EQ("int i =\n"
10837 format("int i = longFunction(arg);", TwoIndent));
10839 FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10840 SixIndent.ContinuationIndentWidth = 6;
10842 EXPECT_EQ("int i =\n"
10845 format("int i = longFunction(arg);", SixIndent));
10848 TEST_F(FormatTest, SpacesInAngles) {
10849 FormatStyle Spaces = getLLVMStyle();
10850 Spaces.SpacesInAngles = true;
10852 verifyFormat("static_cast< int >(arg);", Spaces);
10853 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10854 verifyFormat("f< int, float >();", Spaces);
10855 verifyFormat("template <> g() {}", Spaces);
10856 verifyFormat("template < std::vector< int > > f() {}", Spaces);
10857 verifyFormat("std::function< void(int, int) > fct;", Spaces);
10858 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
10861 Spaces.Standard = FormatStyle::LS_Cpp03;
10862 Spaces.SpacesInAngles = true;
10863 verifyFormat("A< A< int > >();", Spaces);
10865 Spaces.SpacesInAngles = false;
10866 verifyFormat("A<A<int> >();", Spaces);
10868 Spaces.Standard = FormatStyle::LS_Cpp11;
10869 Spaces.SpacesInAngles = true;
10870 verifyFormat("A< A< int > >();", Spaces);
10872 Spaces.SpacesInAngles = false;
10873 verifyFormat("A<A<int>>();", Spaces);
10876 TEST_F(FormatTest, TripleAngleBrackets) {
10877 verifyFormat("f<<<1, 1>>>();");
10878 verifyFormat("f<<<1, 1, 1, s>>>();");
10879 verifyFormat("f<<<a, b, c, d>>>();");
10880 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10881 verifyFormat("f<param><<<1, 1>>>();");
10882 verifyFormat("f<1><<<1, 1>>>();");
10883 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10884 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10885 "aaaaaaaaaaa<<<\n 1, 1>>>();");
10888 TEST_F(FormatTest, MergeLessLessAtEnd) {
10889 verifyFormat("<<");
10890 EXPECT_EQ("< < <", format("\\\n<<<"));
10891 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10892 "aaallvm::outs() <<");
10893 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10894 "aaaallvm::outs()\n <<");
10897 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10898 std::string code = "#if A\n"
10908 EXPECT_EQ(code, format(code));
10911 TEST_F(FormatTest, HandleConflictMarkers) {
10912 // Git/SVN conflict markers.
10913 EXPECT_EQ("int a;\n"
10915 " callme(some(parameter1,\n"
10916 "<<<<<<< text by the vcs\n"
10918 "||||||| text by the vcs\n"
10921 "======= text by the vcs\n"
10922 " parameter2, parameter3),\n"
10923 ">>>>>>> text by the vcs\n"
10924 " otherparameter);\n",
10927 " callme(some(parameter1,\n"
10928 "<<<<<<< text by the vcs\n"
10930 "||||||| text by the vcs\n"
10933 "======= text by the vcs\n"
10936 ">>>>>>> text by the vcs\n"
10937 " otherparameter);\n"));
10939 // Perforce markers.
10940 EXPECT_EQ("void f() {\n"
10942 ">>>> text by the vcs\n"
10944 "==== text by the vcs\n"
10946 "==== text by the vcs\n"
10948 "<<<< text by the vcs\n"
10950 format("void f() {\n"
10952 ">>>> text by the vcs\n"
10954 "==== text by the vcs\n"
10956 "==== text by the vcs\n"
10958 "<<<< text by the vcs\n"
10959 " parameter);\n"));
10961 EXPECT_EQ("<<<<<<<\n"
10970 EXPECT_EQ("<<<<<<<\n"
10981 // FIXME: Handle parsing of macros around conflict markers correctly:
10982 EXPECT_EQ("#define Macro \\\n"
10991 format("#define Macro \\\n"
11003 TEST_F(FormatTest, DisableRegions) {
11004 EXPECT_EQ("int i;\n"
11005 "// clang-format off\n"
11007 "// clang-format on\n"
11010 " // clang-format off\n"
11012 " // clang-format on\n"
11014 EXPECT_EQ("int i;\n"
11015 "/* clang-format off */\n"
11017 "/* clang-format on */\n"
11020 " /* clang-format off */\n"
11022 " /* clang-format on */\n"
11026 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
11028 verifyNoCrash("#define a\\\n /**/}");
11031 TEST_F(FormatTest, FormatsTableGenCode) {
11032 FormatStyle Style = getLLVMStyle();
11033 Style.Language = FormatStyle::LK_TableGen;
11034 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
11038 } // end namespace format
11039 } // end namespace clang