[clang] [clang-format] Add option AllowShortRecordOnASingleLine (PR #154580)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 3 22:26:23 PDT 2025
=?utf-8?q?Tomáš?= Slanina <itzexpoexpo at gmail.com>,
=?utf-8?q?Tomáš?= Slanina <itzexpoexpo at gmail.com>,
=?utf-8?q?Tomáš?= Slanina <itzexpoexpo at gmail.com>,
=?utf-8?q?Tomáš?= Slanina <itzexpoexpo at gmail.com>,
=?utf-8?q?Tomáš?= Slanina <itzexpoexpo at gmail.com>,
=?utf-8?q?Tomáš?= Slanina <itzexpoexpo at gmail.com>,
=?utf-8?q?Tomáš?= Slanina <itzexpoexpo at gmail.com>,
=?utf-8?q?Tomáš?= Slanina <itzexpoexpo at gmail.com>,
=?utf-8?q?Tomáš?= Slanina <itzexpoexpo at gmail.com>,
=?utf-8?q?Tomáš?= Slanina <itzexpoexpo at gmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/154580 at github.com>
================
@@ -15277,6 +15290,123 @@ TEST_F(FormatTest, NeverMergeShortRecords) {
Style);
}
+TEST_F(FormatTest, AllowShortRecordOnASingleLineNonSplit) {
+ FormatStyle Style = getLLVMStyle();
+
+ Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+ Style.BraceWrapping.SplitEmptyRecord = false;
----------------
owenca wrote:
`SplitEmptyRecord` is irrelevant if `BreakBeforeBraces` is the default `BS_Attach`. Basically, what I was looking for is as follows:
```c++
auto Style = getLLVMStyle();
EXPECT_EQ(Style.AllowShortRecordOnASingleLine, FormatStyle::SRS_EmptyIfAttached);
// No need to add test cases for the default.
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Never;
verifyFormat("class foo {\n"
"};"
"class bar {\n"
" int i;\n"
"};",
Style);
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWraping.AfterClass = true;
verifyFormat("class foo\n"
"{\n"
"};"
"class bar\n"
"{\n"
" int i;\n"
"};",
Style);
Style.SplitEmptyRecord = false;
verifyFormat("class foo\n"
"{};",
// No need to repeat the non-empty class test case.
Style);
Style = getLLVMStyle();
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Empty;
verifyFormat("class foo {};\n"
"class bar {\n"
" int i;\n"
"};",
Style);
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWraping.AfterClass = true;
verifyFormat("class foo {};\n"
"{\n"
"};"
"class bar\n"
"{\n"
" int i;\n"
"};",
Style);
Style.SplitEmptyRecord = false;
verifyFormat("class foo {};", Style);
Style = getLLVMStyle();
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Always;
verifyFormat("class foo {};\n"
"class bar { int i; };",
Style);
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWraping.AfterClass = true;
verifyFormat("class foo {};\n"
"{\n"
"};"
"class bar { int i; };",
Style);
Style.SplitEmptyRecord = false;
verifyFormat("class foo {};", Style);
```
You can repeat the above for `struct` and `union`, but IMO it's unnecessary.
https://github.com/llvm/llvm-project/pull/154580
More information about the cfe-commits
mailing list