summaryrefslogtreecommitdiffstats
path: root/clang/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
commitcd8599e8c9774b83160ac1957e139838bf2bed64 (patch)
tree030dde3eaac937443c0a80e6dcea60affab06028 /clang/lib/Format/TokenAnnotator.cpp
parent3407211b2fbab2ca3e06955425854103b67dfdeb (diff)
downloadbcm5719-llvm-cd8599e8c9774b83160ac1957e139838bf2bed64.tar.gz
bcm5719-llvm-cd8599e8c9774b83160ac1957e139838bf2bed64.zip
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. llvm-svn: 175973
Diffstat (limited to 'clang/lib/Format/TokenAnnotator.cpp')
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp32
1 files changed, 24 insertions, 8 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 80780185c0e..b7f43d52edf 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/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();
OpenPOWER on IntegriCloud