From: Daniel Jasper Date: Mon, 3 Jun 2013 09:54:46 +0000 (+0000) Subject: Improve detection preventing certain kind of formatting patterns. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ce912f45660613c1bc49696eb07ff7f8aecffe18;p=clang Improve detection preventing certain kind of formatting patterns. An oversight in this detection made clang-format unable to format the following nicely: void aaaaaaaaaaaaaaaaaaa( cccccccccccccccccccccccccccc); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@183097 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index e0142db554..aa42bdb643 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -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; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 98a6e07ab2..2dea38f790 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -2835,6 +2835,9 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) { verifyFormat("template \nclass C {};", AlwaysBreak); verifyFormat("template \nvoid f();", AlwaysBreak); verifyFormat("template \nvoid f() {}", AlwaysBreak); + verifyFormat("void aaaaaaaaaaaaaaaaaaa(\n" + " ccccccccccccccccccccccccccccccccccccccccccccccc);"); } TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {