From: Daniel Jasper Date: Wed, 13 May 2015 08:16:00 +0000 (+0000) Subject: clang-format: Support column layout with comment after {. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=202c7cf413d2fe00b84d751c988f9e4360748254;p=clang clang-format: Support column layout with comment after {. Before: vector iiiiiiiiiiiiiii = { // 1111111111, 2222222222, 33333333333, 4444444444, // 111111111, 222222222, 3333333333, 444444444, // 11111111, 22222222, 333333333, 44444444}; After: vector iiiiiiiiiiiiiii = { // 1111111111, 2222222222, 33333333333, 4444444444, // 111111111, 222222222, 3333333333, 444444444, // 11111111, 22222222, 333333333, 44444444}; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@237233 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/FormatToken.cpp b/lib/Format/FormatToken.cpp index bbc0fa8b0b..2f0b32971b 100644 --- a/lib/Format/FormatToken.cpp +++ b/lib/Format/FormatToken.cpp @@ -60,13 +60,13 @@ void TokenRole::precomputeFormattingInfos(const FormatToken *Token) {} unsigned CommaSeparatedList::formatAfterToken(LineState &State, ContinuationIndenter *Indenter, bool DryRun) { - if (State.NextToken == nullptr || !State.NextToken->Previous || - !State.NextToken->Previous->Previous) + if (State.NextToken == nullptr || !State.NextToken->Previous) return 0; // Ensure that we start on the opening brace. - const FormatToken *LBrace = State.NextToken->Previous->Previous; - if (LBrace->isNot(tok::l_brace) || LBrace->BlockKind == BK_Block || + const FormatToken *LBrace = + State.NextToken->Previous->getPreviousNonComment(); + if (!LBrace || LBrace->isNot(tok::l_brace) || LBrace->BlockKind == BK_Block || LBrace->Type == TT_DictLiteral || LBrace->Next->Type == TT_DesignatedInitializerPeriod) return 0; @@ -145,6 +145,8 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { return; FormatToken *ItemBegin = Token->Next; + while (ItemBegin->isTrailingComment()) + ItemBegin = ItemBegin->Next; SmallVector MustBreakBeforeItem; // The lengths of an item if it is put at the end of the line. This includes diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 4072fef2e0..371fa0f3d2 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -6368,6 +6368,12 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { " /**/ /**/};", getLLVMStyleWithColumns(39)); + // Trailing comment in the first line. + verifyFormat("vector iiiiiiiiiiiiiii = { //\n" + " 1111111111, 2222222222, 33333333333, 4444444444, //\n" + " 111111111, 222222222, 3333333333, 444444444, //\n" + " 11111111, 22222222, 333333333, 44444444};"); + // With nested lists, we should either format one item per line or all nested // lists one on line. // FIXME: For some nested lists, we can do better.