/// \brief The brace breaking style to use.
BraceBreakingStyle BreakBeforeBraces;
- /// \brief If \c true, format { 1 }, otherwise {1}.
- bool SpacesInBracedLists;
+ /// \brief If \c true, format braced lists as best suited for C++11 braced
+ /// lists.
+ ///
+ /// Important differences:
+ /// - No spaces inside the braced list.
+ /// - No line break before the closing brace.
+ /// - Indentation with the continuation indent, not with the block indent.
+ ///
+ /// Fundamentally, C++11 braced lists are formatted exactly like function
+ /// calls would be formatted in their place. If the braced list follows a name
+ /// (e.g. a type or variable name), clang-format formats as if the "{}" were
+ /// the parentheses of a function call with that name. If there is no name,
+ /// a zero-length name is assumed.
+ bool Cpp11BracedListStyle;
/// \brief If \c true, indent when breaking function declarations which
/// are not also definitions after the type.
PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine &&
PointerBindsToType == R.PointerBindsToType &&
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
- SpacesInBracedLists == R.SpacesInBracedLists &&
+ Cpp11BracedListStyle == R.Cpp11BracedListStyle &&
Standard == R.Standard && UseTab == R.UseTab &&
IndentFunctionDeclarationAfterType ==
R.IndentFunctionDeclarationAfterType;
IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
IO.mapOptional("SpacesBeforeTrailingComments",
Style.SpacesBeforeTrailingComments);
- IO.mapOptional("SpacesInBracedLists", Style.SpacesInBracedLists);
+ IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
IO.mapOptional("Standard", Style.Standard);
IO.mapOptional("IndentWidth", Style.IndentWidth);
IO.mapOptional("UseTab", Style.UseTab);
LLVMStyle.ObjCSpaceBeforeProtocolList = true;
LLVMStyle.PointerBindsToType = false;
LLVMStyle.SpacesBeforeTrailingComments = 1;
- LLVMStyle.SpacesInBracedLists = true;
+ LLVMStyle.Cpp11BracedListStyle = false;
LLVMStyle.Standard = FormatStyle::LS_Cpp03;
LLVMStyle.IndentWidth = 2;
LLVMStyle.UseTab = false;
GoogleStyle.ObjCSpaceBeforeProtocolList = false;
GoogleStyle.PointerBindsToType = true;
GoogleStyle.SpacesBeforeTrailingComments = 2;
- GoogleStyle.SpacesInBracedLists = false;
+ GoogleStyle.Cpp11BracedListStyle = true;
GoogleStyle.Standard = FormatStyle::LS_Auto;
GoogleStyle.IndentWidth = 2;
GoogleStyle.UseTab = false;
unsigned LastSpace = State.Stack.back().LastSpace;
bool AvoidBinPacking;
if (Current.is(tok::l_brace)) {
- NewIndent = Style.IndentWidth + LastSpace;
+ NewIndent =
+ LastSpace + (Style.Cpp11BracedListStyle ? 4 : Style.IndentWidth);
const FormatToken *NextNoComment = Current.getNextNonComment();
AvoidBinPacking = NextNoComment &&
NextNoComment->Type == TT_DesignatedInitializerPeriod;
const FormatToken &Previous = *Current.Previous;
if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
return true;
- if (Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)
- return true;
+ if (!Style.Cpp11BracedListStyle && Current.is(tok::r_brace) &&
+ State.Stack.back().BreakBeforeClosingBrace)
+ return true;
if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
return true;
if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) ||
if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
return false; // No spaces in "{}".
if (Left.is(tok::l_brace) || Right.is(tok::r_brace))
- return Style.SpacesInBracedLists;
+ return !Style.Cpp11BracedListStyle;
if (Right.Type == TT_UnaryOperator)
return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
(Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
" { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n"
"};");
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}\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"
" 333333333333333333333333333333 } },\n"
" { { 1, 2, 3 } }, { { 1, 2, 3 } } };");
verifyGoogleFormat(
- "SomeArrayOfSomeType a = {{{1, 2, 3}}, {{1, 2, 3}},\n"
- " {{111111111111111111111111111111,\n"
- " 222222222222222222222222222222,\n"
- " 333333333333333333333333333333}},\n"
- " {{1, 2, 3}}, {{1, 2, 3}}};");
+ "SomeArrayOfSomeType a = {\n"
+ " {{1, 2, 3}}, {{1, 2, 3}},\n"
+ " {{111111111111111111111111111111, 222222222222222222222222222222,\n"
+ " 333333333333333333333333333333}},\n"
+ " {{1, 2, 3}}, {{1, 2, 3}}};");
// FIXME: We might at some point want to handle this similar to parameter
// lists, where we have an option to put each on a single line.
" });");
FormatStyle NoSpaces = getLLVMStyle();
- NoSpaces.SpacesInBracedLists = false;
+ NoSpaces.Cpp11BracedListStyle = true;
verifyFormat("vector<int> x{1, 2, 3, 4};", NoSpaces);
verifyFormat("vector<T> x{{}, {}, {}, {}};", NoSpaces);
verifyFormat("f({1, 2});", NoSpaces);
CHECK_PARSE_BOOL(IndentCaseLabels);
CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
CHECK_PARSE_BOOL(PointerBindsToType);
- CHECK_PARSE_BOOL(SpacesInBracedLists);
+ CHECK_PARSE_BOOL(Cpp11BracedListStyle);
CHECK_PARSE_BOOL(UseTab);
CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);