https://github.com/rudolflovrencic updated https://github.com/llvm/llvm-project/pull/196021
From 4a3973d8d5bc8de3ece4d6bd09cf4ca21ded16bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rudolf=20Lovren=C4=8Di=C4=87?= <[email protected]> Date: Wed, 6 May 2026 10:00:04 +0200 Subject: [PATCH] [clang-format] AllowShortBlocksOnASingleLine is overridden by If and Loop specific options AllowShortLoopsOnASingleLine and AllowShortIfStatementsOnASingleLine now override AllowShortBlocksOnASingleLine. Fixes #183705 and #187993 --- clang/docs/ClangFormatStyleOptions.rst | 13 ++ clang/docs/ReleaseNotes.rst | 6 + clang/include/clang/Format/Format.h | 13 ++ clang/lib/Format/UnwrappedLineFormatter.cpp | 63 +++++-- clang/unittests/Format/FormatTest.cpp | 172 ++++++++++-------- clang/unittests/Format/FormatTestCSharp.cpp | 19 +- clang/unittests/Format/FormatTestJS.cpp | 48 ++--- clang/unittests/Format/FormatTestTableGen.cpp | 4 +- 8 files changed, 198 insertions(+), 140 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 7b1b7a7384b07..c808dfc6bf448 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -1827,6 +1827,11 @@ the configuration (without a prefix: ``Auto``). Dependent on the value, ``while (true) { continue; }`` can be put on a single line. + For blocks of if statements and loops, this option is overridden by + ``AllowShortIfStatementsOnASingleLine`` and + ``AllowShortLoopsOnASingleLine``, respectively, if they allow merging + the statement. + Possible values: * ``SBS_Never`` (in configuration: ``Never``) @@ -2047,6 +2052,10 @@ the configuration (without a prefix: ``Auto``). **AllowShortIfStatementsOnASingleLine** (``ShortIfStyle``) :versionbadge:`clang-format 3.3` :ref:`¶ <AllowShortIfStatementsOnASingleLine>` Dependent on the value, ``if (a) return;`` can be put on a single line. + If merging is allowed, it also applies to braced if statements, e.g. + ``if (a) { return; }``, overriding ``AllowShortBlocksOnASingleLine`` + and ``BraceWrapping.AfterControlStatement``. + Possible values: * ``SIS_Never`` (in configuration: ``Never``) @@ -2169,6 +2178,10 @@ the configuration (without a prefix: ``Auto``). If ``true``, ``while (true) continue;`` can be put on a single line. + This also applies to braced loops, e.g. ``while (true) { continue; }``, + overriding ``AllowShortBlocksOnASingleLine`` and + ``BraceWrapping.AfterControlStatement``. + .. _AllowShortNamespacesOnASingleLine: **AllowShortNamespacesOnASingleLine** (``Boolean``) :versionbadge:`clang-format 20` :ref:`¶ <AllowShortNamespacesOnASingleLine>` diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 78accb48547e5..91735f7ffa040 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -971,6 +971,12 @@ clang-format enum assignments without affecting other assignments. - Add ``BreakBeforeReturnType`` option to break before the function return type. +- ``AllowShortIfStatementsOnASingleLine`` and ``AllowShortLoopsOnASingleLine`` + now override ``AllowShortBlocksOnASingleLine`` and + ``BraceWrapping.AfterControlStatement`` for the blocks of their constructs. + In particular, braced bodies of short if statements and loops are now merged + onto a single line if the respective option allows it, even if + ``AllowShortBlocksOnASingleLine`` is ``Never``. (#GH183705) libclang -------- diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 27b2d8f4a405b..03ae3106ce3e8 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -767,6 +767,11 @@ struct FormatStyle { /// Dependent on the value, ``while (true) { continue; }`` can be put on a /// single line. + /// + /// For blocks of if statements and loops, this option is overridden by + /// ``AllowShortIfStatementsOnASingleLine`` and + /// ``AllowShortLoopsOnASingleLine``, respectively, if they allow merging + /// the statement. /// \version 3.5 ShortBlockStyle AllowShortBlocksOnASingleLine; @@ -1023,6 +1028,10 @@ struct FormatStyle { }; /// Dependent on the value, ``if (a) return;`` can be put on a single line. + /// + /// If merging is allowed, it also applies to braced if statements, e.g. + /// ``if (a) { return; }``, overriding ``AllowShortBlocksOnASingleLine`` + /// and ``BraceWrapping.AfterControlStatement``. /// \version 3.3 ShortIfStyle AllowShortIfStatementsOnASingleLine; @@ -1062,6 +1071,10 @@ struct FormatStyle { /// If ``true``, ``while (true) continue;`` can be put on a single /// line. + /// + /// This also applies to braced loops, e.g. ``while (true) { continue; }``, + /// overriding ``AllowShortBlocksOnASingleLine`` and + /// ``BraceWrapping.AfterControlStatement``. /// \version 3.7 bool AllowShortLoopsOnASingleLine; diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 42eabc065b1a8..33ab186a3f471 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -231,6 +231,19 @@ class LineJoiner { } private: + /// Returns whether the construct-specific ``AllowShort*`` option permits + /// merging the control statement that \p Tok begins. These options override + /// ``AllowShortBlocksOnASingleLine`` for the blocks of their constructs. + bool allowShortControlStatement(const FormatToken &Tok) const { + if (Tok.is(tok::kw_if)) { + return Style.AllowShortIfStatementsOnASingleLine != + FormatStyle::SIS_Never; + } + if (Tok.isOneOf(tok::kw_while, tok::kw_for, TT_ForEachMacro)) + return Style.AllowShortLoopsOnASingleLine; + return false; + } + /// Calculates how many lines can be merged into 1 starting at \p I. unsigned tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker, @@ -264,13 +277,10 @@ class LineJoiner { : Limit - TheLine->Last->TotalLength; if (TheLine->Last->is(TT_FunctionLBrace) && - TheLine->First == TheLine->Last) { - const bool EmptyFunctionBody = NextLine.First->is(tok::r_brace); - if ((EmptyFunctionBody && !Style.BraceWrapping.SplitEmptyFunction) || - (!EmptyFunctionBody && - Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Always)) { - return tryMergeSimpleBlock(I, E, Limit); - } + TheLine->First == TheLine->Last && + !Style.BraceWrapping.SplitEmptyFunction && + NextLine.First->is(tok::r_brace)) { + return tryMergeSimpleBlock(I, E, Limit); } // Try merging record blocks that have had their left brace wrapped into @@ -445,7 +455,9 @@ class LineJoiner { (FirstNonComment->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for, TT_ForEachMacro) || TheLine->startsWithExportBlock())) { - return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never + return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never || + (TheLine->Last->is(TT_ControlStatementLBrace) && + allowShortControlStatement(*FirstNonComment)) ? tryMergeSimpleBlock(I, E, Limit) : 0; } @@ -647,11 +659,8 @@ class LineJoiner { const bool IsEmptyBlock = Line->Last->is(tok::l_brace) && NextLine->First->is(tok::r_brace); - if ((IsEmptyBlock && !Style.BraceWrapping.SplitEmptyRecord) || - (!IsEmptyBlock && - Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Always)) { + if (IsEmptyBlock && !Style.BraceWrapping.SplitEmptyRecord) return tryMergeSimpleBlock(I, E, Limit); - } } return 0; @@ -888,13 +897,26 @@ class LineJoiner { (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Empty && I[1]->First->isNot(tok::r_brace)); + // Whether the construct-specific AllowShort* option permits merging the + // control statement that begins on this line, overriding + // AllowShortBlocksOnASingleLine. The line either ends with the block's + // left brace or is followed by a line starting with the wrapped brace. + const auto *LBrace = Line.Last->is(tok::l_brace) ? Line.Last : I[1]->First; + const bool MergeShortStatement = LBrace->is(TT_ControlStatementLBrace) && + allowShortControlStatement(*Line.First); + if (IsCtrlStmt(Line) || Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch, tok::kw___finally, tok::r_brace, Keywords.kw___except) || Line.startsWithExportBlock()) { - if (IsSplitBlock) + if (IsSplitBlock && !MergeShortStatement) return 0; + // The construct-specific options AllowShortIfStatementsOnASingleLine and + // AllowShortLoopsOnASingleLine take precedence over + // AllowShortBlocksOnASingleLine: a statement whose specific option + // disallows merging is not put on a single line even when short blocks + // are always allowed. // Don't merge when we can't except the case when // the control statement block is empty if (!Style.AllowShortIfStatementsOnASingleLine && @@ -944,7 +966,9 @@ class LineJoiner { if (IsSplitBlock && Line.First == Line.Last && I > AnnotatedLines.begin() && - (I[-1]->endsWith(tok::kw_else) || IsCtrlStmt(*I[-1]))) { + (I[-1]->endsWith(tok::kw_else) || IsCtrlStmt(*I[-1])) && + !(Line.First->is(TT_ControlStatementLBrace) && + allowShortControlStatement(*I[-1]->First))) { return 0; } FormatToken *Tok = I[1]->First; @@ -988,9 +1012,11 @@ class LineJoiner { if (I[1]->Last->is(TT_LineComment)) return 0; do { - if (Tok->isOneOf(tok::l_brace, tok::r_brace) && - Tok->isNot(BK_BracedInit)) { - return 0; + if (Tok->isOneOf(tok::l_brace, tok::r_brace)) { + const FormatToken *Open = + Tok->is(tok::l_brace) ? Tok : Tok->MatchingParen; + if (!Open || Open->isNot(BK_BracedInit)) + return 0; } Tok = Tok->Next; } while (Tok); @@ -1031,7 +1057,8 @@ class LineJoiner { auto TryMergeBlock = [&] { if (Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never || - Style.AllowShortRecordOnASingleLine == FormatStyle::SRS_Always) { + Style.AllowShortRecordOnASingleLine == FormatStyle::SRS_Always || + MergeShortStatement) { return true; } return I[1]->First == I[1]->Last && I + 2 != E && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 6e1150227c452..34d6b6aafccae 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -320,9 +320,7 @@ TEST_F(FormatTest, RemovesEmptyLines) { "\n" "}"); verifyFormat("void f() {\n" - " if (a) {\n" - " f();\n" - " }\n" + " if (a) { f(); }\n" "}", "void f() {\n" "\n" @@ -773,10 +771,7 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { AllowsMergedIf); verifyFormat("if (a) g();", AllowsMergedIf); - verifyFormat("if (a) {\n" - " g()\n" - "};", - AllowsMergedIf); + verifyFormat("if (a) { g() };", AllowsMergedIf); verifyFormat("if (a)\n" " g();\n" "else\n" @@ -857,10 +852,7 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { AllowsMergedIf); verifyFormat("MYIF (a) g();", AllowsMergedIf); - verifyFormat("MYIF (a) {\n" - " g()\n" - "};", - AllowsMergedIf); + verifyFormat("MYIF (a) { g() };", AllowsMergedIf); verifyFormat("MYIF (a)\n" " g();\n" "else\n" @@ -993,10 +985,7 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { AllowsMergedIf); verifyFormat("if (a) g();", AllowsMergedIf); - verifyFormat("if (a) {\n" - " g()\n" - "};", - AllowsMergedIf); + verifyFormat("if (a) { g() };", AllowsMergedIf); verifyFormat("if (a) g();\n" "else\n" " g();", @@ -1074,10 +1063,7 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { AllowsMergedIf); verifyFormat("MYIF (a) g();", AllowsMergedIf); - verifyFormat("MYIF (a) {\n" - " g()\n" - "};", - AllowsMergedIf); + verifyFormat("MYIF (a) { g() };", AllowsMergedIf); verifyFormat("MYIF (a) g();\n" "else\n" " g();", @@ -1200,10 +1186,7 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { AllowsMergedIf); verifyFormat("if (a) g();", AllowsMergedIf); - verifyFormat("if (a) {\n" - " g()\n" - "};", - AllowsMergedIf); + verifyFormat("if (a) { g() };", AllowsMergedIf); verifyFormat("if (a) g();\n" "else g();", AllowsMergedIf); @@ -1273,10 +1256,7 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { AllowsMergedIf); verifyFormat("MYIF (a) g();", AllowsMergedIf); - verifyFormat("MYIF (a) {\n" - " g()\n" - "};", - AllowsMergedIf); + verifyFormat("MYIF (a) { g() };", AllowsMergedIf); verifyFormat("MYIF (a) g();\n" "else g();", AllowsMergedIf); @@ -1500,10 +1480,7 @@ TEST_F(FormatTest, FormatShortBracedStatements) { FormatStyle::SIS_WithoutElse; verifyFormat("if (true) {}", AllowSimpleBracedStatements); verifyFormat("if (i) break;", AllowSimpleBracedStatements); - verifyFormat("if (i > 0) {\n" - " return i;\n" - "}", - AllowSimpleBracedStatements); + verifyFormat("if (i > 0) { return i; }", AllowSimpleBracedStatements); AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); // Where line-lengths matter, a 2-letter synonym that maintains line length. @@ -1846,6 +1823,56 @@ TEST_F(FormatTest, FormatShortBracedStatements) { Style); } +TEST_F(FormatTest, ShortControlStatementsOverrideShortBlocks) { + // AllowShortIfStatementsOnASingleLine and AllowShortLoopsOnASingleLine + // override AllowShortBlocksOnASingleLine for the blocks of their + // constructs. + auto Style = getLLVMStyle(); + Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; + Style.AllowShortLoopsOnASingleLine = true; + ASSERT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); + + verifyFormat("if (b) { return; }", Style); + verifyFormat("if (b) {}", Style); + verifyFormat("while (b) { --b; }", Style); + verifyFormat("for (;;) { f(); }", Style); + verifyFormat("if (b) { return; }", + "if (b)\n" + "{\n" + " return;\n" + "}", + Style); + + // They also override BraceWrapping.AfterControlStatement. + Style.BreakBeforeBraces = FormatStyle::BS_Custom; + Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; + verifyFormat("if (b) { return; }", Style); + verifyFormat("while (b) { --b; }", Style); + + // Blocks of other constructs are still controlled by + // AllowShortBlocksOnASingleLine (https://llvm.org/PR183705). + Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; + Style.BraceWrapping.AfterFunction = true; + Style.AllowShortFunctionsOnASingleLine = {}; + verifyFormat("void f(bool b)\n" + "{\n" + " if (b) { return; }\n" + "}", + Style); + + // Statements that are not allowed to be merged are not affected. + Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; + Style.AllowShortLoopsOnASingleLine = false; + verifyFormat("if (b) {\n" + " return;\n" + "}", + Style); + verifyFormat("while (b) {\n" + " --b;\n" + "}", + Style); +} + TEST_F(FormatTest, UnderstandsMacros) { verifyFormat("#define A (parentheses)"); verifyFormat("/* comment */ #define A (parentheses)"); @@ -2441,18 +2468,15 @@ TEST_F(FormatTest, ForEachLoops) { "}", ShortBlocks); + // AllowShortLoopsOnASingleLine overrides AllowShortBlocksOnASingleLine. FormatStyle ShortLoops = getLLVMStyle(); ShortLoops.AllowShortLoopsOnASingleLine = true; EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); verifyFormat("void f() {\n" " for (;;) int j = 1;\n" " Q_FOREACH (int &v, vec) int j = 1;\n" - " for (;;) {\n" - " int j = 1;\n" - " }\n" - " Q_FOREACH (int &v, vec) {\n" - " int j = 1;\n" - " }\n" + " for (;;) { int j = 1; }\n" + " Q_FOREACH (int &v, vec) { int j = 1; }\n" "}", ShortLoops); @@ -10179,21 +10203,20 @@ TEST_F(FormatTest, DeclarationsOfMultipleVariables) { " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", Style); verifyFormat("vector<int*> a, b;", Style); - verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); - verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {\n}", Style); - verifyFormat("if (int *p, *q; p != q) {\n p = p->next;\n}", Style); - verifyFormat("/*comment*/ if (int *p, *q; p != q) {\n p = p->next;\n}", - Style); + verifyFormat("for (int *p, *q; p != q; p = p->next) {}", Style); + verifyFormat("/*comment*/ for (int *p, *q; p != q; p = p->next) {}", Style); + verifyFormat("if (int *p, *q; p != q) { p = p->next; }", Style); + verifyFormat("/*comment*/ if (int *p, *q; p != q) { p = p->next; }", Style); verifyFormat("switch (int *p, *q; p != q) {\n default:\n break;\n}", Style); verifyFormat( "/*comment*/ switch (int *p, *q; p != q) {\n default:\n break;\n}", Style); - verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style); - verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style); - verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style); - verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style); + verifyFormat("if ([](int* p, int* q) {}()) {}", Style); + verifyFormat("for ([](int* p, int* q) {}();;) {}", Style); + verifyFormat("for (; [](int* p, int* q) {}();) {}", Style); + verifyFormat("for (;; [](int* p, int* q) {}()) {}", Style); verifyFormat("switch ([](int* p, int* q) {}()) {\n default:\n break;\n}", Style); } @@ -12486,7 +12509,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { "}"); verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); - verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); + verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {}"); verifyFormat("#define A (!a * b)"); verifyFormat("#define MACRO \\\n" @@ -15560,11 +15583,24 @@ TEST_F(FormatTest, MergeShortFunctionBody) { auto Style = getLLVMStyle(); Style.AllowShortFunctionsOnASingleLine = FormatStyle::ShortFunctionStyle(); Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; + Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; Style.BreakBeforeBraces = FormatStyle::BS_Custom; Style.BraceWrapping.AfterFunction = true; verifyFormat("int foo()\n" - "{ return 1; }", + "{\n" + " return 1;\n" + "}\n", + Style); + verifyFormat("int foo()\n" + "{\n" + " if (true) { return 42; };\n" + "}\n", + Style); + verifyFormat("int foo()\n" + "{\n" + " static constexpr auto lambda = []() -> int { return 42; };\n" + "}\n", Style); } @@ -15913,11 +15949,15 @@ TEST_F(FormatTest, AllowShortRecordOnASingleLine) { Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Never; verifyFormat("class foo\n" - "{ int i; };", + "{\n" + " int i;\n" + "};", Style); Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Empty; verifyFormat("class foo\n" - "{ int i; };", + "{\n" + " int i;\n" + "};", Style); Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Always; verifyFormat("class foo\n" @@ -19334,32 +19374,25 @@ TEST_F(FormatTest, AllmanBraceBreaking) { AllmanBraceStyle); AllmanBraceStyle.ColumnLimit = 80; + // The construct-specific AllowShort* options override + // AllowShortBlocksOnASingleLine and BraceWrapping.AfterControlStatement. FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; verifyFormat("void f(bool b)\n" "{\n" - " if (b)\n" - " {\n" - " return;\n" - " }\n" + " if (b) { return; }\n" "}", BreakBeforeBraceShortIfs); verifyFormat("void f(bool b)\n" "{\n" - " if constexpr (b)\n" - " {\n" - " return;\n" - " }\n" + " if constexpr (b) { return; }\n" "}", BreakBeforeBraceShortIfs); verifyFormat("void f(bool b)\n" "{\n" - " if CONSTEXPR (b)\n" - " {\n" - " return;\n" - " }\n" + " if CONSTEXPR (b) { return; }\n" "}", BreakBeforeBraceShortIfs); verifyFormat("void f(bool b)\n" @@ -19379,10 +19412,7 @@ TEST_F(FormatTest, AllmanBraceBreaking) { BreakBeforeBraceShortIfs); verifyFormat("void f(bool b)\n" "{\n" - " while (b)\n" - " {\n" - " return;\n" - " }\n" + " while (b) { return; }\n" "}", BreakBeforeBraceShortIfs); } @@ -19759,10 +19789,7 @@ TEST_F(FormatTest, WhitesmithsBraceBreaking) { BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; verifyFormat("void f(bool b)\n" " {\n" - " if (b)\n" - " {\n" - " return;\n" - " }\n" + " if (b) { return; }\n" " }", BreakBeforeBraceShortIfs); verifyFormat("void f(bool b)\n" @@ -19772,10 +19799,7 @@ TEST_F(FormatTest, WhitesmithsBraceBreaking) { BreakBeforeBraceShortIfs); verifyFormat("void f(bool b)\n" " {\n" - " while (b)\n" - " {\n" - " return;\n" - " }\n" + " while (b) { return; }\n" " }", BreakBeforeBraceShortIfs); } diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp index 805fe6f9bd3c2..35fa26b5c56b4 100644 --- a/clang/unittests/Format/FormatTestCSharp.cpp +++ b/clang/unittests/Format/FormatTestCSharp.cpp @@ -583,12 +583,8 @@ TEST_F(FormatTestCSharp, CSharpSpaceBefore) { verifyFormat("List<string> list;", Style); verifyFormat("Dictionary<string, string> dict;", Style); - verifyFormat("for (int i = 0; i < size (); i++) {\n" - "}", - Style); - verifyFormat("foreach (var x in y) {\n" - "}", - Style); + verifyFormat("for (int i = 0; i < size (); i++) {}", Style); + verifyFormat("foreach (var x in y) {}", Style); verifyFormat("switch (x) {}", Style); verifyFormat("do {\n" "} while (x);", @@ -599,12 +595,8 @@ TEST_F(FormatTestCSharp, CSharpSpaceBefore) { verifyFormat("List<string> list;", Style); verifyFormat("Dictionary<string, string> dict;", Style); - verifyFormat("for(int i = 0; i < size(); i++) {\n" - "}", - Style); - verifyFormat("foreach(var x in y) {\n" - "}", - Style); + verifyFormat("for(int i = 0; i < size(); i++) {}", Style); + verifyFormat("foreach(var x in y) {}", Style); verifyFormat("switch(x) {}", Style); verifyFormat("do {\n" "} while(x);", @@ -1222,8 +1214,7 @@ TEST_F(FormatTestCSharp, CSharpSpaces) { // Not seen as a C-style cast. verifyFormat(R"(// -foreach ((A a, B b) in someList) { -})", +foreach ((A a, B b) in someList) {})", Style); // space after lock in `lock (processes)`. diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index b405a73bce086..e000a67ce5d08 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -871,13 +871,9 @@ TEST_F(FormatTestJS, AsyncFunctions) { " // Comment.\n" " return async.then();\n" "}"); - verifyFormat("for await (const x of y) {\n" - " console.log(x);\n" - "}"); + verifyFormat("for await (const x of y) { console.log(x); }"); verifyFormat("function asyncLoop() {\n" - " for await (const x of y) {\n" - " console.log(x);\n" - " }\n" + " for await (const x of y) { console.log(x); }\n" "}"); } @@ -1334,18 +1330,12 @@ TEST_F(FormatTestJS, ReturnStatements) { } TEST_F(FormatTestJS, ForLoops) { - verifyFormat("for (var i in [2, 3]) {\n" - "}"); - verifyFormat("for (var i of [2, 3]) {\n" - "}"); - verifyFormat("for (let {a, b} of x) {\n" - "}"); - verifyFormat("for (let {a, b} of [x]) {\n" - "}"); - verifyFormat("for (let [a, b] of [x]) {\n" - "}"); - verifyFormat("for (let {a, b} in x) {\n" - "}"); + verifyFormat("for (var i in [2, 3]) {}"); + verifyFormat("for (var i of [2, 3]) {}"); + verifyFormat("for (let {a, b} of x) {}"); + verifyFormat("for (let {a, b} of [x]) {}"); + verifyFormat("for (let [a, b] of [x]) {}"); + verifyFormat("for (let {a, b} in x) {}"); } TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) { @@ -1475,13 +1465,11 @@ TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) { "} class Y {}", " x = {a : 1}\n" " class Y { }"); - verifyFormat("if (x) {\n" - "}\n" + verifyFormat("if (x) {}\n" "return 1", "if (x) {}\n" " return 1"); - verifyFormat("if (x) {\n" - "}\n" + verifyFormat("if (x) {}\n" "class X {}", "if (x) {}\n" " class X {}"); @@ -1495,8 +1483,7 @@ TEST_F(FormatTestJS, ImportExportASI) { // Below "class Y {}" should ideally be on its own line. verifyFormat("export {x} class Y {}", " export {x}\n" " class Y {\n}"); - verifyFormat("if (x) {\n" - "}\n" + verifyFormat("if (x) {}\n" "export class Y {}", "if ( x ) { }\n" " export class Y {}"); @@ -1549,7 +1536,7 @@ TEST_F(FormatTestJS, RegexLiteralClassification) { verifyFormat("f(abc, /abc/);"); verifyFormat("some_map[/abc/];"); verifyFormat("var x = a ? /abc/ : /abc/;"); - verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); + verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {}"); verifyFormat("var x = !/abc/.test(y);"); verifyFormat("var x = foo()! / 10;"); verifyFormat("var x = a && /abc/.test(y);"); @@ -1672,8 +1659,8 @@ TEST_F(FormatTestJS, TypeAnnotations) { verifyFormat("function x(): string {\n return 'x';\n}"); verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}"); verifyFormat("function x(y: string): string {\n return 'x';\n}"); - verifyFormat("for (var y: string in x) {\n x();\n}"); - verifyFormat("for (var y: string of x) {\n x();\n}"); + verifyFormat("for (var y: string in x) { x(); }"); + verifyFormat("for (var y: string of x) { x(); }"); verifyFormat("function x(y: {a?: number;} = {}): number {\n" " return 12;\n" "}"); @@ -2297,7 +2284,7 @@ TEST_F(FormatTestJS, WrapAfterParen) { " });", getGoogleJSStyleWithColumns(40)); verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" - " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); + " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}"); } TEST_F(FormatTestJS, JSDocAnnotations) { @@ -2495,7 +2482,7 @@ TEST_F(FormatTestJS, NonNullAssertionOperator) { verifyFormat("let x = foo!.bar();"); verifyFormat("let x = foo ? bar! : baz;"); verifyFormat("let x = !foo;"); - verifyFormat("if (!+a) {\n}"); + verifyFormat("if (!+a) {}"); verifyFormat("let x = foo[0]!;"); verifyFormat("let x = (foo)!;"); verifyFormat("let x = x(foo!);"); @@ -2903,8 +2890,7 @@ TEST_F(FormatTestJS, BreakAfterOpenBracket) { Style); verifyFormat("for await (const packageId of ops.api.iterateEmbeddedFiles(\n" " this.getFileId().getDriveFile(),\n" - " )) {\n" - "}", + " )) {}", Style); } diff --git a/clang/unittests/Format/FormatTestTableGen.cpp b/clang/unittests/Format/FormatTestTableGen.cpp index df20cc26e1094..4beccd1802067 100644 --- a/clang/unittests/Format/FormatTestTableGen.cpp +++ b/clang/unittests/Format/FormatTestTableGen.cpp @@ -290,9 +290,7 @@ TEST_F(FormatTestTableGen, MultiClass) { " } else {\n" " def : IfElse<x>;\n" " }\n" - " if (dagid x, 0) then {\n" - " def : If2<1>;\n" - " }\n" + " if (dagid x, 0) then { def : If2<1>; }\n" " let y = 1, z = 2 in {\n" " multiclass Multiclass2<int x> {\n" " foreach i = [1, 2, 3] in {\n" _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
