@@ -116,6 +116,16 @@ static bool isCppAttribute(bool IsCpp, const FormatToken &Tok) {
116116 return AttrTok && AttrTok->startsSequence (tok::r_square, tok::r_square);
117117}
118118
119+ static bool isParametersKeyword (
120+ const FormatToken &Tok,
121+ const FormatStyle::FunctionDeclarationWithKeywords *declaration) {
122+ if (!declaration)
123+ return false ;
124+
125+ return std::find (declaration->Keywords .begin (), declaration->Keywords .end (),
126+ Tok.TokenText ) != declaration->Keywords .end ();
127+ }
128+
119129// / A parser that gathers additional information about tokens.
120130// /
121131// / The \c TokenAnnotator tries to match parenthesis and square brakets and
@@ -148,6 +158,23 @@ class AnnotatingParser {
148158 }
149159 }
150160
161+ const FormatStyle::FunctionDeclarationWithKeywords *
162+ findSurroundingFunctionWithKeywordedParameters () const {
163+ // Unknown if line ends with ';', FunctionLikeOrFreestandingMacro otherwise.
164+ if (!Line.First ||
165+ !Line.First ->isOneOf (TT_FunctionLikeOrFreestandingMacro, TT_Unknown)) {
166+ return nullptr ;
167+ }
168+ auto I = std::find_if (
169+ Style.FunctionDeclarationsWithKeywords .begin (),
170+ Style.FunctionDeclarationsWithKeywords .end (),
171+ [this ](
172+ const FormatStyle::FunctionDeclarationWithKeywords &Declaration) {
173+ return Line.First ->TokenText == Declaration.Name ;
174+ });
175+ return I != Style.FunctionDeclarationsWithKeywords .end () ? &*I : nullptr ;
176+ }
177+
151178 bool parseAngle () {
152179 if (!CurrentToken)
153180 return false ;
@@ -2416,8 +2443,13 @@ class AnnotatingParser {
24162443 Current.setType (TT_BinaryOperator);
24172444 } else if (isStartOfName (Current) &&
24182445 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0 )) {
2419- Contexts.back ().FirstStartOfName = &Current;
2420- Current.setType (TT_StartOfName);
2446+ if (isParametersKeyword (
2447+ Current, findSurroundingFunctionWithKeywordedParameters ())) {
2448+ Current.setType (TT_FunctionParameterKeyword);
2449+ } else {
2450+ Contexts.back ().FirstStartOfName = &Current;
2451+ Current.setType (TT_StartOfName);
2452+ }
24212453 } else if (Current.is (tok::semi)) {
24222454 // Reset FirstStartOfName after finding a semicolon so that a for loop
24232455 // with multiple increment statements is not confused with a for loop
@@ -3784,10 +3816,20 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
37843816static bool isFunctionDeclarationName (const LangOptions &LangOpts,
37853817 const FormatToken &Current,
37863818 const AnnotatedLine &Line,
3819+ const FormatStyle &Style,
37873820 FormatToken *&ClosingParen) {
37883821 if (Current.is (TT_FunctionDeclarationName))
37893822 return true ;
37903823
3824+ if (Current.is (TT_FunctionLikeOrFreestandingMacro) &&
3825+ std::find_if (
3826+ Style.FunctionDeclarationsWithKeywords .begin (),
3827+ Style.FunctionDeclarationsWithKeywords .end (),
3828+ [&Current](const FormatStyle::FunctionDeclarationWithKeywords &Decl) {
3829+ return Current.TokenText == Decl.Name ;
3830+ }) != Style.FunctionDeclarationsWithKeywords .end ()) {
3831+ return true ;
3832+ }
37913833 if (!Current.Tok .getIdentifierInfo ())
37923834 return false ;
37933835
@@ -3994,7 +4036,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
39944036 AfterLastAttribute = Tok;
39954037 if (const bool IsCtorOrDtor = Tok->is (TT_CtorDtorDeclName);
39964038 IsCtorOrDtor ||
3997- isFunctionDeclarationName (LangOpts, *Tok, Line, ClosingParen)) {
4039+ isFunctionDeclarationName (LangOpts, *Tok, Line, Style, ClosingParen)) {
39984040 if (!IsCtorOrDtor)
39994041 Tok->setFinalizedType (TT_FunctionDeclarationName);
40004042 LineIsFunctionDeclaration = true ;
@@ -6218,7 +6260,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
62186260 Right.Next ->isOneOf (TT_FunctionDeclarationName, tok::kw_const)));
62196261 }
62206262 if (Right.isOneOf (TT_StartOfName, TT_FunctionDeclarationName,
6221- TT_ClassHeadName, tok::kw_operator)) {
6263+ TT_FunctionParameterKeyword, TT_ClassHeadName,
6264+ tok::kw_operator)) {
62226265 return true ;
62236266 }
62246267 if (Right.isAttribute ())
0 commit comments