curdeius created this revision. curdeius added reviewers: MyDeveloperDay, HazardyKnusperkeks, owenpan. curdeius requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Fixes https://github.com/llvm/llvm-project/issues/50051. Given the style: BraceWrapping AfterFunction: true SplitEmptyFunction: true SplitEmptyRecord: false ... The code that should be like: void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbbbbbbbbbbbbb) { } gets the braces merged together: void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbbbbbbbbbbbbb) {} Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D116049 Files: 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 @@ -12192,6 +12192,38 @@ "}", Style); } + +TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) { + FormatStyle Style = getLLVMStyle(); + Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; + Style.BreakBeforeBraces = FormatStyle::BS_Custom; + Style.BraceWrapping.AfterFunction = true; + Style.BraceWrapping.SplitEmptyFunction = true; + Style.BraceWrapping.SplitEmptyRecord = false; + Style.ColumnLimit = 40; + + verifyFormat("class C {};", Style); + verifyFormat("struct C {};", Style); + verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" + "{\n" + "}", + Style); + verifyFormat("class C {\n" + " C()\n" + " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n" + " bbbbbbbbbbbbbbbbbbb()\n" + " {\n" + " }\n" + " void\n" + " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" + " {\n" + " }\n" + "};", + Style); +} + TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { FormatStyle Style = getLLVMStyle(); Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; @@ -19430,15 +19462,13 @@ TEST_F(FormatTest, CountsUTF8CharactersProperly) { verifyFormat("\"ÐÐ´Ð½Ð°Ð¶Ð´Ñ Ð² ÑÑÑдÑнÑÑ Ð·Ð¸Ð¼Ð½ÑÑ Ð¿Ð¾ÑÑ...\"", getLLVMStyleWithColumns(35)); - verifyFormat("\"ä¸ äº ä¸ å äº å ä¸ å « ä¹ å\"", - getLLVMStyleWithColumns(31)); + verifyFormat("\"ä¸ äº ä¸ å äº å ä¸ å « ä¹ å\"", getLLVMStyleWithColumns(31)); verifyFormat("// ÐÐ´Ð½Ð°Ð¶Ð´Ñ Ð² ÑÑÑдÑнÑÑ Ð·Ð¸Ð¼Ð½ÑÑ Ð¿Ð¾ÑÑ...", getLLVMStyleWithColumns(36)); verifyFormat("// ä¸ äº ä¸ å äº å ä¸ å « ä¹ å", getLLVMStyleWithColumns(32)); verifyFormat("/* ÐÐ´Ð½Ð°Ð¶Ð´Ñ Ð² ÑÑÑдÑнÑÑ Ð·Ð¸Ð¼Ð½ÑÑ Ð¿Ð¾ÑÑ... */", getLLVMStyleWithColumns(39)); - verifyFormat("/* ä¸ äº ä¸ å äº å ä¸ å « ä¹ å */", - getLLVMStyleWithColumns(35)); + verifyFormat("/* ä¸ äº ä¸ å äº å ä¸ å « ä¹ å */", getLLVMStyleWithColumns(35)); } TEST_F(FormatTest, SplitsUTF8Strings) { @@ -19458,21 +19488,20 @@ "\"поÑÑ,\"", format("\"ÐднаждÑ, в ÑÑÑдÑнÑÑ Ð·Ð¸Ð¼Ð½ÑÑ Ð¿Ð¾ÑÑ,\"", getLLVMStyleWithColumns(13))); + EXPECT_EQ("\"ä¸ äº ä¸ \"\n" + "\"å äºå \"\n" + "\"ä¸ å « ä¹ \"\n" + "\"å\"", + format("\"ä¸ äº ä¸ å äºå ä¸ å « ä¹ å\"", getLLVMStyleWithColumns(11))); EXPECT_EQ( - "\"ä¸ äº ä¸ \"\n" - "\"å äºå \"\n" - "\"ä¸ å « ä¹ \"\n" - "\"å\"", - format("\"ä¸ äº ä¸ å äºå ä¸ å « ä¹ å\"", getLLVMStyleWithColumns(11))); - EXPECT_EQ("\"ä¸\t\"\n" - "\"äº \t\"\n" - "\"ä¸ å \"\n" - "\"äº\t\"\n" - "\"å \t\"\n" - "\"ä¸ \"\n" - "\"å «ä¹å\tqq\"", - format("\"ä¸\täº \tä¸ å äº\tå \tä¸ å «ä¹å\tqq\"", - getLLVMStyleWithColumns(11))); + "\"ä¸\t\"\n" + "\"äº \t\"\n" + "\"ä¸ å \"\n" + "\"äº\t\"\n" + "\"å \t\"\n" + "\"ä¸ \"\n" + "\"å «ä¹å\tqq\"", + format("\"ä¸\täº \tä¸ å äº\tå \tä¸ å «ä¹å\tqq\"", getLLVMStyleWithColumns(11))); // UTF8 character in an escape sequence. EXPECT_EQ("\"aaaaaa\"\n" @@ -19517,16 +19546,16 @@ format("/* ÐлÑжÑ, поднимаеÑÑÑ Ð¼ÐµÐ´Ð»ÐµÐ½Ð½Ð¾ в гоÑÑ\n" " * ÐоÑадка, везÑÑÐ°Ñ Ñ Ð²Ð¾ÑоÑÑÑ Ð²Ð¾Ð·. */", getLLVMStyleWithColumns(13))); - EXPECT_EQ( - "/* ä¸äºä¸\n" - " * åäºå ä¸\n" - " * å « ä¹\n" - " * å */", - format("/* ä¸äºä¸ åäºå ä¸ å « ä¹ å */", getLLVMStyleWithColumns(9))); + EXPECT_EQ("/* ä¸äºä¸\n" + " * åäºå ä¸\n" + " * å « ä¹\n" + " * å */", + format("/* ä¸äºä¸ åäºå ä¸ å « ä¹ å */", getLLVMStyleWithColumns(9))); EXPECT_EQ("/* ð£ð®ð¼ð½ ð£ð¬ð²ð¯\n" " * ððªð¥ð\n" " * ðð¿ð±-ð */", - format("/* ð£ð®ð¼ð½ ð£ð¬ð²ð¯ ððªð¥ð ðð¿ð±-ð */", getLLVMStyleWithColumns(12))); + format("/* ð£ð®ð¼ð½ ð£ð¬ð²ð¯ ððªð¥ð ðð¿ð±-ð */", + getLLVMStyleWithColumns(12))); } #endif // _MSC_VER Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -393,11 +393,17 @@ // Try to merge a block with left brace wrapped that wasn't yet covered if (TheLine->Last->is(tok::l_brace)) { - return !Style.BraceWrapping.AfterFunction || - (I[1]->First->is(tok::r_brace) && - !Style.BraceWrapping.SplitEmptyRecord) - ? tryMergeSimpleBlock(I, E, Limit) - : 0; + bool ShouldMerge = false; + if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct)) { + ShouldMerge = !Style.BraceWrapping.AfterClass || + (I[1]->First->is(tok::r_brace) && + !Style.BraceWrapping.SplitEmptyRecord); + } else { + ShouldMerge = !Style.BraceWrapping.AfterFunction || + (I[1]->First->is(tok::r_brace) && + !Style.BraceWrapping.SplitEmptyFunction); + } + return ShouldMerge ? tryMergeSimpleBlock(I, E, Limit) : 0; } // Try to merge a function block with left brace wrapped if (I[1]->First->is(TT_FunctionLBrace) &&
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits