[clang] [clang-format] Fix a bug in breaking before FunctionDeclarationName (PR #153924)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 15 21:27:51 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: owenca (owenca)
<details>
<summary>Changes</summary>
Fixes #<!-- -->153891
---
Full diff: https://github.com/llvm/llvm-project/pull/153924.diff
2 Files Affected:
- (modified) clang/lib/Format/ContinuationIndenter.cpp (+9-2)
- (modified) clang/unittests/Format/FormatTest.cpp (+9-1)
``````````diff
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 9a10403b858f9..888d0faf80931 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -629,9 +629,16 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
// name.
!Style.isJavaScript() && Previous.isNot(tok::kw_template) &&
CurrentState.BreakBeforeParameter) {
- for (const auto *Tok = &Previous; Tok; Tok = Tok->Previous)
- if (Tok->FirstAfterPPLine || Tok->is(TT_LineComment))
+ for (const auto *Tok = &Previous; Tok; Tok = Tok->Previous) {
+ if (Tok->is(TT_LineComment))
return false;
+ if (Tok->is(TT_TemplateCloser)) {
+ Tok = Tok->MatchingParen;
+ assert(Tok);
+ }
+ if (Tok->FirstAfterPPLine)
+ return false;
+ }
return true;
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 96cc650f52a5d..e7c5eab4e6f1e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8614,7 +8614,7 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
verifyFormat("extern \"C\" //\n"
" void f();");
- FormatStyle Style = getLLVMStyle();
+ auto Style = getLLVMStyle();
Style.PointerAlignment = FormatStyle::PAS_Left;
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
@@ -8622,6 +8622,14 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
Style);
+
+ Style = getLLVMStyleWithColumns(45);
+ Style.PenaltyReturnTypeOnItsOwnLine = 400;
+ verifyFormat("template <bool abool, // a comment\n"
+ " bool anotherbool>\n"
+ "static inline std::pair<size_t, MyCustomType>\n"
+ "myfunc(const char *buf, const char *&err);",
+ Style);
}
TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/153924
More information about the cfe-commits
mailing list