@@ -116,6 +116,16 @@ static bool isCppAttribute(bool IsCpp, const FormatToken &Tok) {
116
116
return AttrTok && AttrTok->startsSequence (tok::r_square, tok::r_square);
117
117
}
118
118
119
+ static bool isParametersKeyword (
120
+ const FormatToken &Tok,
121
+ const FormatStyle::KeywordedFunctionLikeMacro *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
+
119
129
// / A parser that gathers additional information about tokens.
120
130
// /
121
131
// / The \c TokenAnnotator tries to match parenthesis and square brakets and
@@ -148,6 +158,32 @@ class AnnotatingParser {
148
158
}
149
159
}
150
160
161
+ const FormatStyle::KeywordedFunctionLikeMacro *
162
+ findKeywordedFunctionLikeMacro () const {
163
+ const FormatToken *TokBeforeFirstLParent = nullptr ;
164
+ for (const FormatToken *T = Line.First ; T != Line.Last ; T = T->Next ) {
165
+ if (T->Tok .is (tok::l_paren)) {
166
+ TokBeforeFirstLParent = T->getPreviousNonComment ();
167
+ break ;
168
+ }
169
+ }
170
+
171
+ // Unknown if line ends with ';', FunctionLikeOrFreestandingMacro otherwise.
172
+ if (!TokBeforeFirstLParent ||
173
+ !TokBeforeFirstLParent->isOneOf (TT_FunctionLikeOrFreestandingMacro,
174
+ TT_Unknown)) {
175
+ return nullptr ;
176
+ }
177
+ auto I = std::find_if (
178
+ Style .KeywordedFunctionLikeMacros .begin (),
179
+ Style .KeywordedFunctionLikeMacros .end (),
180
+ [TokBeforeFirstLParent](
181
+ const FormatStyle::KeywordedFunctionLikeMacro &Declaration) {
182
+ return TokBeforeFirstLParent->TokenText == Declaration.Name ;
183
+ });
184
+ return I != Style .KeywordedFunctionLikeMacros .end () ? &*I : nullptr ;
185
+ }
186
+
151
187
bool parseAngle () {
152
188
if (!CurrentToken)
153
189
return false ;
@@ -2415,8 +2451,12 @@ class AnnotatingParser {
2415
2451
Current.setType (TT_BinaryOperator);
2416
2452
} else if (isStartOfName (Current) &&
2417
2453
(!Line.MightBeFunctionDecl || Current.NestingLevel != 0 )) {
2418
- Contexts.back ().FirstStartOfName = &Current;
2419
- Current.setType (TT_StartOfName);
2454
+ if (isParametersKeyword (Current, findKeywordedFunctionLikeMacro ())) {
2455
+ Current.setType (TT_FunctionParameterKeyword);
2456
+ } else {
2457
+ Contexts.back ().FirstStartOfName = &Current;
2458
+ Current.setType (TT_StartOfName);
2459
+ }
2420
2460
} else if (Current.is (tok::semi)) {
2421
2461
// Reset FirstStartOfName after finding a semicolon so that a for loop
2422
2462
// with multiple increment statements is not confused with a for loop
@@ -3783,10 +3823,20 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
3783
3823
static bool isFunctionDeclarationName (const LangOptions &LangOpts,
3784
3824
const FormatToken &Current,
3785
3825
const AnnotatedLine &Line,
3826
+ const FormatStyle &Style ,
3786
3827
FormatToken *&ClosingParen) {
3787
3828
if (Current.is (TT_FunctionDeclarationName))
3788
3829
return true ;
3789
3830
3831
+ if (Current.is (TT_FunctionLikeOrFreestandingMacro) &&
3832
+ std::find_if (
3833
+ Style .KeywordedFunctionLikeMacros .begin (),
3834
+ Style .KeywordedFunctionLikeMacros .end (),
3835
+ [&Current](const FormatStyle::KeywordedFunctionLikeMacro &Decl) {
3836
+ return Current.TokenText == Decl.Name ;
3837
+ }) != Style .KeywordedFunctionLikeMacros .end ()) {
3838
+ return true ;
3839
+ }
3790
3840
if (!Current.Tok .getIdentifierInfo ())
3791
3841
return false ;
3792
3842
@@ -3993,7 +4043,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
3993
4043
AfterLastAttribute = Tok;
3994
4044
if (const bool IsCtorOrDtor = Tok->is (TT_CtorDtorDeclName);
3995
4045
IsCtorOrDtor ||
3996
- isFunctionDeclarationName (LangOpts, *Tok, Line, ClosingParen)) {
4046
+ isFunctionDeclarationName (LangOpts, *Tok, Line, Style , ClosingParen)) {
3997
4047
if (!IsCtorOrDtor)
3998
4048
Tok->setFinalizedType (TT_FunctionDeclarationName);
3999
4049
LineIsFunctionDeclaration = true ;
@@ -6231,7 +6281,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
6231
6281
Right.Next ->isOneOf (TT_FunctionDeclarationName, tok::kw_const)));
6232
6282
}
6233
6283
if (Right.isOneOf (TT_StartOfName, TT_FunctionDeclarationName,
6234
- TT_ClassHeadName, tok::kw_operator)) {
6284
+ TT_FunctionParameterKeyword, TT_ClassHeadName,
6285
+ tok::kw_operator)) {
6235
6286
return true ;
6236
6287
}
6237
6288
if (Right.isAttribute ())
0 commit comments