Skip to content

Commit 643f2be

Browse files
committed
[clang-format] Improve detection of parameter declarations in K&R C
Clean up the detection of parameter declarations in K&R C function definitions. Also make it more precise by requiring the second token after the r_paren to be either a star or keyword/identifier. Differential Revision: https://reviews.llvm.org/D108094
1 parent 698e210 commit 643f2be

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,14 +1008,26 @@ static bool isC78Type(const FormatToken &Tok) {
10081008
// {
10091009
// return a + b;
10101010
// }
1011-
static bool isC78ParameterDecl(const FormatToken *Tok) {
1012-
if (!Tok)
1011+
static bool isC78ParameterDecl(const FormatToken *Tok, const FormatToken *Next,
1012+
const FormatToken *FuncName) {
1013+
assert(Tok);
1014+
assert(Next);
1015+
assert(FuncName);
1016+
1017+
if (FuncName->isNot(tok::identifier))
1018+
return false;
1019+
1020+
const FormatToken *Prev = FuncName->Previous;
1021+
if (!Prev || (Prev->isNot(tok::star) && !isC78Type(*Prev)))
10131022
return false;
10141023

10151024
if (!isC78Type(*Tok) &&
10161025
!Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union))
10171026
return false;
10181027

1028+
if (Next->isNot(tok::star) && !Next->Tok.getIdentifierInfo())
1029+
return false;
1030+
10191031
Tok = Tok->Previous;
10201032
if (!Tok || Tok->isNot(tok::r_paren))
10211033
return false;
@@ -1378,21 +1390,11 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) {
13781390
parseParens();
13791391
// Break the unwrapped line if a K&R C function definition has a parameter
13801392
// declaration.
1381-
if (!IsTopLevel || !Style.isCpp())
1382-
break;
1383-
if (!Previous || Previous->isNot(tok::identifier))
1384-
break;
1385-
const FormatToken *PrevPrev = Previous->Previous;
1386-
if (!PrevPrev || (!isC78Type(*PrevPrev) && PrevPrev->isNot(tok::star)))
1393+
if (!IsTopLevel || !Style.isCpp() || !Previous || FormatTok->is(tok::eof))
13871394
break;
13881395
const unsigned Position = Tokens->getPosition() + 1;
1389-
if (Position == AllTokens.size())
1390-
break;
13911396
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)) {
1397+
if (isC78ParameterDecl(FormatTok, AllTokens[Position], Previous)) {
13961398
addUnwrappedLine();
13971399
return;
13981400
}

0 commit comments

Comments
 (0)