@@ -431,7 +431,7 @@ void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
431431 }
432432 LLVM_FALLTHROUGH;
433433 default :
434- parseStructuralElement ();
434+ parseStructuralElement (/* IsTopLevel= */ true );
435435 break ;
436436 }
437437 } while (!eof ());
@@ -994,6 +994,33 @@ static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
994994 Keywords.kw_import , tok::kw_export);
995995}
996996
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+
9971024// readTokenWithJavaScriptASI reads the next token and terminates the current
9981025// line if JavaScript Automatic Semicolon Insertion must
9991026// happen between the current token and the next token.
@@ -1041,7 +1068,7 @@ void UnwrappedLineParser::readTokenWithJavaScriptASI() {
10411068 return addUnwrappedLine ();
10421069}
10431070
1044- void UnwrappedLineParser::parseStructuralElement () {
1071+ void UnwrappedLineParser::parseStructuralElement (bool IsTopLevel ) {
10451072 assert (!FormatTok->is (tok::l_brace));
10461073 if (Style.Language == FormatStyle::LK_TableGen &&
10471074 FormatTok->is (tok::pp_include)) {
@@ -1343,6 +1370,18 @@ void UnwrappedLineParser::parseStructuralElement() {
13431370 return ;
13441371 case tok::l_paren:
13451372 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+ }
13461385 break ;
13471386 case tok::kw_operator:
13481387 nextToken ();
0 commit comments