Author: djasper
Date: Fri Sep 19 03:28:43 2014
New Revision: 218111
URL: http://llvm.org/viewvc/llvm-project?rev=218111&view=rev
Log:
clang-format: Prevent column layout if elements aren't uniform enough.
This patch only considers the difference between the length of the
shortest and longest element, but we might want to look at other
features (token count, etc.) in future.
Before:
std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{
aaaaaaa, aaaaaaaaaa,
aaaaa, aaaaaaaaaaaaaaa,
aaa, aaaaaaaaaa,
a, aaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,
aaaaaaa, a};
After:
std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{
aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,
aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};
Modified:
cfe/trunk/lib/Format/FormatToken.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/FormatToken.cpp
URL:
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.cpp?rev=218111&r1=218110&r2=218111&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.cpp (original)
+++ cfe/trunk/lib/Format/FormatToken.cpp Fri Sep 19 03:28:43 2014
@@ -145,6 +145,9 @@ void CommaSeparatedList::precomputeForma
// trailing comments which are otherwise ignored for column alignment.
SmallVector<unsigned, 8> 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())
@@ -171,6 +174,9 @@ void CommaSeparatedList::precomputeForma
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())
@@ -186,8 +192,10 @@ void CommaSeparatedList::precomputeForma
// 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
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL:
http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=218111&r1=218110&r2=218111&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Sep 19 03:28:43 2014
@@ -5631,15 +5631,11 @@ TEST_F(FormatTest, LayoutCxx11BraceIniti
"std::this_thread::sleep_for(\n"
" std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
ExtraSpaces);
- verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
- " aaaaaaa, aaaaaaaaaa,\n"
- " aaaaa, aaaaaaaaaaaaaaa,\n"
- " aaa, aaaaaaaaaa,\n"
- " a, aaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaa, a\n"
- "};",
- ExtraSpaces);
+ verifyFormat(
+ "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
+ " aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
+ " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits