<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Nov 20, 2013 at 3:58 PM, Daniel Jasper <span dir="ltr"><<a href="mailto:djasper@google.com" target="_blank">djasper@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div class="h5">On Wed, Nov 20, 2013 at 3:20 AM, Manuel Klimek <span dir="ltr"><<a href="mailto:klimek@google.com" target="_blank">klimek@google.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Author: klimek<br>
Date: Wed Nov 20 05:20:32 2013<br>
New Revision: 195240<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=195240&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=195240&view=rev</a><br>
Log:<br>
Fix bug where optimization would lead to strange line breaks.<br>
<br>
Before:<br>
  void f() {<br>
    CHECK_EQ(aaaa, (<br>
                       *bbbbbbbbb)->cccccc)<br>
        << "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq";<br>
  }<br>
<br>
After:<br>
  void f() {<br>
    CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)<br>
        << "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq";<br>
  }<br>
<br>
Modified:<br>
    cfe/trunk/lib/Format/ContinuationIndenter.cpp<br>
    cfe/trunk/lib/Format/ContinuationIndenter.h<br>
    cfe/trunk/unittests/Format/FormatTest.cpp<br>
<br>
Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=195240&r1=195239&r2=195240&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=195240&r1=195239&r2=195240&view=diff</a><br>


==============================================================================<br>
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)<br>
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Wed Nov 20 05:20:32 2013<br>
@@ -224,13 +224,14 @@ unsigned ContinuationIndenter::addTokenT<br>
   if (Newline)<br>
     Penalty = addTokenOnNewLine(State, DryRun);<br>
   else<br>
-    addTokenOnCurrentLine(State, DryRun, ExtraSpaces);<br>
+    Penalty = addTokenOnCurrentLine(State, DryRun, ExtraSpaces);<br>
<br>
   return moveStateToNextToken(State, DryRun, Newline) + Penalty;<br>
 }<br>
<br>
-void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,<br>
-                                                 unsigned ExtraSpaces) {<br>
+unsigned ContinuationIndenter::addTokenOnCurrentLine(LineState &State,<br>
+                                                     bool DryRun,<br>
+                                                     unsigned ExtraSpaces) {<br>
   FormatToken &Current = *State.NextToken;<br>
   const FormatToken &Previous = *State.NextToken->Previous;<br>
   if (Current.is(tok::equal) &&<br>
@@ -249,6 +250,15 @@ void ContinuationIndenter::addTokenOnCur<br>
       State.Stack.back().LastSpace = State.Stack.back().VariablePos;<br>
   }<br>
<br>
+  unsigned Penalty = 0;<br>
+  // A break before a "<<" will get Style.PenaltyBreakFirstLessLess, so a<br>
+  // continuation with "<<" has a smaller penalty in general.<br>
+  // If the LHS is long, we don't want to penalize the break though, so we<br>
+  // also add Style.PenaltyBreakFirstLessLess.<br></blockquote><div><br></div></div></div><div>The comment is significantly harder to understand than the code itself (I only understood it after I read the code).</div><div class="im">
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

+  if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 &&<br>
+      State.Column > Style.ColumnLimit / 2)<br>
+    Penalty += Style.PenaltyBreakFirstLessLess;<br></blockquote><div><br></div></div><div>I don't like this approach as the logic behind it is very convoluted and now spread over multiple code sites (in addition to addTokenOnNewLine and addTokenOnCurrentLine it also relies on mustBreak for correct behavior). Also adding a PenaltyBREAK... when not actually breaking seems bad to me.</div>
<div class="im">
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
+<br>
   unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;<br>
<br>
   if (!DryRun)<br>
@@ -307,6 +317,7 @@ void ContinuationIndenter::addTokenOnCur<br>
         State.Stack[State.Stack.size() - 2].CallContinuation == 0)<br>
       State.Stack.back().LastSpace = State.Column;<br>
   }<br>
+  return Penalty;<br>
 }<br>
<br>
 unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,<br>
@@ -332,9 +343,8 @@ unsigned ContinuationIndenter::addTokenO<br>
   Penalty += State.NextToken->SplitPenalty;<br>
<br>
   // Breaking before the first "<<" is generally not desirable if the LHS is<br>
-  // short.<br>
-  if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 &&<br>
-      State.Column <= Style.ColumnLimit / 2)<br>
+  // short (not breaking with a long LHS is penalized in addTokenOnCurrentLine).<br>
+  if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)<br></blockquote><div><br></div></div><div>Simply adding the penalty whenever the LHS broken should do almost the same thing. Did that instead in r195253.</div>
<div><div class="h5">
<div></div></div></div></div></div></div></blockquote><div><br></div><div>I think that solution is worse, as it still doesn't get rid of the very subtle coupling between the previous line's indent and the penalty where the previous line shouldn't matter (in this case we're breaking, after all). It definitely was very confusing to me - it took me forever to understand what the current code was doing, and what it was actually trying to do.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

     Penalty += Style.PenaltyBreakFirstLessLess;<br>
<br>
   if (Current.is(tok::l_brace) && Current.BlockKind == BK_Block) {<br>
<br>
Modified: cfe/trunk/lib/Format/ContinuationIndenter.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.h?rev=195240&r1=195239&r2=195240&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.h?rev=195240&r1=195239&r2=195240&view=diff</a><br>


==============================================================================<br>
--- cfe/trunk/lib/Format/ContinuationIndenter.h (original)<br>
+++ cfe/trunk/lib/Format/ContinuationIndenter.h Wed Nov 20 05:20:32 2013<br>
@@ -91,8 +91,8 @@ private:<br>
   ///<br>
   /// If \p DryRun is \c false, also creates and stores the required<br>
   /// \c Replacement.<br>
-  void addTokenOnCurrentLine(LineState &State, bool DryRun,<br>
-                             unsigned ExtraSpaces);<br>
+  unsigned addTokenOnCurrentLine(LineState &State, bool DryRun,<br>
+                                 unsigned ExtraSpaces);<br>
<br>
   /// \brief Appends the next token to \p State and updates information<br>
   /// necessary for indentation.<br>
<br>
Modified: cfe/trunk/unittests/Format/FormatTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=195240&r1=195239&r2=195240&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=195240&r1=195239&r2=195240&view=diff</a><br>


==============================================================================<br>
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)<br>
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Nov 20 05:20:32 2013<br>
@@ -3744,6 +3744,11 @@ TEST_F(FormatTest, AlignsPipes) {<br>
   EXPECT_EQ("llvm::errs() << \"\n"<br>
             "             << a;",<br>
             format("llvm::errs() << \"\n<<a;"));<br>
+<br>
+  verifyFormat("void f() {\n"<br>
+               "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"<br>
+               "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"<br>
+               "}");<br>
 }<br>
<br>
 TEST_F(FormatTest, UnderstandsEquals) {<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu" target="_blank">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div></div></div><br></div></div>
</blockquote></div><br></div></div>