[clang] [clang-format] Add option AllowShortRecordOnASingleLine (PR #154580)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 7 03:40:22 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>,
=?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>
================
@@ -485,20 +494,35 @@ class LineJoiner {
: 0;
}
+ auto TryMergeShortRecord = [&]() {
+ switch (Style.AllowShortRecordOnASingleLine) {
+ case FormatStyle::SRS_Never:
+ return false;
+ case FormatStyle::SRS_EmptyIfAttached:
+ case FormatStyle::SRS_Empty:
+ return NextLine.First->is(tok::r_brace);
+ case FormatStyle::SRS_Always:
+ return true;
+ }
+ };
+
if (TheLine->Last->is(tok::l_brace)) {
bool ShouldMerge = false;
// Try to merge records.
if (TheLine->Last->is(TT_EnumLBrace)) {
ShouldMerge = Style.AllowShortEnumsOnASingleLine;
} else if (TheLine->Last->is(TT_CompoundRequirementLBrace)) {
ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;
- } else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {
- // NOTE: We use AfterClass (whereas AfterStruct exists) for both classes
- // and structs, but it seems that wrapping is still handled correctly
- // elsewhere.
- ShouldMerge = !Style.BraceWrapping.AfterClass ||
- (NextLine.First->is(tok::r_brace) &&
- !Style.BraceWrapping.SplitEmptyRecord);
+ } else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace,
+ TT_UnionLBrace)) {
+ if (Style.AllowShortRecordOnASingleLine != FormatStyle::SRS_Never) {
+ // NOTE: We use AfterClass (whereas AfterStruct exists) for both
+ // classes and structs, but it seems that wrapping is still handled
+ // correctly elsewhere.
+ ShouldMerge =
+ !Style.BraceWrapping.AfterClass ||
+ (TryMergeShortRecord() && !Style.BraceWrapping.SplitEmptyRecord);
----------------
owenca wrote:
```suggestion
(!Style.BraceWrapping.SplitEmptyRecord && TryMergeShortRecord());
```
as suggested before.
https://github.com/llvm/llvm-project/pull/154580
More information about the cfe-commits
mailing list