[clang] [clang-format] Add option AllowShortRecordOnASingleLine (PR #154580)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 21 00:49:51 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>,
=?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>
================
@@ -574,6 +580,82 @@ class LineJoiner {
return 0;
}
+ unsigned tryMergeRecord(ArrayRef<AnnotatedLine *>::const_iterator I,
+ ArrayRef<AnnotatedLine *>::const_iterator E,
+ unsigned Limit) {
+ const auto *Line = I[0];
+ const auto *NextLine = I[1];
+
+ auto GetRelevantAfterOption = [&](const FormatToken *Tok) {
+ switch (Tok->getType()) {
+ case TT_StructLBrace:
+ return Style.BraceWrapping.AfterStruct;
+ case TT_ClassLBrace:
+ return Style.BraceWrapping.AfterClass;
+ case TT_UnionLBrace:
+ return Style.BraceWrapping.AfterUnion;
+ default:
+ return false;
+ };
+ };
+
+ // Current line begins both record and block, brace was not wrapped.
+ if (Line->Last->isOneOf(TT_StructLBrace, TT_ClassLBrace, TT_UnionLBrace)) {
+ 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 (Style.AllowShortRecordOnASingleLine != FormatStyle::SRS_Never &&
+ (!GetRelevantAfterOption(Line->Last) ||
+ (!Style.BraceWrapping.SplitEmptyRecord && TryMergeShortRecord()))) {
+ return tryMergeSimpleBlock(I, E, Limit);
+ }
+ }
+
+ // Cases where the l_brace was wrapped.
+ // Current line begins record, next line block.
+ if (NextLine->First->isOneOf(TT_StructLBrace, TT_ClassLBrace,
+ TT_UnionLBrace)) {
+ if (I + 2 == E)
+ return 0;
+
+ const bool IsEmptyBlock = I[2]->First->is(tok::r_brace);
+
+ if (!IsEmptyBlock &&
+ Style.AllowShortRecordOnASingleLine == FormatStyle::SRS_Always) {
+ return tryMergeSimpleBlock(I, E, Limit);
+ }
+ }
+
+ if (I == AnnotatedLines.begin())
+ return 0;
+
+ const auto *PreviousLine = I[-1];
+
+ // Previous line begins record, current line block.
+ if (PreviousLine->First->isOneOf(tok::kw_struct, tok::kw_class,
+ tok::kw_union)) {
----------------
owenca wrote:
```suggestion
// Previous line begins record, current line block.
if (I != AnnotatedLines.begin() &&
I[-1]->First->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union)) {
```
https://github.com/llvm/llvm-project/pull/154580
More information about the cfe-commits
mailing list