MyDeveloperDay updated this revision to Diff 189837. MyDeveloperDay added a comment.
Add missing Format.h from the review CHANGES SINCE LAST ACTION https://reviews.llvm.org/D59087/new/ https://reviews.llvm.org/D59087 Files: clang/docs/ClangFormatStyleOptions.rst clang/include/clang/Format/Format.h clang/lib/Format/Format.cpp clang/lib/Format/UnwrappedLineFormatter.cpp clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -487,6 +487,34 @@ verifyFormat("if (a)\n return;", AllowsMergedIf); } +TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { + FormatStyle AllowsMergedIf = getLLVMStyle(); + AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; + AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; + verifyFormat("if (a)\n" + " f();\n" + "else {\n" + " g();\n" + "}", + AllowsMergedIf); + + AllowsMergedIf.AllowShortIfElseStatementsOnASingleLine = true; + verifyFormat("if (a) f();\n" + "else {\n" + " g();\n" + "}", + AllowsMergedIf); + verifyFormat("if (a) f();\n" + "else {\n" + " if (a) f();\n" + " else {\n" + " g();\n" + " }\n" + " g();\n" + "}", + AllowsMergedIf); +} + TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { FormatStyle AllowsMergedLoops = getLLVMStyle(); AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -413,10 +413,12 @@ if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while, TT_LineComment)) return 0; - // Only inline simple if's (no nested if or else). - if (I + 2 != E && Line.startsWith(tok::kw_if) && - I[2]->First->is(tok::kw_else)) - return 0; + // Only inline simple if's (no nested if or else), unless specified + if (!Style.AllowShortIfElseStatementsOnASingleLine) { + if (I + 2 != E && Line.startsWith(tok::kw_if) && + I[2]->First->is(tok::kw_else)) + return 0; + } return 1; } Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -331,6 +331,8 @@ Style.AllowShortFunctionsOnASingleLine); IO.mapOptional("AllowShortIfStatementsOnASingleLine", Style.AllowShortIfStatementsOnASingleLine); + IO.mapOptional("AllowShortIfElseStatementsOnASingleLine", + Style.AllowShortIfElseStatementsOnASingleLine); IO.mapOptional("AllowShortLoopsOnASingleLine", Style.AllowShortLoopsOnASingleLine); IO.mapOptional("AlwaysBreakAfterDefinitionReturnType", @@ -632,6 +634,7 @@ LLVMStyle.AllowShortBlocksOnASingleLine = false; LLVMStyle.AllowShortCaseLabelsOnASingleLine = false; LLVMStyle.AllowShortIfStatementsOnASingleLine = false; + LLVMStyle.AllowShortIfElseStatementsOnASingleLine = false; LLVMStyle.AllowShortLoopsOnASingleLine = false; LLVMStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None; Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -244,6 +244,19 @@ /// If ``true``, ``if (a) return;`` can be put on a single line. bool AllowShortIfStatementsOnASingleLine; + /// When used in conjuction with ``AllowShortIfIfStatementsOnASingleLine`` + /// then when ``true``, ``if (a) return;`` can be put on a single even when + /// the else clause is a compound statement + /// \code + /// true: false: + /// if (a) return; vs. if (a) + /// else { return; + /// return; else { + /// } return; + /// } + /// \endcode + bool AllowShortIfElseStatementsOnASingleLine; + /// If ``true``, ``while (true) continue;`` can be put on a single /// line. bool AllowShortLoopsOnASingleLine; @@ -1728,6 +1741,8 @@ R.AllowShortFunctionsOnASingleLine && AllowShortIfStatementsOnASingleLine == R.AllowShortIfStatementsOnASingleLine && + AllowShortIfElseStatementsOnASingleLine == + R.AllowShortIfElseStatementsOnASingleLine && AllowShortLoopsOnASingleLine == R.AllowShortLoopsOnASingleLine && AlwaysBreakAfterReturnType == R.AlwaysBreakAfterReturnType && AlwaysBreakBeforeMultilineStrings == Index: clang/docs/ClangFormatStyleOptions.rst =================================================================== --- clang/docs/ClangFormatStyleOptions.rst +++ clang/docs/ClangFormatStyleOptions.rst @@ -365,11 +365,27 @@ }; void f() { bar(); } - - **AllowShortIfStatementsOnASingleLine** (``bool``) If ``true``, ``if (a) return;`` can be put on a single line. + .. code-block:: c++ + + if (a) return; + else + return; + +**AllowShortIfElseStatementsOnASingleLine** (``bool``) + When used in conjuction with ``AllowShortIfIfStatementsOnASingleLine`` + then when ``true``, ``if (a) return;`` can be put on a single even when + the else clause is a compound statement + + .. code-block:: c++ + + if (a) return; + else { + return; + } + **AllowShortLoopsOnASingleLine** (``bool``) If ``true``, ``while (true) continue;`` can be put on a single line.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits