[C++11] Switch the clang-format LLVM style to use C++11 style braced

init list formatting. This suggestion has now gone into the LLVM coding
standards, and is particularly relevant now that we're using C++11.

Updated a really ridiculous number of tests to reflect this change.

llvm-svn: 202637
This commit is contained in:
Chandler Carruth 2014-03-02 12:37:31 +00:00
parent b6d0bd48bd
commit f8b7266d57
2 changed files with 276 additions and 288 deletions

View File

@ -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;
}

View File

@ -703,8 +703,7 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
" // A\n"
" \"aaaa\",\n"
" // B\n"
" \"aaaaa\",\n"
"};");
" \"aaaaa\"};");
verifyGoogleFormat(
"aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaa); // 81_cols_with_this_comment");
@ -1364,8 +1363,7 @@ TEST_F(FormatTest, CommentsInStaticInitializers) {
" a,\n"
"\n"
" // Comment after empty line\n"
" b\n"
"}",
" b}",
format("S s = {\n"
" // Some comment\n"
" a,\n"
@ -1378,8 +1376,7 @@ TEST_F(FormatTest, CommentsInStaticInitializers) {
" a,\n"
"\n"
" /* Comment after empty line */\n"
" b\n"
"}",
" b}",
format("S s = {\n"
" /* Some comment */\n"
" a,\n"
@ -1390,8 +1387,7 @@ TEST_F(FormatTest, CommentsInStaticInitializers) {
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}; // comment\n");
}
TEST_F(FormatTest, IgnoresIf0Contents) {
@ -1877,13 +1873,11 @@ TEST_F(FormatTest, StaticInitializers) {
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"
"};");
" 10000000000000000000000000};");
EXPECT_EQ("S s = {a, b};", format("S s = {\n"
" a,\n"
"\n"
@ -1894,8 +1888,7 @@ TEST_F(FormatTest, StaticInitializers) {
// 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));
}
@ -1911,8 +1904,7 @@ TEST_F(FormatTest, DesignatedInitializers) {
" .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
" .ccccccccccccccccccccccccccc = 3,\n"
" .ddddddddddddddddddddddddddd = 4,\n"
" .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5\n"
"};");
" .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
}
@ -1920,39 +1912,31 @@ TEST_F(FormatTest, DesignatedInitializers) {
TEST_F(FormatTest, NestedStaticInitializers) {
verifyFormat("static A x = {{{}}};\n");
verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
" { init1, init2, init3, init4 } } };");
" {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, 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"
" } };");
" {rect.fRight - rect.fLeft, rect.fBottom - rect.fTop}};");
verifyFormat(
"SomeArrayOfSomeType a = { { { 1, 2, 3 },\n"
"SomeArrayOfSomeType a = {\n"
" {{1, 2, 3},\n"
" {1, 2, 3},\n"
" { 111111111111111111111111111111,\n"
" 222222222222222222222222222222,\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"
@ -4389,8 +4373,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
verifyFormat(
"int *MyValues = {\n"
" *A, // Operator detection might be confused by the '{'\n"
"int *MyValues = {*A, // Operator detection might be confused by the '{'\n"
" *BB // Operator detection might be confused by previous comment\n"
"};");
@ -4837,8 +4820,7 @@ TEST_F(FormatTest, IncorrectCodeErrorDetection) {
TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
verifyFormat("int x = {\n"
" avariable,\n"
" b(alongervariable)\n"
"};",
" b(alongervariable)};",
getLLVMStyleWithColumns(25));
}
@ -4850,7 +4832,7 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
verifyFormat("vector<int> x{1, 2, 3, 4};");
verifyFormat("vector<T> x{{}, {}, {}, {}};");
verifyFormat("f({1, 2});");
verifyFormat("auto v = Foo{ 1 };");
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<int>{1, 2, 3};");
@ -4863,61 +4845,14 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
verifyFormat("class Class {\n"
" T member = {arg1, arg2};\n"
"};");
verifyFormat(
"foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
" : vector<int>{ 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<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
" aaaaaaa, aaaaaaaaaa,\n"
" aaaaa, aaaaaaaaaaaaaaa,\n"
" aaa, aaaaaaaaaa,\n"
" a, aaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaa, a\n"
"};");
verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
FormatStyle NoSpaces = getLLVMStyle();
NoSpaces.Cpp11BracedListStyle = true;
verifyFormat("vector<int> x{1, 2, 3, 4};", NoSpaces);
verifyFormat("vector<T> 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<int>{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<int>{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<int> 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"
" } {}",
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
@ -4930,11 +4865,8 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
" // First element:\n"
" 1,\n"
" // Second element:\n"
" 2});",
NoSpaces));
NoSpaces.ColumnLimit = 30;
EXPECT_EQ(
"std::vector<int> MyNumbers{\n"
" 2});"));
EXPECT_EQ("std::vector<int> MyNumbers{\n"
" // First element:\n"
" 1,\n"
" // Second element:\n"
@ -4942,7 +4874,59 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
format("std::vector<int> MyNumbers{// First element:\n"
" 1,\n"
" // Second element:\n"
" 2};", NoSpaces));
" 2};",
getLLVMStyleWithColumns(30)));
FormatStyle ExtraSpaces = getLLVMStyle();
ExtraSpaces.Cpp11BracedListStyle = false;
ExtraSpaces.ColumnLimit = 75;
verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
verifyFormat("vector<T> 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<int>{ 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<int>{ 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<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
" : vector<int>{ 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<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
" aaaaaaa, aaaaaaaaaa,\n"
" aaaaa, aaaaaaaaaaaaaaa,\n"
" aaa, aaaaaaaaaa,\n"
" a, aaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaa, a\n"
"};",
ExtraSpaces);
verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
}
TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
@ -4969,8 +4953,7 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
" 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"
"};");
" X86::R8, X86::R9, X86::R10, X86::R11, 0};");
verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
" 1, 1, 1, 1};",
getLLVMStyleWithColumns(39));
@ -4978,9 +4961,8 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
" 1, 1, 1, 1};",
getLLVMStyleWithColumns(38));
verifyFormat("vector<int> 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<int> x = {1, 1, 1, 1,\n"
@ -5011,8 +4993,7 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
" {aaa, aaa},\n"
" {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
" {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa },\n"
"};");
" aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
// No column layout should be used here.
verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
@ -5975,7 +5956,7 @@ TEST_F(FormatTest, ObjCDictLiterals) {
verifyFormat("@{");
verifyFormat("@{}");
verifyFormat("@{@\"one\" : @1}");
verifyFormat("return @{ @\"one\" : @1 };");
verifyFormat("return @{@\"one\" : @1;");
verifyFormat("@{@\"one\" : @1, }");
verifyFormat("@{@\"one\" : @{@2 : @1}}");
@ -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) {
@ -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"