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