Skip to content

Commit a21d17f

Browse files
authored
[clang-format] Fix a bug in breaking before FunctionDeclarationName (#153924)
Fixes #153891
1 parent 5e57a10 commit a21d17f

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,16 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
629629
// name.
630630
!Style.isJavaScript() && Previous.isNot(tok::kw_template) &&
631631
CurrentState.BreakBeforeParameter) {
632-
for (const auto *Tok = &Previous; Tok; Tok = Tok->Previous)
633-
if (Tok->FirstAfterPPLine || Tok->is(TT_LineComment))
632+
for (const auto *Tok = &Previous; Tok; Tok = Tok->Previous) {
633+
if (Tok->is(TT_LineComment))
634634
return false;
635+
if (Tok->is(TT_TemplateCloser)) {
636+
Tok = Tok->MatchingParen;
637+
assert(Tok);
638+
}
639+
if (Tok->FirstAfterPPLine)
640+
return false;
641+
}
635642

636643
return true;
637644
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8618,14 +8618,22 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
86188618
verifyFormat("extern \"C\" //\n"
86198619
" void f();");
86208620

8621-
FormatStyle Style = getLLVMStyle();
8621+
auto Style = getLLVMStyle();
86228622
Style.PointerAlignment = FormatStyle::PAS_Left;
86238623
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
86248624
" aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
86258625
Style);
86268626
verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
86278627
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
86288628
Style);
8629+
8630+
Style = getLLVMStyleWithColumns(45);
8631+
Style.PenaltyReturnTypeOnItsOwnLine = 400;
8632+
verifyFormat("template <bool abool, // a comment\n"
8633+
" bool anotherbool>\n"
8634+
"static inline std::pair<size_t, MyCustomType>\n"
8635+
"myfunc(const char *buf, const char *&err);",
8636+
Style);
86298637
}
86308638

86318639
TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {

0 commit comments

Comments
 (0)