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