From 399914b7a61a3823cc74ef3e47b1cde42528b9bf Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Fri, 17 May 2013 09:35:01 +0000 Subject: [PATCH] Slightly modify the formatting rules for braced lists. Basically, the new rule is: The opening "{" always has to be on the same line as the first element if the braced list is nested (e.g. in another braced list or in a function). The solution that clang-format produces almost always adheres to this rule anyway and this makes clang-format significantly faster for larger lists. Added a test cases for the only exception I could find (which doesn't seem to be very important at first sight). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182082 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 12 ++++++++++-- unittests/Format/FormatTest.cpp | 12 ++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 6966aabd69..3dd0002abb 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -977,10 +977,18 @@ private: /// \brief Returns \c true, if a line break after \p State is allowed. bool canBreak(const LineState &State) { - if (!State.NextToken->CanBreakBefore && - !(State.NextToken->is(tok::r_brace) && + const AnnotatedToken &Current = *State.NextToken; + const AnnotatedToken &Previous = *Current.Parent; + if (!Current.CanBreakBefore && + !(Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)) return false; + // The opening "{" of a braced list has to be on the same line as the first + // element if it is nested in another braced init list or function call. + if (!Current.MustBreakBefore && Previous.is(tok::l_brace) && + Previous.Parent && + Previous.Parent->isOneOf(tok::l_brace, tok::l_paren, tok::comma)) + return false; return !State.Stack.back().NoLineBreak; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 841ac552a5..e143603f28 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1192,6 +1192,18 @@ TEST_F(FormatTest, StaticInitializers) { "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" " looooooooooooooooooooooooooooooooooongname,\n" " looooooooooooooooooooooooooooooong };"); + // Here, everything other than the "}" would fit on a line. + verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" + " 100000000000000000000000\n" + "};"); + + // FIXME: This would fit into the column limit if we'd fit "{ {" on the first + // line. However, the formatting looks a bit off and this probably doesn't + // happen often in practice. + verifyFormat("static int Variable[1] = {\n" + " { 1000000000000000000000000000000000000 }\n" + "};", + getLLVMStyleWithColumns(40)); } TEST_F(FormatTest, NestedStaticInitializers) { -- 2.40.0