@@ -431,7 +431,7 @@ void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
431
431
}
432
432
LLVM_FALLTHROUGH;
433
433
default :
434
- parseStructuralElement ();
434
+ parseStructuralElement (/* IsTopLevel= */ true );
435
435
break ;
436
436
}
437
437
} while (!eof ());
@@ -994,6 +994,33 @@ static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
994
994
Keywords.kw_import , tok::kw_export);
995
995
}
996
996
997
+ // This function checks whether a token starts the first parameter declaration
998
+ // in a K&R C (aka C78) function definition, e.g.:
999
+ // int f(a, b)
1000
+ // short a, b;
1001
+ // {
1002
+ // return a + b;
1003
+ // }
1004
+ static bool isC78ParameterDecl (const FormatToken *Tok) {
1005
+ if (!Tok)
1006
+ return false ;
1007
+
1008
+ if (!Tok->isOneOf (tok::kw_int, tok::kw_char, tok::kw_float, tok::kw_double,
1009
+ tok::kw_struct, tok::kw_union, tok::kw_long, tok::kw_short,
1010
+ tok::kw_unsigned, tok::kw_register, tok::identifier))
1011
+ return false ;
1012
+
1013
+ Tok = Tok->Previous ;
1014
+ if (!Tok || Tok->isNot (tok::r_paren))
1015
+ return false ;
1016
+
1017
+ Tok = Tok->Previous ;
1018
+ if (!Tok || Tok->isNot (tok::identifier))
1019
+ return false ;
1020
+
1021
+ return Tok->Previous && Tok->Previous ->isOneOf (tok::l_paren, tok::comma);
1022
+ }
1023
+
997
1024
// readTokenWithJavaScriptASI reads the next token and terminates the current
998
1025
// line if JavaScript Automatic Semicolon Insertion must
999
1026
// happen between the current token and the next token.
@@ -1041,7 +1068,7 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
1041
1068
return addUnwrappedLine ();
1042
1069
}
1043
1070
1044
- void UnwrappedLineParser::parseStructuralElement () {
1071
+ void UnwrappedLineParser::parseStructuralElement (bool IsTopLevel ) {
1045
1072
assert (!FormatTok->is (tok::l_brace));
1046
1073
if (Style.Language == FormatStyle::LK_TableGen &&
1047
1074
FormatTok->is (tok::pp_include)) {
@@ -1343,6 +1370,18 @@ void UnwrappedLineParser::parseStructuralElement() {
1343
1370
return ;
1344
1371
case tok::l_paren:
1345
1372
parseParens ();
1373
+ // Break the unwrapped line if a K&R C function definition has a parameter
1374
+ // declaration.
1375
+ if (!IsTopLevel || !Style.isCpp ())
1376
+ break ;
1377
+ if (!Previous || Previous->isNot (tok::identifier))
1378
+ break ;
1379
+ if (Previous->Previous && Previous->Previous ->is (tok::at))
1380
+ break ;
1381
+ if (isC78ParameterDecl (FormatTok)) {
1382
+ addUnwrappedLine ();
1383
+ return ;
1384
+ }
1346
1385
break ;
1347
1386
case tok::kw_operator:
1348
1387
nextToken ();
0 commit comments