clang-format: Adapt line break penalties for LLVM style.

Specifically make clang-format less eager to break after the opening
parenthesis of a function call.

Before:
  aaaaaaaaaaaaaaaaa(
      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);

After:
  aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
                        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);

Apparently that is preferable. This penalties are adapted
conservatively, we might have to increase them a little bit further.

llvm-svn: 193410
This commit is contained in:
Daniel Jasper 2013-10-25 14:29:37 +00:00
parent 2acb2470f9
commit 33b909c5f3
4 changed files with 18 additions and 8 deletions

View File

@ -52,6 +52,9 @@ struct FormatStyle {
/// \brief The penalty for breaking before the first \c <<.
unsigned PenaltyBreakFirstLessLess;
/// \brief The penalty for breaking a function call after "call(".
unsigned PenaltyBreakBeforeFirstCallParameter;
/// \brief Set whether & and * bind to the type as opposed to the variable.
bool PointerBindsToType;

View File

@ -137,6 +137,8 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
IO.mapOptional("ObjCSpaceBeforeProtocolList",
Style.ObjCSpaceBeforeProtocolList);
IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
Style.PenaltyBreakBeforeFirstCallParameter);
IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
IO.mapOptional("PenaltyBreakFirstLessLess",
@ -219,6 +221,7 @@ FormatStyle getLLVMStyle() {
setDefaultPenalties(LLVMStyle);
LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
return LLVMStyle;
}
@ -263,6 +266,7 @@ FormatStyle getGoogleStyle() {
setDefaultPenalties(GoogleStyle);
GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
return GoogleStyle;
}

View File

@ -1177,7 +1177,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
if (Left.is(tok::l_paren) && Line.MightBeFunctionDecl)
return 100;
if (Left.opensScope())
return Left.ParameterCount > 1 ? prec::Comma : 19;
return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
: 19;
if (Right.is(tok::lessless)) {
if (Left.is(tok::string_literal)) {

View File

@ -2973,9 +2973,9 @@ TEST_F(FormatTest, BreaksDesireably) {
"aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
verifyFormat(
"aaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
"aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
// Indent consistently indenpendent of call expression.
verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
@ -3122,7 +3122,7 @@ TEST_F(FormatTest, FormatsBuilderPattern) {
" ->aaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
" ->aaaaaaaaaaaaaaaaa();");
verifyFormat(
verifyGoogleFormat(
"someobj->Add((new util::filetools::Handler(dir))\n"
" ->OnEvent1(NewPermanentCallback(\n"
" this, &HandlerHolderClass::EventHandlerCBA))\n"
@ -4497,7 +4497,7 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
NoSpaces);
}
TEST_F(FormatTest, FormatsBracedListsinColumnLayout) {
TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
verifyFormat("vector<int> x = { 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
" 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
" 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
@ -4538,8 +4538,8 @@ TEST_F(FormatTest, FormatsBracedListsinColumnLayout) {
verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
" 1, 1, 1, 1, };",
getLLVMStyleWithColumns(39));
verifyFormat("vector<int> x = {\n"
" 1, 1, 1, 1, 1, 1, 1, 1, //\n"
verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
" 1, 1, 1, 1, //\n"
"};",
getLLVMStyleWithColumns(39));
verifyFormat("vector<int> x = { 1, 1, 1, 1,\n"
@ -6597,6 +6597,8 @@ TEST_F(FormatTest, ParsesConfiguration) {
ConstructorInitializerIndentWidth, 1234u);
CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
PenaltyBreakBeforeFirstCallParameter, 1234u);
CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
PenaltyReturnTypeOnItsOwnLine, 1234u);