https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/203754
>From cb30b3c8e5fb316f28be4cd34ca1beb4b122f98d Mon Sep 17 00:00:00 2001 From: Owen Pan <[email protected]> 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 | 26 ++++++++++----------- clang/unittests/Format/FormatTest.cpp | 9 +++++++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 42eabc065b1a8..7ea424349d923 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; @@ -326,12 +325,12 @@ class LineJoiner { if (Style.AllowShortFunctionsOnASingleLine.Inline && !Style.AllowShortFunctionsOnASingleLine.Other) { - // Just checking TheLine->Level != 0 is not enough, because it - // provokes treating functions inside indented namespaces as short. if (Style.isJavaScript() && TheLine->Last->is(TT_FunctionLBrace)) return true; - if (TheLine->Level != 0) { + // Just checking `TheLine->Level > 0` is not enough because it would + // cause functions inside indented namespaces to be treated as short. + 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" _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
