14
14
15
15
#include " UnwrappedLineParser.h"
16
16
#include " FormatToken.h"
17
- #include " clang/Basic/TokenKinds.h"
18
17
#include " llvm/ADT/STLExtras.h"
19
18
#include " llvm/Support/Debug.h"
20
19
#include " llvm/Support/raw_ostream.h"
@@ -995,6 +994,13 @@ static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
995
994
Keywords.kw_import , tok::kw_export);
996
995
}
997
996
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
+
998
1004
// This function checks whether a token starts the first parameter declaration
999
1005
// in a K&R C (aka C78) function definition, e.g.:
1000
1006
// int f(a, b)
@@ -1006,9 +1012,8 @@ static bool isC78ParameterDecl(const FormatToken *Tok) {
1006
1012
if (!Tok)
1007
1013
return false ;
1008
1014
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))
1012
1017
return false ;
1013
1018
1014
1019
Tok = Tok->Previous ;
@@ -1369,22 +1374,30 @@ void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) {
1369
1374
case tok::r_brace:
1370
1375
addUnwrappedLine ();
1371
1376
return ;
1372
- case tok::l_paren:
1377
+ case tok::l_paren: {
1373
1378
parseParens ();
1374
1379
// Break the unwrapped line if a K&R C function definition has a parameter
1375
1380
// declaration.
1376
1381
if (!IsTopLevel || !Style.isCpp ())
1377
1382
break ;
1378
1383
if (!Previous || Previous->isNot (tok::identifier))
1379
1384
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)))
1381
1387
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)) {
1384
1396
addUnwrappedLine ();
1385
1397
return ;
1386
1398
}
1387
1399
break ;
1400
+ }
1388
1401
case tok::kw_operator:
1389
1402
nextToken ();
1390
1403
if (FormatTok->isBinaryOperator ())
0 commit comments