/// \brief Returns \c true, if a line break after \p State is allowed.
bool canBreak(const LineState &State) {
- if (!State.NextToken->CanBreakBefore &&
- !(State.NextToken->is(tok::r_brace) &&
+ const AnnotatedToken &Current = *State.NextToken;
+ const AnnotatedToken &Previous = *Current.Parent;
+ if (!Current.CanBreakBefore &&
+ !(Current.is(tok::r_brace) &&
State.Stack.back().BreakBeforeClosingBrace))
return false;
+ // The opening "{" of a braced list has to be on the same line as the first
+ // element if it is nested in another braced init list or function call.
+ if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
+ Previous.Parent &&
+ Previous.Parent->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
+ return false;
return !State.Stack.back().NoLineBreak;
}
"static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n"
" looooooooooooooooooooooooooooooooooongname,\n"
" looooooooooooooooooooooooooooooong };");
+ // Here, everything other than the "}" would fit on a line.
+ verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
+ " 100000000000000000000000\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"
+ "};",
+ getLLVMStyleWithColumns(40));
}
TEST_F(FormatTest, NestedStaticInitializers) {