]> granicus.if.org Git - clang/commitdiff
clang-format: Support assignments as conditional operands.
authorDaniel Jasper <djasper@google.com>
Fri, 14 Nov 2014 13:03:40 +0000 (13:03 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 14 Nov 2014 13:03:40 +0000 (13:03 +0000)
Before:
  return a != b
             // comment
             ? a
             : a = a != b
                   // comment
         ? a =
               b : a;
After:
  return a != b
             // comment
             ? a
             : a = a != b
                       // comment
                       ? a = b
                       : a;

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@221987 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index cb945c6161b5f6029d0629835be37aa30dba9b96..5234de2bb7d78d352fd165af9367335ed2fd9f9a 100644 (file)
@@ -1120,7 +1120,9 @@ public:
       // At the end of the line or when an operator with higher precedence is
       // found, insert fake parenthesis and return.
       if (!Current || (Current->closesScope() && Current->MatchingParen) ||
-          (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) {
+          (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
+          (CurrentPrecedence == prec::Conditional &&
+           Precedence == prec::Assignment && Current->is(tok::colon))) {
         if (LatestOperator) {
           LatestOperator->LastOperator = true;
           if (Precedence == PrecedenceArrowAndPeriod) {
@@ -1220,11 +1222,11 @@ private:
     if (!Current || !Current->is(tok::question))
       return;
     next();
-    parseConditionalExpr();
+    parse(prec::Assignment);
     if (!Current || Current->Type != TT_ConditionalExpr)
       return;
     next();
-    parseConditionalExpr();
+    parse(prec::Assignment);
     addFakeParenthesis(Start, prec::Conditional);
   }
 
index d2e577aacbd023f2c0f2d90d6bbf9a73d0c4f213..0510871a053a45af1fd691be0cf2a511e8a111e3 100644 (file)
@@ -4132,6 +4132,26 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
                "             ccccccccccccccccccccccccccccccccccccccc\n"
                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
+
+  // Assignments in conditional expressions. Apparently not uncommon :-(.
+  verifyFormat("return a != b\n"
+               "           // comment\n"
+               "           ? a = b\n"
+               "           : a = b;");
+  verifyFormat("return a != b\n"
+               "           // comment\n"
+               "           ? a = a != b\n"
+               "                     // comment\n"
+               "                     ? a = b\n"
+               "                     : a\n"
+               "           : a;\n");
+  verifyFormat("return a != b\n"
+               "           // comment\n"
+               "           ? a\n"
+               "           : a = a != b\n"
+               "                     // comment\n"
+               "                     ? a = b\n"
+               "                     : a;");
 }
 
 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {