Improve handling of trailing 'const'.

Reduce the preference for breaking before a trailing 'const' according
to review comments on r182362.

llvm-svn: 182455
This commit is contained in:
Daniel Jasper 2013-05-22 08:28:26 +00:00
parent 7ea2a52a0c
commit 13c37b3483
2 changed files with 26 additions and 7 deletions

View File

@ -959,6 +959,10 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
return 150;
}
// Breaking before a trailing 'const' is bad.
if (Left.is(tok::r_paren) && Right.is(tok::kw_const))
return 150;
// In for-loops, prefer breaking at ',' and ';'.
if (Line.First.is(tok::kw_for) && Left.is(tok::equal))
return 4;
@ -1165,6 +1169,11 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
// change the "binding" behavior of a comment.
return false;
// We only break before r_brace if there was a corresponding break before
// the l_brace, which is tracked by BreakBeforeClosingBrace.
if (Right.isOneOf(tok::r_brace, tok::r_paren, tok::greater))
return false;
// Allow breaking after a trailing 'const', e.g. after a method declaration,
// unless it is follow by ';', '{' or '='.
if (Left.is(tok::kw_const) && Left.Parent != NULL &&
@ -1174,10 +1183,6 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
if (Right.is(tok::kw___attribute))
return true;
// We only break before r_brace if there was a corresponding break before
// the l_brace, which is tracked by BreakBeforeClosingBrace.
if (Right.isOneOf(tok::r_brace, tok::r_paren, tok::greater))
return false;
if (Left.is(tok::identifier) && Right.is(tok::string_literal))
return true;
return (Left.isBinaryOperator() && Left.isNot(tok::lessless)) ||

View File

@ -1929,9 +1929,23 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
}
TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
verifyFormat("void someLongFunction(int someLongParameter)\n"
" const;",
getLLVMStyleWithColumns(45));
verifyFormat("void someLongFunction(\n"
" int someLongParameter) const {}",
getLLVMStyleWithColumns(46));
FormatStyle Style = getGoogleStyle();
Style.ColumnLimit = 47;
verifyFormat("void\n"
"someLongFunction(int someLongParameter) const {\n}",
getLLVMStyleWithColumns(47));
verifyFormat("void someLongFunction(\n"
" int someLongParameter) const {}",
Style);
verifyFormat("LoooooongReturnType\n"
"someLoooooooongFunction() const {}",
getLLVMStyleWithColumns(47));
verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
" const {}",
Style);
verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
" LOCKS_EXCLUDED(aaaaaaaaaaaaa);");