From bfa38f08f23a8984887fda3e14bfa7660a14decd Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 9 Apr 2014 09:53:23 +0000 Subject: [PATCH] clang-format: Recognize lists ending in trailing commas correctly. Previously, this did not look through trailing comments leading to a few formatting oddities. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@205843 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 18 +++++++++++------- unittests/Format/FormatTest.cpp | 11 ++++++----- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index a7536d2d24..03921b04b1 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1546,14 +1546,18 @@ 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; } + + // 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. + const FormatToken *BeforeClosingBrace = nullptr; + if (Left.is(tok::l_brace) && Left.MatchingParen) + BeforeClosingBrace = Left.MatchingParen->getPreviousNonComment(); + else if (Right.is(tok::r_brace)) + BeforeClosingBrace = Right.getPreviousNonComment(); + if (BeforeClosingBrace && BeforeClosingBrace->is(tok::comma)) + return true; + return false; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 975f9c6939..5adc0d5725 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -5129,13 +5129,14 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { " 1, 1, 1, 1, 1, 1, 1, 1,\n" "};", getLLVMStyleWithColumns(39)); - verifyFormat("vector x = {1, 1, 1, 1,\n" - " 1, 1, 1, 1, //\n" + 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" - " /**/ /**/};", + verifyFormat("vector x = {\n" + " 1, 1, 1, 1, 1, 1, 1, 1,\n" + " /**/ /**/\n" + "};", getLLVMStyleWithColumns(39)); verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" " {aaaaaaaaaaaaaaaaaaa},\n" -- 2.40.0