r183097 - Improve detection preventing certain kind of formatting patterns.

Daniel Jasper djasper at google.com
Mon Jun 3 02:54:46 PDT 2013


Author: djasper
Date: Mon Jun  3 04:54:46 2013
New Revision: 183097

URL: http://llvm.org/viewvc/llvm-project?rev=183097&view=rev
Log:
Improve detection preventing certain kind of formatting patterns.

An oversight in this detection made clang-format unable to format
the following nicely:
void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaa,
                         bbbbbbbbbbbbbbbbbbbbbbbbbb>(
    cccccccccccccccccccccccccccc);

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=183097&r1=183096&r2=183097&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Jun  3 04:54:46 2013
@@ -261,7 +261,7 @@ public:
     State.ParenLevel = 0;
     State.StartOfStringLiteral = 0;
     State.StartOfLineLevel = State.ParenLevel;
-    State.LowestLevelOnLine = State.ParenLevel;
+    State.LowestCallLevel = State.ParenLevel;
     State.IgnoreStackForComparison = false;
 
     // The first token has already been indented and thus consumed.
@@ -417,8 +417,8 @@ private:
     /// \brief The \c ParenLevel at the start of this line.
     unsigned StartOfLineLevel;
 
-    /// \brief The lowest \c ParenLevel on the current line.
-    unsigned LowestLevelOnLine;
+    /// \brief The lowest \c ParenLevel of "." or "->" on the current line.
+    unsigned LowestCallLevel;
 
     /// \brief The start column of the string literal, if we're in a string
     /// literal sequence, 0 otherwise.
@@ -456,8 +456,8 @@ private:
         return ParenLevel < Other.ParenLevel;
       if (StartOfLineLevel != Other.StartOfLineLevel)
         return StartOfLineLevel < Other.StartOfLineLevel;
-      if (LowestLevelOnLine != Other.LowestLevelOnLine)
-        return LowestLevelOnLine < Other.LowestLevelOnLine;
+      if (LowestCallLevel != Other.LowestCallLevel)
+        return LowestCallLevel < Other.LowestCallLevel;
       if (StartOfStringLiteral != Other.StartOfStringLiteral)
         return StartOfStringLiteral < Other.StartOfStringLiteral;
       if (IgnoreStackForComparison || Other.IgnoreStackForComparison)
@@ -562,7 +562,7 @@ private:
           Current.Type != TT_DesignatedInitializerPeriod)
         State.Stack.back().LastSpace += Current.TokenLength;
       State.StartOfLineLevel = State.ParenLevel;
-      State.LowestLevelOnLine = State.ParenLevel;
+      State.LowestCallLevel = State.ParenLevel;
 
       // Any break on this level means that the parent level has been broken
       // and we need to avoid bin packing there.
@@ -667,10 +667,12 @@ private:
       State.Stack.back().FirstLessLess = State.Column;
     if (Current.is(tok::question))
       State.Stack.back().QuestionColumn = State.Column;
-    if (Current.isOneOf(tok::period, tok::arrow) &&
-        Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
-      State.Stack.back().StartOfFunctionCall =
-          Current.LastInChainOfCalls ? 0 : State.Column + Current.TokenLength;
+    if (Current.isOneOf(tok::period, tok::arrow)) {
+      State.LowestCallLevel = std::min(State.LowestCallLevel, State.ParenLevel);
+      if (Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
+        State.Stack.back().StartOfFunctionCall =
+            Current.LastInChainOfCalls ? 0 : State.Column + Current.TokenLength;
+    }
     if (Current.Type == TT_CtorInitializerColon) {
       // Indent 2 from the column, so:
       // SomeClass::SomeClass()
@@ -762,8 +764,6 @@ private:
       State.Stack.pop_back();
       --State.ParenLevel;
     }
-    State.LowestLevelOnLine =
-        std::min(State.LowestLevelOnLine, State.ParenLevel);
 
     // Remove scopes created by fake parenthesis.
     for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
@@ -1013,8 +1013,7 @@ private:
     //   SomeParameter, OtherParameter).DoSomething(
     //   ...
     // As they hide "DoSomething" and are generally bad for readability.
-    if (Previous.opensScope() &&
-        State.LowestLevelOnLine < State.StartOfLineLevel)
+    if (Previous.opensScope() && State.LowestCallLevel < State.StartOfLineLevel)
       return false;
     return !State.Stack.back().NoLineBreak;
   }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=183097&r1=183096&r2=183097&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jun  3 04:54:46 2013
@@ -2835,6 +2835,9 @@ TEST_F(FormatTest, WrapsTemplateDeclarat
   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
+  verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+               "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
+               "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
 }
 
 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {





More information about the cfe-commits mailing list