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