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