@@ -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::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
+
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,25 @@ class AnnotatingParser {
148
158
}
149
159
}
150
160
161
+ const FormatStyle::FunctionDeclarationWithKeywords *
162
+ findSurroundingFunctionWithKeywordedParameters(
163
+ const FormatToken &Token) const {
164
+ const FormatToken *Previous = &Token;
165
+ while (auto Prev = Previous->getPreviousNonComment())
166
+ Previous = Prev;
167
+ // Unknown if line ends with ';', FunctionLikeOrFreestandingMacro otherwise.
168
+ if (!Previous->isOneOf(TT_FunctionLikeOrFreestandingMacro, TT_Unknown))
169
+ return nullptr;
170
+ auto I = std::find_if(
171
+ Style.FunctionDeclarationsWithKeywords.begin(),
172
+ Style.FunctionDeclarationsWithKeywords.end(),
173
+ [Previous](
174
+ const FormatStyle::FunctionDeclarationWithKeywords &Declaration) {
175
+ return Previous->TokenText == Declaration.Name;
176
+ });
177
+ return I != Style.FunctionDeclarationsWithKeywords.end() ? &*I : nullptr;
178
+ }
179
+
151
180
bool parseAngle() {
152
181
if (!CurrentToken)
153
182
return false;
@@ -2416,8 +2445,14 @@ class AnnotatingParser {
2416
2445
Current.setType(TT_BinaryOperator);
2417
2446
} else if (isStartOfName(Current) &&
2418
2447
(!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2419
- Contexts.back().FirstStartOfName = &Current;
2420
- Current.setType(TT_StartOfName);
2448
+ if (isParametersKeyword(
2449
+ Current,
2450
+ findSurroundingFunctionWithKeywordedParameters(Current))) {
2451
+ Current.setType(TT_FunctionParameterKeyword);
2452
+ } else {
2453
+ Contexts.back().FirstStartOfName = &Current;
2454
+ Current.setType(TT_StartOfName);
2455
+ }
2421
2456
} else if (Current.is(tok::semi)) {
2422
2457
// Reset FirstStartOfName after finding a semicolon so that a for loop
2423
2458
// with multiple increment statements is not confused with a for loop
@@ -3784,10 +3819,20 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
3784
3819
static bool isFunctionDeclarationName(const LangOptions &LangOpts,
3785
3820
const FormatToken &Current,
3786
3821
const AnnotatedLine &Line,
3822
+ const FormatStyle &Style,
3787
3823
FormatToken *&ClosingParen) {
3788
3824
if (Current.is(TT_FunctionDeclarationName))
3789
3825
return true;
3790
3826
3827
+ if (Current.is(TT_FunctionLikeOrFreestandingMacro) &&
3828
+ std::find_if(
3829
+ Style.FunctionDeclarationsWithKeywords.begin(),
3830
+ Style.FunctionDeclarationsWithKeywords.end(),
3831
+ [&Current](const FormatStyle::FunctionDeclarationWithKeywords &Decl) {
3832
+ return Current.TokenText == Decl.Name;
3833
+ }) != Style.FunctionDeclarationsWithKeywords.end()) {
3834
+ return true;
3835
+ }
3791
3836
if (!Current.Tok.getIdentifierInfo())
3792
3837
return false;
3793
3838
@@ -3994,7 +4039,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
3994
4039
AfterLastAttribute = Tok;
3995
4040
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3996
4041
IsCtorOrDtor ||
3997
- isFunctionDeclarationName(LangOpts, *Tok, Line, ClosingParen)) {
4042
+ isFunctionDeclarationName(LangOpts, *Tok, Line, Style, ClosingParen)) {
3998
4043
if (!IsCtorOrDtor)
3999
4044
Tok->setFinalizedType(TT_FunctionDeclarationName);
4000
4045
LineIsFunctionDeclaration = true;
@@ -6218,7 +6263,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
6218
6263
Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6219
6264
}
6220
6265
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6221
- TT_ClassHeadName, tok::kw_operator)) {
6266
+ TT_FunctionParameterKeyword, TT_ClassHeadName,
6267
+ tok::kw_operator)) {
6222
6268
return true;
6223
6269
}
6224
6270
if (Right.isAttribute())
0 commit comments