From c86b984ea8ecb3e944dc3de48539f4c1f65851ea Mon Sep 17 00:00:00 2001 From: dim Date: Sun, 18 Jan 2015 16:23:48 +0000 Subject: Vendor import of clang RELEASE_360/rc1 tag r226102 (effectively, 3.6.0 RC1): https://llvm.org/svn/llvm-project/cfe/tags/RELEASE_360/rc1@226102 --- lib/Format/FormatToken.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'lib/Format/FormatToken.cpp') diff --git a/lib/Format/FormatToken.cpp b/lib/Format/FormatToken.cpp index c91d25f..badb3a3 100644 --- a/lib/Format/FormatToken.cpp +++ b/lib/Format/FormatToken.cpp @@ -131,9 +131,15 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { if (!Token->MatchingParen || Token->isNot(tok::l_brace)) return; - // In C++11 braced list style, we should not format in columns unless we allow - // bin-packing of function parameters. - if (Style.Cpp11BracedListStyle && !Style.BinPackParameters) + // In C++11 braced list style, we should not format in columns unless they + // have many items (20 or more) or we allow bin-packing of function + // parameters. + if (Style.Cpp11BracedListStyle && !Style.BinPackParameters && + Commas.size() < 19) + return; + + // Column format doesn't really make sense if we don't align after brackets. + if (!Style.AlignAfterOpenBracket) return; FormatToken *ItemBegin = Token->Next; @@ -143,6 +149,9 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { // trailing comments which are otherwise ignored for column alignment. SmallVector EndOfLineItemLength; + unsigned MinItemLength = Style.ColumnLimit; + unsigned MaxItemLength = 0; + for (unsigned i = 0, e = Commas.size() + 1; i != e; ++i) { // Skip comments on their own line. while (ItemBegin->HasUnescapedNewline && ItemBegin->isTrailingComment()) @@ -169,6 +178,9 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { ItemEnd = Commas[i]; // The comma is counted as part of the item when calculating the length. ItemLengths.push_back(CodePointsBetween(ItemBegin, ItemEnd)); + MinItemLength = std::min(MinItemLength, ItemLengths.back()); + MaxItemLength = std::max(MaxItemLength, ItemLengths.back()); + // Consume trailing comments so the are included in EndOfLineItemLength. if (ItemEnd->Next && !ItemEnd->Next->HasUnescapedNewline && ItemEnd->Next->isTrailingComment()) @@ -184,8 +196,10 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { // 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 (HasNestedBracedList || Commas.size() < 5 || Token->NestingLevel != 0) + // list element per line. If the difference between the shortest and longest + // element is too large, column layout would create too much whitespace. + if (HasNestedBracedList || Commas.size() < 5 || Token->NestingLevel != 0 || + MaxItemLength - MinItemLength > 10) return; // We can never place more than ColumnLimit / 3 items in a row (because of the -- cgit v1.1