aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2013-01-14 16:41:43 +0000
committerManuel Klimek <klimek@google.com>2013-01-14 16:41:43 +0000
commit2c7739e3cbf1357c0ef8d894045a300331053565 (patch)
tree7a315176ce512187e231fc46e1826a5baf16dd18 /lib/Format/Format.cpp
parent6f5bb2c93c31a7977382c5079d85db8ca3267cd0 (diff)
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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172433 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r--lib/Format/Format.cpp25
1 files changed, 18 insertions, 7 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index a221e4cce7..aa5d924986 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -449,10 +449,10 @@ private:
(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 @@ private:
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 @@ private:
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 @@ private:
// 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));
}