Fix a bug that would lead to bad line break decisions in for loops.

Before:
    for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaa
             .aaaaaaaaaaaaaaaa;
         aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {}

After:
    for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =
             aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaa;
         aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {}

llvm-svn: 173695
This commit is contained in:
Daniel Jasper 2013-01-28 17:30:17 +00:00
parent 6cff9deb16
commit 48c62f980f
2 changed files with 18 additions and 6 deletions

View File

@ -723,6 +723,12 @@ private:
if (Left.Type == TT_RangeBasedForLoopColon)
return 5;
if (Right.is(tok::arrow) || Right.is(tok::period)) {
if (Left.is(tok::r_paren) && Line.Type == LT_BuilderTypeCall)
return 5; // Should be smaller than breaking at a nested comma.
return 150;
}
// In for-loops, prefer breaking at ',' and ';'.
if (RootToken.is(tok::kw_for) &&
(Left.isNot(tok::comma) && Left.isNot(tok::semi)))
@ -753,12 +759,6 @@ private:
if (Level != prec::Unknown)
return Level;
if (Right.is(tok::arrow) || Right.is(tok::period)) {
if (Left.is(tok::r_paren) && Line.Type == LT_BuilderTypeCall)
return 5; // Should be smaller than breaking at a nested comma.
return 150;
}
return 3;
}

View File

@ -239,6 +239,18 @@ TEST_F(FormatTest, FormatsForLoop) {
verifyFormat(
"for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
" ++IIIII) {\n}");
verifyFormat(
"for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
" aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
" aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
// FIXME: Not sure whether we want extra identation in line 3 here:
verifyFormat(
"for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
" ++aaaaaaaaaaa) {\n}");
}
TEST_F(FormatTest, RangeBasedForLoops) {