https://github.com/dingcyrus created 
https://github.com/llvm/llvm-project/pull/219910

  `AllowShortRecordOnASingleLine` (introduced for C++ records) made
  `LineJoiner::tryFitMultipleLinesInOne` route Java `TT_RecordLBrace`
  lines to `tryMergeRecord`, which only handles C++ class/struct/union
  records. Empty Java `interface` and `record` bodies were therefore no
  longer merged onto a single line, regressing the behavior that
  `BraceWrapping.SplitEmptyRecord: false` previously provided.

  Handle Java records separately and restore the pre-existing
  `SplitEmptyRecord`-based merge.

  Fixes #219711

>From 7514c5a16e35fdc002a4c23c3c387e70da983e16 Mon Sep 17 00:00:00 2001
From: Cyrus Ding <[email protected]>
Date: Mon, 31 Aug 2026 16:19:12 +0800
Subject: [PATCH]   [clang-format] Keep empty Java interface/record body on one
 line

  `AllowShortRecordOnASingleLine` (introduced for C++ records) made
  `LineJoiner::tryFitMultipleLinesInOne` route Java `TT_RecordLBrace`
  lines to `tryMergeRecord`, which only handles C++ class/struct/union
  records. Empty Java `interface` and `record` bodies were therefore no
  longer merged onto a single line, regressing the behavior that
  `BraceWrapping.SplitEmptyRecord: false` previously provided.

  Handle Java records separately and restore the pre-existing
  `SplitEmptyRecord`-based merge.

  Fixes #219711
---
 clang/lib/Format/UnwrappedLineFormatter.cpp | 9 +++++++--
 clang/unittests/Format/FormatTestJava.cpp   | 7 +++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp 
b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 33e6807dfe7dd..f005f228328ce 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -519,9 +519,14 @@ class LineJoiner {
       } else if (TheLine->Last->is(TT_CompoundRequirementLBrace)) {
         ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;
       } else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace,
-                                        TT_UnionLBrace) ||
-                 (TheLine->Last->is(TT_RecordLBrace) && Style.isJava())) {
+                                        TT_UnionLBrace)) {
         return tryMergeRecord(I, E, Limit);
+      } else if (TheLine->Last->is(TT_RecordLBrace) && Style.isJava()) {
+        // Java `interface` and `record` have no dedicated 
`BraceWrapping.After`
+        // option and are not governed by `AllowShortRecordOnASingleLine`.
+        ShouldMerge = !Style.BraceWrapping.AfterClass ||
+                      (NextLine.First->is(tok::r_brace) &&
+                       !Style.BraceWrapping.SplitEmptyRecord);
       } else if (TheLine->InPPDirective ||
                  TheLine->First->isNoneOf(tok::kw_class, tok::kw_enum,
                                           tok::kw_struct, tok::kw_union)) {
diff --git a/clang/unittests/Format/FormatTestJava.cpp 
b/clang/unittests/Format/FormatTestJava.cpp
index fa51e0421d714..a11fce963d820 100644
--- a/clang/unittests/Format/FormatTestJava.cpp
+++ b/clang/unittests/Format/FormatTestJava.cpp
@@ -869,6 +869,13 @@ TEST_F(FormatTestJava, BreakAfterRecord) {
                "public record Foo(int i) {}", Style);
 }
 
+TEST_F(FormatTestJava, EmptyRecordBodyOnASingleLine) {
+  auto Style = getGoogleStyle(FormatStyle::LK_Java);
+  verifyFormat("public interface Marker {}", Style);
+  verifyFormat("public record Marker() {}", Style);
+  verifyFormat("public class Marker {}", Style);
+}
+
 } // namespace
 } // namespace test
 } // namespace format

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to