From aebf04a1ce80d405a7cfd2fe59285ee38b0cc66b Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 11 Nov 2014 19:34:57 +0000 Subject: [PATCH] clang-format: Preserve trailing-comma logic even with comments. Before: vector SomeVector = {// aaa 1, 2, }; After: vector SomeVector = { // aaa 1, 2, }; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@221699 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 29 +++++++++++++++-------------- unittests/Format/FormatTest.cpp | 13 ++++++++++--- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index d3d4bb14fd..715cbf0a48 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1775,9 +1775,22 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Left = *Right.Previous; if (Right.NewlinesBefore > 1) return true; + + // If the last token before a '}' is a comma or a trailing comment, the + // intention is to insert a line break after it in order to make shuffling + // around entries easier. + const FormatToken *BeforeClosingBrace = nullptr; + if (Left.is(tok::l_brace) && Left.BlockKind != BK_Block && Left.MatchingParen) + BeforeClosingBrace = Left.MatchingParen->Previous; + else if (Right.is(tok::r_brace) && Right.BlockKind != BK_Block) + BeforeClosingBrace = &Left; + if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) || + BeforeClosingBrace->isTrailingComment())) + return true; + if (Right.is(tok::comment)) { - return Right.Previous->BlockKind != BK_BracedInit && - Right.Previous->Type != TT_CtorInitializerColon && + return Left.BlockKind != BK_BracedInit && + Left.Type != TT_CtorInitializerColon && (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline); } else if (Right.Previous->isTrailingComment() || (Right.isStringLiteral() && Right.Previous->isStringLiteral())) { @@ -1826,18 +1839,6 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, return true; } - // If the last token before a '}' is a comma or a trailing comment, the - // intention is to insert a line break after it in order to make shuffling - // around entries easier. - const FormatToken *BeforeClosingBrace = nullptr; - if (Left.is(tok::l_brace) && Left.MatchingParen) - BeforeClosingBrace = Left.MatchingParen->Previous; - else if (Right.is(tok::r_brace)) - BeforeClosingBrace = Right.Previous; - if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) || - BeforeClosingBrace->isTrailingComment())) - return true; - if (Style.Language == FormatStyle::LK_JavaScript) { // FIXME: This might apply to other languages and token kinds. if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous && diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 3485392d4f..1f95a89893 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -5645,9 +5645,9 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { // FIXME: The alignment of these trailing comments might be bad. Then again, // this might be utterly useless in real code. verifyFormat("Constructor::Constructor()\n" - " : some_value{ //\n" - " aaaaaaa //\n" - " } {}"); + " : some_value{ //\n" + " aaaaaaa, //\n" + " bbbbbbb} {}"); // In braced lists, the first comment is always assumed to belong to the // first element. Thus, it can be moved to the next or previous line as @@ -5671,6 +5671,13 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { " // Second element:\n" " 2};", getLLVMStyleWithColumns(30))); + // A trailing comma should still lead to an enforced line break. + EXPECT_EQ("vector SomeVector = {\n" + " // aaa\n" + " 1, 2,\n" + "};", + format("vector SomeVector = { // aaa\n" + " 1, 2, };")); FormatStyle ExtraSpaces = getLLVMStyle(); ExtraSpaces.Cpp11BracedListStyle = false; -- 2.40.0