Skip to content

Commit 58494c8

Browse files
committed
[clang-format] Make BreakAfterReturnType work with K&R C functions
This fixes PR50999. Differential Revision: https://reviews.llvm.org/D105964
1 parent 3bf101f commit 58494c8

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,6 +2476,14 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
24762476
if (Next->MatchingParen->Next &&
24772477
Next->MatchingParen->Next->is(TT_PointerOrReference))
24782478
return true;
2479+
// Check for K&R C function definitions, e.g.:
2480+
// int f(i)
2481+
// {
2482+
// return i + 1;
2483+
// }
2484+
if (Next->Next && Next->Next->is(tok::identifier) &&
2485+
!(Next->MatchingParen->Next && Next->MatchingParen->Next->is(tok::semi)))
2486+
return true;
24792487
for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
24802488
Tok = Tok->Next) {
24812489
if (Tok->is(TT_TypeDeclarationParen))

clang/unittests/Format/FormatTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8209,6 +8209,16 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
82098209
"}\n",
82108210
Style);
82118211

8212+
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8213+
Style.BraceWrapping.AfterFunction = true;
8214+
verifyFormat("int f(i);\n" // No break here.
8215+
"int\n" // Break here.
8216+
"f(i)\n"
8217+
"{\n"
8218+
" return i + 1;\n"
8219+
"}\n",
8220+
Style);
8221+
82128222
Style = getGNUStyle();
82138223

82148224
// Test for comments at the end of function declarations.

0 commit comments

Comments
 (0)