[clang] [clang-format] Fix a bug in merging short inline functions (PR #203754)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 14 02:06:39 PDT 2026
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/203754
Fixes #203209
>From 510dae3308c624d5d0a754cff8570fd844b46777 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sun, 14 Jun 2026 02:05:22 -0700
Subject: [PATCH] [clang-format] Fix a bug in merging short inline functions
Fixes #203209
---
clang/lib/Format/UnwrappedLineFormatter.cpp | 22 ++++++++++-----------
clang/unittests/Format/FormatTest.cpp | 9 +++++++++
2 files changed, 20 insertions(+), 11 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 42eabc065b1a8..29cc0e3149e2b 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -314,8 +314,7 @@ class LineJoiner {
}
}
- auto ShouldMergeShortFunctions = [this, &I, &NextLine, PreviousLine,
- TheLine]() {
+ auto ShouldMergeShortFunctions = [&] {
if (Style.AllowShortFunctionsOnASingleLine.isAll())
return true;
@@ -331,7 +330,7 @@ class LineJoiner {
if (Style.isJavaScript() && TheLine->Last->is(TT_FunctionLBrace))
return true;
- if (TheLine->Level != 0) {
+ if (const auto Level = TheLine->Level; Level != 0) {
if (!PreviousLine)
return false;
@@ -339,15 +338,16 @@ class LineJoiner {
// Find the last line with lower level.
const AnnotatedLine *Line = nullptr;
for (auto J = I - 1; J >= AnnotatedLines.begin(); --J) {
- assert(*J);
- if (((*J)->InPPDirective && !(*J)->InMacroBody) ||
- (*J)->isComment() || (*J)->Level > TheLine->Level) {
+ const auto *L = *J;
+ assert(L);
+ if (TheLine->InMacroBody && !L->InMacroBody)
+ break;
+ if (L->isComment() || (!TheLine->InPPDirective && L->InPPDirective))
continue;
- }
- if ((*J)->Level < TheLine->Level ||
- (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths &&
- (*J)->First->is(tok::l_brace))) {
- Line = *J;
+ if (L->Level < Level ||
+ (L->Level == Level && L->First->is(tok::l_brace) &&
+ Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)) {
+ Line = L;
break;
}
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 6e1150227c452..9a73abe6c88c0 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -15421,6 +15421,15 @@ TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
"}",
MergeInlineOnly);
+ MergeInlineOnly.NamespaceIndentation = FormatStyle::NI_All;
+ verifyFormat("namespace {\n"
+ " class Class {\n"
+ "#define MACRO 1\n"
+ " int f() { return 1; }\n"
+ " };\n"
+ "} // namespace",
+ MergeInlineOnly);
+
MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
verifyFormat("class Foo\n"
" {\n"
More information about the cfe-commits
mailing list