[llvm-branch-commits] [clang] 13259ba - [clang-format] Keep empty Java interface/record body on one line (#219910)

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Sep 6 23:07:46 PDT 2026


Author: Cyrus Ding
Date: 2026-09-07T08:07:23+02:00
New Revision: 13259bad3b4842287e5fb7123534264c85010841

URL: https://github.com/llvm/llvm-project/commit/13259bad3b4842287e5fb7123534264c85010841
DIFF: https://github.com/llvm/llvm-project/commit/13259bad3b4842287e5fb7123534264c85010841.diff

LOG:   [clang-format] Keep empty Java interface/record body on one line (#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

(cherry picked from commit 6d390ea43be046d62bc880524ffc4734a21307a7)

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineFormatter.cpp
    clang/unittests/Format/FormatTestJava.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index b783876b6da8f..9c310559078e7 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -518,9 +518,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


        


More information about the llvm-branch-commits mailing list