Skip to content

Commit 4ab7fe9

Browse files
owencatstellar
authored andcommitted
[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 (cherry picked from commit f6928cf)
1 parent 748f09f commit 4ab7fe9

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 21 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,30 @@ 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 unsigned Position = Tokens->getPosition() + 1;
1389+
if (Position == AllTokens.size())
1390+
break;
1391+
assert(Position < AllTokens.size());
1392+
const FormatToken *Next = AllTokens[Position];
1393+
if (Next && Next->isOneOf(tok::l_paren, tok::semi))
1394+
break;
1395+
if (isC78ParameterDecl(FormatTok)) {
13841396
addUnwrappedLine();
13851397
return;
13861398
}
13871399
break;
1400+
}
13881401
case tok::kw_operator:
13891402
nextToken();
13901403
if (FormatTok->isBinaryOperator())

clang/unittests/Format/FormatTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8239,6 +8239,20 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
82398239
" return a + b < c;\n"
82408240
"};",
82418241
Style);
8242+
verifyFormat("byte *\n" // Break here.
8243+
"f(a)\n" // Break here.
8244+
"byte a[];\n"
8245+
"{\n"
8246+
" return a;\n"
8247+
"}",
8248+
Style);
8249+
verifyFormat("bool f(int a, int) override;\n"
8250+
"Bar g(int a, Bar) final;\n"
8251+
"Bar h(a, Bar) final;",
8252+
Style);
8253+
verifyFormat("int\n"
8254+
"f(a)",
8255+
Style);
82428256

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

0 commit comments

Comments
 (0)