[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:07:17 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: owenca (owenca)
<details>
<summary>Changes</summary>
Fixes #<!-- -->203209
---
Full diff: https://github.com/llvm/llvm-project/pull/203754.diff
2 Files Affected:
- (modified) clang/lib/Format/UnwrappedLineFormatter.cpp (+11-11)
- (modified) clang/unittests/Format/FormatTest.cpp (+9)
``````````diff
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"
``````````
</details>
https://github.com/llvm/llvm-project/pull/203754
More information about the cfe-commits
mailing list