[clang] [clang-format] Merge inline short functions for BS_Whitesmiths (PR #134473)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 4 19:34:35 PDT 2025
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/134473
Fix 126747
>From 12acc02f8ffd2317ed1197d7397cb1abc1a4fd07 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 4 Apr 2025 19:31:12 -0700
Subject: [PATCH] [clang-format] Merge inline short functions for
BS_Whitesmiths
Fix 126747
---
clang/lib/Format/UnwrappedLineFormatter.cpp | 9 +++++++--
clang/unittests/Format/FormatTest.cpp | 7 +++++++
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 000a5105ca407..62759d7945f7b 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -316,8 +316,13 @@ class LineJoiner {
const AnnotatedLine *Line = nullptr;
for (auto J = I - 1; J >= AnnotatedLines.begin(); --J) {
assert(*J);
- if (!(*J)->InPPDirective && !(*J)->isComment() &&
- (*J)->Level < TheLine->Level) {
+ if ((*J)->InPPDirective || (*J)->isComment() ||
+ (*J)->Level > TheLine->Level) {
+ continue;
+ }
+ if ((*J)->Level < TheLine->Level ||
+ (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths &&
+ (*J)->First->is(tok::l_brace))) {
Line = *J;
break;
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 4dfa135120605..69c9ee1d1dcb2 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -15142,6 +15142,13 @@ TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
"}",
MergeInlineOnly);
+ MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
+ verifyFormat("class Foo\n"
+ " {\n"
+ " void f() { foo(); }\n"
+ " };",
+ MergeInlineOnly);
+
// Also verify behavior when BraceWrapping.AfterFunction = true
MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
MergeInlineOnly.BraceWrapping.AfterFunction = true;
More information about the cfe-commits
mailing list