diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -1025,6 +1025,21 @@ 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. diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -312,6 +312,27 @@ 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 -------- diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1076,6 +1076,20 @@ /// }); /// \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. diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -604,6 +604,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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; diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2175,7 +2175,7 @@ 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(); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -1706,6 +1706,22 @@ 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 @@ 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);