]> granicus.if.org Git - clang/commitdiff
Improve detection preventing certain kind of formatting patterns.
authorDaniel Jasper <djasper@google.com>
Mon, 3 Jun 2013 09:54:46 +0000 (09:54 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 3 Jun 2013 09:54:46 +0000 (09:54 +0000)
An oversight in this detection made clang-format unable to format
the following nicely:
void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaa,
                         bbbbbbbbbbbbbbbbbbbbbbbbbb>(
    cccccccccccccccccccccccccccc);

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

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

index e0142db554b7e012db08e8a27fafa771a20c28ce..aa42bdb643a2bd432dba8a8b9323f2bc4c6e6736 100644 (file)
@@ -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;
   }
index 98a6e07ab281ab5168d1fe766e0bea2fd14efe93..2dea38f790818c4090fdbc8ee2fa10a84551a713 100644 (file)
@@ -2835,6 +2835,9 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) {
   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) {