[clang-format] [PR42164] Add Option to Break before While

Summary:
Its currently not possible to recreate the GNU style using the `BreakBeforeBraces: Custom` style due to a lack of missing `BeforeWhile` in the `BraceWrappingFlags`

The following request was raised to add `BeforeWhile` in a `do..while` context like `BeforeElse` and `BeforeCatch` to give greater control over the positioning of the `while`

https://bugs.llvm.org/show_bug.cgi?id=42164

Reviewers: krasimir, mitchell-stellar, sammccall

Reviewed By: krasimir

Subscribers: cfe-commits

Tags: #clang, #clang-format

Differential Revision: https://reviews.llvm.org/D79325
This commit is contained in:
mydeveloperday
2020-05-20 07:48:45 +01:00
parent cc918e90c0
commit 807ab2cd0d
6 changed files with 125 additions and 19 deletions

View File

@@ -1025,6 +1025,21 @@ the configuration (without a prefix: ``Auto``).
bar();
});
* ``bool BeforeWhile`` Wrap before ``while``.
.. code-block:: c++
true:
do {
foo();
}
while (1);
false:
do {
foo();
} while (1);
* ``bool IndentBraces`` Indent the wrapped braces themselves.
* ``bool SplitEmptyFunction`` If ``false``, empty function body can be put on a single line.

View File

@@ -312,6 +312,27 @@ clang-format
bool a : 1;
bool bb : 1;
- Option ``BraceWrapping.BeforeWhile`` has been added to allow wrapping
before the ```while`` in a do..while loop. By default the value is (``false``)
In previous releases ``IndentBraces`` implied ``BraceWrapping.BeforeWhile``.
If using a Custom BraceWrapping style you may need to now set
``BraceWrapping.BeforeWhile`` to (``true``) to be explicit.
.. code-block:: c++
true:
do {
foo();
}
while(1);
false:
do {
foo();
} while(1);
libclang
--------

View File

@@ -1076,6 +1076,20 @@ struct FormatStyle {
/// });
/// \endcode
bool BeforeLambdaBody;
/// Wrap before ``while``.
/// \code
/// true:
/// do {
/// foo();
/// }
/// while (1);
///
/// false:
/// do {
/// foo();
/// } while (1);
/// \endcode
bool BeforeWhile;
/// Indent the wrapped braces themselves.
bool IndentBraces;
/// If ``false``, empty function body can be put on a single line.

View File

@@ -604,6 +604,7 @@ template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
IO.mapOptional("BeforeCatch", Wrapping.BeforeCatch);
IO.mapOptional("BeforeElse", Wrapping.BeforeElse);
IO.mapOptional("BeforeLambdaBody", Wrapping.BeforeLambdaBody);
IO.mapOptional("BeforeWhile", Wrapping.BeforeWhile);
IO.mapOptional("IndentBraces", Wrapping.IndentBraces);
IO.mapOptional("SplitEmptyFunction", Wrapping.SplitEmptyFunction);
IO.mapOptional("SplitEmptyRecord", Wrapping.SplitEmptyRecord);
@@ -687,12 +688,24 @@ static FormatStyle expandPresets(const FormatStyle &Style) {
if (Style.BreakBeforeBraces == FormatStyle::BS_Custom)
return Style;
FormatStyle Expanded = Style;
Expanded.BraceWrapping = {false, false, FormatStyle::BWACS_Never,
false, false, false,
false, false, false,
false, false, false,
false, false, true,
true, true};
Expanded.BraceWrapping = {/*AfterCaseLabel=*/false,
/*AfterClass=*/false,
/*AfterControlStatement=*/FormatStyle::BWACS_Never,
/*AfterEnum=*/false,
/*AfterFunction=*/false,
/*AfterNamespace=*/false,
/*AfterObjCDeclaration=*/false,
/*AfterStruct=*/false,
/*AfterUnion=*/false,
/*AfterExternBlock=*/false,
/*BeforeCatch=*/false,
/*BeforeElse=*/false,
/*BeforeLambdaBody=*/false,
/*BeforeWhile=*/false,
/*IndentBraces=*/false,
/*SplitEmptyFunction=*/true,
/*SplitEmptyRecord=*/true,
/*SplitEmptyNamespace=*/true};
switch (Style.BreakBeforeBraces) {
case FormatStyle::BS_Linux:
Expanded.BraceWrapping.AfterClass = true;
@@ -743,12 +756,25 @@ static FormatStyle expandPresets(const FormatStyle &Style) {
Expanded.BraceWrapping.BeforeLambdaBody = true;
break;
case FormatStyle::BS_GNU:
Expanded.BraceWrapping = {true, true, FormatStyle::BWACS_Always,
true, true, true,
true, true, true,
true, true, true,
false, true, true,
true, true};
Expanded.BraceWrapping = {
/*AfterCaseLabel=*/true,
/*AfterClass=*/true,
/*AfterControlStatement=*/FormatStyle::BWACS_Always,
/*AfterEnum=*/true,
/*AfterFunction=*/true,
/*AfterNamespace=*/true,
/*AfterObjCDeclaration=*/true,
/*AfterStruct=*/true,
/*AfterUnion=*/true,
/*AfterExternBlock=*/true,
/*BeforeCatch=*/true,
/*BeforeElse=*/true,
/*BeforeLambdaBody=*/false,
/*BeforeWhile=*/true,
/*IndentBraces=*/true,
/*SplitEmptyFunction=*/true,
/*SplitEmptyRecord=*/true,
/*SplitEmptyNamespace=*/true};
break;
case FormatStyle::BS_WebKit:
Expanded.BraceWrapping.AfterFunction = true;
@@ -790,12 +816,24 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
LLVMStyle.BreakBeforeTernaryOperators = true;
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
LLVMStyle.BraceWrapping = {false, false, FormatStyle::BWACS_Never,
false, false, false,
false, false, false,
false, false, false,
false, false, true,
true, true};
LLVMStyle.BraceWrapping = {/*AfterCaseLabel=*/false,
/*AfterClass=*/false,
/*AfterControlStatement=*/FormatStyle::BWACS_Never,
/*AfterEnum=*/false,
/*AfterFunction=*/false,
/*AfterNamespace=*/false,
/*AfterObjCDeclaration=*/false,
/*AfterStruct=*/false,
/*AfterUnion=*/false,
/*AfterExternBlock=*/false,
/*BeforeCatch=*/false,
/*BeforeElse=*/false,
/*BeforeLambdaBody=*/false,
/*BeforeWhile=*/false,
/*IndentBraces=*/false,
/*SplitEmptyFunction=*/true,
/*SplitEmptyRecord=*/true,
/*SplitEmptyNamespace=*/true};
LLVMStyle.BreakAfterJavaFieldAnnotations = false;
LLVMStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
LLVMStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
@@ -1159,6 +1197,7 @@ FormatStyle getMicrosoftStyle(FormatStyle::LanguageKind Language) {
Style.BraceWrapping.AfterExternBlock = true;
Style.BraceWrapping.BeforeCatch = true;
Style.BraceWrapping.BeforeElse = true;
Style.BraceWrapping.BeforeWhile = false;
Style.PenaltyReturnTypeOnItsOwnLine = 1000;
Style.AllowShortEnumsOnASingleLine = false;
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;

View File

@@ -2175,7 +2175,7 @@ void UnwrappedLineParser::parseDoWhile() {
if (FormatTok->Tok.is(tok::l_brace)) {
CompoundStatementIndenter Indenter(this, Style, Line->Level);
parseBlock(/*MustBeDeclaration=*/false);
if (Style.BraceWrapping.IndentBraces)
if (Style.BraceWrapping.BeforeWhile)
addUnwrappedLine();
} else {
addUnwrappedLine();

View File

@@ -1706,6 +1706,22 @@ TEST_F(FormatTest, MultiLineControlStatements) {
format("try{foo();}catch(...){baz();}", Style));
}
TEST_F(FormatTest, BeforeWhile) {
FormatStyle Style = getLLVMStyle();
Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom;
verifyFormat("do {\n"
" foo();\n"
"} while (1);",
Style);
Style.BraceWrapping.BeforeWhile = true;
verifyFormat("do {\n"
" foo();\n"
"}\n"
"while (1);",
Style);
}
//===----------------------------------------------------------------------===//
// Tests for classes, namespaces, etc.
//===----------------------------------------------------------------------===//
@@ -13543,6 +13559,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);