Index: clang/docs/ClangFormatStyleOptions.rst =================================================================== --- clang/docs/ClangFormatStyleOptions.rst +++ clang/docs/ClangFormatStyleOptions.rst @@ -981,6 +981,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. Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -297,6 +297,22 @@ bar(); }); +- Option ``BraceWrapping.BeforeWhile`` has been added to allow wrapping + before the ```while`` in a do..while loop. By default the value is (``false``) + + .. code-block:: c++ + + true: + do { + foo(); + } + while(1); + + false: + do { + foo(); + } while(1); + libclang -------- Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -1038,6 +1038,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. Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -587,6 +587,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); @@ -670,12 +671,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; @@ -726,12 +739,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; @@ -772,12 +798,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; @@ -1141,6 +1179,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; Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -2167,7 +2167,7 @@ if (FormatTok->Tok.is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(/*MustBeDeclaration=*/false); - if (Style.BraceWrapping.IndentBraces) + if (Style.BraceWrapping.IndentBraces || Style.BraceWrapping.BeforeWhile) addUnwrappedLine(); } else { addUnwrappedLine(); Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -1692,6 +1692,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. //===----------------------------------------------------------------------===// @@ -13042,6 +13058,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);