clang-format: Avoid line breaks before the first <<.

This puts a slight penalty on the linebreak before the first "<<", so
that clang-format generally tries to keep things on the first line.

User feedback has shown that this is generally desirable.

Before:
  llvm::outs()
      << "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =" << aaaaaaaaaaaaaaaaaaaaaaaaaaa;

After:
  llvm::outs() << "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ="
               << aaaaaaaaaaaaaaaaaaaaaaaaaaa;

llvm-svn: 186115
This commit is contained in:
Daniel Jasper 2013-07-11 20:41:21 +00:00
parent 4822d9263a
commit 4e9678f7a1
3 changed files with 48 additions and 10 deletions

View File

@ -45,6 +45,9 @@ struct FormatStyle {
/// \brief The penalty for each character outside of the column limit. /// \brief The penalty for each character outside of the column limit.
unsigned PenaltyExcessCharacter; unsigned PenaltyExcessCharacter;
/// \brief The penalty for breaking before the first "<<".
unsigned PenaltyBreakFirstLessLess;
/// \brief Set whether & and * bind to the type as opposed to the variable. /// \brief Set whether & and * bind to the type as opposed to the variable.
bool PointerBindsToType; bool PointerBindsToType;
@ -173,8 +176,9 @@ struct FormatStyle {
IndentWidth == R.IndentWidth && IndentWidth == R.IndentWidth &&
MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep && MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep &&
ObjCSpaceBeforeProtocolList == R.ObjCSpaceBeforeProtocolList && ObjCSpaceBeforeProtocolList == R.ObjCSpaceBeforeProtocolList &&
PenaltyBreakString == R.PenaltyBreakString &&
PenaltyBreakComment == R.PenaltyBreakComment && PenaltyBreakComment == R.PenaltyBreakComment &&
PenaltyBreakFirstLessLess == R.PenaltyBreakFirstLessLess &&
PenaltyBreakString == R.PenaltyBreakString &&
PenaltyExcessCharacter == R.PenaltyExcessCharacter && PenaltyExcessCharacter == R.PenaltyExcessCharacter &&
PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine && PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine &&
PointerBindsToType == R.PointerBindsToType && PointerBindsToType == R.PointerBindsToType &&

View File

@ -102,6 +102,8 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
Style.ObjCSpaceBeforeProtocolList); Style.ObjCSpaceBeforeProtocolList);
IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment); IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
IO.mapOptional("PenaltyBreakFirstLessLess",
Style.PenaltyBreakFirstLessLess);
IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter); IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
IO.mapOptional("PenaltyReturnTypeOnItsOwnLine", IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
Style.PenaltyReturnTypeOnItsOwnLine); Style.PenaltyReturnTypeOnItsOwnLine);
@ -123,6 +125,13 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
namespace clang { namespace clang {
namespace format { namespace format {
void setDefaultPenalties(FormatStyle &Style) {
Style.PenaltyBreakComment = 45;
Style.PenaltyBreakFirstLessLess = 100;
Style.PenaltyBreakString = 1000;
Style.PenaltyExcessCharacter = 1000000;
}
FormatStyle getLLVMStyle() { FormatStyle getLLVMStyle() {
FormatStyle LLVMStyle; FormatStyle LLVMStyle;
LLVMStyle.AccessModifierOffset = -2; LLVMStyle.AccessModifierOffset = -2;
@ -140,10 +149,6 @@ FormatStyle getLLVMStyle() {
LLVMStyle.IndentCaseLabels = false; LLVMStyle.IndentCaseLabels = false;
LLVMStyle.MaxEmptyLinesToKeep = 1; LLVMStyle.MaxEmptyLinesToKeep = 1;
LLVMStyle.ObjCSpaceBeforeProtocolList = true; LLVMStyle.ObjCSpaceBeforeProtocolList = true;
LLVMStyle.PenaltyBreakComment = 45;
LLVMStyle.PenaltyBreakString = 1000;
LLVMStyle.PenaltyExcessCharacter = 1000000;
LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
LLVMStyle.PointerBindsToType = false; LLVMStyle.PointerBindsToType = false;
LLVMStyle.SpacesBeforeTrailingComments = 1; LLVMStyle.SpacesBeforeTrailingComments = 1;
LLVMStyle.SpacesInBracedLists = true; LLVMStyle.SpacesInBracedLists = true;
@ -152,6 +157,10 @@ FormatStyle getLLVMStyle() {
LLVMStyle.UseTab = false; LLVMStyle.UseTab = false;
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
LLVMStyle.IndentFunctionDeclarationAfterType = false; LLVMStyle.IndentFunctionDeclarationAfterType = false;
setDefaultPenalties(LLVMStyle);
LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
return LLVMStyle; return LLVMStyle;
} }
@ -172,10 +181,6 @@ FormatStyle getGoogleStyle() {
GoogleStyle.IndentCaseLabels = true; GoogleStyle.IndentCaseLabels = true;
GoogleStyle.MaxEmptyLinesToKeep = 1; GoogleStyle.MaxEmptyLinesToKeep = 1;
GoogleStyle.ObjCSpaceBeforeProtocolList = false; GoogleStyle.ObjCSpaceBeforeProtocolList = false;
GoogleStyle.PenaltyBreakComment = 45;
GoogleStyle.PenaltyBreakString = 1000;
GoogleStyle.PenaltyExcessCharacter = 1000000;
GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
GoogleStyle.PointerBindsToType = true; GoogleStyle.PointerBindsToType = true;
GoogleStyle.SpacesBeforeTrailingComments = 2; GoogleStyle.SpacesBeforeTrailingComments = 2;
GoogleStyle.SpacesInBracedLists = false; GoogleStyle.SpacesInBracedLists = false;
@ -184,6 +189,10 @@ FormatStyle getGoogleStyle() {
GoogleStyle.UseTab = false; GoogleStyle.UseTab = false;
GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach; GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
GoogleStyle.IndentFunctionDeclarationAfterType = true; GoogleStyle.IndentFunctionDeclarationAfterType = true;
setDefaultPenalties(GoogleStyle);
GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
return GoogleStyle; return GoogleStyle;
} }
@ -503,6 +512,10 @@ private:
const FormatToken &Current = *State.NextToken; const FormatToken &Current = *State.NextToken;
const FormatToken &Previous = *State.NextToken->Previous; const FormatToken &Previous = *State.NextToken->Previous;
// Extra penalty that needs to be added because of the way certain line
// breaks are chosen.
unsigned ExtraPenalty = 0;
if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) { if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
// FIXME: Is this correct? // FIXME: Is this correct?
int WhitespaceLength = SourceMgr.getSpellingColumnNumber( int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
@ -621,6 +634,11 @@ private:
Line.MustBeDeclaration)) Line.MustBeDeclaration))
State.Stack.back().BreakBeforeParameter = true; State.Stack.back().BreakBeforeParameter = true;
} }
// Breaking before the first "<<" is generally not desirable.
if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
ExtraPenalty += Style.PenaltyBreakFirstLessLess;
} else { } else {
if (Current.is(tok::equal) && if (Current.is(tok::equal) &&
(RootToken->is(tok::kw_for) || State.ParenLevel == 0) && (RootToken->is(tok::kw_for) || State.ParenLevel == 0) &&
@ -699,7 +717,7 @@ private:
} }
} }
return moveStateToNextToken(State, DryRun); return moveStateToNextToken(State, DryRun) + ExtraPenalty;
} }
/// \brief Mark the next token as consumed in \p State and modify its stacks /// \brief Mark the next token as consumed in \p State and modify its stacks

View File

@ -3002,6 +3002,22 @@ TEST_F(FormatTest, AlignsPipes) {
verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaa: \"\n" verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
// Breaking before the first "<<" is generally not desirable.
verifyFormat(
"llvm::errs()\n"
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
getLLVMStyleWithColumns(70));
verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
getLLVMStyleWithColumns(70));
verifyFormat( verifyFormat(
"llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");