[cfe-commits] r172433 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp
Manuel Klimek
klimek at google.com
Mon Jan 14 08:41:43 PST 2013
Author: klimek
Date: Mon Jan 14 10:41:43 2013
New Revision: 172433
URL: http://llvm.org/viewvc/llvm-project?rev=172433&view=rev
Log:
Fixes formatting of nested brace initializers.
We now format this correctly:
Status::Rep Status::global_reps[3] = {
{ kGlobalRef, OK_CODE, NULL, NULL, NULL },
{ kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },
{ kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }
};
- fixed a bug where BreakBeforeClosingBrace would be set on the wrong
state
- added penalties for breaking between = and {, and between { and any
other non-{ token
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=172433&r1=172432&r2=172433&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Jan 14 10:41:43 2013
@@ -449,10 +449,10 @@
(ParenLevel != 0 || getPrecedence(Previous) == prec::Assignment))
State.Stack[ParenLevel].LastSpace = State.Column;
}
- moveStateToNextToken(State);
if (Newline && Previous.is(tok::l_brace)) {
State.Stack.back().BreakBeforeClosingBrace = true;
}
+ moveStateToNextToken(State);
}
/// \brief Mark the next token as consumed in \p State and modify its stacks
@@ -503,6 +503,11 @@
const AnnotatedToken &Left = Tok;
const AnnotatedToken &Right = Tok.Children[0];
+ if (Left.is(tok::l_brace) && Right.isNot(tok::l_brace))
+ return 50;
+ if (Left.is(tok::equal) && Right.is(tok::l_brace))
+ return 150;
+
// In for-loops, prefer breaking at ',' and ';'.
if (RootToken.is(tok::kw_for) &&
(Left.isNot(tok::comma) && Left.isNot(tok::semi)))
@@ -561,7 +566,9 @@
if (!NewLine && State.NextToken->MustBreakBefore)
return UINT_MAX;
- if (NewLine && !State.NextToken->CanBreakBefore)
+ if (NewLine && !State.NextToken->CanBreakBefore &&
+ !(State.NextToken->is(tok::r_brace) &&
+ State.Stack.back().BreakBeforeClosingBrace))
return UINT_MAX;
if (!NewLine && State.NextToken->is(tok::r_brace) &&
State.Stack.back().BreakBeforeClosingBrace)
@@ -1249,17 +1256,21 @@
// change the "binding" behavior of a comment.
return false;
- if (Right.is(tok::r_paren) || Right.is(tok::l_brace) ||
+ // We only break before r_brace if there was a corresponding break before
+ // the l_brace, which is tracked by BreakBeforeClosingBrace.
+ if (Right.is(tok::r_brace))
+ return false;
+
+ if (Right.is(tok::r_paren) ||
Right.is(tok::greater))
return false;
return (isBinaryOperator(Left) && Left.isNot(tok::lessless)) ||
Left.is(tok::comma) || Right.is(tok::lessless) ||
Right.is(tok::arrow) || Right.is(tok::period) ||
Right.is(tok::colon) || Left.is(tok::semi) ||
- Left.is(tok::l_brace) || Left.is(tok::question) ||
- Right.is(tok::r_brace) || Left.Type == TT_ConditionalExpr ||
- (Left.is(tok::r_paren) && Left.Type != TT_CastRParen &&
- Right.is(tok::identifier)) ||
+ Left.is(tok::l_brace) || Left.is(tok::question) || Left.Type ==
+ TT_ConditionalExpr || (Left.is(tok::r_paren) && Left.Type !=
+ TT_CastRParen && Right.is(tok::identifier)) ||
(Left.is(tok::l_paren) && !Right.is(tok::r_paren));
}
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172433&r1=172432&r2=172433&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan 14 10:41:43 2013
@@ -459,6 +459,32 @@
" looooooooooooooooooooooooooooooong };");
}
+TEST_F(FormatTest, NestedStaticInitializers) {
+ verifyFormat("static A x = { { {} } };\n");
+ verifyFormat(
+ "static A x = {\n"
+ " { { init1, init2, init3, init4 }, { init1, init2, init3, init4 } }\n"
+ "};\n");
+ 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"
+ "};");
+ verifyFormat(
+ "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n"
+ " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop"
+ " } };");
+
+ // FIXME: We might at some point want to handle this similar to parameters
+ // lists, where we have an option to put each on a single line.
+ verifyFormat("struct {\n"
+ " unsigned bit;\n"
+ " const char *const name;\n"
+ "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n"
+ " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };");
+}
+
TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
" \\\n"
More information about the cfe-commits
mailing list