r201138 - clang-format: Fix alignment of comments inside statements.

Daniel Jasper djasper at google.com
Tue Feb 11 02:08:12 PST 2014


Author: djasper
Date: Tue Feb 11 04:08:11 2014
New Revision: 201138

URL: http://llvm.org/viewvc/llvm-project?rev=201138&view=rev
Log:
clang-format: Fix alignment of comments inside statements.

Before:
  auto result = SomeObject
                // Calling someFunction on SomeObject
                    .someFunction();

After:
  auto result = SomeObject
                    // Calling someFunction on SomeObject
                    .someFunction();

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=201138&r1=201137&r2=201138&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Tue Feb 11 04:08:11 2014
@@ -335,8 +335,10 @@ unsigned ContinuationIndenter::addTokenO
   // breaks are chosen.
   unsigned Penalty = 0;
 
-  const FormatToken *PreviousNonComment =
-      State.NextToken->getPreviousNonComment();
+  const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
+  const FormatToken *NextNonComment = Previous.getNextNonComment();
+  if (!NextNonComment)
+    NextNonComment = &Current;
   // The first line break on any ParenLevel causes an extra penalty in order
   // prefer similar line breaks.
   if (!State.Stack.back().ContainsLineBreak)
@@ -345,15 +347,18 @@ unsigned ContinuationIndenter::addTokenO
 
   Penalty += State.NextToken->SplitPenalty;
 
+
   // Breaking before the first "<<" is generally not desirable if the LHS is
   // short. Also always add the penalty if the LHS is split over mutliple lines
   // to avoid unncessary line breaks that just work around this penalty.
-  if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 &&
+  if (NextNonComment->is(tok::lessless) &&
+      State.Stack.back().FirstLessLess == 0 &&
       (State.Column <= Style.ColumnLimit / 3 ||
        State.Stack.back().BreakBeforeParameter))
     Penalty += Style.PenaltyBreakFirstLessLess;
 
-  if (Current.is(tok::l_brace) && Current.BlockKind == BK_Block) {
+  if (NextNonComment->is(tok::l_brace) &&
+      NextNonComment->BlockKind == BK_Block) {
     State.Column =
         State.ParenLevel == 0 ? State.FirstIndent : State.Stack.back().Indent;
   } else if (Current.isOneOf(tok::r_brace, tok::r_square)) {
@@ -363,13 +368,14 @@ unsigned ContinuationIndenter::addTokenO
       State.Column = State.Stack[State.Stack.size() - 2].LastSpace;
     else
       State.Column = State.FirstIndent;
-  } else if (Current.isStringLiteral() && State.StartOfStringLiteral != 0) {
+  } else if (NextNonComment->isStringLiteral() &&
+             State.StartOfStringLiteral != 0) {
     State.Column = State.StartOfStringLiteral;
     State.Stack.back().BreakBeforeParameter = true;
-  } else if (Current.is(tok::lessless) &&
+  } else if (NextNonComment->is(tok::lessless) &&
              State.Stack.back().FirstLessLess != 0) {
     State.Column = State.Stack.back().FirstLessLess;
-  } else if (Current.isMemberAccess()) {
+  } else if (NextNonComment->isMemberAccess()) {
     if (State.Stack.back().CallContinuation == 0) {
       State.Column = ContinuationIndent;
       State.Stack.back().CallContinuation = State.Column;
@@ -377,7 +383,7 @@ unsigned ContinuationIndenter::addTokenO
       State.Column = State.Stack.back().CallContinuation;
     }
   } else if (State.Stack.back().QuestionColumn != 0 &&
-             (Current.Type == TT_ConditionalExpr ||
+             (NextNonComment->Type == TT_ConditionalExpr ||
               Previous.Type == TT_ConditionalExpr)) {
     State.Column = State.Stack.back().QuestionColumn;
   } else if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) {
@@ -385,37 +391,38 @@ unsigned ContinuationIndenter::addTokenO
   } else if ((PreviousNonComment &&
               (PreviousNonComment->ClosesTemplateDeclaration ||
                PreviousNonComment->Type == TT_AttributeParen)) ||
-             ((Current.Type == TT_StartOfName ||
-               Current.is(tok::kw_operator)) &&
+             ((NextNonComment->Type == TT_StartOfName ||
+               NextNonComment->is(tok::kw_operator)) &&
               State.ParenLevel == 0 &&
               (!Style.IndentFunctionDeclarationAfterType ||
                State.Line->StartsDefinition))) {
     State.Column =
         std::max(State.Stack.back().LastSpace, State.Stack.back().Indent);
-  } else if (Current.Type == TT_ObjCSelectorName) {
+  } else if (NextNonComment->Type == TT_ObjCSelectorName) {
     if (!State.Stack.back().ObjCSelectorNameFound) {
-      if (Current.LongestObjCSelectorName == 0) {
+      if (NextNonComment->LongestObjCSelectorName == 0) {
         State.Column = State.Stack.back().Indent;
         State.Stack.back().AlignColons = false;
       } else {
         State.Stack.back().ColonPos =
-            State.Stack.back().Indent + Current.LongestObjCSelectorName;
-        State.Column = State.Stack.back().ColonPos - Current.ColumnWidth;
+            State.Stack.back().Indent + NextNonComment->LongestObjCSelectorName;
+        State.Column =
+            State.Stack.back().ColonPos - NextNonComment->ColumnWidth;
       }
     } else if (!State.Stack.back().AlignColons) {
       State.Column = State.Stack.back().Indent;
-    } else if (State.Stack.back().ColonPos > Current.ColumnWidth) {
-      State.Column = State.Stack.back().ColonPos - Current.ColumnWidth;
+    } else if (State.Stack.back().ColonPos > NextNonComment->ColumnWidth) {
+      State.Column = State.Stack.back().ColonPos - NextNonComment->ColumnWidth;
     } else {
       State.Column = State.Stack.back().Indent;
-      State.Stack.back().ColonPos = State.Column + Current.ColumnWidth;
+      State.Stack.back().ColonPos = State.Column + NextNonComment->ColumnWidth;
     }
-  } else if (Current.Type == TT_ArraySubscriptLSquare) {
+  } else if (NextNonComment->Type == TT_ArraySubscriptLSquare) {
     if (State.Stack.back().StartOfArraySubscripts != 0)
       State.Column = State.Stack.back().StartOfArraySubscripts;
     else
       State.Column = ContinuationIndent;
-  } else if (Current.Type == TT_StartOfName ||
+  } else if (NextNonComment->Type == TT_StartOfName ||
              Previous.isOneOf(tok::coloncolon, tok::equal)) {
     State.Column = ContinuationIndent;
   } else if (PreviousNonComment &&
@@ -432,9 +439,9 @@ unsigned ContinuationIndenter::addTokenO
     // when we consume all of the "}"'s FakeRParens at the "{".
     if (State.Stack.size() > 1)
       State.Stack[State.Stack.size() - 2].LastSpace = ContinuationIndent;
-  } else if (Current.Type == TT_CtorInitializerColon) {
+  } else if (NextNonComment->Type == TT_CtorInitializerColon) {
     State.Column = State.FirstIndent + Style.ConstructorInitializerIndentWidth;
-  } else if (Current.Type == TT_CtorInitializerComma) {
+  } else if (NextNonComment->Type == TT_CtorInitializerComma) {
     State.Column = State.Stack.back().Indent;
   } else {
     State.Column = State.Stack.back().Indent;
@@ -451,7 +458,7 @@ unsigned ContinuationIndenter::addTokenO
     State.Stack.back().BreakBeforeParameter = false;
   if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0)
     State.Stack.back().BreakBeforeParameter = false;
-  if (Current.is(tok::question) ||
+  if (NextNonComment->is(tok::question) ||
       (PreviousNonComment && PreviousNonComment->is(tok::question)))
     State.Stack.back().BreakBeforeParameter = true;
 

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=201138&r1=201137&r2=201138&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Feb 11 04:08:11 2014
@@ -581,7 +581,7 @@ private:
     if (I[1]->InPPDirective != (*I)->InPPDirective ||
         (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline))
       return 0;
-    Limit = LimitConsideringMacros(I + 1, E, Limit);
+    Limit = limitConsideringMacros(I + 1, E, Limit);
     AnnotatedLine &Line = **I;
     if (Line.Last->isNot(tok::r_paren))
       return 0;
@@ -625,7 +625,7 @@ private:
       // Check that we still have three lines and they fit into the limit.
       if (I + 2 == E || I[2]->Type == LT_Invalid)
         return 0;
-      Limit = LimitConsideringMacros(I + 2, E, Limit);
+      Limit = limitConsideringMacros(I + 2, E, Limit);
 
       if (!nextTwoLinesFitInto(I, Limit))
         return 0;
@@ -654,7 +654,7 @@ private:
   /// Returns the modified column limit for \p I if it is inside a macro and
   /// needs a trailing '\'.
   unsigned
-  LimitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
+  limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
                          SmallVectorImpl<AnnotatedLine *>::const_iterator E,
                          unsigned Limit) {
     if (I[0]->InPPDirective && I + 1 != E &&

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=201138&r1=201137&r2=201138&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Feb 11 04:08:11 2014
@@ -603,6 +603,9 @@ TEST_F(FormatTest, UnderstandsSingleLine
   verifyFormat("SomeObject\n"
                "    // Calling someFunction on SomeObject\n"
                "    .someFunction();");
+  verifyFormat("auto result = SomeObject\n"
+               "                  // Calling someFunction on SomeObject\n"
+               "                  .someFunction();");
   verifyFormat("void f(int i,  // some comment (probably for i)\n"
                "       int j,  // some comment (probably for j)\n"
                "       int k); // some comment (probably for k)");





More information about the cfe-commits mailing list