From: Daniel Jasper Date: Fri, 15 May 2015 09:41:59 +0000 (+0000) Subject: clang-format: Don't use column layout in lists that have separating X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b5236493da4bf9ba91101ffabb26c11ff38c3950;p=clang clang-format: Don't use column layout in lists that have separating comments. At some point, we might to want to a layout with a different number of columns instead, but at the moment, this causes more confusion than it's worth. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@237427 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/FormatToken.cpp b/lib/Format/FormatToken.cpp index 2f0b32971b..42036666b0 100644 --- a/lib/Format/FormatToken.cpp +++ b/lib/Format/FormatToken.cpp @@ -153,10 +153,13 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { // trailing comments which are otherwise ignored for column alignment. SmallVector EndOfLineItemLength; + bool HasSeparatingComment = false; for (unsigned i = 0, e = Commas.size() + 1; i != e; ++i) { // Skip comments on their own line. - while (ItemBegin->HasUnescapedNewline && ItemBegin->isTrailingComment()) + while (ItemBegin->HasUnescapedNewline && ItemBegin->isTrailingComment()) { ItemBegin = ItemBegin->Next; + HasSeparatingComment = i > 0; + } MustBreakBeforeItem.push_back(ItemBegin->MustBreakBefore); if (ItemBegin->is(tok::l_brace)) @@ -193,10 +196,9 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { ItemBegin = ItemEnd->Next; } - // If this doesn't have a nested list, we require at least 6 elements in order - // create a column layout. If it has a nested list, column layout ensures one - // list element per line. - if (Commas.size() < 5 || Token->NestingLevel != 0) + // Don't use column layout for nested lists, lists with few elements and in + // presence of separating comments. + if (Token->NestingLevel != 0 || Commas.size() < 5 || HasSeparatingComment) return; // We can never place more than ColumnLimit / 3 items in a row (because of the diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 9661919f51..ecc1a7e134 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -6313,11 +6313,9 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" " 1, 22, 333, 4444, 55555, 666666, 7777777};"); - verifyFormat("vector x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " // line comment\n" + verifyFormat("vector x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" - " 1, 22, 333, 4444, 55555,\n" - " // line comment\n" + " 1, 22, 333, 4444, 55555, //\n" " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" " 1, 22, 333, 4444, 55555, 666666, 7777777};"); verifyFormat( @@ -6331,6 +6329,14 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); + verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" + " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" + " // Separating comment.\n" + " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); + verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" + " // Leading comment\n" + " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" + " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); verifyFormat("vector x = {1, 1, 1, 1,\n" " 1, 1, 1, 1};", getLLVMStyleWithColumns(39));