diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 7e79b055066d..79bf1012521e 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -259,7 +259,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.ColumnLimit = 80; LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; LLVMStyle.ConstructorInitializerIndentWidth = 4; - LLVMStyle.Cpp11BracedListStyle = false; + LLVMStyle.Cpp11BracedListStyle = true; LLVMStyle.DerivePointerBinding = false; LLVMStyle.ExperimentalAutoDetectBinPacking = false; LLVMStyle.IndentCaseLabels = false; @@ -272,7 +272,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.ObjCSpaceBeforeProtocolList = true; LLVMStyle.PointerBindsToType = false; LLVMStyle.SpacesBeforeTrailingComments = 1; - LLVMStyle.Standard = FormatStyle::LS_Cpp03; + LLVMStyle.Standard = FormatStyle::LS_Cpp11; LLVMStyle.UseTab = FormatStyle::UT_Never; LLVMStyle.SpacesInParentheses = false; LLVMStyle.SpaceInEmptyParentheses = false; @@ -305,7 +305,6 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { GoogleStyle.AlwaysBreakBeforeMultilineStrings = true; GoogleStyle.AlwaysBreakTemplateDeclarations = true; GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; - GoogleStyle.Cpp11BracedListStyle = true; GoogleStyle.DerivePointerBinding = true; GoogleStyle.IndentCaseLabels = true; GoogleStyle.IndentFunctionDeclarationAfterType = true; @@ -343,6 +342,7 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) { FormatStyle getMozillaStyle() { FormatStyle MozillaStyle = getLLVMStyle(); MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false; + MozillaStyle.Cpp11BracedListStyle = false; MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; MozillaStyle.DerivePointerBinding = true; MozillaStyle.IndentCaseLabels = true; @@ -350,6 +350,7 @@ FormatStyle getMozillaStyle() { MozillaStyle.ObjCSpaceBeforeProtocolList = false; MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200; MozillaStyle.PointerBindsToType = true; + MozillaStyle.Standard = FormatStyle::LS_Cpp03; return MozillaStyle; } @@ -360,11 +361,13 @@ FormatStyle getWebKitStyle() { Style.BreakBeforeBinaryOperators = true; Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; Style.BreakConstructorInitializersBeforeComma = true; + Style.Cpp11BracedListStyle = false; Style.ColumnLimit = 0; Style.IndentWidth = 4; Style.NamespaceIndentation = FormatStyle::NI_Inner; Style.ObjCSpaceAfterProperty = true; Style.PointerBindsToType = true; + Style.Standard = FormatStyle::LS_Cpp03; return Style; } @@ -373,8 +376,10 @@ FormatStyle getGNUStyle() { Style.BreakBeforeBinaryOperators = true; Style.BreakBeforeBraces = FormatStyle::BS_GNU; Style.BreakBeforeTernaryOperators = true; + Style.Cpp11BracedListStyle = false; Style.ColumnLimit = 79; Style.SpaceBeforeParens = FormatStyle::SBPO_Always; + Style.Standard = FormatStyle::LS_Cpp03; return Style; } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 983c455c8cc6..a86776cff9c8 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -108,7 +108,7 @@ TEST_F(FormatTest, FormatsNestedCall) { } TEST_F(FormatTest, NestedNameSpecifiers) { - verifyFormat("vector< ::Type> v;"); + verifyFormat("vector<::Type> v;"); verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); } @@ -257,7 +257,7 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { " f();\n" "}", AllowsMergedIf); - verifyFormat("if (a) { /* Never merge this */\n" + verifyFormat("if (a) {/* Never merge this */\n" " f();\n" "}", AllowsMergedIf); @@ -700,11 +700,10 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) { verifyGoogleFormat("#endif // HEADER_GUARD"); verifyFormat("const char *test[] = {\n" - " // A\n" - " \"aaaa\",\n" - " // B\n" - " \"aaaaa\",\n" - "};"); + " // A\n" + " \"aaaa\",\n" + " // B\n" + " \"aaaaa\"};"); verifyGoogleFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment"); @@ -1327,21 +1326,21 @@ TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { TEST_F(FormatTest, CommentsInStaticInitializers) { EXPECT_EQ( - "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n" - " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" - " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaa, // comment\n" - " aaaaaaaaaaaaaaaaaaaa };", + "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n" + " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" + " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaa, // comment\n" + " aaaaaaaaaaaaaaaaaaaa};", format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" " aaaaaaaaaaaaaaaaaaaa , // comment\n" " aaaaaaaaaaaaaaaaaaaa };")); - verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" - " bbbbbbbbbbb, ccccccccccc };"); - verifyFormat("static SomeType type = { aaaaaaaaaaa,\n" - " // comment for bb....\n" - " bbbbbbbbbbb, ccccccccccc };"); + verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" + " bbbbbbbbbbb, ccccccccccc};"); + verifyFormat("static SomeType type = {aaaaaaaaaaa,\n" + " // comment for bb....\n" + " bbbbbbbbbbb, ccccccccccc};"); verifyGoogleFormat( "static SomeType type = {aaaaaaaaaaa, // comment for aa...\n" " bbbbbbbbbbb, ccccccccccc};"); @@ -1349,23 +1348,22 @@ TEST_F(FormatTest, CommentsInStaticInitializers) { " // comment for bb....\n" " bbbbbbbbbbb, ccccccccccc};"); - verifyFormat("S s = { { a, b, c }, // Group #1\n" - " { d, e, f }, // Group #2\n" - " { g, h, i } }; // Group #3"); - verifyFormat("S s = { { // Group #1\n" - " a, b, c },\n" - " { // Group #2\n" - " d, e, f },\n" - " { // Group #3\n" - " g, h, i } };"); + verifyFormat("S s = {{a, b, c}, // Group #1\n" + " {d, e, f}, // Group #2\n" + " {g, h, i}}; // Group #3"); + verifyFormat("S s = {{// Group #1\n" + " a, b, c},\n" + " {// Group #2\n" + " d, e, f},\n" + " {// Group #3\n" + " g, h, i}};"); EXPECT_EQ("S s = {\n" - " // Some comment\n" - " a,\n" + " // Some comment\n" + " a,\n" "\n" - " // Comment after empty line\n" - " b\n" - "}", + " // Comment after empty line\n" + " b}", format("S s = {\n" " // Some comment\n" " a,\n" @@ -1374,12 +1372,11 @@ TEST_F(FormatTest, CommentsInStaticInitializers) { " b\n" "}")); EXPECT_EQ("S s = {\n" - " /* Some comment */\n" - " a,\n" + " /* Some comment */\n" + " a,\n" "\n" - " /* Comment after empty line */\n" - " b\n" - "}", + " /* Comment after empty line */\n" + " b}", format("S s = {\n" " /* Some comment */\n" " a,\n" @@ -1388,10 +1385,9 @@ TEST_F(FormatTest, CommentsInStaticInitializers) { " b\n" "}")); verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" - " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" - " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" - " 0x00, 0x00, 0x00, 0x00 // comment\n" - "};"); + " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" + " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" + " 0x00, 0x00, 0x00, 0x00}; // comment\n"); } TEST_F(FormatTest, IgnoresIf0Contents) { @@ -1873,86 +1869,74 @@ TEST_F(FormatTest, FormatObjCTryCatch) { } TEST_F(FormatTest, StaticInitializers) { - verifyFormat("static SomeClass SC = { 1, 'a' };"); + verifyFormat("static SomeClass SC = {1, 'a'};"); verifyFormat( "static SomeClass WithALoooooooooooooooooooongName = {\n" - " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" - "};"); + " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); // Here, everything other than the "}" would fit on a line. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" - " 100000000000000000000000\n" - "};"); - EXPECT_EQ("S s = { a, b };", format("S s = {\n" - " a,\n" - "\n" - " b\n" - "};")); + " 10000000000000000000000000};"); + EXPECT_EQ("S s = {a, b};", format("S s = {\n" + " a,\n" + "\n" + " b\n" + "};")); // FIXME: This would fit into the column limit if we'd fit "{ {" on the first // line. However, the formatting looks a bit off and this probably doesn't // happen often in practice. verifyFormat("static int Variable[1] = {\n" - " { 1000000000000000000000000000000000000 }\n" - "};", + " {1000000000000000000000000000000000000}};", getLLVMStyleWithColumns(40)); } TEST_F(FormatTest, DesignatedInitializers) { - verifyFormat("const struct A a = { .a = 1, .b = 2 };"); - verifyFormat("const struct A a = { .aaaaaaaaaa = 1,\n" - " .bbbbbbbbbb = 2,\n" - " .cccccccccc = 3,\n" - " .dddddddddd = 4,\n" - " .eeeeeeeeee = 5 };"); + verifyFormat("const struct A a = {.a = 1, .b = 2};"); + verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" + " .bbbbbbbbbb = 2,\n" + " .cccccccccc = 3,\n" + " .dddddddddd = 4,\n" + " .eeeeeeeeee = 5};"); verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" - " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" - " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" - " .ccccccccccccccccccccccccccc = 3,\n" - " .ddddddddddddddddddddddddddd = 4,\n" - " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5\n" - "};"); + " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" + " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" + " .ccccccccccccccccccccccccccc = 3,\n" + " .ddddddddddddddddddddddddddd = 4,\n" + " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); } TEST_F(FormatTest, NestedStaticInitializers) { - verifyFormat("static A x = { { {} } };\n"); - verifyFormat("static A x = { { { init1, init2, init3, init4 },\n" - " { init1, init2, init3, init4 } } };"); + verifyFormat("static A x = {{{}}};\n"); + verifyFormat("static A x = {{{init1, init2, init3, init4},\n" + " {init1, init2, init3, init4}}};", + getLLVMStyleWithColumns(50)); verifyFormat("somes Status::global_reps[3] = {\n" - " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" - " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" - " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" - "};"); + " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" + " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" + " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", + getLLVMStyleWithColumns(60)); verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); verifyFormat( - "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n" - " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop" - " } };"); + "CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" + " {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};"); verifyFormat( - "SomeArrayOfSomeType a = { { { 1, 2, 3 },\n" - " { 1, 2, 3 },\n" - " { 111111111111111111111111111111,\n" - " 222222222222222222222222222222,\n" - " 333333333333333333333333333333 },\n" - " { 1, 2, 3 },\n" - " { 1, 2, 3 } } };"); + "SomeArrayOfSomeType a = {\n" + " {{1, 2, 3},\n" + " {1, 2, 3},\n" + " {111111111111111111111111111111, 222222222222222222222222222222,\n" + " 333333333333333333333333333333},\n" + " {1, 2, 3},\n" + " {1, 2, 3}}};"); verifyFormat( - "SomeArrayOfSomeType a = { { { 1, 2, 3 } },\n" - " { { 1, 2, 3 } },\n" - " { { 111111111111111111111111111111,\n" - " 222222222222222222222222222222,\n" - " 333333333333333333333333333333 } },\n" - " { { 1, 2, 3 } },\n" - " { { 1, 2, 3 } } };"); - verifyGoogleFormat( "SomeArrayOfSomeType a = {\n" " {{1, 2, 3}},\n" " {{1, 2, 3}},\n" @@ -1965,10 +1949,10 @@ TEST_F(FormatTest, NestedStaticInitializers) { "struct {\n" " unsigned bit;\n" " const char *const name;\n" - "} kBitsToOs[] = { { kOsMac, \"Mac\" },\n" - " { kOsWin, \"Windows\" },\n" - " { kOsLinux, \"Linux\" },\n" - " { kOsCrOS, \"Chrome OS\" } };"); + "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" + " {kOsWin, \"Windows\"},\n" + " {kOsLinux, \"Linux\"},\n" + " {kOsCrOS, \"Chrome OS\"}};"); } TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { @@ -2514,7 +2498,7 @@ TEST_F(FormatTest, LayoutNestedBlocks) { " struct s {\n" " int i;\n" " };\n" - " s kBitsToOs[] = { { 10 } };\n" + " s kBitsToOs[] = {{10}};\n" " for (int i = 0; i < 10; ++i)\n" " return;\n" "}"); @@ -4131,7 +4115,7 @@ TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { TEST_F(FormatTest, UnderstandsTemplateParameters) { verifyFormat("A a;"); - verifyFormat("A > > a;"); + verifyFormat("A>> a;"); verifyFormat("A, 3>, 4> a;"); verifyFormat("bool x = a < 1 || 2 > a;"); verifyFormat("bool x = 5 < f();"); @@ -4230,8 +4214,8 @@ TEST_F(FormatTest, UnderstandsUnaryOperators) { verifyFormat("#define X -1"); verifyFormat("#define X -kConstant"); - verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); - verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); + verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); + verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); verifyFormat("int a = /* confusing comment */ -1;"); // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. @@ -4264,7 +4248,7 @@ TEST_F(FormatTest, UnderstandsOverloadedOperators) { verifyFormat("operator void *();"); verifyFormat("operator SomeType();"); verifyFormat("operator SomeType();"); - verifyFormat("operator SomeType >();"); + verifyFormat("operator SomeType>();"); verifyFormat("void *operator new(std::size_t size);"); verifyFormat("void *operator new[](std::size_t size);"); verifyFormat("void operator delete(void *ptr);"); @@ -4389,9 +4373,8 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); verifyFormat( - "int *MyValues = {\n" - " *A, // Operator detection might be confused by the '{'\n" - " *BB // Operator detection might be confused by previous comment\n" + "int *MyValues = {*A, // Operator detection might be confused by the '{'\n" + " *BB // Operator detection might be confused by previous comment\n" "};"); verifyIndependentOfContext("if (int *a = &b)"); @@ -4836,168 +4819,167 @@ TEST_F(FormatTest, IncorrectCodeErrorDetection) { TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { verifyFormat("int x = {\n" - " avariable,\n" - " b(alongervariable)\n" - "};", + " avariable,\n" + " b(alongervariable)};", getLLVMStyleWithColumns(25)); } TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { - verifyFormat("return (a)(b) { 1, 2, 3 };"); + verifyFormat("return (a)(b) {1, 2, 3};"); } TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { - verifyFormat("vector x{ 1, 2, 3, 4 };"); - verifyFormat("vector x{ {}, {}, {}, {} };"); - verifyFormat("f({ 1, 2 });"); - verifyFormat("auto v = Foo{ 1 };"); - verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });"); - verifyFormat("Class::Class : member{ 1, 2, 3 } {}"); - verifyFormat("new vector{ 1, 2, 3 };"); - verifyFormat("new int[3]{ 1, 2, 3 };"); - verifyFormat("return { arg1, arg2 };"); - verifyFormat("return { arg1, SomeType{ parameter } };"); - verifyFormat("int count = set{ f(), g(), h() }.size();"); - verifyFormat("new T{ arg1, arg2 };"); - verifyFormat("f(MyMap[{ composite, key }]);"); - verifyFormat("class Class {\n" - " T member = { arg1, arg2 };\n" - "};"); - verifyFormat( - "foo = aaaaaaaaaaa ? vector{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" - " : vector{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" - " bbbbbbbbbbbbbbbbbbbb, bbbbb };"); - verifyFormat("DoSomethingWithVector({} /* No data */);"); - verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });"); - verifyFormat( - "someFunction(OtherParam,\n" - " BracedList{ // comment 1 (Forcing interesting break)\n" - " param1, param2,\n" - " // comment 2\n" - " param3, param4 });", - getLLVMStyleWithColumns(75)); - verifyFormat( - "std::this_thread::sleep_for(\n" - " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);"); - verifyFormat("std::vector aaaaaaaaaaaaaaaaaaa{\n" - " aaaaaaa, aaaaaaaaaa,\n" - " aaaaa, aaaaaaaaaaaaaaa,\n" - " aaa, aaaaaaaaaa,\n" - " a, aaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaa, a\n" - "};"); - verifyFormat("vector foo = { ::SomeGlobalFunction() };"); + verifyFormat("vector x{1, 2, 3, 4};"); + verifyFormat("vector x{{}, {}, {}, {}};"); + verifyFormat("f({1, 2});"); + verifyFormat("auto v = Foo{-1};"); + verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); + verifyFormat("Class::Class : member{1, 2, 3} {}"); + verifyFormat("new vector{1, 2, 3};"); + verifyFormat("new int[3]{1, 2, 3};"); + verifyFormat("return {arg1, arg2};"); + verifyFormat("return {arg1, SomeType{parameter}};"); + verifyFormat("int count = set{f(), g(), h()}.size();"); + verifyFormat("new T{arg1, arg2};"); + verifyFormat("f(MyMap[{composite, key}]);"); + verifyFormat("class Class {\n" + " T member = {arg1, arg2};\n" + "};"); + verifyFormat("vector foo = {::SomeGlobalFunction()};"); - FormatStyle NoSpaces = getLLVMStyle(); - NoSpaces.Cpp11BracedListStyle = true; - verifyFormat("vector x{1, 2, 3, 4};", NoSpaces); - verifyFormat("vector x{{}, {}, {}, {}};", NoSpaces); - verifyFormat("f({1, 2});", NoSpaces); - verifyFormat("auto v = Foo{-1};", NoSpaces); - verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});", NoSpaces); - verifyFormat("Class::Class : member{1, 2, 3} {}", NoSpaces); - verifyFormat("new vector{1, 2, 3};", NoSpaces); - verifyFormat("new int[3]{1, 2, 3};", NoSpaces); - verifyFormat("return {arg1, arg2};", NoSpaces); - verifyFormat("return {arg1, SomeType{parameter}};", NoSpaces); - verifyFormat("int count = set{f(), g(), h()}.size();", NoSpaces); - verifyFormat("new T{arg1, arg2};", NoSpaces); - verifyFormat("f(MyMap[{composite, key}]);", NoSpaces); - verifyFormat("class Class {\n" - " T member = {arg1, arg2};\n" - "};", - NoSpaces); - verifyFormat("vector foo = {::SomeGlobalFunction()};", NoSpaces); + // FIXME: The alignment of these trailing comments might be bad. Then again, + // this might be utterly useless in real code. + verifyFormat("Constructor::Constructor()\n" + " : some_value{ //\n" + " aaaaaaa //\n" + " } {}"); - // FIXME: The alignment of these trailing comments might be bad. Then again, - // this might be utterly useless in real code. - verifyFormat("Constructor::Constructor()\n" - " : some_value{ //\n" - " aaaaaaa //\n" - " } {}", - NoSpaces); + // In braced lists, the first comment is always assumed to belong to the + // first element. Thus, it can be moved to the next or previous line as + // appropriate. + EXPECT_EQ("function({// First element:\n" + " 1,\n" + " // Second element:\n" + " 2});", + format("function({\n" + " // First element:\n" + " 1,\n" + " // Second element:\n" + " 2});")); + EXPECT_EQ("std::vector MyNumbers{\n" + " // First element:\n" + " 1,\n" + " // Second element:\n" + " 2};", + format("std::vector MyNumbers{// First element:\n" + " 1,\n" + " // Second element:\n" + " 2};", + getLLVMStyleWithColumns(30))); - // In braced lists, the first comment is always assumed to belong to the - // first element. Thus, it can be moved to the next or previous line as - // appropriate. - EXPECT_EQ("function({// First element:\n" - " 1,\n" - " // Second element:\n" - " 2});", - format("function({\n" - " // First element:\n" - " 1,\n" - " // Second element:\n" - " 2});", - NoSpaces)); - NoSpaces.ColumnLimit = 30; - EXPECT_EQ( - "std::vector MyNumbers{\n" - " // First element:\n" - " 1,\n" - " // Second element:\n" - " 2};", - format("std::vector MyNumbers{// First element:\n" - " 1,\n" - " // Second element:\n" - " 2};", NoSpaces)); + FormatStyle ExtraSpaces = getLLVMStyle(); + ExtraSpaces.Cpp11BracedListStyle = false; + ExtraSpaces.ColumnLimit = 75; + verifyFormat("vector x{ 1, 2, 3, 4 };", ExtraSpaces); + verifyFormat("vector x{ {}, {}, {}, {} };", ExtraSpaces); + verifyFormat("f({ 1, 2 });", ExtraSpaces); + verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); + verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); + verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); + verifyFormat("new vector{ 1, 2, 3 };", ExtraSpaces); + verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); + verifyFormat("return { arg1, arg2 };", ExtraSpaces); + verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); + verifyFormat("int count = set{ f(), g(), h() }.size();", ExtraSpaces); + verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); + verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); + verifyFormat("class Class {\n" + " T member = { arg1, arg2 };\n" + "};", + ExtraSpaces); + verifyFormat( + "foo = aaaaaaaaaaa ? vector{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" + " : vector{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" + " bbbbbbbbbbbbbbbbbbbb, bbbbb };", + ExtraSpaces); + verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); + verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", + ExtraSpaces); + verifyFormat( + "someFunction(OtherParam,\n" + " BracedList{ // comment 1 (Forcing interesting break)\n" + " param1, param2,\n" + " // comment 2\n" + " param3, param4 });", + ExtraSpaces); + verifyFormat( + "std::this_thread::sleep_for(\n" + " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", + ExtraSpaces); + verifyFormat("std::vector aaaaaaaaaaaaaaaaaaa{\n" + " aaaaaaa, aaaaaaaaaa,\n" + " aaaaa, aaaaaaaaaaaaaaa,\n" + " aaa, aaaaaaaaaa,\n" + " a, aaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaa, a\n" + "};", + ExtraSpaces); + verifyFormat("vector foo = { ::SomeGlobalFunction() };", ExtraSpaces); } TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { - verifyFormat("vector x = { 1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " 1, 22, 333, 4444, 55555, 666666, 7777777 };"); - verifyFormat("vector x = { 1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " // line comment\n" - " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " 1, 22, 333, 4444, 55555,\n" - " // line comment\n" - " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " 1, 22, 333, 4444, 55555, 666666, 7777777 };"); + verifyFormat("vector x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555, 666666, 7777777};"); + verifyFormat("vector x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " // line comment\n" + " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555,\n" + " // line comment\n" + " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555, 666666, 7777777};"); verifyFormat( - "vector x = { 1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " 1, 22, 333, 4444, 55555, 666666, // comment\n" - " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" - " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" - " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" - " 7777777 };"); + "vector x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" + " 1, 22, 333, 4444, 55555, 666666, // comment\n" + " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" + " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" + " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" + " 7777777};"); verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" - " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" - " X86::R8, X86::R9, X86::R10, X86::R11, 0\n" - "};"); - verifyFormat("vector x = { 1, 1, 1, 1,\n" - " 1, 1, 1, 1 };", + " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" + " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); + verifyFormat("vector x = {1, 1, 1, 1,\n" + " 1, 1, 1, 1};", getLLVMStyleWithColumns(39)); - verifyFormat("vector x = { 1, 1, 1, 1,\n" - " 1, 1, 1, 1 };", + verifyFormat("vector x = {1, 1, 1, 1,\n" + " 1, 1, 1, 1};", getLLVMStyleWithColumns(38)); verifyFormat("vector aaaaaaaaaaaaaaaaaaaaaa = {\n" - " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1\n" - "};", - getLLVMStyleWithColumns(40)); + " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", + getLLVMStyleWithColumns(43)); // Trailing commas. - verifyFormat("vector x = { 1, 1, 1, 1,\n" - " 1, 1, 1, 1, };", + verifyFormat("vector x = {1, 1, 1, 1,\n" + " 1, 1, 1, 1, };", getLLVMStyleWithColumns(39)); - verifyFormat("vector x = { 1, 1, 1, 1,\n" - " 1, 1, 1, 1, //\n" + verifyFormat("vector x = {1, 1, 1, 1,\n" + " 1, 1, 1, 1, //\n" "};", getLLVMStyleWithColumns(39)); - verifyFormat("vector x = { 1, 1, 1, 1,\n" - " 1, 1, 1, 1,\n" - " /**/ /**/ };", + verifyFormat("vector x = {1, 1, 1, 1,\n" + " 1, 1, 1, 1,\n" + " /**/ /**/};", getLLVMStyleWithColumns(39)); - verifyFormat("return { { aaaaaaaaaaaaaaaaaaaaa },\n" - " { aaaaaaaaaaaaaaaaaaa },\n" - " { aaaaaaaaaaaaaaaaaaaaa },\n" - " { aaaaaaaaaaaaaaaaa } };", + verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" + " {aaaaaaaaaaaaaaaaaaa},\n" + " {aaaaaaaaaaaaaaaaaaaaa},\n" + " {aaaaaaaaaaaaaaaaa}};", getLLVMStyleWithColumns(60)); // With nested lists, we should either format one item per line or all nested @@ -5005,18 +4987,17 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { // FIXME: For some nested lists, we can do better. verifyFormat( "SomeStruct my_struct_array = {\n" - " { aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" - " aaaaaaaaaaaaa, aaaaaaa, aaa },\n" - " { aaa, aaa },\n" - " { aaa, aaa },\n" - " { aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa },\n" - " { aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" - " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa },\n" - "};"); + " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" + " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" + " {aaa, aaa},\n" + " {aaa, aaa},\n" + " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" + " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); // No column layout should be used here. - verifyFormat("aaaaaaaaaaaaaaa = { aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" - " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb };"); + verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" + " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); } TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { @@ -5110,9 +5091,9 @@ TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { // Elaborate type variable declarations. - verifyFormat("struct foo a = { bar };\nint n;"); - verifyFormat("class foo a = { bar };\nint n;"); - verifyFormat("union foo a = { bar };\nint n;"); + verifyFormat("struct foo a = {bar};\nint n;"); + verifyFormat("class foo a = {bar};\nint n;"); + verifyFormat("union foo a = {bar};\nint n;"); // Elaborate types inside function definitions. verifyFormat("struct foo f() {}\nint n;"); @@ -5378,13 +5359,13 @@ TEST_F(FormatTest, BlockCommentsInMacros) { TEST_F(FormatTest, BlockCommentsAtEndOfLine) { EXPECT_EQ("a = {\n" - " 1111 /* */\n" + " 1111 /* */\n" "};", format("a = {1111 /* */\n" "};", getLLVMStyleWithColumns(15))); EXPECT_EQ("a = {\n" - " 1111 /* */\n" + " 1111 /* */\n" "};", format("a = {1111 /* */\n" "};", @@ -5392,8 +5373,8 @@ TEST_F(FormatTest, BlockCommentsAtEndOfLine) { // FIXME: The formatting is still wrong here. EXPECT_EQ("a = {\n" - " 1111 /* a\n" - " */\n" + " 1111 /* a\n" + " */\n" "};", format("a = {1111 /* a */\n" "};", @@ -5974,21 +5955,21 @@ TEST_F(FormatTest, ObjCLiterals) { TEST_F(FormatTest, ObjCDictLiterals) { verifyFormat("@{"); verifyFormat("@{}"); - verifyFormat("@{ @\"one\" : @1 }"); - verifyFormat("return @{ @\"one\" : @1 };"); - verifyFormat("@{ @\"one\" : @1, }"); + verifyFormat("@{@\"one\" : @1}"); + verifyFormat("return @{@\"one\" : @1;"); + verifyFormat("@{@\"one\" : @1, }"); - verifyFormat("@{ @\"one\" : @{ @2 : @1 } }"); - verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }"); + verifyFormat("@{@\"one\" : @{@2 : @1}}"); + verifyFormat("@{@\"one\" : @{@2 : @1}, }"); - verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }"); + verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); verifyFormat("[self setDict:@{}"); - verifyFormat("[self setDict:@{ @1 : @2 }"); - verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);"); + verifyFormat("[self setDict:@{@1 : @2}"); + verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); verifyFormat( - "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };"); + "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); verifyFormat( - "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };"); + "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); verifyFormat( "NSDictionary *d = @{\n" @@ -6461,8 +6442,10 @@ TEST_F(FormatTest, SkipsUnknownStringLiterals) { } TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { + FormatStyle Style = getLLVMStyle(); + Style.Standard = FormatStyle::LS_Cpp03; EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", - format("#define x(_a) printf(\"foo\"_a);", getLLVMStyle())); + format("#define x(_a) printf(\"foo\"_a);", Style)); } TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { @@ -6560,7 +6543,7 @@ TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { verifyFormat("void f() {\n" " return g() {}\n" " void h() {}"); - verifyFormat("int a[] = { void forgot_closing_brace() { f();\n" + verifyFormat("int a[] = {void forgot_closing_brace() {f();\n" "g();\n" "}"); } @@ -6568,7 +6551,7 @@ TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { verifyFormat( "void f() {\n" - " return C{ param1, param2 }.SomeCall(param1, param2);\n" + " return C{param1, param2}.SomeCall(param1, param2);\n" "}\n"); } @@ -6583,6 +6566,7 @@ TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { TEST_F(FormatTest, ConfigurableIndentWidth) { FormatStyle EightIndent = getLLVMStyleWithColumns(18); EightIndent.IndentWidth = 8; + EightIndent.ContinuationIndentWidth = 8; verifyFormat("void f() {\n" " someFunction();\n" " if (true) {\n" @@ -6597,8 +6581,7 @@ TEST_F(FormatTest, ConfigurableIndentWidth) { EightIndent); verifyFormat("int x[] = {\n" " call(),\n" - " call(),\n" - "};", + " call()};", EightIndent); } @@ -6712,13 +6695,13 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { Tab)); Tab.UseTab = FormatStyle::UT_ForIndentation; - verifyFormat("T t[] = {\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + verifyFormat("{\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" "};", Tab); verifyFormat("enum A {\n" @@ -7954,10 +7937,10 @@ TEST_F(FormatTest, FormatsLambdas) { verifyFormat("int c = [&a, &a, a] {\n" " [=, a, b, &c] { return b++; }();\n" "}();\n"); - verifyFormat("auto c = { [&a, &a, a] {\n" + verifyFormat("auto c = {[&a, &a, a] {\n" " [=, a, b, &c] { return b++; }();\n" - "} }\n"); - verifyFormat("auto c = { [&a, &a, a] { [=, a, b, &c] {}(); } }\n"); + "}}\n"); + verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); verifyFormat("void f() {\n" " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" "}\n"); @@ -7969,11 +7952,11 @@ TEST_F(FormatTest, FormatsLambdas) { // Lambdas with return types. verifyFormat("int c = []()->int { return 2; }();\n"); - verifyFormat("int c = []()->vector { return { 2 }; }();\n"); - verifyFormat("Foo([]()->std::vector { return { 2 }; }());"); + verifyFormat("int c = []()->vector { return {2}; }();\n"); + verifyFormat("Foo([]()->std::vector { return {2}; }());"); // Not lambdas. - verifyFormat("constexpr char hello[]{ \"hello\" };"); + verifyFormat("constexpr char hello[]{\"hello\"};"); verifyFormat("double &operator[](int i) { return 0; }\n" "int i;"); verifyFormat("std::unique_ptr foo() {}"); @@ -7990,7 +7973,7 @@ TEST_F(FormatTest, FormatsBlocks) { // it would ideally look. verifyFormat("[operation setCompletionBlock:^{ [self onOperationDone]; }];"); verifyFormat("int i = {[operation setCompletionBlock : ^{ [self " - "onOperationDone]; }] };"); + "onOperationDone]; }]};"); verifyFormat("[operation setCompletionBlock:^(int *i) { f(); }];"); verifyFormat("int a = [operation block:^int(int *i) { return 1; }];"); verifyFormat("[myObject doSomethingWith:arg1\n"