From ae8e0d8da9de8eb1cf81fb0e24e9dfda43957aab Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Thu, 17 Apr 2014 11:32:02 +0000 Subject: [PATCH] clang-format: Respect BinPackParameters in Cpp11BracedListStyle. With BinPackParameters=false and Cpp11BracedListStyle=true (i.e. mostly for Chromium): Before: const Aaaaaa aaaaa = {aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk}; After: const Aaaaaa aaaaa = {aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk}; This fixes llvm.org/PR19359. I am not sure we'll want this in all cases in the long run, but I'll guess we'll get feedback on that. llvm-svn: 206458 --- clang/lib/Format/ContinuationIndenter.cpp | 1 + clang/lib/Format/FormatToken.cpp | 5 ++++ clang/unittests/Format/FormatTest.cpp | 32 ++++++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 52ee47987f15..de2047c458f9 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -725,6 +725,7 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, AvoidBinPacking = Current.BlockKind == BK_Block || Current.Type == TT_ArrayInitializerLSquare || Current.Type == TT_DictLiteral || + !Style.BinPackParameters || (NextNoComment && NextNoComment->Type == TT_DesignatedInitializerPeriod); } else { diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index c147dbb6b1b6..a7c003bc18a6 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -132,6 +132,11 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { if (!Token->MatchingParen || Token->isNot(tok::l_brace)) return; + // In C++11 braced list style, we should not format in columns unless we allow + // bin-packing of function parameters. + if (Style.Cpp11BracedListStyle && !Style.BinPackParameters) + return; + FormatToken *ItemBegin = Token->Next; SmallVector MustBreakBeforeItem; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index ee9afe40c934..d3fbb02e5d4c 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -5027,7 +5027,7 @@ TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { verifyFormat("return (a)(b) {1, 2, 3};"); } -TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { +TEST_F(FormatTest, LayoutCxx11BraceInitializers) { verifyFormat("vector x{1, 2, 3, 4};"); verifyFormat("vector x{\n" " 1, 2, 3, 4,\n" @@ -5049,6 +5049,36 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { "};"); verifyFormat("vector foo = {::SomeGlobalFunction()};"); + // In combination with BinPackParameters = false. + FormatStyle NoBinPacking = getLLVMStyle(); + NoBinPacking.BinPackParameters = false; + verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" + " bbbbb,\n" + " ccccc,\n" + " ddddd,\n" + " eeeee,\n" + " ffffff,\n" + " ggggg,\n" + " hhhhhh,\n" + " iiiiii,\n" + " jjjjjj,\n" + " kkkkkk};", + NoBinPacking); + verifyFormat("const Aaaaaa aaaaa = {\n" + " aaaaa,\n" + " bbbbb,\n" + " ccccc,\n" + " ddddd,\n" + " eeeee,\n" + " ffffff,\n" + " ggggg,\n" + " hhhhhh,\n" + " iiiiii,\n" + " jjjjjj,\n" + " kkkkkk,\n" + "};", + NoBinPacking); + // FIXME: The alignment of these trailing comments might be bad. Then again, // this might be utterly useless in real code. verifyFormat("Constructor::Constructor()\n"