Index: clang/docs/ClangFormatStyleOptions.rst =================================================================== --- clang/docs/ClangFormatStyleOptions.rst +++ clang/docs/ClangFormatStyleOptions.rst @@ -407,47 +407,46 @@ }; void f() { bar(); } + + **AllowShortIfStatementsOnASingleLine** (``ShortIfStyle``) - Dependent on the value, ``if (a) return 0;`` can be put on a - single line. + If ``true``, ``if (a) return;`` can be put on a single line. Possible values: * ``SIS_Never`` (in configuration: ``Never``) - Do not allow short if functions. + Never put short ifs on the same line. .. code-block:: c++ - if (a) - return; - else - return; + if (a) + return ; + else { + return; + } * ``SIS_WithoutElse`` (in configuration: ``WithoutElse``) - Allow short if functions on the same line, as long as else - is not a compound statement. + Without else put short ifs on the same line only if + the else is not a compound statement. .. code-block:: c++ - if (a) return; - else - return; - - if (a) - return; - else { - return; - } + if (a) return; + else + return; * ``SIS_Always`` (in configuration: ``Always``) - Allow short if statements even if the else is a compound statement. + Always put short ifs on the same line if + the else is not a compound statement or not. .. code-block:: c++ - if (a) return; - else { - return; - } + if (a) return; + else { + return; + } + + **AllowShortLambdasOnASingleLine** (``ShortLambdaStyle``) Dependent on the value, ``auto lambda []() { return 0; }`` can be put on a @@ -705,6 +704,23 @@ Nested configuration flags: + * ``bool AfterCaseLabel`` Wrap case labels. + + .. code-block:: c++ + + false: true: + switch (foo) { vs. switch (foo) { + case 1: { case 1: + bar(); { + break; bar(); + } break; + default: { } + plop(); default: + } { + } plop(); + } + } + * ``bool AfterClass`` Wrap class definitions. .. code-block:: c++ @@ -1043,28 +1059,19 @@ .. code-block:: c++ - try - { + try { foo(); } - catch () - { + catch () { } void foo() { bar(); } - class foo - { + class foo { }; - if (foo()) - { + if (foo()) { } - else - { + else { } - enum X : int - { - A, - B - }; + enum X : int { A, B }; * ``BS_GNU`` (in configuration: ``GNU``) Always break before braces and add an extra level of indentation to @@ -1504,6 +1511,7 @@ #endif + **IndentWidth** (``unsigned``) The number of columns to use for indentation. @@ -1954,6 +1962,7 @@ **SpaceAfterLogicalNot** (``bool``) If ``true``, a space is inserted after the logical not operator (``!``). + .. code-block:: c++ true: false: @@ -2033,6 +2042,19 @@ } } + * ``SBPO_NonEmptyParentheses`` (in configuration: ``NonEmptyParentheses``) + Put a space before opening parentheses only if the parentheses are not + empty i.e. '()' + + .. code-block:: c++ + + void() { + if (true) { + f(); + g (x, y, z); + } + } + * ``SBPO_Always`` (in configuration: ``Always``) Always put a space before opening parentheses, except when it's prohibited by the syntax rules (in function-like macro definitions) or Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -715,6 +715,22 @@ /// AfterClass: true /// \endcode struct BraceWrappingFlags { + /// Wrap case labels. + /// \code + /// false: true: + /// switch (foo) { vs. switch (foo) { + /// case 1: { case 1: + /// bar(); { + /// break; bar(); + /// } break; + /// default: { } + /// plop(); default: + /// } { + /// } plop(); + /// } + /// } + /// \endcode + bool AfterCaseLabel; /// Wrap class definitions. /// \code /// true: Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -511,6 +511,7 @@ template <> struct MappingTraits { static void mapping(IO &IO, FormatStyle::BraceWrappingFlags &Wrapping) { + IO.mapOptional("AfterCaseLabel", Wrapping.AfterCaseLabel); IO.mapOptional("AfterClass", Wrapping.AfterClass); IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement); IO.mapOptional("AfterEnum", Wrapping.AfterEnum); @@ -603,7 +604,7 @@ if (Style.BreakBeforeBraces == FormatStyle::BS_Custom) return Style; FormatStyle Expanded = Style; - Expanded.BraceWrapping = {false, false, false, false, false, + Expanded.BraceWrapping = {false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true}; switch (Style.BreakBeforeBraces) { @@ -628,6 +629,7 @@ Expanded.BraceWrapping.BeforeElse = true; break; case FormatStyle::BS_Allman: + Expanded.BraceWrapping.AfterCaseLabel = true; Expanded.BraceWrapping.AfterClass = true; Expanded.BraceWrapping.AfterControlStatement = true; Expanded.BraceWrapping.AfterEnum = true; @@ -641,7 +643,7 @@ break; case FormatStyle::BS_GNU: Expanded.BraceWrapping = {true, true, true, true, true, true, true, true, - true, true, true, true, true, true, true}; + true, true, true, true, true, true, true, true}; break; case FormatStyle::BS_WebKit: Expanded.BraceWrapping.AfterFunction = true; @@ -680,7 +682,7 @@ LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None; LLVMStyle.BreakBeforeTernaryOperators = true; LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; - LLVMStyle.BraceWrapping = {false, false, false, false, false, + LLVMStyle.BraceWrapping = {false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true}; LLVMStyle.BreakAfterJavaFieldAnnotations = false; Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -172,10 +172,16 @@ public: CompoundStatementIndenter(UnwrappedLineParser *Parser, const FormatStyle &Style, unsigned &LineLevel) + : CompoundStatementIndenter(Parser, LineLevel, + Style.BraceWrapping.AfterControlStatement, + Style.BraceWrapping.IndentBraces) { + } + CompoundStatementIndenter(UnwrappedLineParser *Parser, unsigned &LineLevel, + bool WrapBrace, bool IndentBrace) : LineLevel(LineLevel), OldLineLevel(LineLevel) { - if (Style.BraceWrapping.AfterControlStatement) + if (WrapBrace) Parser->addUnwrappedLine(); - if (Style.BraceWrapping.IndentBraces) + if (IndentBrace) ++LineLevel; } ~CompoundStatementIndenter() { LineLevel = OldLineLevel; } @@ -1950,7 +1956,9 @@ if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) --Line->Level; if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { - CompoundStatementIndenter Indenter(this, Style, Line->Level); + CompoundStatementIndenter Indenter(this, Line->Level, + Style.BraceWrapping.AfterCaseLabel, + Style.BraceWrapping.IndentBraces); parseBlock(/*MustBeDeclaration=*/false); if (FormatTok->Tok.is(tok::kw_break)) { if (Style.BraceWrapping.AfterControlStatement) Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -1117,6 +1117,7 @@ Style.IndentCaseLabels = true; Style.AllowShortBlocksOnASingleLine = false; Style.BreakBeforeBraces = FormatStyle::BS_Custom; + Style.BraceWrapping.AfterCaseLabel = true; Style.BraceWrapping.AfterControlStatement = true; EXPECT_EQ("switch (n)\n" "{\n" @@ -1138,6 +1139,27 @@ " }\n" "}", Style)); + Style.BraceWrapping.AfterCaseLabel = false; + EXPECT_EQ("switch (n)\n" + "{\n" + " case 0: {\n" + " return false;\n" + " }\n" + " default: {\n" + " return true;\n" + " }\n" + "}", + format("switch (n) {\n" + " case 0:\n" + " {\n" + " return false;\n" + " }\n" + " default:\n" + " {\n" + " return true;\n" + " }\n" + "}", + Style)); } TEST_F(FormatTest, CaseRanges) { @@ -1291,6 +1313,7 @@ Style)); Style.AllowShortCaseLabelsOnASingleLine = true; Style.BreakBeforeBraces = FormatStyle::BS_Custom; + Style.BraceWrapping.AfterCaseLabel = true; Style.BraceWrapping.AfterControlStatement = true; EXPECT_EQ("switch (n)\n" "{\n" @@ -11356,6 +11379,7 @@ CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); + CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement); CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); @@ -12841,20 +12865,18 @@ TEST_F(FormatTest, WrappedClosingParenthesisIndent) { FormatStyle Style = getLLVMStyle(); - verifyFormat( - "int Foo::getter(\n" - " //\n" - ") const {\n" - " return foo;\n" - "}", - Style); - verifyFormat( - "void Foo::setter(\n" - " //\n" - ") {\n" - " foo = 1;\n" - "}", - Style); + verifyFormat("int Foo::getter(\n" + " //\n" + ") const {\n" + " return foo;\n" + "}", + Style); + verifyFormat("void Foo::setter(\n" + " //\n" + ") {\n" + " foo = 1;\n" + "}", + Style); } TEST_F(FormatTest, SpacesInAngles) {