r173792 - Initial support for multiple variable declarations.

Daniel Jasper djasper at google.com
Tue Jan 29 03:21:01 PST 2013


Author: djasper
Date: Tue Jan 29 05:21:01 2013
New Revision: 173792

URL: http://llvm.org/viewvc/llvm-project?rev=173792&view=rev
Log:
Initial support for multiple variable declarations.

Before:
  SomeType aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),
      aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();
After:
  SomeType aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),
           aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();

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=173792&r1=173791&r2=173792&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Jan 29 05:21:01 2013
@@ -366,7 +366,7 @@ public:
     State.Column = FirstIndent;
     State.NextToken = &RootToken;
     State.Stack.push_back(ParenState(FirstIndent + 4, FirstIndent));
-    State.ForLoopVariablePos = 0;
+    State.VariablePos = 0;
     State.LineContainsContinuedForLoopSection = false;
 
     DEBUG({
@@ -504,10 +504,10 @@ private:
     /// \brief The token that needs to be next formatted.
     const AnnotatedToken *NextToken;
 
-    /// \brief The column of the first variable in a for-loop declaration.
+    /// \brief The column of first variable name in a variable declaration.
     ///
-    /// Used to align the second variable if necessary.
-    unsigned ForLoopVariablePos;
+    /// Used to align the further variables if necessary.
+    unsigned VariablePos;
 
     /// \brief \c true if this line contains a continued for-loop section.
     bool LineContainsContinuedForLoopSection;
@@ -522,8 +522,8 @@ private:
         return Other.NextToken > NextToken;
       if (Other.Column != Column)
         return Other.Column > Column;
-      if (Other.ForLoopVariablePos != ForLoopVariablePos)
-        return Other.ForLoopVariablePos < ForLoopVariablePos;
+      if (Other.VariablePos != VariablePos)
+        return Other.VariablePos < VariablePos;
       if (Other.LineContainsContinuedForLoopSection !=
           LineContainsContinuedForLoopSection)
         return LineContainsContinuedForLoopSection;
@@ -566,9 +566,10 @@ private:
                                 State.Stack.back().Indent) + 4;
       } else if (Current.Type == TT_ConditionalExpr) {
         State.Column = State.Stack.back().QuestionColumn;
-      } else if (RootToken.is(tok::kw_for) && ParenLevel == 1 &&
-                 Previous.is(tok::comma)) {
-        State.Column = State.ForLoopVariablePos;
+      } else if (Previous.is(tok::comma) && State.VariablePos != 0 &&
+                 ((RootToken.is(tok::kw_for) && ParenLevel == 1) ||
+                  ParenLevel == 0)) {
+        State.Column = State.VariablePos;
       } else if (State.NextToken->Parent->ClosesTemplateDeclaration ||
                  Current.Type == TT_StartOfName) {
         State.Column = State.Stack[ParenLevel].Indent - 4;
@@ -595,9 +596,9 @@ private:
       if (Current.is(tok::colon) && Current.Type != TT_ConditionalExpr)
         State.Stack[ParenLevel].Indent += 2;
     } else {
-      if (Current.is(tok::equal) && RootToken.is(tok::kw_for))
-        State.ForLoopVariablePos = State.Column -
-                                   Previous.FormatTok.TokenLength;
+      if (Current.is(tok::equal) &&
+          (RootToken.is(tok::kw_for) || ParenLevel == 0))
+        State.VariablePos = State.Column - Previous.FormatTok.TokenLength;
 
       unsigned Spaces = State.NextToken->SpaceRequiredBefore ? 1 : 0;
       if (State.NextToken->Type == TT_LineComment)
@@ -652,7 +653,7 @@ private:
            Previous.Type != TT_TemplateOpener) ||
           !Style.AllowAllParametersOnNextLine)
         State.Stack.back().BreakAfterComma = true;
-      
+
       // Any break on this level means that the parent level has been broken
       // and we need to avoid bin packing there.
       for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
@@ -1008,7 +1009,7 @@ public:
             // FIXME: Do we incorrectly label ":" with this?
             StartsObjCMethodExpr = false;
             Left->Type = TT_Unknown;
-	  }
+          }
           if (StartsObjCMethodExpr)
             objCSelector.markEnd(*CurrentToken);
           Left->MatchingParen = CurrentToken;
@@ -1223,7 +1224,6 @@ public:
         return LT_PreprocessorDirective;
       }
       while (CurrentToken != NULL) {
-        
         if (CurrentToken->is(tok::kw_virtual))
           KeywordVirtualFound = true;
         if (CurrentToken->is(tok::period) || CurrentToken->is(tok::arrow))

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=173792&r1=173791&r2=173792&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Jan 29 05:21:01 2013
@@ -1161,6 +1161,24 @@ TEST_F(FormatTest, BreaksConditionalExpr
                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
 }
 
+TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
+  verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
+               "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
+  verifyFormat("bool a = true, b = false;");
+
+  // FIXME: Indentation looks weird.
+  verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
+               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
+               "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
+               "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
+
+  // FIXME: This is bad as we hide "d".
+  verifyFormat(
+      "bool aaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
+      "                             cccccccccccccccccccccccccccc, d = e && f;");
+
+}
+
 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
   verifyFormat("arr[foo ? bar : baz];");
   verifyFormat("f()[foo ? bar : baz];");





More information about the cfe-commits mailing list