xref: /freebsd-src/contrib/llvm-project/clang/lib/Format/TokenAnnotator.cpp (revision 52418fc2be8efa5172b90a3a9e617017173612c4)
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.is(TT_ConditionalExpr))
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_TrailingReturnArrow)) {
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->Previous && Tok->Previous->is(tok::kw_noexcept))
1773         Tok->setType(TT_TrailingReturnArrow);
1774       break;
1775     case tok::equal:
1776       // In TableGen, there must be a value after "=";
1777       if (Style.isTableGen() && !parseTableGenValue())
1778         return false;
1779       break;
1780     default:
1781       break;
1782     }
1783     return true;
1784   }
1785 
1786   void parseCSharpGenericTypeConstraint() {
1787     int OpenAngleBracketsCount = 0;
1788     while (CurrentToken) {
1789       if (CurrentToken->is(tok::less)) {
1790         // parseAngle is too greedy and will consume the whole line.
1791         CurrentToken->setType(TT_TemplateOpener);
1792         ++OpenAngleBracketsCount;
1793         next();
1794       } else if (CurrentToken->is(tok::greater)) {
1795         CurrentToken->setType(TT_TemplateCloser);
1796         --OpenAngleBracketsCount;
1797         next();
1798       } else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1799         // We allow line breaks after GenericTypeConstraintComma's
1800         // so do not flag commas in Generics as GenericTypeConstraintComma's.
1801         CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1802         next();
1803       } else if (CurrentToken->is(Keywords.kw_where)) {
1804         CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1805         next();
1806       } else if (CurrentToken->is(tok::colon)) {
1807         CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1808         next();
1809       } else {
1810         next();
1811       }
1812     }
1813   }
1814 
1815   void parseIncludeDirective() {
1816     if (CurrentToken && CurrentToken->is(tok::less)) {
1817       next();
1818       while (CurrentToken) {
1819         // Mark tokens up to the trailing line comments as implicit string
1820         // literals.
1821         if (CurrentToken->isNot(tok::comment) &&
1822             !CurrentToken->TokenText.starts_with("//")) {
1823           CurrentToken->setType(TT_ImplicitStringLiteral);
1824         }
1825         next();
1826       }
1827     }
1828   }
1829 
1830   void parseWarningOrError() {
1831     next();
1832     // We still want to format the whitespace left of the first token of the
1833     // warning or error.
1834     next();
1835     while (CurrentToken) {
1836       CurrentToken->setType(TT_ImplicitStringLiteral);
1837       next();
1838     }
1839   }
1840 
1841   void parsePragma() {
1842     next(); // Consume "pragma".
1843     if (CurrentToken &&
1844         CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1845                               Keywords.kw_region)) {
1846       bool IsMarkOrRegion =
1847           CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1848       next();
1849       next(); // Consume first token (so we fix leading whitespace).
1850       while (CurrentToken) {
1851         if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1852           CurrentToken->setType(TT_ImplicitStringLiteral);
1853         next();
1854       }
1855     }
1856   }
1857 
1858   void parseHasInclude() {
1859     if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1860       return;
1861     next(); // '('
1862     parseIncludeDirective();
1863     next(); // ')'
1864   }
1865 
1866   LineType parsePreprocessorDirective() {
1867     bool IsFirstToken = CurrentToken->IsFirst;
1868     LineType Type = LT_PreprocessorDirective;
1869     next();
1870     if (!CurrentToken)
1871       return Type;
1872 
1873     if (Style.isJavaScript() && IsFirstToken) {
1874       // JavaScript files can contain shebang lines of the form:
1875       // #!/usr/bin/env node
1876       // Treat these like C++ #include directives.
1877       while (CurrentToken) {
1878         // Tokens cannot be comments here.
1879         CurrentToken->setType(TT_ImplicitStringLiteral);
1880         next();
1881       }
1882       return LT_ImportStatement;
1883     }
1884 
1885     if (CurrentToken->is(tok::numeric_constant)) {
1886       CurrentToken->SpacesRequiredBefore = 1;
1887       return Type;
1888     }
1889     // Hashes in the middle of a line can lead to any strange token
1890     // sequence.
1891     if (!CurrentToken->Tok.getIdentifierInfo())
1892       return Type;
1893     // In Verilog macro expansions start with a backtick just like preprocessor
1894     // directives. Thus we stop if the word is not a preprocessor directive.
1895     if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1896       return LT_Invalid;
1897     switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1898     case tok::pp_include:
1899     case tok::pp_include_next:
1900     case tok::pp_import:
1901       next();
1902       parseIncludeDirective();
1903       Type = LT_ImportStatement;
1904       break;
1905     case tok::pp_error:
1906     case tok::pp_warning:
1907       parseWarningOrError();
1908       break;
1909     case tok::pp_pragma:
1910       parsePragma();
1911       break;
1912     case tok::pp_if:
1913     case tok::pp_elif:
1914       Contexts.back().IsExpression = true;
1915       next();
1916       if (CurrentToken)
1917         CurrentToken->SpacesRequiredBefore = true;
1918       parseLine();
1919       break;
1920     default:
1921       break;
1922     }
1923     while (CurrentToken) {
1924       FormatToken *Tok = CurrentToken;
1925       next();
1926       if (Tok->is(tok::l_paren)) {
1927         parseParens();
1928       } else if (Tok->isOneOf(Keywords.kw___has_include,
1929                               Keywords.kw___has_include_next)) {
1930         parseHasInclude();
1931       }
1932     }
1933     return Type;
1934   }
1935 
1936 public:
1937   LineType parseLine() {
1938     if (!CurrentToken)
1939       return LT_Invalid;
1940     NonTemplateLess.clear();
1941     if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1942       // We were not yet allowed to use C++17 optional when this was being
1943       // written. So we used LT_Invalid to mark that the line is not a
1944       // preprocessor directive.
1945       auto Type = parsePreprocessorDirective();
1946       if (Type != LT_Invalid)
1947         return Type;
1948     }
1949 
1950     // Directly allow to 'import <string-literal>' to support protocol buffer
1951     // definitions (github.com/google/protobuf) or missing "#" (either way we
1952     // should not break the line).
1953     IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1954     if ((Style.Language == FormatStyle::LK_Java &&
1955          CurrentToken->is(Keywords.kw_package)) ||
1956         (!Style.isVerilog() && Info &&
1957          Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1958          CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1959                                      tok::kw_static))) {
1960       next();
1961       parseIncludeDirective();
1962       return LT_ImportStatement;
1963     }
1964 
1965     // If this line starts and ends in '<' and '>', respectively, it is likely
1966     // part of "#define <a/b.h>".
1967     if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1968       parseIncludeDirective();
1969       return LT_ImportStatement;
1970     }
1971 
1972     // In .proto files, top-level options and package statements are very
1973     // similar to import statements and should not be line-wrapped.
1974     if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
1975         CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1976       next();
1977       if (CurrentToken && CurrentToken->is(tok::identifier)) {
1978         while (CurrentToken)
1979           next();
1980         return LT_ImportStatement;
1981       }
1982     }
1983 
1984     bool KeywordVirtualFound = false;
1985     bool ImportStatement = false;
1986 
1987     // import {...} from '...';
1988     if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
1989       ImportStatement = true;
1990 
1991     while (CurrentToken) {
1992       if (CurrentToken->is(tok::kw_virtual))
1993         KeywordVirtualFound = true;
1994       if (Style.isJavaScript()) {
1995         // export {...} from '...';
1996         // An export followed by "from 'some string';" is a re-export from
1997         // another module identified by a URI and is treated as a
1998         // LT_ImportStatement (i.e. prevent wraps on it for long URIs).
1999         // Just "export {...};" or "export class ..." should not be treated as
2000         // an import in this sense.
2001         if (Line.First->is(tok::kw_export) &&
2002             CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
2003             CurrentToken->Next->isStringLiteral()) {
2004           ImportStatement = true;
2005         }
2006         if (isClosureImportStatement(*CurrentToken))
2007           ImportStatement = true;
2008       }
2009       if (!consumeToken())
2010         return LT_Invalid;
2011     }
2012     if (Line.Type == LT_AccessModifier)
2013       return LT_AccessModifier;
2014     if (KeywordVirtualFound)
2015       return LT_VirtualFunctionDecl;
2016     if (ImportStatement)
2017       return LT_ImportStatement;
2018 
2019     if (Line.startsWith(TT_ObjCMethodSpecifier)) {
2020       if (Contexts.back().FirstObjCSelectorName) {
2021         Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
2022             Contexts.back().LongestObjCSelectorName;
2023       }
2024       return LT_ObjCMethodDecl;
2025     }
2026 
2027     for (const auto &ctx : Contexts)
2028       if (ctx.ContextType == Context::StructArrayInitializer)
2029         return LT_ArrayOfStructInitializer;
2030 
2031     return LT_Other;
2032   }
2033 
2034 private:
2035   bool isClosureImportStatement(const FormatToken &Tok) {
2036     // FIXME: Closure-library specific stuff should not be hard-coded but be
2037     // configurable.
2038     return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
2039            Tok.Next->Next &&
2040            (Tok.Next->Next->TokenText == "module" ||
2041             Tok.Next->Next->TokenText == "provide" ||
2042             Tok.Next->Next->TokenText == "require" ||
2043             Tok.Next->Next->TokenText == "requireType" ||
2044             Tok.Next->Next->TokenText == "forwardDeclare") &&
2045            Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
2046   }
2047 
2048   void resetTokenMetadata() {
2049     if (!CurrentToken)
2050       return;
2051 
2052     // Reset token type in case we have already looked at it and then
2053     // recovered from an error (e.g. failure to find the matching >).
2054     if (!CurrentToken->isTypeFinalized() &&
2055         !CurrentToken->isOneOf(
2056             TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
2057             TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
2058             TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
2059             TT_NamespaceMacro, TT_OverloadedOperator, TT_RegexLiteral,
2060             TT_TemplateString, TT_ObjCStringLiteral, TT_UntouchableMacroFunc,
2061             TT_StatementAttributeLikeMacro, TT_FunctionLikeOrFreestandingMacro,
2062             TT_ClassLBrace, TT_EnumLBrace, TT_RecordLBrace, TT_StructLBrace,
2063             TT_UnionLBrace, TT_RequiresClause,
2064             TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
2065             TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
2066             TT_BracedListLBrace)) {
2067       CurrentToken->setType(TT_Unknown);
2068     }
2069     CurrentToken->Role.reset();
2070     CurrentToken->MatchingParen = nullptr;
2071     CurrentToken->FakeLParens.clear();
2072     CurrentToken->FakeRParens = 0;
2073   }
2074 
2075   void next() {
2076     if (!CurrentToken)
2077       return;
2078 
2079     CurrentToken->NestingLevel = Contexts.size() - 1;
2080     CurrentToken->BindingStrength = Contexts.back().BindingStrength;
2081     modifyContext(*CurrentToken);
2082     determineTokenType(*CurrentToken);
2083     CurrentToken = CurrentToken->Next;
2084 
2085     resetTokenMetadata();
2086   }
2087 
2088   /// A struct to hold information valid in a specific context, e.g.
2089   /// a pair of parenthesis.
2090   struct Context {
2091     Context(tok::TokenKind ContextKind, unsigned BindingStrength,
2092             bool IsExpression)
2093         : ContextKind(ContextKind), BindingStrength(BindingStrength),
2094           IsExpression(IsExpression) {}
2095 
2096     tok::TokenKind ContextKind;
2097     unsigned BindingStrength;
2098     bool IsExpression;
2099     unsigned LongestObjCSelectorName = 0;
2100     bool ColonIsForRangeExpr = false;
2101     bool ColonIsDictLiteral = false;
2102     bool ColonIsObjCMethodExpr = false;
2103     FormatToken *FirstObjCSelectorName = nullptr;
2104     FormatToken *FirstStartOfName = nullptr;
2105     bool CanBeExpression = true;
2106     bool CaretFound = false;
2107     bool InCpp11AttributeSpecifier = false;
2108     bool InCSharpAttributeSpecifier = false;
2109     bool VerilogAssignmentFound = false;
2110     // Whether the braces may mean concatenation instead of structure or array
2111     // literal.
2112     bool VerilogMayBeConcatenation = false;
2113     bool IsTableGenDAGArg = false;
2114     bool IsTableGenBangOpe = false;
2115     bool IsTableGenCondOpe = false;
2116     enum {
2117       Unknown,
2118       // Like the part after `:` in a constructor.
2119       //   Context(...) : IsExpression(IsExpression)
2120       CtorInitializer,
2121       // Like in the parentheses in a foreach.
2122       ForEachMacro,
2123       // Like the inheritance list in a class declaration.
2124       //   class Input : public IO
2125       InheritanceList,
2126       // Like in the braced list.
2127       //   int x[] = {};
2128       StructArrayInitializer,
2129       // Like in `static_cast<int>`.
2130       TemplateArgument,
2131       // C11 _Generic selection.
2132       C11GenericSelection,
2133       // Like in the outer parentheses in `ffnand ff1(.q());`.
2134       VerilogInstancePortList,
2135     } ContextType = Unknown;
2136   };
2137 
2138   /// Puts a new \c Context onto the stack \c Contexts for the lifetime
2139   /// of each instance.
2140   struct ScopedContextCreator {
2141     AnnotatingParser &P;
2142 
2143     ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
2144                          unsigned Increase)
2145         : P(P) {
2146       P.Contexts.push_back(Context(ContextKind,
2147                                    P.Contexts.back().BindingStrength + Increase,
2148                                    P.Contexts.back().IsExpression));
2149     }
2150 
2151     ~ScopedContextCreator() {
2152       if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
2153         if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
2154           P.Contexts.pop_back();
2155           P.Contexts.back().ContextType = Context::StructArrayInitializer;
2156           return;
2157         }
2158       }
2159       P.Contexts.pop_back();
2160     }
2161   };
2162 
2163   void modifyContext(const FormatToken &Current) {
2164     auto AssignmentStartsExpression = [&]() {
2165       if (Current.getPrecedence() != prec::Assignment)
2166         return false;
2167 
2168       if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
2169         return false;
2170       if (Line.First->is(tok::kw_template)) {
2171         assert(Current.Previous);
2172         if (Current.Previous->is(tok::kw_operator)) {
2173           // `template ... operator=` cannot be an expression.
2174           return false;
2175         }
2176 
2177         // `template` keyword can start a variable template.
2178         const FormatToken *Tok = Line.First->getNextNonComment();
2179         assert(Tok); // Current token is on the same line.
2180         if (Tok->isNot(TT_TemplateOpener)) {
2181           // Explicit template instantiations do not have `<>`.
2182           return false;
2183         }
2184 
2185         // This is the default value of a template parameter, determine if it's
2186         // type or non-type.
2187         if (Contexts.back().ContextKind == tok::less) {
2188           assert(Current.Previous->Previous);
2189           return !Current.Previous->Previous->isOneOf(tok::kw_typename,
2190                                                       tok::kw_class);
2191         }
2192 
2193         Tok = Tok->MatchingParen;
2194         if (!Tok)
2195           return false;
2196         Tok = Tok->getNextNonComment();
2197         if (!Tok)
2198           return false;
2199 
2200         if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
2201                          tok::kw_using)) {
2202           return false;
2203         }
2204 
2205         return true;
2206       }
2207 
2208       // Type aliases use `type X = ...;` in TypeScript and can be exported
2209       // using `export type ...`.
2210       if (Style.isJavaScript() &&
2211           (Line.startsWith(Keywords.kw_type, tok::identifier) ||
2212            Line.startsWith(tok::kw_export, Keywords.kw_type,
2213                            tok::identifier))) {
2214         return false;
2215       }
2216 
2217       return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
2218     };
2219 
2220     if (AssignmentStartsExpression()) {
2221       Contexts.back().IsExpression = true;
2222       if (!Line.startsWith(TT_UnaryOperator)) {
2223         for (FormatToken *Previous = Current.Previous;
2224              Previous && Previous->Previous &&
2225              !Previous->Previous->isOneOf(tok::comma, tok::semi);
2226              Previous = Previous->Previous) {
2227           if (Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
2228             Previous = Previous->MatchingParen;
2229             if (!Previous)
2230               break;
2231           }
2232           if (Previous->opensScope())
2233             break;
2234           if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
2235               Previous->isPointerOrReference() && Previous->Previous &&
2236               Previous->Previous->isNot(tok::equal)) {
2237             Previous->setType(TT_PointerOrReference);
2238           }
2239         }
2240       }
2241     } else if (Current.is(tok::lessless) &&
2242                (!Current.Previous ||
2243                 Current.Previous->isNot(tok::kw_operator))) {
2244       Contexts.back().IsExpression = true;
2245     } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
2246       Contexts.back().IsExpression = true;
2247     } else if (Current.is(TT_TrailingReturnArrow)) {
2248       Contexts.back().IsExpression = false;
2249     } else if (Current.is(Keywords.kw_assert)) {
2250       Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
2251     } else if (Current.Previous &&
2252                Current.Previous->is(TT_CtorInitializerColon)) {
2253       Contexts.back().IsExpression = true;
2254       Contexts.back().ContextType = Context::CtorInitializer;
2255     } else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
2256       Contexts.back().ContextType = Context::InheritanceList;
2257     } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
2258       for (FormatToken *Previous = Current.Previous;
2259            Previous && Previous->isOneOf(tok::star, tok::amp);
2260            Previous = Previous->Previous) {
2261         Previous->setType(TT_PointerOrReference);
2262       }
2263       if (Line.MustBeDeclaration &&
2264           Contexts.front().ContextType != Context::CtorInitializer) {
2265         Contexts.back().IsExpression = false;
2266       }
2267     } else if (Current.is(tok::kw_new)) {
2268       Contexts.back().CanBeExpression = false;
2269     } else if (Current.is(tok::semi) ||
2270                (Current.is(tok::exclaim) && Current.Previous &&
2271                 Current.Previous->isNot(tok::kw_operator))) {
2272       // This should be the condition or increment in a for-loop.
2273       // But not operator !() (can't use TT_OverloadedOperator here as its not
2274       // been annotated yet).
2275       Contexts.back().IsExpression = true;
2276     }
2277   }
2278 
2279   static FormatToken *untilMatchingParen(FormatToken *Current) {
2280     // Used when `MatchingParen` is not yet established.
2281     int ParenLevel = 0;
2282     while (Current) {
2283       if (Current->is(tok::l_paren))
2284         ++ParenLevel;
2285       if (Current->is(tok::r_paren))
2286         --ParenLevel;
2287       if (ParenLevel < 1)
2288         break;
2289       Current = Current->Next;
2290     }
2291     return Current;
2292   }
2293 
2294   static bool isDeductionGuide(FormatToken &Current) {
2295     // Look for a deduction guide template<T> A(...) -> A<...>;
2296     if (Current.Previous && Current.Previous->is(tok::r_paren) &&
2297         Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
2298       // Find the TemplateCloser.
2299       FormatToken *TemplateCloser = Current.Next->Next;
2300       int NestingLevel = 0;
2301       while (TemplateCloser) {
2302         // Skip over an expressions in parens  A<(3 < 2)>;
2303         if (TemplateCloser->is(tok::l_paren)) {
2304           // No Matching Paren yet so skip to matching paren
2305           TemplateCloser = untilMatchingParen(TemplateCloser);
2306           if (!TemplateCloser)
2307             break;
2308         }
2309         if (TemplateCloser->is(tok::less))
2310           ++NestingLevel;
2311         if (TemplateCloser->is(tok::greater))
2312           --NestingLevel;
2313         if (NestingLevel < 1)
2314           break;
2315         TemplateCloser = TemplateCloser->Next;
2316       }
2317       // Assuming we have found the end of the template ensure its followed
2318       // with a semi-colon.
2319       if (TemplateCloser && TemplateCloser->Next &&
2320           TemplateCloser->Next->is(tok::semi) &&
2321           Current.Previous->MatchingParen) {
2322         // Determine if the identifier `A` prior to the A<..>; is the same as
2323         // prior to the A(..)
2324         FormatToken *LeadingIdentifier =
2325             Current.Previous->MatchingParen->Previous;
2326 
2327         return LeadingIdentifier &&
2328                LeadingIdentifier->TokenText == Current.Next->TokenText;
2329       }
2330     }
2331     return false;
2332   }
2333 
2334   void determineTokenType(FormatToken &Current) {
2335     if (Current.isNot(TT_Unknown)) {
2336       // The token type is already known.
2337       return;
2338     }
2339 
2340     if ((Style.isJavaScript() || Style.isCSharp()) &&
2341         Current.is(tok::exclaim)) {
2342       if (Current.Previous) {
2343         bool IsIdentifier =
2344             Style.isJavaScript()
2345                 ? Keywords.isJavaScriptIdentifier(
2346                       *Current.Previous, /* AcceptIdentifierName= */ true)
2347                 : Current.Previous->is(tok::identifier);
2348         if (IsIdentifier ||
2349             Current.Previous->isOneOf(
2350                 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
2351                 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
2352                 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
2353             Current.Previous->Tok.isLiteral()) {
2354           Current.setType(TT_NonNullAssertion);
2355           return;
2356         }
2357       }
2358       if (Current.Next &&
2359           Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
2360         Current.setType(TT_NonNullAssertion);
2361         return;
2362       }
2363     }
2364 
2365     // Line.MightBeFunctionDecl can only be true after the parentheses of a
2366     // function declaration have been found. In this case, 'Current' is a
2367     // trailing token of this declaration and thus cannot be a name.
2368     if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) &&
2369         Current.is(Keywords.kw_instanceof)) {
2370       Current.setType(TT_BinaryOperator);
2371     } else if (isStartOfName(Current) &&
2372                (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2373       Contexts.back().FirstStartOfName = &Current;
2374       Current.setType(TT_StartOfName);
2375     } else if (Current.is(tok::semi)) {
2376       // Reset FirstStartOfName after finding a semicolon so that a for loop
2377       // with multiple increment statements is not confused with a for loop
2378       // having multiple variable declarations.
2379       Contexts.back().FirstStartOfName = nullptr;
2380     } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2381       AutoFound = true;
2382     } else if (Current.is(tok::arrow) &&
2383                Style.Language == FormatStyle::LK_Java) {
2384       Current.setType(TT_TrailingReturnArrow);
2385     } else if (Current.is(tok::arrow) && Style.isVerilog()) {
2386       // The implication operator.
2387       Current.setType(TT_BinaryOperator);
2388     } else if (Current.is(tok::arrow) && AutoFound &&
2389                Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2390                !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
2391       // not auto operator->() -> xxx;
2392       Current.setType(TT_TrailingReturnArrow);
2393     } else if (Current.is(tok::arrow) && Current.Previous &&
2394                Current.Previous->is(tok::r_brace)) {
2395       // Concept implicit conversion constraint needs to be treated like
2396       // a trailing return type  ... } -> <type>.
2397       Current.setType(TT_TrailingReturnArrow);
2398     } else if (isDeductionGuide(Current)) {
2399       // Deduction guides trailing arrow " A(...) -> A<T>;".
2400       Current.setType(TT_TrailingReturnArrow);
2401     } else if (Current.isPointerOrReference()) {
2402       Current.setType(determineStarAmpUsage(
2403           Current,
2404           Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2405           Contexts.back().ContextType == Context::TemplateArgument));
2406     } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2407                (Style.isVerilog() && Current.is(tok::pipe))) {
2408       Current.setType(determinePlusMinusCaretUsage(Current));
2409       if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2410         Contexts.back().CaretFound = true;
2411     } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2412       Current.setType(determineIncrementUsage(Current));
2413     } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2414       Current.setType(TT_UnaryOperator);
2415     } else if (Current.is(tok::question)) {
2416       if (Style.isJavaScript() && Line.MustBeDeclaration &&
2417           !Contexts.back().IsExpression) {
2418         // In JavaScript, `interface X { foo?(): bar; }` is an optional method
2419         // on the interface, not a ternary expression.
2420         Current.setType(TT_JsTypeOptionalQuestion);
2421       } else if (Style.isTableGen()) {
2422         // In TableGen, '?' is just an identifier like token.
2423         Current.setType(TT_Unknown);
2424       } else {
2425         Current.setType(TT_ConditionalExpr);
2426       }
2427     } else if (Current.isBinaryOperator() &&
2428                (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2429                (Current.isNot(tok::greater) &&
2430                 Style.Language != FormatStyle::LK_TextProto)) {
2431       if (Style.isVerilog()) {
2432         if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2433             !Contexts.back().VerilogAssignmentFound) {
2434           // In Verilog `<=` is assignment if in its own statement. It is a
2435           // statement instead of an expression, that is it can not be chained.
2436           Current.ForcedPrecedence = prec::Assignment;
2437           Current.setFinalizedType(TT_BinaryOperator);
2438         }
2439         if (Current.getPrecedence() == prec::Assignment)
2440           Contexts.back().VerilogAssignmentFound = true;
2441       }
2442       Current.setType(TT_BinaryOperator);
2443     } else if (Current.is(tok::comment)) {
2444       if (Current.TokenText.starts_with("/*")) {
2445         if (Current.TokenText.ends_with("*/")) {
2446           Current.setType(TT_BlockComment);
2447         } else {
2448           // The lexer has for some reason determined a comment here. But we
2449           // cannot really handle it, if it isn't properly terminated.
2450           Current.Tok.setKind(tok::unknown);
2451         }
2452       } else {
2453         Current.setType(TT_LineComment);
2454       }
2455     } else if (Current.is(tok::string_literal)) {
2456       if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2457           Current.getPreviousNonComment() &&
2458           Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2459           Current.getNextNonComment() &&
2460           Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2461         Current.setType(TT_StringInConcatenation);
2462       }
2463     } else if (Current.is(tok::l_paren)) {
2464       if (lParenStartsCppCast(Current))
2465         Current.setType(TT_CppCastLParen);
2466     } else if (Current.is(tok::r_paren)) {
2467       if (rParenEndsCast(Current))
2468         Current.setType(TT_CastRParen);
2469       if (Current.MatchingParen && Current.Next &&
2470           !Current.Next->isBinaryOperator() &&
2471           !Current.Next->isOneOf(
2472               tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
2473               tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
2474         if (FormatToken *AfterParen = Current.MatchingParen->Next;
2475             AfterParen && AfterParen->isNot(tok::caret)) {
2476           // Make sure this isn't the return type of an Obj-C block declaration.
2477           if (FormatToken *BeforeParen = Current.MatchingParen->Previous;
2478               BeforeParen && BeforeParen->is(tok::identifier) &&
2479               BeforeParen->isNot(TT_TypenameMacro) &&
2480               BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2481               (!BeforeParen->Previous ||
2482                BeforeParen->Previous->ClosesTemplateDeclaration ||
2483                BeforeParen->Previous->ClosesRequiresClause)) {
2484             Current.setType(TT_FunctionAnnotationRParen);
2485           }
2486         }
2487       }
2488     } else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2489                Style.Language != FormatStyle::LK_Java) {
2490       // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
2491       // marks declarations and properties that need special formatting.
2492       switch (Current.Next->Tok.getObjCKeywordID()) {
2493       case tok::objc_interface:
2494       case tok::objc_implementation:
2495       case tok::objc_protocol:
2496         Current.setType(TT_ObjCDecl);
2497         break;
2498       case tok::objc_property:
2499         Current.setType(TT_ObjCProperty);
2500         break;
2501       default:
2502         break;
2503       }
2504     } else if (Current.is(tok::period)) {
2505       FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2506       if (PreviousNoComment &&
2507           PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2508         Current.setType(TT_DesignatedInitializerPeriod);
2509       } else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
2510                  Current.Previous->isOneOf(TT_JavaAnnotation,
2511                                            TT_LeadingJavaAnnotation)) {
2512         Current.setType(Current.Previous->getType());
2513       }
2514     } else if (canBeObjCSelectorComponent(Current) &&
2515                // FIXME(bug 36976): ObjC return types shouldn't use
2516                // TT_CastRParen.
2517                Current.Previous && Current.Previous->is(TT_CastRParen) &&
2518                Current.Previous->MatchingParen &&
2519                Current.Previous->MatchingParen->Previous &&
2520                Current.Previous->MatchingParen->Previous->is(
2521                    TT_ObjCMethodSpecifier)) {
2522       // This is the first part of an Objective-C selector name. (If there's no
2523       // colon after this, this is the only place which annotates the identifier
2524       // as a selector.)
2525       Current.setType(TT_SelectorName);
2526     } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2527                                tok::kw_requires) &&
2528                Current.Previous &&
2529                !Current.Previous->isOneOf(tok::equal, tok::at,
2530                                           TT_CtorInitializerComma,
2531                                           TT_CtorInitializerColon) &&
2532                Line.MightBeFunctionDecl && Contexts.size() == 1) {
2533       // Line.MightBeFunctionDecl can only be true after the parentheses of a
2534       // function declaration have been found.
2535       Current.setType(TT_TrailingAnnotation);
2536     } else if ((Style.Language == FormatStyle::LK_Java ||
2537                 Style.isJavaScript()) &&
2538                Current.Previous) {
2539       if (Current.Previous->is(tok::at) &&
2540           Current.isNot(Keywords.kw_interface)) {
2541         const FormatToken &AtToken = *Current.Previous;
2542         const FormatToken *Previous = AtToken.getPreviousNonComment();
2543         if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
2544           Current.setType(TT_LeadingJavaAnnotation);
2545         else
2546           Current.setType(TT_JavaAnnotation);
2547       } else if (Current.Previous->is(tok::period) &&
2548                  Current.Previous->isOneOf(TT_JavaAnnotation,
2549                                            TT_LeadingJavaAnnotation)) {
2550         Current.setType(Current.Previous->getType());
2551       }
2552     }
2553   }
2554 
2555   /// Take a guess at whether \p Tok starts a name of a function or
2556   /// variable declaration.
2557   ///
2558   /// This is a heuristic based on whether \p Tok is an identifier following
2559   /// something that is likely a type.
2560   bool isStartOfName(const FormatToken &Tok) {
2561     // Handled in ExpressionParser for Verilog.
2562     if (Style.isVerilog())
2563       return false;
2564 
2565     if (Tok.isNot(tok::identifier) || !Tok.Previous)
2566       return false;
2567 
2568     if (const auto *NextNonComment = Tok.getNextNonComment();
2569         (!NextNonComment && !Line.InMacroBody) ||
2570         (NextNonComment &&
2571          (NextNonComment->isPointerOrReference() ||
2572           NextNonComment->is(tok::string_literal) ||
2573           (Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
2574       return false;
2575     }
2576 
2577     if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2578                               Keywords.kw_as)) {
2579       return false;
2580     }
2581     if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2582       return false;
2583 
2584     // Skip "const" as it does not have an influence on whether this is a name.
2585     FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2586 
2587     // For javascript const can be like "let" or "var"
2588     if (!Style.isJavaScript())
2589       while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2590         PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2591 
2592     if (!PreviousNotConst)
2593       return false;
2594 
2595     if (PreviousNotConst->ClosesRequiresClause)
2596       return false;
2597 
2598     if (Style.isTableGen()) {
2599       // keywords such as let and def* defines names.
2600       if (Keywords.isTableGenDefinition(*PreviousNotConst))
2601         return true;
2602       // Otherwise C++ style declarations is available only inside the brace.
2603       if (Contexts.back().ContextKind != tok::l_brace)
2604         return false;
2605     }
2606 
2607     bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2608                        PreviousNotConst->Previous &&
2609                        PreviousNotConst->Previous->is(tok::hash);
2610 
2611     if (PreviousNotConst->is(TT_TemplateCloser)) {
2612       return PreviousNotConst && PreviousNotConst->MatchingParen &&
2613              PreviousNotConst->MatchingParen->Previous &&
2614              PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2615              PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2616     }
2617 
2618     if ((PreviousNotConst->is(tok::r_paren) &&
2619          PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2620         PreviousNotConst->is(TT_AttributeRParen)) {
2621       return true;
2622     }
2623 
2624     // If is a preprocess keyword like #define.
2625     if (IsPPKeyword)
2626       return false;
2627 
2628     // int a or auto a.
2629     if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto) &&
2630         PreviousNotConst->isNot(TT_StatementAttributeLikeMacro)) {
2631       return true;
2632     }
2633 
2634     // *a or &a or &&a.
2635     if (PreviousNotConst->is(TT_PointerOrReference))
2636       return true;
2637 
2638     // MyClass a;
2639     if (PreviousNotConst->isTypeName(LangOpts))
2640       return true;
2641 
2642     // type[] a in Java
2643     if (Style.Language == FormatStyle::LK_Java &&
2644         PreviousNotConst->is(tok::r_square)) {
2645       return true;
2646     }
2647 
2648     // const a = in JavaScript.
2649     return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2650   }
2651 
2652   /// Determine whether '(' is starting a C++ cast.
2653   bool lParenStartsCppCast(const FormatToken &Tok) {
2654     // C-style casts are only used in C++.
2655     if (!IsCpp)
2656       return false;
2657 
2658     FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2659     if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2660         LeftOfParens->MatchingParen) {
2661       auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2662       if (Prev &&
2663           Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2664                         tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2665         // FIXME: Maybe we should handle identifiers ending with "_cast",
2666         // e.g. any_cast?
2667         return true;
2668       }
2669     }
2670     return false;
2671   }
2672 
2673   /// Determine whether ')' is ending a cast.
2674   bool rParenEndsCast(const FormatToken &Tok) {
2675     assert(Tok.is(tok::r_paren));
2676 
2677     if (!Tok.MatchingParen || !Tok.Previous)
2678       return false;
2679 
2680     // C-style casts are only used in C++, C# and Java.
2681     if (!IsCpp && !Style.isCSharp() && Style.Language != FormatStyle::LK_Java)
2682       return false;
2683 
2684     const auto *LParen = Tok.MatchingParen;
2685     const auto *BeforeRParen = Tok.Previous;
2686     const auto *AfterRParen = Tok.Next;
2687 
2688     // Empty parens aren't casts and there are no casts at the end of the line.
2689     if (BeforeRParen == LParen || !AfterRParen)
2690       return false;
2691 
2692     if (LParen->is(TT_OverloadedOperatorLParen))
2693       return false;
2694 
2695     auto *LeftOfParens = LParen->getPreviousNonComment();
2696     if (LeftOfParens) {
2697       // If there is a closing parenthesis left of the current
2698       // parentheses, look past it as these might be chained casts.
2699       if (LeftOfParens->is(tok::r_paren) &&
2700           LeftOfParens->isNot(TT_CastRParen)) {
2701         if (!LeftOfParens->MatchingParen ||
2702             !LeftOfParens->MatchingParen->Previous) {
2703           return false;
2704         }
2705         LeftOfParens = LeftOfParens->MatchingParen->Previous;
2706       }
2707 
2708       if (LeftOfParens->is(tok::r_square)) {
2709         //   delete[] (void *)ptr;
2710         auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2711           if (Tok->isNot(tok::r_square))
2712             return nullptr;
2713 
2714           Tok = Tok->getPreviousNonComment();
2715           if (!Tok || Tok->isNot(tok::l_square))
2716             return nullptr;
2717 
2718           Tok = Tok->getPreviousNonComment();
2719           if (!Tok || Tok->isNot(tok::kw_delete))
2720             return nullptr;
2721           return Tok;
2722         };
2723         if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2724           LeftOfParens = MaybeDelete;
2725       }
2726 
2727       // The Condition directly below this one will see the operator arguments
2728       // as a (void *foo) cast.
2729       //   void operator delete(void *foo) ATTRIB;
2730       if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2731           LeftOfParens->Previous->is(tok::kw_operator)) {
2732         return false;
2733       }
2734 
2735       // If there is an identifier (or with a few exceptions a keyword) right
2736       // before the parentheses, this is unlikely to be a cast.
2737       if (LeftOfParens->Tok.getIdentifierInfo() &&
2738           !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2739                                  tok::kw_delete, tok::kw_throw)) {
2740         return false;
2741       }
2742 
2743       // Certain other tokens right before the parentheses are also signals that
2744       // this cannot be a cast.
2745       if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2746                                 TT_TemplateCloser, tok::ellipsis)) {
2747         return false;
2748       }
2749     }
2750 
2751     if (AfterRParen->is(tok::question) ||
2752         (AfterRParen->is(tok::ampamp) && !BeforeRParen->isTypeName(LangOpts))) {
2753       return false;
2754     }
2755 
2756     // `foreach((A a, B b) in someList)` should not be seen as a cast.
2757     if (AfterRParen->is(Keywords.kw_in) && Style.isCSharp())
2758       return false;
2759 
2760     // Functions which end with decorations like volatile, noexcept are unlikely
2761     // to be casts.
2762     if (AfterRParen->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2763                              tok::kw_requires, tok::kw_throw, tok::arrow,
2764                              Keywords.kw_override, Keywords.kw_final) ||
2765         isCppAttribute(IsCpp, *AfterRParen)) {
2766       return false;
2767     }
2768 
2769     // As Java has no function types, a "(" after the ")" likely means that this
2770     // is a cast.
2771     if (Style.Language == FormatStyle::LK_Java && AfterRParen->is(tok::l_paren))
2772       return true;
2773 
2774     // If a (non-string) literal follows, this is likely a cast.
2775     if (AfterRParen->isOneOf(tok::kw_sizeof, tok::kw_alignof) ||
2776         (AfterRParen->Tok.isLiteral() &&
2777          AfterRParen->isNot(tok::string_literal))) {
2778       return true;
2779     }
2780 
2781     // Heuristically try to determine whether the parentheses contain a type.
2782     auto IsQualifiedPointerOrReference = [](const FormatToken *T,
2783                                             const LangOptions &LangOpts) {
2784       // This is used to handle cases such as x = (foo *const)&y;
2785       assert(!T->isTypeName(LangOpts) && "Should have already been checked");
2786       // Strip trailing qualifiers such as const or volatile when checking
2787       // whether the parens could be a cast to a pointer/reference type.
2788       while (T) {
2789         if (T->is(TT_AttributeRParen)) {
2790           // Handle `x = (foo *__attribute__((foo)))&v;`:
2791           assert(T->is(tok::r_paren));
2792           assert(T->MatchingParen);
2793           assert(T->MatchingParen->is(tok::l_paren));
2794           assert(T->MatchingParen->is(TT_AttributeLParen));
2795           if (const auto *Tok = T->MatchingParen->Previous;
2796               Tok && Tok->isAttribute()) {
2797             T = Tok->Previous;
2798             continue;
2799           }
2800         } else if (T->is(TT_AttributeSquare)) {
2801           // Handle `x = (foo *[[clang::foo]])&v;`:
2802           if (T->MatchingParen && T->MatchingParen->Previous) {
2803             T = T->MatchingParen->Previous;
2804             continue;
2805           }
2806         } else if (T->canBePointerOrReferenceQualifier()) {
2807           T = T->Previous;
2808           continue;
2809         }
2810         break;
2811       }
2812       return T && T->is(TT_PointerOrReference);
2813     };
2814     bool ParensAreType =
2815         BeforeRParen->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2816         BeforeRParen->isTypeName(LangOpts) ||
2817         IsQualifiedPointerOrReference(BeforeRParen, LangOpts);
2818     bool ParensCouldEndDecl =
2819         AfterRParen->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2820     if (ParensAreType && !ParensCouldEndDecl)
2821       return true;
2822 
2823     // At this point, we heuristically assume that there are no casts at the
2824     // start of the line. We assume that we have found most cases where there
2825     // are by the logic above, e.g. "(void)x;".
2826     if (!LeftOfParens)
2827       return false;
2828 
2829     // Certain token types inside the parentheses mean that this can't be a
2830     // cast.
2831     for (const auto *Token = LParen->Next; Token != &Tok; Token = Token->Next)
2832       if (Token->is(TT_BinaryOperator))
2833         return false;
2834 
2835     // If the following token is an identifier or 'this', this is a cast. All
2836     // cases where this can be something else are handled above.
2837     if (AfterRParen->isOneOf(tok::identifier, tok::kw_this))
2838       return true;
2839 
2840     // Look for a cast `( x ) (`.
2841     if (AfterRParen->is(tok::l_paren) && BeforeRParen->Previous) {
2842       if (BeforeRParen->is(tok::identifier) &&
2843           BeforeRParen->Previous->is(tok::l_paren)) {
2844         return true;
2845       }
2846     }
2847 
2848     if (!AfterRParen->Next)
2849       return false;
2850 
2851     if (AfterRParen->is(tok::l_brace) &&
2852         AfterRParen->getBlockKind() == BK_BracedInit) {
2853       return true;
2854     }
2855 
2856     // If the next token after the parenthesis is a unary operator, assume
2857     // that this is cast, unless there are unexpected tokens inside the
2858     // parenthesis.
2859     const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star);
2860     if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) ||
2861         AfterRParen->is(tok::plus) ||
2862         !AfterRParen->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2863       return false;
2864     }
2865 
2866     if (NextIsAmpOrStar &&
2867         (AfterRParen->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2868       return false;
2869     }
2870 
2871     if (Line.InPPDirective && AfterRParen->is(tok::minus))
2872       return false;
2873 
2874     // Search for unexpected tokens.
2875     for (auto *Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
2876       if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2877         return false;
2878 
2879     return true;
2880   }
2881 
2882   /// Returns true if the token is used as a unary operator.
2883   bool determineUnaryOperatorByUsage(const FormatToken &Tok) {
2884     const FormatToken *PrevToken = Tok.getPreviousNonComment();
2885     if (!PrevToken)
2886       return true;
2887 
2888     // These keywords are deliberately not included here because they may
2889     // precede only one of unary star/amp and plus/minus but not both.  They are
2890     // either included in determineStarAmpUsage or determinePlusMinusCaretUsage.
2891     //
2892     // @ - It may be followed by a unary `-` in Objective-C literals. We don't
2893     //   know how they can be followed by a star or amp.
2894     if (PrevToken->isOneOf(
2895             TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2896             tok::equal, tok::question, tok::l_square, tok::l_brace,
2897             tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2898             tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2899       return true;
2900     }
2901 
2902     // We put sizeof here instead of only in determineStarAmpUsage. In the cases
2903     // where the unary `+` operator is overloaded, it is reasonable to write
2904     // things like `sizeof +x`. Like commit 446d6ec996c6c3.
2905     if (PrevToken->is(tok::kw_sizeof))
2906       return true;
2907 
2908     // A sequence of leading unary operators.
2909     if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2910       return true;
2911 
2912     // There can't be two consecutive binary operators.
2913     if (PrevToken->is(TT_BinaryOperator))
2914       return true;
2915 
2916     return false;
2917   }
2918 
2919   /// Return the type of the given token assuming it is * or &.
2920   TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
2921                                   bool InTemplateArgument) {
2922     if (Style.isJavaScript())
2923       return TT_BinaryOperator;
2924 
2925     // && in C# must be a binary operator.
2926     if (Style.isCSharp() && Tok.is(tok::ampamp))
2927       return TT_BinaryOperator;
2928 
2929     if (Style.isVerilog()) {
2930       // In Verilog, `*` can only be a binary operator.  `&` can be either unary
2931       // or binary.  `*` also includes `*>` in module path declarations in
2932       // specify blocks because merged tokens take the type of the first one by
2933       // default.
2934       if (Tok.is(tok::star))
2935         return TT_BinaryOperator;
2936       return determineUnaryOperatorByUsage(Tok) ? TT_UnaryOperator
2937                                                 : TT_BinaryOperator;
2938     }
2939 
2940     const FormatToken *PrevToken = Tok.getPreviousNonComment();
2941     if (!PrevToken)
2942       return TT_UnaryOperator;
2943     if (PrevToken->is(TT_TypeName))
2944       return TT_PointerOrReference;
2945     if (PrevToken->isOneOf(tok::kw_new, tok::kw_delete) && Tok.is(tok::ampamp))
2946       return TT_BinaryOperator;
2947 
2948     const FormatToken *NextToken = Tok.getNextNonComment();
2949 
2950     if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
2951       return TT_BinaryOperator;
2952 
2953     if (!NextToken ||
2954         NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
2955                            TT_RequiresClause) ||
2956         (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
2957         NextToken->canBePointerOrReferenceQualifier() ||
2958         (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
2959       return TT_PointerOrReference;
2960     }
2961 
2962     if (PrevToken->is(tok::coloncolon))
2963       return TT_PointerOrReference;
2964 
2965     if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
2966       return TT_PointerOrReference;
2967 
2968     if (determineUnaryOperatorByUsage(Tok))
2969       return TT_UnaryOperator;
2970 
2971     if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
2972       return TT_PointerOrReference;
2973     if (NextToken->is(tok::kw_operator) && !IsExpression)
2974       return TT_PointerOrReference;
2975     if (NextToken->isOneOf(tok::comma, tok::semi))
2976       return TT_PointerOrReference;
2977 
2978     // After right braces, star tokens are likely to be pointers to struct,
2979     // union, or class.
2980     //   struct {} *ptr;
2981     // This by itself is not sufficient to distinguish from multiplication
2982     // following a brace-initialized expression, as in:
2983     // int i = int{42} * 2;
2984     // In the struct case, the part of the struct declaration until the `{` and
2985     // the `}` are put on separate unwrapped lines; in the brace-initialized
2986     // case, the matching `{` is on the same unwrapped line, so check for the
2987     // presence of the matching brace to distinguish between those.
2988     if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) &&
2989         !PrevToken->MatchingParen) {
2990       return TT_PointerOrReference;
2991     }
2992 
2993     if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
2994       return TT_UnaryOperator;
2995 
2996     if (PrevToken->Tok.isLiteral() ||
2997         PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
2998                            tok::kw_false, tok::r_brace)) {
2999       return TT_BinaryOperator;
3000     }
3001 
3002     const FormatToken *NextNonParen = NextToken;
3003     while (NextNonParen && NextNonParen->is(tok::l_paren))
3004       NextNonParen = NextNonParen->getNextNonComment();
3005     if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
3006                          NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
3007                          NextNonParen->isUnaryOperator())) {
3008       return TT_BinaryOperator;
3009     }
3010 
3011     // If we know we're in a template argument, there are no named declarations.
3012     // Thus, having an identifier on the right-hand side indicates a binary
3013     // operator.
3014     if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
3015       return TT_BinaryOperator;
3016 
3017     // "&&" followed by "(", "*", or "&" is quite unlikely to be two successive
3018     // unary "&".
3019     if (Tok.is(tok::ampamp) &&
3020         NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
3021       return TT_BinaryOperator;
3022     }
3023 
3024     // This catches some cases where evaluation order is used as control flow:
3025     //   aaa && aaa->f();
3026     if (NextToken->Tok.isAnyIdentifier()) {
3027       const FormatToken *NextNextToken = NextToken->getNextNonComment();
3028       if (NextNextToken && NextNextToken->is(tok::arrow))
3029         return TT_BinaryOperator;
3030     }
3031 
3032     // It is very unlikely that we are going to find a pointer or reference type
3033     // definition on the RHS of an assignment.
3034     if (IsExpression && !Contexts.back().CaretFound)
3035       return TT_BinaryOperator;
3036 
3037     // Opeartors at class scope are likely pointer or reference members.
3038     if (!Scopes.empty() && Scopes.back() == ST_Class)
3039       return TT_PointerOrReference;
3040 
3041     // Tokens that indicate member access or chained operator& use.
3042     auto IsChainedOperatorAmpOrMember = [](const FormatToken *token) {
3043       return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
3044                                       tok::arrowstar, tok::periodstar);
3045     };
3046 
3047     // It's more likely that & represents operator& than an uninitialized
3048     // reference.
3049     if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
3050         IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
3051         NextToken && NextToken->Tok.isAnyIdentifier()) {
3052       if (auto NextNext = NextToken->getNextNonComment();
3053           NextNext &&
3054           (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
3055         return TT_BinaryOperator;
3056       }
3057     }
3058 
3059     return TT_PointerOrReference;
3060   }
3061 
3062   TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
3063     if (determineUnaryOperatorByUsage(Tok))
3064       return TT_UnaryOperator;
3065 
3066     const FormatToken *PrevToken = Tok.getPreviousNonComment();
3067     if (!PrevToken)
3068       return TT_UnaryOperator;
3069 
3070     if (PrevToken->is(tok::at))
3071       return TT_UnaryOperator;
3072 
3073     // Fall back to marking the token as binary operator.
3074     return TT_BinaryOperator;
3075   }
3076 
3077   /// Determine whether ++/-- are pre- or post-increments/-decrements.
3078   TokenType determineIncrementUsage(const FormatToken &Tok) {
3079     const FormatToken *PrevToken = Tok.getPreviousNonComment();
3080     if (!PrevToken || PrevToken->is(TT_CastRParen))
3081       return TT_UnaryOperator;
3082     if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
3083       return TT_TrailingUnaryOperator;
3084 
3085     return TT_UnaryOperator;
3086   }
3087 
3088   SmallVector<Context, 8> Contexts;
3089 
3090   const FormatStyle &Style;
3091   AnnotatedLine &Line;
3092   FormatToken *CurrentToken;
3093   bool AutoFound;
3094   bool IsCpp;
3095   LangOptions LangOpts;
3096   const AdditionalKeywords &Keywords;
3097 
3098   SmallVector<ScopeType> &Scopes;
3099 
3100   // Set of "<" tokens that do not open a template parameter list. If parseAngle
3101   // determines that a specific token can't be a template opener, it will make
3102   // same decision irrespective of the decisions for tokens leading up to it.
3103   // Store this information to prevent this from causing exponential runtime.
3104   llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
3105 
3106   int TemplateDeclarationDepth;
3107 };
3108 
3109 static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
3110 static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
3111 
3112 /// Parses binary expressions by inserting fake parenthesis based on
3113 /// operator precedence.
3114 class ExpressionParser {
3115 public:
3116   ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
3117                    AnnotatedLine &Line)
3118       : Style(Style), Keywords(Keywords), Line(Line), Current(Line.First) {}
3119 
3120   /// Parse expressions with the given operator precedence.
3121   void parse(int Precedence = 0) {
3122     // Skip 'return' and ObjC selector colons as they are not part of a binary
3123     // expression.
3124     while (Current && (Current->is(tok::kw_return) ||
3125                        (Current->is(tok::colon) &&
3126                         Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
3127       next();
3128     }
3129 
3130     if (!Current || Precedence > PrecedenceArrowAndPeriod)
3131       return;
3132 
3133     // Conditional expressions need to be parsed separately for proper nesting.
3134     if (Precedence == prec::Conditional) {
3135       parseConditionalExpr();
3136       return;
3137     }
3138 
3139     // Parse unary operators, which all have a higher precedence than binary
3140     // operators.
3141     if (Precedence == PrecedenceUnaryOperator) {
3142       parseUnaryOperator();
3143       return;
3144     }
3145 
3146     FormatToken *Start = Current;
3147     FormatToken *LatestOperator = nullptr;
3148     unsigned OperatorIndex = 0;
3149     // The first name of the current type in a port list.
3150     FormatToken *VerilogFirstOfType = nullptr;
3151 
3152     while (Current) {
3153       // In Verilog ports in a module header that don't have a type take the
3154       // type of the previous one.  For example,
3155       //   module a(output b,
3156       //                   c,
3157       //            output d);
3158       // In this case there need to be fake parentheses around b and c.
3159       if (Style.isVerilog() && Precedence == prec::Comma) {
3160         VerilogFirstOfType =
3161             verilogGroupDecl(VerilogFirstOfType, LatestOperator);
3162       }
3163 
3164       // Consume operators with higher precedence.
3165       parse(Precedence + 1);
3166 
3167       int CurrentPrecedence = getCurrentPrecedence();
3168 
3169       if (Precedence == CurrentPrecedence && Current &&
3170           Current->is(TT_SelectorName)) {
3171         if (LatestOperator)
3172           addFakeParenthesis(Start, prec::Level(Precedence));
3173         Start = Current;
3174       }
3175 
3176       if ((Style.isCSharp() || Style.isJavaScript() ||
3177            Style.Language == FormatStyle::LK_Java) &&
3178           Precedence == prec::Additive && Current) {
3179         // A string can be broken without parentheses around it when it is
3180         // already in a sequence of strings joined by `+` signs.
3181         FormatToken *Prev = Current->getPreviousNonComment();
3182         if (Prev && Prev->is(tok::string_literal) &&
3183             (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
3184                                                  TT_StringInConcatenation))) {
3185           Prev->setType(TT_StringInConcatenation);
3186         }
3187       }
3188 
3189       // At the end of the line or when an operator with lower precedence is
3190       // found, insert fake parenthesis and return.
3191       if (!Current ||
3192           (Current->closesScope() &&
3193            (Current->MatchingParen || Current->is(TT_TemplateString))) ||
3194           (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
3195           (CurrentPrecedence == prec::Conditional &&
3196            Precedence == prec::Assignment && Current->is(tok::colon))) {
3197         break;
3198       }
3199 
3200       // Consume scopes: (), [], <> and {}
3201       // In addition to that we handle require clauses as scope, so that the
3202       // constraints in that are correctly indented.
3203       if (Current->opensScope() ||
3204           Current->isOneOf(TT_RequiresClause,
3205                            TT_RequiresClauseInARequiresExpression)) {
3206         // In fragment of a JavaScript template string can look like '}..${' and
3207         // thus close a scope and open a new one at the same time.
3208         while (Current && (!Current->closesScope() || Current->opensScope())) {
3209           next();
3210           parse();
3211         }
3212         next();
3213       } else {
3214         // Operator found.
3215         if (CurrentPrecedence == Precedence) {
3216           if (LatestOperator)
3217             LatestOperator->NextOperator = Current;
3218           LatestOperator = Current;
3219           Current->OperatorIndex = OperatorIndex;
3220           ++OperatorIndex;
3221         }
3222         next(/*SkipPastLeadingComments=*/Precedence > 0);
3223       }
3224     }
3225 
3226     // Group variables of the same type.
3227     if (Style.isVerilog() && Precedence == prec::Comma && VerilogFirstOfType)
3228       addFakeParenthesis(VerilogFirstOfType, prec::Comma);
3229 
3230     if (LatestOperator && (Current || Precedence > 0)) {
3231       // The requires clauses do not neccessarily end in a semicolon or a brace,
3232       // but just go over to struct/class or a function declaration, we need to
3233       // intervene so that the fake right paren is inserted correctly.
3234       auto End =
3235           (Start->Previous &&
3236            Start->Previous->isOneOf(TT_RequiresClause,
3237                                     TT_RequiresClauseInARequiresExpression))
3238               ? [this]() {
3239                   auto Ret = Current ? Current : Line.Last;
3240                   while (!Ret->ClosesRequiresClause && Ret->Previous)
3241                     Ret = Ret->Previous;
3242                   return Ret;
3243                 }()
3244               : nullptr;
3245 
3246       if (Precedence == PrecedenceArrowAndPeriod) {
3247         // Call expressions don't have a binary operator precedence.
3248         addFakeParenthesis(Start, prec::Unknown, End);
3249       } else {
3250         addFakeParenthesis(Start, prec::Level(Precedence), End);
3251       }
3252     }
3253   }
3254 
3255 private:
3256   /// Gets the precedence (+1) of the given token for binary operators
3257   /// and other tokens that we treat like binary operators.
3258   int getCurrentPrecedence() {
3259     if (Current) {
3260       const FormatToken *NextNonComment = Current->getNextNonComment();
3261       if (Current->is(TT_ConditionalExpr))
3262         return prec::Conditional;
3263       if (NextNonComment && Current->is(TT_SelectorName) &&
3264           (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
3265            (Style.isProto() && NextNonComment->is(tok::less)))) {
3266         return prec::Assignment;
3267       }
3268       if (Current->is(TT_JsComputedPropertyName))
3269         return prec::Assignment;
3270       if (Current->is(TT_TrailingReturnArrow))
3271         return prec::Comma;
3272       if (Current->is(TT_FatArrow))
3273         return prec::Assignment;
3274       if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
3275           (Current->is(tok::comment) && NextNonComment &&
3276            NextNonComment->is(TT_SelectorName))) {
3277         return 0;
3278       }
3279       if (Current->is(TT_RangeBasedForLoopColon))
3280         return prec::Comma;
3281       if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
3282           Current->is(Keywords.kw_instanceof)) {
3283         return prec::Relational;
3284       }
3285       if (Style.isJavaScript() &&
3286           Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
3287         return prec::Relational;
3288       }
3289       if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
3290         return Current->getPrecedence();
3291       if (Current->isOneOf(tok::period, tok::arrow) &&
3292           Current->isNot(TT_TrailingReturnArrow)) {
3293         return PrecedenceArrowAndPeriod;
3294       }
3295       if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
3296           Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
3297                            Keywords.kw_throws)) {
3298         return 0;
3299       }
3300       // In Verilog case labels are not on separate lines straight out of
3301       // UnwrappedLineParser. The colon is not part of an expression.
3302       if (Style.isVerilog() && Current->is(tok::colon))
3303         return 0;
3304     }
3305     return -1;
3306   }
3307 
3308   void addFakeParenthesis(FormatToken *Start, prec::Level Precedence,
3309                           FormatToken *End = nullptr) {
3310     // Do not assign fake parenthesis to tokens that are part of an
3311     // unexpanded macro call. The line within the macro call contains
3312     // the parenthesis and commas, and we will not find operators within
3313     // that structure.
3314     if (Start->MacroParent)
3315       return;
3316 
3317     Start->FakeLParens.push_back(Precedence);
3318     if (Precedence > prec::Unknown)
3319       Start->StartsBinaryExpression = true;
3320     if (!End && Current)
3321       End = Current->getPreviousNonComment();
3322     if (End) {
3323       ++End->FakeRParens;
3324       if (Precedence > prec::Unknown)
3325         End->EndsBinaryExpression = true;
3326     }
3327   }
3328 
3329   /// Parse unary operator expressions and surround them with fake
3330   /// parentheses if appropriate.
3331   void parseUnaryOperator() {
3332     llvm::SmallVector<FormatToken *, 2> Tokens;
3333     while (Current && Current->is(TT_UnaryOperator)) {
3334       Tokens.push_back(Current);
3335       next();
3336     }
3337     parse(PrecedenceArrowAndPeriod);
3338     for (FormatToken *Token : llvm::reverse(Tokens)) {
3339       // The actual precedence doesn't matter.
3340       addFakeParenthesis(Token, prec::Unknown);
3341     }
3342   }
3343 
3344   void parseConditionalExpr() {
3345     while (Current && Current->isTrailingComment())
3346       next();
3347     FormatToken *Start = Current;
3348     parse(prec::LogicalOr);
3349     if (!Current || Current->isNot(tok::question))
3350       return;
3351     next();
3352     parse(prec::Assignment);
3353     if (!Current || Current->isNot(TT_ConditionalExpr))
3354       return;
3355     next();
3356     parse(prec::Assignment);
3357     addFakeParenthesis(Start, prec::Conditional);
3358   }
3359 
3360   void next(bool SkipPastLeadingComments = true) {
3361     if (Current)
3362       Current = Current->Next;
3363     while (Current &&
3364            (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
3365            Current->isTrailingComment()) {
3366       Current = Current->Next;
3367     }
3368   }
3369 
3370   // Add fake parenthesis around declarations of the same type for example in a
3371   // module prototype. Return the first port / variable of the current type.
3372   FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
3373                                 FormatToken *PreviousComma) {
3374     if (!Current)
3375       return nullptr;
3376 
3377     FormatToken *Start = Current;
3378 
3379     // Skip attributes.
3380     while (Start->startsSequence(tok::l_paren, tok::star)) {
3381       if (!(Start = Start->MatchingParen) ||
3382           !(Start = Start->getNextNonComment())) {
3383         return nullptr;
3384       }
3385     }
3386 
3387     FormatToken *Tok = Start;
3388 
3389     if (Tok->is(Keywords.kw_assign))
3390       Tok = Tok->getNextNonComment();
3391 
3392     // Skip any type qualifiers to find the first identifier. It may be either a
3393     // new type name or a variable name. There can be several type qualifiers
3394     // preceding a variable name, and we can not tell them apart by looking at
3395     // the word alone since a macro can be defined as either a type qualifier or
3396     // a variable name. Thus we use the last word before the dimensions instead
3397     // of the first word as the candidate for the variable or type name.
3398     FormatToken *First = nullptr;
3399     while (Tok) {
3400       FormatToken *Next = Tok->getNextNonComment();
3401 
3402       if (Tok->is(tok::hash)) {
3403         // Start of a macro expansion.
3404         First = Tok;
3405         Tok = Next;
3406         if (Tok)
3407           Tok = Tok->getNextNonComment();
3408       } else if (Tok->is(tok::hashhash)) {
3409         // Concatenation. Skip.
3410         Tok = Next;
3411         if (Tok)
3412           Tok = Tok->getNextNonComment();
3413       } else if (Keywords.isVerilogQualifier(*Tok) ||
3414                  Keywords.isVerilogIdentifier(*Tok)) {
3415         First = Tok;
3416         Tok = Next;
3417         // The name may have dots like `interface_foo.modport_foo`.
3418         while (Tok && Tok->isOneOf(tok::period, tok::coloncolon) &&
3419                (Tok = Tok->getNextNonComment())) {
3420           if (Keywords.isVerilogIdentifier(*Tok))
3421             Tok = Tok->getNextNonComment();
3422         }
3423       } else if (!Next) {
3424         Tok = nullptr;
3425       } else if (Tok->is(tok::l_paren)) {
3426         // Make sure the parenthesized list is a drive strength. Otherwise the
3427         // statement may be a module instantiation in which case we have already
3428         // found the instance name.
3429         if (Next->isOneOf(
3430                 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3431                 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3432                 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3433                 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3434                 Keywords.kw_weak1)) {
3435           Tok->setType(TT_VerilogStrength);
3436           Tok = Tok->MatchingParen;
3437           if (Tok) {
3438             Tok->setType(TT_VerilogStrength);
3439             Tok = Tok->getNextNonComment();
3440           }
3441         } else {
3442           break;
3443         }
3444       } else if (Tok->is(Keywords.kw_verilogHash)) {
3445         // Delay control.
3446         if (Next->is(tok::l_paren))
3447           Next = Next->MatchingParen;
3448         if (Next)
3449           Tok = Next->getNextNonComment();
3450       } else {
3451         break;
3452       }
3453     }
3454 
3455     // Find the second identifier. If it exists it will be the name.
3456     FormatToken *Second = nullptr;
3457     // Dimensions.
3458     while (Tok && Tok->is(tok::l_square) && (Tok = Tok->MatchingParen))
3459       Tok = Tok->getNextNonComment();
3460     if (Tok && (Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*Tok)))
3461       Second = Tok;
3462 
3463     // If the second identifier doesn't exist and there are qualifiers, the type
3464     // is implied.
3465     FormatToken *TypedName = nullptr;
3466     if (Second) {
3467       TypedName = Second;
3468       if (First && First->is(TT_Unknown))
3469         First->setType(TT_VerilogDimensionedTypeName);
3470     } else if (First != Start) {
3471       // If 'First' is null, then this isn't a declaration, 'TypedName' gets set
3472       // to null as intended.
3473       TypedName = First;
3474     }
3475 
3476     if (TypedName) {
3477       // This is a declaration with a new type.
3478       if (TypedName->is(TT_Unknown))
3479         TypedName->setType(TT_StartOfName);
3480       // Group variables of the previous type.
3481       if (FirstOfType && PreviousComma) {
3482         PreviousComma->setType(TT_VerilogTypeComma);
3483         addFakeParenthesis(FirstOfType, prec::Comma, PreviousComma->Previous);
3484       }
3485 
3486       FirstOfType = TypedName;
3487 
3488       // Don't let higher precedence handle the qualifiers. For example if we
3489       // have:
3490       //    parameter x = 0
3491       // We skip `parameter` here. This way the fake parentheses for the
3492       // assignment will be around `x = 0`.
3493       while (Current && Current != FirstOfType) {
3494         if (Current->opensScope()) {
3495           next();
3496           parse();
3497         }
3498         next();
3499       }
3500     }
3501 
3502     return FirstOfType;
3503   }
3504 
3505   const FormatStyle &Style;
3506   const AdditionalKeywords &Keywords;
3507   const AnnotatedLine &Line;
3508   FormatToken *Current;
3509 };
3510 
3511 } // end anonymous namespace
3512 
3513 void TokenAnnotator::setCommentLineLevels(
3514     SmallVectorImpl<AnnotatedLine *> &Lines) const {
3515   const AnnotatedLine *NextNonCommentLine = nullptr;
3516   for (AnnotatedLine *Line : llvm::reverse(Lines)) {
3517     assert(Line->First);
3518 
3519     // If the comment is currently aligned with the line immediately following
3520     // it, that's probably intentional and we should keep it.
3521     if (NextNonCommentLine && NextNonCommentLine->First->NewlinesBefore < 2 &&
3522         Line->isComment() && !isClangFormatOff(Line->First->TokenText) &&
3523         NextNonCommentLine->First->OriginalColumn ==
3524             Line->First->OriginalColumn) {
3525       const bool PPDirectiveOrImportStmt =
3526           NextNonCommentLine->Type == LT_PreprocessorDirective ||
3527           NextNonCommentLine->Type == LT_ImportStatement;
3528       if (PPDirectiveOrImportStmt)
3529         Line->Type = LT_CommentAbovePPDirective;
3530       // Align comments for preprocessor lines with the # in column 0 if
3531       // preprocessor lines are not indented. Otherwise, align with the next
3532       // line.
3533       Line->Level = Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
3534                             PPDirectiveOrImportStmt
3535                         ? 0
3536                         : NextNonCommentLine->Level;
3537     } else {
3538       NextNonCommentLine = Line->First->isNot(tok::r_brace) ? Line : nullptr;
3539     }
3540 
3541     setCommentLineLevels(Line->Children);
3542   }
3543 }
3544 
3545 static unsigned maxNestingDepth(const AnnotatedLine &Line) {
3546   unsigned Result = 0;
3547   for (const auto *Tok = Line.First; Tok; Tok = Tok->Next)
3548     Result = std::max(Result, Tok->NestingLevel);
3549   return Result;
3550 }
3551 
3552 // Returns the name of a function with no return type, e.g. a constructor or
3553 // destructor.
3554 static FormatToken *getFunctionName(const AnnotatedLine &Line,
3555                                     FormatToken *&OpeningParen) {
3556   for (FormatToken *Tok = Line.getFirstNonComment(), *Name = nullptr; Tok;
3557        Tok = Tok->getNextNonComment()) {
3558     // Skip C++11 attributes both before and after the function name.
3559     if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3560       Tok = Tok->MatchingParen;
3561       if (!Tok)
3562         break;
3563       continue;
3564     }
3565 
3566     // Make sure the name is followed by a pair of parentheses.
3567     if (Name) {
3568       if (Tok->is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) &&
3569           Tok->MatchingParen) {
3570         OpeningParen = Tok;
3571         return Name;
3572       }
3573       return nullptr;
3574     }
3575 
3576     // Skip keywords that may precede the constructor/destructor name.
3577     if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3578                      tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3579       continue;
3580     }
3581 
3582     // A qualified name may start from the global namespace.
3583     if (Tok->is(tok::coloncolon)) {
3584       Tok = Tok->Next;
3585       if (!Tok)
3586         break;
3587     }
3588 
3589     // Skip to the unqualified part of the name.
3590     while (Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3591       assert(Tok->Next);
3592       Tok = Tok->Next->Next;
3593       if (!Tok)
3594         return nullptr;
3595     }
3596 
3597     // Skip the `~` if a destructor name.
3598     if (Tok->is(tok::tilde)) {
3599       Tok = Tok->Next;
3600       if (!Tok)
3601         break;
3602     }
3603 
3604     // Make sure the name is not already annotated, e.g. as NamespaceMacro.
3605     if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
3606       break;
3607 
3608     Name = Tok;
3609   }
3610 
3611   return nullptr;
3612 }
3613 
3614 // Checks if Tok is a constructor/destructor name qualified by its class name.
3615 static bool isCtorOrDtorName(const FormatToken *Tok) {
3616   assert(Tok && Tok->is(tok::identifier));
3617   const auto *Prev = Tok->Previous;
3618 
3619   if (Prev && Prev->is(tok::tilde))
3620     Prev = Prev->Previous;
3621 
3622   if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3623     return false;
3624 
3625   assert(Prev->Previous);
3626   return Prev->Previous->TokenText == Tok->TokenText;
3627 }
3628 
3629 void TokenAnnotator::annotate(AnnotatedLine &Line) {
3630   AnnotatingParser Parser(Style, Line, Keywords, Scopes);
3631   Line.Type = Parser.parseLine();
3632 
3633   for (auto &Child : Line.Children)
3634     annotate(*Child);
3635 
3636   // With very deep nesting, ExpressionParser uses lots of stack and the
3637   // formatting algorithm is very slow. We're not going to do a good job here
3638   // anyway - it's probably generated code being formatted by mistake.
3639   // Just skip the whole line.
3640   if (maxNestingDepth(Line) > 50)
3641     Line.Type = LT_Invalid;
3642 
3643   if (Line.Type == LT_Invalid)
3644     return;
3645 
3646   ExpressionParser ExprParser(Style, Keywords, Line);
3647   ExprParser.parse();
3648 
3649   if (IsCpp) {
3650     FormatToken *OpeningParen = nullptr;
3651     auto *Tok = getFunctionName(Line, OpeningParen);
3652     if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
3653                 Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
3654       Tok->setFinalizedType(TT_CtorDtorDeclName);
3655       assert(OpeningParen);
3656       OpeningParen->setFinalizedType(TT_FunctionDeclarationLParen);
3657     }
3658   }
3659 
3660   if (Line.startsWith(TT_ObjCMethodSpecifier))
3661     Line.Type = LT_ObjCMethodDecl;
3662   else if (Line.startsWith(TT_ObjCDecl))
3663     Line.Type = LT_ObjCDecl;
3664   else if (Line.startsWith(TT_ObjCProperty))
3665     Line.Type = LT_ObjCProperty;
3666 
3667   auto *First = Line.First;
3668   First->SpacesRequiredBefore = 1;
3669   First->CanBreakBefore = First->MustBreakBefore;
3670 
3671   if (First->is(tok::eof) && First->NewlinesBefore == 0 &&
3672       Style.InsertNewlineAtEOF) {
3673     First->NewlinesBefore = 1;
3674   }
3675 }
3676 
3677 // This function heuristically determines whether 'Current' starts the name of a
3678 // function declaration.
3679 static bool isFunctionDeclarationName(const LangOptions &LangOpts,
3680                                       const FormatToken &Current,
3681                                       const AnnotatedLine &Line,
3682                                       FormatToken *&ClosingParen) {
3683   assert(Current.Previous);
3684 
3685   if (Current.is(TT_FunctionDeclarationName))
3686     return true;
3687 
3688   if (!Current.Tok.getIdentifierInfo())
3689     return false;
3690 
3691   const auto &Previous = *Current.Previous;
3692 
3693   if (const auto *PrevPrev = Previous.Previous;
3694       PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3695     return false;
3696   }
3697 
3698   auto skipOperatorName =
3699       [&LangOpts](const FormatToken *Next) -> const FormatToken * {
3700     for (; Next; Next = Next->Next) {
3701       if (Next->is(TT_OverloadedOperatorLParen))
3702         return Next;
3703       if (Next->is(TT_OverloadedOperator))
3704         continue;
3705       if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
3706         // For 'new[]' and 'delete[]'.
3707         if (Next->Next &&
3708             Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3709           Next = Next->Next->Next;
3710         }
3711         continue;
3712       }
3713       if (Next->startsSequence(tok::l_square, tok::r_square)) {
3714         // For operator[]().
3715         Next = Next->Next;
3716         continue;
3717       }
3718       if ((Next->isTypeName(LangOpts) || Next->is(tok::identifier)) &&
3719           Next->Next && Next->Next->isPointerOrReference()) {
3720         // For operator void*(), operator char*(), operator Foo*().
3721         Next = Next->Next;
3722         continue;
3723       }
3724       if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3725         Next = Next->MatchingParen;
3726         continue;
3727       }
3728 
3729       break;
3730     }
3731     return nullptr;
3732   };
3733 
3734   const auto *Next = Current.Next;
3735   const bool IsCpp = LangOpts.CXXOperatorNames;
3736 
3737   // Find parentheses of parameter list.
3738   if (Current.is(tok::kw_operator)) {
3739     if (Previous.Tok.getIdentifierInfo() &&
3740         !Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
3741       return true;
3742     }
3743     if (Previous.is(tok::r_paren) && Previous.is(TT_TypeDeclarationParen)) {
3744       assert(Previous.MatchingParen);
3745       assert(Previous.MatchingParen->is(tok::l_paren));
3746       assert(Previous.MatchingParen->is(TT_TypeDeclarationParen));
3747       return true;
3748     }
3749     if (!Previous.isPointerOrReference() && Previous.isNot(TT_TemplateCloser))
3750       return false;
3751     Next = skipOperatorName(Next);
3752   } else {
3753     if (Current.isNot(TT_StartOfName) || Current.NestingLevel != 0)
3754       return false;
3755     for (; Next; Next = Next->Next) {
3756       if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3757         Next = Next->MatchingParen;
3758       } else if (Next->is(tok::coloncolon)) {
3759         Next = Next->Next;
3760         if (!Next)
3761           return false;
3762         if (Next->is(tok::kw_operator)) {
3763           Next = skipOperatorName(Next->Next);
3764           break;
3765         }
3766         if (Next->isNot(tok::identifier))
3767           return false;
3768       } else if (isCppAttribute(IsCpp, *Next)) {
3769         Next = Next->MatchingParen;
3770         if (!Next)
3771           return false;
3772       } else if (Next->is(tok::l_paren)) {
3773         break;
3774       } else {
3775         return false;
3776       }
3777     }
3778   }
3779 
3780   // Check whether parameter list can belong to a function declaration.
3781   if (!Next || Next->isNot(tok::l_paren) || !Next->MatchingParen)
3782     return false;
3783   ClosingParen = Next->MatchingParen;
3784   assert(ClosingParen->is(tok::r_paren));
3785   // If the lines ends with "{", this is likely a function definition.
3786   if (Line.Last->is(tok::l_brace))
3787     return true;
3788   if (Next->Next == ClosingParen)
3789     return true; // Empty parentheses.
3790   // If there is an &/&& after the r_paren, this is likely a function.
3791   if (ClosingParen->Next && ClosingParen->Next->is(TT_PointerOrReference))
3792     return true;
3793 
3794   // Check for K&R C function definitions (and C++ function definitions with
3795   // unnamed parameters), e.g.:
3796   //   int f(i)
3797   //   {
3798   //     return i + 1;
3799   //   }
3800   //   bool g(size_t = 0, bool b = false)
3801   //   {
3802   //     return !b;
3803   //   }
3804   if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
3805       !Line.endsWith(tok::semi)) {
3806     return true;
3807   }
3808 
3809   for (const FormatToken *Tok = Next->Next; Tok && Tok != ClosingParen;
3810        Tok = Tok->Next) {
3811     if (Tok->is(TT_TypeDeclarationParen))
3812       return true;
3813     if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
3814       Tok = Tok->MatchingParen;
3815       continue;
3816     }
3817     if (Tok->is(tok::kw_const) || Tok->isTypeName(LangOpts) ||
3818         Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3819       return true;
3820     }
3821     if (Tok->isOneOf(tok::l_brace, TT_ObjCMethodExpr) || Tok->Tok.isLiteral())
3822       return false;
3823   }
3824   return false;
3825 }
3826 
3827 bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
3828   assert(Line.MightBeFunctionDecl);
3829 
3830   if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
3831        Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
3832       Line.Level > 0) {
3833     return false;
3834   }
3835 
3836   switch (Style.BreakAfterReturnType) {
3837   case FormatStyle::RTBS_None:
3838   case FormatStyle::RTBS_Automatic:
3839   case FormatStyle::RTBS_ExceptShortType:
3840     return false;
3841   case FormatStyle::RTBS_All:
3842   case FormatStyle::RTBS_TopLevel:
3843     return true;
3844   case FormatStyle::RTBS_AllDefinitions:
3845   case FormatStyle::RTBS_TopLevelDefinitions:
3846     return Line.mightBeFunctionDefinition();
3847   }
3848 
3849   return false;
3850 }
3851 
3852 void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
3853   for (AnnotatedLine *ChildLine : Line.Children)
3854     calculateFormattingInformation(*ChildLine);
3855 
3856   auto *First = Line.First;
3857   First->TotalLength = First->IsMultiline
3858                            ? Style.ColumnLimit
3859                            : Line.FirstStartColumn + First->ColumnWidth;
3860   FormatToken *Current = First->Next;
3861   bool InFunctionDecl = Line.MightBeFunctionDecl;
3862   bool AlignArrayOfStructures =
3863       (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
3864        Line.Type == LT_ArrayOfStructInitializer);
3865   if (AlignArrayOfStructures)
3866     calculateArrayInitializerColumnList(Line);
3867 
3868   bool SeenName = false;
3869   bool LineIsFunctionDeclaration = false;
3870   FormatToken *ClosingParen = nullptr;
3871   FormatToken *AfterLastAttribute = nullptr;
3872 
3873   for (auto *Tok = Current; Tok; Tok = Tok->Next) {
3874     if (Tok->is(TT_StartOfName))
3875       SeenName = true;
3876     if (Tok->Previous->EndsCppAttributeGroup)
3877       AfterLastAttribute = Tok;
3878     if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3879         IsCtorOrDtor ||
3880         isFunctionDeclarationName(LangOpts, *Tok, Line, ClosingParen)) {
3881       if (!IsCtorOrDtor)
3882         Tok->setFinalizedType(TT_FunctionDeclarationName);
3883       LineIsFunctionDeclaration = true;
3884       SeenName = true;
3885       if (ClosingParen) {
3886         auto *OpeningParen = ClosingParen->MatchingParen;
3887         assert(OpeningParen);
3888         if (OpeningParen->is(TT_Unknown))
3889           OpeningParen->setType(TT_FunctionDeclarationLParen);
3890       }
3891       break;
3892     }
3893   }
3894 
3895   if (IsCpp && (LineIsFunctionDeclaration || First->is(TT_CtorDtorDeclName)) &&
3896       Line.endsWith(tok::semi, tok::r_brace)) {
3897     auto *Tok = Line.Last->Previous;
3898     while (Tok->isNot(tok::r_brace))
3899       Tok = Tok->Previous;
3900     if (auto *LBrace = Tok->MatchingParen; LBrace) {
3901       assert(LBrace->is(tok::l_brace));
3902       Tok->setBlockKind(BK_Block);
3903       LBrace->setBlockKind(BK_Block);
3904       LBrace->setFinalizedType(TT_FunctionLBrace);
3905     }
3906   }
3907 
3908   if (IsCpp && SeenName && AfterLastAttribute &&
3909       mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
3910     AfterLastAttribute->MustBreakBefore = true;
3911     if (LineIsFunctionDeclaration)
3912       Line.ReturnTypeWrapped = true;
3913   }
3914 
3915   if (IsCpp) {
3916     if (!LineIsFunctionDeclaration) {
3917       // Annotate */&/&& in `operator` function calls as binary operators.
3918       for (const auto *Tok = First; Tok; Tok = Tok->Next) {
3919         if (Tok->isNot(tok::kw_operator))
3920           continue;
3921         do {
3922           Tok = Tok->Next;
3923         } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
3924         if (!Tok || !Tok->MatchingParen)
3925           break;
3926         const auto *LeftParen = Tok;
3927         for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
3928              Tok = Tok->Next) {
3929           if (Tok->isNot(tok::identifier))
3930             continue;
3931           auto *Next = Tok->Next;
3932           const bool NextIsBinaryOperator =
3933               Next && Next->isPointerOrReference() && Next->Next &&
3934               Next->Next->is(tok::identifier);
3935           if (!NextIsBinaryOperator)
3936             continue;
3937           Next->setType(TT_BinaryOperator);
3938           Tok = Next;
3939         }
3940       }
3941     } else if (ClosingParen) {
3942       for (auto *Tok = ClosingParen->Next; Tok; Tok = Tok->Next) {
3943         if (Tok->is(TT_CtorInitializerColon))
3944           break;
3945         if (Tok->is(tok::arrow)) {
3946           Tok->setType(TT_TrailingReturnArrow);
3947           break;
3948         }
3949         if (Tok->isNot(TT_TrailingAnnotation))
3950           continue;
3951         const auto *Next = Tok->Next;
3952         if (!Next || Next->isNot(tok::l_paren))
3953           continue;
3954         Tok = Next->MatchingParen;
3955         if (!Tok)
3956           break;
3957       }
3958     }
3959   }
3960 
3961   while (Current) {
3962     const FormatToken *Prev = Current->Previous;
3963     if (Current->is(TT_LineComment)) {
3964       if (Prev->is(BK_BracedInit) && Prev->opensScope()) {
3965         Current->SpacesRequiredBefore =
3966             (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
3967                 ? 0
3968                 : 1;
3969       } else if (Prev->is(TT_VerilogMultiLineListLParen)) {
3970         Current->SpacesRequiredBefore = 0;
3971       } else {
3972         Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
3973       }
3974 
3975       // If we find a trailing comment, iterate backwards to determine whether
3976       // it seems to relate to a specific parameter. If so, break before that
3977       // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
3978       // to the previous line in:
3979       //   SomeFunction(a,
3980       //                b, // comment
3981       //                c);
3982       if (!Current->HasUnescapedNewline) {
3983         for (FormatToken *Parameter = Current->Previous; Parameter;
3984              Parameter = Parameter->Previous) {
3985           if (Parameter->isOneOf(tok::comment, tok::r_brace))
3986             break;
3987           if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
3988             if (Parameter->Previous->isNot(TT_CtorInitializerComma) &&
3989                 Parameter->HasUnescapedNewline) {
3990               Parameter->MustBreakBefore = true;
3991             }
3992             break;
3993           }
3994         }
3995       }
3996     } else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
3997                spaceRequiredBefore(Line, *Current)) {
3998       Current->SpacesRequiredBefore = 1;
3999     }
4000 
4001     const auto &Children = Prev->Children;
4002     if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
4003       Current->MustBreakBefore = true;
4004     } else {
4005       Current->MustBreakBefore =
4006           Current->MustBreakBefore || mustBreakBefore(Line, *Current);
4007       if (!Current->MustBreakBefore && InFunctionDecl &&
4008           Current->is(TT_FunctionDeclarationName)) {
4009         Current->MustBreakBefore = mustBreakForReturnType(Line);
4010       }
4011     }
4012 
4013     Current->CanBreakBefore =
4014         Current->MustBreakBefore || canBreakBefore(Line, *Current);
4015     unsigned ChildSize = 0;
4016     if (Prev->Children.size() == 1) {
4017       FormatToken &LastOfChild = *Prev->Children[0]->Last;
4018       ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
4019                                                   : LastOfChild.TotalLength + 1;
4020     }
4021     if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
4022         (Prev->Children.size() == 1 &&
4023          Prev->Children[0]->First->MustBreakBefore) ||
4024         Current->IsMultiline) {
4025       Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
4026     } else {
4027       Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
4028                              ChildSize + Current->SpacesRequiredBefore;
4029     }
4030 
4031     if (Current->is(TT_CtorInitializerColon))
4032       InFunctionDecl = false;
4033 
4034     // FIXME: Only calculate this if CanBreakBefore is true once static
4035     // initializers etc. are sorted out.
4036     // FIXME: Move magic numbers to a better place.
4037 
4038     // Reduce penalty for aligning ObjC method arguments using the colon
4039     // alignment as this is the canonical way (still prefer fitting everything
4040     // into one line if possible). Trying to fit a whole expression into one
4041     // line should not force other line breaks (e.g. when ObjC method
4042     // expression is a part of other expression).
4043     Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
4044     if (Style.Language == FormatStyle::LK_ObjC &&
4045         Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
4046       if (Current->ParameterIndex == 1)
4047         Current->SplitPenalty += 5 * Current->BindingStrength;
4048     } else {
4049       Current->SplitPenalty += 20 * Current->BindingStrength;
4050     }
4051 
4052     Current = Current->Next;
4053   }
4054 
4055   calculateUnbreakableTailLengths(Line);
4056   unsigned IndentLevel = Line.Level;
4057   for (Current = First; Current; Current = Current->Next) {
4058     if (Current->Role)
4059       Current->Role->precomputeFormattingInfos(Current);
4060     if (Current->MatchingParen &&
4061         Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
4062         IndentLevel > 0) {
4063       --IndentLevel;
4064     }
4065     Current->IndentLevel = IndentLevel;
4066     if (Current->opensBlockOrBlockTypeList(Style))
4067       ++IndentLevel;
4068   }
4069 
4070   LLVM_DEBUG({ printDebugInfo(Line); });
4071 }
4072 
4073 void TokenAnnotator::calculateUnbreakableTailLengths(
4074     AnnotatedLine &Line) const {
4075   unsigned UnbreakableTailLength = 0;
4076   FormatToken *Current = Line.Last;
4077   while (Current) {
4078     Current->UnbreakableTailLength = UnbreakableTailLength;
4079     if (Current->CanBreakBefore ||
4080         Current->isOneOf(tok::comment, tok::string_literal)) {
4081       UnbreakableTailLength = 0;
4082     } else {
4083       UnbreakableTailLength +=
4084           Current->ColumnWidth + Current->SpacesRequiredBefore;
4085     }
4086     Current = Current->Previous;
4087   }
4088 }
4089 
4090 void TokenAnnotator::calculateArrayInitializerColumnList(
4091     AnnotatedLine &Line) const {
4092   if (Line.First == Line.Last)
4093     return;
4094   auto *CurrentToken = Line.First;
4095   CurrentToken->ArrayInitializerLineStart = true;
4096   unsigned Depth = 0;
4097   while (CurrentToken && CurrentToken != Line.Last) {
4098     if (CurrentToken->is(tok::l_brace)) {
4099       CurrentToken->IsArrayInitializer = true;
4100       if (CurrentToken->Next)
4101         CurrentToken->Next->MustBreakBefore = true;
4102       CurrentToken =
4103           calculateInitializerColumnList(Line, CurrentToken->Next, Depth + 1);
4104     } else {
4105       CurrentToken = CurrentToken->Next;
4106     }
4107   }
4108 }
4109 
4110 FormatToken *TokenAnnotator::calculateInitializerColumnList(
4111     AnnotatedLine &Line, FormatToken *CurrentToken, unsigned Depth) const {
4112   while (CurrentToken && CurrentToken != Line.Last) {
4113     if (CurrentToken->is(tok::l_brace))
4114       ++Depth;
4115     else if (CurrentToken->is(tok::r_brace))
4116       --Depth;
4117     if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
4118       CurrentToken = CurrentToken->Next;
4119       if (!CurrentToken)
4120         break;
4121       CurrentToken->StartsColumn = true;
4122       CurrentToken = CurrentToken->Previous;
4123     }
4124     CurrentToken = CurrentToken->Next;
4125   }
4126   return CurrentToken;
4127 }
4128 
4129 unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
4130                                       const FormatToken &Tok,
4131                                       bool InFunctionDecl) const {
4132   const FormatToken &Left = *Tok.Previous;
4133   const FormatToken &Right = Tok;
4134 
4135   if (Left.is(tok::semi))
4136     return 0;
4137 
4138   // Language specific handling.
4139   if (Style.Language == FormatStyle::LK_Java) {
4140     if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
4141       return 1;
4142     if (Right.is(Keywords.kw_implements))
4143       return 2;
4144     if (Left.is(tok::comma) && Left.NestingLevel == 0)
4145       return 3;
4146   } else if (Style.isJavaScript()) {
4147     if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
4148       return 100;
4149     if (Left.is(TT_JsTypeColon))
4150       return 35;
4151     if ((Left.is(TT_TemplateString) && Left.TokenText.ends_with("${")) ||
4152         (Right.is(TT_TemplateString) && Right.TokenText.starts_with("}"))) {
4153       return 100;
4154     }
4155     // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
4156     if (Left.opensScope() && Right.closesScope())
4157       return 200;
4158   } else if (Style.Language == FormatStyle::LK_Proto) {
4159     if (Right.is(tok::l_square))
4160       return 1;
4161     if (Right.is(tok::period))
4162       return 500;
4163   }
4164 
4165   if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
4166     return 1;
4167   if (Right.is(tok::l_square)) {
4168     if (Left.is(tok::r_square))
4169       return 200;
4170     // Slightly prefer formatting local lambda definitions like functions.
4171     if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
4172       return 35;
4173     if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4174                        TT_ArrayInitializerLSquare,
4175                        TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
4176       return 500;
4177     }
4178   }
4179 
4180   if (Left.is(tok::coloncolon))
4181     return Style.PenaltyBreakScopeResolution;
4182   if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
4183       Right.is(tok::kw_operator)) {
4184     if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
4185       return 3;
4186     if (Left.is(TT_StartOfName))
4187       return 110;
4188     if (InFunctionDecl && Right.NestingLevel == 0)
4189       return Style.PenaltyReturnTypeOnItsOwnLine;
4190     return 200;
4191   }
4192   if (Right.is(TT_PointerOrReference))
4193     return 190;
4194   if (Right.is(TT_TrailingReturnArrow))
4195     return 110;
4196   if (Left.is(tok::equal) && Right.is(tok::l_brace))
4197     return 160;
4198   if (Left.is(TT_CastRParen))
4199     return 100;
4200   if (Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
4201     return 5000;
4202   if (Left.is(tok::comment))
4203     return 1000;
4204 
4205   if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
4206                    TT_CtorInitializerColon)) {
4207     return 2;
4208   }
4209 
4210   if (Right.isMemberAccess()) {
4211     // Breaking before the "./->" of a chained call/member access is reasonably
4212     // cheap, as formatting those with one call per line is generally
4213     // desirable. In particular, it should be cheaper to break before the call
4214     // than it is to break inside a call's parameters, which could lead to weird
4215     // "hanging" indents. The exception is the very last "./->" to support this
4216     // frequent pattern:
4217     //
4218     //   aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
4219     //       dddddddd);
4220     //
4221     // which might otherwise be blown up onto many lines. Here, clang-format
4222     // won't produce "hanging" indents anyway as there is no other trailing
4223     // call.
4224     //
4225     // Also apply higher penalty is not a call as that might lead to a wrapping
4226     // like:
4227     //
4228     //   aaaaaaa
4229     //       .aaaaaaaaa.bbbbbbbb(cccccccc);
4230     return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
4231                ? 150
4232                : 35;
4233   }
4234 
4235   if (Right.is(TT_TrailingAnnotation) &&
4236       (!Right.Next || Right.Next->isNot(tok::l_paren))) {
4237     // Moving trailing annotations to the next line is fine for ObjC method
4238     // declarations.
4239     if (Line.startsWith(TT_ObjCMethodSpecifier))
4240       return 10;
4241     // Generally, breaking before a trailing annotation is bad unless it is
4242     // function-like. It seems to be especially preferable to keep standard
4243     // annotations (i.e. "const", "final" and "override") on the same line.
4244     // Use a slightly higher penalty after ")" so that annotations like
4245     // "const override" are kept together.
4246     bool is_short_annotation = Right.TokenText.size() < 10;
4247     return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
4248   }
4249 
4250   // In for-loops, prefer breaking at ',' and ';'.
4251   if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
4252     return 4;
4253 
4254   // In Objective-C method expressions, prefer breaking before "param:" over
4255   // breaking after it.
4256   if (Right.is(TT_SelectorName))
4257     return 0;
4258   if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
4259     return Line.MightBeFunctionDecl ? 50 : 500;
4260 
4261   // In Objective-C type declarations, avoid breaking after the category's
4262   // open paren (we'll prefer breaking after the protocol list's opening
4263   // angle bracket, if present).
4264   if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous &&
4265       Left.Previous->isOneOf(tok::identifier, tok::greater)) {
4266     return 500;
4267   }
4268 
4269   if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
4270     return Style.PenaltyBreakOpenParenthesis;
4271   if (Left.is(tok::l_paren) && InFunctionDecl &&
4272       Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
4273     return 100;
4274   }
4275   if (Left.is(tok::l_paren) && Left.Previous &&
4276       (Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
4277        Left.Previous->isIf())) {
4278     return 1000;
4279   }
4280   if (Left.is(tok::equal) && InFunctionDecl)
4281     return 110;
4282   if (Right.is(tok::r_brace))
4283     return 1;
4284   if (Left.is(TT_TemplateOpener))
4285     return 100;
4286   if (Left.opensScope()) {
4287     // If we aren't aligning after opening parens/braces we can always break
4288     // here unless the style does not want us to place all arguments on the
4289     // next line.
4290     if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
4291         (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
4292       return 0;
4293     }
4294     if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
4295       return 19;
4296     return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
4297                                    : 19;
4298   }
4299   if (Left.is(TT_JavaAnnotation))
4300     return 50;
4301 
4302   if (Left.is(TT_UnaryOperator))
4303     return 60;
4304   if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
4305       Left.Previous->isLabelString() &&
4306       (Left.NextOperator || Left.OperatorIndex != 0)) {
4307     return 50;
4308   }
4309   if (Right.is(tok::plus) && Left.isLabelString() &&
4310       (Right.NextOperator || Right.OperatorIndex != 0)) {
4311     return 25;
4312   }
4313   if (Left.is(tok::comma))
4314     return 1;
4315   if (Right.is(tok::lessless) && Left.isLabelString() &&
4316       (Right.NextOperator || Right.OperatorIndex != 1)) {
4317     return 25;
4318   }
4319   if (Right.is(tok::lessless)) {
4320     // Breaking at a << is really cheap.
4321     if (Left.isNot(tok::r_paren) || Right.OperatorIndex > 0) {
4322       // Slightly prefer to break before the first one in log-like statements.
4323       return 2;
4324     }
4325     return 1;
4326   }
4327   if (Left.ClosesTemplateDeclaration)
4328     return Style.PenaltyBreakTemplateDeclaration;
4329   if (Left.ClosesRequiresClause)
4330     return 0;
4331   if (Left.is(TT_ConditionalExpr))
4332     return prec::Conditional;
4333   prec::Level Level = Left.getPrecedence();
4334   if (Level == prec::Unknown)
4335     Level = Right.getPrecedence();
4336   if (Level == prec::Assignment)
4337     return Style.PenaltyBreakAssignment;
4338   if (Level != prec::Unknown)
4339     return Level;
4340 
4341   return 3;
4342 }
4343 
4344 bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
4345   if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
4346     return true;
4347   if (Right.is(TT_OverloadedOperatorLParen) &&
4348       Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
4349     return true;
4350   }
4351   if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
4352       Right.ParameterCount > 0) {
4353     return true;
4354   }
4355   return false;
4356 }
4357 
4358 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
4359                                           const FormatToken &Left,
4360                                           const FormatToken &Right) const {
4361   if (Left.is(tok::kw_return) &&
4362       !Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
4363     return true;
4364   }
4365   if (Left.is(tok::kw_throw) && Right.is(tok::l_paren) && Right.MatchingParen &&
4366       Right.MatchingParen->is(TT_CastRParen)) {
4367     return true;
4368   }
4369   if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java)
4370     return true;
4371   if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
4372       Left.Tok.getObjCKeywordID() == tok::objc_property) {
4373     return true;
4374   }
4375   if (Right.is(tok::hashhash))
4376     return Left.is(tok::hash);
4377   if (Left.isOneOf(tok::hashhash, tok::hash))
4378     return Right.is(tok::hash);
4379   if (Left.is(BK_Block) && Right.is(tok::r_brace) &&
4380       Right.MatchingParen == &Left && Line.Children.empty()) {
4381     return Style.SpaceInEmptyBlock;
4382   }
4383   if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
4384       (Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
4385        Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
4386     return Style.SpacesInParensOptions.InEmptyParentheses;
4387   }
4388   if (Style.SpacesInParens == FormatStyle::SIPO_Custom &&
4389       Style.SpacesInParensOptions.ExceptDoubleParentheses &&
4390       Left.is(tok::r_paren) && Right.is(tok::r_paren)) {
4391     auto *InnerLParen = Left.MatchingParen;
4392     if (InnerLParen && InnerLParen->Previous == Right.MatchingParen) {
4393       InnerLParen->SpacesRequiredBefore = 0;
4394       return false;
4395     }
4396   }
4397   if (Style.SpacesInParensOptions.InConditionalStatements) {
4398     const FormatToken *LeftParen = nullptr;
4399     if (Left.is(tok::l_paren))
4400       LeftParen = &Left;
4401     else if (Right.is(tok::r_paren) && Right.MatchingParen)
4402       LeftParen = Right.MatchingParen;
4403     if (LeftParen) {
4404       if (LeftParen->is(TT_ConditionLParen))
4405         return true;
4406       if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous))
4407         return true;
4408     }
4409   }
4410 
4411   // trailing return type 'auto': []() -> auto {}, auto foo() -> auto {}
4412   if (Left.is(tok::kw_auto) && Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
4413                                              // function return type 'auto'
4414                                              TT_FunctionTypeLParen)) {
4415     return true;
4416   }
4417 
4418   // auto{x} auto(x)
4419   if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace))
4420     return false;
4421 
4422   const auto *BeforeLeft = Left.Previous;
4423 
4424   // operator co_await(x)
4425   if (Right.is(tok::l_paren) && Left.is(tok::kw_co_await) && BeforeLeft &&
4426       BeforeLeft->is(tok::kw_operator)) {
4427     return false;
4428   }
4429   // co_await (x), co_yield (x), co_return (x)
4430   if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
4431       !Right.isOneOf(tok::semi, tok::r_paren)) {
4432     return true;
4433   }
4434 
4435   if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
4436     return (Right.is(TT_CastRParen) ||
4437             (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
4438                ? Style.SpacesInParensOptions.InCStyleCasts
4439                : Style.SpacesInParensOptions.Other;
4440   }
4441   if (Right.isOneOf(tok::semi, tok::comma))
4442     return false;
4443   if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) {
4444     bool IsLightweightGeneric = Right.MatchingParen &&
4445                                 Right.MatchingParen->Next &&
4446                                 Right.MatchingParen->Next->is(tok::colon);
4447     return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
4448   }
4449   if (Right.is(tok::less) && Left.is(tok::kw_template))
4450     return Style.SpaceAfterTemplateKeyword;
4451   if (Left.isOneOf(tok::exclaim, tok::tilde))
4452     return false;
4453   if (Left.is(tok::at) &&
4454       Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
4455                     tok::numeric_constant, tok::l_paren, tok::l_brace,
4456                     tok::kw_true, tok::kw_false)) {
4457     return false;
4458   }
4459   if (Left.is(tok::colon))
4460     return Left.isNot(TT_ObjCMethodExpr);
4461   if (Left.is(tok::coloncolon)) {
4462     return Right.is(tok::star) && Right.is(TT_PointerOrReference) &&
4463            Style.PointerAlignment != FormatStyle::PAS_Left;
4464   }
4465   if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
4466     if (Style.Language == FormatStyle::LK_TextProto ||
4467         (Style.Language == FormatStyle::LK_Proto &&
4468          (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
4469       // Format empty list as `<>`.
4470       if (Left.is(tok::less) && Right.is(tok::greater))
4471         return false;
4472       return !Style.Cpp11BracedListStyle;
4473     }
4474     // Don't attempt to format operator<(), as it is handled later.
4475     if (Right.isNot(TT_OverloadedOperatorLParen))
4476       return false;
4477   }
4478   if (Right.is(tok::ellipsis)) {
4479     return Left.Tok.isLiteral() || (Left.is(tok::identifier) && BeforeLeft &&
4480                                     BeforeLeft->is(tok::kw_case));
4481   }
4482   if (Left.is(tok::l_square) && Right.is(tok::amp))
4483     return Style.SpacesInSquareBrackets;
4484   if (Right.is(TT_PointerOrReference)) {
4485     if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) {
4486       if (!Left.MatchingParen)
4487         return true;
4488       FormatToken *TokenBeforeMatchingParen =
4489           Left.MatchingParen->getPreviousNonComment();
4490       if (!TokenBeforeMatchingParen || Left.isNot(TT_TypeDeclarationParen))
4491         return true;
4492     }
4493     // Add a space if the previous token is a pointer qualifier or the closing
4494     // parenthesis of __attribute__(()) expression and the style requires spaces
4495     // after pointer qualifiers.
4496     if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4497          Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4498         (Left.is(TT_AttributeRParen) ||
4499          Left.canBePointerOrReferenceQualifier())) {
4500       return true;
4501     }
4502     if (Left.Tok.isLiteral())
4503       return true;
4504     // for (auto a = 0, b = 0; const auto & c : {1, 2, 3})
4505     if (Left.isTypeOrIdentifier(LangOpts) && Right.Next && Right.Next->Next &&
4506         Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4507       return getTokenPointerOrReferenceAlignment(Right) !=
4508              FormatStyle::PAS_Left;
4509     }
4510     return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
4511            (getTokenPointerOrReferenceAlignment(Right) !=
4512                 FormatStyle::PAS_Left ||
4513             (Line.IsMultiVariableDeclStmt &&
4514              (Left.NestingLevel == 0 ||
4515               (Left.NestingLevel == 1 && startsWithInitStatement(Line)))));
4516   }
4517   if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
4518       (Left.isNot(TT_PointerOrReference) ||
4519        (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4520         !Line.IsMultiVariableDeclStmt))) {
4521     return true;
4522   }
4523   if (Left.is(TT_PointerOrReference)) {
4524     // Add a space if the next token is a pointer qualifier and the style
4525     // requires spaces before pointer qualifiers.
4526     if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4527          Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4528         Right.canBePointerOrReferenceQualifier()) {
4529       return true;
4530     }
4531     // & 1
4532     if (Right.Tok.isLiteral())
4533       return true;
4534     // & /* comment
4535     if (Right.is(TT_BlockComment))
4536       return true;
4537     // foo() -> const Bar * override/final
4538     // S::foo() & noexcept/requires
4539     if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4540                       TT_RequiresClause) &&
4541         Right.isNot(TT_StartOfName)) {
4542       return true;
4543     }
4544     // & {
4545     if (Right.is(tok::l_brace) && Right.is(BK_Block))
4546       return true;
4547     // for (auto a = 0, b = 0; const auto& c : {1, 2, 3})
4548     if (BeforeLeft && BeforeLeft->isTypeOrIdentifier(LangOpts) && Right.Next &&
4549         Right.Next->is(TT_RangeBasedForLoopColon)) {
4550       return getTokenPointerOrReferenceAlignment(Left) !=
4551              FormatStyle::PAS_Right;
4552     }
4553     if (Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4554                       tok::l_paren)) {
4555       return false;
4556     }
4557     if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4558       return false;
4559     // FIXME: Setting IsMultiVariableDeclStmt for the whole line is error-prone,
4560     // because it does not take into account nested scopes like lambdas.
4561     // In multi-variable declaration statements, attach */& to the variable
4562     // independently of the style. However, avoid doing it if we are in a nested
4563     // scope, e.g. lambda. We still need to special-case statements with
4564     // initializers.
4565     if (Line.IsMultiVariableDeclStmt &&
4566         (Left.NestingLevel == Line.First->NestingLevel ||
4567          ((Left.NestingLevel == Line.First->NestingLevel + 1) &&
4568           startsWithInitStatement(Line)))) {
4569       return false;
4570     }
4571     if (!BeforeLeft)
4572       return false;
4573     if (BeforeLeft->is(tok::coloncolon)) {
4574       return Left.is(tok::star) &&
4575              Style.PointerAlignment != FormatStyle::PAS_Right;
4576     }
4577     return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
4578   }
4579   // Ensure right pointer alignment with ellipsis e.g. int *...P
4580   if (Left.is(tok::ellipsis) && BeforeLeft &&
4581       BeforeLeft->isPointerOrReference()) {
4582     return Style.PointerAlignment != FormatStyle::PAS_Right;
4583   }
4584 
4585   if (Right.is(tok::star) && Left.is(tok::l_paren))
4586     return false;
4587   if (Left.is(tok::star) && Right.isPointerOrReference())
4588     return false;
4589   if (Right.isPointerOrReference()) {
4590     const FormatToken *Previous = &Left;
4591     while (Previous && Previous->isNot(tok::kw_operator)) {
4592       if (Previous->is(tok::identifier) || Previous->isTypeName(LangOpts)) {
4593         Previous = Previous->getPreviousNonComment();
4594         continue;
4595       }
4596       if (Previous->is(TT_TemplateCloser) && Previous->MatchingParen) {
4597         Previous = Previous->MatchingParen->getPreviousNonComment();
4598         continue;
4599       }
4600       if (Previous->is(tok::coloncolon)) {
4601         Previous = Previous->getPreviousNonComment();
4602         continue;
4603       }
4604       break;
4605     }
4606     // Space between the type and the * in:
4607     //   operator void*()
4608     //   operator char*()
4609     //   operator void const*()
4610     //   operator void volatile*()
4611     //   operator /*comment*/ const char*()
4612     //   operator volatile /*comment*/ char*()
4613     //   operator Foo*()
4614     //   operator C<T>*()
4615     //   operator std::Foo*()
4616     //   operator C<T>::D<U>*()
4617     // dependent on PointerAlignment style.
4618     if (Previous) {
4619       if (Previous->endsSequence(tok::kw_operator))
4620         return Style.PointerAlignment != FormatStyle::PAS_Left;
4621       if (Previous->is(tok::kw_const) || Previous->is(tok::kw_volatile)) {
4622         return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4623                (Style.SpaceAroundPointerQualifiers ==
4624                 FormatStyle::SAPQ_After) ||
4625                (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4626       }
4627     }
4628   }
4629   if (Style.isCSharp() && Left.is(Keywords.kw_is) && Right.is(tok::l_square))
4630     return true;
4631   const auto SpaceRequiredForArrayInitializerLSquare =
4632       [](const FormatToken &LSquareTok, const FormatStyle &Style) {
4633         return Style.SpacesInContainerLiterals ||
4634                (Style.isProto() && !Style.Cpp11BracedListStyle &&
4635                 LSquareTok.endsSequence(tok::l_square, tok::colon,
4636                                         TT_SelectorName));
4637       };
4638   if (Left.is(tok::l_square)) {
4639     return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
4640             SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4641            (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4642                          TT_LambdaLSquare) &&
4643             Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
4644   }
4645   if (Right.is(tok::r_square)) {
4646     return Right.MatchingParen &&
4647            ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4648              SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
4649                                                      Style)) ||
4650             (Style.SpacesInSquareBrackets &&
4651              Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4652                                           TT_StructuredBindingLSquare,
4653                                           TT_LambdaLSquare)));
4654   }
4655   if (Right.is(tok::l_square) &&
4656       !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4657                      TT_DesignatedInitializerLSquare,
4658                      TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4659       !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
4660       !(Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4661         Right.is(TT_ArraySubscriptLSquare))) {
4662     return false;
4663   }
4664   if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
4665     return !Left.Children.empty(); // No spaces in "{}".
4666   if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) ||
4667       (Right.is(tok::r_brace) && Right.MatchingParen &&
4668        Right.MatchingParen->isNot(BK_Block))) {
4669     return !Style.Cpp11BracedListStyle || Style.SpacesInParensOptions.Other;
4670   }
4671   if (Left.is(TT_BlockComment)) {
4672     // No whitespace in x(/*foo=*/1), except for JavaScript.
4673     return Style.isJavaScript() || !Left.TokenText.ends_with("=*/");
4674   }
4675 
4676   // Space between template and attribute.
4677   // e.g. template <typename T> [[nodiscard]] ...
4678   if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
4679     return true;
4680   // Space before parentheses common for all languages
4681   if (Right.is(tok::l_paren)) {
4682     if (Left.is(TT_TemplateCloser) && Right.isNot(TT_FunctionTypeLParen))
4683       return spaceRequiredBeforeParens(Right);
4684     if (Left.isOneOf(TT_RequiresClause,
4685                      TT_RequiresClauseInARequiresExpression)) {
4686       return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4687              spaceRequiredBeforeParens(Right);
4688     }
4689     if (Left.is(TT_RequiresExpression)) {
4690       return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4691              spaceRequiredBeforeParens(Right);
4692     }
4693     if (Left.is(TT_AttributeRParen) ||
4694         (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
4695       return true;
4696     }
4697     if (Left.is(TT_ForEachMacro)) {
4698       return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4699              spaceRequiredBeforeParens(Right);
4700     }
4701     if (Left.is(TT_IfMacro)) {
4702       return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4703              spaceRequiredBeforeParens(Right);
4704     }
4705     if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
4706         Left.isOneOf(tok::kw_new, tok::kw_delete) &&
4707         Right.isNot(TT_OverloadedOperatorLParen) &&
4708         !(Line.MightBeFunctionDecl && Left.is(TT_FunctionDeclarationName))) {
4709       return Style.SpaceBeforeParensOptions.AfterPlacementOperator;
4710     }
4711     if (Line.Type == LT_ObjCDecl)
4712       return true;
4713     if (Left.is(tok::semi))
4714       return true;
4715     if (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4716                      tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4717         Left.isIf(Line.Type != LT_PreprocessorDirective) ||
4718         Right.is(TT_ConditionLParen)) {
4719       return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4720              spaceRequiredBeforeParens(Right);
4721     }
4722 
4723     // TODO add Operator overloading specific Options to
4724     // SpaceBeforeParensOptions
4725     if (Right.is(TT_OverloadedOperatorLParen))
4726       return spaceRequiredBeforeParens(Right);
4727     // Function declaration or definition
4728     if (Line.MightBeFunctionDecl && Right.is(TT_FunctionDeclarationLParen)) {
4729       if (spaceRequiredBeforeParens(Right))
4730         return true;
4731       const auto &Options = Style.SpaceBeforeParensOptions;
4732       return Line.mightBeFunctionDefinition()
4733                  ? Options.AfterFunctionDefinitionName
4734                  : Options.AfterFunctionDeclarationName;
4735     }
4736     // Lambda
4737     if (Line.Type != LT_PreprocessorDirective && Left.is(tok::r_square) &&
4738         Left.MatchingParen && Left.MatchingParen->is(TT_LambdaLSquare)) {
4739       return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4740              spaceRequiredBeforeParens(Right);
4741     }
4742     if (!BeforeLeft || !BeforeLeft->isOneOf(tok::period, tok::arrow)) {
4743       if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4744         return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4745                spaceRequiredBeforeParens(Right);
4746       }
4747       if (Left.isOneOf(tok::kw_new, tok::kw_delete)) {
4748         return ((!Line.MightBeFunctionDecl || !BeforeLeft) &&
4749                 Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
4750                spaceRequiredBeforeParens(Right);
4751       }
4752 
4753       if (Left.is(tok::r_square) && Left.MatchingParen &&
4754           Left.MatchingParen->Previous &&
4755           Left.MatchingParen->Previous->is(tok::kw_delete)) {
4756         return (Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
4757                spaceRequiredBeforeParens(Right);
4758       }
4759     }
4760     // Handle builtins like identifiers.
4761     if (Line.Type != LT_PreprocessorDirective &&
4762         (Left.Tok.getIdentifierInfo() || Left.is(tok::r_paren))) {
4763       return spaceRequiredBeforeParens(Right);
4764     }
4765     return false;
4766   }
4767   if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
4768     return false;
4769   if (Right.is(TT_UnaryOperator)) {
4770     return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
4771            (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
4772   }
4773   // No space between the variable name and the initializer list.
4774   // A a1{1};
4775   // Verilog doesn't have such syntax, but it has word operators that are C++
4776   // identifiers like `a inside {b, c}`. So the rule is not applicable.
4777   if (!Style.isVerilog() &&
4778       (Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4779                     tok::r_paren) ||
4780        Left.isTypeName(LangOpts)) &&
4781       Right.is(tok::l_brace) && Right.getNextNonComment() &&
4782       Right.isNot(BK_Block)) {
4783     return false;
4784   }
4785   if (Left.is(tok::period) || Right.is(tok::period))
4786     return false;
4787   // u#str, U#str, L#str, u8#str
4788   // uR#str, UR#str, LR#str, u8R#str
4789   if (Right.is(tok::hash) && Left.is(tok::identifier) &&
4790       (Left.TokenText == "L" || Left.TokenText == "u" ||
4791        Left.TokenText == "U" || Left.TokenText == "u8" ||
4792        Left.TokenText == "LR" || Left.TokenText == "uR" ||
4793        Left.TokenText == "UR" || Left.TokenText == "u8R")) {
4794     return false;
4795   }
4796   if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
4797       Left.MatchingParen->Previous &&
4798       (Left.MatchingParen->Previous->is(tok::period) ||
4799        Left.MatchingParen->Previous->is(tok::coloncolon))) {
4800     // Java call to generic function with explicit type:
4801     // A.<B<C<...>>>DoSomething();
4802     // A::<B<C<...>>>DoSomething();  // With a Java 8 method reference.
4803     return false;
4804   }
4805   if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
4806     return false;
4807   if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at)) {
4808     // Objective-C dictionary literal -> no space after opening brace.
4809     return false;
4810   }
4811   if (Right.is(tok::r_brace) && Right.MatchingParen &&
4812       Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4813     // Objective-C dictionary literal -> no space before closing brace.
4814     return false;
4815   }
4816   if (Right.is(TT_TrailingAnnotation) && Right.isOneOf(tok::amp, tok::ampamp) &&
4817       Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4818       (!Right.Next || Right.Next->is(tok::semi))) {
4819     // Match const and volatile ref-qualifiers without any additional
4820     // qualifiers such as
4821     // void Fn() const &;
4822     return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
4823   }
4824 
4825   return true;
4826 }
4827 
4828 bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
4829                                          const FormatToken &Right) const {
4830   const FormatToken &Left = *Right.Previous;
4831 
4832   // If the token is finalized don't touch it (as it could be in a
4833   // clang-format-off section).
4834   if (Left.Finalized)
4835     return Right.hasWhitespaceBefore();
4836 
4837   const bool IsVerilog = Style.isVerilog();
4838   assert(!IsVerilog || !IsCpp);
4839 
4840   // Never ever merge two words.
4841   if (Keywords.isWordLike(Right, IsVerilog) &&
4842       Keywords.isWordLike(Left, IsVerilog)) {
4843     return true;
4844   }
4845 
4846   // Leave a space between * and /* to avoid C4138 `comment end` found outside
4847   // of comment.
4848   if (Left.is(tok::star) && Right.is(tok::comment))
4849     return true;
4850 
4851   if (IsCpp) {
4852     if (Left.is(TT_OverloadedOperator) &&
4853         Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
4854       return true;
4855     }
4856     // Space between UDL and dot: auto b = 4s .count();
4857     if (Right.is(tok::period) && Left.is(tok::numeric_constant))
4858       return true;
4859     // Space between import <iostream>.
4860     // or import .....;
4861     if (Left.is(Keywords.kw_import) && Right.isOneOf(tok::less, tok::ellipsis))
4862       return true;
4863     // Space between `module :` and `import :`.
4864     if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
4865         Right.is(TT_ModulePartitionColon)) {
4866       return true;
4867     }
4868     // No space between import foo:bar but keep a space between import :bar;
4869     if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
4870       return false;
4871     // No space between :bar;
4872     if (Left.is(TT_ModulePartitionColon) &&
4873         Right.isOneOf(tok::identifier, tok::kw_private)) {
4874       return false;
4875     }
4876     if (Left.is(tok::ellipsis) && Right.is(tok::identifier) &&
4877         Line.First->is(Keywords.kw_import)) {
4878       return false;
4879     }
4880     // Space in __attribute__((attr)) ::type.
4881     if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
4882         Right.is(tok::coloncolon)) {
4883       return true;
4884     }
4885 
4886     if (Left.is(tok::kw_operator))
4887       return Right.is(tok::coloncolon);
4888     if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
4889         !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
4890       return true;
4891     }
4892     if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) &&
4893         Right.is(TT_TemplateOpener)) {
4894       return true;
4895     }
4896     // C++ Core Guidelines suppression tag, e.g. `[[suppress(type.5)]]`.
4897     if (Left.is(tok::identifier) && Right.is(tok::numeric_constant))
4898       return Right.TokenText[0] != '.';
4899     // `Left` is a keyword (including C++ alternative operator) or identifier.
4900     if (Left.Tok.getIdentifierInfo() && Right.Tok.isLiteral())
4901       return true;
4902   } else if (Style.isProto()) {
4903     if (Right.is(tok::period) &&
4904         Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
4905                      Keywords.kw_repeated, Keywords.kw_extend)) {
4906       return true;
4907     }
4908     if (Right.is(tok::l_paren) &&
4909         Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
4910       return true;
4911     }
4912     if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
4913       return true;
4914     // Slashes occur in text protocol extension syntax: [type/type] { ... }.
4915     if (Left.is(tok::slash) || Right.is(tok::slash))
4916       return false;
4917     if (Left.MatchingParen &&
4918         Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
4919         Right.isOneOf(tok::l_brace, tok::less)) {
4920       return !Style.Cpp11BracedListStyle;
4921     }
4922     // A percent is probably part of a formatting specification, such as %lld.
4923     if (Left.is(tok::percent))
4924       return false;
4925     // Preserve the existence of a space before a percent for cases like 0x%04x
4926     // and "%d %d"
4927     if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
4928       return Right.hasWhitespaceBefore();
4929   } else if (Style.isJson()) {
4930     if (Right.is(tok::colon) && Left.is(tok::string_literal))
4931       return Style.SpaceBeforeJsonColon;
4932   } else if (Style.isCSharp()) {
4933     // Require spaces around '{' and  before '}' unless they appear in
4934     // interpolated strings. Interpolated strings are merged into a single token
4935     // so cannot have spaces inserted by this function.
4936 
4937     // No space between 'this' and '['
4938     if (Left.is(tok::kw_this) && Right.is(tok::l_square))
4939       return false;
4940 
4941     // No space between 'new' and '('
4942     if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
4943       return false;
4944 
4945     // Space before { (including space within '{ {').
4946     if (Right.is(tok::l_brace))
4947       return true;
4948 
4949     // Spaces inside braces.
4950     if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace))
4951       return true;
4952 
4953     if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace))
4954       return true;
4955 
4956     // Spaces around '=>'.
4957     if (Left.is(TT_FatArrow) || Right.is(TT_FatArrow))
4958       return true;
4959 
4960     // No spaces around attribute target colons
4961     if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon))
4962       return false;
4963 
4964     // space between type and variable e.g. Dictionary<string,string> foo;
4965     if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
4966       return true;
4967 
4968     // spaces inside square brackets.
4969     if (Left.is(tok::l_square) || Right.is(tok::r_square))
4970       return Style.SpacesInSquareBrackets;
4971 
4972     // No space before ? in nullable types.
4973     if (Right.is(TT_CSharpNullable))
4974       return false;
4975 
4976     // No space before null forgiving '!'.
4977     if (Right.is(TT_NonNullAssertion))
4978       return false;
4979 
4980     // No space between consecutive commas '[,,]'.
4981     if (Left.is(tok::comma) && Right.is(tok::comma))
4982       return false;
4983 
4984     // space after var in `var (key, value)`
4985     if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren))
4986       return true;
4987 
4988     // space between keywords and paren e.g. "using ("
4989     if (Right.is(tok::l_paren)) {
4990       if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
4991                        Keywords.kw_lock)) {
4992         return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4993                spaceRequiredBeforeParens(Right);
4994       }
4995     }
4996 
4997     // space between method modifier and opening parenthesis of a tuple return
4998     // type
4999     if ((Left.isAccessSpecifierKeyword() ||
5000          Left.isOneOf(tok::kw_virtual, tok::kw_extern, tok::kw_static,
5001                       Keywords.kw_internal, Keywords.kw_abstract,
5002                       Keywords.kw_sealed, Keywords.kw_override,
5003                       Keywords.kw_async, Keywords.kw_unsafe)) &&
5004         Right.is(tok::l_paren)) {
5005       return true;
5006     }
5007   } else if (Style.isJavaScript()) {
5008     if (Left.is(TT_FatArrow))
5009       return true;
5010     // for await ( ...
5011     if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
5012         Left.Previous->is(tok::kw_for)) {
5013       return true;
5014     }
5015     if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
5016         Right.MatchingParen) {
5017       const FormatToken *Next = Right.MatchingParen->getNextNonComment();
5018       // An async arrow function, for example: `x = async () => foo();`,
5019       // as opposed to calling a function called async: `x = async();`
5020       if (Next && Next->is(TT_FatArrow))
5021         return true;
5022     }
5023     if ((Left.is(TT_TemplateString) && Left.TokenText.ends_with("${")) ||
5024         (Right.is(TT_TemplateString) && Right.TokenText.starts_with("}"))) {
5025       return false;
5026     }
5027     // In tagged template literals ("html`bar baz`"), there is no space between
5028     // the tag identifier and the template string.
5029     if (Keywords.isJavaScriptIdentifier(Left,
5030                                         /* AcceptIdentifierName= */ false) &&
5031         Right.is(TT_TemplateString)) {
5032       return false;
5033     }
5034     if (Right.is(tok::star) &&
5035         Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
5036       return false;
5037     }
5038     if (Right.isOneOf(tok::l_brace, tok::l_square) &&
5039         Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
5040                      Keywords.kw_extends, Keywords.kw_implements)) {
5041       return true;
5042     }
5043     if (Right.is(tok::l_paren)) {
5044       // JS methods can use some keywords as names (e.g. `delete()`).
5045       if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
5046         return false;
5047       // Valid JS method names can include keywords, e.g. `foo.delete()` or
5048       // `bar.instanceof()`. Recognize call positions by preceding period.
5049       if (Left.Previous && Left.Previous->is(tok::period) &&
5050           Left.Tok.getIdentifierInfo()) {
5051         return false;
5052       }
5053       // Additional unary JavaScript operators that need a space after.
5054       if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
5055                        tok::kw_void)) {
5056         return true;
5057       }
5058     }
5059     // `foo as const;` casts into a const type.
5060     if (Left.endsSequence(tok::kw_const, Keywords.kw_as))
5061       return false;
5062     if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
5063                       tok::kw_const) ||
5064          // "of" is only a keyword if it appears after another identifier
5065          // (e.g. as "const x of y" in a for loop), or after a destructuring
5066          // operation (const [x, y] of z, const {a, b} of c).
5067          (Left.is(Keywords.kw_of) && Left.Previous &&
5068           (Left.Previous->is(tok::identifier) ||
5069            Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
5070         (!Left.Previous || Left.Previous->isNot(tok::period))) {
5071       return true;
5072     }
5073     if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
5074         Left.Previous->is(tok::period) && Right.is(tok::l_paren)) {
5075       return false;
5076     }
5077     if (Left.is(Keywords.kw_as) &&
5078         Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
5079       return true;
5080     }
5081     if (Left.is(tok::kw_default) && Left.Previous &&
5082         Left.Previous->is(tok::kw_export)) {
5083       return true;
5084     }
5085     if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
5086       return true;
5087     if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
5088       return false;
5089     if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
5090       return false;
5091     if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
5092         Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
5093       return false;
5094     }
5095     if (Left.is(tok::ellipsis))
5096       return false;
5097     if (Left.is(TT_TemplateCloser) &&
5098         !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
5099                        Keywords.kw_implements, Keywords.kw_extends)) {
5100       // Type assertions ('<type>expr') are not followed by whitespace. Other
5101       // locations that should have whitespace following are identified by the
5102       // above set of follower tokens.
5103       return false;
5104     }
5105     if (Right.is(TT_NonNullAssertion))
5106       return false;
5107     if (Left.is(TT_NonNullAssertion) &&
5108         Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
5109       return true; // "x! as string", "x! in y"
5110     }
5111   } else if (Style.Language == FormatStyle::LK_Java) {
5112     if (Left.is(TT_CaseLabelArrow) || Right.is(TT_CaseLabelArrow))
5113       return true;
5114     if (Left.is(tok::r_square) && Right.is(tok::l_brace))
5115       return true;
5116     // spaces inside square brackets.
5117     if (Left.is(tok::l_square) || Right.is(tok::r_square))
5118       return Style.SpacesInSquareBrackets;
5119 
5120     if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) {
5121       return Style.SpaceBeforeParensOptions.AfterControlStatements ||
5122              spaceRequiredBeforeParens(Right);
5123     }
5124     if ((Left.isAccessSpecifierKeyword() ||
5125          Left.isOneOf(tok::kw_static, Keywords.kw_final, Keywords.kw_abstract,
5126                       Keywords.kw_native)) &&
5127         Right.is(TT_TemplateOpener)) {
5128       return true;
5129     }
5130   } else if (IsVerilog) {
5131     // An escaped identifier ends with whitespace.
5132     if (Left.is(tok::identifier) && Left.TokenText[0] == '\\')
5133       return true;
5134     // Add space between things in a primitive's state table unless in a
5135     // transition like `(0?)`.
5136     if ((Left.is(TT_VerilogTableItem) &&
5137          !Right.isOneOf(tok::r_paren, tok::semi)) ||
5138         (Right.is(TT_VerilogTableItem) && Left.isNot(tok::l_paren))) {
5139       const FormatToken *Next = Right.getNextNonComment();
5140       return !(Next && Next->is(tok::r_paren));
5141     }
5142     // Don't add space within a delay like `#0`.
5143     if (Left.isNot(TT_BinaryOperator) &&
5144         Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
5145       return false;
5146     }
5147     // Add space after a delay.
5148     if (Right.isNot(tok::semi) &&
5149         (Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
5150          Left.endsSequence(tok::numeric_constant,
5151                            Keywords.kw_verilogHashHash) ||
5152          (Left.is(tok::r_paren) && Left.MatchingParen &&
5153           Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
5154       return true;
5155     }
5156     // Don't add embedded spaces in a number literal like `16'h1?ax` or an array
5157     // literal like `'{}`.
5158     if (Left.is(Keywords.kw_apostrophe) ||
5159         (Left.is(TT_VerilogNumberBase) && Right.is(tok::numeric_constant))) {
5160       return false;
5161     }
5162     // Add spaces around the implication operator `->`.
5163     if (Left.is(tok::arrow) || Right.is(tok::arrow))
5164       return true;
5165     // Don't add spaces between two at signs. Like in a coverage event.
5166     // Don't add spaces between at and a sensitivity list like
5167     // `@(posedge clk)`.
5168     if (Left.is(tok::at) && Right.isOneOf(tok::l_paren, tok::star, tok::at))
5169       return false;
5170     // Add space between the type name and dimension like `logic [1:0]`.
5171     if (Right.is(tok::l_square) &&
5172         Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
5173       return true;
5174     }
5175     // In a tagged union expression, there should be a space after the tag.
5176     if (Right.isOneOf(tok::period, Keywords.kw_apostrophe) &&
5177         Keywords.isVerilogIdentifier(Left) && Left.getPreviousNonComment() &&
5178         Left.getPreviousNonComment()->is(Keywords.kw_tagged)) {
5179       return true;
5180     }
5181     // Don't add spaces between a casting type and the quote or repetition count
5182     // and the brace. The case of tagged union expressions is handled by the
5183     // previous rule.
5184     if ((Right.is(Keywords.kw_apostrophe) ||
5185          (Right.is(BK_BracedInit) && Right.is(tok::l_brace))) &&
5186         !(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
5187           Keywords.isVerilogWordOperator(Left)) &&
5188         (Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
5189                       tok::numeric_constant) ||
5190          Keywords.isWordLike(Left))) {
5191       return false;
5192     }
5193     // Don't add spaces in imports like `import foo::*;`.
5194     if ((Right.is(tok::star) && Left.is(tok::coloncolon)) ||
5195         (Left.is(tok::star) && Right.is(tok::semi))) {
5196       return false;
5197     }
5198     // Add space in attribute like `(* ASYNC_REG = "TRUE" *)`.
5199     if (Left.endsSequence(tok::star, tok::l_paren) && Right.is(tok::identifier))
5200       return true;
5201     // Add space before drive strength like in `wire (strong1, pull0)`.
5202     if (Right.is(tok::l_paren) && Right.is(TT_VerilogStrength))
5203       return true;
5204     // Don't add space in a streaming concatenation like `{>>{j}}`.
5205     if ((Left.is(tok::l_brace) &&
5206          Right.isOneOf(tok::lessless, tok::greatergreater)) ||
5207         (Left.endsSequence(tok::lessless, tok::l_brace) ||
5208          Left.endsSequence(tok::greatergreater, tok::l_brace))) {
5209       return false;
5210     }
5211   } else if (Style.isTableGen()) {
5212     // Avoid to connect [ and {. [{ is start token of multiline string.
5213     if (Left.is(tok::l_square) && Right.is(tok::l_brace))
5214       return true;
5215     if (Left.is(tok::r_brace) && Right.is(tok::r_square))
5216       return true;
5217     // Do not insert around colon in DAGArg and cond operator.
5218     if (Right.isOneOf(TT_TableGenDAGArgListColon,
5219                       TT_TableGenDAGArgListColonToAlign) ||
5220         Left.isOneOf(TT_TableGenDAGArgListColon,
5221                      TT_TableGenDAGArgListColonToAlign)) {
5222       return false;
5223     }
5224     if (Right.is(TT_TableGenCondOperatorColon))
5225       return false;
5226     if (Left.isOneOf(TT_TableGenDAGArgOperatorID,
5227                      TT_TableGenDAGArgOperatorToBreak) &&
5228         Right.isNot(TT_TableGenDAGArgCloser)) {
5229       return true;
5230     }
5231     // Do not insert bang operators and consequent openers.
5232     if (Right.isOneOf(tok::l_paren, tok::less) &&
5233         Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator)) {
5234       return false;
5235     }
5236     // Trailing paste requires space before '{' or ':', the case in name values.
5237     // Not before ';', the case in normal values.
5238     if (Left.is(TT_TableGenTrailingPasteOperator) &&
5239         Right.isOneOf(tok::l_brace, tok::colon)) {
5240       return true;
5241     }
5242     // Otherwise paste operator does not prefer space around.
5243     if (Left.is(tok::hash) || Right.is(tok::hash))
5244       return false;
5245     // Sure not to connect after defining keywords.
5246     if (Keywords.isTableGenDefinition(Left))
5247       return true;
5248   }
5249 
5250   if (Left.is(TT_ImplicitStringLiteral))
5251     return Right.hasWhitespaceBefore();
5252   if (Line.Type == LT_ObjCMethodDecl) {
5253     if (Left.is(TT_ObjCMethodSpecifier))
5254       return true;
5255     if (Left.is(tok::r_paren) && Left.isNot(TT_AttributeRParen) &&
5256         canBeObjCSelectorComponent(Right)) {
5257       // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a
5258       // keyword in Objective-C, and '+ (instancetype)new;' is a standard class
5259       // method declaration.
5260       return false;
5261     }
5262   }
5263   if (Line.Type == LT_ObjCProperty &&
5264       (Right.is(tok::equal) || Left.is(tok::equal))) {
5265     return false;
5266   }
5267 
5268   if (Right.is(TT_TrailingReturnArrow) || Left.is(TT_TrailingReturnArrow))
5269     return true;
5270 
5271   if (Left.is(tok::comma) && Right.isNot(TT_OverloadedOperatorLParen) &&
5272       // In an unexpanded macro call we only find the parentheses and commas
5273       // in a line; the commas and closing parenthesis do not require a space.
5274       (Left.Children.empty() || !Left.MacroParent)) {
5275     return true;
5276   }
5277   if (Right.is(tok::comma))
5278     return false;
5279   if (Right.is(TT_ObjCBlockLParen))
5280     return true;
5281   if (Right.is(TT_CtorInitializerColon))
5282     return Style.SpaceBeforeCtorInitializerColon;
5283   if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
5284     return false;
5285   if (Right.is(TT_RangeBasedForLoopColon) &&
5286       !Style.SpaceBeforeRangeBasedForLoopColon) {
5287     return false;
5288   }
5289   if (Left.is(TT_BitFieldColon)) {
5290     return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5291            Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
5292   }
5293   if (Right.is(tok::colon)) {
5294     if (Right.is(TT_CaseLabelColon))
5295       return Style.SpaceBeforeCaseColon;
5296     if (Right.is(TT_GotoLabelColon))
5297       return false;
5298     // `private:` and `public:`.
5299     if (!Right.getNextNonComment())
5300       return false;
5301     if (Right.is(TT_ObjCMethodExpr))
5302       return false;
5303     if (Left.is(tok::question))
5304       return false;
5305     if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
5306       return false;
5307     if (Right.is(TT_DictLiteral))
5308       return Style.SpacesInContainerLiterals;
5309     if (Right.is(TT_AttributeColon))
5310       return false;
5311     if (Right.is(TT_CSharpNamedArgumentColon))
5312       return false;
5313     if (Right.is(TT_GenericSelectionColon))
5314       return false;
5315     if (Right.is(TT_BitFieldColon)) {
5316       return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
5317              Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
5318     }
5319     return true;
5320   }
5321   // Do not merge "- -" into "--".
5322   if ((Left.isOneOf(tok::minus, tok::minusminus) &&
5323        Right.isOneOf(tok::minus, tok::minusminus)) ||
5324       (Left.isOneOf(tok::plus, tok::plusplus) &&
5325        Right.isOneOf(tok::plus, tok::plusplus))) {
5326     return true;
5327   }
5328   if (Left.is(TT_UnaryOperator)) {
5329     // Lambda captures allow for a lone &, so "&]" needs to be properly
5330     // handled.
5331     if (Left.is(tok::amp) && Right.is(tok::r_square))
5332       return Style.SpacesInSquareBrackets;
5333     return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim);
5334   }
5335 
5336   // If the next token is a binary operator or a selector name, we have
5337   // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
5338   if (Left.is(TT_CastRParen)) {
5339     return Style.SpaceAfterCStyleCast ||
5340            Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
5341   }
5342 
5343   auto ShouldAddSpacesInAngles = [this, &Right]() {
5344     if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
5345       return true;
5346     if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
5347       return Right.hasWhitespaceBefore();
5348     return false;
5349   };
5350 
5351   if (Left.is(tok::greater) && Right.is(tok::greater)) {
5352     if (Style.Language == FormatStyle::LK_TextProto ||
5353         (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral))) {
5354       return !Style.Cpp11BracedListStyle;
5355     }
5356     return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
5357            ((Style.Standard < FormatStyle::LS_Cpp11) ||
5358             ShouldAddSpacesInAngles());
5359   }
5360   if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
5361       Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
5362       (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) {
5363     return false;
5364   }
5365   if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) &&
5366       Right.getPrecedence() == prec::Assignment) {
5367     return false;
5368   }
5369   if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) &&
5370       (Left.is(tok::identifier) || Left.is(tok::kw_this))) {
5371     return false;
5372   }
5373   if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) {
5374     // Generally don't remove existing spaces between an identifier and "::".
5375     // The identifier might actually be a macro name such as ALWAYS_INLINE. If
5376     // this turns out to be too lenient, add analysis of the identifier itself.
5377     return Right.hasWhitespaceBefore();
5378   }
5379   if (Right.is(tok::coloncolon) &&
5380       !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
5381     // Put a space between < and :: in vector< ::std::string >
5382     return (Left.is(TT_TemplateOpener) &&
5383             ((Style.Standard < FormatStyle::LS_Cpp11) ||
5384              ShouldAddSpacesInAngles())) ||
5385            !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
5386                           tok::kw___super, TT_TemplateOpener,
5387                           TT_TemplateCloser)) ||
5388            (Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
5389   }
5390   if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
5391     return ShouldAddSpacesInAngles();
5392   // Space before TT_StructuredBindingLSquare.
5393   if (Right.is(TT_StructuredBindingLSquare)) {
5394     return !Left.isOneOf(tok::amp, tok::ampamp) ||
5395            getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
5396   }
5397   // Space before & or && following a TT_StructuredBindingLSquare.
5398   if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) &&
5399       Right.isOneOf(tok::amp, tok::ampamp)) {
5400     return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
5401   }
5402   if ((Right.is(TT_BinaryOperator) && Left.isNot(tok::l_paren)) ||
5403       (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
5404        Right.isNot(tok::r_paren))) {
5405     return true;
5406   }
5407   if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
5408       Left.MatchingParen &&
5409       Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
5410     return false;
5411   }
5412   if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
5413       Line.Type == LT_ImportStatement) {
5414     return true;
5415   }
5416   if (Right.is(TT_TrailingUnaryOperator))
5417     return false;
5418   if (Left.is(TT_RegexLiteral))
5419     return false;
5420   return spaceRequiredBetween(Line, Left, Right);
5421 }
5422 
5423 // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
5424 static bool isAllmanBrace(const FormatToken &Tok) {
5425   return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
5426          !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
5427 }
5428 
5429 // Returns 'true' if 'Tok' is a function argument.
5430 static bool IsFunctionArgument(const FormatToken &Tok) {
5431   return Tok.MatchingParen && Tok.MatchingParen->Next &&
5432          Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren);
5433 }
5434 
5435 static bool
5436 isItAnEmptyLambdaAllowed(const FormatToken &Tok,
5437                          FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5438   return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
5439 }
5440 
5441 static bool isAllmanLambdaBrace(const FormatToken &Tok) {
5442   return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
5443          !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
5444 }
5445 
5446 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
5447                                      const FormatToken &Right) const {
5448   const FormatToken &Left = *Right.Previous;
5449   if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
5450     return true;
5451 
5452   if (Style.BreakFunctionDefinitionParameters && Line.MightBeFunctionDecl &&
5453       Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
5454       Left.ParameterCount > 0) {
5455     return true;
5456   }
5457 
5458   const auto *BeforeLeft = Left.Previous;
5459   const auto *AfterRight = Right.Next;
5460 
5461   if (Style.isCSharp()) {
5462     if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
5463         Style.BraceWrapping.AfterFunction) {
5464       return true;
5465     }
5466     if (Right.is(TT_CSharpNamedArgumentColon) ||
5467         Left.is(TT_CSharpNamedArgumentColon)) {
5468       return false;
5469     }
5470     if (Right.is(TT_CSharpGenericTypeConstraint))
5471       return true;
5472     if (AfterRight && AfterRight->is(TT_FatArrow) &&
5473         (Right.is(tok::numeric_constant) ||
5474          (Right.is(tok::identifier) && Right.TokenText == "_"))) {
5475       return true;
5476     }
5477 
5478     // Break after C# [...] and before public/protected/private/internal.
5479     if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) &&
5480         (Right.isAccessSpecifier(/*ColonRequired=*/false) ||
5481          Right.is(Keywords.kw_internal))) {
5482       return true;
5483     }
5484     // Break between ] and [ but only when there are really 2 attributes.
5485     if (Left.is(TT_AttributeSquare) && Right.is(TT_AttributeSquare) &&
5486         Left.is(tok::r_square) && Right.is(tok::l_square)) {
5487       return true;
5488     }
5489   } else if (Style.isJavaScript()) {
5490     // FIXME: This might apply to other languages and token kinds.
5491     if (Right.is(tok::string_literal) && Left.is(tok::plus) && BeforeLeft &&
5492         BeforeLeft->is(tok::string_literal)) {
5493       return true;
5494     }
5495     if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
5496         BeforeLeft && BeforeLeft->is(tok::equal) &&
5497         Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
5498                             tok::kw_const) &&
5499         // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
5500         // above.
5501         !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
5502       // Object literals on the top level of a file are treated as "enum-style".
5503       // Each key/value pair is put on a separate line, instead of bin-packing.
5504       return true;
5505     }
5506     if (Left.is(tok::l_brace) && Line.Level == 0 &&
5507         (Line.startsWith(tok::kw_enum) ||
5508          Line.startsWith(tok::kw_const, tok::kw_enum) ||
5509          Line.startsWith(tok::kw_export, tok::kw_enum) ||
5510          Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
5511       // JavaScript top-level enum key/value pairs are put on separate lines
5512       // instead of bin-packing.
5513       return true;
5514     }
5515     if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && BeforeLeft &&
5516         BeforeLeft->is(TT_FatArrow)) {
5517       // JS arrow function (=> {...}).
5518       switch (Style.AllowShortLambdasOnASingleLine) {
5519       case FormatStyle::SLS_All:
5520         return false;
5521       case FormatStyle::SLS_None:
5522         return true;
5523       case FormatStyle::SLS_Empty:
5524         return !Left.Children.empty();
5525       case FormatStyle::SLS_Inline:
5526         // allow one-lining inline (e.g. in function call args) and empty arrow
5527         // functions.
5528         return (Left.NestingLevel == 0 && Line.Level == 0) &&
5529                !Left.Children.empty();
5530       }
5531       llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum");
5532     }
5533 
5534     if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
5535         !Left.Children.empty()) {
5536       // Support AllowShortFunctionsOnASingleLine for JavaScript.
5537       return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
5538              Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
5539              (Left.NestingLevel == 0 && Line.Level == 0 &&
5540               Style.AllowShortFunctionsOnASingleLine &
5541                   FormatStyle::SFS_InlineOnly);
5542     }
5543   } else if (Style.Language == FormatStyle::LK_Java) {
5544     if (Right.is(tok::plus) && Left.is(tok::string_literal) && AfterRight &&
5545         AfterRight->is(tok::string_literal)) {
5546       return true;
5547     }
5548   } else if (Style.isVerilog()) {
5549     // Break between assignments.
5550     if (Left.is(TT_VerilogAssignComma))
5551       return true;
5552     // Break between ports of different types.
5553     if (Left.is(TT_VerilogTypeComma))
5554       return true;
5555     // Break between ports in a module instantiation and after the parameter
5556     // list.
5557     if (Style.VerilogBreakBetweenInstancePorts &&
5558         (Left.is(TT_VerilogInstancePortComma) ||
5559          (Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5560           Left.MatchingParen &&
5561           Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5562       return true;
5563     }
5564     // Break after labels. In Verilog labels don't have the 'case' keyword, so
5565     // it is hard to identify them in UnwrappedLineParser.
5566     if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5567       return true;
5568   } else if (Style.BreakAdjacentStringLiterals &&
5569              (IsCpp || Style.isProto() ||
5570               Style.Language == FormatStyle::LK_TableGen)) {
5571     if (Left.isStringLiteral() && Right.isStringLiteral())
5572       return true;
5573   }
5574 
5575   // Basic JSON newline processing.
5576   if (Style.isJson()) {
5577     // Always break after a JSON record opener.
5578     // {
5579     // }
5580     if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace))
5581       return true;
5582     // Always break after a JSON array opener based on BreakArrays.
5583     if ((Left.is(TT_ArrayInitializerLSquare) && Left.is(tok::l_square) &&
5584          Right.isNot(tok::r_square)) ||
5585         Left.is(tok::comma)) {
5586       if (Right.is(tok::l_brace))
5587         return true;
5588       // scan to the right if an we see an object or an array inside
5589       // then break.
5590       for (const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5591         if (Tok->isOneOf(tok::l_brace, tok::l_square))
5592           return true;
5593         if (Tok->isOneOf(tok::r_brace, tok::r_square))
5594           break;
5595       }
5596       return Style.BreakArrays;
5597     }
5598   } else if (Style.isTableGen()) {
5599     // Break the comma in side cond operators.
5600     // !cond(case1:1,
5601     //       case2:0);
5602     if (Left.is(TT_TableGenCondOperatorComma))
5603       return true;
5604     if (Left.is(TT_TableGenDAGArgOperatorToBreak) &&
5605         Right.isNot(TT_TableGenDAGArgCloser)) {
5606       return true;
5607     }
5608     if (Left.is(TT_TableGenDAGArgListCommaToBreak))
5609       return true;
5610     if (Right.is(TT_TableGenDAGArgCloser) && Right.MatchingParen &&
5611         Right.MatchingParen->is(TT_TableGenDAGArgOpenerToBreak) &&
5612         &Left != Right.MatchingParen->Next) {
5613       // Check to avoid empty DAGArg such as (ins).
5614       return Style.TableGenBreakInsideDAGArg == FormatStyle::DAS_BreakAll;
5615     }
5616   }
5617 
5618   if (Line.startsWith(tok::kw_asm) && Right.is(TT_InlineASMColon) &&
5619       Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
5620     return true;
5621   }
5622 
5623   // If the last token before a '}', ']', or ')' is a comma or a trailing
5624   // comment, the intention is to insert a line break after it in order to make
5625   // shuffling around entries easier. Import statements, especially in
5626   // JavaScript, can be an exception to this rule.
5627   if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) {
5628     const FormatToken *BeforeClosingBrace = nullptr;
5629     if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5630          (Style.isJavaScript() && Left.is(tok::l_paren))) &&
5631         Left.isNot(BK_Block) && Left.MatchingParen) {
5632       BeforeClosingBrace = Left.MatchingParen->Previous;
5633     } else if (Right.MatchingParen &&
5634                (Right.MatchingParen->isOneOf(tok::l_brace,
5635                                              TT_ArrayInitializerLSquare) ||
5636                 (Style.isJavaScript() &&
5637                  Right.MatchingParen->is(tok::l_paren)))) {
5638       BeforeClosingBrace = &Left;
5639     }
5640     if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5641                                BeforeClosingBrace->isTrailingComment())) {
5642       return true;
5643     }
5644   }
5645 
5646   if (Right.is(tok::comment)) {
5647     return Left.isNot(BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
5648            (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
5649   }
5650   if (Left.isTrailingComment())
5651     return true;
5652   if (Left.IsUnterminatedLiteral)
5653     return true;
5654 
5655   if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5656       Left.is(tok::string_literal) && Right.is(tok::lessless) && AfterRight &&
5657       AfterRight->is(tok::string_literal)) {
5658     return Right.NewlinesBefore > 0;
5659   }
5660 
5661   if (Right.is(TT_RequiresClause)) {
5662     switch (Style.RequiresClausePosition) {
5663     case FormatStyle::RCPS_OwnLine:
5664     case FormatStyle::RCPS_WithFollowing:
5665       return true;
5666     default:
5667       break;
5668     }
5669   }
5670   // Can break after template<> declaration
5671   if (Left.ClosesTemplateDeclaration && Left.MatchingParen &&
5672       Left.MatchingParen->NestingLevel == 0) {
5673     // Put concepts on the next line e.g.
5674     // template<typename T>
5675     // concept ...
5676     if (Right.is(tok::kw_concept))
5677       return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
5678     return Style.BreakTemplateDeclarations == FormatStyle::BTDS_Yes ||
5679            (Style.BreakTemplateDeclarations == FormatStyle::BTDS_Leave &&
5680             Right.NewlinesBefore > 0);
5681   }
5682   if (Left.ClosesRequiresClause && Right.isNot(tok::semi)) {
5683     switch (Style.RequiresClausePosition) {
5684     case FormatStyle::RCPS_OwnLine:
5685     case FormatStyle::RCPS_WithPreceding:
5686       return true;
5687     default:
5688       break;
5689     }
5690   }
5691   if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
5692     if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
5693         (Left.is(TT_CtorInitializerComma) ||
5694          Right.is(TT_CtorInitializerColon))) {
5695       return true;
5696     }
5697 
5698     if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5699         Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5700       return true;
5701     }
5702   }
5703   if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
5704       Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
5705       Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5706     return true;
5707   }
5708   if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
5709     if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
5710          Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
5711         Right.is(TT_CtorInitializerColon)) {
5712       return true;
5713     }
5714 
5715     if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5716         Left.is(TT_CtorInitializerColon)) {
5717       return true;
5718     }
5719   }
5720   // Break only if we have multiple inheritance.
5721   if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
5722       Right.is(TT_InheritanceComma)) {
5723     return true;
5724   }
5725   if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
5726       Left.is(TT_InheritanceComma)) {
5727     return true;
5728   }
5729   if (Right.is(tok::string_literal) && Right.TokenText.starts_with("R\"")) {
5730     // Multiline raw string literals are special wrt. line breaks. The author
5731     // has made a deliberate choice and might have aligned the contents of the
5732     // string literal accordingly. Thus, we try keep existing line breaks.
5733     return Right.IsMultiline && Right.NewlinesBefore > 0;
5734   }
5735   if ((Left.is(tok::l_brace) ||
5736        (Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
5737       Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
5738     // Don't put enums or option definitions onto single lines in protocol
5739     // buffers.
5740     return true;
5741   }
5742   if (Right.is(TT_InlineASMBrace))
5743     return Right.HasUnescapedNewline;
5744 
5745   if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
5746     auto *FirstNonComment = Line.getFirstNonComment();
5747     bool AccessSpecifier =
5748         FirstNonComment && (FirstNonComment->is(Keywords.kw_internal) ||
5749                             FirstNonComment->isAccessSpecifierKeyword());
5750 
5751     if (Style.BraceWrapping.AfterEnum) {
5752       if (Line.startsWith(tok::kw_enum) ||
5753           Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5754         return true;
5755       }
5756       // Ensure BraceWrapping for `public enum A {`.
5757       if (AccessSpecifier && FirstNonComment->Next &&
5758           FirstNonComment->Next->is(tok::kw_enum)) {
5759         return true;
5760       }
5761     }
5762 
5763     // Ensure BraceWrapping for `public interface A {`.
5764     if (Style.BraceWrapping.AfterClass &&
5765         ((AccessSpecifier && FirstNonComment->Next &&
5766           FirstNonComment->Next->is(Keywords.kw_interface)) ||
5767          Line.startsWith(Keywords.kw_interface))) {
5768       return true;
5769     }
5770 
5771     // Don't attempt to interpret struct return types as structs.
5772     if (Right.isNot(TT_FunctionLBrace)) {
5773       return (Line.startsWith(tok::kw_class) &&
5774               Style.BraceWrapping.AfterClass) ||
5775              (Line.startsWith(tok::kw_struct) &&
5776               Style.BraceWrapping.AfterStruct);
5777     }
5778   }
5779 
5780   if (Left.is(TT_ObjCBlockLBrace) &&
5781       Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
5782     return true;
5783   }
5784 
5785   // Ensure wrapping after __attribute__((XX)) and @interface etc.
5786   if (Left.isOneOf(TT_AttributeRParen, TT_AttributeMacro) &&
5787       Right.is(TT_ObjCDecl)) {
5788     return true;
5789   }
5790 
5791   if (Left.is(TT_LambdaLBrace)) {
5792     if (IsFunctionArgument(Left) &&
5793         Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
5794       return false;
5795     }
5796 
5797     if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
5798         Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
5799         (!Left.Children.empty() &&
5800          Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
5801       return true;
5802     }
5803   }
5804 
5805   if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
5806       (Left.isPointerOrReference() || Left.is(TT_TemplateCloser))) {
5807     return true;
5808   }
5809 
5810   // Put multiple Java annotation on a new line.
5811   if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
5812       Left.is(TT_LeadingJavaAnnotation) &&
5813       Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
5814       (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
5815     return true;
5816   }
5817 
5818   if (Right.is(TT_ProtoExtensionLSquare))
5819     return true;
5820 
5821   // In text proto instances if a submessage contains at least 2 entries and at
5822   // least one of them is a submessage, like A { ... B { ... } ... },
5823   // put all of the entries of A on separate lines by forcing the selector of
5824   // the submessage B to be put on a newline.
5825   //
5826   // Example: these can stay on one line:
5827   // a { scalar_1: 1 scalar_2: 2 }
5828   // a { b { key: value } }
5829   //
5830   // and these entries need to be on a new line even if putting them all in one
5831   // line is under the column limit:
5832   // a {
5833   //   scalar: 1
5834   //   b { key: value }
5835   // }
5836   //
5837   // We enforce this by breaking before a submessage field that has previous
5838   // siblings, *and* breaking before a field that follows a submessage field.
5839   //
5840   // Be careful to exclude the case  [proto.ext] { ... } since the `]` is
5841   // the TT_SelectorName there, but we don't want to break inside the brackets.
5842   //
5843   // Another edge case is @submessage { key: value }, which is a common
5844   // substitution placeholder. In this case we want to keep `@` and `submessage`
5845   // together.
5846   //
5847   // We ensure elsewhere that extensions are always on their own line.
5848   if (Style.isProto() && Right.is(TT_SelectorName) &&
5849       Right.isNot(tok::r_square) && AfterRight) {
5850     // Keep `@submessage` together in:
5851     // @submessage { key: value }
5852     if (Left.is(tok::at))
5853       return false;
5854     // Look for the scope opener after selector in cases like:
5855     // selector { ...
5856     // selector: { ...
5857     // selector: @base { ...
5858     const auto *LBrace = AfterRight;
5859     if (LBrace && LBrace->is(tok::colon)) {
5860       LBrace = LBrace->Next;
5861       if (LBrace && LBrace->is(tok::at)) {
5862         LBrace = LBrace->Next;
5863         if (LBrace)
5864           LBrace = LBrace->Next;
5865       }
5866     }
5867     if (LBrace &&
5868         // The scope opener is one of {, [, <:
5869         // selector { ... }
5870         // selector [ ... ]
5871         // selector < ... >
5872         //
5873         // In case of selector { ... }, the l_brace is TT_DictLiteral.
5874         // In case of an empty selector {}, the l_brace is not TT_DictLiteral,
5875         // so we check for immediately following r_brace.
5876         ((LBrace->is(tok::l_brace) &&
5877           (LBrace->is(TT_DictLiteral) ||
5878            (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
5879          LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
5880       // If Left.ParameterCount is 0, then this submessage entry is not the
5881       // first in its parent submessage, and we want to break before this entry.
5882       // If Left.ParameterCount is greater than 0, then its parent submessage
5883       // might contain 1 or more entries and we want to break before this entry
5884       // if it contains at least 2 entries. We deal with this case later by
5885       // detecting and breaking before the next entry in the parent submessage.
5886       if (Left.ParameterCount == 0)
5887         return true;
5888       // However, if this submessage is the first entry in its parent
5889       // submessage, Left.ParameterCount might be 1 in some cases.
5890       // We deal with this case later by detecting an entry
5891       // following a closing paren of this submessage.
5892     }
5893 
5894     // If this is an entry immediately following a submessage, it will be
5895     // preceded by a closing paren of that submessage, like in:
5896     //     left---.  .---right
5897     //            v  v
5898     // sub: { ... } key: value
5899     // If there was a comment between `}` an `key` above, then `key` would be
5900     // put on a new line anyways.
5901     if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
5902       return true;
5903   }
5904 
5905   return false;
5906 }
5907 
5908 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
5909                                     const FormatToken &Right) const {
5910   const FormatToken &Left = *Right.Previous;
5911   // Language-specific stuff.
5912   if (Style.isCSharp()) {
5913     if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
5914         Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
5915       return false;
5916     }
5917     // Only break after commas for generic type constraints.
5918     if (Line.First->is(TT_CSharpGenericTypeConstraint))
5919       return Left.is(TT_CSharpGenericTypeConstraintComma);
5920     // Keep nullable operators attached to their identifiers.
5921     if (Right.is(TT_CSharpNullable))
5922       return false;
5923   } else if (Style.Language == FormatStyle::LK_Java) {
5924     if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5925                      Keywords.kw_implements)) {
5926       return false;
5927     }
5928     if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5929                       Keywords.kw_implements)) {
5930       return true;
5931     }
5932   } else if (Style.isJavaScript()) {
5933     const FormatToken *NonComment = Right.getPreviousNonComment();
5934     if (NonComment &&
5935         (NonComment->isAccessSpecifierKeyword() ||
5936          NonComment->isOneOf(
5937              tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
5938              tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
5939              tok::kw_static, Keywords.kw_readonly, Keywords.kw_override,
5940              Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set,
5941              Keywords.kw_async, Keywords.kw_await))) {
5942       return false; // Otherwise automatic semicolon insertion would trigger.
5943     }
5944     if (Right.NestingLevel == 0 &&
5945         (Left.Tok.getIdentifierInfo() ||
5946          Left.isOneOf(tok::r_square, tok::r_paren)) &&
5947         Right.isOneOf(tok::l_square, tok::l_paren)) {
5948       return false; // Otherwise automatic semicolon insertion would trigger.
5949     }
5950     if (NonComment && NonComment->is(tok::identifier) &&
5951         NonComment->TokenText == "asserts") {
5952       return false;
5953     }
5954     if (Left.is(TT_FatArrow) && Right.is(tok::l_brace))
5955       return false;
5956     if (Left.is(TT_JsTypeColon))
5957       return true;
5958     // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
5959     if (Left.is(tok::exclaim) && Right.is(tok::colon))
5960       return false;
5961     // Look for is type annotations like:
5962     // function f(): a is B { ... }
5963     // Do not break before is in these cases.
5964     if (Right.is(Keywords.kw_is)) {
5965       const FormatToken *Next = Right.getNextNonComment();
5966       // If `is` is followed by a colon, it's likely that it's a dict key, so
5967       // ignore it for this check.
5968       // For example this is common in Polymer:
5969       // Polymer({
5970       //   is: 'name',
5971       //   ...
5972       // });
5973       if (!Next || Next->isNot(tok::colon))
5974         return false;
5975     }
5976     if (Left.is(Keywords.kw_in))
5977       return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
5978     if (Right.is(Keywords.kw_in))
5979       return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
5980     if (Right.is(Keywords.kw_as))
5981       return false; // must not break before as in 'x as type' casts
5982     if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
5983       // extends and infer can appear as keywords in conditional types:
5984       //   https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
5985       // do not break before them, as the expressions are subject to ASI.
5986       return false;
5987     }
5988     if (Left.is(Keywords.kw_as))
5989       return true;
5990     if (Left.is(TT_NonNullAssertion))
5991       return true;
5992     if (Left.is(Keywords.kw_declare) &&
5993         Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
5994                       Keywords.kw_function, tok::kw_class, tok::kw_enum,
5995                       Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
5996                       Keywords.kw_let, tok::kw_const)) {
5997       // See grammar for 'declare' statements at:
5998       // https://github.com/Microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md#A.10
5999       return false;
6000     }
6001     if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
6002         Right.isOneOf(tok::identifier, tok::string_literal)) {
6003       return false; // must not break in "module foo { ...}"
6004     }
6005     if (Right.is(TT_TemplateString) && Right.closesScope())
6006       return false;
6007     // Don't split tagged template literal so there is a break between the tag
6008     // identifier and template string.
6009     if (Left.is(tok::identifier) && Right.is(TT_TemplateString))
6010       return false;
6011     if (Left.is(TT_TemplateString) && Left.opensScope())
6012       return true;
6013   } else if (Style.isTableGen()) {
6014     // Avoid to break after "def", "class", "let" and so on.
6015     if (Keywords.isTableGenDefinition(Left))
6016       return false;
6017     // Avoid to break after '(' in the cases that is in bang operators.
6018     if (Right.is(tok::l_paren)) {
6019       return !Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
6020                            TT_TemplateCloser);
6021     }
6022     // Avoid to break between the value and its suffix part.
6023     if (Left.is(TT_TableGenValueSuffix))
6024       return false;
6025     // Avoid to break around paste operator.
6026     if (Left.is(tok::hash) || Right.is(tok::hash))
6027       return false;
6028     if (Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator))
6029       return false;
6030   }
6031 
6032   if (Left.is(tok::at))
6033     return false;
6034   if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
6035     return false;
6036   if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
6037     return Right.isNot(tok::l_paren);
6038   if (Right.is(TT_PointerOrReference)) {
6039     return Line.IsMultiVariableDeclStmt ||
6040            (getTokenPointerOrReferenceAlignment(Right) ==
6041                 FormatStyle::PAS_Right &&
6042             (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
6043   }
6044   if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
6045       Right.is(tok::kw_operator)) {
6046     return true;
6047   }
6048   if (Left.is(TT_PointerOrReference))
6049     return false;
6050   if (Right.isTrailingComment()) {
6051     // We rely on MustBreakBefore being set correctly here as we should not
6052     // change the "binding" behavior of a comment.
6053     // The first comment in a braced lists is always interpreted as belonging to
6054     // the first list element. Otherwise, it should be placed outside of the
6055     // list.
6056     return Left.is(BK_BracedInit) ||
6057            (Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 &&
6058             Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
6059   }
6060   if (Left.is(tok::question) && Right.is(tok::colon))
6061     return false;
6062   if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
6063     return Style.BreakBeforeTernaryOperators;
6064   if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
6065     return !Style.BreakBeforeTernaryOperators;
6066   if (Left.is(TT_InheritanceColon))
6067     return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
6068   if (Right.is(TT_InheritanceColon))
6069     return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
6070   if (Right.is(TT_ObjCMethodExpr) && Right.isNot(tok::r_square) &&
6071       Left.isNot(TT_SelectorName)) {
6072     return true;
6073   }
6074 
6075   if (Right.is(tok::colon) &&
6076       !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) {
6077     return false;
6078   }
6079   if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
6080     if (Style.isProto()) {
6081       if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
6082         return false;
6083       // Prevent cases like:
6084       //
6085       // submessage:
6086       //     { key: valueeeeeeeeeeee }
6087       //
6088       // when the snippet does not fit into one line.
6089       // Prefer:
6090       //
6091       // submessage: {
6092       //   key: valueeeeeeeeeeee
6093       // }
6094       //
6095       // instead, even if it is longer by one line.
6096       //
6097       // Note that this allows the "{" to go over the column limit
6098       // when the column limit is just between ":" and "{", but that does
6099       // not happen too often and alternative formattings in this case are
6100       // not much better.
6101       //
6102       // The code covers the cases:
6103       //
6104       // submessage: { ... }
6105       // submessage: < ... >
6106       // repeated: [ ... ]
6107       if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
6108            Right.is(TT_DictLiteral)) ||
6109           Right.is(TT_ArrayInitializerLSquare)) {
6110         return false;
6111       }
6112     }
6113     return true;
6114   }
6115   if (Right.is(tok::r_square) && Right.MatchingParen &&
6116       Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
6117     return false;
6118   }
6119   if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
6120                                     Right.Next->is(TT_ObjCMethodExpr))) {
6121     return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
6122   }
6123   if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
6124     return true;
6125   if (Right.is(tok::kw_concept))
6126     return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
6127   if (Right.is(TT_RequiresClause))
6128     return true;
6129   if (Left.ClosesTemplateDeclaration) {
6130     return Style.BreakTemplateDeclarations != FormatStyle::BTDS_Leave ||
6131            Right.NewlinesBefore > 0;
6132   }
6133   if (Left.is(TT_FunctionAnnotationRParen))
6134     return true;
6135   if (Left.ClosesRequiresClause)
6136     return true;
6137   if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
6138                     TT_OverloadedOperator)) {
6139     return false;
6140   }
6141   if (Left.is(TT_RangeBasedForLoopColon))
6142     return true;
6143   if (Right.is(TT_RangeBasedForLoopColon))
6144     return false;
6145   if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
6146     return true;
6147   if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
6148       (Left.is(tok::less) && Right.is(tok::less))) {
6149     return false;
6150   }
6151   if (Right.is(TT_BinaryOperator) &&
6152       Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
6153       (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
6154        Right.getPrecedence() != prec::Assignment)) {
6155     return true;
6156   }
6157   if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
6158       Left.is(tok::kw_operator)) {
6159     return false;
6160   }
6161   if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
6162       Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) {
6163     return false;
6164   }
6165   if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
6166       !Style.Cpp11BracedListStyle) {
6167     return false;
6168   }
6169   if (Left.is(TT_AttributeLParen) ||
6170       (Left.is(tok::l_paren) && Left.is(TT_TypeDeclarationParen))) {
6171     return false;
6172   }
6173   if (Left.is(tok::l_paren) && Left.Previous &&
6174       (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
6175     return false;
6176   }
6177   if (Right.is(TT_ImplicitStringLiteral))
6178     return false;
6179 
6180   if (Right.is(TT_TemplateCloser))
6181     return false;
6182   if (Right.is(tok::r_square) && Right.MatchingParen &&
6183       Right.MatchingParen->is(TT_LambdaLSquare)) {
6184     return false;
6185   }
6186 
6187   // We only break before r_brace if there was a corresponding break before
6188   // the l_brace, which is tracked by BreakBeforeClosingBrace.
6189   if (Right.is(tok::r_brace)) {
6190     return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) ||
6191                                    (Right.isBlockIndentedInitRBrace(Style)));
6192   }
6193 
6194   // We only break before r_paren if we're in a block indented context.
6195   if (Right.is(tok::r_paren)) {
6196     if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
6197         !Right.MatchingParen) {
6198       return false;
6199     }
6200     auto Next = Right.Next;
6201     if (Next && Next->is(tok::r_paren))
6202       Next = Next->Next;
6203     if (Next && Next->is(tok::l_paren))
6204       return false;
6205     const FormatToken *Previous = Right.MatchingParen->Previous;
6206     return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
6207   }
6208 
6209   // Allow breaking after a trailing annotation, e.g. after a method
6210   // declaration.
6211   if (Left.is(TT_TrailingAnnotation)) {
6212     return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
6213                           tok::less, tok::coloncolon);
6214   }
6215 
6216   if (Right.isAttribute())
6217     return true;
6218 
6219   if (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))
6220     return Left.isNot(TT_AttributeSquare);
6221 
6222   if (Left.is(tok::identifier) && Right.is(tok::string_literal))
6223     return true;
6224 
6225   if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
6226     return true;
6227 
6228   if (Left.is(TT_CtorInitializerColon)) {
6229     return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
6230            (!Right.isTrailingComment() || Right.NewlinesBefore > 0);
6231   }
6232   if (Right.is(TT_CtorInitializerColon))
6233     return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
6234   if (Left.is(TT_CtorInitializerComma) &&
6235       Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6236     return false;
6237   }
6238   if (Right.is(TT_CtorInitializerComma) &&
6239       Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
6240     return true;
6241   }
6242   if (Left.is(TT_InheritanceComma) &&
6243       Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6244     return false;
6245   }
6246   if (Right.is(TT_InheritanceComma) &&
6247       Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
6248     return true;
6249   }
6250   if (Left.is(TT_ArrayInitializerLSquare))
6251     return true;
6252   if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
6253     return true;
6254   if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
6255       !Left.isOneOf(tok::arrowstar, tok::lessless) &&
6256       Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
6257       (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
6258        Left.getPrecedence() == prec::Assignment)) {
6259     return true;
6260   }
6261   if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) ||
6262       (Left.is(tok::r_square) && Right.is(TT_AttributeSquare))) {
6263     return false;
6264   }
6265 
6266   auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
6267   if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
6268     if (isAllmanLambdaBrace(Left))
6269       return !isItAnEmptyLambdaAllowed(Left, ShortLambdaOption);
6270     if (isAllmanLambdaBrace(Right))
6271       return !isItAnEmptyLambdaAllowed(Right, ShortLambdaOption);
6272   }
6273 
6274   if (Right.is(tok::kw_noexcept) && Right.is(TT_TrailingAnnotation)) {
6275     switch (Style.AllowBreakBeforeNoexceptSpecifier) {
6276     case FormatStyle::BBNSS_Never:
6277       return false;
6278     case FormatStyle::BBNSS_Always:
6279       return true;
6280     case FormatStyle::BBNSS_OnlyWithParen:
6281       return Right.Next && Right.Next->is(tok::l_paren);
6282     }
6283   }
6284 
6285   return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
6286                       tok::kw_class, tok::kw_struct, tok::comment) ||
6287          Right.isMemberAccess() ||
6288          Right.isOneOf(TT_TrailingReturnArrow, tok::lessless, tok::colon,
6289                        tok::l_square, tok::at) ||
6290          (Left.is(tok::r_paren) &&
6291           Right.isOneOf(tok::identifier, tok::kw_const)) ||
6292          (Left.is(tok::l_paren) && Right.isNot(tok::r_paren)) ||
6293          (Left.is(TT_TemplateOpener) && Right.isNot(TT_TemplateCloser));
6294 }
6295 
6296 void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) const {
6297   llvm::errs() << "AnnotatedTokens(L=" << Line.Level << ", P=" << Line.PPLevel
6298                << ", T=" << Line.Type << ", C=" << Line.IsContinuation
6299                << "):\n";
6300   const FormatToken *Tok = Line.First;
6301   while (Tok) {
6302     llvm::errs() << " M=" << Tok->MustBreakBefore
6303                  << " C=" << Tok->CanBreakBefore
6304                  << " T=" << getTokenTypeName(Tok->getType())
6305                  << " S=" << Tok->SpacesRequiredBefore
6306                  << " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount
6307                  << " BK=" << Tok->getBlockKind() << " P=" << Tok->SplitPenalty
6308                  << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength
6309                  << " PPK=" << Tok->getPackingKind() << " FakeLParens=";
6310     for (prec::Level LParen : Tok->FakeLParens)
6311       llvm::errs() << LParen << "/";
6312     llvm::errs() << " FakeRParens=" << Tok->FakeRParens;
6313     llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo();
6314     llvm::errs() << " Text='" << Tok->TokenText << "'\n";
6315     if (!Tok->Next)
6316       assert(Tok == Line.Last);
6317     Tok = Tok->Next;
6318   }
6319   llvm::errs() << "----\n";
6320 }
6321 
6322 FormatStyle::PointerAlignmentStyle
6323 TokenAnnotator::getTokenReferenceAlignment(const FormatToken &Reference) const {
6324   assert(Reference.isOneOf(tok::amp, tok::ampamp));
6325   switch (Style.ReferenceAlignment) {
6326   case FormatStyle::RAS_Pointer:
6327     return Style.PointerAlignment;
6328   case FormatStyle::RAS_Left:
6329     return FormatStyle::PAS_Left;
6330   case FormatStyle::RAS_Right:
6331     return FormatStyle::PAS_Right;
6332   case FormatStyle::RAS_Middle:
6333     return FormatStyle::PAS_Middle;
6334   }
6335   assert(0); //"Unhandled value of ReferenceAlignment"
6336   return Style.PointerAlignment;
6337 }
6338 
6339 FormatStyle::PointerAlignmentStyle
6340 TokenAnnotator::getTokenPointerOrReferenceAlignment(
6341     const FormatToken &PointerOrReference) const {
6342   if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
6343     switch (Style.ReferenceAlignment) {
6344     case FormatStyle::RAS_Pointer:
6345       return Style.PointerAlignment;
6346     case FormatStyle::RAS_Left:
6347       return FormatStyle::PAS_Left;
6348     case FormatStyle::RAS_Right:
6349       return FormatStyle::PAS_Right;
6350     case FormatStyle::RAS_Middle:
6351       return FormatStyle::PAS_Middle;
6352     }
6353   }
6354   assert(PointerOrReference.is(tok::star));
6355   return Style.PointerAlignment;
6356 }
6357 
6358 } // namespace format
6359 } // namespace clang
6360