Skip to content

Commit de763c4

Browse files
committed
[clang-format] Distinguish K&R C function definition and attribute
This is a follow-up to https://reviews.llvm.org/D107950 which missed user-defined types in K&R C. Differential Revision: https://reviews.llvm.org/D107961
1 parent 3c6f115 commit de763c4

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "UnwrappedLineParser.h"
1616
#include "FormatToken.h"
17-
#include "clang/Basic/TokenKinds.h"
1817
#include "llvm/ADT/STLExtras.h"
1918
#include "llvm/Support/Debug.h"
2019
#include "llvm/Support/raw_ostream.h"
@@ -995,6 +994,13 @@ static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
995994
Keywords.kw_import, tok::kw_export);
996995
}
997996

997+
// Checks whether a token is a type in K&R C (aka C78).
998+
static bool isC78Type(const FormatToken &Tok) {
999+
return Tok.isOneOf(tok::kw_char, tok::kw_short, tok::kw_int, tok::kw_long,
1000+
tok::kw_unsigned, tok::kw_float, tok::kw_double,
1001+
tok::identifier);
1002+
}
1003+
9981004
// This function checks whether a token starts the first parameter declaration
9991005
// in a K&R C (aka C78) function definition, e.g.:
10001006
// int f(a, b)
@@ -1006,9 +1012,8 @@ static bool isC78ParameterDecl(const FormatToken *Tok) {
10061012
if (!Tok)
10071013
return false;
10081014

1009-
if (!Tok->isOneOf(tok::kw_int, tok::kw_char, tok::kw_float, tok::kw_double,
1010-
tok::kw_struct, tok::kw_union, tok::kw_long, tok::kw_short,
1011-
tok::kw_unsigned, tok::kw_register))
1015+
if (!isC78Type(*Tok) &&
1016+
!Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union))
10121017
return false;
10131018

10141019
Tok = Tok->Previous;
@@ -1369,22 +1374,26 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) {
13691374
case tok::r_brace:
13701375
addUnwrappedLine();
13711376
return;
1372-
case tok::l_paren:
1377+
case tok::l_paren: {
13731378
parseParens();
13741379
// Break the unwrapped line if a K&R C function definition has a parameter
13751380
// declaration.
13761381
if (!IsTopLevel || !Style.isCpp())
13771382
break;
13781383
if (!Previous || Previous->isNot(tok::identifier))
13791384
break;
1380-
if (Previous->Previous && Previous->Previous->is(tok::at))
1385+
const FormatToken *PrevPrev = Previous->Previous;
1386+
if (!PrevPrev || (!isC78Type(*PrevPrev) && PrevPrev->isNot(tok::star)))
13811387
break;
1382-
if (!Line->Tokens.begin()->Tok->is(tok::kw_typedef) &&
1383-
isC78ParameterDecl(FormatTok)) {
1388+
const FormatToken *Next = AllTokens[Tokens->getPosition() + 1];
1389+
if (Next && Next->isOneOf(tok::l_paren, tok::semi))
1390+
break;
1391+
if (isC78ParameterDecl(FormatTok)) {
13841392
addUnwrappedLine();
13851393
return;
13861394
}
13871395
break;
1396+
}
13881397
case tok::kw_operator:
13891398
nextToken();
13901399
if (FormatTok->isBinaryOperator())

clang/unittests/Format/FormatTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8247,6 +8247,16 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
82478247
" return a + b < c;\n"
82488248
"};",
82498249
Style);
8250+
verifyFormat("byte *\n" // Break here.
8251+
"f(a)\n" // Break here.
8252+
"byte a[];\n"
8253+
"{\n"
8254+
" return a;\n"
8255+
"}",
8256+
Style);
8257+
verifyFormat("bool f(int a, int) override;\n"
8258+
"Bar g(int a, Bar) final; // comment",
8259+
Style);
82508260

82518261
// The return breaking style doesn't affect:
82528262
// * function and object definitions with attribute-like macros

0 commit comments

Comments
 (0)