clang 19.0.0git
TokenAnnotator.cpp
Go to the documentation of this file.
1//===--- TokenAnnotator.cpp - Format C++ code -----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements a token annotator, i.e. creates
11/// \c AnnotatedTokens out of \c FormatTokens with required extra information.
12///
13//===----------------------------------------------------------------------===//
14
15#include "TokenAnnotator.h"
16#include "FormatToken.h"
19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/Support/Debug.h"
21
22#define DEBUG_TYPE "format-token-annotator"
23
24namespace clang {
25namespace format {
26
28 const FormatStyle &Style) {
29 switch (Style.BreakAfterAttributes) {
31 return true;
33 return Tok.NewlinesBefore > 0;
34 default:
35 return false;
36 }
37}
38
39namespace {
40
41/// Returns \c true if the line starts with a token that can start a statement
42/// with an initializer.
43static bool startsWithInitStatement(const AnnotatedLine &Line) {
44 return Line.startsWith(tok::kw_for) || Line.startsWith(tok::kw_if) ||
45 Line.startsWith(tok::kw_switch);
46}
47
48/// Returns \c true if the token can be used as an identifier in
49/// an Objective-C \c \@selector, \c false otherwise.
50///
51/// Because getFormattingLangOpts() always lexes source code as
52/// Objective-C++, C++ keywords like \c new and \c delete are
53/// lexed as tok::kw_*, not tok::identifier, even for Objective-C.
54///
55/// For Objective-C and Objective-C++, both identifiers and keywords
56/// are valid inside @selector(...) (or a macro which
57/// invokes @selector(...)). So, we allow treat any identifier or
58/// keyword as a potential Objective-C selector component.
59static bool canBeObjCSelectorComponent(const FormatToken &Tok) {
60 return Tok.Tok.getIdentifierInfo();
61}
62
63/// With `Left` being '(', check if we're at either `[...](` or
64/// `[...]<...>(`, where the [ opens a lambda capture list.
65static bool isLambdaParameterList(const FormatToken *Left) {
66 // Skip <...> if present.
67 if (Left->Previous && Left->Previous->is(tok::greater) &&
68 Left->Previous->MatchingParen &&
69 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
70 Left = Left->Previous->MatchingParen;
71 }
72
73 // Check for `[...]`.
74 return Left->Previous && Left->Previous->is(tok::r_square) &&
75 Left->Previous->MatchingParen &&
76 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
77}
78
79/// Returns \c true if the token is followed by a boolean condition, \c false
80/// otherwise.
81static bool isKeywordWithCondition(const FormatToken &Tok) {
82 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
83 tok::kw_constexpr, tok::kw_catch);
84}
85
86/// Returns \c true if the token starts a C++ attribute, \c false otherwise.
87static bool isCppAttribute(bool IsCpp, const FormatToken &Tok) {
88 if (!IsCpp || !Tok.startsSequence(tok::l_square, tok::l_square))
89 return false;
90 // The first square bracket is part of an ObjC array literal
91 if (Tok.Previous && Tok.Previous->is(tok::at))
92 return false;
93 const FormatToken *AttrTok = Tok.Next->Next;
94 if (!AttrTok)
95 return false;
96 // C++17 '[[using ns: foo, bar(baz, blech)]]'
97 // We assume nobody will name an ObjC variable 'using'.
98 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
99 return true;
100 if (AttrTok->isNot(tok::identifier))
101 return false;
102 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
103 // ObjC message send. We assume nobody will use : in a C++11 attribute
104 // specifier parameter, although this is technically valid:
105 // [[foo(:)]].
106 if (AttrTok->is(tok::colon) ||
107 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
108 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
109 return false;
110 }
111 if (AttrTok->is(tok::ellipsis))
112 return true;
113 AttrTok = AttrTok->Next;
114 }
115 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
116}
117
118/// A parser that gathers additional information about tokens.
119///
120/// The \c TokenAnnotator tries to match parenthesis and square brakets and
121/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
122/// into template parameter lists.
123class AnnotatingParser {
124public:
125 AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
126 const AdditionalKeywords &Keywords,
127 SmallVector<ScopeType> &Scopes)
128 : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
129 IsCpp(Style.isCpp()), LangOpts(getFormattingLangOpts(Style)),
130 Keywords(Keywords), Scopes(Scopes) {
131 assert(IsCpp == LangOpts.CXXOperatorNames);
132 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
133 resetTokenMetadata();
134 }
135
136private:
137 ScopeType getScopeType(const FormatToken &Token) const {
138 switch (Token.getType()) {
139 case TT_FunctionLBrace:
140 case TT_LambdaLBrace:
141 return ST_Function;
142 case TT_ClassLBrace:
143 case TT_StructLBrace:
144 case TT_UnionLBrace:
145 return ST_Class;
146 default:
147 return ST_Other;
148 }
149 }
150
151 bool parseAngle() {
152 if (!CurrentToken || !CurrentToken->Previous)
153 return false;
154 if (NonTemplateLess.count(CurrentToken->Previous) > 0)
155 return false;
156
157 const FormatToken &Previous = *CurrentToken->Previous; // The '<'.
158 if (Previous.Previous) {
159 if (Previous.Previous->Tok.isLiteral())
160 return false;
161 if (Previous.Previous->is(tok::r_brace))
162 return false;
163 if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
164 (!Previous.Previous->MatchingParen ||
165 Previous.Previous->MatchingParen->isNot(
166 TT_OverloadedOperatorLParen))) {
167 return false;
168 }
169 if (Previous.Previous->is(tok::kw_operator) &&
170 CurrentToken->is(tok::l_paren)) {
171 return false;
172 }
173 }
174
175 FormatToken *Left = CurrentToken->Previous;
176 Left->ParentBracket = Contexts.back().ContextKind;
177 ScopedContextCreator ContextCreator(*this, tok::less, 12);
178
179 // If this angle is in the context of an expression, we need to be more
180 // hesitant to detect it as opening template parameters.
181 bool InExprContext = Contexts.back().IsExpression;
182
183 Contexts.back().IsExpression = false;
184 // If there's a template keyword before the opening angle bracket, this is a
185 // template parameter, not an argument.
186 if (Left->Previous && Left->Previous->isNot(tok::kw_template))
187 Contexts.back().ContextType = Context::TemplateArgument;
188
189 if (Style.Language == FormatStyle::LK_Java &&
190 CurrentToken->is(tok::question)) {
191 next();
192 }
193
194 while (CurrentToken) {
195 if (CurrentToken->is(tok::greater)) {
196 // Try to do a better job at looking for ">>" within the condition of
197 // a statement. Conservatively insert spaces between consecutive ">"
198 // tokens to prevent splitting right bitshift operators and potentially
199 // altering program semantics. This check is overly conservative and
200 // will prevent spaces from being inserted in select nested template
201 // parameter cases, but should not alter program semantics.
202 if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) &&
203 Left->ParentBracket != tok::less &&
204 CurrentToken->getStartOfNonWhitespace() ==
205 CurrentToken->Next->getStartOfNonWhitespace().getLocWithOffset(
206 -1)) {
207 return false;
208 }
209 Left->MatchingParen = CurrentToken;
210 CurrentToken->MatchingParen = Left;
211 // In TT_Proto, we must distignuish between:
212 // map<key, value>
213 // msg < item: data >
214 // msg: < item: data >
215 // In TT_TextProto, map<key, value> does not occur.
216 if (Style.Language == FormatStyle::LK_TextProto ||
217 (Style.Language == FormatStyle::LK_Proto && Left->Previous &&
218 Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
219 CurrentToken->setType(TT_DictLiteral);
220 } else {
221 CurrentToken->setType(TT_TemplateCloser);
222 CurrentToken->Tok.setLength(1);
223 }
224 if (CurrentToken->Next && CurrentToken->Next->Tok.isLiteral())
225 return false;
226 next();
227 return true;
228 }
229 if (CurrentToken->is(tok::question) &&
230 Style.Language == FormatStyle::LK_Java) {
231 next();
232 continue;
233 }
234 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
235 (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
236 !Style.isCSharp() && !Style.isProto())) {
237 return false;
238 }
239 // If a && or || is found and interpreted as a binary operator, this set
240 // of angles is likely part of something like "a < b && c > d". If the
241 // angles are inside an expression, the ||/&& might also be a binary
242 // operator that was misinterpreted because we are parsing template
243 // parameters.
244 // FIXME: This is getting out of hand, write a decent parser.
245 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
246 CurrentToken->Previous->is(TT_BinaryOperator) &&
247 Contexts[Contexts.size() - 2].IsExpression &&
248 !Line.startsWith(tok::kw_template)) {
249 return false;
250 }
251 updateParameterCount(Left, CurrentToken);
252 if (Style.Language == FormatStyle::LK_Proto) {
253 if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) {
254 if (CurrentToken->is(tok::colon) ||
255 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
256 Previous->isNot(tok::colon))) {
257 Previous->setType(TT_SelectorName);
258 }
259 }
260 }
261 if (Style.isTableGen()) {
262 if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
263 // They appear as separators. Unless they are not in class definition.
264 next();
265 continue;
266 }
267 // In angle, there must be Value like tokens. Types are also able to be
268 // parsed in the same way with Values.
269 if (!parseTableGenValue())
270 return false;
271 continue;
272 }
273 if (!consumeToken())
274 return false;
275 }
276 return false;
277 }
278
279 bool parseUntouchableParens() {
280 while (CurrentToken) {
281 CurrentToken->Finalized = true;
282 switch (CurrentToken->Tok.getKind()) {
283 case tok::l_paren:
284 next();
285 if (!parseUntouchableParens())
286 return false;
287 continue;
288 case tok::r_paren:
289 next();
290 return true;
291 default:
292 // no-op
293 break;
294 }
295 next();
296 }
297 return false;
298 }
299
300 bool parseParens(bool LookForDecls = false) {
301 if (!CurrentToken)
302 return false;
303 assert(CurrentToken->Previous && "Unknown previous token");
304 FormatToken &OpeningParen = *CurrentToken->Previous;
305 assert(OpeningParen.is(tok::l_paren));
306 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
307 OpeningParen.ParentBracket = Contexts.back().ContextKind;
308 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
309
310 // FIXME: This is a bit of a hack. Do better.
311 Contexts.back().ColonIsForRangeExpr =
312 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
313
314 if (OpeningParen.Previous &&
315 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
316 OpeningParen.Finalized = true;
317 return parseUntouchableParens();
318 }
319
320 bool StartsObjCMethodExpr = false;
321 if (!Style.isVerilog()) {
322 if (FormatToken *MaybeSel = OpeningParen.Previous) {
323 // @selector( starts a selector.
324 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) &&
325 MaybeSel->Previous && MaybeSel->Previous->is(tok::at)) {
326 StartsObjCMethodExpr = true;
327 }
328 }
329 }
330
331 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
332 // Find the previous kw_operator token.
333 FormatToken *Prev = &OpeningParen;
334 while (Prev->isNot(tok::kw_operator)) {
335 Prev = Prev->Previous;
336 assert(Prev && "Expect a kw_operator prior to the OperatorLParen!");
337 }
338
339 // If faced with "a.operator*(argument)" or "a->operator*(argument)",
340 // i.e. the operator is called as a member function,
341 // then the argument must be an expression.
342 bool OperatorCalledAsMemberFunction =
343 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
344 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
345 } else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
346 Contexts.back().IsExpression = true;
347 Contexts.back().ContextType = Context::VerilogInstancePortList;
348 } else if (Style.isJavaScript() &&
349 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
350 Line.startsWith(tok::kw_export, Keywords.kw_type,
351 tok::identifier))) {
352 // type X = (...);
353 // export type X = (...);
354 Contexts.back().IsExpression = false;
355 } else if (OpeningParen.Previous &&
356 (OpeningParen.Previous->isOneOf(
357 tok::kw_static_assert, tok::kw_noexcept, tok::kw_explicit,
358 tok::kw_while, tok::l_paren, tok::comma,
359 TT_BinaryOperator) ||
360 OpeningParen.Previous->isIf())) {
361 // static_assert, if and while usually contain expressions.
362 Contexts.back().IsExpression = true;
363 } else if (Style.isJavaScript() && OpeningParen.Previous &&
364 (OpeningParen.Previous->is(Keywords.kw_function) ||
365 (OpeningParen.Previous->endsSequence(tok::identifier,
366 Keywords.kw_function)))) {
367 // function(...) or function f(...)
368 Contexts.back().IsExpression = false;
369 } else if (Style.isJavaScript() && OpeningParen.Previous &&
370 OpeningParen.Previous->is(TT_JsTypeColon)) {
371 // let x: (SomeType);
372 Contexts.back().IsExpression = false;
373 } else if (isLambdaParameterList(&OpeningParen)) {
374 // This is a parameter list of a lambda expression.
375 Contexts.back().IsExpression = false;
376 } else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
377 Contexts.back().IsExpression = false;
378 } else if (OpeningParen.Previous &&
379 OpeningParen.Previous->is(tok::kw__Generic)) {
380 Contexts.back().ContextType = Context::C11GenericSelection;
381 Contexts.back().IsExpression = true;
382 } else if (Line.InPPDirective &&
383 (!OpeningParen.Previous ||
384 OpeningParen.Previous->isNot(tok::identifier))) {
385 Contexts.back().IsExpression = true;
386 } else if (Contexts[Contexts.size() - 2].CaretFound) {
387 // This is the parameter list of an ObjC block.
388 Contexts.back().IsExpression = false;
389 } else if (OpeningParen.Previous &&
390 OpeningParen.Previous->is(TT_ForEachMacro)) {
391 // The first argument to a foreach macro is a declaration.
392 Contexts.back().ContextType = Context::ForEachMacro;
393 Contexts.back().IsExpression = false;
394 } else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
395 OpeningParen.Previous->MatchingParen->isOneOf(
396 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
397 Contexts.back().IsExpression = false;
398 } else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
399 bool IsForOrCatch =
400 OpeningParen.Previous &&
401 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
402 Contexts.back().IsExpression = !IsForOrCatch;
403 }
404
405 if (Style.isTableGen()) {
406 if (FormatToken *Prev = OpeningParen.Previous) {
407 if (Prev->is(TT_TableGenCondOperator)) {
408 Contexts.back().IsTableGenCondOpe = true;
409 Contexts.back().IsExpression = true;
410 } else if (Contexts.size() > 1 &&
411 Contexts[Contexts.size() - 2].IsTableGenBangOpe) {
412 // Hack to handle bang operators. The parent context's flag
413 // was set by parseTableGenSimpleValue().
414 // We have to specify the context outside because the prev of "(" may
415 // be ">", not the bang operator in this case.
416 Contexts.back().IsTableGenBangOpe = true;
417 Contexts.back().IsExpression = true;
418 } else {
419 // Otherwise, this paren seems DAGArg.
420 if (!parseTableGenDAGArg())
421 return false;
422 return parseTableGenDAGArgAndList(&OpeningParen);
423 }
424 }
425 }
426
427 // Infer the role of the l_paren based on the previous token if we haven't
428 // detected one yet.
429 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
430 if (PrevNonComment->isAttribute()) {
431 OpeningParen.setType(TT_AttributeLParen);
432 } else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
433 tok::kw_typeof,
434#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) tok::kw___##Trait,
435#include "clang/Basic/TransformTypeTraits.def"
436 tok::kw__Atomic)) {
437 OpeningParen.setType(TT_TypeDeclarationParen);
438 // decltype() and typeof() usually contain expressions.
439 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
440 Contexts.back().IsExpression = true;
441 }
442 }
443
444 if (StartsObjCMethodExpr) {
445 Contexts.back().ColonIsObjCMethodExpr = true;
446 OpeningParen.setType(TT_ObjCMethodExpr);
447 }
448
449 // MightBeFunctionType and ProbablyFunctionType are used for
450 // function pointer and reference types as well as Objective-C
451 // block types:
452 //
453 // void (*FunctionPointer)(void);
454 // void (&FunctionReference)(void);
455 // void (&&FunctionReference)(void);
456 // void (^ObjCBlock)(void);
457 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
458 bool ProbablyFunctionType =
459 CurrentToken->isPointerOrReference() || CurrentToken->is(tok::caret);
460 bool HasMultipleLines = false;
461 bool HasMultipleParametersOnALine = false;
462 bool MightBeObjCForRangeLoop =
463 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
464 FormatToken *PossibleObjCForInToken = nullptr;
465 while (CurrentToken) {
466 // LookForDecls is set when "if (" has been seen. Check for
467 // 'identifier' '*' 'identifier' followed by not '=' -- this
468 // '*' has to be a binary operator but determineStarAmpUsage() will
469 // categorize it as an unary operator, so set the right type here.
470 if (LookForDecls && CurrentToken->Next) {
471 FormatToken *Prev = CurrentToken->getPreviousNonComment();
472 if (Prev) {
473 FormatToken *PrevPrev = Prev->getPreviousNonComment();
474 FormatToken *Next = CurrentToken->Next;
475 if (PrevPrev && PrevPrev->is(tok::identifier) &&
476 PrevPrev->isNot(TT_TypeName) && Prev->isPointerOrReference() &&
477 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
478 Prev->setType(TT_BinaryOperator);
479 LookForDecls = false;
480 }
481 }
482 }
483
484 if (CurrentToken->Previous->is(TT_PointerOrReference) &&
485 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
486 tok::coloncolon)) {
487 ProbablyFunctionType = true;
488 }
489 if (CurrentToken->is(tok::comma))
490 MightBeFunctionType = false;
491 if (CurrentToken->Previous->is(TT_BinaryOperator))
492 Contexts.back().IsExpression = true;
493 if (CurrentToken->is(tok::r_paren)) {
494 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
495 ProbablyFunctionType && CurrentToken->Next &&
496 (CurrentToken->Next->is(tok::l_paren) ||
497 (CurrentToken->Next->is(tok::l_square) &&
498 Line.MustBeDeclaration))) {
499 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
500 ? TT_ObjCBlockLParen
501 : TT_FunctionTypeLParen);
502 }
503 OpeningParen.MatchingParen = CurrentToken;
504 CurrentToken->MatchingParen = &OpeningParen;
505
506 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
507 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
508 // Detect the case where macros are used to generate lambdas or
509 // function bodies, e.g.:
510 // auto my_lambda = MACRO((Type *type, int i) { .. body .. });
511 for (FormatToken *Tok = &OpeningParen; Tok != CurrentToken;
512 Tok = Tok->Next) {
513 if (Tok->is(TT_BinaryOperator) && Tok->isPointerOrReference())
514 Tok->setType(TT_PointerOrReference);
515 }
516 }
517
518 if (StartsObjCMethodExpr) {
519 CurrentToken->setType(TT_ObjCMethodExpr);
520 if (Contexts.back().FirstObjCSelectorName) {
521 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
522 Contexts.back().LongestObjCSelectorName;
523 }
524 }
525
526 if (OpeningParen.is(TT_AttributeLParen))
527 CurrentToken->setType(TT_AttributeRParen);
528 if (OpeningParen.is(TT_TypeDeclarationParen))
529 CurrentToken->setType(TT_TypeDeclarationParen);
530 if (OpeningParen.Previous &&
531 OpeningParen.Previous->is(TT_JavaAnnotation)) {
532 CurrentToken->setType(TT_JavaAnnotation);
533 }
534 if (OpeningParen.Previous &&
535 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
536 CurrentToken->setType(TT_LeadingJavaAnnotation);
537 }
538 if (OpeningParen.Previous &&
539 OpeningParen.Previous->is(TT_AttributeSquare)) {
540 CurrentToken->setType(TT_AttributeSquare);
541 }
542
543 if (!HasMultipleLines)
544 OpeningParen.setPackingKind(PPK_Inconclusive);
545 else if (HasMultipleParametersOnALine)
546 OpeningParen.setPackingKind(PPK_BinPacked);
547 else
548 OpeningParen.setPackingKind(PPK_OnePerLine);
549
550 next();
551 return true;
552 }
553 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
554 return false;
555
556 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
557 OpeningParen.setType(TT_Unknown);
558 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
559 !CurrentToken->Next->HasUnescapedNewline &&
560 !CurrentToken->Next->isTrailingComment()) {
561 HasMultipleParametersOnALine = true;
562 }
563 bool ProbablyFunctionTypeLParen =
564 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
565 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
566 if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
567 CurrentToken->Previous->isTypeName(LangOpts)) &&
568 !(CurrentToken->is(tok::l_brace) ||
569 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
570 Contexts.back().IsExpression = false;
571 }
572 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
573 MightBeObjCForRangeLoop = false;
574 if (PossibleObjCForInToken) {
575 PossibleObjCForInToken->setType(TT_Unknown);
576 PossibleObjCForInToken = nullptr;
577 }
578 }
579 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
580 PossibleObjCForInToken = CurrentToken;
581 PossibleObjCForInToken->setType(TT_ObjCForIn);
582 }
583 // When we discover a 'new', we set CanBeExpression to 'false' in order to
584 // parse the type correctly. Reset that after a comma.
585 if (CurrentToken->is(tok::comma))
586 Contexts.back().CanBeExpression = true;
587
588 if (Style.isTableGen()) {
589 if (CurrentToken->is(tok::comma)) {
590 if (Contexts.back().IsTableGenCondOpe)
591 CurrentToken->setType(TT_TableGenCondOperatorComma);
592 next();
593 } else if (CurrentToken->is(tok::colon)) {
594 if (Contexts.back().IsTableGenCondOpe)
595 CurrentToken->setType(TT_TableGenCondOperatorColon);
596 next();
597 }
598 // In TableGen there must be Values in parens.
599 if (!parseTableGenValue())
600 return false;
601 continue;
602 }
603
604 FormatToken *Tok = CurrentToken;
605 if (!consumeToken())
606 return false;
607 updateParameterCount(&OpeningParen, Tok);
608 if (CurrentToken && CurrentToken->HasUnescapedNewline)
609 HasMultipleLines = true;
610 }
611 return false;
612 }
613
614 bool isCSharpAttributeSpecifier(const FormatToken &Tok) {
615 if (!Style.isCSharp())
616 return false;
617
618 // `identifier[i]` is not an attribute.
619 if (Tok.Previous && Tok.Previous->is(tok::identifier))
620 return false;
621
622 // Chains of [] in `identifier[i][j][k]` are not attributes.
623 if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
624 auto *MatchingParen = Tok.Previous->MatchingParen;
625 if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
626 return false;
627 }
628
629 const FormatToken *AttrTok = Tok.Next;
630 if (!AttrTok)
631 return false;
632
633 // Just an empty declaration e.g. string [].
634 if (AttrTok->is(tok::r_square))
635 return false;
636
637 // Move along the tokens inbetween the '[' and ']' e.g. [STAThread].
638 while (AttrTok && AttrTok->isNot(tok::r_square))
639 AttrTok = AttrTok->Next;
640
641 if (!AttrTok)
642 return false;
643
644 // Allow an attribute to be the only content of a file.
645 AttrTok = AttrTok->Next;
646 if (!AttrTok)
647 return true;
648
649 // Limit this to being an access modifier that follows.
650 if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
651 tok::comment, tok::kw_class, tok::kw_static,
652 tok::l_square, Keywords.kw_internal)) {
653 return true;
654 }
655
656 // incase its a [XXX] retval func(....
657 if (AttrTok->Next &&
658 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
659 return true;
660 }
661
662 return false;
663 }
664
665 bool parseSquare() {
666 if (!CurrentToken)
667 return false;
668
669 // A '[' could be an index subscript (after an identifier or after
670 // ')' or ']'), it could be the start of an Objective-C method
671 // expression, it could the start of an Objective-C array literal,
672 // or it could be a C++ attribute specifier [[foo::bar]].
673 FormatToken *Left = CurrentToken->Previous;
674 Left->ParentBracket = Contexts.back().ContextKind;
675 FormatToken *Parent = Left->getPreviousNonComment();
676
677 // Cases where '>' is followed by '['.
678 // In C++, this can happen either in array of templates (foo<int>[10])
679 // or when array is a nested template type (unique_ptr<type1<type2>[]>).
680 bool CppArrayTemplates =
681 IsCpp && Parent && Parent->is(TT_TemplateCloser) &&
682 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
683 Contexts.back().ContextType == Context::TemplateArgument);
684
685 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
686 const bool IsCpp11AttributeSpecifier =
687 isCppAttribute(IsCpp, *Left) || IsInnerSquare;
688
689 // Treat C# Attributes [STAThread] much like C++ attributes [[...]].
690 bool IsCSharpAttributeSpecifier =
691 isCSharpAttributeSpecifier(*Left) ||
692 Contexts.back().InCSharpAttributeSpecifier;
693
694 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
695 bool IsCppStructuredBinding = Left->isCppStructuredBinding(IsCpp);
696 bool StartsObjCMethodExpr =
697 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
698 IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier &&
699 Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
700 !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
701 (!Parent ||
702 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
703 tok::kw_return, tok::kw_throw) ||
704 Parent->isUnaryOperator() ||
705 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
706 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
707 (getBinOpPrecedence(Parent->Tok.getKind(), true, true) >
709 bool ColonFound = false;
710
711 unsigned BindingIncrease = 1;
712 if (IsCppStructuredBinding) {
713 Left->setType(TT_StructuredBindingLSquare);
714 } else if (Left->is(TT_Unknown)) {
715 if (StartsObjCMethodExpr) {
716 Left->setType(TT_ObjCMethodExpr);
717 } else if (InsideInlineASM) {
718 Left->setType(TT_InlineASMSymbolicNameLSquare);
719 } else if (IsCpp11AttributeSpecifier) {
720 Left->setType(TT_AttributeSquare);
721 if (!IsInnerSquare && Left->Previous)
722 Left->Previous->EndsCppAttributeGroup = false;
723 } else if (Style.isJavaScript() && Parent &&
724 Contexts.back().ContextKind == tok::l_brace &&
725 Parent->isOneOf(tok::l_brace, tok::comma)) {
726 Left->setType(TT_JsComputedPropertyName);
727 } else if (IsCpp && Contexts.back().ContextKind == tok::l_brace &&
728 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
729 Left->setType(TT_DesignatedInitializerLSquare);
730 } else if (IsCSharpAttributeSpecifier) {
731 Left->setType(TT_AttributeSquare);
732 } else if (CurrentToken->is(tok::r_square) && Parent &&
733 Parent->is(TT_TemplateCloser)) {
734 Left->setType(TT_ArraySubscriptLSquare);
735 } else if (Style.isProto()) {
736 // Square braces in LK_Proto can either be message field attributes:
737 //
738 // optional Aaa aaa = 1 [
739 // (aaa) = aaa
740 // ];
741 //
742 // extensions 123 [
743 // (aaa) = aaa
744 // ];
745 //
746 // or text proto extensions (in options):
747 //
748 // option (Aaa.options) = {
749 // [type.type/type] {
750 // key: value
751 // }
752 // }
753 //
754 // or repeated fields (in options):
755 //
756 // option (Aaa.options) = {
757 // keys: [ 1, 2, 3 ]
758 // }
759 //
760 // In the first and the third case we want to spread the contents inside
761 // the square braces; in the second we want to keep them inline.
762 Left->setType(TT_ArrayInitializerLSquare);
763 if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
764 tok::equal) &&
765 !Left->endsSequence(tok::l_square, tok::numeric_constant,
766 tok::identifier) &&
767 !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
768 Left->setType(TT_ProtoExtensionLSquare);
769 BindingIncrease = 10;
770 }
771 } else if (!CppArrayTemplates && Parent &&
772 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
773 tok::comma, tok::l_paren, tok::l_square,
774 tok::question, tok::colon, tok::kw_return,
775 // Should only be relevant to JavaScript:
776 tok::kw_default)) {
777 Left->setType(TT_ArrayInitializerLSquare);
778 } else {
779 BindingIncrease = 10;
780 Left->setType(TT_ArraySubscriptLSquare);
781 }
782 }
783
784 ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease);
785 Contexts.back().IsExpression = true;
786 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
787 Contexts.back().IsExpression = false;
788
789 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
790 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
791 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
792
793 while (CurrentToken) {
794 if (CurrentToken->is(tok::r_square)) {
795 if (IsCpp11AttributeSpecifier) {
796 CurrentToken->setType(TT_AttributeSquare);
797 if (!IsInnerSquare)
798 CurrentToken->EndsCppAttributeGroup = true;
799 }
800 if (IsCSharpAttributeSpecifier) {
801 CurrentToken->setType(TT_AttributeSquare);
802 } else if (((CurrentToken->Next &&
803 CurrentToken->Next->is(tok::l_paren)) ||
804 (CurrentToken->Previous &&
805 CurrentToken->Previous->Previous == Left)) &&
806 Left->is(TT_ObjCMethodExpr)) {
807 // An ObjC method call is rarely followed by an open parenthesis. It
808 // also can't be composed of just one token, unless it's a macro that
809 // will be expanded to more tokens.
810 // FIXME: Do we incorrectly label ":" with this?
811 StartsObjCMethodExpr = false;
812 Left->setType(TT_Unknown);
813 }
814 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
815 CurrentToken->setType(TT_ObjCMethodExpr);
816 // If we haven't seen a colon yet, make sure the last identifier
817 // before the r_square is tagged as a selector name component.
818 if (!ColonFound && CurrentToken->Previous &&
819 CurrentToken->Previous->is(TT_Unknown) &&
820 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
821 CurrentToken->Previous->setType(TT_SelectorName);
822 }
823 // determineStarAmpUsage() thinks that '*' '[' is allocating an
824 // array of pointers, but if '[' starts a selector then '*' is a
825 // binary operator.
826 if (Parent && Parent->is(TT_PointerOrReference))
827 Parent->overwriteFixedType(TT_BinaryOperator);
828 }
829 // An arrow after an ObjC method expression is not a lambda arrow.
830 if (CurrentToken->is(TT_ObjCMethodExpr) && CurrentToken->Next &&
831 CurrentToken->Next->is(TT_TrailingReturnArrow)) {
832 CurrentToken->Next->overwriteFixedType(TT_Unknown);
833 }
834 Left->MatchingParen = CurrentToken;
835 CurrentToken->MatchingParen = Left;
836 // FirstObjCSelectorName is set when a colon is found. This does
837 // not work, however, when the method has no parameters.
838 // Here, we set FirstObjCSelectorName when the end of the method call is
839 // reached, in case it was not set already.
840 if (!Contexts.back().FirstObjCSelectorName) {
841 FormatToken *Previous = CurrentToken->getPreviousNonComment();
842 if (Previous && Previous->is(TT_SelectorName)) {
843 Previous->ObjCSelectorNameParts = 1;
844 Contexts.back().FirstObjCSelectorName = Previous;
845 }
846 } else {
847 Left->ParameterCount =
848 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
849 }
850 if (Contexts.back().FirstObjCSelectorName) {
851 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
852 Contexts.back().LongestObjCSelectorName;
853 if (Left->BlockParameterCount > 1)
854 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
855 }
856 if (Style.isTableGen() && Left->is(TT_TableGenListOpener))
857 CurrentToken->setType(TT_TableGenListCloser);
858 next();
859 return true;
860 }
861 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
862 return false;
863 if (CurrentToken->is(tok::colon)) {
864 if (IsCpp11AttributeSpecifier &&
865 CurrentToken->endsSequence(tok::colon, tok::identifier,
866 tok::kw_using)) {
867 // Remember that this is a [[using ns: foo]] C++ attribute, so we
868 // don't add a space before the colon (unlike other colons).
869 CurrentToken->setType(TT_AttributeColon);
870 } else if (!Style.isVerilog() && !Line.InPragmaDirective &&
871 Left->isOneOf(TT_ArraySubscriptLSquare,
872 TT_DesignatedInitializerLSquare)) {
873 Left->setType(TT_ObjCMethodExpr);
874 StartsObjCMethodExpr = true;
875 Contexts.back().ColonIsObjCMethodExpr = true;
876 if (Parent && Parent->is(tok::r_paren)) {
877 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
878 Parent->setType(TT_CastRParen);
879 }
880 }
881 ColonFound = true;
882 }
883 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
884 !ColonFound) {
885 Left->setType(TT_ArrayInitializerLSquare);
886 }
887 FormatToken *Tok = CurrentToken;
888 if (Style.isTableGen()) {
889 if (CurrentToken->isOneOf(tok::comma, tok::minus, tok::ellipsis)) {
890 // '-' and '...' appears as a separator in slice.
891 next();
892 } else {
893 // In TableGen there must be a list of Values in square brackets.
894 // It must be ValueList or SliceElements.
895 if (!parseTableGenValue())
896 return false;
897 }
898 updateParameterCount(Left, Tok);
899 continue;
900 }
901 if (!consumeToken())
902 return false;
903 updateParameterCount(Left, Tok);
904 }
905 return false;
906 }
907
908 void skipToNextNonComment() {
909 next();
910 while (CurrentToken && CurrentToken->is(tok::comment))
911 next();
912 }
913
914 // Simplified parser for TableGen Value. Returns true on success.
915 // It consists of SimpleValues, SimpleValues with Suffixes, and Value followed
916 // by '#', paste operator.
917 // There also exists the case the Value is parsed as NameValue.
918 // In this case, the Value ends if '{' is found.
919 bool parseTableGenValue(bool ParseNameMode = false) {
920 if (!CurrentToken)
921 return false;
922 while (CurrentToken->is(tok::comment))
923 next();
924 if (!parseTableGenSimpleValue())
925 return false;
926 if (!CurrentToken)
927 return true;
928 // Value "#" [Value]
929 if (CurrentToken->is(tok::hash)) {
930 if (CurrentToken->Next &&
931 CurrentToken->Next->isOneOf(tok::colon, tok::semi, tok::l_brace)) {
932 // Trailing paste operator.
933 // These are only the allowed cases in TGParser::ParseValue().
934 CurrentToken->setType(TT_TableGenTrailingPasteOperator);
935 next();
936 return true;
937 }
938 FormatToken *HashTok = CurrentToken;
939 skipToNextNonComment();
940 HashTok->setType(TT_Unknown);
941 if (!parseTableGenValue(ParseNameMode))
942 return false;
943 }
944 // In name mode, '{' is regarded as the end of the value.
945 // See TGParser::ParseValue in TGParser.cpp
946 if (ParseNameMode && CurrentToken->is(tok::l_brace))
947 return true;
948 // These tokens indicates this is a value with suffixes.
949 if (CurrentToken->isOneOf(tok::l_brace, tok::l_square, tok::period)) {
950 CurrentToken->setType(TT_TableGenValueSuffix);
951 FormatToken *Suffix = CurrentToken;
952 skipToNextNonComment();
953 if (Suffix->is(tok::l_square))
954 return parseSquare();
955 if (Suffix->is(tok::l_brace)) {
956 Scopes.push_back(getScopeType(*Suffix));
957 return parseBrace();
958 }
959 }
960 return true;
961 }
962
963 // TokVarName ::= "$" ualpha (ualpha | "0"..."9")*
964 // Appears as a part of DagArg.
965 // This does not change the current token on fail.
966 bool tryToParseTableGenTokVar() {
967 if (!CurrentToken)
968 return false;
969 if (CurrentToken->is(tok::identifier) &&
970 CurrentToken->TokenText.front() == '$') {
971 skipToNextNonComment();
972 return true;
973 }
974 return false;
975 }
976
977 // DagArg ::= Value [":" TokVarName] | TokVarName
978 // Appears as a part of SimpleValue6.
979 bool parseTableGenDAGArg(bool AlignColon = false) {
980 if (tryToParseTableGenTokVar())
981 return true;
982 if (parseTableGenValue()) {
983 if (CurrentToken && CurrentToken->is(tok::colon)) {
984 if (AlignColon)
985 CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
986 else
987 CurrentToken->setType(TT_TableGenDAGArgListColon);
988 skipToNextNonComment();
989 return tryToParseTableGenTokVar();
990 }
991 return true;
992 }
993 return false;
994 }
995
996 // Judge if the token is a operator ID to insert line break in DAGArg.
997 // That is, TableGenBreakingDAGArgOperators is empty (by the definition of the
998 // option) or the token is in the list.
999 bool isTableGenDAGArgBreakingOperator(const FormatToken &Tok) {
1000 auto &Opes = Style.TableGenBreakingDAGArgOperators;
1001 // If the list is empty, all operators are breaking operators.
1002 if (Opes.empty())
1003 return true;
1004 // Otherwise, the operator is limited to normal identifiers.
1005 if (Tok.isNot(tok::identifier) ||
1006 Tok.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
1007 return false;
1008 }
1009 // The case next is colon, it is not a operator of identifier.
1010 if (!Tok.Next || Tok.Next->is(tok::colon))
1011 return false;
1012 return std::find(Opes.begin(), Opes.end(), Tok.TokenText.str()) !=
1013 Opes.end();
1014 }
1015
1016 // SimpleValue6 ::= "(" DagArg [DagArgList] ")"
1017 // This parses SimpleValue 6's inside part of "(" ")"
1018 bool parseTableGenDAGArgAndList(FormatToken *Opener) {
1019 FormatToken *FirstTok = CurrentToken;
1020 if (!parseTableGenDAGArg())
1021 return false;
1022 bool BreakInside = false;
1023 if (Style.TableGenBreakInsideDAGArg != FormatStyle::DAS_DontBreak) {
1024 // Specialized detection for DAGArgOperator, that determines the way of
1025 // line break for this DAGArg elements.
1026 if (isTableGenDAGArgBreakingOperator(*FirstTok)) {
1027 // Special case for identifier DAGArg operator.
1028 BreakInside = true;
1029 Opener->setType(TT_TableGenDAGArgOpenerToBreak);
1030 if (FirstTok->isOneOf(TT_TableGenBangOperator,
1031 TT_TableGenCondOperator)) {
1032 // Special case for bang/cond operators. Set the whole operator as
1033 // the DAGArg operator. Always break after it.
1034 CurrentToken->Previous->setType(TT_TableGenDAGArgOperatorToBreak);
1035 } else if (FirstTok->is(tok::identifier)) {
1036 if (Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll)
1037 FirstTok->setType(TT_TableGenDAGArgOperatorToBreak);
1038 else
1039 FirstTok->setType(TT_TableGenDAGArgOperatorID);
1040 }
1041 }
1042 }
1043 // Parse the [DagArgList] part
1044 bool FirstDAGArgListElm = true;
1045 while (CurrentToken) {
1046 if (!FirstDAGArgListElm && CurrentToken->is(tok::comma)) {
1047 CurrentToken->setType(BreakInside ? TT_TableGenDAGArgListCommaToBreak
1048 : TT_TableGenDAGArgListComma);
1049 skipToNextNonComment();
1050 }
1051 if (CurrentToken && CurrentToken->is(tok::r_paren)) {
1052 CurrentToken->setType(TT_TableGenDAGArgCloser);
1053 Opener->MatchingParen = CurrentToken;
1054 CurrentToken->MatchingParen = Opener;
1055 skipToNextNonComment();
1056 return true;
1057 }
1058 if (!parseTableGenDAGArg(
1059 BreakInside &&
1060 Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
1061 return false;
1062 }
1063 FirstDAGArgListElm = false;
1064 }
1065 return false;
1066 }
1067
1068 bool parseTableGenSimpleValue() {
1069 assert(Style.isTableGen());
1070 if (!CurrentToken)
1071 return false;
1072 FormatToken *Tok = CurrentToken;
1073 skipToNextNonComment();
1074 // SimpleValue 1, 2, 3: Literals
1075 if (Tok->isOneOf(tok::numeric_constant, tok::string_literal,
1076 TT_TableGenMultiLineString, tok::kw_true, tok::kw_false,
1077 tok::question, tok::kw_int)) {
1078 return true;
1079 }
1080 // SimpleValue 4: ValueList, Type
1081 if (Tok->is(tok::l_brace)) {
1082 Scopes.push_back(getScopeType(*Tok));
1083 return parseBrace();
1084 }
1085 // SimpleValue 5: List initializer
1086 if (Tok->is(tok::l_square)) {
1087 Tok->setType(TT_TableGenListOpener);
1088 if (!parseSquare())
1089 return false;
1090 if (Tok->is(tok::less)) {
1091 CurrentToken->setType(TT_TemplateOpener);
1092 return parseAngle();
1093 }
1094 return true;
1095 }
1096 // SimpleValue 6: DAGArg [DAGArgList]
1097 // SimpleValue6 ::= "(" DagArg [DagArgList] ")"
1098 if (Tok->is(tok::l_paren)) {
1099 Tok->setType(TT_TableGenDAGArgOpener);
1100 return parseTableGenDAGArgAndList(Tok);
1101 }
1102 // SimpleValue 9: Bang operator
1103 if (Tok->is(TT_TableGenBangOperator)) {
1104 if (CurrentToken && CurrentToken->is(tok::less)) {
1105 CurrentToken->setType(TT_TemplateOpener);
1106 skipToNextNonComment();
1107 if (!parseAngle())
1108 return false;
1109 }
1110 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1111 return false;
1112 skipToNextNonComment();
1113 // FIXME: Hack using inheritance to child context
1114 Contexts.back().IsTableGenBangOpe = true;
1115 bool Result = parseParens();
1116 Contexts.back().IsTableGenBangOpe = false;
1117 return Result;
1118 }
1119 // SimpleValue 9: Cond operator
1120 if (Tok->is(TT_TableGenCondOperator)) {
1121 Tok = CurrentToken;
1122 skipToNextNonComment();
1123 if (!Tok || Tok->isNot(tok::l_paren))
1124 return false;
1125 bool Result = parseParens();
1126 return Result;
1127 }
1128 // We have to check identifier at the last because the kind of bang/cond
1129 // operators are also identifier.
1130 // SimpleValue 7: Identifiers
1131 if (Tok->is(tok::identifier)) {
1132 // SimpleValue 8: Anonymous record
1133 if (CurrentToken && CurrentToken->is(tok::less)) {
1134 CurrentToken->setType(TT_TemplateOpener);
1135 skipToNextNonComment();
1136 return parseAngle();
1137 }
1138 return true;
1139 }
1140
1141 return false;
1142 }
1143
1144 bool couldBeInStructArrayInitializer() const {
1145 if (Contexts.size() < 2)
1146 return false;
1147 // We want to back up no more then 2 context levels i.e.
1148 // . { { <-
1149 const auto End = std::next(Contexts.rbegin(), 2);
1150 auto Last = Contexts.rbegin();
1151 unsigned Depth = 0;
1152 for (; Last != End; ++Last)
1153 if (Last->ContextKind == tok::l_brace)
1154 ++Depth;
1155 return Depth == 2 && Last->ContextKind != tok::l_brace;
1156 }
1157
1158 bool parseBrace() {
1159 if (!CurrentToken)
1160 return true;
1161
1162 assert(CurrentToken->Previous);
1163 FormatToken &OpeningBrace = *CurrentToken->Previous;
1164 assert(OpeningBrace.is(tok::l_brace));
1165 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
1166
1167 if (Contexts.back().CaretFound)
1168 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
1169 Contexts.back().CaretFound = false;
1170
1171 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
1172 Contexts.back().ColonIsDictLiteral = true;
1173 if (OpeningBrace.is(BK_BracedInit))
1174 Contexts.back().IsExpression = true;
1175 if (Style.isJavaScript() && OpeningBrace.Previous &&
1176 OpeningBrace.Previous->is(TT_JsTypeColon)) {
1177 Contexts.back().IsExpression = false;
1178 }
1179 if (Style.isVerilog() &&
1180 (!OpeningBrace.getPreviousNonComment() ||
1181 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
1182 Contexts.back().VerilogMayBeConcatenation = true;
1183 }
1184 if (Style.isTableGen())
1185 Contexts.back().ColonIsDictLiteral = false;
1186
1187 unsigned CommaCount = 0;
1188 while (CurrentToken) {
1189 if (CurrentToken->is(tok::r_brace)) {
1190 assert(!Scopes.empty());
1191 assert(Scopes.back() == getScopeType(OpeningBrace));
1192 Scopes.pop_back();
1193 assert(OpeningBrace.Optional == CurrentToken->Optional);
1194 OpeningBrace.MatchingParen = CurrentToken;
1195 CurrentToken->MatchingParen = &OpeningBrace;
1196 if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1197 if (OpeningBrace.ParentBracket == tok::l_brace &&
1198 couldBeInStructArrayInitializer() && CommaCount > 0) {
1199 Contexts.back().ContextType = Context::StructArrayInitializer;
1200 }
1201 }
1202 next();
1203 return true;
1204 }
1205 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
1206 return false;
1207 updateParameterCount(&OpeningBrace, CurrentToken);
1208 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
1209 FormatToken *Previous = CurrentToken->getPreviousNonComment();
1210 if (Previous->is(TT_JsTypeOptionalQuestion))
1211 Previous = Previous->getPreviousNonComment();
1212 if ((CurrentToken->is(tok::colon) && !Style.isTableGen() &&
1213 (!Contexts.back().ColonIsDictLiteral || !IsCpp)) ||
1214 Style.isProto()) {
1215 OpeningBrace.setType(TT_DictLiteral);
1216 if (Previous->Tok.getIdentifierInfo() ||
1217 Previous->is(tok::string_literal)) {
1218 Previous->setType(TT_SelectorName);
1219 }
1220 }
1221 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown) &&
1222 !Style.isTableGen()) {
1223 OpeningBrace.setType(TT_DictLiteral);
1224 } else if (Style.isJavaScript()) {
1225 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1226 }
1227 }
1228 if (CurrentToken->is(tok::comma)) {
1229 if (Style.isJavaScript())
1230 OpeningBrace.overwriteFixedType(TT_DictLiteral);
1231 ++CommaCount;
1232 }
1233 if (!consumeToken())
1234 return false;
1235 }
1236 return true;
1237 }
1238
1239 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
1240 // For ObjC methods, the number of parameters is calculated differently as
1241 // method declarations have a different structure (the parameters are not
1242 // inside a bracket scope).
1243 if (Current->is(tok::l_brace) && Current->is(BK_Block))
1244 ++Left->BlockParameterCount;
1245 if (Current->is(tok::comma)) {
1246 ++Left->ParameterCount;
1247 if (!Left->Role)
1248 Left->Role.reset(new CommaSeparatedList(Style));
1249 Left->Role->CommaFound(Current);
1250 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
1251 Left->ParameterCount = 1;
1252 }
1253 }
1254
1255 bool parseConditional() {
1256 while (CurrentToken) {
1257 if (CurrentToken->is(tok::colon)) {
1258 CurrentToken->setType(TT_ConditionalExpr);
1259 next();
1260 return true;
1261 }
1262 if (!consumeToken())
1263 return false;
1264 }
1265 return false;
1266 }
1267
1268 bool parseTemplateDeclaration() {
1269 if (CurrentToken && CurrentToken->is(tok::less)) {
1270 CurrentToken->setType(TT_TemplateOpener);
1271 next();
1272 if (!parseAngle())
1273 return false;
1274 if (CurrentToken)
1275 CurrentToken->Previous->ClosesTemplateDeclaration = true;
1276 return true;
1277 }
1278 return false;
1279 }
1280
1281 bool consumeToken() {
1282 if (IsCpp) {
1283 const auto *Prev = CurrentToken->getPreviousNonComment();
1284 if (Prev && Prev->is(tok::r_square) && Prev->is(TT_AttributeSquare) &&
1285 CurrentToken->isOneOf(tok::kw_if, tok::kw_switch, tok::kw_case,
1286 tok::kw_default, tok::kw_for, tok::kw_while) &&
1287 mustBreakAfterAttributes(*CurrentToken, Style)) {
1288 CurrentToken->MustBreakBefore = true;
1289 }
1290 }
1291 FormatToken *Tok = CurrentToken;
1292 next();
1293 // In Verilog primitives' state tables, `:`, `?`, and `-` aren't normal
1294 // operators.
1295 if (Tok->is(TT_VerilogTableItem))
1296 return true;
1297 // Multi-line string itself is a single annotated token.
1298 if (Tok->is(TT_TableGenMultiLineString))
1299 return true;
1300 switch (Tok->Tok.getKind()) {
1301 case tok::plus:
1302 case tok::minus:
1303 if (!Tok->Previous && Line.MustBeDeclaration)
1304 Tok->setType(TT_ObjCMethodSpecifier);
1305 break;
1306 case tok::colon:
1307 if (!Tok->Previous)
1308 return false;
1309 // Goto labels and case labels are already identified in
1310 // UnwrappedLineParser.
1311 if (Tok->isTypeFinalized())
1312 break;
1313 // Colons from ?: are handled in parseConditional().
1314 if (Style.isJavaScript()) {
1315 if (Contexts.back().ColonIsForRangeExpr || // colon in for loop
1316 (Contexts.size() == 1 && // switch/case labels
1317 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
1318 Contexts.back().ContextKind == tok::l_paren || // function params
1319 Contexts.back().ContextKind == tok::l_square || // array type
1320 (!Contexts.back().IsExpression &&
1321 Contexts.back().ContextKind == tok::l_brace) || // object type
1322 (Contexts.size() == 1 &&
1323 Line.MustBeDeclaration)) { // method/property declaration
1324 Contexts.back().IsExpression = false;
1325 Tok->setType(TT_JsTypeColon);
1326 break;
1327 }
1328 } else if (Style.isCSharp()) {
1329 if (Contexts.back().InCSharpAttributeSpecifier) {
1330 Tok->setType(TT_AttributeColon);
1331 break;
1332 }
1333 if (Contexts.back().ContextKind == tok::l_paren) {
1334 Tok->setType(TT_CSharpNamedArgumentColon);
1335 break;
1336 }
1337 } else if (Style.isVerilog() && Tok->isNot(TT_BinaryOperator)) {
1338 // The distribution weight operators are labeled
1339 // TT_BinaryOperator by the lexer.
1340 if (Keywords.isVerilogEnd(*Tok->Previous) ||
1341 Keywords.isVerilogBegin(*Tok->Previous)) {
1342 Tok->setType(TT_VerilogBlockLabelColon);
1343 } else if (Contexts.back().ContextKind == tok::l_square) {
1344 Tok->setType(TT_BitFieldColon);
1345 } else if (Contexts.back().ColonIsDictLiteral) {
1346 Tok->setType(TT_DictLiteral);
1347 } else if (Contexts.size() == 1) {
1348 // In Verilog a case label doesn't have the case keyword. We
1349 // assume a colon following an expression is a case label.
1350 // Colons from ?: are annotated in parseConditional().
1351 Tok->setType(TT_CaseLabelColon);
1352 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1353 --Line.Level;
1354 }
1355 break;
1356 }
1357 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1358 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1359 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1360 Tok->setType(TT_ModulePartitionColon);
1361 } else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
1362 Tok->setType(TT_DictLiteral);
1363 if (Style.Language == FormatStyle::LK_TextProto) {
1364 if (FormatToken *Previous = Tok->getPreviousNonComment())
1365 Previous->setType(TT_SelectorName);
1366 }
1367 } else if (Contexts.back().ColonIsObjCMethodExpr ||
1368 Line.startsWith(TT_ObjCMethodSpecifier)) {
1369 Tok->setType(TT_ObjCMethodExpr);
1370 const FormatToken *BeforePrevious = Tok->Previous->Previous;
1371 // Ensure we tag all identifiers in method declarations as
1372 // TT_SelectorName.
1373 bool UnknownIdentifierInMethodDeclaration =
1374 Line.startsWith(TT_ObjCMethodSpecifier) &&
1375 Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
1376 if (!BeforePrevious ||
1377 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
1378 !(BeforePrevious->is(TT_CastRParen) ||
1379 (BeforePrevious->is(TT_ObjCMethodExpr) &&
1380 BeforePrevious->is(tok::colon))) ||
1381 BeforePrevious->is(tok::r_square) ||
1382 Contexts.back().LongestObjCSelectorName == 0 ||
1383 UnknownIdentifierInMethodDeclaration) {
1384 Tok->Previous->setType(TT_SelectorName);
1385 if (!Contexts.back().FirstObjCSelectorName) {
1386 Contexts.back().FirstObjCSelectorName = Tok->Previous;
1387 } else if (Tok->Previous->ColumnWidth >
1388 Contexts.back().LongestObjCSelectorName) {
1389 Contexts.back().LongestObjCSelectorName =
1390 Tok->Previous->ColumnWidth;
1391 }
1392 Tok->Previous->ParameterIndex =
1393 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1394 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1395 }
1396 } else if (Contexts.back().ColonIsForRangeExpr) {
1397 Tok->setType(TT_RangeBasedForLoopColon);
1398 } else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1399 Tok->setType(TT_GenericSelectionColon);
1400 } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
1401 Tok->setType(TT_BitFieldColon);
1402 } else if (Contexts.size() == 1 &&
1403 !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
1404 tok::kw_default)) {
1405 FormatToken *Prev = Tok->getPreviousNonComment();
1406 if (!Prev)
1407 break;
1408 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1409 Prev->ClosesRequiresClause) {
1410 Tok->setType(TT_CtorInitializerColon);
1411 } else if (Prev->is(tok::kw_try)) {
1412 // Member initializer list within function try block.
1413 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1414 if (!PrevPrev)
1415 break;
1416 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1417 Tok->setType(TT_CtorInitializerColon);
1418 } else {
1419 Tok->setType(TT_InheritanceColon);
1420 }
1421 } else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
1422 (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
1423 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
1424 Tok->Next->Next->is(tok::colon)))) {
1425 // This handles a special macro in ObjC code where selectors including
1426 // the colon are passed as macro arguments.
1427 Tok->setType(TT_ObjCMethodExpr);
1428 } else if (Contexts.back().ContextKind == tok::l_paren &&
1429 !Line.InPragmaDirective) {
1430 if (Style.isTableGen() && Contexts.back().IsTableGenDAGArg) {
1431 Tok->setType(TT_TableGenDAGArgListColon);
1432 break;
1433 }
1434 Tok->setType(TT_InlineASMColon);
1435 }
1436 break;
1437 case tok::pipe:
1438 case tok::amp:
1439 // | and & in declarations/type expressions represent union and
1440 // intersection types, respectively.
1441 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1442 Tok->setType(TT_JsTypeOperator);
1443 break;
1444 case tok::kw_if:
1445 if (Style.isTableGen()) {
1446 // In TableGen it has the form 'if' <value> 'then'.
1447 if (!parseTableGenValue())
1448 return false;
1449 if (CurrentToken && CurrentToken->is(Keywords.kw_then))
1450 next(); // skip then
1451 break;
1452 }
1453 if (CurrentToken &&
1454 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1455 next();
1456 }
1457 [[fallthrough]];
1458 case tok::kw_while:
1459 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1460 next();
1461 if (!parseParens(/*LookForDecls=*/true))
1462 return false;
1463 }
1464 break;
1465 case tok::kw_for:
1466 if (Style.isJavaScript()) {
1467 // x.for and {for: ...}
1468 if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
1469 (Tok->Next && Tok->Next->is(tok::colon))) {
1470 break;
1471 }
1472 // JS' for await ( ...
1473 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1474 next();
1475 }
1476 if (IsCpp && CurrentToken && CurrentToken->is(tok::kw_co_await))
1477 next();
1478 Contexts.back().ColonIsForRangeExpr = true;
1479 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1480 return false;
1481 next();
1482 if (!parseParens())
1483 return false;
1484 break;
1485 case tok::l_paren:
1486 // When faced with 'operator()()', the kw_operator handler incorrectly
1487 // marks the first l_paren as a OverloadedOperatorLParen. Here, we make
1488 // the first two parens OverloadedOperators and the second l_paren an
1489 // OverloadedOperatorLParen.
1490 if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
1491 Tok->Previous->MatchingParen &&
1492 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1493 Tok->Previous->setType(TT_OverloadedOperator);
1494 Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1495 Tok->setType(TT_OverloadedOperatorLParen);
1496 }
1497
1498 if (Style.isVerilog()) {
1499 // Identify the parameter list and port list in a module instantiation.
1500 // This is still needed when we already have
1501 // UnwrappedLineParser::parseVerilogHierarchyHeader because that
1502 // function is only responsible for the definition, not the
1503 // instantiation.
1504 auto IsInstancePort = [&]() {
1505 const FormatToken *Prev = Tok->getPreviousNonComment();
1506 const FormatToken *PrevPrev;
1507 // In the following example all 4 left parentheses will be treated as
1508 // 'TT_VerilogInstancePortLParen'.
1509 //
1510 // module_x instance_1(port_1); // Case A.
1511 // module_x #(parameter_1) // Case B.
1512 // instance_2(port_1), // Case C.
1513 // instance_3(port_1); // Case D.
1514 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1515 return false;
1516 // Case A.
1517 if (Keywords.isVerilogIdentifier(*Prev) &&
1518 Keywords.isVerilogIdentifier(*PrevPrev)) {
1519 return true;
1520 }
1521 // Case B.
1522 if (Prev->is(Keywords.kw_verilogHash) &&
1523 Keywords.isVerilogIdentifier(*PrevPrev)) {
1524 return true;
1525 }
1526 // Case C.
1527 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1528 return true;
1529 // Case D.
1530 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1531 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1532 if (PrevParen->is(tok::r_paren) && PrevParen->MatchingParen &&
1533 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1534 return true;
1535 }
1536 }
1537 return false;
1538 };
1539
1540 if (IsInstancePort())
1541 Tok->setFinalizedType(TT_VerilogInstancePortLParen);
1542 }
1543
1544 if (!parseParens())
1545 return false;
1546 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1547 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1548 !Line.startsWith(tok::l_paren) &&
1549 !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1550 if (const auto *Previous = Tok->Previous;
1551 !Previous ||
1552 (!Previous->isAttribute() &&
1553 !Previous->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation))) {
1554 Line.MightBeFunctionDecl = true;
1555 Tok->MightBeFunctionDeclParen = true;
1556 }
1557 }
1558 break;
1559 case tok::l_square:
1560 if (Style.isTableGen())
1561 Tok->setType(TT_TableGenListOpener);
1562 if (!parseSquare())
1563 return false;
1564 break;
1565 case tok::l_brace:
1566 if (Style.Language == FormatStyle::LK_TextProto) {
1567 FormatToken *Previous = Tok->getPreviousNonComment();
1568 if (Previous && Previous->isNot(TT_DictLiteral))
1569 Previous->setType(TT_SelectorName);
1570 }
1571 Scopes.push_back(getScopeType(*Tok));
1572 if (!parseBrace())
1573 return false;
1574 break;
1575 case tok::less:
1576 if (parseAngle()) {
1577 Tok->setType(TT_TemplateOpener);
1578 // In TT_Proto, we must distignuish between:
1579 // map<key, value>
1580 // msg < item: data >
1581 // msg: < item: data >
1582 // In TT_TextProto, map<key, value> does not occur.
1583 if (Style.Language == FormatStyle::LK_TextProto ||
1584 (Style.Language == FormatStyle::LK_Proto && Tok->Previous &&
1585 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1586 Tok->setType(TT_DictLiteral);
1587 FormatToken *Previous = Tok->getPreviousNonComment();
1588 if (Previous && Previous->isNot(TT_DictLiteral))
1589 Previous->setType(TT_SelectorName);
1590 }
1591 if (Style.isTableGen())
1592 Tok->setType(TT_TemplateOpener);
1593 } else {
1594 Tok->setType(TT_BinaryOperator);
1595 NonTemplateLess.insert(Tok);
1596 CurrentToken = Tok;
1597 next();
1598 }
1599 break;
1600 case tok::r_paren:
1601 case tok::r_square:
1602 return false;
1603 case tok::r_brace:
1604 // Don't pop scope when encountering unbalanced r_brace.
1605 if (!Scopes.empty())
1606 Scopes.pop_back();
1607 // Lines can start with '}'.
1608 if (Tok->Previous)
1609 return false;
1610 break;
1611 case tok::greater:
1612 if (Style.Language != FormatStyle::LK_TextProto)
1613 Tok->setType(TT_BinaryOperator);
1614 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1615 Tok->SpacesRequiredBefore = 1;
1616 break;
1617 case tok::kw_operator:
1618 if (Style.isProto())
1619 break;
1620 while (CurrentToken &&
1621 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1622 if (CurrentToken->isOneOf(tok::star, tok::amp))
1623 CurrentToken->setType(TT_PointerOrReference);
1624 auto Next = CurrentToken->getNextNonComment();
1625 if (!Next)
1626 break;
1627 if (Next->is(tok::less))
1628 next();
1629 else
1630 consumeToken();
1631 if (!CurrentToken)
1632 break;
1633 auto Previous = CurrentToken->getPreviousNonComment();
1634 assert(Previous);
1635 if (CurrentToken->is(tok::comma) && Previous->isNot(tok::kw_operator))
1636 break;
1637 if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1638 tok::star, tok::arrow, tok::amp, tok::ampamp) ||
1639 // User defined literal.
1640 Previous->TokenText.starts_with("\"\"")) {
1641 Previous->setType(TT_OverloadedOperator);
1642 if (CurrentToken->isOneOf(tok::less, tok::greater))
1643 break;
1644 }
1645 }
1646 if (CurrentToken && CurrentToken->is(tok::l_paren))
1647 CurrentToken->setType(TT_OverloadedOperatorLParen);
1648 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1649 CurrentToken->Previous->setType(TT_OverloadedOperator);
1650 break;
1651 case tok::question:
1652 if (Style.isJavaScript() && Tok->Next &&
1653 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1654 tok::r_brace, tok::r_square)) {
1655 // Question marks before semicolons, colons, etc. indicate optional
1656 // types (fields, parameters), e.g.
1657 // function(x?: string, y?) {...}
1658 // class X { y?; }
1659 Tok->setType(TT_JsTypeOptionalQuestion);
1660 break;
1661 }
1662 // Declarations cannot be conditional expressions, this can only be part
1663 // of a type declaration.
1664 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1665 Style.isJavaScript()) {
1666 break;
1667 }
1668 if (Style.isCSharp()) {
1669 // `Type?)`, `Type?>`, `Type? name;` and `Type? name =` can only be
1670 // nullable types.
1671
1672 // `Type?)`, `Type?>`, `Type? name;`
1673 if (Tok->Next &&
1674 (Tok->Next->startsSequence(tok::question, tok::r_paren) ||
1675 Tok->Next->startsSequence(tok::question, tok::greater) ||
1676 Tok->Next->startsSequence(tok::question, tok::identifier,
1677 tok::semi))) {
1678 Tok->setType(TT_CSharpNullable);
1679 break;
1680 }
1681
1682 // `Type? name =`
1683 if (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1684 Tok->Next->Next->is(tok::equal)) {
1685 Tok->setType(TT_CSharpNullable);
1686 break;
1687 }
1688
1689 // Line.MustBeDeclaration will be true for `Type? name;`.
1690 // But not
1691 // cond ? "A" : "B";
1692 // cond ? id : "B";
1693 // cond ? cond2 ? "A" : "B" : "C";
1694 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1695 (!Tok->Next ||
1696 !Tok->Next->isOneOf(tok::identifier, tok::string_literal) ||
1697 !Tok->Next->Next ||
1698 !Tok->Next->Next->isOneOf(tok::colon, tok::question))) {
1699 Tok->setType(TT_CSharpNullable);
1700 break;
1701 }
1702 }
1703 parseConditional();
1704 break;
1705 case tok::kw_template:
1706 parseTemplateDeclaration();
1707 break;
1708 case tok::comma:
1709 switch (Contexts.back().ContextType) {
1710 case Context::CtorInitializer:
1711 Tok->setType(TT_CtorInitializerComma);
1712 break;
1713 case Context::InheritanceList:
1714 Tok->setType(TT_InheritanceComma);
1715 break;
1716 case Context::VerilogInstancePortList:
1717 Tok->setFinalizedType(TT_VerilogInstancePortComma);
1718 break;
1719 default:
1720 if (Style.isVerilog() && Contexts.size() == 1 &&
1721 Line.startsWith(Keywords.kw_assign)) {
1722 Tok->setFinalizedType(TT_VerilogAssignComma);
1723 } else if (Contexts.back().FirstStartOfName &&
1724 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1725 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
1726 Line.IsMultiVariableDeclStmt = true;
1727 }
1728 break;
1729 }
1730 if (Contexts.back().ContextType == Context::ForEachMacro)
1731 Contexts.back().IsExpression = true;
1732 break;
1733 case tok::kw_default:
1734 // Unindent case labels.
1735 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*Tok) &&
1736 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1737 --Line.Level;
1738 }
1739 break;
1740 case tok::identifier:
1741 if (Tok->isOneOf(Keywords.kw___has_include,
1742 Keywords.kw___has_include_next)) {
1743 parseHasInclude();
1744 }
1745 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1746 Tok->Next->isNot(tok::l_paren)) {
1747 Tok->setType(TT_CSharpGenericTypeConstraint);
1748 parseCSharpGenericTypeConstraint();
1749 if (!Tok->getPreviousNonComment())
1750 Line.IsContinuation = true;
1751 }
1752 if (Style.isTableGen()) {
1753 if (Tok->is(Keywords.kw_assert)) {
1754 if (!parseTableGenValue())
1755 return false;
1756 } else if (Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) &&
1757 (!Tok->Next ||
1758 !Tok->Next->isOneOf(tok::colon, tok::l_brace))) {
1759 // The case NameValue appears.
1760 if (!parseTableGenValue(true))
1761 return false;
1762 }
1763 }
1764 break;
1765 case tok::arrow:
1766 if (Tok->Previous && Tok->Previous->is(tok::kw_noexcept))
1767 Tok->setType(TT_TrailingReturnArrow);
1768 break;
1769 case tok::equal:
1770 // In TableGen, there must be a value after "=";
1771 if (Style.isTableGen() && !parseTableGenValue())
1772 return false;
1773 break;
1774 default:
1775 break;
1776 }
1777 return true;
1778 }
1779
1780 void parseCSharpGenericTypeConstraint() {
1781 int OpenAngleBracketsCount = 0;
1782 while (CurrentToken) {
1783 if (CurrentToken->is(tok::less)) {
1784 // parseAngle is too greedy and will consume the whole line.
1785 CurrentToken->setType(TT_TemplateOpener);
1786 ++OpenAngleBracketsCount;
1787 next();
1788 } else if (CurrentToken->is(tok::greater)) {
1789 CurrentToken->setType(TT_TemplateCloser);
1790 --OpenAngleBracketsCount;
1791 next();
1792 } else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1793 // We allow line breaks after GenericTypeConstraintComma's
1794 // so do not flag commas in Generics as GenericTypeConstraintComma's.
1795 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1796 next();
1797 } else if (CurrentToken->is(Keywords.kw_where)) {
1798 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1799 next();
1800 } else if (CurrentToken->is(tok::colon)) {
1801 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1802 next();
1803 } else {
1804 next();
1805 }
1806 }
1807 }
1808
1809 void parseIncludeDirective() {
1810 if (CurrentToken && CurrentToken->is(tok::less)) {
1811 next();
1812 while (CurrentToken) {
1813 // Mark tokens up to the trailing line comments as implicit string
1814 // literals.
1815 if (CurrentToken->isNot(tok::comment) &&
1816 !CurrentToken->TokenText.starts_with("//")) {
1817 CurrentToken->setType(TT_ImplicitStringLiteral);
1818 }
1819 next();
1820 }
1821 }
1822 }
1823
1824 void parseWarningOrError() {
1825 next();
1826 // We still want to format the whitespace left of the first token of the
1827 // warning or error.
1828 next();
1829 while (CurrentToken) {
1830 CurrentToken->setType(TT_ImplicitStringLiteral);
1831 next();
1832 }
1833 }
1834
1835 void parsePragma() {
1836 next(); // Consume "pragma".
1837 if (CurrentToken &&
1838 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1839 Keywords.kw_region)) {
1840 bool IsMarkOrRegion =
1841 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1842 next();
1843 next(); // Consume first token (so we fix leading whitespace).
1844 while (CurrentToken) {
1845 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1846 CurrentToken->setType(TT_ImplicitStringLiteral);
1847 next();
1848 }
1849 }
1850 }
1851
1852 void parseHasInclude() {
1853 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1854 return;
1855 next(); // '('
1856 parseIncludeDirective();
1857 next(); // ')'
1858 }
1859
1860 LineType parsePreprocessorDirective() {
1861 bool IsFirstToken = CurrentToken->IsFirst;
1863 next();
1864 if (!CurrentToken)
1865 return Type;
1866
1867 if (Style.isJavaScript() && IsFirstToken) {
1868 // JavaScript files can contain shebang lines of the form:
1869 // #!/usr/bin/env node
1870 // Treat these like C++ #include directives.
1871 while (CurrentToken) {
1872 // Tokens cannot be comments here.
1873 CurrentToken->setType(TT_ImplicitStringLiteral);
1874 next();
1875 }
1876 return LT_ImportStatement;
1877 }
1878
1879 if (CurrentToken->is(tok::numeric_constant)) {
1880 CurrentToken->SpacesRequiredBefore = 1;
1881 return Type;
1882 }
1883 // Hashes in the middle of a line can lead to any strange token
1884 // sequence.
1885 if (!CurrentToken->Tok.getIdentifierInfo())
1886 return Type;
1887 // In Verilog macro expansions start with a backtick just like preprocessor
1888 // directives. Thus we stop if the word is not a preprocessor directive.
1889 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1890 return LT_Invalid;
1891 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1892 case tok::pp_include:
1893 case tok::pp_include_next:
1894 case tok::pp_import:
1895 next();
1896 parseIncludeDirective();
1898 break;
1899 case tok::pp_error:
1900 case tok::pp_warning:
1901 parseWarningOrError();
1902 break;
1903 case tok::pp_pragma:
1904 parsePragma();
1905 break;
1906 case tok::pp_if:
1907 case tok::pp_elif:
1908 Contexts.back().IsExpression = true;
1909 next();
1910 if (CurrentToken)
1911 CurrentToken->SpacesRequiredBefore = true;
1912 parseLine();
1913 break;
1914 default:
1915 break;
1916 }
1917 while (CurrentToken) {
1918 FormatToken *Tok = CurrentToken;
1919 next();
1920 if (Tok->is(tok::l_paren)) {
1921 parseParens();
1922 } else if (Tok->isOneOf(Keywords.kw___has_include,
1923 Keywords.kw___has_include_next)) {
1924 parseHasInclude();
1925 }
1926 }
1927 return Type;
1928 }
1929
1930public:
1931 LineType parseLine() {
1932 if (!CurrentToken)
1933 return LT_Invalid;
1934 NonTemplateLess.clear();
1935 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1936 // We were not yet allowed to use C++17 optional when this was being
1937 // written. So we used LT_Invalid to mark that the line is not a
1938 // preprocessor directive.
1939 auto Type = parsePreprocessorDirective();
1940 if (Type != LT_Invalid)
1941 return Type;
1942 }
1943
1944 // Directly allow to 'import <string-literal>' to support protocol buffer
1945 // definitions (github.com/google/protobuf) or missing "#" (either way we
1946 // should not break the line).
1947 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1948 if ((Style.Language == FormatStyle::LK_Java &&
1949 CurrentToken->is(Keywords.kw_package)) ||
1950 (!Style.isVerilog() && Info &&
1951 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1952 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1953 tok::kw_static))) {
1954 next();
1955 parseIncludeDirective();
1956 return LT_ImportStatement;
1957 }
1958
1959 // If this line starts and ends in '<' and '>', respectively, it is likely
1960 // part of "#define <a/b.h>".
1961 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1962 parseIncludeDirective();
1963 return LT_ImportStatement;
1964 }
1965
1966 // In .proto files, top-level options and package statements are very
1967 // similar to import statements and should not be line-wrapped.
1968 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
1969 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1970 next();
1971 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1972 while (CurrentToken)
1973 next();
1974 return LT_ImportStatement;
1975 }
1976 }
1977
1978 bool KeywordVirtualFound = false;
1979 bool ImportStatement = false;
1980
1981 // import {...} from '...';
1982 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
1983 ImportStatement = true;
1984
1985 while (CurrentToken) {
1986 if (CurrentToken->is(tok::kw_virtual))
1987 KeywordVirtualFound = true;
1988 if (Style.isJavaScript()) {
1989 // export {...} from '...';
1990 // An export followed by "from 'some string';" is a re-export from
1991 // another module identified by a URI and is treated as a
1992 // LT_ImportStatement (i.e. prevent wraps on it for long URIs).
1993 // Just "export {...};" or "export class ..." should not be treated as
1994 // an import in this sense.
1995 if (Line.First->is(tok::kw_export) &&
1996 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
1997 CurrentToken->Next->isStringLiteral()) {
1998 ImportStatement = true;
1999 }
2000 if (isClosureImportStatement(*CurrentToken))
2001 ImportStatement = true;
2002 }
2003 if (!consumeToken())
2004 return LT_Invalid;
2005 }
2006 if (KeywordVirtualFound)
2008 if (ImportStatement)
2009 return LT_ImportStatement;
2010
2011 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2012 if (Contexts.back().FirstObjCSelectorName) {
2013 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2014 Contexts.back().LongestObjCSelectorName;
2015 }
2016 return LT_ObjCMethodDecl;
2017 }
2018
2019 for (const auto &ctx : Contexts)
2020 if (ctx.ContextType == Context::StructArrayInitializer)
2022
2023 return LT_Other;
2024 }
2025
2026private:
2027 bool isClosureImportStatement(const FormatToken &Tok) {
2028 // FIXME: Closure-library specific stuff should not be hard-coded but be
2029 // configurable.
2030 return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
2031 Tok.Next->Next &&
2032 (Tok.Next->Next->TokenText == "module" ||
2033 Tok.Next->Next->TokenText == "provide" ||
2034 Tok.Next->Next->TokenText == "require" ||
2035 Tok.Next->Next->TokenText == "requireType" ||
2036 Tok.Next->Next->TokenText == "forwardDeclare") &&
2037 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
2038 }
2039
2040 void resetTokenMetadata() {
2041 if (!CurrentToken)
2042 return;
2043
2044 // Reset token type in case we have already looked at it and then
2045 // recovered from an error (e.g. failure to find the matching >).
2046 if (!CurrentToken->isTypeFinalized() &&
2047 !CurrentToken->isOneOf(
2048 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2049 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2050 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2051 TT_NamespaceMacro, TT_OverloadedOperator, TT_RegexLiteral,
2052 TT_TemplateString, TT_ObjCStringLiteral, TT_UntouchableMacroFunc,
2053 TT_StatementAttributeLikeMacro, TT_FunctionLikeOrFreestandingMacro,
2054 TT_ClassLBrace, TT_EnumLBrace, TT_RecordLBrace, TT_StructLBrace,
2055 TT_UnionLBrace, TT_RequiresClause,
2056 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2057 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2058 TT_BracedListLBrace)) {
2059 CurrentToken->setType(TT_Unknown);
2060 }
2061 CurrentToken->Role.reset();
2062 CurrentToken->MatchingParen = nullptr;
2063 CurrentToken->FakeLParens.clear();
2064 CurrentToken->FakeRParens = 0;
2065 }
2066
2067 void next() {
2068 if (!CurrentToken)
2069 return;
2070
2071 CurrentToken->NestingLevel = Contexts.size() - 1;
2072 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2073 modifyContext(*CurrentToken);
2074 determineTokenType(*CurrentToken);
2075 CurrentToken = CurrentToken->Next;
2076
2077 resetTokenMetadata();
2078 }
2079
2080 /// A struct to hold information valid in a specific context, e.g.
2081 /// a pair of parenthesis.
2082 struct Context {
2083 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
2084 bool IsExpression)
2087
2088 tok::TokenKind ContextKind;
2095 FormatToken *FirstObjCSelectorName = nullptr;
2096 FormatToken *FirstStartOfName = nullptr;
2097 bool CanBeExpression = true;
2098 bool CaretFound = false;
2102 // Whether the braces may mean concatenation instead of structure or array
2103 // literal.
2105 bool IsTableGenDAGArg = false;
2106 bool IsTableGenBangOpe = false;
2107 bool IsTableGenCondOpe = false;
2108 enum {
2109 Unknown,
2110 // Like the part after `:` in a constructor.
2111 // Context(...) : IsExpression(IsExpression)
2112 CtorInitializer,
2113 // Like in the parentheses in a foreach.
2114 ForEachMacro,
2115 // Like the inheritance list in a class declaration.
2116 // class Input : public IO
2117 InheritanceList,
2118 // Like in the braced list.
2119 // int x[] = {};
2120 StructArrayInitializer,
2121 // Like in `static_cast<int>`.
2122 TemplateArgument,
2123 // C11 _Generic selection.
2124 C11GenericSelection,
2125 // Like in the outer parentheses in `ffnand ff1(.q());`.
2126 VerilogInstancePortList,
2128 };
2129
2130 /// Puts a new \c Context onto the stack \c Contexts for the lifetime
2131 /// of each instance.
2132 struct ScopedContextCreator {
2133 AnnotatingParser &P;
2134
2135 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
2136 unsigned Increase)
2137 : P(P) {
2138 P.Contexts.push_back(Context(ContextKind,
2139 P.Contexts.back().BindingStrength + Increase,
2140 P.Contexts.back().IsExpression));
2141 }
2142
2143 ~ScopedContextCreator() {
2144 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
2145 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2146 P.Contexts.pop_back();
2147 P.Contexts.back().ContextType = Context::StructArrayInitializer;
2148 return;
2149 }
2150 }
2151 P.Contexts.pop_back();
2152 }
2153 };
2154
2155 void modifyContext(const FormatToken &Current) {
2156 auto AssignmentStartsExpression = [&]() {
2157 if (Current.getPrecedence() != prec::Assignment)
2158 return false;
2159
2160 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2161 return false;
2162 if (Line.First->is(tok::kw_template)) {
2163 assert(Current.Previous);
2164 if (Current.Previous->is(tok::kw_operator)) {
2165 // `template ... operator=` cannot be an expression.
2166 return false;
2167 }
2168
2169 // `template` keyword can start a variable template.
2170 const FormatToken *Tok = Line.First->getNextNonComment();
2171 assert(Tok); // Current token is on the same line.
2172 if (Tok->isNot(TT_TemplateOpener)) {
2173 // Explicit template instantiations do not have `<>`.
2174 return false;
2175 }
2176
2177 // This is the default value of a template parameter, determine if it's
2178 // type or non-type.
2179 if (Contexts.back().ContextKind == tok::less) {
2180 assert(Current.Previous->Previous);
2181 return !Current.Previous->Previous->isOneOf(tok::kw_typename,
2182 tok::kw_class);
2183 }
2184
2185 Tok = Tok->MatchingParen;
2186 if (!Tok)
2187 return false;
2188 Tok = Tok->getNextNonComment();
2189 if (!Tok)
2190 return false;
2191
2192 if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2193 tok::kw_using)) {
2194 return false;
2195 }
2196
2197 return true;
2198 }
2199
2200 // Type aliases use `type X = ...;` in TypeScript and can be exported
2201 // using `export type ...`.
2202 if (Style.isJavaScript() &&
2203 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2204 Line.startsWith(tok::kw_export, Keywords.kw_type,
2205 tok::identifier))) {
2206 return false;
2207 }
2208
2209 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2210 };
2211
2212 if (AssignmentStartsExpression()) {
2213 Contexts.back().IsExpression = true;
2214 if (!Line.startsWith(TT_UnaryOperator)) {
2215 for (FormatToken *Previous = Current.Previous;
2216 Previous && Previous->Previous &&
2217 !Previous->Previous->isOneOf(tok::comma, tok::semi);
2218 Previous = Previous->Previous) {
2219 if (Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2220 Previous = Previous->MatchingParen;
2221 if (!Previous)
2222 break;
2223 }
2224 if (Previous->opensScope())
2225 break;
2226 if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2227 Previous->isPointerOrReference() && Previous->Previous &&
2228 Previous->Previous->isNot(tok::equal)) {
2229 Previous->setType(TT_PointerOrReference);
2230 }
2231 }
2232 }
2233 } else if (Current.is(tok::lessless) &&
2234 (!Current.Previous ||
2235 Current.Previous->isNot(tok::kw_operator))) {
2236 Contexts.back().IsExpression = true;
2237 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2238 Contexts.back().IsExpression = true;
2239 } else if (Current.is(TT_TrailingReturnArrow)) {
2240 Contexts.back().IsExpression = false;
2241 } else if (Current.is(Keywords.kw_assert)) {
2242 Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
2243 } else if (Current.Previous &&
2244 Current.Previous->is(TT_CtorInitializerColon)) {
2245 Contexts.back().IsExpression = true;
2246 Contexts.back().ContextType = Context::CtorInitializer;
2247 } else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2248 Contexts.back().ContextType = Context::InheritanceList;
2249 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2250 for (FormatToken *Previous = Current.Previous;
2251 Previous && Previous->isOneOf(tok::star, tok::amp);
2252 Previous = Previous->Previous) {
2253 Previous->setType(TT_PointerOrReference);
2254 }
2255 if (Line.MustBeDeclaration &&
2256 Contexts.front().ContextType != Context::CtorInitializer) {
2257 Contexts.back().IsExpression = false;
2258 }
2259 } else if (Current.is(tok::kw_new)) {
2260 Contexts.back().CanBeExpression = false;
2261 } else if (Current.is(tok::semi) ||
2262 (Current.is(tok::exclaim) && Current.Previous &&
2263 Current.Previous->isNot(tok::kw_operator))) {
2264 // This should be the condition or increment in a for-loop.
2265 // But not operator !() (can't use TT_OverloadedOperator here as its not
2266 // been annotated yet).
2267 Contexts.back().IsExpression = true;
2268 }
2269 }
2270
2271 static FormatToken *untilMatchingParen(FormatToken *Current) {
2272 // Used when `MatchingParen` is not yet established.
2273 int ParenLevel = 0;
2274 while (Current) {
2275 if (Current->is(tok::l_paren))
2276 ++ParenLevel;
2277 if (Current->is(tok::r_paren))
2278 --ParenLevel;
2279 if (ParenLevel < 1)
2280 break;
2281 Current = Current->Next;
2282 }
2283 return Current;
2284 }
2285
2286 static bool isDeductionGuide(FormatToken &Current) {
2287 // Look for a deduction guide template<T> A(...) -> A<...>;
2288 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2289 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2290 // Find the TemplateCloser.
2291 FormatToken *TemplateCloser = Current.Next->Next;
2292 int NestingLevel = 0;
2293 while (TemplateCloser) {
2294 // Skip over an expressions in parens A<(3 < 2)>;
2295 if (TemplateCloser->is(tok::l_paren)) {
2296 // No Matching Paren yet so skip to matching paren
2297 TemplateCloser = untilMatchingParen(TemplateCloser);
2298 if (!TemplateCloser)
2299 break;
2300 }
2301 if (TemplateCloser->is(tok::less))
2302 ++NestingLevel;
2303 if (TemplateCloser->is(tok::greater))
2304 --NestingLevel;
2305 if (NestingLevel < 1)
2306 break;
2307 TemplateCloser = TemplateCloser->Next;
2308 }
2309 // Assuming we have found the end of the template ensure its followed
2310 // with a semi-colon.
2311 if (TemplateCloser && TemplateCloser->Next &&
2312 TemplateCloser->Next->is(tok::semi) &&
2313 Current.Previous->MatchingParen) {
2314 // Determine if the identifier `A` prior to the A<..>; is the same as
2315 // prior to the A(..)
2316 FormatToken *LeadingIdentifier =
2317 Current.Previous->MatchingParen->Previous;
2318
2319 return LeadingIdentifier &&
2320 LeadingIdentifier->TokenText == Current.Next->TokenText;
2321 }
2322 }
2323 return false;
2324 }
2325
2326 void determineTokenType(FormatToken &Current) {
2327 if (Current.isNot(TT_Unknown)) {
2328 // The token type is already known.
2329 return;
2330 }
2331
2332 if ((Style.isJavaScript() || Style.isCSharp()) &&
2333 Current.is(tok::exclaim)) {
2334 if (Current.Previous) {
2335 bool IsIdentifier =
2336 Style.isJavaScript()
2337 ? Keywords.IsJavaScriptIdentifier(
2338 *Current.Previous, /* AcceptIdentifierName= */ true)
2339 : Current.Previous->is(tok::identifier);
2340 if (IsIdentifier ||
2341 Current.Previous->isOneOf(
2342 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2343 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2344 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2345 Current.Previous->Tok.isLiteral()) {
2346 Current.setType(TT_NonNullAssertion);
2347 return;
2348 }
2349 }
2350 if (Current.Next &&
2351 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2352 Current.setType(TT_NonNullAssertion);
2353 return;
2354 }
2355 }
2356
2357 // Line.MightBeFunctionDecl can only be true after the parentheses of a
2358 // function declaration have been found. In this case, 'Current' is a
2359 // trailing token of this declaration and thus cannot be a name.
2360 if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) &&
2361 Current.is(Keywords.kw_instanceof)) {
2362 Current.setType(TT_BinaryOperator);
2363 } else if (isStartOfName(Current) &&
2364 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2365 Contexts.back().FirstStartOfName = &Current;
2366 Current.setType(TT_StartOfName);
2367 } else if (Current.is(tok::semi)) {
2368 // Reset FirstStartOfName after finding a semicolon so that a for loop
2369 // with multiple increment statements is not confused with a for loop
2370 // having multiple variable declarations.
2371 Contexts.back().FirstStartOfName = nullptr;
2372 } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2373 AutoFound = true;
2374 } else if (Current.is(tok::arrow) &&
2375 Style.Language == FormatStyle::LK_Java) {
2376 Current.setType(TT_TrailingReturnArrow);
2377 } else if (Current.is(tok::arrow) && Style.isVerilog()) {
2378 // The implication operator.
2379 Current.setType(TT_BinaryOperator);
2380 } else if (Current.is(tok::arrow) && AutoFound &&
2381 Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2382 !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
2383 // not auto operator->() -> xxx;
2384 Current.setType(TT_TrailingReturnArrow);
2385 } else if (Current.is(tok::arrow) && Current.Previous &&
2386 Current.Previous->is(tok::r_brace)) {
2387 // Concept implicit conversion constraint needs to be treated like
2388 // a trailing return type ... } -> <type>.
2389 Current.setType(TT_TrailingReturnArrow);
2390 } else if (isDeductionGuide(Current)) {
2391 // Deduction guides trailing arrow " A(...) -> A<T>;".
2392 Current.setType(TT_TrailingReturnArrow);
2393 } else if (Current.isPointerOrReference()) {
2394 Current.setType(determineStarAmpUsage(
2395 Current,
2396 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2397 Contexts.back().ContextType == Context::TemplateArgument));
2398 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2399 (Style.isVerilog() && Current.is(tok::pipe))) {
2400 Current.setType(determinePlusMinusCaretUsage(Current));
2401 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2402 Contexts.back().CaretFound = true;
2403 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2404 Current.setType(determineIncrementUsage(Current));
2405 } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2406 Current.setType(TT_UnaryOperator);
2407 } else if (Current.is(tok::question)) {
2408 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2409 !Contexts.back().IsExpression) {
2410 // In JavaScript, `interface X { foo?(): bar; }` is an optional method
2411 // on the interface, not a ternary expression.
2412 Current.setType(TT_JsTypeOptionalQuestion);
2413 } else if (Style.isTableGen()) {
2414 // In TableGen, '?' is just an identifier like token.
2415 Current.setType(TT_Unknown);
2416 } else {
2417 Current.setType(TT_ConditionalExpr);
2418 }
2419 } else if (Current.isBinaryOperator() &&
2420 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2421 (Current.isNot(tok::greater) &&
2422 Style.Language != FormatStyle::LK_TextProto)) {
2423 if (Style.isVerilog()) {
2424 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2425 !Contexts.back().VerilogAssignmentFound) {
2426 // In Verilog `<=` is assignment if in its own statement. It is a
2427 // statement instead of an expression, that is it can not be chained.
2428 Current.ForcedPrecedence = prec::Assignment;
2429 Current.setFinalizedType(TT_BinaryOperator);
2430 }
2431 if (Current.getPrecedence() == prec::Assignment)
2432 Contexts.back().VerilogAssignmentFound = true;
2433 }
2434 Current.setType(TT_BinaryOperator);
2435 } else if (Current.is(tok::comment)) {
2436 if (Current.TokenText.starts_with("/*")) {
2437 if (Current.TokenText.ends_with("*/")) {
2438 Current.setType(TT_BlockComment);
2439 } else {
2440 // The lexer has for some reason determined a comment here. But we
2441 // cannot really handle it, if it isn't properly terminated.
2442 Current.Tok.setKind(tok::unknown);
2443 }
2444 } else {
2445 Current.setType(TT_LineComment);
2446 }
2447 } else if (Current.is(tok::string_literal)) {
2448 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2449 Current.getPreviousNonComment() &&
2450 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2451 Current.getNextNonComment() &&
2452 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2453 Current.setType(TT_StringInConcatenation);
2454 }
2455 } else if (Current.is(tok::l_paren)) {
2456 if (lParenStartsCppCast(Current))
2457 Current.setType(TT_CppCastLParen);
2458 } else if (Current.is(tok::r_paren)) {
2459 if (rParenEndsCast(Current))
2460 Current.setType(TT_CastRParen);
2461 if (Current.MatchingParen && Current.Next &&
2462 !Current.Next->isBinaryOperator() &&
2463 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
2464 tok::comma, tok::period, tok::arrow,
2465 tok::coloncolon, tok::kw_noexcept)) {
2466 if (FormatToken *AfterParen = Current.MatchingParen->Next;
2467 AfterParen && AfterParen->isNot(tok::caret)) {
2468 // Make sure this isn't the return type of an Obj-C block declaration.
2469 if (FormatToken *BeforeParen = Current.MatchingParen->Previous;
2470 BeforeParen && BeforeParen->is(tok::identifier) &&
2471 BeforeParen->isNot(TT_TypenameMacro) &&
2472 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2473 (!BeforeParen->Previous ||
2474 BeforeParen->Previous->ClosesTemplateDeclaration ||
2475 BeforeParen->Previous->ClosesRequiresClause)) {
2476 Current.setType(TT_FunctionAnnotationRParen);
2477 }
2478 }
2479 }
2480 } else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2481 Style.Language != FormatStyle::LK_Java) {
2482 // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
2483 // marks declarations and properties that need special formatting.
2484 switch (Current.Next->Tok.getObjCKeywordID()) {
2485 case tok::objc_interface:
2486 case tok::objc_implementation:
2487 case tok::objc_protocol:
2488 Current.setType(TT_ObjCDecl);
2489 break;
2490 case tok::objc_property:
2491 Current.setType(TT_ObjCProperty);
2492 break;
2493 default:
2494 break;
2495 }
2496 } else if (Current.is(tok::period)) {
2497 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2498 if (PreviousNoComment &&
2499 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2500 Current.setType(TT_DesignatedInitializerPeriod);
2501 } else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
2502 Current.Previous->isOneOf(TT_JavaAnnotation,
2503 TT_LeadingJavaAnnotation)) {
2504 Current.setType(Current.Previous->getType());
2505 }
2506 } else if (canBeObjCSelectorComponent(Current) &&
2507 // FIXME(bug 36976): ObjC return types shouldn't use
2508 // TT_CastRParen.
2509 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2510 Current.Previous->MatchingParen &&
2511 Current.Previous->MatchingParen->Previous &&
2512 Current.Previous->MatchingParen->Previous->is(
2513 TT_ObjCMethodSpecifier)) {
2514 // This is the first part of an Objective-C selector name. (If there's no
2515 // colon after this, this is the only place which annotates the identifier
2516 // as a selector.)
2517 Current.setType(TT_SelectorName);
2518 } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2519 tok::kw_requires) &&
2520 Current.Previous &&
2521 !Current.Previous->isOneOf(tok::equal, tok::at,
2522 TT_CtorInitializerComma,
2523 TT_CtorInitializerColon) &&
2524 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2525 // Line.MightBeFunctionDecl can only be true after the parentheses of a
2526 // function declaration have been found.
2527 Current.setType(TT_TrailingAnnotation);
2528 } else if ((Style.Language == FormatStyle::LK_Java ||
2529 Style.isJavaScript()) &&
2530 Current.Previous) {
2531 if (Current.Previous->is(tok::at) &&
2532 Current.isNot(Keywords.kw_interface)) {
2533 const FormatToken &AtToken = *Current.Previous;
2534 const FormatToken *Previous = AtToken.getPreviousNonComment();
2535 if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
2536 Current.setType(TT_LeadingJavaAnnotation);
2537 else
2538 Current.setType(TT_JavaAnnotation);
2539 } else if (Current.Previous->is(tok::period) &&
2540 Current.Previous->isOneOf(TT_JavaAnnotation,
2541 TT_LeadingJavaAnnotation)) {
2542 Current.setType(Current.Previous->getType());
2543 }
2544 }
2545 }
2546
2547 /// Take a guess at whether \p Tok starts a name of a function or
2548 /// variable declaration.
2549 ///
2550 /// This is a heuristic based on whether \p Tok is an identifier following
2551 /// something that is likely a type.
2552 bool isStartOfName(const FormatToken &Tok) {
2553 // Handled in ExpressionParser for Verilog.
2554 if (Style.isVerilog())
2555 return false;
2556
2557 if (Tok.isNot(tok::identifier) || !Tok.Previous)
2558 return false;
2559
2560 if (const auto *NextNonComment = Tok.getNextNonComment();
2561 (!NextNonComment && !Line.InMacroBody) ||
2562 (NextNonComment &&
2563 (NextNonComment->isPointerOrReference() ||
2564 NextNonComment->is(tok::string_literal) ||
2565 (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2566 return false;
2567 }
2568
2569 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2570 Keywords.kw_as)) {
2571 return false;
2572 }
2573 if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2574 return false;
2575
2576 // Skip "const" as it does not have an influence on whether this is a name.
2577 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2578
2579 // For javascript const can be like "let" or "var"
2580 if (!Style.isJavaScript())
2581 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2582 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2583
2584 if (!PreviousNotConst)
2585 return false;
2586
2587 if (PreviousNotConst->ClosesRequiresClause)
2588 return false;
2589
2590 if (Style.isTableGen()) {
2591 // keywords such as let and def* defines names.
2592 if (Keywords.isTableGenDefinition(*PreviousNotConst))
2593 return true;
2594 // Otherwise C++ style declarations is available only inside the brace.
2595 if (Contexts.back().ContextKind != tok::l_brace)
2596 return false;
2597 }
2598
2599 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2600 PreviousNotConst->Previous &&
2601 PreviousNotConst->Previous->is(tok::hash);
2602
2603 if (PreviousNotConst->is(TT_TemplateCloser)) {
2604 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2605 PreviousNotConst->MatchingParen->Previous &&
2606 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2607 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2608 }
2609
2610 if ((PreviousNotConst->is(tok::r_paren) &&
2611 PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2612 PreviousNotConst->is(TT_AttributeRParen)) {
2613 return true;
2614 }
2615
2616 // If is a preprocess keyword like #define.
2617 if (IsPPKeyword)
2618 return false;
2619
2620 // int a or auto a.
2621 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto))
2622 return true;
2623
2624 // *a or &a or &&a.
2625 if (PreviousNotConst->is(TT_PointerOrReference))
2626 return true;
2627
2628 // MyClass a;
2629 if (PreviousNotConst->isTypeName(LangOpts))
2630 return true;
2631
2632 // type[] a in Java
2633 if (Style.Language == FormatStyle::LK_Java &&
2634 PreviousNotConst->is(tok::r_square)) {
2635 return true;
2636 }
2637
2638 // const a = in JavaScript.
2639 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2640 }
2641
2642 /// Determine whether '(' is starting a C++ cast.
2643 bool lParenStartsCppCast(const FormatToken &Tok) {
2644 // C-style casts are only used in C++.
2645 if (!IsCpp)
2646 return false;
2647
2648 FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2649 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2650 LeftOfParens->MatchingParen) {
2651 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2652 if (Prev &&
2653 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2654 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2655 // FIXME: Maybe we should handle identifiers ending with "_cast",
2656 // e.g. any_cast?
2657 return true;
2658 }
2659 }
2660 return false;
2661 }
2662
2663 /// Determine whether ')' is ending a cast.
2664 bool rParenEndsCast(const FormatToken &Tok) {
2665 // C-style casts are only used in C++, C# and Java.
2666 if (!Style.isCSharp() && !IsCpp && Style.Language != FormatStyle::LK_Java)
2667 return false;
2668
2669 // Empty parens aren't casts and there are no casts at the end of the line.
2670 if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
2671 return false;
2672
2673 if (Tok.MatchingParen->is(TT_OverloadedOperatorLParen))
2674 return false;
2675
2676 FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
2677 if (LeftOfParens) {
2678 // If there is a closing parenthesis left of the current
2679 // parentheses, look past it as these might be chained casts.
2680 if (LeftOfParens->is(tok::r_paren) &&
2681 LeftOfParens->isNot(TT_CastRParen)) {
2682 if (!LeftOfParens->MatchingParen ||
2683 !LeftOfParens->MatchingParen->Previous) {
2684 return false;
2685 }
2686 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2687 }
2688
2689 if (LeftOfParens->is(tok::r_square)) {
2690 // delete[] (void *)ptr;
2691 auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2692 if (Tok->isNot(tok::r_square))
2693 return nullptr;
2694
2695 Tok = Tok->getPreviousNonComment();
2696 if (!Tok || Tok->isNot(tok::l_square))
2697 return nullptr;
2698
2699 Tok = Tok->getPreviousNonComment();
2700 if (!Tok || Tok->isNot(tok::kw_delete))
2701 return nullptr;
2702 return Tok;
2703 };
2704 if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2705 LeftOfParens = MaybeDelete;
2706 }
2707
2708 // The Condition directly below this one will see the operator arguments
2709 // as a (void *foo) cast.
2710 // void operator delete(void *foo) ATTRIB;
2711 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2712 LeftOfParens->Previous->is(tok::kw_operator)) {
2713 return false;
2714 }
2715
2716 // If there is an identifier (or with a few exceptions a keyword) right
2717 // before the parentheses, this is unlikely to be a cast.
2718 if (LeftOfParens->Tok.getIdentifierInfo() &&
2719 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2720 tok::kw_delete, tok::kw_throw)) {
2721 return false;
2722 }
2723
2724 // Certain other tokens right before the parentheses are also signals that
2725 // this cannot be a cast.
2726 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2727 TT_TemplateCloser, tok::ellipsis)) {
2728 return false;
2729 }
2730 }
2731
2732 if (Tok.Next->is(tok::question) ||
2733 (Tok.Next->is(tok::ampamp) && !Tok.Previous->isTypeName(LangOpts))) {
2734 return false;
2735 }
2736
2737 // `foreach((A a, B b) in someList)` should not be seen as a cast.
2738 if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
2739 return false;
2740
2741 // Functions which end with decorations like volatile, noexcept are unlikely
2742 // to be casts.
2743 if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2744 tok::kw_requires, tok::kw_throw, tok::arrow,
2745 Keywords.kw_override, Keywords.kw_final) ||
2746 isCppAttribute(IsCpp, *Tok.Next)) {
2747 return false;
2748 }
2749
2750 // As Java has no function types, a "(" after the ")" likely means that this
2751 // is a cast.
2752 if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
2753 return true;
2754
2755 // If a (non-string) literal follows, this is likely a cast.
2756 if (Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2757 (Tok.Next->Tok.isLiteral() && Tok.Next->isNot(tok::string_literal))) {
2758 return true;
2759 }
2760
2761 // Heuristically try to determine whether the parentheses contain a type.
2762 auto IsQualifiedPointerOrReference = [](FormatToken *T,
2763 const LangOptions &LangOpts) {
2764 // This is used to handle cases such as x = (foo *const)&y;
2765 assert(!T->isTypeName(LangOpts) && "Should have already been checked");
2766 // Strip trailing qualifiers such as const or volatile when checking
2767 // whether the parens could be a cast to a pointer/reference type.
2768 while (T) {
2769 if (T->is(TT_AttributeRParen)) {
2770 // Handle `x = (foo *__attribute__((foo)))&v;`:
2771 assert(T->is(tok::r_paren));
2772 assert(T->MatchingParen);
2773 assert(T->MatchingParen->is(tok::l_paren));
2774 assert(T->MatchingParen->is(TT_AttributeLParen));
2775 if (const auto *Tok = T->MatchingParen->Previous;
2776 Tok && Tok->isAttribute()) {
2777 T = Tok->Previous;
2778 continue;
2779 }
2780 } else if (T->is(TT_AttributeSquare)) {
2781 // Handle `x = (foo *[[clang::foo]])&v;`:
2782 if (T->MatchingParen && T->MatchingParen->Previous) {
2783 T = T->MatchingParen->Previous;
2784 continue;
2785 }
2786 } else if (T->canBePointerOrReferenceQualifier()) {
2787 T = T->Previous;
2788 continue;
2789 }
2790 break;
2791 }
2792 return T && T->is(TT_PointerOrReference);
2793 };
2794 bool ParensAreType =
2795 !Tok.Previous ||
2796 Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2797 Tok.Previous->isTypeName(LangOpts) ||
2798 IsQualifiedPointerOrReference(Tok.Previous, LangOpts);
2799 bool ParensCouldEndDecl =
2800 Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2801 if (ParensAreType && !ParensCouldEndDecl)
2802 return true;
2803
2804 // At this point, we heuristically assume that there are no casts at the
2805 // start of the line. We assume that we have found most cases where there
2806 // are by the logic above, e.g. "(void)x;".
2807 if (!LeftOfParens)
2808 return false;
2809
2810 // Certain token types inside the parentheses mean that this can't be a
2811 // cast.
2812 for (const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
2813 Token = Token->Next) {
2814 if (Token->is(TT_BinaryOperator))
2815 return false;
2816 }
2817
2818 // If the following token is an identifier or 'this', this is a cast. All
2819 // cases where this can be something else are handled above.
2820 if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
2821 return true;
2822
2823 // Look for a cast `( x ) (`.
2824 if (Tok.Next->is(tok::l_paren) && Tok.Previous && Tok.Previous->Previous) {
2825 if (Tok.Previous->is(tok::identifier) &&
2826 Tok.Previous->Previous->is(tok::l_paren)) {
2827 return true;
2828 }
2829 }
2830
2831 if (!Tok.Next->Next)
2832 return false;
2833
2834 // If the next token after the parenthesis is a unary operator, assume
2835 // that this is cast, unless there are unexpected tokens inside the
2836 // parenthesis.
2837 const bool NextIsAmpOrStar = Tok.Next->isOneOf(tok::amp, tok::star);
2838 if (!(Tok.Next->isUnaryOperator() || NextIsAmpOrStar) ||
2839 Tok.Next->is(tok::plus) ||
2840 !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2841 return false;
2842 }
2843 if (NextIsAmpOrStar &&
2844 (Tok.Next->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2845 return false;
2846 }
2847 if (Line.InPPDirective && Tok.Next->is(tok::minus))
2848 return false;
2849 // Search for unexpected tokens.
2850 for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
2851 Prev = Prev->Previous) {
2852 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2853 return false;
2854 }
2855 return true;
2856 }
2857
2858 /// Returns true if the token is used as a unary operator.
2859 bool determineUnaryOperatorByUsage(const FormatToken &Tok) {
2860 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2861 if (!PrevToken)
2862 return true;
2863
2864 // These keywords are deliberately not included here because they may
2865 // precede only one of unary star/amp and plus/minus but not both. They are
2866 // either included in determineStarAmpUsage or determinePlusMinusCaretUsage.
2867 //
2868 // @ - It may be followed by a unary `-` in Objective-C literals. We don't
2869 // know how they can be followed by a star or amp.
2870 if (PrevToken->isOneOf(
2871 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2872 tok::equal, tok::question, tok::l_square, tok::l_brace,
2873 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2874 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2875 return true;
2876 }
2877
2878 // We put sizeof here instead of only in determineStarAmpUsage. In the cases
2879 // where the unary `+` operator is overloaded, it is reasonable to write
2880 // things like `sizeof +x`. Like commit 446d6ec996c6c3.
2881 if (PrevToken->is(tok::kw_sizeof))
2882 return true;
2883
2884 // A sequence of leading unary operators.
2885 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2886 return true;
2887
2888 // There can't be two consecutive binary operators.
2889 if (PrevToken->is(TT_BinaryOperator))
2890 return true;
2891
2892 return false;
2893 }
2894
2895 /// Return the type of the given token assuming it is * or &.
2896 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
2897 bool InTemplateArgument) {
2898 if (Style.isJavaScript())
2899 return TT_BinaryOperator;
2900
2901 // && in C# must be a binary operator.
2902 if (Style.isCSharp() && Tok.is(tok::ampamp))
2903 return TT_BinaryOperator;
2904
2905 if (Style.isVerilog()) {
2906 // In Verilog, `*` can only be a binary operator. `&` can be either unary
2907 // or binary. `*` also includes `*>` in module path declarations in
2908 // specify blocks because merged tokens take the type of the first one by
2909 // default.
2910 if (Tok.is(tok::star))
2911 return TT_BinaryOperator;
2912 return determineUnaryOperatorByUsage(Tok) ? TT_UnaryOperator
2913 : TT_BinaryOperator;
2914 }
2915
2916 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2917 if (!PrevToken)
2918 return TT_UnaryOperator;
2919 if (PrevToken->is(TT_TypeName))
2920 return TT_PointerOrReference;
2921 if (PrevToken->isOneOf(tok::kw_new, tok::kw_delete) && Tok.is(tok::ampamp))
2922 return TT_BinaryOperator;
2923
2924 const FormatToken *NextToken = Tok.getNextNonComment();
2925
2926 if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
2927 return TT_BinaryOperator;
2928
2929 if (!NextToken ||
2930 NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
2931 TT_RequiresClause) ||
2932 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
2933 NextToken->canBePointerOrReferenceQualifier() ||
2934 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
2935 return TT_PointerOrReference;
2936 }
2937
2938 if (PrevToken->is(tok::coloncolon))
2939 return TT_PointerOrReference;
2940
2941 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
2942 return TT_PointerOrReference;
2943
2944 if (determineUnaryOperatorByUsage(Tok))
2945 return TT_UnaryOperator;
2946
2947 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
2948 return TT_PointerOrReference;
2949 if (NextToken->is(tok::kw_operator) && !IsExpression)
2950 return TT_PointerOrReference;
2951 if (NextToken->isOneOf(tok::comma, tok::semi))
2952 return TT_PointerOrReference;
2953
2954 // After right braces, star tokens are likely to be pointers to struct,
2955 // union, or class.
2956 // struct {} *ptr;
2957 // This by itself is not sufficient to distinguish from multiplication
2958 // following a brace-initialized expression, as in:
2959 // int i = int{42} * 2;
2960 // In the struct case, the part of the struct declaration until the `{` and
2961 // the `}` are put on separate unwrapped lines; in the brace-initialized
2962 // case, the matching `{` is on the same unwrapped line, so check for the
2963 // presence of the matching brace to distinguish between those.
2964 if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) &&
2965 !PrevToken->MatchingParen) {
2966 return TT_PointerOrReference;
2967 }
2968
2969 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
2970 return TT_UnaryOperator;
2971
2972 if (PrevToken->Tok.isLiteral() ||
2973 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
2974 tok::kw_false, tok::r_brace)) {
2975 return TT_BinaryOperator;
2976 }
2977
2978 const FormatToken *NextNonParen = NextToken;
2979 while (NextNonParen && NextNonParen->is(tok::l_paren))
2980 NextNonParen = NextNonParen->getNextNonComment();
2981 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
2982 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
2983 NextNonParen->isUnaryOperator())) {
2984 return TT_BinaryOperator;
2985 }
2986
2987 // If we know we're in a template argument, there are no named declarations.
2988 // Thus, having an identifier on the right-hand side indicates a binary
2989 // operator.
2990 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
2991 return TT_BinaryOperator;
2992
2993 // "&&" followed by "(", "*", or "&" is quite unlikely to be two successive
2994 // unary "&".
2995 if (Tok.is(tok::ampamp) &&
2996 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
2997 return TT_BinaryOperator;
2998 }
2999
3000 // This catches some cases where evaluation order is used as control flow:
3001 // aaa && aaa->f();
3002 if (NextToken->Tok.isAnyIdentifier()) {
3003 const FormatToken *NextNextToken = NextToken->getNextNonComment();
3004 if (NextNextToken && NextNextToken->is(tok::arrow))
3005 return TT_BinaryOperator;
3006 }
3007
3008 // It is very unlikely that we are going to find a pointer or reference type
3009 // definition on the RHS of an assignment.
3010 if (IsExpression && !Contexts.back().CaretFound)
3011 return TT_BinaryOperator;
3012
3013 // Opeartors at class scope are likely pointer or reference members.
3014 if (!Scopes.empty() && Scopes.back() == ST_Class)
3015 return TT_PointerOrReference;
3016
3017 // Tokens that indicate member access or chained operator& use.
3018 auto IsChainedOperatorAmpOrMember = [](const FormatToken *token) {
3019 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3020 tok::arrowstar, tok::periodstar);
3021 };
3022
3023 // It's more likely that & represents operator& than an uninitialized
3024 // reference.
3025 if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
3026 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3027 NextToken && NextToken->Tok.isAnyIdentifier()) {
3028 if (auto NextNext = NextToken->getNextNonComment();
3029 NextNext &&
3030 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3031 return TT_BinaryOperator;
3032 }
3033 }
3034
3035 return TT_PointerOrReference;
3036 }
3037
3038 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
3039 if (determineUnaryOperatorByUsage(Tok))
3040 return TT_UnaryOperator;
3041
3042 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3043 if (!PrevToken)
3044 return TT_UnaryOperator;
3045
3046 if (PrevToken->is(tok::at))
3047 return TT_UnaryOperator;
3048
3049 // Fall back to marking the token as binary operator.
3050 return TT_BinaryOperator;
3051 }
3052
3053 /// Determine whether ++/-- are pre- or post-increments/-decrements.
3054 TokenType determineIncrementUsage(const FormatToken &Tok) {
3055 const FormatToken *PrevToken = Tok.getPreviousNonComment();
3056 if (!PrevToken || PrevToken->is(TT_CastRParen))
3057 return TT_UnaryOperator;
3058 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3059 return TT_TrailingUnaryOperator;
3060
3061 return TT_UnaryOperator;
3062 }
3063
3064 SmallVector<Context, 8> Contexts;
3065
3066 const FormatStyle &Style;
3067 AnnotatedLine &Line;
3068 FormatToken *CurrentToken;
3069 bool AutoFound;
3070 bool IsCpp;
3071 LangOptions LangOpts;
3072 const AdditionalKeywords &Keywords;
3073
3074 SmallVector<ScopeType> &Scopes;
3075
3076 // Set of "<" tokens that do not open a template parameter list. If parseAngle
3077 // determines that a specific token can't be a template opener, it will make
3078 // same decision irrespective of the decisions for tokens leading up to it.
3079 // Store this information to prevent this from causing exponential runtime.
3081};
3082
3083static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
3084static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
3085
3086/// Parses binary expressions by inserting fake parenthesis based on
3087/// operator precedence.
3088class ExpressionParser {
3089public:
3090 ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
3091 AnnotatedLine &Line)
3092 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.First) {}
3093
3094 /// Parse expressions with the given operator precedence.
3095 void parse(int Precedence = 0) {
3096 // Skip 'return' and ObjC selector colons as they are not part of a binary
3097 // expression.
3098 while (Current && (Current->is(tok::kw_return) ||
3099 (Current->is(tok::colon) &&
3100 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3101 next();
3102 }
3103
3104 if (!Current || Precedence > PrecedenceArrowAndPeriod)
3105 return;
3106
3107 // Conditional expressions need to be parsed separately for proper nesting.
3108 if (Precedence == prec::Conditional) {
3109 parseConditionalExpr();
3110 return;
3111 }
3112
3113 // Parse unary operators, which all have a higher precedence than binary
3114 // operators.
3115 if (Precedence == PrecedenceUnaryOperator) {
3116 parseUnaryOperator();
3117 return;
3118 }
3119
3120 FormatToken *Start = Current;
3121 FormatToken *LatestOperator = nullptr;
3122 unsigned OperatorIndex = 0;
3123 // The first name of the current type in a port list.
3124 FormatToken *VerilogFirstOfType = nullptr;
3125
3126 while (Current) {
3127 // In Verilog ports in a module header that don't have a type take the
3128 // type of the previous one. For example,
3129 // module a(output b,
3130 // c,
3131 // output d);
3132 // In this case there need to be fake parentheses around b and c.
3133 if (Style.isVerilog() && Precedence == prec::Comma) {
3134 VerilogFirstOfType =
3135 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3136 }
3137
3138 // Consume operators with higher precedence.
3139 parse(Precedence + 1);
3140
3141 int CurrentPrecedence = getCurrentPrecedence();
3142
3143 if (Precedence == CurrentPrecedence && Current &&
3144 Current->is(TT_SelectorName)) {
3145 if (LatestOperator)
3146 addFakeParenthesis(Start, prec::Level(Precedence));
3147 Start = Current;
3148 }
3149
3150 if ((Style.isCSharp() || Style.isJavaScript() ||
3151 Style.Language == FormatStyle::LK_Java) &&
3152 Precedence == prec::Additive && Current) {
3153 // A string can be broken without parentheses around it when it is
3154 // already in a sequence of strings joined by `+` signs.
3155 FormatToken *Prev = Current->getPreviousNonComment();
3156 if (Prev && Prev->is(tok::string_literal) &&
3157 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3158 TT_StringInConcatenation))) {
3159 Prev->setType(TT_StringInConcatenation);
3160 }
3161 }
3162
3163 // At the end of the line or when an operator with lower precedence is
3164 // found, insert fake parenthesis and return.
3165 if (!Current ||
3166 (Current->closesScope() &&
3167 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3168 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3169 (CurrentPrecedence == prec::Conditional &&
3170 Precedence == prec::Assignment && Current->is(tok::colon))) {
3171 break;
3172 }
3173
3174 // Consume scopes: (), [], <> and {}
3175 // In addition to that we handle require clauses as scope, so that the
3176 // constraints in that are correctly indented.
3177 if (Current->opensScope() ||
3178 Current->isOneOf(TT_RequiresClause,
3179 TT_RequiresClauseInARequiresExpression)) {
3180 // In fragment of a JavaScript template string can look like '}..${' and
3181 // thus close a scope and open a new one at the same time.
3182 while (Current && (!Current->closesScope() || Current->opensScope())) {
3183 next();
3184 parse();
3185 }
3186 next();
3187 } else {
3188 // Operator found.
3189 if (CurrentPrecedence == Precedence) {
3190 if (LatestOperator)
3191 LatestOperator->NextOperator = Current;
3192 LatestOperator = Current;
3193 Current->OperatorIndex = OperatorIndex;
3194 ++OperatorIndex;
3195 }
3196 next(/*SkipPastLeadingComments=*/Precedence > 0);
3197 }
3198 }
3199
3200 // Group variables of the same type.
3201 if (Style.isVerilog() && Precedence == prec::Comma && VerilogFirstOfType)
3202 addFakeParenthesis(VerilogFirstOfType, prec::Comma);
3203
3204 if (LatestOperator && (Current || Precedence > 0)) {
3205 // The requires clauses do not neccessarily end in a semicolon or a brace,
3206 // but just go over to struct/class or a function declaration, we need to
3207 // intervene so that the fake right paren is inserted correctly.
3208 auto End =
3209 (Start->Previous &&
3210 Start->Previous->isOneOf(TT_RequiresClause,
3211 TT_RequiresClauseInARequiresExpression))
3212 ? [this]() {
3213 auto Ret = Current ? Current : Line.Last;
3214 while (!Ret->ClosesRequiresClause && Ret->Previous)
3215 Ret = Ret->Previous;
3216 return Ret;
3217 }()
3218 : nullptr;
3219
3220 if (Precedence == PrecedenceArrowAndPeriod) {
3221 // Call expressions don't have a binary operator precedence.
3222 addFakeParenthesis(Start, prec::Unknown, End);
3223 } else {
3224 addFakeParenthesis(Start, prec::Level(Precedence), End);
3225 }
3226 }
3227 }
3228
3229private:
3230 /// Gets the precedence (+1) of the given token for binary operators
3231 /// and other tokens that we treat like binary operators.
3232 int getCurrentPrecedence() {
3233 if (Current) {
3234 const FormatToken *NextNonComment = Current->getNextNonComment();
3235 if (Current->is(TT_ConditionalExpr))
3236 return prec::Conditional;
3237 if (NextNonComment && Current->is(TT_SelectorName) &&
3238 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3239 (Style.isProto() && NextNonComment->is(tok::less)))) {
3240 return prec::Assignment;
3241 }
3242 if (Current->is(TT_JsComputedPropertyName))
3243 return prec::Assignment;
3244 if (Current->is(TT_TrailingReturnArrow))
3245 return prec::Comma;
3246 if (Current->is(TT_FatArrow))
3247 return prec::Assignment;
3248 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3249 (Current->is(tok::comment) && NextNonComment &&
3250 NextNonComment->is(TT_SelectorName))) {
3251 return 0;
3252 }
3253 if (Current->is(TT_RangeBasedForLoopColon))
3254 return prec::Comma;
3255 if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
3256 Current->is(Keywords.kw_instanceof)) {
3257 return prec::Relational;
3258 }
3259 if (Style.isJavaScript() &&
3260 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3261 return prec::Relational;
3262 }
3263 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
3264 return Current->getPrecedence();
3265 if (Current->isOneOf(tok::period, tok::arrow) &&
3266 Current->isNot(TT_TrailingReturnArrow)) {
3267 return PrecedenceArrowAndPeriod;
3268 }
3269 if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
3270 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3271 Keywords.kw_throws)) {
3272 return 0;
3273 }
3274 // In Verilog case labels are not on separate lines straight out of
3275 // UnwrappedLineParser. The colon is not part of an expression.
3276 if (Style.isVerilog() && Current->is(tok::colon))
3277 return 0;
3278 }
3279 return -1;
3280 }
3281
3282 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence,
3283 FormatToken *End = nullptr) {
3284 // Do not assign fake parenthesis to tokens that are part of an
3285 // unexpanded macro call. The line within the macro call contains
3286 // the parenthesis and commas, and we will not find operators within
3287 // that structure.
3288 if (Start->MacroParent)
3289 return;
3290
3291 Start->FakeLParens.push_back(Precedence);
3292 if (Precedence > prec::Unknown)
3293 Start->StartsBinaryExpression = true;
3294 if (!End && Current)
3295 End = Current->getPreviousNonComment();
3296 if (End) {
3297 ++End->FakeRParens;
3298 if (Precedence > prec::Unknown)
3299 End->EndsBinaryExpression = true;
3300 }
3301 }
3302
3303 /// Parse unary operator expressions and surround them with fake
3304 /// parentheses if appropriate.
3305 void parseUnaryOperator() {
3307 while (Current && Current->is(TT_UnaryOperator)) {
3308 Tokens.push_back(Current);
3309 next();
3310 }
3311 parse(PrecedenceArrowAndPeriod);
3312 for (FormatToken *Token : llvm::reverse(Tokens)) {
3313 // The actual precedence doesn't matter.
3314 addFakeParenthesis(Token, prec::Unknown);
3315 }
3316 }
3317
3318 void parseConditionalExpr() {
3319 while (Current && Current->isTrailingComment())
3320 next();
3321 FormatToken *Start = Current;
3322 parse(prec::LogicalOr);
3323 if (!Current || Current->isNot(tok::question))
3324 return;
3325 next();
3326 parse(prec::Assignment);
3327 if (!Current || Current->isNot(TT_ConditionalExpr))
3328 return;
3329 next();
3330 parse(prec::Assignment);
3331 addFakeParenthesis(Start, prec::Conditional);
3332 }
3333
3334 void next(bool SkipPastLeadingComments = true) {
3335 if (Current)
3336 Current = Current->Next;
3337 while (Current &&
3338 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3339 Current->isTrailingComment()) {
3340 Current = Current->Next;
3341 }
3342 }
3343
3344 // Add fake parenthesis around declarations of the same type for example in a
3345 // module prototype. Return the first port / variable of the current type.
3346 FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
3347 FormatToken *PreviousComma) {
3348 if (!Current)
3349 return nullptr;
3350
3351 FormatToken *Start = Current;
3352
3353 // Skip attributes.
3354 while (Start->startsSequence(tok::l_paren, tok::star)) {
3355 if (!(Start = Start->MatchingParen) ||
3356 !(Start = Start->getNextNonComment())) {
3357 return nullptr;
3358 }
3359 }
3360
3361 FormatToken *Tok = Start;
3362
3363 if (Tok->is(Keywords.kw_assign))
3364 Tok = Tok->getNextNonComment();
3365
3366 // Skip any type qualifiers to find the first identifier. It may be either a
3367 // new type name or a variable name. There can be several type qualifiers
3368 // preceding a variable name, and we can not tell them apart by looking at
3369 // the word alone since a macro can be defined as either a type qualifier or
3370 // a variable name. Thus we use the last word before the dimensions instead
3371 // of the first word as the candidate for the variable or type name.
3372 FormatToken *First = nullptr;
3373 while (Tok) {
3374 FormatToken *Next = Tok->getNextNonComment();
3375
3376 if (Tok->is(tok::hash)) {
3377 // Start of a macro expansion.
3378 First = Tok;
3379 Tok = Next;
3380 if (Tok)
3381 Tok = Tok->getNextNonComment();
3382 } else if (Tok->is(tok::hashhash)) {
3383 // Concatenation. Skip.
3384 Tok = Next;
3385 if (Tok)
3386 Tok = Tok->getNextNonComment();
3387 } else if (Keywords.isVerilogQualifier(*Tok) ||
3388 Keywords.isVerilogIdentifier(*Tok)) {
3389 First = Tok;
3390 Tok = Next;
3391 // The name may have dots like `interface_foo.modport_foo`.
3392 while (Tok && Tok->isOneOf(tok::period, tok::coloncolon) &&
3393 (Tok = Tok->getNextNonComment())) {
3394 if (Keywords.isVerilogIdentifier(*Tok))
3395 Tok = Tok->getNextNonComment();
3396 }
3397 } else if (!Next) {
3398 Tok = nullptr;
3399 } else if (Tok->is(tok::l_paren)) {
3400 // Make sure the parenthesized list is a drive strength. Otherwise the
3401 // statement may be a module instantiation in which case we have already
3402 // found the instance name.
3403 if (Next->isOneOf(
3404 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3405 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3406 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3407 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3408 Keywords.kw_weak1)) {
3409 Tok->setType(TT_VerilogStrength);
3410 Tok = Tok->MatchingParen;
3411 if (Tok) {
3412 Tok->setType(TT_VerilogStrength);
3413 Tok = Tok->getNextNonComment();
3414 }
3415 } else {
3416 break;
3417 }
3418 } else if (Tok->is(tok::hash)) {
3419 if (Next->is(tok::l_paren))
3420 Next = Next->MatchingParen;
3421 if (Next)
3422 Tok = Next->getNextNonComment();
3423 } else {
3424 break;
3425 }
3426 }
3427
3428 // Find the second identifier. If it exists it will be the name.
3429 FormatToken *Second = nullptr;
3430 // Dimensions.
3431 while (Tok && Tok->is(tok::l_square) && (Tok = Tok->MatchingParen))
3432 Tok = Tok->getNextNonComment();
3433 if (Tok && (Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*Tok)))
3434 Second = Tok;
3435
3436 // If the second identifier doesn't exist and there are qualifiers, the type
3437 // is implied.
3438 FormatToken *TypedName = nullptr;
3439 if (Second) {
3440 TypedName = Second;
3441 if (First && First->is(TT_Unknown))
3442 First->setType(TT_VerilogDimensionedTypeName);
3443 } else if (First != Start) {
3444 // If 'First' is null, then this isn't a declaration, 'TypedName' gets set
3445 // to null as intended.
3446 TypedName = First;
3447 }
3448
3449 if (TypedName) {
3450 // This is a declaration with a new type.
3451 if (TypedName->is(TT_Unknown))
3452 TypedName->setType(TT_StartOfName);
3453 // Group variables of the previous type.
3454 if (FirstOfType && PreviousComma) {
3455 PreviousComma->setType(TT_VerilogTypeComma);
3456 addFakeParenthesis(FirstOfType, prec::Comma, PreviousComma->Previous);
3457 }
3458
3459 FirstOfType = TypedName;
3460
3461 // Don't let higher precedence handle the qualifiers. For example if we
3462 // have:
3463 // parameter x = 0
3464 // We skip `parameter` here. This way the fake parentheses for the
3465 // assignment will be around `x = 0`.
3466 while (Current && Current != FirstOfType) {
3467 if (Current->opensScope()) {
3468 next();
3469 parse();
3470 }
3471 next();
3472 }
3473 }
3474
3475 return FirstOfType;
3476 }
3477
3478 const FormatStyle &Style;
3479 const AdditionalKeywords &Keywords;
3480 const AnnotatedLine &Line;
3481 FormatToken *Current;
3482};
3483
3484} // end anonymous namespace
3485
3487 SmallVectorImpl<AnnotatedLine *> &Lines) const {
3488 const AnnotatedLine *NextNonCommentLine = nullptr;
3489 for (AnnotatedLine *Line : llvm::reverse(Lines)) {
3490 assert(Line->First);
3491
3492 // If the comment is currently aligned with the line immediately following
3493 // it, that's probably intentional and we should keep it.
3494 if (NextNonCommentLine && NextNonCommentLine->First->NewlinesBefore < 2 &&
3495 Line->isComment() && !isClangFormatOff(Line->First->TokenText) &&
3496 NextNonCommentLine->First->OriginalColumn ==
3497 Line->First->OriginalColumn) {
3498 const bool PPDirectiveOrImportStmt =
3499 NextNonCommentLine->Type == LT_PreprocessorDirective ||
3500 NextNonCommentLine->Type == LT_ImportStatement;
3501 if (PPDirectiveOrImportStmt)
3503 // Align comments for preprocessor lines with the # in column 0 if
3504 // preprocessor lines are not indented. Otherwise, align with the next
3505 // line.
3506 Line->Level = Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
3507 PPDirectiveOrImportStmt
3508 ? 0
3509 : NextNonCommentLine->Level;
3510 } else {
3511 NextNonCommentLine = Line->First->isNot(tok::r_brace) ? Line : nullptr;
3512 }
3513
3514 setCommentLineLevels(Line->Children);
3515 }
3516}
3517
3518static unsigned maxNestingDepth(const AnnotatedLine &Line) {
3519 unsigned Result = 0;
3520 for (const auto *Tok = Line.First; Tok; Tok = Tok->Next)
3521 Result = std::max(Result, Tok->NestingLevel);
3522 return Result;
3523}
3524
3525// Returns the name of a function with no return type, e.g. a constructor or
3526// destructor.
3528 for (FormatToken *Tok = Line.getFirstNonComment(), *Name = nullptr; Tok;
3529 Tok = Tok->getNextNonComment()) {
3530 // Skip C++11 attributes both before and after the function name.
3531 if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3532 Tok = Tok->MatchingParen;
3533 if (!Tok)
3534 break;
3535 continue;
3536 }
3537
3538 // Make sure the name is followed by a pair of parentheses.
3539 if (Name) {
3540 return Tok->is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) &&
3541 Tok->MatchingParen
3542 ? Name
3543 : nullptr;
3544 }
3545
3546 // Skip keywords that may precede the constructor/destructor name.
3547 if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3548 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3549 continue;
3550 }
3551
3552 // A qualified name may start from the global namespace.
3553 if (Tok->is(tok::coloncolon)) {
3554 Tok = Tok->Next;
3555 if (!Tok)
3556 break;
3557 }
3558
3559 // Skip to the unqualified part of the name.
3560 while (Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3561 assert(Tok->Next);
3562 Tok = Tok->Next->Next;
3563 if (!Tok)
3564 return nullptr;
3565 }
3566
3567 // Skip the `~` if a destructor name.
3568 if (Tok->is(tok::tilde)) {
3569 Tok = Tok->Next;
3570 if (!Tok)
3571 break;
3572 }
3573
3574 // Make sure the name is not already annotated, e.g. as NamespaceMacro.
3575 if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
3576 break;
3577
3578 Name = Tok;
3579 }
3580
3581 return nullptr;
3582}
3583
3584// Checks if Tok is a constructor/destructor name qualified by its class name.
3585static bool isCtorOrDtorName(const FormatToken *Tok) {
3586 assert(Tok && Tok->is(tok::identifier));
3587 const auto *Prev = Tok->Previous;
3588
3589 if (Prev && Prev->is(tok::tilde))
3590 Prev = Prev->Previous;
3591
3592 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3593 return false;
3594
3595 assert(Prev->Previous);
3596 return Prev->Previous->TokenText == Tok->TokenText;
3597}
3598
3600 AnnotatingParser Parser(Style, Line, Keywords, Scopes);
3601 Line.Type = Parser.parseLine();
3602
3603 for (auto &Child : Line.Children)
3604 annotate(*Child);
3605
3606 // With very deep nesting, ExpressionParser uses lots of stack and the
3607 // formatting algorithm is very slow. We're not going to do a good job here
3608 // anyway - it's probably generated code being formatted by mistake.
3609 // Just skip the whole line.
3610 if (maxNestingDepth(Line) > 50)
3611 Line.Type = LT_Invalid;
3612
3613 if (Line.Type == LT_Invalid)
3614 return;
3615
3616 ExpressionParser ExprParser(Style, Keywords, Line);
3617 ExprParser.parse();
3618
3619 if (IsCpp) {
3620 auto *Tok = getFunctionName(Line);
3621 if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
3622 Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
3623 Tok->setFinalizedType(TT_CtorDtorDeclName);
3624 }
3625 }
3626
3627 if (Line.startsWith(TT_ObjCMethodSpecifier))
3628 Line.Type = LT_ObjCMethodDecl;
3629 else if (Line.startsWith(TT_ObjCDecl))
3630 Line.Type = LT_ObjCDecl;
3631 else if (Line.startsWith(TT_ObjCProperty))
3632 Line.Type = LT_ObjCProperty;
3633
3634 auto *First = Line.First;
3635 First->SpacesRequiredBefore = 1;
3636 First->CanBreakBefore = First->MustBreakBefore;
3637
3638 if (First->is(tok::eof) && First->NewlinesBefore == 0 &&
3639 Style.InsertNewlineAtEOF) {
3640 First->NewlinesBefore = 1;
3641 }
3642}
3643
3644// This function heuristically determines whether 'Current' starts the name of a
3645// function declaration.
3646static bool isFunctionDeclarationName(const LangOptions &LangOpts,
3647 const FormatToken &Current,
3648 const AnnotatedLine &Line,
3649 FormatToken *&ClosingParen) {
3650 assert(Current.Previous);
3651
3652 if (Current.is(TT_FunctionDeclarationName))
3653 return true;
3654
3655 if (!Current.Tok.getIdentifierInfo())
3656 return false;
3657
3658 const auto &Previous = *Current.Previous;
3659
3660 if (const auto *PrevPrev = Previous.Previous;
3661 PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3662 return false;
3663 }
3664
3665 auto skipOperatorName =
3666 [&LangOpts](const FormatToken *Next) -> const FormatToken * {
3667 for (; Next; Next = Next->Next) {
3668 if (Next->is(TT_OverloadedOperatorLParen))
3669 return Next;
3670 if (Next->is(TT_OverloadedOperator))
3671 continue;
3672 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
3673 // For 'new[]' and 'delete[]'.
3674 if (Next->Next &&
3675 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3676 Next = Next->Next->Next;
3677 }
3678 continue;
3679 }
3680 if (Next->startsSequence(tok::l_square, tok::r_square)) {
3681 // For operator[]().
3682 Next = Next->Next;
3683 continue;
3684 }
3685 if ((Next->isTypeName(LangOpts) || Next->is(tok::identifier)) &&
3686 Next->Next && Next->Next->isPointerOrReference()) {
3687 // For operator void*(), operator char*(), operator Foo*().
3688 Next = Next->Next;
3689 continue;
3690 }
3691 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3692 Next = Next->MatchingParen;
3693 continue;
3694 }
3695
3696 break;
3697 }
3698 return nullptr;
3699 };
3700
3701 const auto *Next = Current.Next;
3702 const bool IsCpp = LangOpts.CXXOperatorNames;
3703
3704 // Find parentheses of parameter list.
3705 if (Current.is(tok::kw_operator)) {
3706 if (Previous.Tok.getIdentifierInfo() &&
3707 !Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
3708 return true;
3709 }
3710 if (Previous.is(tok::r_paren) && Previous.is(TT_TypeDeclarationParen)) {
3711 assert(Previous.MatchingParen);
3712 assert(Previous.MatchingParen->is(tok::l_paren));
3713 assert(Previous.MatchingParen->is(TT_TypeDeclarationParen));
3714 return true;
3715 }
3716 if (!Previous.isPointerOrReference() && Previous.isNot(TT_TemplateCloser))
3717 return false;
3718 Next = skipOperatorName(Next);
3719 } else {
3720 if (Current.isNot(TT_StartOfName) || Current.NestingLevel != 0)
3721 return false;
3722 for (; Next; Next = Next->Next) {
3723 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3724 Next = Next->MatchingParen;
3725 } else if (Next->is(tok::coloncolon)) {
3726 Next = Next->Next;
3727 if (!Next)
3728 return false;
3729 if (Next->is(tok::kw_operator)) {
3730 Next = skipOperatorName(Next->Next);
3731 break;
3732 }
3733 if (Next->isNot(tok::identifier))
3734 return false;
3735 } else if (isCppAttribute(IsCpp, *Next)) {
3736 Next = Next->MatchingParen;
3737 if (!Next)
3738 return false;
3739 } else if (Next->is(tok::l_paren)) {
3740 break;
3741 } else {
3742 return false;
3743 }
3744 }
3745 }
3746
3747 // Check whether parameter list can belong to a function declaration.
3748 if (!Next || Next->isNot(tok::l_paren) || !Next->MatchingParen)
3749 return false;
3750 ClosingParen = Next->MatchingParen;
3751 assert(ClosingParen->is(tok::r_paren));
3752 // If the lines ends with "{", this is likely a function definition.
3753 if (Line.Last->is(tok::l_brace))
3754 return true;
3755 if (Next->Next == ClosingParen)
3756 return true; // Empty parentheses.
3757 // If there is an &/&& after the r_paren, this is likely a function.
3758 if (ClosingParen->Next && ClosingParen->Next->is(TT_PointerOrReference))
3759 return true;
3760
3761 // Check for K&R C function definitions (and C++ function definitions with
3762 // unnamed parameters), e.g.:
3763 // int f(i)
3764 // {
3765 // return i + 1;
3766 // }
3767 // bool g(size_t = 0, bool b = false)
3768 // {
3769 // return !b;
3770 // }
3771 if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
3772 !Line.endsWith(tok::semi)) {
3773 return true;
3774 }
3775
3776 for (const FormatToken *Tok = Next->Next; Tok && Tok != ClosingParen;
3777 Tok = Tok->Next) {
3778 if (Tok->is(TT_TypeDeclarationParen))
3779 return true;
3780 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
3781 Tok = Tok->MatchingParen;
3782 continue;
3783 }
3784 if (Tok->is(tok::kw_const) || Tok->isTypeName(LangOpts) ||
3785 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3786 return true;
3787 }
3788 if (Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) || Tok->Tok.isLiteral())
3789 return false;
3790 }
3791 return false;
3792}
3793
3794bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
3795 assert(Line.MightBeFunctionDecl);
3796
3797 if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
3798 Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
3799 Line.Level > 0) {
3800 return false;
3801 }
3802
3803 switch (Style.BreakAfterReturnType) {
3807 return false;
3810 return true;
3813 return Line.mightBeFunctionDefinition();
3814 }
3815
3816 return false;
3817}
3818
3820 for (AnnotatedLine *ChildLine : Line.Children)
3822
3823 auto *First = Line.First;
3824 First->TotalLength = First->IsMultiline
3825 ? Style.ColumnLimit
3826 : Line.FirstStartColumn + First->ColumnWidth;
3827 FormatToken *Current = First->Next;
3828 bool InFunctionDecl = Line.MightBeFunctionDecl;
3829 bool AlignArrayOfStructures =
3830 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
3832 if (AlignArrayOfStructures)
3833 calculateArrayInitializerColumnList(Line);
3834
3835 bool SeenName = false;
3836 bool LineIsFunctionDeclaration = false;
3837 FormatToken *ClosingParen = nullptr;
3838 FormatToken *AfterLastAttribute = nullptr;
3839
3840 for (auto *Tok = Current; Tok; Tok = Tok->Next) {
3841 if (Tok->is(TT_StartOfName))
3842 SeenName = true;
3843 if (Tok->Previous->EndsCppAttributeGroup)
3844 AfterLastAttribute = Tok;
3845 if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3846 IsCtorOrDtor ||
3847 isFunctionDeclarationName(LangOpts, *Tok, Line, ClosingParen)) {
3848 if (!IsCtorOrDtor)
3849 Tok->setFinalizedType(TT_FunctionDeclarationName);
3850 LineIsFunctionDeclaration = true;
3851 SeenName = true;
3852 break;
3853 }
3854 }
3855
3856 if (IsCpp && (LineIsFunctionDeclaration || First->is(TT_CtorDtorDeclName)) &&
3857 Line.endsWith(tok::semi, tok::r_brace)) {
3858 auto *Tok = Line.Last->Previous;
3859 while (Tok->isNot(tok::r_brace))
3860 Tok = Tok->Previous;
3861 if (auto *LBrace = Tok->MatchingParen; LBrace) {
3862 assert(LBrace->is(tok::l_brace));
3863 Tok->setBlockKind(BK_Block);
3864 LBrace->setBlockKind(BK_Block);
3865 LBrace->setFinalizedType(TT_FunctionLBrace);
3866 }
3867 }
3868
3869 if (IsCpp && SeenName && AfterLastAttribute &&
3870 mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
3871 AfterLastAttribute->MustBreakBefore = true;
3872 if (LineIsFunctionDeclaration)
3873 Line.ReturnTypeWrapped = true;
3874 }
3875
3876 if (IsCpp) {
3877 if (!LineIsFunctionDeclaration) {
3878 // Annotate */&/&& in `operator` function calls as binary operators.
3879 for (const auto *Tok = First; Tok; Tok = Tok->Next) {
3880 if (Tok->isNot(tok::kw_operator))
3881 continue;
3882 do {
3883 Tok = Tok->Next;
3884 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
3885 if (!Tok || !Tok->MatchingParen)
3886 break;
3887 const auto *LeftParen = Tok;
3888 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
3889 Tok = Tok->Next) {
3890 if (Tok->isNot(tok::identifier))
3891 continue;
3892 auto *Next = Tok->Next;
3893 const bool NextIsBinaryOperator =
3894 Next && Next->isPointerOrReference() && Next->Next &&
3895 Next->Next->is(tok::identifier);
3896 if (!NextIsBinaryOperator)
3897 continue;
3898 Next->setType(TT_BinaryOperator);
3899 Tok = Next;
3900 }
3901 }
3902 } else if (ClosingParen) {
3903 for (auto *Tok = ClosingParen->Next; Tok; Tok = Tok->Next) {
3904 if (Tok->is(TT_CtorInitializerColon))
3905 break;
3906 if (Tok->is(tok::arrow)) {
3907 Tok->setType(TT_TrailingReturnArrow);
3908 break;
3909 }
3910 if (Tok->isNot(TT_TrailingAnnotation))
3911 continue;
3912 const auto *Next = Tok->Next;
3913 if (!Next || Next->isNot(tok::l_paren))
3914 continue;
3915 Tok = Next->MatchingParen;
3916 if (!Tok)
3917 break;
3918 }
3919 }
3920 }
3921
3922 while (Current) {
3923 const FormatToken *Prev = Current->Previous;
3924 if (Current->is(TT_LineComment)) {
3925 if (Prev->is(BK_BracedInit) && Prev->opensScope()) {
3926 Current->SpacesRequiredBefore =
3927 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
3928 ? 0
3929 : 1;
3930 } else if (Prev->is(TT_VerilogMultiLineListLParen)) {
3931 Current->SpacesRequiredBefore = 0;
3932 } else {
3933 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
3934 }
3935
3936 // If we find a trailing comment, iterate backwards to determine whether
3937 // it seems to relate to a specific parameter. If so, break before that
3938 // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
3939 // to the previous line in:
3940 // SomeFunction(a,
3941 // b, // comment
3942 // c);
3943 if (!Current->HasUnescapedNewline) {
3944 for (FormatToken *Parameter = Current->Previous; Parameter;
3945 Parameter = Parameter->Previous) {
3946 if (Parameter->isOneOf(tok::comment, tok::r_brace))
3947 break;
3948 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
3949 if (Parameter->Previous->isNot(TT_CtorInitializerComma) &&
3950 Parameter->HasUnescapedNewline) {
3951 Parameter->MustBreakBefore = true;
3952 }
3953 break;
3954 }
3955 }
3956 }
3957 } else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
3958 spaceRequiredBefore(Line, *Current)) {
3959 Current->SpacesRequiredBefore = 1;
3960 }
3961
3962 const auto &Children = Prev->Children;
3963 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
3964 Current->MustBreakBefore = true;
3965 } else {
3966 Current->MustBreakBefore =
3967 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
3968 if (!Current->MustBreakBefore && InFunctionDecl &&
3969 Current->is(TT_FunctionDeclarationName)) {
3970 Current->MustBreakBefore = mustBreakForReturnType(Line);
3971 }
3972 }
3973
3974 Current->CanBreakBefore =
3975 Current->MustBreakBefore || canBreakBefore(Line, *Current);
3976 unsigned ChildSize = 0;
3977 if (Prev->Children.size() == 1) {
3978 FormatToken &LastOfChild = *Prev->Children[0]->Last;
3979 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
3980 : LastOfChild.TotalLength + 1;
3981 }
3982 if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
3983 (Prev->Children.size() == 1 &&
3984 Prev->Children[0]->First->MustBreakBefore) ||
3985 Current->IsMultiline) {
3986 Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
3987 } else {
3988 Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
3989 ChildSize + Current->SpacesRequiredBefore;
3990 }
3991
3992 if (Current->is(TT_CtorInitializerColon))
3993 InFunctionDecl = false;
3994
3995 // FIXME: Only calculate this if CanBreakBefore is true once static
3996 // initializers etc. are sorted out.
3997 // FIXME: Move magic numbers to a better place.
3998
3999 // Reduce penalty for aligning ObjC method arguments using the colon
4000 // alignment as this is the canonical way (still prefer fitting everything
4001 // into one line if possible). Trying to fit a whole expression into one
4002 // line should not force other line breaks (e.g. when ObjC method
4003 // expression is a part of other expression).
4004 Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
4005 if (Style.Language == FormatStyle::LK_ObjC &&
4006 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4007 if (Current->ParameterIndex == 1)
4008 Current->SplitPenalty += 5 * Current->BindingStrength;
4009 } else {
4010 Current->SplitPenalty += 20 * Current->BindingStrength;
4011 }
4012
4013 Current = Current->Next;
4014 }
4015
4016 calculateUnbreakableTailLengths(Line);
4017 unsigned IndentLevel = Line.Level;
4018 for (Current = First; Current; Current = Current->Next) {
4019 if (Current->Role)
4020 Current->Role->precomputeFormattingInfos(Current);
4021 if (Current->MatchingParen &&
4022 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4023 IndentLevel > 0) {
4024 --IndentLevel;
4025 }
4026 Current->IndentLevel = IndentLevel;
4027 if (Current->opensBlockOrBlockTypeList(Style))
4028 ++IndentLevel;
4029 }
4030
4031 LLVM_DEBUG({ printDebugInfo(Line); });
4032}
4033
4034void TokenAnnotator::calculateUnbreakableTailLengths(
4035 AnnotatedLine &Line) const {
4036 unsigned UnbreakableTailLength = 0;
4037 FormatToken *Current = Line.Last;
4038 while (Current) {
4039 Current->UnbreakableTailLength = UnbreakableTailLength;
4040 if (Current->CanBreakBefore ||
4041 Current->isOneOf(tok::comment, tok::string_literal)) {
4042 UnbreakableTailLength = 0;
4043 } else {
4044 UnbreakableTailLength +=
4045 Current->ColumnWidth + Current->SpacesRequiredBefore;
4046 }
4047 Current = Current->Previous;
4048 }
4049}
4050
4051void TokenAnnotator::calculateArrayInitializerColumnList(
4052 AnnotatedLine &Line) const {
4053 if (Line.First == Line.Last)
4054 return;
4055 auto *CurrentToken = Line.First;
4056 CurrentToken->ArrayInitializerLineStart = true;
4057 unsigned Depth = 0;
4058 while (CurrentToken && CurrentToken != Line.Last) {
4059 if (CurrentToken->is(tok::l_brace)) {
4060 CurrentToken->IsArrayInitializer = true;
4061 if (CurrentToken->Next)
4062 CurrentToken->Next->MustBreakBefore = true;
4063 CurrentToken =
4064 calculateInitializerColumnList(Line, CurrentToken->Next, Depth + 1);
4065 } else {
4066 CurrentToken = CurrentToken->Next;
4067 }
4068 }
4069}
4070
4071FormatToken *TokenAnnotator::calculateInitializerColumnList(
4072 AnnotatedLine &Line, FormatToken *CurrentToken, unsigned Depth) const {
4073 while (CurrentToken && CurrentToken != Line.Last) {
4074 if (CurrentToken->is(tok::l_brace))
4075 ++Depth;
4076 else if (CurrentToken->is(tok::r_brace))
4077 --Depth;
4078 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4079 CurrentToken = CurrentToken->Next;
4080 if (!CurrentToken)
4081 break;
4082 CurrentToken->StartsColumn = true;
4083 CurrentToken = CurrentToken->Previous;
4084 }
4085 CurrentToken = CurrentToken->Next;
4086 }
4087 return CurrentToken;
4088}
4089
4090unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
4091 const FormatToken &Tok,
4092 bool InFunctionDecl) const {
4093 const FormatToken &Left = *Tok.Previous;
4094 const FormatToken &Right = Tok;
4095
4096 if (Left.is(tok::semi))
4097 return 0;
4098
4099 // Language specific handling.
4100 if (Style.Language == FormatStyle::LK_Java) {
4101 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4102 return 1;
4103 if (Right.is(Keywords.kw_implements))
4104 return 2;
4105 if (Left.is(tok::comma) && Left.NestingLevel == 0)
4106 return 3;
4107 } else if (Style.isJavaScript()) {
4108 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
4109 return 100;
4110 if (Left.is(TT_JsTypeColon))
4111 return 35;
4112 if ((Left.is(TT_TemplateString) && Left.TokenText.ends_with("${")) ||
4113 (Right.is(TT_TemplateString) && Right.TokenText.starts_with("}"))) {
4114 return 100;
4115 }
4116 // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
4117 if (Left.opensScope() && Right.closesScope())
4118 return 200;
4119 } else if (Style.Language == FormatStyle::LK_Proto) {
4120 if (Right.is(tok::l_square))
4121 return 1;
4122 if (Right.is(tok::period))
4123 return 500;
4124 }
4125
4126 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
4127 return 1;
4128 if (Right.is(tok::l_square)) {
4129 if (Left.is(tok::r_square))
4130 return 200;
4131 // Slightly prefer formatting local lambda definitions like functions.
4132 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
4133 return 35;
4134 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4135 TT_ArrayInitializerLSquare,
4136 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
4137 return 500;
4138 }
4139 }
4140
4141 if (Left.is(tok::coloncolon))
4142 return Style.PenaltyBreakScopeResolution;
4143 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
4144 Right.is(tok::kw_operator)) {
4145 if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
4146 return 3;
4147 if (Left.is(TT_StartOfName))
4148 return 110;
4149 if (InFunctionDecl && Right.NestingLevel == 0)
4150 return Style.PenaltyReturnTypeOnItsOwnLine;
4151 return 200;
4152 }
4153 if (Right.is(TT_PointerOrReference))
4154 return 190;
4155 if (Right.is(TT_TrailingReturnArrow))
4156 return 110;
4157 if (Left.is(tok::equal) && Right.is(tok::l_brace))
4158 return 160;
4159 if (Left.is(TT_CastRParen))
4160 return 100;
4161 if (Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4162 return 5000;
4163 if (Left.is(tok::comment))
4164 return 1000;
4165
4166 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4167 TT_CtorInitializerColon)) {
4168 return 2;
4169 }
4170
4171 if (Right.isMemberAccess()) {
4172 // Breaking before the "./->" of a chained call/member access is reasonably
4173 // cheap, as formatting those with one call per line is generally
4174 // desirable. In particular, it should be cheaper to break before the call
4175 // than it is to break inside a call's parameters, which could lead to weird
4176 // "hanging" indents. The exception is the very last "./->" to support this
4177 // frequent pattern:
4178 //
4179 // aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
4180 // dddddddd);
4181 //
4182 // which might otherwise be blown up onto many lines. Here, clang-format
4183 // won't produce "hanging" indents anyway as there is no other trailing
4184 // call.
4185 //
4186 // Also apply higher penalty is not a call as that might lead to a wrapping
4187 // like:
4188 //
4189 // aaaaaaa
4190 // .aaaaaaaaa.bbbbbbbb(cccccccc);
4191 return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
4192 ? 150
4193 : 35;
4194 }
4195
4196 if (Right.is(TT_TrailingAnnotation) &&
4197 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
4198 // Moving trailing annotations to the next line is fine for ObjC method
4199 // declarations.
4200 if (Line.startsWith(TT_ObjCMethodSpecifier))
4201 return 10;
4202 // Generally, breaking before a trailing annotation is bad unless it is
4203 // function-like. It seems to be especially preferable to keep standard
4204 // annotations (i.e. "const", "final" and "override") on the same line.
4205 // Use a slightly higher penalty after ")" so that annotations like
4206 // "const override" are kept together.
4207 bool is_short_annotation = Right.TokenText.size() < 10;
4208 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4209 }
4210
4211 // In for-loops, prefer breaking at ',' and ';'.
4212 if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
4213 return 4;
4214
4215 // In Objective-C method expressions, prefer breaking before "param:" over
4216 // breaking after it.
4217 if (Right.is(TT_SelectorName))
4218 return 0;
4219 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
4220 return Line.MightBeFunctionDecl ? 50 : 500;
4221
4222 // In Objective-C type declarations, avoid breaking after the category's
4223 // open paren (we'll prefer breaking after the protocol list's opening
4224 // angle bracket, if present).
4225 if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous &&
4226 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4227 return 500;
4228 }
4229
4230 if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4231 return Style.PenaltyBreakOpenParenthesis;
4232 if (Left.is(tok::l_paren) && InFunctionDecl &&
4233 Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
4234 return 100;
4235 }
4236 if (Left.is(tok::l_paren) && Left.Previous &&
4237 (Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4238 Left.Previous->isIf())) {
4239 return 1000;
4240 }
4241 if (Left.is(tok::equal) && InFunctionDecl)
4242 return 110;
4243 if (Right.is(tok::r_brace))
4244 return 1;
4245 if (Left.is(TT_TemplateOpener))
4246 return 100;
4247 if (Left.opensScope()) {
4248 // If we aren't aligning after opening parens/braces we can always break
4249 // here unless the style does not want us to place all arguments on the
4250 // next line.
4251 if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
4252 (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4253 return 0;
4254 }
4255 if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
4256 return 19;
4257 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4258 : 19;
4259 }
4260 if (Left.is(TT_JavaAnnotation))
4261 return 50;
4262
4263 if (Left.is(TT_UnaryOperator))
4264 return 60;
4265 if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
4266 Left.Previous->isLabelString() &&
4267 (Left.NextOperator || Left.OperatorIndex != 0)) {
4268 return 50;
4269 }
4270 if (Right.is(tok::plus) && Left.isLabelString() &&
4271 (Right.NextOperator || Right.OperatorIndex != 0)) {
4272 return 25;
4273 }
4274 if (Left.is(tok::comma))
4275 return 1;
4276 if (Right.is(tok::lessless) && Left.isLabelString() &&
4277 (Right.NextOperator || Right.OperatorIndex != 1)) {
4278 return 25;
4279 }
4280 if (Right.is(tok::lessless)) {
4281 // Breaking at a << is really cheap.
4282 if (Left.isNot(tok::r_paren) || Right.OperatorIndex > 0) {
4283 // Slightly prefer to break before the first one in log-like statements.
4284 return 2;
4285 }
4286 return 1;
4287 }
4288 if (Left.ClosesTemplateDeclaration)
4289 return Style.PenaltyBreakTemplateDeclaration;
4290 if (Left.ClosesRequiresClause)
4291 return 0;
4292 if (Left.is(TT_ConditionalExpr))
4293 return prec::Conditional;
4294 prec::Level Level = Left.getPrecedence();
4295 if (Level == prec::Unknown)
4296 Level = Right.getPrecedence();
4297 if (Level == prec::Assignment)
4298 return Style.PenaltyBreakAssignment;
4299 if (Level != prec::Unknown)
4300 return Level;
4301
4302 return 3;
4303}
4304
4305bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
4306 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4307 return true;
4308 if (Right.is(TT_OverloadedOperatorLParen) &&
4309 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4310 return true;
4311 }
4312 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4313 Right.ParameterCount > 0) {
4314 return true;
4315 }
4316 return false;
4317}
4318
4319bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
4320 const FormatToken &Left,
4321 const FormatToken &Right) const {
4322 if (Left.is(tok::kw_return) &&
4323 !Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4324 return true;
4325 }
4326 if (Left.is(tok::kw_throw) && Right.is(tok::l_paren) && Right.MatchingParen &&
4327 Right.MatchingParen->is(TT_CastRParen)) {
4328 return true;
4329 }
4330 if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java)
4331 return true;
4332 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
4333 Left.Tok.getObjCKeywordID() == tok::objc_property) {
4334 return true;
4335 }
4336 if (Right.is(tok::hashhash))
4337 return Left.is(tok::hash);
4338 if (Left.isOneOf(tok::hashhash, tok::hash))
4339 return Right.is(tok::hash);
4340 if (Left.is(BK_Block) && Right.is(tok::r_brace) &&
4341 Right.MatchingParen == &Left && Line.Children.empty()) {
4342 return Style.SpaceInEmptyBlock;
4343 }
4344 if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
4345 (Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
4346 Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
4347 return Style.SpacesInParensOptions.InEmptyParentheses;
4348 }
4349 if (Style.SpacesInParensOptions.InConditionalStatements) {
4350 const FormatToken *LeftParen = nullptr;
4351 if (Left.is(tok::l_paren))
4352 LeftParen = &Left;
4353 else if (Right.is(tok::r_paren) && Right.MatchingParen)
4354 LeftParen = Right.MatchingParen;
4355 if (LeftParen) {
4356 if (LeftParen->is(TT_ConditionLParen))
4357 return true;
4358 if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous))
4359 return true;
4360 }
4361 }
4362
4363 // trailing return type 'auto': []() -> auto {}, auto foo() -> auto {}
4364 if (Left.is(tok::kw_auto) && Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4365 // function return type 'auto'
4366 TT_FunctionTypeLParen)) {
4367 return true;
4368 }
4369
4370 // auto{x} auto(x)
4371 if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace))
4372 return false;
4373
4374 const auto *BeforeLeft = Left.Previous;
4375
4376 // operator co_await(x)
4377 if (Right.is(tok::l_paren) && Left.is(tok::kw_co_await) && BeforeLeft &&
4378 BeforeLeft->is(tok::kw_operator)) {
4379 return false;
4380 }
4381 // co_await (x), co_yield (x), co_return (x)
4382 if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4383 !Right.isOneOf(tok::semi, tok::r_paren)) {
4384 return true;
4385 }
4386
4387 if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
4388 return (Right.is(TT_CastRParen) ||
4389 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
4390 ? Style.SpacesInParensOptions.InCStyleCasts
4391 : Style.SpacesInParensOptions.Other;
4392 }
4393 if (Right.isOneOf(tok::semi, tok::comma))
4394 return false;
4395 if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) {
4396 bool IsLightweightGeneric = Right.MatchingParen &&
4397 Right.MatchingParen->Next &&
4398 Right.MatchingParen->Next->is(tok::colon);
4399 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4400 }
4401 if (Right.is(tok::less) && Left.is(tok::kw_template))
4402 return Style.SpaceAfterTemplateKeyword;
4403 if (Left.isOneOf(tok::exclaim, tok::tilde))
4404 return false;
4405 if (Left.is(tok::at) &&
4406 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4407 tok::numeric_constant, tok::l_paren, tok::l_brace,
4408 tok::kw_true, tok::kw_false)) {
4409 return false;
4410 }
4411 if (Left.is(tok::colon))
4412 return Left.isNot(TT_ObjCMethodExpr);
4413 if (Left.is(tok::coloncolon)) {
4414 return Right.is(tok::star) && Right.is(TT_PointerOrReference) &&
4415 Style.PointerAlignment != FormatStyle::PAS_Left;
4416 }
4417 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
4418 if (Style.Language == FormatStyle::LK_TextProto ||
4419 (Style.Language == FormatStyle::LK_Proto &&
4420 (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
4421 // Format empty list as `<>`.
4422 if (Left.is(tok::less) && Right.is(tok::greater))
4423 return false;
4424 return !Style.Cpp11BracedListStyle;
4425 }
4426 // Don't attempt to format operator<(), as it is handled later.
4427 if (Right.isNot(TT_OverloadedOperatorLParen))
4428 return false;
4429 }
4430 if (Right.is(tok::ellipsis)) {
4431 return Left.Tok.isLiteral() || (Left.is(tok::identifier) && BeforeLeft &&
4432 BeforeLeft->is(tok::kw_case));
4433 }
4434 if (Left.is(tok::l_square) && Right.is(tok::amp))
4435 return Style.SpacesInSquareBrackets;
4436 if (Right.is(TT_PointerOrReference)) {
4437 if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) {
4438 if (!Left.MatchingParen)
4439 return true;
4440 FormatToken *TokenBeforeMatchingParen =
4441 Left.MatchingParen->getPreviousNonComment();
4442 if (!TokenBeforeMatchingParen || Left.isNot(TT_TypeDeclarationParen))
4443 return true;
4444 }
4445 // Add a space if the previous token is a pointer qualifier or the closing
4446 // parenthesis of __attribute__(()) expression and the style requires spaces
4447 // after pointer qualifiers.
4448 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4449 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4450 (Left.is(TT_AttributeRParen) ||
4451 Left.canBePointerOrReferenceQualifier())) {
4452 return true;
4453 }
4454 if (Left.Tok.isLiteral())
4455 return true;
4456 // for (auto a = 0, b = 0; const auto & c : {1, 2, 3})
4457 if (Left.isTypeOrIdentifier(LangOpts) && Right.Next && Right.Next->Next &&
4458 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4459 return getTokenPointerOrReferenceAlignment(Right) !=
4461 }
4462 return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
4463 (getTokenPointerOrReferenceAlignment(Right) !=
4465 (Line.IsMultiVariableDeclStmt &&
4466 (Left.NestingLevel == 0 ||
4467 (Left.NestingLevel == 1 && startsWithInitStatement(Line)))));
4468 }
4469 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
4470 (Left.isNot(TT_PointerOrReference) ||
4471 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4472 !Line.IsMultiVariableDeclStmt))) {
4473 return true;
4474 }
4475 if (Left.is(TT_PointerOrReference)) {
4476 // Add a space if the next token is a pointer qualifier and the style
4477 // requires spaces before pointer qualifiers.
4478 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4479 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4480 Right.canBePointerOrReferenceQualifier()) {
4481 return true;
4482 }
4483 // & 1
4484 if (Right.Tok.isLiteral())
4485 return true;
4486 // & /* comment
4487 if (Right.is(TT_BlockComment))
4488 return true;
4489 // foo() -> const Bar * override/final
4490 // S::foo() & noexcept/requires
4491 if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4492 TT_RequiresClause) &&
4493 Right.isNot(TT_StartOfName)) {
4494 return true;
4495 }
4496 // & {
4497 if (Right.is(tok::l_brace) && Right.is(BK_Block))
4498 return true;
4499 // for (auto a = 0, b = 0; const auto& c : {1, 2, 3})
4500 if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) && Right.Next &&
4501 Right.Next->is(TT_RangeBasedForLoopColon)) {
4502 return getTokenPointerOrReferenceAlignment(Left) !=
4504 }
4505 if (Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4506 tok::l_paren)) {
4507 return false;
4508 }
4509 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4510 return false;
4511 // FIXME: Setting IsMultiVariableDeclStmt for the whole line is error-prone,
4512 // because it does not take into account nested scopes like lambdas.
4513 // In multi-variable declaration statements, attach */& to the variable
4514 // independently of the style. However, avoid doing it if we are in a nested
4515 // scope, e.g. lambda. We still need to special-case statements with
4516 // initializers.
4517 if (Line.IsMultiVariableDeclStmt &&
4518 (Left.NestingLevel == Line.First->NestingLevel ||
4519 ((Left.NestingLevel == Line.First->NestingLevel + 1) &&
4520 startsWithInitStatement(Line)))) {
4521 return false;
4522 }
4523 if (!BeforeLeft)
4524 return false;
4525 if (BeforeLeft->is(tok::coloncolon)) {
4526 return Left.is(tok::star) &&
4527 Style.PointerAlignment != FormatStyle::PAS_Right;
4528 }
4529 return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
4530 }
4531 // Ensure right pointer alignment with ellipsis e.g. int *...P
4532 if (Left.is(tok::ellipsis) && BeforeLeft &&
4533 BeforeLeft->isPointerOrReference()) {
4534 return Style.PointerAlignment != FormatStyle::PAS_Right;
4535 }
4536
4537 if (Right.is(tok::star) && Left.is(tok::l_paren))
4538 return false;
4539 if (Left.is(tok::star) && Right.isPointerOrReference())
4540 return false;
4541 if (Right.isPointerOrReference()) {
4542 const FormatToken *Previous = &Left;
4543 while (Previous && Previous->isNot(tok::kw_operator)) {
4544 if (Previous->is(tok::identifier) || Previous->isTypeName(LangOpts)) {
4545 Previous = Previous->getPreviousNonComment();
4546 continue;
4547 }
4548 if (Previous->is(TT_TemplateCloser) && Previous->MatchingParen) {
4549 Previous = Previous->MatchingParen->getPreviousNonComment();
4550 continue;
4551 }
4552 if (Previous->is(tok::coloncolon)) {
4553 Previous = Previous->getPreviousNonComment();
4554 continue;
4555 }
4556 break;
4557 }
4558 // Space between the type and the * in:
4559 // operator void*()
4560 // operator char*()
4561 // operator void const*()
4562 // operator void volatile*()
4563 // operator /*comment*/ const char*()
4564 // operator volatile /*comment*/ char*()
4565 // operator Foo*()
4566 // operator C<T>*()
4567 // operator std::Foo*()
4568 // operator C<T>::D<U>*()
4569 // dependent on PointerAlignment style.
4570 if (Previous) {
4571 if (Previous->endsSequence(tok::kw_operator))
4572 return Style.PointerAlignment != FormatStyle::PAS_Left;
4573 if (Previous->is(tok::kw_const) || Previous->is(tok::kw_volatile)) {
4574 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4575 (Style.SpaceAroundPointerQualifiers ==
4577 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4578 }
4579 }
4580 }
4581 if (Style.isCSharp() && Left.is(Keywords.kw_is) && Right.is(tok::l_square))
4582 return true;
4583 const auto SpaceRequiredForArrayInitializerLSquare =
4584 [](const FormatToken &LSquareTok, const FormatStyle &Style) {
4585 return Style.SpacesInContainerLiterals ||
4586 (Style.isProto() && !Style.Cpp11BracedListStyle &&
4587 LSquareTok.endsSequence(tok::l_square, tok::colon,
4588 TT_SelectorName));
4589 };
4590 if (Left.is(tok::l_square)) {
4591 return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
4592 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4593 (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4594 TT_LambdaLSquare) &&
4595 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
4596 }
4597 if (Right.is(tok::r_square)) {
4598 return Right.MatchingParen &&
4599 ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4600 SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
4601 Style)) ||
4602 (Style.SpacesInSquareBrackets &&
4603 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4604 TT_StructuredBindingLSquare,
4605 TT_LambdaLSquare)));
4606 }
4607 if (Right.is(tok::l_square) &&
4608 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4609 TT_DesignatedInitializerLSquare,
4610 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4611 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
4612 !(Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4613 Right.is(TT_ArraySubscriptLSquare))) {
4614 return false;
4615 }
4616 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
4617 return !Left.Children.empty(); // No spaces in "{}".
4618 if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) ||
4619 (Right.is(tok::r_brace) && Right.MatchingParen &&
4620 Right.MatchingParen->isNot(BK_Block))) {
4621 return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4622 }
4623 if (Left.is(TT_BlockComment)) {
4624 // No whitespace in x(/*foo=*/1), except for JavaScript.
4625 return Style.isJavaScript() || !Left.TokenText.ends_with("=*/");
4626 }
4627
4628 // Space between template and attribute.
4629 // e.g. template <typename T> [[nodiscard]] ...
4630 if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
4631 return true;
4632 // Space before parentheses common for all languages
4633 if (Right.is(tok::l_paren)) {
4634 if (Left.is(TT_TemplateCloser) && Right.isNot(TT_FunctionTypeLParen))
4635 return spaceRequiredBeforeParens(Right);
4636 if (Left.isOneOf(TT_RequiresClause,
4637 TT_RequiresClauseInARequiresExpression)) {
4638 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4639 spaceRequiredBeforeParens(Right);
4640 }
4641 if (Left.is(TT_RequiresExpression)) {
4642 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4643 spaceRequiredBeforeParens(Right);
4644 }
4645 if (Left.is(TT_AttributeRParen) ||
4646 (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
4647 return true;
4648 }
4649 if (Left.is(TT_ForEachMacro)) {
4650 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4651 spaceRequiredBeforeParens(Right);
4652 }
4653 if (Left.is(TT_IfMacro)) {
4654 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4655 spaceRequiredBeforeParens(Right);
4656 }
4657 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
4658 Left.isOneOf(tok::kw_new, tok::kw_delete) &&
4659 Right.isNot(TT_OverloadedOperatorLParen) &&
4660 !(Line.MightBeFunctionDecl && Left.is(TT_FunctionDeclarationName))) {
4661 return Style.SpaceBeforeParensOptions.AfterPlacementOperator;
4662 }
4663 if (Line.Type == LT_ObjCDecl)
4664 return true;
4665 if (Left.is(tok::semi))
4666 return true;
4667 if (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4668 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4669 Left.isIf(Line.Type != LT_PreprocessorDirective) ||
4670 Right.is(TT_ConditionLParen)) {
4671 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4672 spaceRequiredBeforeParens(Right);
4673 }
4674
4675 // TODO add Operator overloading specific Options to
4676 // SpaceBeforeParensOptions
4677 if (Right.is(TT_OverloadedOperatorLParen))
4678 return spaceRequiredBeforeParens(Right);
4679 // Function declaration or definition
4680 if (Line.MightBeFunctionDecl && (Left.is(TT_FunctionDeclarationName))) {
4681 if (Line.mightBeFunctionDefinition()) {
4682 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4683 spaceRequiredBeforeParens(Right);
4684 } else {
4685 return Style.SpaceBeforeParensOptions.AfterFunctionDeclarationName ||
4686 spaceRequiredBeforeParens(Right);
4687 }
4688 }
4689 // Lambda
4690 if (Line.Type != LT_PreprocessorDirective && Left.is(tok::r_square) &&
4691 Left.MatchingParen && Left.MatchingParen->is(TT_LambdaLSquare)) {
4692 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4693 spaceRequiredBeforeParens(Right);
4694 }
4695 if (!BeforeLeft || !BeforeLeft->isOneOf(tok::period, tok::arrow)) {
4696 if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4697 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4698 spaceRequiredBeforeParens(Right);
4699 }
4700 if (Left.isOneOf(tok::kw_new, tok::kw_delete)) {
4701 return ((!Line.MightBeFunctionDecl || !BeforeLeft) &&
4702 Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
4703 spaceRequiredBeforeParens(Right);
4704 }
4705
4706 if (Left.is(tok::r_square) && Left.MatchingParen &&
4707 Left.MatchingParen->Previous &&
4708 Left.MatchingParen->Previous->is(tok::kw_delete)) {
4709 return (Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
4710 spaceRequiredBeforeParens(Right);
4711 }
4712 }
4713 // Handle builtins like identifiers.
4714 if (Line.Type != LT_PreprocessorDirective &&
4715 (Left.Tok.getIdentifierInfo() || Left.is(tok::r_paren))) {
4716 return spaceRequiredBeforeParens(Right);
4717 }
4718 return false;
4719 }
4720 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
4721 return false;
4722 if (Right.is(TT_UnaryOperator)) {
4723 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
4724 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
4725 }
4726 // No space between the variable name and the initializer list.
4727 // A a1{1};
4728 // Verilog doesn't have such syntax, but it has word operators that are C++
4729 // identifiers like `a inside {b, c}`. So the rule is not applicable.
4730 if (!Style.isVerilog() &&
4731 (Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4732 tok::r_paren) ||
4733 Left.isTypeName(LangOpts)) &&
4734 Right.is(tok::l_brace) && Right.getNextNonComment() &&
4735 Right.isNot(BK_Block)) {
4736 return false;
4737 }
4738 if (Left.is(tok::period) || Right.is(tok::period))
4739 return false;
4740 // u#str, U#str, L#str, u8#str
4741 // uR#str, UR#str, LR#str, u8R#str
4742 if (Right.is(tok::hash) && Left.is(tok::identifier) &&
4743 (Left.TokenText == "L" || Left.TokenText == "u" ||
4744 Left.TokenText == "U" || Left.TokenText == "u8" ||
4745 Left.TokenText == "LR" || Left.TokenText == "uR" ||
4746 Left.TokenText == "UR" || Left.TokenText == "u8R")) {
4747 return false;
4748 }
4749 if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
4750 Left.MatchingParen->Previous &&
4751 (Left.MatchingParen->Previous->is(tok::period) ||
4752 Left.MatchingParen->Previous->is(tok::coloncolon))) {
4753 // Java call to generic function with explicit type:
4754 // A.<B<C<...>>>DoSomething();
4755 // A::<B<C<...>>>DoSomething(); // With a Java 8 method reference.
4756 return false;
4757 }
4758 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
4759 return false;
4760 if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at)) {
4761 // Objective-C dictionary literal -> no space after opening brace.
4762 return false;
4763 }
4764 if (Right.is(tok::r_brace) && Right.MatchingParen &&
4765 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4766 // Objective-C dictionary literal -> no space before closing brace.
4767 return false;
4768 }
4769 if (Right.is(TT_TrailingAnnotation) && Right.isOneOf(tok::amp, tok::ampamp) &&
4770 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4771 (!Right.Next || Right.Next->is(tok::semi))) {
4772 // Match const and volatile ref-qualifiers without any additional
4773 // qualifiers such as
4774 // void Fn() const &;
4775 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
4776 }
4777
4778 return true;
4779}
4780
4781bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
4782 const FormatToken &Right) const {
4783 const FormatToken &Left = *Right.Previous;
4784
4785 // If the token is finalized don't touch it (as it could be in a
4786 // clang-format-off section).
4787 if (Left.Finalized)
4788 return Right.hasWhitespaceBefore();
4789
4790 const bool IsVerilog = Style.isVerilog();
4791 assert(!IsVerilog || !IsCpp);
4792
4793 // Never ever merge two words.
4794 if (Keywords.isWordLike(Right, IsVerilog) &&
4795 Keywords.isWordLike(Left, IsVerilog)) {
4796 return true;
4797 }
4798
4799 // Leave a space between * and /* to avoid C4138 `comment end` found outside
4800 // of comment.
4801 if (Left.is(tok::star) && Right.is(tok::comment))
4802 return true;
4803
4804 if (IsCpp) {
4805 if (Left.is(TT_OverloadedOperator) &&
4806 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
4807 return true;
4808 }
4809 // Space between UDL and dot: auto b = 4s .count();
4810 if (Right.is(tok::period) && Left.is(tok::numeric_constant))
4811 return true;
4812 // Space between import <iostream>.
4813 // or import .....;
4814 if (Left.is(Keywords.kw_import) && Right.isOneOf(tok::less, tok::ellipsis))
4815 return true;
4816 // Space between `module :` and `import :`.
4817 if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
4818 Right.is(TT_ModulePartitionColon)) {
4819 return true;
4820 }
4821 // No space between import foo:bar but keep a space between import :bar;
4822 if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
4823 return false;
4824 // No space between :bar;
4825 if (Left.is(TT_ModulePartitionColon) &&
4826 Right.isOneOf(tok::identifier, tok::kw_private)) {
4827 return false;
4828 }
4829 if (Left.is(tok::ellipsis) && Right.is(tok::identifier) &&
4830 Line.First->is(Keywords.kw_import)) {
4831 return false;
4832 }
4833 // Space in __attribute__((attr)) ::type.
4834 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
4835 Right.is(tok::coloncolon)) {
4836 return true;
4837 }
4838
4839 if (Left.is(tok::kw_operator))
4840 return Right.is(tok::coloncolon);
4841 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
4842 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
4843 return true;
4844 }
4845 if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) &&
4846 Right.is(TT_TemplateOpener)) {
4847 return true;
4848 }
4849 if (Left.Tok.getIdentifierInfo() && Right.is(tok::numeric_constant))
4850 return Right.TokenText[0] != '.';
4851 } else if (Style.isProto()) {
4852 if (Right.is(tok::period) &&
4853 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
4854 Keywords.kw_repeated, Keywords.kw_extend)) {
4855 return true;
4856 }
4857 if (Right.is(tok::l_paren) &&
4858 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
4859 return true;
4860 }
4861 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
4862 return true;
4863 // Slashes occur in text protocol extension syntax: [type/type] { ... }.
4864 if (Left.is(tok::slash) || Right.is(tok::slash))
4865 return false;
4866 if (Left.MatchingParen &&
4867 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
4868 Right.isOneOf(tok::l_brace, tok::less)) {
4869 return !Style.Cpp11BracedListStyle;
4870 }
4871 // A percent is probably part of a formatting specification, such as %lld.
4872 if (Left.is(tok::percent))
4873 return false;
4874 // Preserve the existence of a space before a percent for cases like 0x%04x
4875 // and "%d %d"
4876 if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
4877 return Right.hasWhitespaceBefore();
4878 } else if (Style.isJson()) {
4879 if (Right.is(tok::colon) && Left.is(tok::string_literal))
4880 return Style.SpaceBeforeJsonColon;
4881 } else if (Style.isCSharp()) {
4882 // Require spaces around '{' and before '}' unless they appear in
4883 // interpolated strings. Interpolated strings are merged into a single token
4884 // so cannot have spaces inserted by this function.
4885
4886 // No space between 'this' and '['
4887 if (Left.is(tok::kw_this) && Right.is(tok::l_square))
4888 return false;
4889
4890 // No space between 'new' and '('
4891 if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
4892 return false;
4893
4894 // Space before { (including space within '{ {').
4895 if (Right.is(tok::l_brace))
4896 return true;
4897
4898 // Spaces inside braces.
4899 if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace))
4900 return true;
4901
4902 if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace))
4903 return true;
4904
4905 // Spaces around '=>'.
4906 if (Left.is(TT_FatArrow) || Right.is(TT_FatArrow))
4907 return true;
4908
4909 // No spaces around attribute target colons
4910 if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon))
4911 return false;
4912
4913 // space between type and variable e.g. Dictionary<string,string> foo;
4914 if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
4915 return true;
4916
4917 // spaces inside square brackets.
4918 if (Left.is(tok::l_square) || Right.is(tok::r_square))
4919 return Style.SpacesInSquareBrackets;
4920
4921 // No space before ? in nullable types.
4922 if (Right.is(TT_CSharpNullable))
4923 return false;
4924
4925 // No space before null forgiving '!'.
4926 if (Right.is(TT_NonNullAssertion))
4927 return false;
4928
4929 // No space between consecutive commas '[,,]'.
4930 if (Left.is(tok::comma) && Right.is(tok::comma))
4931 return false;
4932
4933 // space after var in `var (key, value)`
4934 if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren))
4935 return true;
4936
4937 // space between keywords and paren e.g. "using ("
4938 if (Right.is(tok::l_paren)) {
4939 if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
4940 Keywords.kw_lock)) {
4941 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4942 spaceRequiredBeforeParens(Right);
4943 }
4944 }
4945
4946 // space between method modifier and opening parenthesis of a tuple return
4947 // type
4948 if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
4949 tok::kw_virtual, tok::kw_extern, tok::kw_static,
4950 Keywords.kw_internal, Keywords.kw_abstract,
4951 Keywords.kw_sealed, Keywords.kw_override,
4952 Keywords.kw_async, Keywords.kw_unsafe) &&
4953 Right.is(tok::l_paren)) {
4954 return true;
4955 }
4956 } else if (Style.isJavaScript()) {
4957 if (Left.is(TT_FatArrow))
4958 return true;
4959 // for await ( ...
4960 if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
4961 Left.Previous->is(tok::kw_for)) {
4962 return true;
4963 }
4964 if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
4965 Right.MatchingParen) {
4966 const FormatToken *Next = Right.MatchingParen->getNextNonComment();
4967 // An async arrow function, for example: `x = async () => foo();`,
4968 // as opposed to calling a function called async: `x = async();`
4969 if (Next && Next->is(TT_FatArrow))
4970 return true;
4971 }
4972 if ((Left.is(TT_TemplateString) && Left.TokenText.ends_with("${")) ||
4973 (Right.is(TT_TemplateString) && Right.TokenText.starts_with("}"))) {
4974 return false;
4975 }
4976 // In tagged template literals ("html`bar baz`"), there is no space between
4977 // the tag identifier and the template string.
4978 if (Keywords.IsJavaScriptIdentifier(Left,
4979 /* AcceptIdentifierName= */ false) &&
4980 Right.is(TT_TemplateString)) {
4981 return false;
4982 }
4983 if (Right.is(tok::star) &&
4984 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
4985 return false;
4986 }
4987 if (Right.isOneOf(tok::l_brace, tok::l_square) &&
4988 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
4989 Keywords.kw_extends, Keywords.kw_implements)) {
4990 return true;
4991 }
4992 if (Right.is(tok::l_paren)) {
4993 // JS methods can use some keywords as names (e.g. `delete()`).
4994 if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
4995 return false;
4996 // Valid JS method names can include keywords, e.g. `foo.delete()` or
4997 // `bar.instanceof()`. Recognize call positions by preceding period.
4998 if (Left.Previous && Left.Previous->is(tok::period) &&
4999 Left.Tok.getIdentifierInfo()) {
5000 return false;
5001 }
5002 // Additional unary JavaScript operators that need a space after.
5003 if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5004 tok::kw_void)) {
5005 return true;
5006 }
5007 }
5008 // `foo as const;` casts into a const type.
5009 if (Left.endsSequence(tok::kw_const, Keywords.kw_as))
5010 return false;
5011 if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5012 tok::kw_const) ||
5013 // "of" is only a keyword if it appears after another identifier
5014 // (e.g. as "const x of y" in a for loop), or after a destructuring
5015 // operation (const [x, y] of z, const {a, b} of c).
5016 (Left.is(Keywords.kw_of) && Left.Previous &&
5017 (Left.Previous->is(tok::identifier) ||
5018 Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
5019 (!Left.Previous || Left.Previous->isNot(tok::period))) {
5020 return true;
5021 }
5022 if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
5023 Left.Previous->is(tok::period) && Right.is(tok::l_paren)) {
5024 return false;
5025 }
5026 if (Left.is(Keywords.kw_as) &&
5027 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5028 return true;
5029 }
5030 if (Left.is(tok::kw_default) && Left.Previous &&
5031 Left.Previous->is(tok::kw_export)) {
5032 return true;
5033 }
5034 if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
5035 return true;
5036 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5037 return false;
5038 if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
5039 return false;
5040 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
5041 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5042 return false;
5043 }
5044 if (Left.is(tok::ellipsis))
5045 return false;
5046 if (Left.is(TT_TemplateCloser) &&
5047 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5048 Keywords.kw_implements, Keywords.kw_extends)) {
5049 // Type assertions ('<type>expr') are not followed by whitespace. Other
5050 // locations that should have whitespace following are identified by the
5051 // above set of follower tokens.
5052 return false;
5053 }
5054 if (Right.is(TT_NonNullAssertion))
5055 return false;
5056 if (Left.is(TT_NonNullAssertion) &&
5057 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5058 return true; // "x! as string", "x! in y"
5059 }
5060 } else if (Style.Language == FormatStyle::LK_Java) {
5061 if (Left.is(TT_CaseLabelArrow) || Right.is(TT_CaseLabelArrow))
5062 return true;
5063 if (Left.is(tok::r_square) && Right.is(tok::l_brace))
5064 return true;
5065 // spaces inside square brackets.
5066 if (Left.is(tok::l_square) || Right.is(tok::r_square))
5067 return Style.SpacesInSquareBrackets;
5068
5069 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) {
5070 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5071 spaceRequiredBeforeParens(Right);
5072 }
5073 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
5074 tok::kw_protected) ||
5075 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
5076 Keywords.kw_native)) &&
5077 Right.is(TT_TemplateOpener)) {
5078 return true;
5079 }
5080 } else if (IsVerilog) {
5081 // An escaped identifier ends with whitespace.
5082 if (Left.is(tok::identifier) && Left.TokenText[0] == '\\')
5083 return true;
5084 // Add space between things in a primitive's state table unless in a
5085 // transition like `(0?)`.
5086 if ((Left.is(TT_VerilogTableItem) &&
5087 !Right.isOneOf(tok::r_paren, tok::semi)) ||
5088 (Right.is(TT_VerilogTableItem) && Left.isNot(tok::l_paren))) {
5089 const FormatToken *Next = Right.getNextNonComment();
5090 return !(Next && Next->is(tok::r_paren));
5091 }
5092 // Don't add space within a delay like `#0`.
5093 if (Left.isNot(TT_BinaryOperator) &&
5094 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5095 return false;
5096 }
5097 // Add space after a delay.
5098 if (Right.isNot(tok::semi) &&
5099 (Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5100 Left.endsSequence(tok::numeric_constant,
5101 Keywords.kw_verilogHashHash) ||
5102 (Left.is(tok::r_paren) && Left.MatchingParen &&
5103 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5104 return true;
5105 }
5106 // Don't add embedded spaces in a number literal like `16'h1?ax` or an array
5107 // literal like `'{}`.
5108 if (Left.is(Keywords.kw_apostrophe) ||
5109 (Left.is(TT_VerilogNumberBase) && Right.is(tok::numeric_constant))) {
5110 return false;
5111 }
5112 // Add spaces around the implication operator `->`.
5113 if (Left.is(tok::arrow) || Right.is(tok::arrow))
5114 return true;
5115 // Don't add spaces between two at signs. Like in a coverage event.
5116 // Don't add spaces between at and a sensitivity list like
5117 // `@(posedge clk)`.
5118 if (Left.is(tok::at) && Right.isOneOf(tok::l_paren, tok::star, tok::at))
5119 return false;
5120 // Add space between the type name and dimension like `logic [1:0]`.
5121 if (Right.is(tok::l_square) &&
5122 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5123 return true;
5124 }
5125 // In a tagged union expression, there should be a space after the tag.
5126 if (Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5127 Keywords.isVerilogIdentifier(Left) && Left.getPreviousNonComment() &&
5128 Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5129 return true;
5130 }
5131 // Don't add spaces between a casting type and the quote or repetition count
5132 // and the brace. The case of tagged union expressions is handled by the
5133 // previous rule.
5134 if ((Right.is(Keywords.kw_apostrophe) ||
5135 (Right.is(BK_BracedInit) && Right.is(tok::l_brace))) &&
5136 !(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
5137 Keywords.isVerilogWordOperator(Left)) &&
5138 (Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5139 tok::numeric_constant) ||
5140 Keywords.isWordLike(Left))) {
5141 return false;
5142 }
5143 // Don't add spaces in imports like `import foo::*;`.
5144 if ((Right.is(tok::star) && Left.is(tok::coloncolon)) ||
5145 (Left.is(tok::star) && Right.is(tok::semi))) {
5146 return false;
5147 }
5148 // Add space in attribute like `(* ASYNC_REG = "TRUE" *)`.
5149 if (Left.endsSequence(tok::star, tok::l_paren) && Right.is(tok::identifier))
5150 return true;
5151 // Add space before drive strength like in `wire (strong1, pull0)`.
5152 if (Right.is(tok::l_paren) && Right.is(TT_VerilogStrength))
5153 return true;
5154 // Don't add space in a streaming concatenation like `{>>{j}}`.
5155 if ((Left.is(tok::l_brace) &&
5156 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5157 (Left.endsSequence(tok::lessless, tok::l_brace) ||
5158 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5159 return false;
5160 }
5161 } else if (Style.isTableGen()) {
5162 // Avoid to connect [ and {. [{ is start token of multiline string.
5163 if (Left.is(tok::l_square) && Right.is(tok::l_brace))
5164 return true;
5165 if (Left.is(tok::r_brace) && Right.is(tok::r_square))
5166 return true;
5167 // Do not insert around colon in DAGArg and cond operator.
5168 if (Right.isOneOf(TT_TableGenDAGArgListColon,
5169 TT_TableGenDAGArgListColonToAlign) ||
5170 Left.isOneOf(TT_TableGenDAGArgListColon,
5171 TT_TableGenDAGArgListColonToAlign)) {
5172 return false;
5173 }
5174 if (Right.is(TT_TableGenCondOperatorColon))
5175 return false;
5176 if (Left.isOneOf(TT_TableGenDAGArgOperatorID,
5177 TT_TableGenDAGArgOperatorToBreak) &&
5178 Right.isNot(TT_TableGenDAGArgCloser)) {
5179 return true;
5180 }
5181 // Do not insert bang operators and consequent openers.
5182 if (Right.isOneOf(tok::l_paren, tok::less) &&
5183 Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5184 return false;
5185 }
5186 // Trailing paste requires space before '{' or ':', the case in name values.
5187 // Not before ';', the case in normal values.
5188 if (Left.is(TT_TableGenTrailingPasteOperator) &&
5189 Right.isOneOf(tok::l_brace, tok::colon)) {
5190 return true;
5191 }
5192 // Otherwise paste operator does not prefer space around.
5193 if (Left.is(tok::hash) || Right.is(tok::hash))
5194 return false;
5195 // Sure not to connect after defining keywords.
5196 if (Keywords.isTableGenDefinition(Left))
5197 return true;
5198 }
5199
5200 if (Left.is(TT_ImplicitStringLiteral))
5201 return Right.hasWhitespaceBefore();
5202 if (Line.Type == LT_ObjCMethodDecl) {
5203 if (Left.is(TT_ObjCMethodSpecifier))
5204 return true;
5205 if (Left.is(tok::r_paren) && Left.isNot(TT_AttributeRParen) &&
5206 canBeObjCSelectorComponent(Right)) {
5207 // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a
5208 // keyword in Objective-C, and '+ (instancetype)new;' is a standard class
5209 // method declaration.
5210 return false;
5211 }
5212 }
5213 if (Line.Type == LT_ObjCProperty &&
5214 (Right.is(tok::equal) || Left.is(tok::equal))) {
5215 return false;
5216 }
5217
5218 if (Right.is(TT_TrailingReturnArrow) || Left.is(TT_TrailingReturnArrow))
5219 return true;
5220
5221 if (Left.is(tok::comma) && Right.isNot(TT_OverloadedOperatorLParen) &&
5222 // In an unexpanded macro call we only find the parentheses and commas
5223 // in a line; the commas and closing parenthesis do not require a space.
5224 (Left.Children.empty() || !Left.MacroParent)) {
5225 return true;
5226 }
5227 if (Right.is(tok::comma))
5228 return false;
5229 if (Right.is(TT_ObjCBlockLParen))
5230 return true;
5231 if (Right.is(TT_CtorInitializerColon))
5232 return Style.SpaceBeforeCtorInitializerColon;
5233 if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5234 return false;
5235 if (Right.is(TT_RangeBasedForLoopColon) &&
5236 !Style.SpaceBeforeRangeBasedForLoopColon) {
5237 return false;
5238 }
5239 if (Left.is(TT_BitFieldColon)) {
5240 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5241 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5242 }
5243 if (Right.is(tok::colon)) {
5244 if (Right.is(TT_CaseLabelColon))
5245 return Style.SpaceBeforeCaseColon;
5246 if (Right.is(TT_GotoLabelColon))
5247 return false;
5248 // `private:` and `public:`.
5249 if (!Right.getNextNonComment())
5250 return false;
5251 if (Right.is(TT_ObjCMethodExpr))
5252 return false;
5253 if (Left.is(tok::question))
5254 return false;
5255 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
5256 return false;
5257 if (Right.is(TT_DictLiteral))
5258 return Style.SpacesInContainerLiterals;
5259 if (Right.is(TT_AttributeColon))
5260 return false;
5261 if (Right.is(TT_CSharpNamedArgumentColon))
5262 return false;
5263 if (Right.is(TT_GenericSelectionColon))
5264 return false;
5265 if (Right.is(TT_BitFieldColon)) {
5266 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5267 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5268 }
5269 return true;
5270 }
5271 // Do not merge "- -" into "--".
5272 if ((Left.isOneOf(tok::minus, tok::minusminus) &&
5273 Right.isOneOf(tok::minus, tok::minusminus)) ||
5274 (Left.isOneOf(tok::plus, tok::plusplus) &&
5275 Right.isOneOf(tok::plus, tok::plusplus))) {
5276 return true;
5277 }
5278 if (Left.is(TT_UnaryOperator)) {
5279 // Lambda captures allow for a lone &, so "&]" needs to be properly
5280 // handled.
5281 if (Left.is(tok::amp) && Right.is(tok::r_square))
5282 return Style.SpacesInSquareBrackets;
5283 return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim);
5284 }
5285
5286 // If the next token is a binary operator or a selector name, we have
5287 // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
5288 if (Left.is(TT_CastRParen)) {
5289 return Style.SpaceAfterCStyleCast ||
5290 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5291 }
5292
5293 auto ShouldAddSpacesInAngles = [this, &Right]() {
5294 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5295 return true;
5296 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5297 return Right.hasWhitespaceBefore();
5298 return false;
5299 };
5300
5301 if (Left.is(tok::greater) && Right.is(tok::greater)) {
5302 if (Style.Language == FormatStyle::LK_TextProto ||
5303 (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral))) {
5304 return !Style.Cpp11BracedListStyle;
5305 }
5306 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
5307 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5308 ShouldAddSpacesInAngles());
5309 }
5310 if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5311 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5312 (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) {
5313 return false;
5314 }
5315 if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) &&
5316 Right.getPrecedence() == prec::Assignment) {
5317 return false;
5318 }
5319 if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) &&
5320 (Left.is(tok::identifier) || Left.is(tok::kw_this))) {
5321 return false;
5322 }
5323 if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) {
5324 // Generally don't remove existing spaces between an identifier and "::".
5325 // The identifier might actually be a macro name such as ALWAYS_INLINE. If
5326 // this turns out to be too lenient, add analysis of the identifier itself.
5327 return Right.hasWhitespaceBefore();
5328 }
5329 if (Right.is(tok::coloncolon) &&
5330 !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5331 // Put a space between < and :: in vector< ::std::string >
5332 return (Left.is(TT_TemplateOpener) &&
5333 ((Style.Standard < FormatStyle::LS_Cpp11) ||
5334 ShouldAddSpacesInAngles())) ||
5335 !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
5336 tok::kw___super, TT_TemplateOpener,
5337 TT_TemplateCloser)) ||
5338 (Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5339 }
5340 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
5341 return ShouldAddSpacesInAngles();
5342 // Space before TT_StructuredBindingLSquare.
5343 if (Right.is(TT_StructuredBindingLSquare)) {
5344 return !Left.isOneOf(tok::amp, tok::ampamp) ||
5345 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
5346 }
5347 // Space before & or && following a TT_StructuredBindingLSquare.
5348 if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) &&
5349 Right.isOneOf(tok::amp, tok::ampamp)) {
5350 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5351 }
5352 if ((Right.is(TT_BinaryOperator) && Left.isNot(tok::l_paren)) ||
5353 (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5354 Right.isNot(tok::r_paren))) {
5355 return true;
5356 }
5357 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
5358 Left.MatchingParen &&
5359 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5360 return false;
5361 }
5362 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
5363 Line.Type == LT_ImportStatement) {
5364 return true;
5365 }
5366 if (Right.is(TT_TrailingUnaryOperator))
5367 return false;
5368 if (Left.is(TT_RegexLiteral))
5369 return false;
5370 return spaceRequiredBetween(Line, Left, Right);
5371}
5372
5373// Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
5374static bool isAllmanBrace(const FormatToken &Tok) {
5375 return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
5376 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5377}
5378
5379// Returns 'true' if 'Tok' is a function argument.
5380static bool IsFunctionArgument(const FormatToken &Tok) {
5381 return Tok.MatchingParen && Tok.MatchingParen->Next &&
5382 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren);
5383}
5384
5385static bool
5387 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5388 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5389}
5390
5391static bool isAllmanLambdaBrace(const FormatToken &Tok) {
5392 return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
5393 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5394}
5395
5396bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
5397 const FormatToken &Right) const {
5398 const FormatToken &Left = *Right.Previous;
5399 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
5400 return true;
5401
5402 if (Style.BreakFunctionDefinitionParameters && Line.MightBeFunctionDecl &&
5403 Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5404 Left.ParameterCount > 0) {
5405 return true;
5406 }
5407
5408 const auto *BeforeLeft = Left.Previous;
5409 const auto *AfterRight = Right.Next;
5410
5411 if (Style.isCSharp()) {
5412 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
5413 Style.BraceWrapping.AfterFunction) {
5414 return true;
5415 }
5416 if (Right.is(TT_CSharpNamedArgumentColon) ||
5417 Left.is(TT_CSharpNamedArgumentColon)) {
5418 return false;
5419 }
5420 if (Right.is(TT_CSharpGenericTypeConstraint))
5421 return true;
5422 if (AfterRight && AfterRight->is(TT_FatArrow) &&
5423 (Right.is(tok::numeric_constant) ||
5424 (Right.is(tok::identifier) && Right.TokenText == "_"))) {
5425 return true;
5426 }
5427
5428 // Break after C# [...] and before public/protected/private/internal.
5429 if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) &&
5430 (Right.isAccessSpecifier(/*ColonRequired=*/false) ||
5431 Right.is(Keywords.kw_internal))) {
5432 return true;
5433 }
5434 // Break between ] and [ but only when there are really 2 attributes.
5435 if (Left.is(TT_AttributeSquare) && Right.is(TT_AttributeSquare) &&
5436 Left.is(tok::r_square) && Right.is(tok::l_square)) {
5437 return true;
5438 }
5439 } else if (Style.isJavaScript()) {
5440 // FIXME: This might apply to other languages and token kinds.
5441 if (Right.is(tok::string_literal) && Left.is(tok::plus) && BeforeLeft &&
5442 BeforeLeft->is(tok::string_literal)) {
5443 return true;
5444 }
5445 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
5446 BeforeLeft && BeforeLeft->is(tok::equal) &&
5447 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5448 tok::kw_const) &&
5449 // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
5450 // above.
5451 !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
5452 // Object literals on the top level of a file are treated as "enum-style".
5453 // Each key/value pair is put on a separate line, instead of bin-packing.
5454 return true;
5455 }
5456 if (Left.is(tok::l_brace) && Line.Level == 0 &&
5457 (Line.startsWith(tok::kw_enum) ||
5458 Line.startsWith(tok::kw_const, tok::kw_enum) ||
5459 Line.startsWith(tok::kw_export, tok::kw_enum) ||
5460 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5461 // JavaScript top-level enum key/value pairs are put on separate lines
5462 // instead of bin-packing.
5463 return true;
5464 }
5465 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && BeforeLeft &&
5466 BeforeLeft->is(TT_FatArrow)) {
5467 // JS arrow function (=> {...}).
5468 switch (Style.AllowShortLambdasOnASingleLine) {
5470 return false;
5472 return true;
5474 return !Left.Children.empty();
5476 // allow one-lining inline (e.g. in function call args) and empty arrow
5477 // functions.
5478 return (Left.NestingLevel == 0 && Line.Level == 0) &&
5479 !Left.Children.empty();
5480 }
5481 llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum");
5482 }
5483
5484 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
5485 !Left.Children.empty()) {
5486 // Support AllowShortFunctionsOnASingleLine for JavaScript.
5487 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
5488 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
5489 (Left.NestingLevel == 0 && Line.Level == 0 &&
5490 Style.AllowShortFunctionsOnASingleLine &
5492 }
5493 } else if (Style.Language == FormatStyle::LK_Java) {
5494 if (Right.is(tok::plus) && Left.is(tok::string_literal) && AfterRight &&
5495 AfterRight->is(tok::string_literal)) {
5496 return true;
5497 }
5498 } else if (Style.isVerilog()) {
5499 // Break between assignments.
5500 if (Left.is(TT_VerilogAssignComma))
5501 return true;
5502 // Break between ports of different types.
5503 if (Left.is(TT_VerilogTypeComma))
5504 return true;
5505 // Break between ports in a module instantiation and after the parameter
5506 // list.
5507 if (Style.VerilogBreakBetweenInstancePorts &&
5508 (Left.is(TT_VerilogInstancePortComma) ||
5509 (Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5510 Left.MatchingParen &&
5511 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5512 return true;
5513 }
5514 // Break after labels. In Verilog labels don't have the 'case' keyword, so
5515 // it is hard to identify them in UnwrappedLineParser.
5516 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5517 return true;
5518 } else if (Style.BreakAdjacentStringLiterals &&
5519 (IsCpp || Style.isProto() ||
5520 Style.Language == FormatStyle::LK_TableGen)) {
5521 if (Left.isStringLiteral() && Right.isStringLiteral())
5522 return true;
5523 }
5524
5525 // Basic JSON newline processing.
5526 if (Style.isJson()) {
5527 // Always break after a JSON record opener.
5528 // {
5529 // }
5530 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace))
5531 return true;
5532 // Always break after a JSON array opener based on BreakArrays.
5533 if ((Left.is(TT_ArrayInitializerLSquare) && Left.is(tok::l_square) &&
5534 Right.isNot(tok::r_square)) ||
5535 Left.is(tok::comma)) {
5536 if (Right.is(tok::l_brace))
5537 return true;
5538 // scan to the right if an we see an object or an array inside
5539 // then break.
5540 for (const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5541 if (Tok->isOneOf(tok::l_brace, tok::l_square))
5542 return true;
5543 if (Tok->isOneOf(tok::r_brace, tok::r_square))
5544 break;
5545 }
5546 return Style.BreakArrays;
5547 }
5548 } else if (Style.isTableGen()) {
5549 // Break the comma in side cond operators.
5550 // !cond(case1:1,
5551 // case2:0);
5552 if (Left.is(TT_TableGenCondOperatorComma))
5553 return true;
5554 if (Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5555 Right.isNot(TT_TableGenDAGArgCloser)) {
5556 return true;
5557 }
5558 if (Left.is(TT_TableGenDAGArgListCommaToBreak))
5559 return true;
5560 if (Right.is(TT_TableGenDAGArgCloser) && Right.MatchingParen &&
5561 Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5562 &Left != Right.MatchingParen->Next) {
5563 // Check to avoid empty DAGArg such as (ins).
5564 return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
5565 }
5566 }
5567
5568 if (Line.startsWith(tok::kw_asm) && Right.is(TT_InlineASMColon) &&
5569 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
5570 return true;
5571 }
5572
5573 // If the last token before a '}', ']', or ')' is a comma or a trailing
5574 // comment, the intention is to insert a line break after it in order to make
5575 // shuffling around entries easier. Import statements, especially in
5576 // JavaScript, can be an exception to this rule.
5577 if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) {
5578 const FormatToken *BeforeClosingBrace = nullptr;
5579 if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5580 (Style.isJavaScript() && Left.is(tok::l_paren))) &&
5581 Left.isNot(BK_Block) && Left.MatchingParen) {
5582 BeforeClosingBrace = Left.MatchingParen->Previous;
5583 } else if (Right.MatchingParen &&
5584 (Right.MatchingParen->isOneOf(tok::l_brace,
5585 TT_ArrayInitializerLSquare) ||
5586 (Style.isJavaScript() &&
5587 Right.MatchingParen->is(tok::l_paren)))) {
5588 BeforeClosingBrace = &Left;
5589 }
5590 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5591 BeforeClosingBrace->isTrailingComment())) {
5592 return true;
5593 }
5594 }
5595
5596 if (Right.is(tok::comment)) {
5597 return Left.isNot(BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
5598 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
5599 }
5600 if (Left.isTrailingComment())
5601 return true;
5602 if (Left.IsUnterminatedLiteral)
5603 return true;
5604
5605 if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5606 Left.is(tok::string_literal) && Right.is(tok::lessless) && AfterRight &&
5607 AfterRight->is(tok::string_literal)) {
5608 return Right.NewlinesBefore > 0;
5609 }
5610
5611 if (Right.is(TT_RequiresClause)) {
5612 switch (Style.RequiresClausePosition) {
5615 return true;
5616 default:
5617 break;
5618 }
5619 }
5620 // Can break after template<> declaration
5621 if (Left.ClosesTemplateDeclaration && Left.MatchingParen &&
5622 Left.MatchingParen->NestingLevel == 0) {
5623 // Put concepts on the next line e.g.
5624 // template<typename T>
5625 // concept ...
5626 if (Right.is(tok::kw_concept))
5627 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
5628 return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
5629 (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
5630 Right.NewlinesBefore > 0);
5631 }
5632 if (Left.ClosesRequiresClause && Right.isNot(tok::semi)) {
5633 switch (Style.RequiresClausePosition) {
5636 return true;
5637 default:
5638 break;
5639 }
5640 }
5641 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
5642 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
5643 (Left.is(TT_CtorInitializerComma) ||
5644 Right.is(TT_CtorInitializerColon))) {
5645 return true;
5646 }
5647
5648 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5649 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5650 return true;
5651 }
5652 }
5653 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
5654 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
5655 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5656 return true;
5657 }
5658 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
5659 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
5660 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
5661 Right.is(TT_CtorInitializerColon)) {
5662 return true;
5663 }
5664
5665 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5666 Left.is(TT_CtorInitializerColon)) {
5667 return true;
5668 }
5669 }
5670 // Break only if we have multiple inheritance.
5671 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
5672 Right.is(TT_InheritanceComma)) {
5673 return true;
5674 }
5675 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
5676 Left.is(TT_InheritanceComma)) {
5677 return true;
5678 }
5679 if (Right.is(tok::string_literal) && Right.TokenText.starts_with("R\"")) {
5680 // Multiline raw string literals are special wrt. line breaks. The author
5681 // has made a deliberate choice and might have aligned the contents of the
5682 // string literal accordingly. Thus, we try keep existing line breaks.
5683 return Right.IsMultiline && Right.NewlinesBefore > 0;
5684 }
5685 if ((Left.is(tok::l_brace) ||
5686 (Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
5687 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
5688 // Don't put enums or option definitions onto single lines in protocol
5689 // buffers.
5690 return true;
5691 }
5692 if (Right.is(TT_InlineASMBrace))
5693 return Right.HasUnescapedNewline;
5694
5695 if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
5696 auto *FirstNonComment = Line.getFirstNonComment();
5697 bool AccessSpecifier =
5698 FirstNonComment &&
5699 FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,
5700 tok::kw_private, tok::kw_protected);
5701
5702 if (Style.BraceWrapping.AfterEnum) {
5703 if (Line.startsWith(tok::kw_enum) ||
5704 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5705 return true;
5706 }
5707 // Ensure BraceWrapping for `public enum A {`.
5708 if (AccessSpecifier && FirstNonComment->Next &&
5709 FirstNonComment->Next->is(tok::kw_enum)) {
5710 return true;
5711 }
5712 }
5713
5714 // Ensure BraceWrapping for `public interface A {`.
5715 if (Style.BraceWrapping.AfterClass &&
5716 ((AccessSpecifier && FirstNonComment->Next &&
5717 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5718 Line.startsWith(Keywords.kw_interface))) {
5719 return true;
5720 }
5721
5722 // Don't attempt to interpret struct return types as structs.
5723 if (Right.isNot(TT_FunctionLBrace)) {
5724 return (Line.startsWith(tok::kw_class) &&
5725 Style.BraceWrapping.AfterClass) ||
5726 (Line.startsWith(tok::kw_struct) &&
5727 Style.BraceWrapping.AfterStruct);
5728 }
5729 }
5730
5731 if (Left.is(TT_ObjCBlockLBrace) &&
5732 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
5733 return true;
5734 }
5735
5736 // Ensure wrapping after __attribute__((XX)) and @interface etc.
5737 if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5738 Right.is(TT_ObjCDecl)) {
5739 return true;
5740 }
5741
5742 if (Left.is(TT_LambdaLBrace)) {
5743 if (IsFunctionArgument(Left) &&
5744 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
5745 return false;
5746 }
5747
5748 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
5749 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
5750 (!Left.Children.empty() &&
5751 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
5752 return true;
5753 }
5754 }
5755
5756 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
5757 (Left.isPointerOrReference() || Left.is(TT_TemplateCloser))) {
5758 return true;
5759 }
5760
5761 // Put multiple Java annotation on a new line.
5762 if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
5763 Left.is(TT_LeadingJavaAnnotation) &&
5764 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
5765 (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
5766 return true;
5767 }
5768
5769 if (Right.is(TT_ProtoExtensionLSquare))
5770 return true;
5771
5772 // In text proto instances if a submessage contains at least 2 entries and at
5773 // least one of them is a submessage, like A { ... B { ... } ... },
5774 // put all of the entries of A on separate lines by forcing the selector of
5775 // the submessage B to be put on a newline.
5776 //
5777 // Example: these can stay on one line:
5778 // a { scalar_1: 1 scalar_2: 2 }
5779 // a { b { key: value } }
5780 //
5781 // and these entries need to be on a new line even if putting them all in one
5782 // line is under the column limit:
5783 // a {
5784 // scalar: 1
5785 // b { key: value }
5786 // }
5787 //
5788 // We enforce this by breaking before a submessage field that has previous
5789 // siblings, *and* breaking before a field that follows a submessage field.
5790 //
5791 // Be careful to exclude the case [proto.ext] { ... } since the `]` is
5792 // the TT_SelectorName there, but we don't want to break inside the brackets.
5793 //
5794 // Another edge case is @submessage { key: value }, which is a common
5795 // substitution placeholder. In this case we want to keep `@` and `submessage`
5796 // together.
5797 //
5798 // We ensure elsewhere that extensions are always on their own line.
5799 if (Style.isProto() && Right.is(TT_SelectorName) &&
5800 Right.isNot(tok::r_square) && AfterRight) {
5801 // Keep `@submessage` together in:
5802 // @submessage { key: value }
5803 if (Left.is(tok::at))
5804 return false;
5805 // Look for the scope opener after selector in cases like:
5806 // selector { ...
5807 // selector: { ...
5808 // selector: @base { ...
5809 const auto *LBrace = AfterRight;
5810 if (LBrace && LBrace->is(tok::colon)) {
5811 LBrace = LBrace->Next;
5812 if (LBrace && LBrace->is(tok::at)) {
5813 LBrace = LBrace->Next;
5814 if (LBrace)
5815 LBrace = LBrace->Next;
5816 }
5817 }
5818 if (LBrace &&
5819 // The scope opener is one of {, [, <:
5820 // selector { ... }
5821 // selector [ ... ]
5822 // selector < ... >
5823 //
5824 // In case of selector { ... }, the l_brace is TT_DictLiteral.
5825 // In case of an empty selector {}, the l_brace is not TT_DictLiteral,
5826 // so we check for immediately following r_brace.
5827 ((LBrace->is(tok::l_brace) &&
5828 (LBrace->is(TT_DictLiteral) ||
5829 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
5830 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
5831 // If Left.ParameterCount is 0, then this submessage entry is not the
5832 // first in its parent submessage, and we want to break before this entry.
5833 // If Left.ParameterCount is greater than 0, then its parent submessage
5834 // might contain 1 or more entries and we want to break before this entry
5835 // if it contains at least 2 entries. We deal with this case later by
5836 // detecting and breaking before the next entry in the parent submessage.
5837 if (Left.ParameterCount == 0)
5838 return true;
5839 // However, if this submessage is the first entry in its parent
5840 // submessage, Left.ParameterCount might be 1 in some cases.
5841 // We deal with this case later by detecting an entry
5842 // following a closing paren of this submessage.
5843 }
5844
5845 // If this is an entry immediately following a submessage, it will be
5846 // preceded by a closing paren of that submessage, like in:
5847 // left---. .---right
5848 // v v
5849 // sub: { ... } key: value
5850 // If there was a comment between `}` an `key` above, then `key` would be
5851 // put on a new line anyways.
5852 if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
5853 return true;
5854 }
5855
5856 return false;
5857}
5858
5859bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
5860 const FormatToken &Right) const {
5861 const FormatToken &Left = *Right.Previous;
5862 // Language-specific stuff.
5863 if (Style.isCSharp()) {
5864 if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
5865 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
5866 return false;
5867 }
5868 // Only break after commas for generic type constraints.
5869 if (Line.First->is(TT_CSharpGenericTypeConstraint))
5870 return Left.is(TT_CSharpGenericTypeConstraintComma);
5871 // Keep nullable operators attached to their identifiers.
5872 if (Right.is(TT_CSharpNullable))
5873 return false;
5874 } else if (Style.Language == FormatStyle::LK_Java) {
5875 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5876 Keywords.kw_implements)) {
5877 return false;
5878 }
5879 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5880 Keywords.kw_implements)) {
5881 return true;
5882 }
5883 } else if (Style.isJavaScript()) {
5884 const FormatToken *NonComment = Right.getPreviousNonComment();
5885 if (NonComment &&
5886 NonComment->isOneOf(
5887 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
5888 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
5889 tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
5890 Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
5891 Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
5892 Keywords.kw_await)) {
5893 return false; // Otherwise automatic semicolon insertion would trigger.
5894 }
5895 if (Right.NestingLevel == 0 &&
5896 (Left.Tok.getIdentifierInfo() ||
5897 Left.isOneOf(tok::r_square, tok::r_paren)) &&
5898 Right.isOneOf(tok::l_square, tok::l_paren)) {
5899 return false; // Otherwise automatic semicolon insertion would trigger.
5900 }
5901 if (NonComment && NonComment->is(tok::identifier) &&
5902 NonComment->TokenText == "asserts") {
5903 return false;
5904 }
5905 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace))
5906 return false;
5907 if (Left.is(TT_JsTypeColon))
5908 return true;
5909 // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
5910 if (Left.is(tok::exclaim) && Right.is(tok::colon))
5911 return false;
5912 // Look for is type annotations like:
5913 // function f(): a is B { ... }
5914 // Do not break before is in these cases.
5915 if (Right.is(Keywords.kw_is)) {
5916 const FormatToken *Next = Right.getNextNonComment();
5917 // If `is` is followed by a colon, it's likely that it's a dict key, so
5918 // ignore it for this check.
5919 // For example this is common in Polymer:
5920 // Polymer({
5921 // is: 'name',
5922 // ...
5923 // });
5924 if (!Next || Next->isNot(tok::colon))
5925 return false;
5926 }
5927 if (Left.is(Keywords.kw_in))
5928 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
5929 if (Right.is(Keywords.kw_in))
5930 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
5931 if (Right.is(Keywords.kw_as))
5932 return false; // must not break before as in 'x as type' casts
5933 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
5934 // extends and infer can appear as keywords in conditional types:
5935 // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
5936 // do not break before them, as the expressions are subject to ASI.
5937 return false;
5938 }
5939 if (Left.is(Keywords.kw_as))
5940 return true;
5941 if (Left.is(TT_NonNullAssertion))
5942 return true;
5943 if (Left.is(Keywords.kw_declare) &&
5944 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
5945 Keywords.kw_function, tok::kw_class, tok::kw_enum,
5946 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
5947 Keywords.kw_let, tok::kw_const)) {
5948 // See grammar for 'declare' statements at:
5949 // https://github.com/Microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md#A.10
5950 return false;
5951 }
5952 if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
5953 Right.isOneOf(tok::identifier, tok::string_literal)) {
5954 return false; // must not break in "module foo { ...}"
5955 }
5956 if (Right.is(TT_TemplateString) && Right.closesScope())
5957 return false;
5958 // Don't split tagged template literal so there is a break between the tag
5959 // identifier and template string.
5960 if (Left.is(tok::identifier) && Right.is(TT_TemplateString))
5961 return false;
5962 if (Left.is(TT_TemplateString) && Left.opensScope())
5963 return true;
5964 } else if (Style.isTableGen()) {
5965 // Avoid to break after "def", "class", "let" and so on.
5966 if (Keywords.isTableGenDefinition(Left))
5967 return false;
5968 // Avoid to break after '(' in the cases that is in bang operators.
5969 if (Right.is(tok::l_paren)) {
5970 return !Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
5971 TT_TemplateCloser);
5972 }
5973 // Avoid to break between the value and its suffix part.
5974 if (Left.is(TT_TableGenValueSuffix))
5975 return false;
5976 // Avoid to break around paste operator.
5977 if (Left.is(tok::hash) || Right.is(tok::hash))
5978 return false;
5979 if (Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
5980 return false;
5981 }
5982
5983 if (Left.is(tok::at))
5984 return false;
5985 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
5986 return false;
5987 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
5988 return Right.isNot(tok::l_paren);
5989 if (Right.is(TT_PointerOrReference)) {
5990 return Line.IsMultiVariableDeclStmt ||
5991 (getTokenPointerOrReferenceAlignment(Right) ==
5993 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
5994 }
5995 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
5996 Right.is(tok::kw_operator)) {
5997 return true;
5998 }
5999 if (Left.is(TT_PointerOrReference))
6000 return false;
6001 if (Right.isTrailingComment()) {
6002 // We rely on MustBreakBefore being set correctly here as we should not
6003 // change the "binding" behavior of a comment.
6004 // The first comment in a braced lists is always interpreted as belonging to
6005 // the first list element. Otherwise, it should be placed outside of the
6006 // list.
6007 return Left.is(BK_BracedInit) ||
6008 (Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 &&
6009 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6010 }
6011 if (Left.is(tok::question) && Right.is(tok::colon))
6012 return false;
6013 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
6014 return Style.BreakBeforeTernaryOperators;
6015 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
6016 return !Style.BreakBeforeTernaryOperators;
6017 if (Left.is(TT_InheritanceColon))
6018 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6019 if (Right.is(TT_InheritanceColon))
6020 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6021 if (Right.is(TT_ObjCMethodExpr) && Right.isNot(tok::r_square) &&
6022 Left.isNot(TT_SelectorName)) {
6023 return true;
6024 }
6025
6026 if (Right.is(tok::colon) &&
6027 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) {
6028 return false;
6029 }
6030 if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
6031 if (Style.isProto()) {
6032 if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
6033 return false;
6034 // Prevent cases like:
6035 //
6036 // submessage:
6037 // { key: valueeeeeeeeeeee }
6038 //
6039 // when the snippet does not fit into one line.
6040 // Prefer:
6041 //
6042 // submessage: {
6043 // key: valueeeeeeeeeeee
6044 // }
6045 //
6046 // instead, even if it is longer by one line.
6047 //
6048 // Note that this allows the "{" to go over the column limit
6049 // when the column limit is just between ":" and "{", but that does
6050 // not happen too often and alternative formattings in this case are
6051 // not much better.
6052 //
6053 // The code covers the cases:
6054 //
6055 // submessage: { ... }
6056 // submessage: < ... >
6057 // repeated: [ ... ]
6058 if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
6059 Right.is(TT_DictLiteral)) ||
6060 Right.is(TT_ArrayInitializerLSquare)) {
6061 return false;
6062 }
6063 }
6064 return true;
6065 }
6066 if (Right.is(tok::r_square) && Right.MatchingParen &&
6067 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6068 return false;
6069 }
6070 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
6071 Right.Next->is(TT_ObjCMethodExpr))) {
6072 return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
6073 }
6074 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
6075 return true;
6076 if (Right.is(tok::kw_concept))
6077 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6078 if (Right.is(TT_RequiresClause))
6079 return true;
6080 if (Left.ClosesTemplateDeclaration) {
6081 return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6082 Right.NewlinesBefore > 0;
6083 }
6084 if (Left.is(TT_FunctionAnnotationRParen))
6085 return true;
6086 if (Left.ClosesRequiresClause)
6087 return true;
6088 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6089 TT_OverloadedOperator)) {
6090 return false;
6091 }
6092 if (Left.is(TT_RangeBasedForLoopColon))
6093 return true;
6094 if (Right.is(TT_RangeBasedForLoopColon))
6095 return false;
6096 if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
6097 return true;
6098 if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
6099 (Left.is(tok::less) && Right.is(tok::less))) {
6100 return false;
6101 }
6102 if (Right.is(TT_BinaryOperator) &&
6103 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6104 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6105 Right.getPrecedence() != prec::Assignment)) {
6106 return true;
6107 }
6108 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
6109 Left.is(tok::kw_operator)) {
6110 return false;
6111 }
6112 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
6113 Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) {
6114 return false;
6115 }
6116 if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
6117 !Style.Cpp11BracedListStyle) {
6118 return false;
6119 }
6120 if (Left.is(TT_AttributeLParen) ||
6121 (Left.is(tok::l_paren) && Left.is(TT_TypeDeclarationParen))) {
6122 return false;
6123 }
6124 if (Left.is(tok::l_paren) && Left.Previous &&
6125 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6126 return false;
6127 }
6128 if (Right.is(TT_ImplicitStringLiteral))
6129 return false;
6130
6131 if (Right.is(TT_TemplateCloser))
6132 return false;
6133 if (Right.is(tok::r_square) && Right.MatchingParen &&
6134 Right.MatchingParen->is(TT_LambdaLSquare)) {
6135 return false;
6136 }
6137
6138 // We only break before r_brace if there was a corresponding break before
6139 // the l_brace, which is tracked by BreakBeforeClosingBrace.
6140 if (Right.is(tok::r_brace)) {
6141 return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) ||
6142 (Right.isBlockIndentedInitRBrace(Style)));
6143 }
6144
6145 // We only break before r_paren if we're in a block indented context.
6146 if (Right.is(tok::r_paren)) {
6147 if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
6148 !Right.MatchingParen) {
6149 return false;
6150 }
6151 auto Next = Right.Next;
6152 if (Next && Next->is(tok::r_paren))
6153 Next = Next->Next;
6154 if (Next && Next->is(tok::l_paren))
6155 return false;
6156 const FormatToken *Previous = Right.MatchingParen->Previous;
6157 return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
6158 }
6159
6160 // Allow breaking after a trailing annotation, e.g. after a method
6161 // declaration.
6162 if (Left.is(TT_TrailingAnnotation)) {
6163 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6164 tok::less, tok::coloncolon);
6165 }
6166
6167 if (Right.isAttribute())
6168 return true;
6169
6170 if (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))
6171 return Left.isNot(TT_AttributeSquare);
6172
6173 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
6174 return true;
6175
6176 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
6177 return true;
6178
6179 if (Left.is(TT_CtorInitializerColon)) {
6180 return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6181 (!Right.isTrailingComment() || Right.NewlinesBefore > 0);
6182 }
6183 if (Right.is(TT_CtorInitializerColon))
6184 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
6185 if (Left.is(TT_CtorInitializerComma) &&
6186 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6187 return false;
6188 }
6189 if (Right.is(TT_CtorInitializerComma) &&
6190 Style.BreakConstructorInitializers ==