[clang-format] Expand AllowShortBlocksOnASingleLine for WebKit

See PR40840

Differential Revision: https://reviews.llvm.org/D66059

llvm-svn: 368539
This commit is contained in:
Owen Pan 2019-08-11 17:48:36 +00:00
parent ce6a2cf966
commit 10234da71d
6 changed files with 116 additions and 24 deletions

View File

@ -344,10 +344,42 @@ the configuration (without a prefix: ``Auto``).
int d, int d,
int e); int e);
**AllowShortBlocksOnASingleLine** (``bool``) **AllowShortBlocksOnASingleLine** (``ShortBlockStyle``)
Allows contracting simple braced statements to a single line. Dependent on the value, ``while (true) { continue; }`` can be put on a
single line.
Possible values:
* ``SBS_Never`` (in configuration: ``Never``)
Never merge blocks into a single line.
.. code-block:: c++
while (true) {
}
while (true) {
continue;
}
* ``SBS_Empty`` (in configuration: ``Empty``)
Only merge empty blocks.
.. code-block:: c++
while (true) {}
while (true) {
continue;
}
* ``SBS_Always`` (in configuration: ``Always``)
Always merge short blocks into a single line.
.. code-block:: c++
while (true) {}
while (true) { continue; }
E.g., this allows ``if (a) { return; }`` to be put on a single line.
**AllowShortCaseLabelsOnASingleLine** (``bool``) **AllowShortCaseLabelsOnASingleLine** (``bool``)
If ``true``, short case labels will be contracted to a single line. If ``true``, short case labels will be contracted to a single line.

View File

@ -216,10 +216,37 @@ struct FormatStyle {
/// \endcode /// \endcode
bool AllowAllParametersOfDeclarationOnNextLine; bool AllowAllParametersOfDeclarationOnNextLine;
/// Allows contracting simple braced statements to a single line. /// Different styles for merging short blocks containing at most one
/// /// statement.
/// E.g., this allows ``if (a) { return; }`` to be put on a single line. enum ShortBlockStyle {
bool AllowShortBlocksOnASingleLine; /// Never merge blocks into a single line.
/// \code
/// while (true) {
/// }
/// while (true) {
/// continue;
/// }
/// \endcode
SBS_Never,
/// Only merge empty blocks.
/// \code
/// while (true) {}
/// while (true) {
/// continue;
/// }
/// \endcode
SBS_Empty,
/// Always merge short blocks into a single line.
/// \code
/// while (true) {}
/// while (true) { continue; }
/// \endcode
SBS_Always,
};
/// Dependent on the value, ``while (true) { continue; }`` can be put on a
/// single line.
ShortBlockStyle AllowShortBlocksOnASingleLine;
/// If ``true``, short case labels will be contracted to a single line. /// If ``true``, short case labels will be contracted to a single line.
/// \code /// \code

View File

@ -95,6 +95,16 @@ template <> struct ScalarEnumerationTraits<FormatStyle::JavaScriptQuoteStyle> {
} }
}; };
template <> struct ScalarEnumerationTraits<FormatStyle::ShortBlockStyle> {
static void enumeration(IO &IO, FormatStyle::ShortBlockStyle &Value) {
IO.enumCase(Value, "Never", FormatStyle::SBS_Never);
IO.enumCase(Value, "false", FormatStyle::SBS_Never);
IO.enumCase(Value, "Always", FormatStyle::SBS_Always);
IO.enumCase(Value, "true", FormatStyle::SBS_Always);
IO.enumCase(Value, "Empty", FormatStyle::SBS_Empty);
}
};
template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> { template <> struct ScalarEnumerationTraits<FormatStyle::ShortFunctionStyle> {
static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) { static void enumeration(IO &IO, FormatStyle::ShortFunctionStyle &Value) {
IO.enumCase(Value, "None", FormatStyle::SFS_None); IO.enumCase(Value, "None", FormatStyle::SFS_None);
@ -674,7 +684,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AllowAllConstructorInitializersOnNextLine = true; LLVMStyle.AllowAllConstructorInitializersOnNextLine = true;
LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
LLVMStyle.AllowShortBlocksOnASingleLine = false; LLVMStyle.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
LLVMStyle.AllowShortCaseLabelsOnASingleLine = false; LLVMStyle.AllowShortCaseLabelsOnASingleLine = false;
LLVMStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; LLVMStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;
LLVMStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; LLVMStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All;
@ -969,6 +979,7 @@ FormatStyle getWebKitStyle() {
Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
Style.AlignOperands = false; Style.AlignOperands = false;
Style.AlignTrailingComments = false; Style.AlignTrailingComments = false;
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
Style.BreakBeforeBraces = FormatStyle::BS_WebKit; Style.BreakBeforeBraces = FormatStyle::BS_WebKit;
Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
@ -1019,7 +1030,6 @@ FormatStyle getMicrosoftStyle(FormatStyle::LanguageKind Language) {
Style.BraceWrapping.BeforeElse = true; Style.BraceWrapping.BeforeElse = true;
Style.PenaltyReturnTypeOnItsOwnLine = 1000; Style.PenaltyReturnTypeOnItsOwnLine = 1000;
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
Style.AllowShortBlocksOnASingleLine = false;
Style.AllowShortCaseLabelsOnASingleLine = false; Style.AllowShortCaseLabelsOnASingleLine = false;
Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;
Style.AllowShortLoopsOnASingleLine = false; Style.AllowShortLoopsOnASingleLine = false;

View File

@ -3044,7 +3044,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
Style.BraceWrapping.AfterEnum) || Style.BraceWrapping.AfterEnum) ||
(Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) || (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
(Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct); (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine) if (Left.is(TT_ObjCBlockLBrace) &&
Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
return true; return true;
if (Left.is(TT_LambdaLBrace)) { if (Left.is(TT_LambdaLBrace)) {

View File

@ -300,7 +300,7 @@ private:
// Try to merge a control statement block with left brace unwrapped // Try to merge a control statement block with left brace unwrapped
if (TheLine->Last->is(tok::l_brace) && TheLine->First != TheLine->Last && if (TheLine->Last->is(tok::l_brace) && TheLine->First != TheLine->Last &&
TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) { TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) {
return Style.AllowShortBlocksOnASingleLine return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never
? tryMergeSimpleBlock(I, E, Limit) ? tryMergeSimpleBlock(I, E, Limit)
: 0; : 0;
} }
@ -317,7 +317,7 @@ private:
I != AnnotatedLines.begin() && I != AnnotatedLines.begin() &&
I[-1]->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) { I[-1]->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) {
unsigned MergedLines = 0; unsigned MergedLines = 0;
if (Style.AllowShortBlocksOnASingleLine) { if (Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never) {
MergedLines = tryMergeSimpleBlock(I - 1, E, Limit); MergedLines = tryMergeSimpleBlock(I - 1, E, Limit);
// If we managed to merge the block, discard the first merged line // If we managed to merge the block, discard the first merged line
// since we are merging starting from I. // since we are merging starting from I.
@ -411,7 +411,8 @@ private:
if (Limit == 0) if (Limit == 0)
return 0; return 0;
if (Style.BraceWrapping.AfterControlStatement && if (Style.BraceWrapping.AfterControlStatement &&
(I[1]->First->is(tok::l_brace) && !Style.AllowShortBlocksOnASingleLine)) I[1]->First->is(tok::l_brace) &&
Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
return 0; return 0;
if (I[1]->InPPDirective != (*I)->InPPDirective || if (I[1]->InPPDirective != (*I)->InPPDirective ||
(I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline))
@ -511,7 +512,7 @@ private:
if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try, if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
tok::kw___try, tok::kw_catch, tok::kw___finally, tok::kw___try, tok::kw_catch, tok::kw___finally,
tok::kw_for, tok::r_brace, Keywords.kw___except)) { tok::kw_for, tok::r_brace, Keywords.kw___except)) {
if (!Style.AllowShortBlocksOnASingleLine) if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
return 0; return 0;
// Don't merge when we can't except the case when // Don't merge when we can't except the case when
// the control statement block is empty // the control statement block is empty
@ -607,7 +608,7 @@ private:
return 0; return 0;
Limit -= 2; Limit -= 2;
unsigned MergedLines = 0; unsigned MergedLines = 0;
if (Style.AllowShortBlocksOnASingleLine || if (Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never ||
(I[1]->First == I[1]->Last && I + 2 != E && (I[1]->First == I[1]->Last && I + 2 != E &&
I[2]->First->is(tok::r_brace))) { I[2]->First->is(tok::r_brace))) {
MergedLines = tryMergeSimpleBlock(I + 1, E, Limit); MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);

View File

@ -561,7 +561,8 @@ TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
TEST_F(FormatTest, FormatShortBracedStatements) { TEST_F(FormatTest, FormatShortBracedStatements) {
FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
AllowSimpleBracedStatements.ColumnLimit = 40; AllowSimpleBracedStatements.ColumnLimit = 40;
AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true; AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
FormatStyle::SBS_Always;
AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine =
FormatStyle::SIS_WithoutElse; FormatStyle::SIS_WithoutElse;
@ -714,7 +715,7 @@ TEST_F(FormatTest, FormatShortBracedStatements) {
TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) {
FormatStyle Style = getLLVMStyleWithColumns(60); FormatStyle Style = getLLVMStyleWithColumns(60);
Style.AllowShortBlocksOnASingleLine = true; Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse;
Style.BreakBeforeBraces = FormatStyle::BS_Allman; Style.BreakBeforeBraces = FormatStyle::BS_Allman;
EXPECT_EQ("#define A \\\n" EXPECT_EQ("#define A \\\n"
@ -1163,7 +1164,7 @@ TEST_F(FormatTest, FormatsSwitchStatement) {
FormatStyle Style = getLLVMStyle(); FormatStyle Style = getLLVMStyle();
Style.IndentCaseLabels = true; Style.IndentCaseLabels = true;
Style.AllowShortBlocksOnASingleLine = false; Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
Style.BreakBeforeBraces = FormatStyle::BS_Custom; Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.AfterCaseLabel = true; Style.BraceWrapping.AfterCaseLabel = true;
Style.BraceWrapping.AfterControlStatement = true; Style.BraceWrapping.AfterControlStatement = true;
@ -3692,10 +3693,10 @@ TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
EXPECT_EQ("{}", format("{}")); EXPECT_EQ("{}", format("{}"));
verifyFormat("enum E {};"); verifyFormat("enum E {};");
verifyFormat("enum E {}"); verifyFormat("enum E {}");
EXPECT_EQ("void f() { }", format("void f() {}", getWebKitStyle()));
FormatStyle Style = getLLVMStyle(); FormatStyle Style = getLLVMStyle();
Style.AllowShortBlocksOnASingleLine = true;
Style.SpaceInEmptyBlock = true; Style.SpaceInEmptyBlock = true;
EXPECT_EQ("void f() { }", format("void f() {}", Style));
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
} }
@ -11745,7 +11746,6 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine);
CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
CHECK_PARSE_BOOL(BinPackArguments); CHECK_PARSE_BOOL(BinPackArguments);
@ -11920,6 +11920,19 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
CHECK_PARSE("AllowShortBlocksOnASingleLine: Never",
AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty",
AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty);
CHECK_PARSE("AllowShortBlocksOnASingleLine: Always",
AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
// For backward compatibility:
CHECK_PARSE("AllowShortBlocksOnASingleLine: false",
AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never);
CHECK_PARSE("AllowShortBlocksOnASingleLine: true",
AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always);
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
@ -12527,6 +12540,14 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) {
// Allow functions on a single line. // Allow functions on a single line.
verifyFormat("void f() { return; }", Style); verifyFormat("void f() { return; }", Style);
// Allow empty blocks on a single line and insert a space in empty blocks.
EXPECT_EQ("void f() { }", format("void f() {}", Style));
EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
// However, don't merge non-empty short loops.
EXPECT_EQ("while (true) {\n"
" continue;\n"
"}", format("while (true) { continue; }", Style));
// Constructor initializers are formatted one per line with the "," on the // Constructor initializers are formatted one per line with the "," on the
// new line. // new line.
verifyFormat("Constructor()\n" verifyFormat("Constructor()\n"
@ -13033,7 +13054,7 @@ TEST_F(FormatTest, EmptyLinesInLambdas) {
TEST_F(FormatTest, FormatsBlocks) { TEST_F(FormatTest, FormatsBlocks) {
FormatStyle ShortBlocks = getLLVMStyle(); FormatStyle ShortBlocks = getLLVMStyle();
ShortBlocks.AllowShortBlocksOnASingleLine = true; ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
verifyFormat("int (^Block)(int, int);", ShortBlocks); verifyFormat("int (^Block)(int, int);", ShortBlocks);
verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
@ -13191,10 +13212,10 @@ TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
"};", "};",
ZeroColumn); ZeroColumn);
ZeroColumn.AllowShortBlocksOnASingleLine = true; ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
ZeroColumn.AllowShortBlocksOnASingleLine = false; ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
EXPECT_EQ("void (^largeBlock)(void) = ^{\n" EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
" int i;\n" " int i;\n"
"};", "};",