@@ -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,23 @@ class AnnotatingParser {
148
158
}
149
159
}
150
160
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
+
151
178
bool parseAngle() {
152
179
if (!CurrentToken)
153
180
return false;
@@ -2416,8 +2443,13 @@ class AnnotatingParser {
2416
2443
Current.setType(TT_BinaryOperator);
2417
2444
} else if (isStartOfName(Current) &&
2418
2445
(!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
+ }
2421
2453
} else if (Current.is(tok::semi)) {
2422
2454
// Reset FirstStartOfName after finding a semicolon so that a for loop
2423
2455
// with multiple increment statements is not confused with a for loop
@@ -3784,10 +3816,20 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
3784
3816
static bool isFunctionDeclarationName(const LangOptions &LangOpts,
3785
3817
const FormatToken &Current,
3786
3818
const AnnotatedLine &Line,
3819
+ const FormatStyle &Style,
3787
3820
FormatToken *&ClosingParen) {
3788
3821
if (Current.is(TT_FunctionDeclarationName))
3789
3822
return true;
3790
3823
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
+ }
3791
3833
if (!Current.Tok.getIdentifierInfo())
3792
3834
return false;
3793
3835
@@ -3994,7 +4036,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
3994
4036
AfterLastAttribute = Tok;
3995
4037
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3996
4038
IsCtorOrDtor ||
3997
- isFunctionDeclarationName(LangOpts, *Tok, Line, ClosingParen)) {
4039
+ isFunctionDeclarationName(LangOpts, *Tok, Line, Style, ClosingParen)) {
3998
4040
if (!IsCtorOrDtor)
3999
4041
Tok->setFinalizedType(TT_FunctionDeclarationName);
4000
4042
LineIsFunctionDeclaration = true;
@@ -6218,7 +6260,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
6218
6260
Right.Next->isOneOf(TT_FunctionDeclarationName, tok::kw_const)));
6219
6261
}
6220
6262
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6221
- TT_ClassHeadName, tok::kw_operator)) {
6263
+ TT_FunctionParameterKeyword, TT_ClassHeadName,
6264
+ tok::kw_operator)) {
6222
6265
return true;
6223
6266
}
6224
6267
if (Right.isAttribute())
0 commit comments