From 7a474af66601280b22668c9fcfea2b3266c317a0 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Fri, 21 Mar 2014 12:38:57 +0000 Subject: [PATCH] clang-format: Let a trailing comma in braced lists enforce linebreaks. Before: vector x{1, 2, 3, 4, }; After: vector x{ 1, 2, 3, 4, }; This fixes llvm.org/PR18519. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204458 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 8 ++++++++ unittests/Format/FormatTest.cpp | 14 ++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index c3cc000044..efb983e736 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1478,6 +1478,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right) { + const FormatToken &Left = *Right.Previous; if (Right.is(tok::comment)) { return Right.Previous->BlockKind != BK_BracedInit && Right.Previous->Type != TT_CtorInitializerColon && @@ -1514,6 +1515,13 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, Style.Language == FormatStyle::LK_Proto) { // Don't enums onto single lines in protocol buffers. return true; + } else if ((Left.is(tok::l_brace) && Left.MatchingParen && + Left.MatchingParen->Previous && + Left.MatchingParen->Previous->is(tok::comma)) || + (Right.is(tok::r_brace) && Left.is(tok::comma))) { + // If the last token before a '}' is a comma, the intention is to insert a + // line break after it in order to make shuffling around entries easier. + return true; } return false; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index bd926f63a6..e1b72d9a99 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -4901,6 +4901,9 @@ TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { verifyFormat("vector x{1, 2, 3, 4};"); + verifyFormat("vector x{\n" + " 1, 2, 3, 4,\n" + "};"); verifyFormat("vector x{{}, {}, {}, {}};"); verifyFormat("f({1, 2});"); verifyFormat("auto v = Foo{-1};"); @@ -5036,8 +5039,9 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { getLLVMStyleWithColumns(43)); // Trailing commas. - verifyFormat("vector x = {1, 1, 1, 1,\n" - " 1, 1, 1, 1, };", + verifyFormat("vector x = {\n" + " 1, 1, 1, 1, 1, 1, 1, 1,\n" + "};", getLLVMStyleWithColumns(39)); verifyFormat("vector x = {1, 1, 1, 1,\n" " 1, 1, 1, 1, //\n" @@ -6028,10 +6032,12 @@ TEST_F(FormatTest, ObjCDictLiterals) { verifyFormat("@{}"); verifyFormat("@{@\"one\" : @1}"); verifyFormat("return @{@\"one\" : @1;"); - verifyFormat("@{@\"one\" : @1, }"); + verifyFormat("@{@\"one\" : @1}"); verifyFormat("@{@\"one\" : @{@2 : @1}}"); - verifyFormat("@{@\"one\" : @{@2 : @1}, }"); + verifyFormat("@{\n" + " @\"one\" : @{@2 : @1},\n" + "}"); verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); verifyFormat("[self setDict:@{}"); -- 2.40.0