Skip to content

Commit

Permalink
[clang-format] Improve BAS_DontAlign+AllowAllArgumentsOnNextLine=false
Browse files Browse the repository at this point in the history
TokenAnnotator::splitPenalty() was always returning 0 for opening parens if
AlignAfterOpenBracket was set to BAS_DontAlign, so the preferred point for
line breaking was always after the open paren (and was ignoring
PenaltyBreakBeforeFirstCallParameter). This change restricts the zero
penalty to the AllowAllArgumentsOnNextLine case. This results in improved
formatting for FreeBSD where we set AllowAllArgumentsOnNextLine: false
and a high value for PenaltyBreakBeforeFirstCallParameter to avoid breaking
after the open paren.

Before:
```
functionCall(
    paramA, paramB, paramC);
void functionDecl(
    int A, int B, int C)
```
After:
```
functionCall(paramA, paramB,
    paramC);
void functionDecl(int A, int B,
    int C)
```

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D90246
  • Loading branch information
arichardson committed Nov 2, 2020
1 parent 5bc438e commit 906b9db
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
6 changes: 5 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Expand Up @@ -2734,7 +2734,11 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
if (Left.is(TT_TemplateOpener))
return 100;
if (Left.opensScope()) {
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign)
// If we aren't aligning after opening parens/braces we can always break
// here unless the style does not want us to place all arguments on the
// next line.
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
(Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine))
return 0;
if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
return 19;
Expand Down
54 changes: 54 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Expand Up @@ -4992,6 +4992,60 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
Style);
}

TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
// Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
// and BAS_Align.
auto Style = getLLVMStyle();
Style.ColumnLimit = 35;
StringRef Input = "functionCall(paramA, paramB, paramC);\n"
"void functionDecl(int A, int B, int C);";
Style.AllowAllArgumentsOnNextLine = false;
Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
" int C);"),
format(Input, Style));
Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
" int C);"),
format(Input, Style));
// However, BAS_AlwaysBreak should take precedence over
// AllowAllArgumentsOnNextLine.
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
EXPECT_EQ(StringRef("functionCall(\n"
" paramA, paramB, paramC);\n"
"void functionDecl(\n"
" int A, int B, int C);"),
format(Input, Style));

// When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
// first argument.
Style.AllowAllArgumentsOnNextLine = true;
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
EXPECT_EQ(StringRef("functionCall(\n"
" paramA, paramB, paramC);\n"
"void functionDecl(\n"
" int A, int B, int C);"),
format(Input, Style));
// It wouldn't fit on one line with aligned parameters so this setting
// doesn't change anything for BAS_Align.
Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
" int C);"),
format(Input, Style));
Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
EXPECT_EQ(StringRef("functionCall(\n"
" paramA, paramB, paramC);\n"
"void functionDecl(\n"
" int A, int B, int C);"),
format(Input, Style));
}

TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
FormatStyle Style = getLLVMStyle();
Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
Expand Down

0 comments on commit 906b9db

Please sign in to comment.