From 6c9cefd8ba7711a78f45e8eb72d72a89101b4952 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Sat, 23 Nov 2013 10:22:59 +0000 Subject: [PATCH] clang-format: Prefer column layout if possible. Add a severe penalty for not using column layout for braced lists. If there are solutions with column layout, these are generally preferable over bin-packed solutions. Before: std::vector aaaaaaaaaaaaaaaaaaa{ aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a, aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a }; After: std::vector aaaaaaaaaaaaaaaaaaa{ aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a, aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a }; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@195546 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/FormatToken.cpp | 5 ++++- unittests/Format/FormatTest.cpp | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/Format/FormatToken.cpp b/lib/Format/FormatToken.cpp index 8ac704a3bb..bab2425205 100644 --- a/lib/Format/FormatToken.cpp +++ b/lib/Format/FormatToken.cpp @@ -48,8 +48,11 @@ unsigned CommaSeparatedList::format(LineState &State, // Find the best ColumnFormat, i.e. the best number of columns to use. const ColumnFormat *Format = getColumnFormat(RemainingCodePoints); + // If no ColumnFormat can be used, the braced list would generally be + // bin-packed. Add a severe penalty to this so that column layouts are + // prefered if possible. if (!Format) - return 0; + return 10000; // Format the entire list. unsigned Penalty = 0; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 5ddcc3f131..5404c49a01 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -4698,6 +4698,14 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { verifyFormat( "std::this_thread::sleep_for(\n" " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);"); + verifyFormat("std::vector aaaaaaaaaaaaaaaaaaa{\n" + " aaaaaaa, aaaaaaaaaa,\n" + " aaaaa, aaaaaaaaaaaaaaa,\n" + " aaa, aaaaaaaaaa,\n" + " a, aaaaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" + " aaaaaaa, a\n" + "};"); FormatStyle NoSpaces = getLLVMStyle(); NoSpaces.Cpp11BracedListStyle = true; -- 2.40.0