aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/TokenAnnotator.cpp
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2013-02-23 21:01:55 +0000
committerDaniel Jasper <djasper@google.com>2013-02-23 21:01:55 +0000
commit237d4c1c785be13656bff6c09e5b7ccd066ff5ba (patch)
tree9e223d4193e9c674d0b15715b83516b1e2401624 /lib/Format/TokenAnnotator.cpp
parent04ea68c12578955c125f7df3b58432fcdb28484e (diff)
Better formatting of conditional expressions.
In conditional expressions, if the condition is split over multiple lines, also break before both operands. This prevents formattings like: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ? b : c; Which are bad, because they suggestion incorrect operator precedence: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ? b : c); This lead to the discovery that the expression parser incorrectly handled conditional operators and that it could also handle semicolons (which in turn reduced the amount of special casing for for-loops). As a side-effect, we can now apply the bin-packing configuration to the sections of for-loops. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175973 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/TokenAnnotator.cpp')
-rw-r--r--lib/Format/TokenAnnotator.cpp32
1 files changed, 24 insertions, 8 deletions
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index 80780185c0..b7f43d52ed 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -724,7 +724,7 @@ public:
ExpressionParser(AnnotatedLine &Line) : Current(&Line.First) {}
/// \brief Parse expressions with the given operatore precedence.
- void parse(prec::Level Precedence = prec::Unknown) {
+ void parse(int Precedence = 0) {
if (Precedence > prec::PointerToMember || Current == NULL)
return;
@@ -740,18 +740,27 @@ public:
AnnotatedToken *Start = Current;
bool OperatorFound = false;
- while (Current != NULL) {
+ while (Current) {
// Consume operators with higher precedence.
parse(prec::Level(Precedence + 1));
+ int CurrentPrecedence = 0;
+ if (Current) {
+ if (Current->Type == TT_ConditionalExpr)
+ CurrentPrecedence = 1 + (int) prec::Conditional;
+ else if (Current->is(tok::semi))
+ CurrentPrecedence = 1;
+ else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
+ CurrentPrecedence = 1 + (int) getPrecedence(*Current);
+ }
+
// At the end of the line or when an operator with higher precedence is
// found, insert fake parenthesis and return.
- if (Current == NULL || Current->is(tok::semi) || closesScope(*Current) ||
- ((Current->Type == TT_BinaryOperator || Current->is(tok::comma)) &&
- getPrecedence(*Current) < Precedence)) {
+ if (Current == NULL || closesScope(*Current) ||
+ (CurrentPrecedence != 0 && CurrentPrecedence < Precedence)) {
if (OperatorFound) {
++Start->FakeLParens;
- if (Current != NULL)
+ if (Current)
++Current->Parent->FakeRParens;
}
return;
@@ -759,14 +768,21 @@ public:
// Consume scopes: (), [], <> and {}
if (opensScope(*Current)) {
- while (Current != NULL && !closesScope(*Current)) {
+ AnnotatedToken *Left = Current;
+ while (Current && !closesScope(*Current)) {
next();
parse();
}
+ // Remove fake parens that just duplicate the real parens.
+ if (Current && Left->Children[0].FakeLParens > 0 &&
+ Current->Parent->FakeRParens > 0) {
+ --Left->Children[0].FakeLParens;
+ --Current->Parent->FakeRParens;
+ }
next();
} else {
// Operator found.
- if (getPrecedence(*Current) == Precedence)
+ if (CurrentPrecedence == Precedence)
OperatorFound = true;
next();