1 //===--- TokenAnnotator.cpp - Format C++ code -----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file implements a token annotator, i.e. creates
12 /// \c AnnotatedTokens out of \c FormatTokens with required extra information.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "TokenAnnotator.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/Support/Debug.h"
20 
21 #define DEBUG_TYPE "format-token-annotator"
22 
23 namespace clang {
24 namespace format {
25 
26 namespace {
27 
28 /// Returns \c true if the token can be used as an identifier in
29 /// an Objective-C \c @selector, \c false otherwise.
30 ///
31 /// Because getFormattingLangOpts() always lexes source code as
32 /// Objective-C++, C++ keywords like \c new and \c delete are
33 /// lexed as tok::kw_*, not tok::identifier, even for Objective-C.
34 ///
35 /// For Objective-C and Objective-C++, both identifiers and keywords
36 /// are valid inside @selector(...) (or a macro which
37 /// invokes @selector(...)). So, we allow treat any identifier or
38 /// keyword as a potential Objective-C selector component.
39 static bool canBeObjCSelectorComponent(const FormatToken &Tok) {
40   return Tok.Tok.getIdentifierInfo() != nullptr;
41 }
42 
43 /// A parser that gathers additional information about tokens.
44 ///
45 /// The \c TokenAnnotator tries to match parenthesis and square brakets and
46 /// store a parenthesis levels. It also tries to resolve matching "<" and ">"
47 /// into template parameter lists.
48 class AnnotatingParser {
49 public:
50   AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
51                    const AdditionalKeywords &Keywords)
52       : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
53         Keywords(Keywords) {
54     Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
55     resetTokenMetadata(CurrentToken);
56   }
57 
58 private:
59   bool parseAngle() {
60     if (!CurrentToken || !CurrentToken->Previous)
61       return false;
62     if (NonTemplateLess.count(CurrentToken->Previous))
63       return false;
64 
65     const FormatToken &Previous = *CurrentToken->Previous;  // The '<'.
66     if (Previous.Previous) {
67       if (Previous.Previous->Tok.isLiteral())
68         return false;
69       if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
70           (!Previous.Previous->MatchingParen ||
71            !Previous.Previous->MatchingParen->is(TT_OverloadedOperatorLParen)))
72         return false;
73     }
74 
75     FormatToken *Left = CurrentToken->Previous;
76     Left->ParentBracket = Contexts.back().ContextKind;
77     ScopedContextCreator ContextCreator(*this, tok::less, 12);
78 
79     // If this angle is in the context of an expression, we need to be more
80     // hesitant to detect it as opening template parameters.
81     bool InExprContext = Contexts.back().IsExpression;
82 
83     Contexts.back().IsExpression = false;
84     // If there's a template keyword before the opening angle bracket, this is a
85     // template parameter, not an argument.
86     Contexts.back().InTemplateArgument =
87         Left->Previous && Left->Previous->Tok.isNot(tok::kw_template);
88 
89     if (Style.Language == FormatStyle::LK_Java &&
90         CurrentToken->is(tok::question))
91       next();
92 
93     while (CurrentToken) {
94       if (CurrentToken->is(tok::greater)) {
95         Left->MatchingParen = CurrentToken;
96         CurrentToken->MatchingParen = Left;
97         // In TT_Proto, we must distignuish between:
98         //   map<key, value>
99         //   msg < item: data >
100         //   msg: < item: data >
101         // In TT_TextProto, map<key, value> does not occur.
102         if (Style.Language == FormatStyle::LK_TextProto ||
103             (Style.Language == FormatStyle::LK_Proto && Left->Previous &&
104              Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral)))
105           CurrentToken->Type = TT_DictLiteral;
106         else
107           CurrentToken->Type = TT_TemplateCloser;
108         next();
109         return true;
110       }
111       if (CurrentToken->is(tok::question) &&
112           Style.Language == FormatStyle::LK_Java) {
113         next();
114         continue;
115       }
116       if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
117           (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
118            Style.Language != FormatStyle::LK_Proto &&
119            Style.Language != FormatStyle::LK_TextProto))
120         return false;
121       // If a && or || is found and interpreted as a binary operator, this set
122       // of angles is likely part of something like "a < b && c > d". If the
123       // angles are inside an expression, the ||/&& might also be a binary
124       // operator that was misinterpreted because we are parsing template
125       // parameters.
126       // FIXME: This is getting out of hand, write a decent parser.
127       if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
128           CurrentToken->Previous->is(TT_BinaryOperator) &&
129           Contexts[Contexts.size() - 2].IsExpression &&
130           !Line.startsWith(tok::kw_template))
131         return false;
132       updateParameterCount(Left, CurrentToken);
133       if (Style.Language == FormatStyle::LK_Proto) {
134         if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) {
135           if (CurrentToken->is(tok::colon) ||
136               (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
137                Previous->isNot(tok::colon)))
138             Previous->Type = TT_SelectorName;
139         }
140       }
141       if (!consumeToken())
142         return false;
143     }
144     return false;
145   }
146 
147   bool parseParens(bool LookForDecls = false) {
148     if (!CurrentToken)
149       return false;
150     FormatToken *Left = CurrentToken->Previous;
151     Left->ParentBracket = Contexts.back().ContextKind;
152     ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
153 
154     // FIXME: This is a bit of a hack. Do better.
155     Contexts.back().ColonIsForRangeExpr =
156         Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
157 
158     bool StartsObjCMethodExpr = false;
159     if (FormatToken *MaybeSel = Left->Previous) {
160       // @selector( starts a selector.
161       if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
162           MaybeSel->Previous->is(tok::at)) {
163         StartsObjCMethodExpr = true;
164       }
165     }
166 
167     if (Left->is(TT_OverloadedOperatorLParen)) {
168       Contexts.back().IsExpression = false;
169     } else if (Style.Language == FormatStyle::LK_JavaScript &&
170                (Line.startsWith(Keywords.kw_type, tok::identifier) ||
171                 Line.startsWith(tok::kw_export, Keywords.kw_type,
172                                 tok::identifier))) {
173       // type X = (...);
174       // export type X = (...);
175       Contexts.back().IsExpression = false;
176     } else if (Left->Previous &&
177                (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype,
178                                         tok::kw_if, tok::kw_while, tok::l_paren,
179                                         tok::comma) ||
180                 Left->Previous->endsSequence(tok::kw_constexpr, tok::kw_if) ||
181                 Left->Previous->is(TT_BinaryOperator))) {
182       // static_assert, if and while usually contain expressions.
183       Contexts.back().IsExpression = true;
184     } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous &&
185                (Left->Previous->is(Keywords.kw_function) ||
186                 (Left->Previous->endsSequence(tok::identifier,
187                                               Keywords.kw_function)))) {
188       // function(...) or function f(...)
189       Contexts.back().IsExpression = false;
190     } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous &&
191                Left->Previous->is(TT_JsTypeColon)) {
192       // let x: (SomeType);
193       Contexts.back().IsExpression = false;
194     } else if (Left->Previous && Left->Previous->is(tok::r_square) &&
195                Left->Previous->MatchingParen &&
196                Left->Previous->MatchingParen->is(TT_LambdaLSquare)) {
197       // This is a parameter list of a lambda expression.
198       Contexts.back().IsExpression = false;
199     } else if (Line.InPPDirective &&
200                (!Left->Previous || !Left->Previous->is(tok::identifier))) {
201       Contexts.back().IsExpression = true;
202     } else if (Contexts[Contexts.size() - 2].CaretFound) {
203       // This is the parameter list of an ObjC block.
204       Contexts.back().IsExpression = false;
205     } else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
206       Left->Type = TT_AttributeParen;
207     } else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) {
208       // The first argument to a foreach macro is a declaration.
209       Contexts.back().IsForEachMacro = true;
210       Contexts.back().IsExpression = false;
211     } else if (Left->Previous && Left->Previous->MatchingParen &&
212                Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
213       Contexts.back().IsExpression = false;
214     } else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
215       bool IsForOrCatch =
216           Left->Previous && Left->Previous->isOneOf(tok::kw_for, tok::kw_catch);
217       Contexts.back().IsExpression = !IsForOrCatch;
218     }
219 
220     if (StartsObjCMethodExpr) {
221       Contexts.back().ColonIsObjCMethodExpr = true;
222       Left->Type = TT_ObjCMethodExpr;
223     }
224 
225     // MightBeFunctionType and ProbablyFunctionType are used for
226     // function pointer and reference types as well as Objective-C
227     // block types:
228     //
229     // void (*FunctionPointer)(void);
230     // void (&FunctionReference)(void);
231     // void (^ObjCBlock)(void);
232     bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
233     bool ProbablyFunctionType =
234         CurrentToken->isOneOf(tok::star, tok::amp, tok::caret);
235     bool HasMultipleLines = false;
236     bool HasMultipleParametersOnALine = false;
237     bool MightBeObjCForRangeLoop =
238         Left->Previous && Left->Previous->is(tok::kw_for);
239     FormatToken *PossibleObjCForInToken = nullptr;
240     while (CurrentToken) {
241       // LookForDecls is set when "if (" has been seen. Check for
242       // 'identifier' '*' 'identifier' followed by not '=' -- this
243       // '*' has to be a binary operator but determineStarAmpUsage() will
244       // categorize it as an unary operator, so set the right type here.
245       if (LookForDecls && CurrentToken->Next) {
246         FormatToken *Prev = CurrentToken->getPreviousNonComment();
247         if (Prev) {
248           FormatToken *PrevPrev = Prev->getPreviousNonComment();
249           FormatToken *Next = CurrentToken->Next;
250           if (PrevPrev && PrevPrev->is(tok::identifier) &&
251               Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
252               CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
253             Prev->Type = TT_BinaryOperator;
254             LookForDecls = false;
255           }
256         }
257       }
258 
259       if (CurrentToken->Previous->is(TT_PointerOrReference) &&
260           CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
261                                                     tok::coloncolon))
262         ProbablyFunctionType = true;
263       if (CurrentToken->is(tok::comma))
264         MightBeFunctionType = false;
265       if (CurrentToken->Previous->is(TT_BinaryOperator))
266         Contexts.back().IsExpression = true;
267       if (CurrentToken->is(tok::r_paren)) {
268         if (MightBeFunctionType && ProbablyFunctionType && CurrentToken->Next &&
269             (CurrentToken->Next->is(tok::l_paren) ||
270              (CurrentToken->Next->is(tok::l_square) && Line.MustBeDeclaration)))
271           Left->Type = Left->Next->is(tok::caret) ? TT_ObjCBlockLParen
272                                                   : TT_FunctionTypeLParen;
273         Left->MatchingParen = CurrentToken;
274         CurrentToken->MatchingParen = Left;
275 
276         if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
277             Left->Previous && Left->Previous->is(tok::l_paren)) {
278           // Detect the case where macros are used to generate lambdas or
279           // function bodies, e.g.:
280           //   auto my_lambda = MARCO((Type *type, int i) { .. body .. });
281           for (FormatToken *Tok = Left; Tok != CurrentToken; Tok = Tok->Next) {
282             if (Tok->is(TT_BinaryOperator) &&
283                 Tok->isOneOf(tok::star, tok::amp, tok::ampamp))
284               Tok->Type = TT_PointerOrReference;
285           }
286         }
287 
288         if (StartsObjCMethodExpr) {
289           CurrentToken->Type = TT_ObjCMethodExpr;
290           if (Contexts.back().FirstObjCSelectorName) {
291             Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
292                 Contexts.back().LongestObjCSelectorName;
293           }
294         }
295 
296         if (Left->is(TT_AttributeParen))
297           CurrentToken->Type = TT_AttributeParen;
298         if (Left->Previous && Left->Previous->is(TT_JavaAnnotation))
299           CurrentToken->Type = TT_JavaAnnotation;
300         if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation))
301           CurrentToken->Type = TT_LeadingJavaAnnotation;
302 
303         if (!HasMultipleLines)
304           Left->PackingKind = PPK_Inconclusive;
305         else if (HasMultipleParametersOnALine)
306           Left->PackingKind = PPK_BinPacked;
307         else
308           Left->PackingKind = PPK_OnePerLine;
309 
310         next();
311         return true;
312       }
313       if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
314         return false;
315 
316       if (CurrentToken->is(tok::l_brace))
317         Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen
318       if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
319           !CurrentToken->Next->HasUnescapedNewline &&
320           !CurrentToken->Next->isTrailingComment())
321         HasMultipleParametersOnALine = true;
322       if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
323            CurrentToken->Previous->isSimpleTypeSpecifier()) &&
324           !CurrentToken->is(tok::l_brace))
325         Contexts.back().IsExpression = false;
326       if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
327         MightBeObjCForRangeLoop = false;
328         if (PossibleObjCForInToken) {
329           PossibleObjCForInToken->Type = TT_Unknown;
330           PossibleObjCForInToken = nullptr;
331         }
332       }
333       if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
334         PossibleObjCForInToken = CurrentToken;
335         PossibleObjCForInToken->Type = TT_ObjCForIn;
336       }
337       // When we discover a 'new', we set CanBeExpression to 'false' in order to
338       // parse the type correctly. Reset that after a comma.
339       if (CurrentToken->is(tok::comma))
340         Contexts.back().CanBeExpression = true;
341 
342       FormatToken *Tok = CurrentToken;
343       if (!consumeToken())
344         return false;
345       updateParameterCount(Left, Tok);
346       if (CurrentToken && CurrentToken->HasUnescapedNewline)
347         HasMultipleLines = true;
348     }
349     return false;
350   }
351 
352   bool isCpp11AttributeSpecifier(const FormatToken &Tok) {
353     if (!Style.isCpp() || !Tok.startsSequence(tok::l_square, tok::l_square))
354       return false;
355     const FormatToken *AttrTok = Tok.Next->Next;
356     if (!AttrTok)
357       return false;
358     // C++17 '[[using ns: foo, bar(baz, blech)]]'
359     // We assume nobody will name an ObjC variable 'using'.
360     if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
361       return true;
362     if (AttrTok->isNot(tok::identifier))
363       return false;
364     while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
365       // ObjC message send. We assume nobody will use : in a C++11 attribute
366       // specifier parameter, although this is technically valid:
367       // [[foo(:)]]
368       if (AttrTok->is(tok::colon) ||
369           AttrTok->startsSequence(tok::identifier, tok::identifier) ||
370           AttrTok->startsSequence(tok::r_paren, tok::identifier))
371         return false;
372       if (AttrTok->is(tok::ellipsis))
373         return true;
374       AttrTok = AttrTok->Next;
375     }
376     return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
377   }
378 
379   bool parseSquare() {
380     if (!CurrentToken)
381       return false;
382 
383     // A '[' could be an index subscript (after an identifier or after
384     // ')' or ']'), it could be the start of an Objective-C method
385     // expression, it could the start of an Objective-C array literal,
386     // or it could be a C++ attribute specifier [[foo::bar]].
387     FormatToken *Left = CurrentToken->Previous;
388     Left->ParentBracket = Contexts.back().ContextKind;
389     FormatToken *Parent = Left->getPreviousNonComment();
390 
391     // Cases where '>' is followed by '['.
392     // In C++, this can happen either in array of templates (foo<int>[10])
393     // or when array is a nested template type (unique_ptr<type1<type2>[]>).
394     bool CppArrayTemplates =
395         Style.isCpp() && Parent && Parent->is(TT_TemplateCloser) &&
396         (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
397          Contexts.back().InTemplateArgument);
398 
399     bool IsCpp11AttributeSpecifier = isCpp11AttributeSpecifier(*Left) ||
400                                      Contexts.back().InCpp11AttributeSpecifier;
401 
402     bool InsideInlineASM = Line.startsWith(tok::kw_asm);
403     bool StartsObjCMethodExpr =
404         !InsideInlineASM && !CppArrayTemplates && Style.isCpp() &&
405         !IsCpp11AttributeSpecifier && Contexts.back().CanBeExpression &&
406         Left->isNot(TT_LambdaLSquare) &&
407         !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
408         (!Parent ||
409          Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
410                          tok::kw_return, tok::kw_throw) ||
411          Parent->isUnaryOperator() ||
412          // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
413          Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
414          getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
415     bool ColonFound = false;
416 
417     unsigned BindingIncrease = 1;
418     if (Left->isCppStructuredBinding(Style)) {
419       Left->Type = TT_StructuredBindingLSquare;
420     } else if (Left->is(TT_Unknown)) {
421       if (StartsObjCMethodExpr) {
422         Left->Type = TT_ObjCMethodExpr;
423       } else if (IsCpp11AttributeSpecifier) {
424         Left->Type = TT_AttributeSquare;
425       } else if (Style.Language == FormatStyle::LK_JavaScript && Parent &&
426                  Contexts.back().ContextKind == tok::l_brace &&
427                  Parent->isOneOf(tok::l_brace, tok::comma)) {
428         Left->Type = TT_JsComputedPropertyName;
429       } else if (Style.isCpp() && Contexts.back().ContextKind == tok::l_brace &&
430                  Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
431         Left->Type = TT_DesignatedInitializerLSquare;
432       } else if (CurrentToken->is(tok::r_square) && Parent &&
433                  Parent->is(TT_TemplateCloser)) {
434         Left->Type = TT_ArraySubscriptLSquare;
435       } else if (Style.Language == FormatStyle::LK_Proto ||
436                  Style.Language == FormatStyle::LK_TextProto) {
437         // Square braces in LK_Proto can either be message field attributes:
438         //
439         // optional Aaa aaa = 1 [
440         //   (aaa) = aaa
441         // ];
442         //
443         // extensions 123 [
444         //   (aaa) = aaa
445         // ];
446         //
447         // or text proto extensions (in options):
448         //
449         // option (Aaa.options) = {
450         //   [type.type/type] {
451         //     key: value
452         //   }
453         // }
454         //
455         // or repeated fields (in options):
456         //
457         // option (Aaa.options) = {
458         //   keys: [ 1, 2, 3 ]
459         // }
460         //
461         // In the first and the third case we want to spread the contents inside
462         // the square braces; in the second we want to keep them inline.
463         Left->Type = TT_ArrayInitializerLSquare;
464         if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
465                                 tok::equal) &&
466             !Left->endsSequence(tok::l_square, tok::numeric_constant,
467                                 tok::identifier) &&
468             !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
469           Left->Type = TT_ProtoExtensionLSquare;
470           BindingIncrease = 10;
471         }
472       } else if (!CppArrayTemplates && Parent &&
473                  Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
474                                  tok::comma, tok::l_paren, tok::l_square,
475                                  tok::question, tok::colon, tok::kw_return,
476                                  // Should only be relevant to JavaScript:
477                                  tok::kw_default)) {
478         Left->Type = TT_ArrayInitializerLSquare;
479       } else {
480         BindingIncrease = 10;
481         Left->Type = TT_ArraySubscriptLSquare;
482       }
483     }
484 
485     ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease);
486     Contexts.back().IsExpression = true;
487     if (Style.Language == FormatStyle::LK_JavaScript && Parent &&
488         Parent->is(TT_JsTypeColon))
489       Contexts.back().IsExpression = false;
490 
491     Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
492     Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
493 
494     while (CurrentToken) {
495       if (CurrentToken->is(tok::r_square)) {
496         if (IsCpp11AttributeSpecifier)
497           CurrentToken->Type = TT_AttributeSquare;
498         else if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
499                  Left->is(TT_ObjCMethodExpr)) {
500           // An ObjC method call is rarely followed by an open parenthesis.
501           // FIXME: Do we incorrectly label ":" with this?
502           StartsObjCMethodExpr = false;
503           Left->Type = TT_Unknown;
504         }
505         if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
506           CurrentToken->Type = TT_ObjCMethodExpr;
507           // If we haven't seen a colon yet, make sure the last identifier
508           // before the r_square is tagged as a selector name component.
509           if (!ColonFound && CurrentToken->Previous &&
510               CurrentToken->Previous->is(TT_Unknown) &&
511               canBeObjCSelectorComponent(*CurrentToken->Previous))
512             CurrentToken->Previous->Type = TT_SelectorName;
513           // determineStarAmpUsage() thinks that '*' '[' is allocating an
514           // array of pointers, but if '[' starts a selector then '*' is a
515           // binary operator.
516           if (Parent && Parent->is(TT_PointerOrReference))
517             Parent->Type = TT_BinaryOperator;
518         }
519         Left->MatchingParen = CurrentToken;
520         CurrentToken->MatchingParen = Left;
521         // FirstObjCSelectorName is set when a colon is found. This does
522         // not work, however, when the method has no parameters.
523         // Here, we set FirstObjCSelectorName when the end of the method call is
524         // reached, in case it was not set already.
525         if (!Contexts.back().FirstObjCSelectorName) {
526             FormatToken* Previous = CurrentToken->getPreviousNonComment();
527             if (Previous && Previous->is(TT_SelectorName)) {
528               Previous->ObjCSelectorNameParts = 1;
529               Contexts.back().FirstObjCSelectorName = Previous;
530             }
531         } else {
532           Left->ParameterCount =
533               Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
534         }
535         if (Contexts.back().FirstObjCSelectorName) {
536           Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
537               Contexts.back().LongestObjCSelectorName;
538           if (Left->BlockParameterCount > 1)
539             Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
540         }
541         next();
542         return true;
543       }
544       if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
545         return false;
546       if (CurrentToken->is(tok::colon)) {
547         if (IsCpp11AttributeSpecifier &&
548             CurrentToken->endsSequence(tok::colon, tok::identifier,
549                                        tok::kw_using)) {
550           // Remember that this is a [[using ns: foo]] C++ attribute, so we
551           // don't add a space before the colon (unlike other colons).
552           CurrentToken->Type = TT_AttributeColon;
553         } else if (Left->isOneOf(TT_ArraySubscriptLSquare,
554                                  TT_DesignatedInitializerLSquare)) {
555           Left->Type = TT_ObjCMethodExpr;
556           StartsObjCMethodExpr = true;
557           Contexts.back().ColonIsObjCMethodExpr = true;
558           if (Parent && Parent->is(tok::r_paren))
559             // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
560             Parent->Type = TT_CastRParen;
561         }
562         ColonFound = true;
563       }
564       if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
565           !ColonFound)
566         Left->Type = TT_ArrayInitializerLSquare;
567       FormatToken *Tok = CurrentToken;
568       if (!consumeToken())
569         return false;
570       updateParameterCount(Left, Tok);
571     }
572     return false;
573   }
574 
575   bool parseBrace() {
576     if (CurrentToken) {
577       FormatToken *Left = CurrentToken->Previous;
578       Left->ParentBracket = Contexts.back().ContextKind;
579 
580       if (Contexts.back().CaretFound)
581         Left->Type = TT_ObjCBlockLBrace;
582       Contexts.back().CaretFound = false;
583 
584       ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
585       Contexts.back().ColonIsDictLiteral = true;
586       if (Left->BlockKind == BK_BracedInit)
587         Contexts.back().IsExpression = true;
588       if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous &&
589           Left->Previous->is(TT_JsTypeColon))
590         Contexts.back().IsExpression = false;
591 
592       while (CurrentToken) {
593         if (CurrentToken->is(tok::r_brace)) {
594           Left->MatchingParen = CurrentToken;
595           CurrentToken->MatchingParen = Left;
596           next();
597           return true;
598         }
599         if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
600           return false;
601         updateParameterCount(Left, CurrentToken);
602         if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
603           FormatToken *Previous = CurrentToken->getPreviousNonComment();
604           if (Previous->is(TT_JsTypeOptionalQuestion))
605             Previous = Previous->getPreviousNonComment();
606           if ((CurrentToken->is(tok::colon) &&
607                (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
608               Style.Language == FormatStyle::LK_Proto ||
609               Style.Language == FormatStyle::LK_TextProto) {
610             Left->Type = TT_DictLiteral;
611             if (Previous->Tok.getIdentifierInfo() ||
612                 Previous->is(tok::string_literal))
613               Previous->Type = TT_SelectorName;
614           }
615           if (CurrentToken->is(tok::colon) ||
616               Style.Language == FormatStyle::LK_JavaScript)
617             Left->Type = TT_DictLiteral;
618         }
619         if (CurrentToken->is(tok::comma) &&
620             Style.Language == FormatStyle::LK_JavaScript)
621           Left->Type = TT_DictLiteral;
622         if (!consumeToken())
623           return false;
624       }
625     }
626     return true;
627   }
628 
629   void updateParameterCount(FormatToken *Left, FormatToken *Current) {
630     // For ObjC methods, the number of parameters is calculated differently as
631     // method declarations have a different structure (the parameters are not
632     // inside a bracket scope).
633     if (Current->is(tok::l_brace) && Current->BlockKind == BK_Block)
634       ++Left->BlockParameterCount;
635     if (Current->is(tok::comma)) {
636       ++Left->ParameterCount;
637       if (!Left->Role)
638         Left->Role.reset(new CommaSeparatedList(Style));
639       Left->Role->CommaFound(Current);
640     } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
641       Left->ParameterCount = 1;
642     }
643   }
644 
645   bool parseConditional() {
646     while (CurrentToken) {
647       if (CurrentToken->is(tok::colon)) {
648         CurrentToken->Type = TT_ConditionalExpr;
649         next();
650         return true;
651       }
652       if (!consumeToken())
653         return false;
654     }
655     return false;
656   }
657 
658   bool parseTemplateDeclaration() {
659     if (CurrentToken && CurrentToken->is(tok::less)) {
660       CurrentToken->Type = TT_TemplateOpener;
661       next();
662       if (!parseAngle())
663         return false;
664       if (CurrentToken)
665         CurrentToken->Previous->ClosesTemplateDeclaration = true;
666       return true;
667     }
668     return false;
669   }
670 
671   bool consumeToken() {
672     FormatToken *Tok = CurrentToken;
673     next();
674     switch (Tok->Tok.getKind()) {
675     case tok::plus:
676     case tok::minus:
677       if (!Tok->Previous && Line.MustBeDeclaration)
678         Tok->Type = TT_ObjCMethodSpecifier;
679       break;
680     case tok::colon:
681       if (!Tok->Previous)
682         return false;
683       // Colons from ?: are handled in parseConditional().
684       if (Style.Language == FormatStyle::LK_JavaScript) {
685         if (Contexts.back().ColonIsForRangeExpr || // colon in for loop
686             (Contexts.size() == 1 &&               // switch/case labels
687              !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
688             Contexts.back().ContextKind == tok::l_paren ||  // function params
689             Contexts.back().ContextKind == tok::l_square || // array type
690             (!Contexts.back().IsExpression &&
691              Contexts.back().ContextKind == tok::l_brace) || // object type
692             (Contexts.size() == 1 &&
693              Line.MustBeDeclaration)) { // method/property declaration
694           Contexts.back().IsExpression = false;
695           Tok->Type = TT_JsTypeColon;
696           break;
697         }
698       }
699       if (Contexts.back().ColonIsDictLiteral ||
700           Style.Language == FormatStyle::LK_Proto ||
701           Style.Language == FormatStyle::LK_TextProto) {
702         Tok->Type = TT_DictLiteral;
703         if (Style.Language == FormatStyle::LK_TextProto) {
704           if (FormatToken *Previous = Tok->getPreviousNonComment())
705             Previous->Type = TT_SelectorName;
706         }
707       } else if (Contexts.back().ColonIsObjCMethodExpr ||
708                  Line.startsWith(TT_ObjCMethodSpecifier)) {
709         Tok->Type = TT_ObjCMethodExpr;
710         const FormatToken *BeforePrevious = Tok->Previous->Previous;
711         // Ensure we tag all identifiers in method declarations as
712         // TT_SelectorName.
713         bool UnknownIdentifierInMethodDeclaration =
714             Line.startsWith(TT_ObjCMethodSpecifier) &&
715             Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
716         if (!BeforePrevious ||
717             // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
718             !(BeforePrevious->is(TT_CastRParen) ||
719               (BeforePrevious->is(TT_ObjCMethodExpr) &&
720                BeforePrevious->is(tok::colon))) ||
721             BeforePrevious->is(tok::r_square) ||
722             Contexts.back().LongestObjCSelectorName == 0 ||
723             UnknownIdentifierInMethodDeclaration) {
724           Tok->Previous->Type = TT_SelectorName;
725           if (!Contexts.back().FirstObjCSelectorName)
726             Contexts.back().FirstObjCSelectorName = Tok->Previous;
727           else if (Tok->Previous->ColumnWidth >
728                    Contexts.back().LongestObjCSelectorName)
729             Contexts.back().LongestObjCSelectorName =
730                 Tok->Previous->ColumnWidth;
731           Tok->Previous->ParameterIndex =
732               Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
733           ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
734         }
735       } else if (Contexts.back().ColonIsForRangeExpr) {
736         Tok->Type = TT_RangeBasedForLoopColon;
737       } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
738         Tok->Type = TT_BitFieldColon;
739       } else if (Contexts.size() == 1 &&
740                  !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
741         if (Tok->getPreviousNonComment()->isOneOf(tok::r_paren,
742                                                   tok::kw_noexcept))
743           Tok->Type = TT_CtorInitializerColon;
744         else
745           Tok->Type = TT_InheritanceColon;
746       } else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
747                  (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
748                   (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
749                    Tok->Next->Next->is(tok::colon)))) {
750         // This handles a special macro in ObjC code where selectors including
751         // the colon are passed as macro arguments.
752         Tok->Type = TT_ObjCMethodExpr;
753       } else if (Contexts.back().ContextKind == tok::l_paren) {
754         Tok->Type = TT_InlineASMColon;
755       }
756       break;
757     case tok::pipe:
758     case tok::amp:
759       // | and & in declarations/type expressions represent union and
760       // intersection types, respectively.
761       if (Style.Language == FormatStyle::LK_JavaScript &&
762           !Contexts.back().IsExpression)
763         Tok->Type = TT_JsTypeOperator;
764       break;
765     case tok::kw_if:
766     case tok::kw_while:
767       if (Tok->is(tok::kw_if) && CurrentToken &&
768           CurrentToken->is(tok::kw_constexpr))
769         next();
770       if (CurrentToken && CurrentToken->is(tok::l_paren)) {
771         next();
772         if (!parseParens(/*LookForDecls=*/true))
773           return false;
774       }
775       break;
776     case tok::kw_for:
777       if (Style.Language == FormatStyle::LK_JavaScript) {
778         // x.for and {for: ...}
779         if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
780             (Tok->Next && Tok->Next->is(tok::colon)))
781           break;
782         // JS' for await ( ...
783         if (CurrentToken && CurrentToken->is(Keywords.kw_await))
784           next();
785       }
786       Contexts.back().ColonIsForRangeExpr = true;
787       next();
788       if (!parseParens())
789         return false;
790       break;
791     case tok::l_paren:
792       // When faced with 'operator()()', the kw_operator handler incorrectly
793       // marks the first l_paren as a OverloadedOperatorLParen. Here, we make
794       // the first two parens OverloadedOperators and the second l_paren an
795       // OverloadedOperatorLParen.
796       if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
797           Tok->Previous->MatchingParen &&
798           Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
799         Tok->Previous->Type = TT_OverloadedOperator;
800         Tok->Previous->MatchingParen->Type = TT_OverloadedOperator;
801         Tok->Type = TT_OverloadedOperatorLParen;
802       }
803 
804       if (!parseParens())
805         return false;
806       if (Line.MustBeDeclaration && Contexts.size() == 1 &&
807           !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
808           (!Tok->Previous ||
809            !Tok->Previous->isOneOf(tok::kw_decltype, tok::kw___attribute,
810                                    TT_LeadingJavaAnnotation)))
811         Line.MightBeFunctionDecl = true;
812       break;
813     case tok::l_square:
814       if (!parseSquare())
815         return false;
816       break;
817     case tok::l_brace:
818       if (Style.Language == FormatStyle::LK_TextProto) {
819         FormatToken *Previous = Tok->getPreviousNonComment();
820         if (Previous && Previous->Type != TT_DictLiteral)
821           Previous->Type = TT_SelectorName;
822       }
823       if (!parseBrace())
824         return false;
825       break;
826     case tok::less:
827       if (parseAngle()) {
828         Tok->Type = TT_TemplateOpener;
829         // In TT_Proto, we must distignuish between:
830         //   map<key, value>
831         //   msg < item: data >
832         //   msg: < item: data >
833         // In TT_TextProto, map<key, value> does not occur.
834         if (Style.Language == FormatStyle::LK_TextProto ||
835             (Style.Language == FormatStyle::LK_Proto && Tok->Previous &&
836              Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
837           Tok->Type = TT_DictLiteral;
838           FormatToken *Previous = Tok->getPreviousNonComment();
839           if (Previous && Previous->Type != TT_DictLiteral)
840             Previous->Type = TT_SelectorName;
841         }
842       } else {
843         Tok->Type = TT_BinaryOperator;
844         NonTemplateLess.insert(Tok);
845         CurrentToken = Tok;
846         next();
847       }
848       break;
849     case tok::r_paren:
850     case tok::r_square:
851       return false;
852     case tok::r_brace:
853       // Lines can start with '}'.
854       if (Tok->Previous)
855         return false;
856       break;
857     case tok::greater:
858       if (Style.Language != FormatStyle::LK_TextProto)
859         Tok->Type = TT_BinaryOperator;
860       break;
861     case tok::kw_operator:
862       if (Style.Language == FormatStyle::LK_TextProto ||
863           Style.Language == FormatStyle::LK_Proto)
864         break;
865       while (CurrentToken &&
866              !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
867         if (CurrentToken->isOneOf(tok::star, tok::amp))
868           CurrentToken->Type = TT_PointerOrReference;
869         consumeToken();
870         if (CurrentToken &&
871             CurrentToken->Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator,
872                                             tok::comma))
873           CurrentToken->Previous->Type = TT_OverloadedOperator;
874       }
875       if (CurrentToken) {
876         CurrentToken->Type = TT_OverloadedOperatorLParen;
877         if (CurrentToken->Previous->is(TT_BinaryOperator))
878           CurrentToken->Previous->Type = TT_OverloadedOperator;
879       }
880       break;
881     case tok::question:
882       if (Style.Language == FormatStyle::LK_JavaScript && Tok->Next &&
883           Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
884                              tok::r_brace)) {
885         // Question marks before semicolons, colons, etc. indicate optional
886         // types (fields, parameters), e.g.
887         //   function(x?: string, y?) {...}
888         //   class X { y?; }
889         Tok->Type = TT_JsTypeOptionalQuestion;
890         break;
891       }
892       // Declarations cannot be conditional expressions, this can only be part
893       // of a type declaration.
894       if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
895           Style.Language == FormatStyle::LK_JavaScript)
896         break;
897       parseConditional();
898       break;
899     case tok::kw_template:
900       parseTemplateDeclaration();
901       break;
902     case tok::comma:
903       if (Contexts.back().InCtorInitializer)
904         Tok->Type = TT_CtorInitializerComma;
905       else if (Contexts.back().InInheritanceList)
906         Tok->Type = TT_InheritanceComma;
907       else if (Contexts.back().FirstStartOfName &&
908                (Contexts.size() == 1 || Line.startsWith(tok::kw_for))) {
909         Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
910         Line.IsMultiVariableDeclStmt = true;
911       }
912       if (Contexts.back().IsForEachMacro)
913         Contexts.back().IsExpression = true;
914       break;
915     case tok::identifier:
916       if (Tok->isOneOf(Keywords.kw___has_include,
917                        Keywords.kw___has_include_next)) {
918         parseHasInclude();
919       }
920       break;
921     default:
922       break;
923     }
924     return true;
925   }
926 
927   void parseIncludeDirective() {
928     if (CurrentToken && CurrentToken->is(tok::less)) {
929       next();
930       while (CurrentToken) {
931         // Mark tokens up to the trailing line comments as implicit string
932         // literals.
933         if (CurrentToken->isNot(tok::comment) &&
934             !CurrentToken->TokenText.startswith("//"))
935           CurrentToken->Type = TT_ImplicitStringLiteral;
936         next();
937       }
938     }
939   }
940 
941   void parseWarningOrError() {
942     next();
943     // We still want to format the whitespace left of the first token of the
944     // warning or error.
945     next();
946     while (CurrentToken) {
947       CurrentToken->Type = TT_ImplicitStringLiteral;
948       next();
949     }
950   }
951 
952   void parsePragma() {
953     next(); // Consume "pragma".
954     if (CurrentToken &&
955         CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) {
956       bool IsMark = CurrentToken->is(Keywords.kw_mark);
957       next(); // Consume "mark".
958       next(); // Consume first token (so we fix leading whitespace).
959       while (CurrentToken) {
960         if (IsMark || CurrentToken->Previous->is(TT_BinaryOperator))
961           CurrentToken->Type = TT_ImplicitStringLiteral;
962         next();
963       }
964     }
965   }
966 
967   void parseHasInclude() {
968     if (!CurrentToken || !CurrentToken->is(tok::l_paren))
969       return;
970     next(); // '('
971     parseIncludeDirective();
972     next(); // ')'
973   }
974 
975   LineType parsePreprocessorDirective() {
976     bool IsFirstToken = CurrentToken->IsFirst;
977     LineType Type = LT_PreprocessorDirective;
978     next();
979     if (!CurrentToken)
980       return Type;
981 
982     if (Style.Language == FormatStyle::LK_JavaScript && IsFirstToken) {
983       // JavaScript files can contain shebang lines of the form:
984       // #!/usr/bin/env node
985       // Treat these like C++ #include directives.
986       while (CurrentToken) {
987         // Tokens cannot be comments here.
988         CurrentToken->Type = TT_ImplicitStringLiteral;
989         next();
990       }
991       return LT_ImportStatement;
992     }
993 
994     if (CurrentToken->Tok.is(tok::numeric_constant)) {
995       CurrentToken->SpacesRequiredBefore = 1;
996       return Type;
997     }
998     // Hashes in the middle of a line can lead to any strange token
999     // sequence.
1000     if (!CurrentToken->Tok.getIdentifierInfo())
1001       return Type;
1002     switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1003     case tok::pp_include:
1004     case tok::pp_include_next:
1005     case tok::pp_import:
1006       next();
1007       parseIncludeDirective();
1008       Type = LT_ImportStatement;
1009       break;
1010     case tok::pp_error:
1011     case tok::pp_warning:
1012       parseWarningOrError();
1013       break;
1014     case tok::pp_pragma:
1015       parsePragma();
1016       break;
1017     case tok::pp_if:
1018     case tok::pp_elif:
1019       Contexts.back().IsExpression = true;
1020       parseLine();
1021       break;
1022     default:
1023       break;
1024     }
1025     while (CurrentToken) {
1026       FormatToken *Tok = CurrentToken;
1027       next();
1028       if (Tok->is(tok::l_paren))
1029         parseParens();
1030       else if (Tok->isOneOf(Keywords.kw___has_include,
1031                             Keywords.kw___has_include_next))
1032         parseHasInclude();
1033     }
1034     return Type;
1035   }
1036 
1037 public:
1038   LineType parseLine() {
1039     NonTemplateLess.clear();
1040     if (CurrentToken->is(tok::hash))
1041       return parsePreprocessorDirective();
1042 
1043     // Directly allow to 'import <string-literal>' to support protocol buffer
1044     // definitions (github.com/google/protobuf) or missing "#" (either way we
1045     // should not break the line).
1046     IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1047     if ((Style.Language == FormatStyle::LK_Java &&
1048          CurrentToken->is(Keywords.kw_package)) ||
1049         (Info && Info->getPPKeywordID() == tok::pp_import &&
1050          CurrentToken->Next &&
1051          CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1052                                      tok::kw_static))) {
1053       next();
1054       parseIncludeDirective();
1055       return LT_ImportStatement;
1056     }
1057 
1058     // If this line starts and ends in '<' and '>', respectively, it is likely
1059     // part of "#define <a/b.h>".
1060     if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1061       parseIncludeDirective();
1062       return LT_ImportStatement;
1063     }
1064 
1065     // In .proto files, top-level options are very similar to import statements
1066     // and should not be line-wrapped.
1067     if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
1068         CurrentToken->is(Keywords.kw_option)) {
1069       next();
1070       if (CurrentToken && CurrentToken->is(tok::identifier))
1071         return LT_ImportStatement;
1072     }
1073 
1074     bool KeywordVirtualFound = false;
1075     bool ImportStatement = false;
1076 
1077     // import {...} from '...';
1078     if (Style.Language == FormatStyle::LK_JavaScript &&
1079         CurrentToken->is(Keywords.kw_import))
1080       ImportStatement = true;
1081 
1082     while (CurrentToken) {
1083       if (CurrentToken->is(tok::kw_virtual))
1084         KeywordVirtualFound = true;
1085       if (Style.Language == FormatStyle::LK_JavaScript) {
1086         // export {...} from '...';
1087         // An export followed by "from 'some string';" is a re-export from
1088         // another module identified by a URI and is treated as a
1089         // LT_ImportStatement (i.e. prevent wraps on it for long URIs).
1090         // Just "export {...};" or "export class ..." should not be treated as
1091         // an import in this sense.
1092         if (Line.First->is(tok::kw_export) &&
1093             CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
1094             CurrentToken->Next->isStringLiteral())
1095           ImportStatement = true;
1096         if (isClosureImportStatement(*CurrentToken))
1097           ImportStatement = true;
1098       }
1099       if (!consumeToken())
1100         return LT_Invalid;
1101     }
1102     if (KeywordVirtualFound)
1103       return LT_VirtualFunctionDecl;
1104     if (ImportStatement)
1105       return LT_ImportStatement;
1106 
1107     if (Line.startsWith(TT_ObjCMethodSpecifier)) {
1108       if (Contexts.back().FirstObjCSelectorName)
1109         Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
1110             Contexts.back().LongestObjCSelectorName;
1111       return LT_ObjCMethodDecl;
1112     }
1113 
1114     return LT_Other;
1115   }
1116 
1117 private:
1118   bool isClosureImportStatement(const FormatToken &Tok) {
1119     // FIXME: Closure-library specific stuff should not be hard-coded but be
1120     // configurable.
1121     return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
1122            Tok.Next->Next &&
1123            (Tok.Next->Next->TokenText == "module" ||
1124             Tok.Next->Next->TokenText == "provide" ||
1125             Tok.Next->Next->TokenText == "require" ||
1126             Tok.Next->Next->TokenText == "forwardDeclare") &&
1127            Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
1128   }
1129 
1130   void resetTokenMetadata(FormatToken *Token) {
1131     if (!Token)
1132       return;
1133 
1134     // Reset token type in case we have already looked at it and then
1135     // recovered from an error (e.g. failure to find the matching >).
1136     if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro,
1137                                TT_FunctionLBrace, TT_ImplicitStringLiteral,
1138                                TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow,
1139                                TT_OverloadedOperator, TT_RegexLiteral,
1140                                TT_TemplateString, TT_ObjCStringLiteral))
1141       CurrentToken->Type = TT_Unknown;
1142     CurrentToken->Role.reset();
1143     CurrentToken->MatchingParen = nullptr;
1144     CurrentToken->FakeLParens.clear();
1145     CurrentToken->FakeRParens = 0;
1146   }
1147 
1148   void next() {
1149     if (CurrentToken) {
1150       CurrentToken->NestingLevel = Contexts.size() - 1;
1151       CurrentToken->BindingStrength = Contexts.back().BindingStrength;
1152       modifyContext(*CurrentToken);
1153       determineTokenType(*CurrentToken);
1154       CurrentToken = CurrentToken->Next;
1155     }
1156 
1157     resetTokenMetadata(CurrentToken);
1158   }
1159 
1160   /// A struct to hold information valid in a specific context, e.g.
1161   /// a pair of parenthesis.
1162   struct Context {
1163     Context(tok::TokenKind ContextKind, unsigned BindingStrength,
1164             bool IsExpression)
1165         : ContextKind(ContextKind), BindingStrength(BindingStrength),
1166           IsExpression(IsExpression) {}
1167 
1168     tok::TokenKind ContextKind;
1169     unsigned BindingStrength;
1170     bool IsExpression;
1171     unsigned LongestObjCSelectorName = 0;
1172     bool ColonIsForRangeExpr = false;
1173     bool ColonIsDictLiteral = false;
1174     bool ColonIsObjCMethodExpr = false;
1175     FormatToken *FirstObjCSelectorName = nullptr;
1176     FormatToken *FirstStartOfName = nullptr;
1177     bool CanBeExpression = true;
1178     bool InTemplateArgument = false;
1179     bool InCtorInitializer = false;
1180     bool InInheritanceList = false;
1181     bool CaretFound = false;
1182     bool IsForEachMacro = false;
1183     bool InCpp11AttributeSpecifier = false;
1184   };
1185 
1186   /// Puts a new \c Context onto the stack \c Contexts for the lifetime
1187   /// of each instance.
1188   struct ScopedContextCreator {
1189     AnnotatingParser &P;
1190 
1191     ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
1192                          unsigned Increase)
1193         : P(P) {
1194       P.Contexts.push_back(Context(ContextKind,
1195                                    P.Contexts.back().BindingStrength + Increase,
1196                                    P.Contexts.back().IsExpression));
1197     }
1198 
1199     ~ScopedContextCreator() { P.Contexts.pop_back(); }
1200   };
1201 
1202   void modifyContext(const FormatToken &Current) {
1203     if (Current.getPrecedence() == prec::Assignment &&
1204         !Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) &&
1205         // Type aliases use `type X = ...;` in TypeScript and can be exported
1206         // using `export type ...`.
1207         !(Style.Language == FormatStyle::LK_JavaScript &&
1208           (Line.startsWith(Keywords.kw_type, tok::identifier) ||
1209            Line.startsWith(tok::kw_export, Keywords.kw_type,
1210                            tok::identifier))) &&
1211         (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
1212       Contexts.back().IsExpression = true;
1213       if (!Line.startsWith(TT_UnaryOperator)) {
1214         for (FormatToken *Previous = Current.Previous;
1215              Previous && Previous->Previous &&
1216              !Previous->Previous->isOneOf(tok::comma, tok::semi);
1217              Previous = Previous->Previous) {
1218           if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
1219             Previous = Previous->MatchingParen;
1220             if (!Previous)
1221               break;
1222           }
1223           if (Previous->opensScope())
1224             break;
1225           if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
1226               Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
1227               Previous->Previous && Previous->Previous->isNot(tok::equal))
1228             Previous->Type = TT_PointerOrReference;
1229         }
1230       }
1231     } else if (Current.is(tok::lessless) &&
1232                (!Current.Previous || !Current.Previous->is(tok::kw_operator))) {
1233       Contexts.back().IsExpression = true;
1234     } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
1235       Contexts.back().IsExpression = true;
1236     } else if (Current.is(TT_TrailingReturnArrow)) {
1237       Contexts.back().IsExpression = false;
1238     } else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) {
1239       Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
1240     } else if (Current.Previous &&
1241                Current.Previous->is(TT_CtorInitializerColon)) {
1242       Contexts.back().IsExpression = true;
1243       Contexts.back().InCtorInitializer = true;
1244     } else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
1245       Contexts.back().InInheritanceList = true;
1246     } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
1247       for (FormatToken *Previous = Current.Previous;
1248            Previous && Previous->isOneOf(tok::star, tok::amp);
1249            Previous = Previous->Previous)
1250         Previous->Type = TT_PointerOrReference;
1251       if (Line.MustBeDeclaration && !Contexts.front().InCtorInitializer)
1252         Contexts.back().IsExpression = false;
1253     } else if (Current.is(tok::kw_new)) {
1254       Contexts.back().CanBeExpression = false;
1255     } else if (Current.isOneOf(tok::semi, tok::exclaim)) {
1256       // This should be the condition or increment in a for-loop.
1257       Contexts.back().IsExpression = true;
1258     }
1259   }
1260 
1261   void determineTokenType(FormatToken &Current) {
1262     if (!Current.is(TT_Unknown))
1263       // The token type is already known.
1264       return;
1265 
1266     if (Style.Language == FormatStyle::LK_JavaScript) {
1267       if (Current.is(tok::exclaim)) {
1268         if (Current.Previous &&
1269             (Current.Previous->isOneOf(tok::identifier, tok::kw_namespace,
1270                                        tok::r_paren, tok::r_square,
1271                                        tok::r_brace) ||
1272              Current.Previous->Tok.isLiteral())) {
1273           Current.Type = TT_JsNonNullAssertion;
1274           return;
1275         }
1276         if (Current.Next &&
1277             Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
1278           Current.Type = TT_JsNonNullAssertion;
1279           return;
1280         }
1281       }
1282     }
1283 
1284     // Line.MightBeFunctionDecl can only be true after the parentheses of a
1285     // function declaration have been found. In this case, 'Current' is a
1286     // trailing token of this declaration and thus cannot be a name.
1287     if (Current.is(Keywords.kw_instanceof)) {
1288       Current.Type = TT_BinaryOperator;
1289     } else if (isStartOfName(Current) &&
1290                (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
1291       Contexts.back().FirstStartOfName = &Current;
1292       Current.Type = TT_StartOfName;
1293     } else if (Current.is(tok::semi)) {
1294       // Reset FirstStartOfName after finding a semicolon so that a for loop
1295       // with multiple increment statements is not confused with a for loop
1296       // having multiple variable declarations.
1297       Contexts.back().FirstStartOfName = nullptr;
1298     } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
1299       AutoFound = true;
1300     } else if (Current.is(tok::arrow) &&
1301                Style.Language == FormatStyle::LK_Java) {
1302       Current.Type = TT_LambdaArrow;
1303     } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration &&
1304                Current.NestingLevel == 0) {
1305       Current.Type = TT_TrailingReturnArrow;
1306     } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
1307       Current.Type = determineStarAmpUsage(Current,
1308                                            Contexts.back().CanBeExpression &&
1309                                                Contexts.back().IsExpression,
1310                                            Contexts.back().InTemplateArgument);
1311     } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
1312       Current.Type = determinePlusMinusCaretUsage(Current);
1313       if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
1314         Contexts.back().CaretFound = true;
1315     } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
1316       Current.Type = determineIncrementUsage(Current);
1317     } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
1318       Current.Type = TT_UnaryOperator;
1319     } else if (Current.is(tok::question)) {
1320       if (Style.Language == FormatStyle::LK_JavaScript &&
1321           Line.MustBeDeclaration && !Contexts.back().IsExpression) {
1322         // In JavaScript, `interface X { foo?(): bar; }` is an optional method
1323         // on the interface, not a ternary expression.
1324         Current.Type = TT_JsTypeOptionalQuestion;
1325       } else {
1326         Current.Type = TT_ConditionalExpr;
1327       }
1328     } else if (Current.isBinaryOperator() &&
1329                (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
1330                (!Current.is(tok::greater) &&
1331                 Style.Language != FormatStyle::LK_TextProto)) {
1332       Current.Type = TT_BinaryOperator;
1333     } else if (Current.is(tok::comment)) {
1334       if (Current.TokenText.startswith("/*")) {
1335         if (Current.TokenText.endswith("*/"))
1336           Current.Type = TT_BlockComment;
1337         else
1338           // The lexer has for some reason determined a comment here. But we
1339           // cannot really handle it, if it isn't properly terminated.
1340           Current.Tok.setKind(tok::unknown);
1341       } else {
1342         Current.Type = TT_LineComment;
1343       }
1344     } else if (Current.is(tok::r_paren)) {
1345       if (rParenEndsCast(Current))
1346         Current.Type = TT_CastRParen;
1347       if (Current.MatchingParen && Current.Next &&
1348           !Current.Next->isBinaryOperator() &&
1349           !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
1350                                  tok::comma, tok::period, tok::arrow,
1351                                  tok::coloncolon))
1352         if (FormatToken *AfterParen = Current.MatchingParen->Next) {
1353           // Make sure this isn't the return type of an Obj-C block declaration
1354           if (AfterParen->Tok.isNot(tok::caret)) {
1355             if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
1356               if (BeforeParen->is(tok::identifier) &&
1357                   BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
1358                   (!BeforeParen->Previous ||
1359                    BeforeParen->Previous->ClosesTemplateDeclaration))
1360                 Current.Type = TT_FunctionAnnotationRParen;
1361           }
1362         }
1363     } else if (Current.is(tok::at) && Current.Next &&
1364                Style.Language != FormatStyle::LK_JavaScript &&
1365                Style.Language != FormatStyle::LK_Java) {
1366       // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
1367       // marks declarations and properties that need special formatting.
1368       switch (Current.Next->Tok.getObjCKeywordID()) {
1369       case tok::objc_interface:
1370       case tok::objc_implementation:
1371       case tok::objc_protocol:
1372         Current.Type = TT_ObjCDecl;
1373         break;
1374       case tok::objc_property:
1375         Current.Type = TT_ObjCProperty;
1376         break;
1377       default:
1378         break;
1379       }
1380     } else if (Current.is(tok::period)) {
1381       FormatToken *PreviousNoComment = Current.getPreviousNonComment();
1382       if (PreviousNoComment &&
1383           PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
1384         Current.Type = TT_DesignatedInitializerPeriod;
1385       else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
1386                Current.Previous->isOneOf(TT_JavaAnnotation,
1387                                          TT_LeadingJavaAnnotation)) {
1388         Current.Type = Current.Previous->Type;
1389       }
1390     } else if (canBeObjCSelectorComponent(Current) &&
1391                // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
1392                Current.Previous && Current.Previous->is(TT_CastRParen) &&
1393                Current.Previous->MatchingParen &&
1394                Current.Previous->MatchingParen->Previous &&
1395                Current.Previous->MatchingParen->Previous->is(
1396                    TT_ObjCMethodSpecifier)) {
1397       // This is the first part of an Objective-C selector name. (If there's no
1398       // colon after this, this is the only place which annotates the identifier
1399       // as a selector.)
1400       Current.Type = TT_SelectorName;
1401     } else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
1402                Current.Previous &&
1403                !Current.Previous->isOneOf(tok::equal, tok::at) &&
1404                Line.MightBeFunctionDecl && Contexts.size() == 1) {
1405       // Line.MightBeFunctionDecl can only be true after the parentheses of a
1406       // function declaration have been found.
1407       Current.Type = TT_TrailingAnnotation;
1408     } else if ((Style.Language == FormatStyle::LK_Java ||
1409                 Style.Language == FormatStyle::LK_JavaScript) &&
1410                Current.Previous) {
1411       if (Current.Previous->is(tok::at) &&
1412           Current.isNot(Keywords.kw_interface)) {
1413         const FormatToken &AtToken = *Current.Previous;
1414         const FormatToken *Previous = AtToken.getPreviousNonComment();
1415         if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
1416           Current.Type = TT_LeadingJavaAnnotation;
1417         else
1418           Current.Type = TT_JavaAnnotation;
1419       } else if (Current.Previous->is(tok::period) &&
1420                  Current.Previous->isOneOf(TT_JavaAnnotation,
1421                                            TT_LeadingJavaAnnotation)) {
1422         Current.Type = Current.Previous->Type;
1423       }
1424     }
1425   }
1426 
1427   /// Take a guess at whether \p Tok starts a name of a function or
1428   /// variable declaration.
1429   ///
1430   /// This is a heuristic based on whether \p Tok is an identifier following
1431   /// something that is likely a type.
1432   bool isStartOfName(const FormatToken &Tok) {
1433     if (Tok.isNot(tok::identifier) || !Tok.Previous)
1434       return false;
1435 
1436     if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
1437                               Keywords.kw_as))
1438       return false;
1439     if (Style.Language == FormatStyle::LK_JavaScript &&
1440         Tok.Previous->is(Keywords.kw_in))
1441       return false;
1442 
1443     // Skip "const" as it does not have an influence on whether this is a name.
1444     FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
1445     while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
1446       PreviousNotConst = PreviousNotConst->getPreviousNonComment();
1447 
1448     if (!PreviousNotConst)
1449       return false;
1450 
1451     bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
1452                        PreviousNotConst->Previous &&
1453                        PreviousNotConst->Previous->is(tok::hash);
1454 
1455     if (PreviousNotConst->is(TT_TemplateCloser))
1456       return PreviousNotConst && PreviousNotConst->MatchingParen &&
1457              PreviousNotConst->MatchingParen->Previous &&
1458              PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
1459              PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
1460 
1461     if (PreviousNotConst->is(tok::r_paren) && PreviousNotConst->MatchingParen &&
1462         PreviousNotConst->MatchingParen->Previous &&
1463         PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype))
1464       return true;
1465 
1466     return (!IsPPKeyword &&
1467             PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto)) ||
1468            PreviousNotConst->is(TT_PointerOrReference) ||
1469            PreviousNotConst->isSimpleTypeSpecifier();
1470   }
1471 
1472   /// Determine whether ')' is ending a cast.
1473   bool rParenEndsCast(const FormatToken &Tok) {
1474     // C-style casts are only used in C++ and Java.
1475     if (!Style.isCpp() && Style.Language != FormatStyle::LK_Java)
1476       return false;
1477 
1478     // Empty parens aren't casts and there are no casts at the end of the line.
1479     if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
1480       return false;
1481 
1482     FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
1483     if (LeftOfParens) {
1484       // If there is a closing parenthesis left of the current parentheses,
1485       // look past it as these might be chained casts.
1486       if (LeftOfParens->is(tok::r_paren)) {
1487         if (!LeftOfParens->MatchingParen ||
1488             !LeftOfParens->MatchingParen->Previous)
1489           return false;
1490         LeftOfParens = LeftOfParens->MatchingParen->Previous;
1491       }
1492 
1493       // If there is an identifier (or with a few exceptions a keyword) right
1494       // before the parentheses, this is unlikely to be a cast.
1495       if (LeftOfParens->Tok.getIdentifierInfo() &&
1496           !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
1497                                  tok::kw_delete))
1498         return false;
1499 
1500       // Certain other tokens right before the parentheses are also signals that
1501       // this cannot be a cast.
1502       if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
1503                                 TT_TemplateCloser, tok::ellipsis))
1504         return false;
1505     }
1506 
1507     if (Tok.Next->is(tok::question))
1508       return false;
1509 
1510     // As Java has no function types, a "(" after the ")" likely means that this
1511     // is a cast.
1512     if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
1513       return true;
1514 
1515     // If a (non-string) literal follows, this is likely a cast.
1516     if (Tok.Next->isNot(tok::string_literal) &&
1517         (Tok.Next->Tok.isLiteral() ||
1518          Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
1519       return true;
1520 
1521     // Heuristically try to determine whether the parentheses contain a type.
1522     bool ParensAreType =
1523         !Tok.Previous ||
1524         Tok.Previous->isOneOf(TT_PointerOrReference, TT_TemplateCloser) ||
1525         Tok.Previous->isSimpleTypeSpecifier();
1526     bool ParensCouldEndDecl =
1527         Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
1528     if (ParensAreType && !ParensCouldEndDecl)
1529       return true;
1530 
1531     // At this point, we heuristically assume that there are no casts at the
1532     // start of the line. We assume that we have found most cases where there
1533     // are by the logic above, e.g. "(void)x;".
1534     if (!LeftOfParens)
1535       return false;
1536 
1537     // Certain token types inside the parentheses mean that this can't be a
1538     // cast.
1539     for (const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
1540          Token = Token->Next)
1541       if (Token->is(TT_BinaryOperator))
1542         return false;
1543 
1544     // If the following token is an identifier or 'this', this is a cast. All
1545     // cases where this can be something else are handled above.
1546     if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
1547       return true;
1548 
1549     if (!Tok.Next->Next)
1550       return false;
1551 
1552     // If the next token after the parenthesis is a unary operator, assume
1553     // that this is cast, unless there are unexpected tokens inside the
1554     // parenthesis.
1555     bool NextIsUnary =
1556         Tok.Next->isUnaryOperator() || Tok.Next->isOneOf(tok::amp, tok::star);
1557     if (!NextIsUnary || Tok.Next->is(tok::plus) ||
1558         !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant))
1559       return false;
1560     // Search for unexpected tokens.
1561     for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
1562          Prev = Prev->Previous) {
1563       if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
1564         return false;
1565     }
1566     return true;
1567   }
1568 
1569   /// Return the type of the given token assuming it is * or &.
1570   TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
1571                                   bool InTemplateArgument) {
1572     if (Style.Language == FormatStyle::LK_JavaScript)
1573       return TT_BinaryOperator;
1574 
1575     const FormatToken *PrevToken = Tok.getPreviousNonComment();
1576     if (!PrevToken)
1577       return TT_UnaryOperator;
1578 
1579     const FormatToken *NextToken = Tok.getNextNonComment();
1580     if (!NextToken ||
1581         NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_const) ||
1582         (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
1583       return TT_PointerOrReference;
1584 
1585     if (PrevToken->is(tok::coloncolon))
1586       return TT_PointerOrReference;
1587 
1588     if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
1589                            tok::comma, tok::semi, tok::kw_return, tok::colon,
1590                            tok::equal, tok::kw_delete, tok::kw_sizeof,
1591                            tok::kw_throw) ||
1592         PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
1593                            TT_UnaryOperator, TT_CastRParen))
1594       return TT_UnaryOperator;
1595 
1596     if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
1597       return TT_PointerOrReference;
1598     if (NextToken->is(tok::kw_operator) && !IsExpression)
1599       return TT_PointerOrReference;
1600     if (NextToken->isOneOf(tok::comma, tok::semi))
1601       return TT_PointerOrReference;
1602 
1603     if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen) {
1604       FormatToken *TokenBeforeMatchingParen =
1605           PrevToken->MatchingParen->getPreviousNonComment();
1606       if (TokenBeforeMatchingParen &&
1607           TokenBeforeMatchingParen->isOneOf(tok::kw_typeof, tok::kw_decltype))
1608         return TT_PointerOrReference;
1609     }
1610 
1611     if (PrevToken->Tok.isLiteral() ||
1612         PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
1613                            tok::kw_false, tok::r_brace) ||
1614         NextToken->Tok.isLiteral() ||
1615         NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
1616         NextToken->isUnaryOperator() ||
1617         // If we know we're in a template argument, there are no named
1618         // declarations. Thus, having an identifier on the right-hand side
1619         // indicates a binary operator.
1620         (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
1621       return TT_BinaryOperator;
1622 
1623     // "&&(" is quite unlikely to be two successive unary "&".
1624     if (Tok.is(tok::ampamp) && NextToken && NextToken->is(tok::l_paren))
1625       return TT_BinaryOperator;
1626 
1627     // This catches some cases where evaluation order is used as control flow:
1628     //   aaa && aaa->f();
1629     const FormatToken *NextNextToken = NextToken->getNextNonComment();
1630     if (NextNextToken && NextNextToken->is(tok::arrow))
1631       return TT_BinaryOperator;
1632 
1633     // It is very unlikely that we are going to find a pointer or reference type
1634     // definition on the RHS of an assignment.
1635     if (IsExpression && !Contexts.back().CaretFound)
1636       return TT_BinaryOperator;
1637 
1638     return TT_PointerOrReference;
1639   }
1640 
1641   TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
1642     const FormatToken *PrevToken = Tok.getPreviousNonComment();
1643     if (!PrevToken)
1644       return TT_UnaryOperator;
1645 
1646     if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
1647       // This must be a sequence of leading unary operators.
1648       return TT_UnaryOperator;
1649 
1650     // Use heuristics to recognize unary operators.
1651     if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
1652                            tok::question, tok::colon, tok::kw_return,
1653                            tok::kw_case, tok::at, tok::l_brace))
1654       return TT_UnaryOperator;
1655 
1656     // There can't be two consecutive binary operators.
1657     if (PrevToken->is(TT_BinaryOperator))
1658       return TT_UnaryOperator;
1659 
1660     // Fall back to marking the token as binary operator.
1661     return TT_BinaryOperator;
1662   }
1663 
1664   /// Determine whether ++/-- are pre- or post-increments/-decrements.
1665   TokenType determineIncrementUsage(const FormatToken &Tok) {
1666     const FormatToken *PrevToken = Tok.getPreviousNonComment();
1667     if (!PrevToken || PrevToken->is(TT_CastRParen))
1668       return TT_UnaryOperator;
1669     if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
1670       return TT_TrailingUnaryOperator;
1671 
1672     return TT_UnaryOperator;
1673   }
1674 
1675   SmallVector<Context, 8> Contexts;
1676 
1677   const FormatStyle &Style;
1678   AnnotatedLine &Line;
1679   FormatToken *CurrentToken;
1680   bool AutoFound;
1681   const AdditionalKeywords &Keywords;
1682 
1683   // Set of "<" tokens that do not open a template parameter list. If parseAngle
1684   // determines that a specific token can't be a template opener, it will make
1685   // same decision irrespective of the decisions for tokens leading up to it.
1686   // Store this information to prevent this from causing exponential runtime.
1687   llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
1688 };
1689 
1690 static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
1691 static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
1692 
1693 /// Parses binary expressions by inserting fake parenthesis based on
1694 /// operator precedence.
1695 class ExpressionParser {
1696 public:
1697   ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
1698                    AnnotatedLine &Line)
1699       : Style(Style), Keywords(Keywords), Current(Line.First) {}
1700 
1701   /// Parse expressions with the given operator precedence.
1702   void parse(int Precedence = 0) {
1703     // Skip 'return' and ObjC selector colons as they are not part of a binary
1704     // expression.
1705     while (Current && (Current->is(tok::kw_return) ||
1706                        (Current->is(tok::colon) &&
1707                         Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))))
1708       next();
1709 
1710     if (!Current || Precedence > PrecedenceArrowAndPeriod)
1711       return;
1712 
1713     // Conditional expressions need to be parsed separately for proper nesting.
1714     if (Precedence == prec::Conditional) {
1715       parseConditionalExpr();
1716       return;
1717     }
1718 
1719     // Parse unary operators, which all have a higher precedence than binary
1720     // operators.
1721     if (Precedence == PrecedenceUnaryOperator) {
1722       parseUnaryOperator();
1723       return;
1724     }
1725 
1726     FormatToken *Start = Current;
1727     FormatToken *LatestOperator = nullptr;
1728     unsigned OperatorIndex = 0;
1729 
1730     while (Current) {
1731       // Consume operators with higher precedence.
1732       parse(Precedence + 1);
1733 
1734       int CurrentPrecedence = getCurrentPrecedence();
1735 
1736       if (Current && Current->is(TT_SelectorName) &&
1737           Precedence == CurrentPrecedence) {
1738         if (LatestOperator)
1739           addFakeParenthesis(Start, prec::Level(Precedence));
1740         Start = Current;
1741       }
1742 
1743       // At the end of the line or when an operator with higher precedence is
1744       // found, insert fake parenthesis and return.
1745       if (!Current ||
1746           (Current->closesScope() &&
1747            (Current->MatchingParen || Current->is(TT_TemplateString))) ||
1748           (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
1749           (CurrentPrecedence == prec::Conditional &&
1750            Precedence == prec::Assignment && Current->is(tok::colon))) {
1751         break;
1752       }
1753 
1754       // Consume scopes: (), [], <> and {}
1755       if (Current->opensScope()) {
1756         // In fragment of a JavaScript template string can look like '}..${' and
1757         // thus close a scope and open a new one at the same time.
1758         while (Current && (!Current->closesScope() || Current->opensScope())) {
1759           next();
1760           parse();
1761         }
1762         next();
1763       } else {
1764         // Operator found.
1765         if (CurrentPrecedence == Precedence) {
1766           if (LatestOperator)
1767             LatestOperator->NextOperator = Current;
1768           LatestOperator = Current;
1769           Current->OperatorIndex = OperatorIndex;
1770           ++OperatorIndex;
1771         }
1772         next(/*SkipPastLeadingComments=*/Precedence > 0);
1773       }
1774     }
1775 
1776     if (LatestOperator && (Current || Precedence > 0)) {
1777       // LatestOperator->LastOperator = true;
1778       if (Precedence == PrecedenceArrowAndPeriod) {
1779         // Call expressions don't have a binary operator precedence.
1780         addFakeParenthesis(Start, prec::Unknown);
1781       } else {
1782         addFakeParenthesis(Start, prec::Level(Precedence));
1783       }
1784     }
1785   }
1786 
1787 private:
1788   /// Gets the precedence (+1) of the given token for binary operators
1789   /// and other tokens that we treat like binary operators.
1790   int getCurrentPrecedence() {
1791     if (Current) {
1792       const FormatToken *NextNonComment = Current->getNextNonComment();
1793       if (Current->is(TT_ConditionalExpr))
1794         return prec::Conditional;
1795       if (NextNonComment && Current->is(TT_SelectorName) &&
1796           (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
1797            ((Style.Language == FormatStyle::LK_Proto ||
1798              Style.Language == FormatStyle::LK_TextProto) &&
1799             NextNonComment->is(tok::less))))
1800         return prec::Assignment;
1801       if (Current->is(TT_JsComputedPropertyName))
1802         return prec::Assignment;
1803       if (Current->is(TT_LambdaArrow))
1804         return prec::Comma;
1805       if (Current->is(TT_JsFatArrow))
1806         return prec::Assignment;
1807       if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
1808           (Current->is(tok::comment) && NextNonComment &&
1809            NextNonComment->is(TT_SelectorName)))
1810         return 0;
1811       if (Current->is(TT_RangeBasedForLoopColon))
1812         return prec::Comma;
1813       if ((Style.Language == FormatStyle::LK_Java ||
1814            Style.Language == FormatStyle::LK_JavaScript) &&
1815           Current->is(Keywords.kw_instanceof))
1816         return prec::Relational;
1817       if (Style.Language == FormatStyle::LK_JavaScript &&
1818           Current->isOneOf(Keywords.kw_in, Keywords.kw_as))
1819         return prec::Relational;
1820       if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
1821         return Current->getPrecedence();
1822       if (Current->isOneOf(tok::period, tok::arrow))
1823         return PrecedenceArrowAndPeriod;
1824       if ((Style.Language == FormatStyle::LK_Java ||
1825            Style.Language == FormatStyle::LK_JavaScript) &&
1826           Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
1827                            Keywords.kw_throws))
1828         return 0;
1829     }
1830     return -1;
1831   }
1832 
1833   void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
1834     Start->FakeLParens.push_back(Precedence);
1835     if (Precedence > prec::Unknown)
1836       Start->StartsBinaryExpression = true;
1837     if (Current) {
1838       FormatToken *Previous = Current->Previous;
1839       while (Previous->is(tok::comment) && Previous->Previous)
1840         Previous = Previous->Previous;
1841       ++Previous->FakeRParens;
1842       if (Precedence > prec::Unknown)
1843         Previous->EndsBinaryExpression = true;
1844     }
1845   }
1846 
1847   /// Parse unary operator expressions and surround them with fake
1848   /// parentheses if appropriate.
1849   void parseUnaryOperator() {
1850     llvm::SmallVector<FormatToken *, 2> Tokens;
1851     while (Current && Current->is(TT_UnaryOperator)) {
1852       Tokens.push_back(Current);
1853       next();
1854     }
1855     parse(PrecedenceArrowAndPeriod);
1856     for (FormatToken *Token : llvm::reverse(Tokens))
1857       // The actual precedence doesn't matter.
1858       addFakeParenthesis(Token, prec::Unknown);
1859   }
1860 
1861   void parseConditionalExpr() {
1862     while (Current && Current->isTrailingComment()) {
1863       next();
1864     }
1865     FormatToken *Start = Current;
1866     parse(prec::LogicalOr);
1867     if (!Current || !Current->is(tok::question))
1868       return;
1869     next();
1870     parse(prec::Assignment);
1871     if (!Current || Current->isNot(TT_ConditionalExpr))
1872       return;
1873     next();
1874     parse(prec::Assignment);
1875     addFakeParenthesis(Start, prec::Conditional);
1876   }
1877 
1878   void next(bool SkipPastLeadingComments = true) {
1879     if (Current)
1880       Current = Current->Next;
1881     while (Current &&
1882            (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
1883            Current->isTrailingComment())
1884       Current = Current->Next;
1885   }
1886 
1887   const FormatStyle &Style;
1888   const AdditionalKeywords &Keywords;
1889   FormatToken *Current;
1890 };
1891 
1892 } // end anonymous namespace
1893 
1894 void TokenAnnotator::setCommentLineLevels(
1895     SmallVectorImpl<AnnotatedLine *> &Lines) {
1896   const AnnotatedLine *NextNonCommentLine = nullptr;
1897   for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
1898                                                           E = Lines.rend();
1899        I != E; ++I) {
1900     bool CommentLine = true;
1901     for (const FormatToken *Tok = (*I)->First; Tok; Tok = Tok->Next) {
1902       if (!Tok->is(tok::comment)) {
1903         CommentLine = false;
1904         break;
1905       }
1906     }
1907 
1908     // If the comment is currently aligned with the line immediately following
1909     // it, that's probably intentional and we should keep it.
1910     if (NextNonCommentLine && CommentLine &&
1911         NextNonCommentLine->First->NewlinesBefore <= 1 &&
1912         NextNonCommentLine->First->OriginalColumn ==
1913             (*I)->First->OriginalColumn) {
1914       // Align comments for preprocessor lines with the # in column 0.
1915       // Otherwise, align with the next line.
1916       (*I)->Level = (NextNonCommentLine->Type == LT_PreprocessorDirective ||
1917                      NextNonCommentLine->Type == LT_ImportStatement)
1918                         ? 0
1919                         : NextNonCommentLine->Level;
1920     } else {
1921       NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr;
1922     }
1923 
1924     setCommentLineLevels((*I)->Children);
1925   }
1926 }
1927 
1928 static unsigned maxNestingDepth(const AnnotatedLine &Line) {
1929   unsigned Result = 0;
1930   for (const auto *Tok = Line.First; Tok != nullptr; Tok = Tok->Next)
1931     Result = std::max(Result, Tok->NestingLevel);
1932   return Result;
1933 }
1934 
1935 void TokenAnnotator::annotate(AnnotatedLine &Line) {
1936   for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1937                                                   E = Line.Children.end();
1938        I != E; ++I) {
1939     annotate(**I);
1940   }
1941   AnnotatingParser Parser(Style, Line, Keywords);
1942   Line.Type = Parser.parseLine();
1943 
1944   // With very deep nesting, ExpressionParser uses lots of stack and the
1945   // formatting algorithm is very slow. We're not going to do a good job here
1946   // anyway - it's probably generated code being formatted by mistake.
1947   // Just skip the whole line.
1948   if (maxNestingDepth(Line) > 50)
1949     Line.Type = LT_Invalid;
1950 
1951   if (Line.Type == LT_Invalid)
1952     return;
1953 
1954   ExpressionParser ExprParser(Style, Keywords, Line);
1955   ExprParser.parse();
1956 
1957   if (Line.startsWith(TT_ObjCMethodSpecifier))
1958     Line.Type = LT_ObjCMethodDecl;
1959   else if (Line.startsWith(TT_ObjCDecl))
1960     Line.Type = LT_ObjCDecl;
1961   else if (Line.startsWith(TT_ObjCProperty))
1962     Line.Type = LT_ObjCProperty;
1963 
1964   Line.First->SpacesRequiredBefore = 1;
1965   Line.First->CanBreakBefore = Line.First->MustBreakBefore;
1966 }
1967 
1968 // This function heuristically determines whether 'Current' starts the name of a
1969 // function declaration.
1970 static bool isFunctionDeclarationName(const FormatToken &Current,
1971                                       const AnnotatedLine &Line) {
1972   auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
1973     for (; Next; Next = Next->Next) {
1974       if (Next->is(TT_OverloadedOperatorLParen))
1975         return Next;
1976       if (Next->is(TT_OverloadedOperator))
1977         continue;
1978       if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
1979         // For 'new[]' and 'delete[]'.
1980         if (Next->Next && Next->Next->is(tok::l_square) && Next->Next->Next &&
1981             Next->Next->Next->is(tok::r_square))
1982           Next = Next->Next->Next;
1983         continue;
1984       }
1985 
1986       break;
1987     }
1988     return nullptr;
1989   };
1990 
1991   // Find parentheses of parameter list.
1992   const FormatToken *Next = Current.Next;
1993   if (Current.is(tok::kw_operator)) {
1994     if (Current.Previous && Current.Previous->is(tok::coloncolon))
1995       return false;
1996     Next = skipOperatorName(Next);
1997   } else {
1998     if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0)
1999       return false;
2000     for (; Next; Next = Next->Next) {
2001       if (Next->is(TT_TemplateOpener)) {
2002         Next = Next->MatchingParen;
2003       } else if (Next->is(tok::coloncolon)) {
2004         Next = Next->Next;
2005         if (!Next)
2006           return false;
2007         if (Next->is(tok::kw_operator)) {
2008           Next = skipOperatorName(Next->Next);
2009           break;
2010         }
2011         if (!Next->is(tok::identifier))
2012           return false;
2013       } else if (Next->is(tok::l_paren)) {
2014         break;
2015       } else {
2016         return false;
2017       }
2018     }
2019   }
2020 
2021   // Check whether parameter list can belong to a function declaration.
2022   if (!Next || !Next->is(tok::l_paren) || !Next->MatchingParen)
2023     return false;
2024   // If the lines ends with "{", this is likely an function definition.
2025   if (Line.Last->is(tok::l_brace))
2026     return true;
2027   if (Next->Next == Next->MatchingParen)
2028     return true; // Empty parentheses.
2029   // If there is an &/&& after the r_paren, this is likely a function.
2030   if (Next->MatchingParen->Next &&
2031       Next->MatchingParen->Next->is(TT_PointerOrReference))
2032     return true;
2033   for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
2034        Tok = Tok->Next) {
2035     if (Tok->is(tok::l_paren) && Tok->MatchingParen) {
2036       Tok = Tok->MatchingParen;
2037       continue;
2038     }
2039     if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
2040         Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis))
2041       return true;
2042     if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
2043         Tok->Tok.isLiteral())
2044       return false;
2045   }
2046   return false;
2047 }
2048 
2049 bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
2050   assert(Line.MightBeFunctionDecl);
2051 
2052   if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
2053        Style.AlwaysBreakAfterReturnType ==
2054            FormatStyle::RTBS_TopLevelDefinitions) &&
2055       Line.Level > 0)
2056     return false;
2057 
2058   switch (Style.AlwaysBreakAfterReturnType) {
2059   case FormatStyle::RTBS_None:
2060     return false;
2061   case FormatStyle::RTBS_All:
2062   case FormatStyle::RTBS_TopLevel:
2063     return true;
2064   case FormatStyle::RTBS_AllDefinitions:
2065   case FormatStyle::RTBS_TopLevelDefinitions:
2066     return Line.mightBeFunctionDefinition();
2067   }
2068 
2069   return false;
2070 }
2071 
2072 void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
2073   for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
2074                                                   E = Line.Children.end();
2075        I != E; ++I) {
2076     calculateFormattingInformation(**I);
2077   }
2078 
2079   Line.First->TotalLength =
2080       Line.First->IsMultiline ? Style.ColumnLimit
2081                               : Line.FirstStartColumn + Line.First->ColumnWidth;
2082   FormatToken *Current = Line.First->Next;
2083   bool InFunctionDecl = Line.MightBeFunctionDecl;
2084   while (Current) {
2085     if (isFunctionDeclarationName(*Current, Line))
2086       Current->Type = TT_FunctionDeclarationName;
2087     if (Current->is(TT_LineComment)) {
2088       if (Current->Previous->BlockKind == BK_BracedInit &&
2089           Current->Previous->opensScope())
2090         Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1;
2091       else
2092         Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
2093 
2094       // If we find a trailing comment, iterate backwards to determine whether
2095       // it seems to relate to a specific parameter. If so, break before that
2096       // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
2097       // to the previous line in:
2098       //   SomeFunction(a,
2099       //                b, // comment
2100       //                c);
2101       if (!Current->HasUnescapedNewline) {
2102         for (FormatToken *Parameter = Current->Previous; Parameter;
2103              Parameter = Parameter->Previous) {
2104           if (Parameter->isOneOf(tok::comment, tok::r_brace))
2105             break;
2106           if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
2107             if (!Parameter->Previous->is(TT_CtorInitializerComma) &&
2108                 Parameter->HasUnescapedNewline)
2109               Parameter->MustBreakBefore = true;
2110             break;
2111           }
2112         }
2113       }
2114     } else if (Current->SpacesRequiredBefore == 0 &&
2115                spaceRequiredBefore(Line, *Current)) {
2116       Current->SpacesRequiredBefore = 1;
2117     }
2118 
2119     Current->MustBreakBefore =
2120         Current->MustBreakBefore || mustBreakBefore(Line, *Current);
2121 
2122     if (!Current->MustBreakBefore && InFunctionDecl &&
2123         Current->is(TT_FunctionDeclarationName))
2124       Current->MustBreakBefore = mustBreakForReturnType(Line);
2125 
2126     Current->CanBreakBefore =
2127         Current->MustBreakBefore || canBreakBefore(Line, *Current);
2128     unsigned ChildSize = 0;
2129     if (Current->Previous->Children.size() == 1) {
2130       FormatToken &LastOfChild = *Current->Previous->Children[0]->Last;
2131       ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
2132                                                   : LastOfChild.TotalLength + 1;
2133     }
2134     const FormatToken *Prev = Current->Previous;
2135     if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
2136         (Prev->Children.size() == 1 &&
2137          Prev->Children[0]->First->MustBreakBefore) ||
2138         Current->IsMultiline)
2139       Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
2140     else
2141       Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
2142                              ChildSize + Current->SpacesRequiredBefore;
2143 
2144     if (Current->is(TT_CtorInitializerColon))
2145       InFunctionDecl = false;
2146 
2147     // FIXME: Only calculate this if CanBreakBefore is true once static
2148     // initializers etc. are sorted out.
2149     // FIXME: Move magic numbers to a better place.
2150 
2151     // Reduce penalty for aligning ObjC method arguments using the colon
2152     // alignment as this is the canonical way (still prefer fitting everything
2153     // into one line if possible). Trying to fit a whole expression into one
2154     // line should not force other line breaks (e.g. when ObjC method
2155     // expression is a part of other expression).
2156     Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
2157     if (Style.Language == FormatStyle::LK_ObjC &&
2158         Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
2159       if (Current->ParameterIndex == 1)
2160         Current->SplitPenalty += 5 * Current->BindingStrength;
2161     } else {
2162       Current->SplitPenalty += 20 * Current->BindingStrength;
2163     }
2164 
2165     Current = Current->Next;
2166   }
2167 
2168   calculateUnbreakableTailLengths(Line);
2169   unsigned IndentLevel = Line.Level;
2170   for (Current = Line.First; Current != nullptr; Current = Current->Next) {
2171     if (Current->Role)
2172       Current->Role->precomputeFormattingInfos(Current);
2173     if (Current->MatchingParen &&
2174         Current->MatchingParen->opensBlockOrBlockTypeList(Style)) {
2175       assert(IndentLevel > 0);
2176       --IndentLevel;
2177     }
2178     Current->IndentLevel = IndentLevel;
2179     if (Current->opensBlockOrBlockTypeList(Style))
2180       ++IndentLevel;
2181   }
2182 
2183   LLVM_DEBUG({ printDebugInfo(Line); });
2184 }
2185 
2186 void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
2187   unsigned UnbreakableTailLength = 0;
2188   FormatToken *Current = Line.Last;
2189   while (Current) {
2190     Current->UnbreakableTailLength = UnbreakableTailLength;
2191     if (Current->CanBreakBefore ||
2192         Current->isOneOf(tok::comment, tok::string_literal)) {
2193       UnbreakableTailLength = 0;
2194     } else {
2195       UnbreakableTailLength +=
2196           Current->ColumnWidth + Current->SpacesRequiredBefore;
2197     }
2198     Current = Current->Previous;
2199   }
2200 }
2201 
2202 unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
2203                                       const FormatToken &Tok,
2204                                       bool InFunctionDecl) {
2205   const FormatToken &Left = *Tok.Previous;
2206   const FormatToken &Right = Tok;
2207 
2208   if (Left.is(tok::semi))
2209     return 0;
2210 
2211   if (Style.Language == FormatStyle::LK_Java) {
2212     if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
2213       return 1;
2214     if (Right.is(Keywords.kw_implements))
2215       return 2;
2216     if (Left.is(tok::comma) && Left.NestingLevel == 0)
2217       return 3;
2218   } else if (Style.Language == FormatStyle::LK_JavaScript) {
2219     if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
2220       return 100;
2221     if (Left.is(TT_JsTypeColon))
2222       return 35;
2223     if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
2224         (Right.is(TT_TemplateString) && Right.TokenText.startswith("}")))
2225       return 100;
2226     // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
2227     if (Left.opensScope() && Right.closesScope())
2228       return 200;
2229   }
2230 
2231   if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
2232     return 1;
2233   if (Right.is(tok::l_square)) {
2234     if (Style.Language == FormatStyle::LK_Proto)
2235       return 1;
2236     if (Left.is(tok::r_square))
2237       return 200;
2238     // Slightly prefer formatting local lambda definitions like functions.
2239     if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
2240       return 35;
2241     if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
2242                        TT_ArrayInitializerLSquare,
2243                        TT_DesignatedInitializerLSquare, TT_AttributeSquare))
2244       return 500;
2245   }
2246 
2247   if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
2248       Right.is(tok::kw_operator)) {
2249     if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
2250       return 3;
2251     if (Left.is(TT_StartOfName))
2252       return 110;
2253     if (InFunctionDecl && Right.NestingLevel == 0)
2254       return Style.PenaltyReturnTypeOnItsOwnLine;
2255     return 200;
2256   }
2257   if (Right.is(TT_PointerOrReference))
2258     return 190;
2259   if (Right.is(TT_LambdaArrow))
2260     return 110;
2261   if (Left.is(tok::equal) && Right.is(tok::l_brace))
2262     return 160;
2263   if (Left.is(TT_CastRParen))
2264     return 100;
2265   if (Left.is(tok::coloncolon) ||
2266       (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
2267     return 500;
2268   if (Left.isOneOf(tok::kw_class, tok::kw_struct))
2269     return 5000;
2270   if (Left.is(tok::comment))
2271     return 1000;
2272 
2273   if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
2274                    TT_CtorInitializerColon))
2275     return 2;
2276 
2277   if (Right.isMemberAccess()) {
2278     // Breaking before the "./->" of a chained call/member access is reasonably
2279     // cheap, as formatting those with one call per line is generally
2280     // desirable. In particular, it should be cheaper to break before the call
2281     // than it is to break inside a call's parameters, which could lead to weird
2282     // "hanging" indents. The exception is the very last "./->" to support this
2283     // frequent pattern:
2284     //
2285     //   aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
2286     //       dddddddd);
2287     //
2288     // which might otherwise be blown up onto many lines. Here, clang-format
2289     // won't produce "hanging" indents anyway as there is no other trailing
2290     // call.
2291     //
2292     // Also apply higher penalty is not a call as that might lead to a wrapping
2293     // like:
2294     //
2295     //   aaaaaaa
2296     //       .aaaaaaaaa.bbbbbbbb(cccccccc);
2297     return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
2298                ? 150
2299                : 35;
2300   }
2301 
2302   if (Right.is(TT_TrailingAnnotation) &&
2303       (!Right.Next || Right.Next->isNot(tok::l_paren))) {
2304     // Moving trailing annotations to the next line is fine for ObjC method
2305     // declarations.
2306     if (Line.startsWith(TT_ObjCMethodSpecifier))
2307       return 10;
2308     // Generally, breaking before a trailing annotation is bad unless it is
2309     // function-like. It seems to be especially preferable to keep standard
2310     // annotations (i.e. "const", "final" and "override") on the same line.
2311     // Use a slightly higher penalty after ")" so that annotations like
2312     // "const override" are kept together.
2313     bool is_short_annotation = Right.TokenText.size() < 10;
2314     return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
2315   }
2316 
2317   // In for-loops, prefer breaking at ',' and ';'.
2318   if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
2319     return 4;
2320 
2321   // In Objective-C method expressions, prefer breaking before "param:" over
2322   // breaking after it.
2323   if (Right.is(TT_SelectorName))
2324     return 0;
2325   if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
2326     return Line.MightBeFunctionDecl ? 50 : 500;
2327 
2328   // In Objective-C type declarations, avoid breaking after the category's
2329   // open paren (we'll prefer breaking after the protocol list's opening
2330   // angle bracket, if present).
2331   if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous &&
2332       Left.Previous->isOneOf(tok::identifier, tok::greater))
2333     return 500;
2334 
2335   if (Left.is(tok::l_paren) && InFunctionDecl &&
2336       Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign)
2337     return 100;
2338   if (Left.is(tok::l_paren) && Left.Previous &&
2339       (Left.Previous->isOneOf(tok::kw_if, tok::kw_for) ||
2340        Left.Previous->endsSequence(tok::kw_constexpr, tok::kw_if)))
2341     return 1000;
2342   if (Left.is(tok::equal) && InFunctionDecl)
2343     return 110;
2344   if (Right.is(tok::r_brace))
2345     return 1;
2346   if (Left.is(TT_TemplateOpener))
2347     return 100;
2348   if (Left.opensScope()) {
2349     if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign)
2350       return 0;
2351     if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
2352       return 19;
2353     return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
2354                                    : 19;
2355   }
2356   if (Left.is(TT_JavaAnnotation))
2357     return 50;
2358 
2359   if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
2360       Left.Previous->isLabelString() &&
2361       (Left.NextOperator || Left.OperatorIndex != 0))
2362     return 50;
2363   if (Right.is(tok::plus) && Left.isLabelString() &&
2364       (Right.NextOperator || Right.OperatorIndex != 0))
2365     return 25;
2366   if (Left.is(tok::comma))
2367     return 1;
2368   if (Right.is(tok::lessless) && Left.isLabelString() &&
2369       (Right.NextOperator || Right.OperatorIndex != 1))
2370     return 25;
2371   if (Right.is(tok::lessless)) {
2372     // Breaking at a << is really cheap.
2373     if (!Left.is(tok::r_paren) || Right.OperatorIndex > 0)
2374       // Slightly prefer to break before the first one in log-like statements.
2375       return 2;
2376     return 1;
2377   }
2378   if (Left.ClosesTemplateDeclaration)
2379     return Style.PenaltyBreakTemplateDeclaration;
2380   if (Left.is(TT_ConditionalExpr))
2381     return prec::Conditional;
2382   prec::Level Level = Left.getPrecedence();
2383   if (Level == prec::Unknown)
2384     Level = Right.getPrecedence();
2385   if (Level == prec::Assignment)
2386     return Style.PenaltyBreakAssignment;
2387   if (Level != prec::Unknown)
2388     return Level;
2389 
2390   return 3;
2391 }
2392 
2393 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
2394                                           const FormatToken &Left,
2395                                           const FormatToken &Right) {
2396   if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
2397     return true;
2398   if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java)
2399     return true;
2400   if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
2401       Left.Tok.getObjCKeywordID() == tok::objc_property)
2402     return true;
2403   if (Right.is(tok::hashhash))
2404     return Left.is(tok::hash);
2405   if (Left.isOneOf(tok::hashhash, tok::hash))
2406     return Right.is(tok::hash);
2407   if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
2408     return Style.SpaceInEmptyParentheses;
2409   if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
2410     return (Right.is(TT_CastRParen) ||
2411             (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
2412                ? Style.SpacesInCStyleCastParentheses
2413                : Style.SpacesInParentheses;
2414   if (Right.isOneOf(tok::semi, tok::comma))
2415     return false;
2416   if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) {
2417     bool IsLightweightGeneric =
2418         Right.MatchingParen && Right.MatchingParen->Next &&
2419         Right.MatchingParen->Next->is(tok::colon);
2420     return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
2421   }
2422   if (Right.is(tok::less) && Left.is(tok::kw_template))
2423     return Style.SpaceAfterTemplateKeyword;
2424   if (Left.isOneOf(tok::exclaim, tok::tilde))
2425     return false;
2426   if (Left.is(tok::at) &&
2427       Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
2428                     tok::numeric_constant, tok::l_paren, tok::l_brace,
2429                     tok::kw_true, tok::kw_false))
2430     return false;
2431   if (Left.is(tok::colon))
2432     return !Left.is(TT_ObjCMethodExpr);
2433   if (Left.is(tok::coloncolon))
2434     return false;
2435   if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
2436     if (Style.Language == FormatStyle::LK_TextProto ||
2437         (Style.Language == FormatStyle::LK_Proto &&
2438          (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
2439       // Format empty list as `<>`.
2440       if (Left.is(tok::less) && Right.is(tok::greater))
2441         return false;
2442       return !Style.Cpp11BracedListStyle;
2443     }
2444     return false;
2445   }
2446   if (Right.is(tok::ellipsis))
2447     return Left.Tok.isLiteral() || (Left.is(tok::identifier) && Left.Previous &&
2448                                     Left.Previous->is(tok::kw_case));
2449   if (Left.is(tok::l_square) && Right.is(tok::amp))
2450     return false;
2451   if (Right.is(TT_PointerOrReference)) {
2452     if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) {
2453       if (!Left.MatchingParen)
2454         return true;
2455       FormatToken *TokenBeforeMatchingParen =
2456           Left.MatchingParen->getPreviousNonComment();
2457       if (!TokenBeforeMatchingParen ||
2458           !TokenBeforeMatchingParen->isOneOf(tok::kw_typeof, tok::kw_decltype))
2459         return true;
2460     }
2461     return (Left.Tok.isLiteral() ||
2462             (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
2463              (Style.PointerAlignment != FormatStyle::PAS_Left ||
2464               (Line.IsMultiVariableDeclStmt &&
2465                (Left.NestingLevel == 0 ||
2466                 (Left.NestingLevel == 1 && Line.First->is(tok::kw_for)))))));
2467   }
2468   if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
2469       (!Left.is(TT_PointerOrReference) ||
2470        (Style.PointerAlignment != FormatStyle::PAS_Right &&
2471         !Line.IsMultiVariableDeclStmt)))
2472     return true;
2473   if (Left.is(TT_PointerOrReference))
2474     return Right.Tok.isLiteral() || Right.is(TT_BlockComment) ||
2475            (Right.isOneOf(Keywords.kw_override, Keywords.kw_final) &&
2476             !Right.is(TT_StartOfName)) ||
2477            (Right.is(tok::l_brace) && Right.BlockKind == BK_Block) ||
2478            (!Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
2479                            tok::l_paren) &&
2480             (Style.PointerAlignment != FormatStyle::PAS_Right &&
2481              !Line.IsMultiVariableDeclStmt) &&
2482             Left.Previous &&
2483             !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
2484   if (Right.is(tok::star) && Left.is(tok::l_paren))
2485     return false;
2486   const auto SpaceRequiredForArrayInitializerLSquare =
2487       [](const FormatToken &LSquareTok, const FormatStyle &Style) {
2488         return Style.SpacesInContainerLiterals ||
2489                ((Style.Language == FormatStyle::LK_Proto ||
2490                  Style.Language == FormatStyle::LK_TextProto) &&
2491                 !Style.Cpp11BracedListStyle &&
2492                 LSquareTok.endsSequence(tok::l_square, tok::colon,
2493                                         TT_SelectorName));
2494       };
2495   if (Left.is(tok::l_square))
2496     return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
2497             SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
2498            (Left.isOneOf(TT_ArraySubscriptLSquare,
2499                          TT_StructuredBindingLSquare) &&
2500             Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
2501   if (Right.is(tok::r_square))
2502     return Right.MatchingParen &&
2503            ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
2504              SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
2505                                                      Style)) ||
2506             (Style.SpacesInSquareBrackets &&
2507              Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
2508                                           TT_StructuredBindingLSquare)) ||
2509             Right.MatchingParen->is(TT_AttributeParen));
2510   if (Right.is(tok::l_square) &&
2511       !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
2512                      TT_DesignatedInitializerLSquare,
2513                      TT_StructuredBindingLSquare, TT_AttributeSquare) &&
2514       !Left.isOneOf(tok::numeric_constant, TT_DictLiteral))
2515     return false;
2516   if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
2517     return !Left.Children.empty(); // No spaces in "{}".
2518   if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) ||
2519       (Right.is(tok::r_brace) && Right.MatchingParen &&
2520        Right.MatchingParen->BlockKind != BK_Block))
2521     return !Style.Cpp11BracedListStyle;
2522   if (Left.is(TT_BlockComment))
2523     // No whitespace in x(/*foo=*/1), except for JavaScript.
2524     return Style.Language == FormatStyle::LK_JavaScript ||
2525            !Left.TokenText.endswith("=*/");
2526   if (Right.is(tok::l_paren)) {
2527     if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) ||
2528         (Left.is(tok::r_square) && Left.is(TT_AttributeSquare)))
2529       return true;
2530     return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
2531            (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
2532             (Left.isOneOf(tok::kw_if, tok::pp_elif, tok::kw_for, tok::kw_while,
2533                           tok::kw_switch, tok::kw_case, TT_ForEachMacro,
2534                           TT_ObjCForIn) ||
2535              Left.endsSequence(tok::kw_constexpr, tok::kw_if) ||
2536              (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch,
2537                            tok::kw_new, tok::kw_delete) &&
2538               (!Left.Previous || Left.Previous->isNot(tok::period))))) ||
2539            (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
2540             (Left.is(tok::identifier) || Left.isFunctionLikeKeyword() ||
2541              Left.is(tok::r_paren)) &&
2542             Line.Type != LT_PreprocessorDirective);
2543   }
2544   if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
2545     return false;
2546   if (Right.is(TT_UnaryOperator))
2547     return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
2548            (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
2549   if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
2550                     tok::r_paren) ||
2551        Left.isSimpleTypeSpecifier()) &&
2552       Right.is(tok::l_brace) && Right.getNextNonComment() &&
2553       Right.BlockKind != BK_Block)
2554     return false;
2555   if (Left.is(tok::period) || Right.is(tok::period))
2556     return false;
2557   if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
2558     return false;
2559   if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
2560       Left.MatchingParen->Previous &&
2561       (Left.MatchingParen->Previous->is(tok::period) ||
2562        Left.MatchingParen->Previous->is(tok::coloncolon)))
2563     // Java call to generic function with explicit type:
2564     // A.<B<C<...>>>DoSomething();
2565     // A::<B<C<...>>>DoSomething();  // With a Java 8 method reference.
2566     return false;
2567   if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
2568     return false;
2569   if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at))
2570     // Objective-C dictionary literal -> no space after opening brace.
2571     return false;
2572   if (Right.is(tok::r_brace) && Right.MatchingParen &&
2573       Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at))
2574     // Objective-C dictionary literal -> no space before closing brace.
2575     return false;
2576   return true;
2577 }
2578 
2579 bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
2580                                          const FormatToken &Right) {
2581   const FormatToken &Left = *Right.Previous;
2582   if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
2583     return true; // Never ever merge two identifiers.
2584   if (Style.isCpp()) {
2585     if (Left.is(tok::kw_operator))
2586       return Right.is(tok::coloncolon);
2587     if (Right.is(tok::l_brace) && Right.BlockKind == BK_BracedInit &&
2588         !Left.opensScope() && Style.SpaceBeforeCpp11BracedList)
2589       return true;
2590   } else if (Style.Language == FormatStyle::LK_Proto ||
2591              Style.Language == FormatStyle::LK_TextProto) {
2592     if (Right.is(tok::period) &&
2593         Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
2594                      Keywords.kw_repeated, Keywords.kw_extend))
2595       return true;
2596     if (Right.is(tok::l_paren) &&
2597         Left.isOneOf(Keywords.kw_returns, Keywords.kw_option))
2598       return true;
2599     if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
2600       return true;
2601     // Slashes occur in text protocol extension syntax: [type/type] { ... }.
2602     if (Left.is(tok::slash) || Right.is(tok::slash))
2603       return false;
2604     if (Left.MatchingParen && Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
2605         Right.isOneOf(tok::l_brace, tok::less))
2606       return !Style.Cpp11BracedListStyle;
2607     // A percent is probably part of a formatting specification, such as %lld.
2608     if (Left.is(tok::percent))
2609       return false;
2610     // Preserve the existence of a space before a percent for cases like 0x%04x
2611     // and "%d %d"
2612     if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
2613       return Right.WhitespaceRange.getEnd() != Right.WhitespaceRange.getBegin();
2614   } else if (Style.Language == FormatStyle::LK_JavaScript) {
2615     if (Left.is(TT_JsFatArrow))
2616       return true;
2617     // for await ( ...
2618     if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
2619         Left.Previous->is(tok::kw_for))
2620       return true;
2621     if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
2622         Right.MatchingParen) {
2623       const FormatToken *Next = Right.MatchingParen->getNextNonComment();
2624       // An async arrow function, for example: `x = async () => foo();`,
2625       // as opposed to calling a function called async: `x = async();`
2626       if (Next && Next->is(TT_JsFatArrow))
2627         return true;
2628     }
2629     if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
2630         (Right.is(TT_TemplateString) && Right.TokenText.startswith("}")))
2631       return false;
2632     // In tagged template literals ("html`bar baz`"), there is no space between
2633     // the tag identifier and the template string. getIdentifierInfo makes sure
2634     // that the identifier is not a pseudo keyword like `yield`, either.
2635     if (Left.is(tok::identifier) && Keywords.IsJavaScriptIdentifier(Left) &&
2636         Right.is(TT_TemplateString))
2637       return false;
2638     if (Right.is(tok::star) &&
2639         Left.isOneOf(Keywords.kw_function, Keywords.kw_yield))
2640       return false;
2641     if (Right.isOneOf(tok::l_brace, tok::l_square) &&
2642         Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
2643                      Keywords.kw_extends, Keywords.kw_implements))
2644       return true;
2645     if (Right.is(tok::l_paren)) {
2646       // JS methods can use some keywords as names (e.g. `delete()`).
2647       if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
2648         return false;
2649       // Valid JS method names can include keywords, e.g. `foo.delete()` or
2650       // `bar.instanceof()`. Recognize call positions by preceding period.
2651       if (Left.Previous && Left.Previous->is(tok::period) &&
2652           Left.Tok.getIdentifierInfo())
2653         return false;
2654       // Additional unary JavaScript operators that need a space after.
2655       if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
2656                        tok::kw_void))
2657         return true;
2658     }
2659     if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
2660                       tok::kw_const) ||
2661          // "of" is only a keyword if it appears after another identifier
2662          // (e.g. as "const x of y" in a for loop), or after a destructuring
2663          // operation (const [x, y] of z, const {a, b} of c).
2664          (Left.is(Keywords.kw_of) && Left.Previous &&
2665           (Left.Previous->Tok.is(tok::identifier) ||
2666            Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
2667         (!Left.Previous || !Left.Previous->is(tok::period)))
2668       return true;
2669     if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
2670         Left.Previous->is(tok::period) && Right.is(tok::l_paren))
2671       return false;
2672     if (Left.is(Keywords.kw_as) &&
2673         Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren))
2674       return true;
2675     if (Left.is(tok::kw_default) && Left.Previous &&
2676         Left.Previous->is(tok::kw_export))
2677       return true;
2678     if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
2679       return true;
2680     if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
2681       return false;
2682     if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
2683       return false;
2684     if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
2685         Line.First->isOneOf(Keywords.kw_import, tok::kw_export))
2686       return false;
2687     if (Left.is(tok::ellipsis))
2688       return false;
2689     if (Left.is(TT_TemplateCloser) &&
2690         !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
2691                        Keywords.kw_implements, Keywords.kw_extends))
2692       // Type assertions ('<type>expr') are not followed by whitespace. Other
2693       // locations that should have whitespace following are identified by the
2694       // above set of follower tokens.
2695       return false;
2696     if (Right.is(TT_JsNonNullAssertion))
2697       return false;
2698     if (Left.is(TT_JsNonNullAssertion) &&
2699         Right.isOneOf(Keywords.kw_as, Keywords.kw_in))
2700       return true; // "x! as string", "x! in y"
2701   } else if (Style.Language == FormatStyle::LK_Java) {
2702     if (Left.is(tok::r_square) && Right.is(tok::l_brace))
2703       return true;
2704     if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
2705       return Style.SpaceBeforeParens != FormatStyle::SBPO_Never;
2706     if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
2707                       tok::kw_protected) ||
2708          Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
2709                       Keywords.kw_native)) &&
2710         Right.is(TT_TemplateOpener))
2711       return true;
2712   }
2713   if (Left.is(TT_ImplicitStringLiteral))
2714     return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
2715   if (Line.Type == LT_ObjCMethodDecl) {
2716     if (Left.is(TT_ObjCMethodSpecifier))
2717       return true;
2718     if (Left.is(tok::r_paren) && canBeObjCSelectorComponent(Right))
2719       // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a
2720       // keyword in Objective-C, and '+ (instancetype)new;' is a standard class
2721       // method declaration.
2722       return false;
2723   }
2724   if (Line.Type == LT_ObjCProperty &&
2725       (Right.is(tok::equal) || Left.is(tok::equal)))
2726     return false;
2727 
2728   if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
2729       Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow))
2730     return true;
2731   if (Right.is(TT_OverloadedOperatorLParen))
2732     return Style.SpaceBeforeParens == FormatStyle::SBPO_Always;
2733   if (Left.is(tok::comma))
2734     return true;
2735   if (Right.is(tok::comma))
2736     return false;
2737   if (Right.is(TT_ObjCBlockLParen))
2738     return true;
2739   if (Right.is(TT_CtorInitializerColon))
2740     return Style.SpaceBeforeCtorInitializerColon;
2741   if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
2742     return false;
2743   if (Right.is(TT_RangeBasedForLoopColon) &&
2744       !Style.SpaceBeforeRangeBasedForLoopColon)
2745     return false;
2746   if (Right.is(tok::colon)) {
2747     if (Line.First->isOneOf(tok::kw_case, tok::kw_default) ||
2748         !Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi))
2749       return false;
2750     if (Right.is(TT_ObjCMethodExpr))
2751       return false;
2752     if (Left.is(tok::question))
2753       return false;
2754     if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
2755       return false;
2756     if (Right.is(TT_DictLiteral))
2757       return Style.SpacesInContainerLiterals;
2758     if (Right.is(TT_AttributeColon))
2759       return false;
2760     return true;
2761   }
2762   if (Left.is(TT_UnaryOperator))
2763     return Right.is(TT_BinaryOperator);
2764 
2765   // If the next token is a binary operator or a selector name, we have
2766   // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
2767   if (Left.is(TT_CastRParen))
2768     return Style.SpaceAfterCStyleCast ||
2769            Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
2770 
2771   if (Left.is(tok::greater) && Right.is(tok::greater)) {
2772     if (Style.Language == FormatStyle::LK_TextProto ||
2773         (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral)))
2774       return !Style.Cpp11BracedListStyle;
2775     return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
2776            (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
2777   }
2778   if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
2779       Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
2780       (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod)))
2781     return false;
2782   if (!Style.SpaceBeforeAssignmentOperators &&
2783       Right.getPrecedence() == prec::Assignment)
2784     return false;
2785   if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) &&
2786       (Left.is(tok::identifier) || Left.is(tok::kw_this)))
2787     return false;
2788   if (Right.is(tok::coloncolon) && Left.is(tok::identifier))
2789     // Generally don't remove existing spaces between an identifier and "::".
2790     // The identifier might actually be a macro name such as ALWAYS_INLINE. If
2791     // this turns out to be too lenient, add analysis of the identifier itself.
2792     return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
2793   if (Right.is(tok::coloncolon) && !Left.isOneOf(tok::l_brace, tok::comment))
2794     return (Left.is(TT_TemplateOpener) &&
2795             Style.Standard == FormatStyle::LS_Cpp03) ||
2796            !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
2797                           tok::kw___super, TT_TemplateCloser,
2798                           TT_TemplateOpener)) ||
2799            (Left.is(tok ::l_paren) && Style.SpacesInParentheses);
2800   if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
2801     return Style.SpacesInAngles;
2802   // Space before TT_StructuredBindingLSquare.
2803   if (Right.is(TT_StructuredBindingLSquare))
2804     return !Left.isOneOf(tok::amp, tok::ampamp) ||
2805            Style.PointerAlignment != FormatStyle::PAS_Right;
2806   // Space before & or && following a TT_StructuredBindingLSquare.
2807   if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) &&
2808       Right.isOneOf(tok::amp, tok::ampamp))
2809     return Style.PointerAlignment != FormatStyle::PAS_Left;
2810   if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) ||
2811       (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
2812        !Right.is(tok::r_paren)))
2813     return true;
2814   if (Left.is(TT_TemplateCloser) && Right.is(tok::l_paren) &&
2815       Right.isNot(TT_FunctionTypeLParen))
2816     return Style.SpaceBeforeParens == FormatStyle::SBPO_Always;
2817   if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
2818       Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen))
2819     return false;
2820   if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
2821       Line.startsWith(tok::hash))
2822     return true;
2823   if (Right.is(TT_TrailingUnaryOperator))
2824     return false;
2825   if (Left.is(TT_RegexLiteral))
2826     return false;
2827   return spaceRequiredBetween(Line, Left, Right);
2828 }
2829 
2830 // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
2831 static bool isAllmanBrace(const FormatToken &Tok) {
2832   return Tok.is(tok::l_brace) && Tok.BlockKind == BK_Block &&
2833          !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
2834 }
2835 
2836 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
2837                                      const FormatToken &Right) {
2838   const FormatToken &Left = *Right.Previous;
2839   if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
2840     return true;
2841 
2842   if (Style.Language == FormatStyle::LK_JavaScript) {
2843     // FIXME: This might apply to other languages and token kinds.
2844     if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous &&
2845         Left.Previous->is(tok::string_literal))
2846       return true;
2847     if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
2848         Left.Previous && Left.Previous->is(tok::equal) &&
2849         Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
2850                             tok::kw_const) &&
2851         // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
2852         // above.
2853         !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let))
2854       // Object literals on the top level of a file are treated as "enum-style".
2855       // Each key/value pair is put on a separate line, instead of bin-packing.
2856       return true;
2857     if (Left.is(tok::l_brace) && Line.Level == 0 &&
2858         (Line.startsWith(tok::kw_enum) ||
2859          Line.startsWith(tok::kw_const, tok::kw_enum) ||
2860          Line.startsWith(tok::kw_export, tok::kw_enum) ||
2861          Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum)))
2862       // JavaScript top-level enum key/value pairs are put on separate lines
2863       // instead of bin-packing.
2864       return true;
2865     if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
2866         !Left.Children.empty())
2867       // Support AllowShortFunctionsOnASingleLine for JavaScript.
2868       return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
2869              Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
2870              (Left.NestingLevel == 0 && Line.Level == 0 &&
2871               Style.AllowShortFunctionsOnASingleLine &
2872                   FormatStyle::SFS_InlineOnly);
2873   } else if (Style.Language == FormatStyle::LK_Java) {
2874     if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
2875         Right.Next->is(tok::string_literal))
2876       return true;
2877   } else if (Style.Language == FormatStyle::LK_Cpp ||
2878              Style.Language == FormatStyle::LK_ObjC ||
2879              Style.Language == FormatStyle::LK_Proto ||
2880              Style.Language == FormatStyle::LK_TableGen ||
2881              Style.Language == FormatStyle::LK_TextProto) {
2882     if (Left.isStringLiteral() && Right.isStringLiteral())
2883       return true;
2884   }
2885 
2886   // If the last token before a '}', ']', or ')' is a comma or a trailing
2887   // comment, the intention is to insert a line break after it in order to make
2888   // shuffling around entries easier. Import statements, especially in
2889   // JavaScript, can be an exception to this rule.
2890   if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) {
2891     const FormatToken *BeforeClosingBrace = nullptr;
2892     if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
2893          (Style.Language == FormatStyle::LK_JavaScript &&
2894           Left.is(tok::l_paren))) &&
2895         Left.BlockKind != BK_Block && Left.MatchingParen)
2896       BeforeClosingBrace = Left.MatchingParen->Previous;
2897     else if (Right.MatchingParen &&
2898              (Right.MatchingParen->isOneOf(tok::l_brace,
2899                                            TT_ArrayInitializerLSquare) ||
2900               (Style.Language == FormatStyle::LK_JavaScript &&
2901                Right.MatchingParen->is(tok::l_paren))))
2902       BeforeClosingBrace = &Left;
2903     if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
2904                                BeforeClosingBrace->isTrailingComment()))
2905       return true;
2906   }
2907 
2908   if (Right.is(tok::comment))
2909     return Left.BlockKind != BK_BracedInit &&
2910            Left.isNot(TT_CtorInitializerColon) &&
2911            (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
2912   if (Left.isTrailingComment())
2913     return true;
2914   if (Right.Previous->IsUnterminatedLiteral)
2915     return true;
2916   if (Right.is(tok::lessless) && Right.Next &&
2917       Right.Previous->is(tok::string_literal) &&
2918       Right.Next->is(tok::string_literal))
2919     return true;
2920   if (Right.Previous->ClosesTemplateDeclaration &&
2921       Right.Previous->MatchingParen &&
2922       Right.Previous->MatchingParen->NestingLevel == 0 &&
2923       Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes)
2924     return true;
2925   if (Right.is(TT_CtorInitializerComma) &&
2926       Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
2927       !Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
2928     return true;
2929   if (Right.is(TT_CtorInitializerColon) &&
2930       Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
2931       !Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
2932     return true;
2933   // Break only if we have multiple inheritance.
2934   if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
2935       Right.is(TT_InheritanceComma))
2936     return true;
2937   if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\""))
2938     // Multiline raw string literals are special wrt. line breaks. The author
2939     // has made a deliberate choice and might have aligned the contents of the
2940     // string literal accordingly. Thus, we try keep existing line breaks.
2941     return Right.IsMultiline && Right.NewlinesBefore > 0;
2942   if ((Right.Previous->is(tok::l_brace) ||
2943        (Right.Previous->is(tok::less) && Right.Previous->Previous &&
2944         Right.Previous->Previous->is(tok::equal))) &&
2945       Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
2946     // Don't put enums or option definitions onto single lines in protocol
2947     // buffers.
2948     return true;
2949   }
2950   if (Right.is(TT_InlineASMBrace))
2951     return Right.HasUnescapedNewline;
2952   if (isAllmanBrace(Left) || isAllmanBrace(Right))
2953     return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
2954            (Line.startsWith(tok::kw_typedef, tok::kw_enum) &&
2955             Style.BraceWrapping.AfterEnum) ||
2956            (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
2957            (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
2958   if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine)
2959     return true;
2960 
2961   if ((Style.Language == FormatStyle::LK_Java ||
2962        Style.Language == FormatStyle::LK_JavaScript) &&
2963       Left.is(TT_LeadingJavaAnnotation) &&
2964       Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
2965       (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations))
2966     return true;
2967 
2968   if (Right.is(TT_ProtoExtensionLSquare))
2969     return true;
2970 
2971   // In text proto instances if a submessage contains at least 2 entries and at
2972   // least one of them is a submessage, like A { ... B { ... } ... },
2973   // put all of the entries of A on separate lines by forcing the selector of
2974   // the submessage B to be put on a newline.
2975   //
2976   // Example: these can stay on one line:
2977   // a { scalar_1: 1 scalar_2: 2 }
2978   // a { b { key: value } }
2979   //
2980   // and these entries need to be on a new line even if putting them all in one
2981   // line is under the column limit:
2982   // a {
2983   //   scalar: 1
2984   //   b { key: value }
2985   // }
2986   //
2987   // We enforce this by breaking before a submessage field that has previous
2988   // siblings, *and* breaking before a field that follows a submessage field.
2989   //
2990   // Be careful to exclude the case  [proto.ext] { ... } since the `]` is
2991   // the TT_SelectorName there, but we don't want to break inside the brackets.
2992   //
2993   // Another edge case is @submessage { key: value }, which is a common
2994   // substitution placeholder. In this case we want to keep `@` and `submessage`
2995   // together.
2996   //
2997   // We ensure elsewhere that extensions are always on their own line.
2998   if ((Style.Language == FormatStyle::LK_Proto ||
2999        Style.Language == FormatStyle::LK_TextProto) &&
3000       Right.is(TT_SelectorName) && !Right.is(tok::r_square) && Right.Next) {
3001     // Keep `@submessage` together in:
3002     // @submessage { key: value }
3003     if (Right.Previous && Right.Previous->is(tok::at))
3004       return false;
3005     // Look for the scope opener after selector in cases like:
3006     // selector { ...
3007     // selector: { ...
3008     // selector: @base { ...
3009     FormatToken *LBrace = Right.Next;
3010     if (LBrace && LBrace->is(tok::colon)) {
3011       LBrace = LBrace->Next;
3012       if (LBrace && LBrace->is(tok::at)) {
3013         LBrace = LBrace->Next;
3014         if (LBrace)
3015           LBrace = LBrace->Next;
3016       }
3017     }
3018     if (LBrace &&
3019         // The scope opener is one of {, [, <:
3020         // selector { ... }
3021         // selector [ ... ]
3022         // selector < ... >
3023         //
3024         // In case of selector { ... }, the l_brace is TT_DictLiteral.
3025         // In case of an empty selector {}, the l_brace is not TT_DictLiteral,
3026         // so we check for immediately following r_brace.
3027         ((LBrace->is(tok::l_brace) &&
3028           (LBrace->is(TT_DictLiteral) ||
3029            (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
3030          LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
3031       // If Left.ParameterCount is 0, then this submessage entry is not the
3032       // first in its parent submessage, and we want to break before this entry.
3033       // If Left.ParameterCount is greater than 0, then its parent submessage
3034       // might contain 1 or more entries and we want to break before this entry
3035       // if it contains at least 2 entries. We deal with this case later by
3036       // detecting and breaking before the next entry in the parent submessage.
3037       if (Left.ParameterCount == 0)
3038         return true;
3039       // However, if this submessage is the first entry in its parent
3040       // submessage, Left.ParameterCount might be 1 in some cases.
3041       // We deal with this case later by detecting an entry
3042       // following a closing paren of this submessage.
3043     }
3044 
3045     // If this is an entry immediately following a submessage, it will be
3046     // preceded by a closing paren of that submessage, like in:
3047     //     left---.  .---right
3048     //            v  v
3049     // sub: { ... } key: value
3050     // If there was a comment between `}` an `key` above, then `key` would be
3051     // put on a new line anyways.
3052     if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
3053       return true;
3054   }
3055 
3056   // Deal with lambda arguments in C++ - we want consistent line breaks whether
3057   // they happen to be at arg0, arg1 or argN. The selection is a bit nuanced
3058   // as aggressive line breaks are placed when the lambda is not the last arg.
3059   if ((Style.Language == FormatStyle::LK_Cpp ||
3060        Style.Language == FormatStyle::LK_ObjC) &&
3061       Left.is(tok::l_paren) && Left.BlockParameterCount > 0 &&
3062       !Right.isOneOf(tok::l_paren, TT_LambdaLSquare)) {
3063     // Multiple lambdas in the same function call force line breaks.
3064     if (Left.BlockParameterCount > 1)
3065       return true;
3066 
3067     // A lambda followed by another arg forces a line break.
3068     if (!Left.Role)
3069       return false;
3070     auto Comma = Left.Role->lastComma();
3071     if (!Comma)
3072       return false;
3073     auto Next = Comma->getNextNonComment();
3074     if (!Next)
3075       return false;
3076     if (!Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret))
3077       return true;
3078   }
3079 
3080   return false;
3081 }
3082 
3083 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
3084                                     const FormatToken &Right) {
3085   const FormatToken &Left = *Right.Previous;
3086 
3087   // Language-specific stuff.
3088   if (Style.Language == FormatStyle::LK_Java) {
3089     if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
3090                      Keywords.kw_implements))
3091       return false;
3092     if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
3093                       Keywords.kw_implements))
3094       return true;
3095   } else if (Style.Language == FormatStyle::LK_JavaScript) {
3096     const FormatToken *NonComment = Right.getPreviousNonComment();
3097     if (NonComment &&
3098         NonComment->isOneOf(
3099             tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
3100             tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
3101             tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
3102             Keywords.kw_readonly, Keywords.kw_abstract, Keywords.kw_get,
3103             Keywords.kw_set, Keywords.kw_async, Keywords.kw_await))
3104       return false; // Otherwise automatic semicolon insertion would trigger.
3105     if (Right.NestingLevel == 0 &&
3106         (Left.Tok.getIdentifierInfo() ||
3107          Left.isOneOf(tok::r_square, tok::r_paren)) &&
3108         Right.isOneOf(tok::l_square, tok::l_paren))
3109       return false; // Otherwise automatic semicolon insertion would trigger.
3110     if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace))
3111       return false;
3112     if (Left.is(TT_JsTypeColon))
3113       return true;
3114     // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
3115     if (Left.is(tok::exclaim) && Right.is(tok::colon))
3116       return false;
3117     // Look for is type annotations like:
3118     // function f(): a is B { ... }
3119     // Do not break before is in these cases.
3120     if (Right.is(Keywords.kw_is)) {
3121       const FormatToken* Next = Right.getNextNonComment();
3122       // If `is` is followed by a colon, it's likely that it's a dict key, so
3123       // ignore it for this check.
3124       // For example this is common in Polymer:
3125       // Polymer({
3126       //   is: 'name',
3127       //   ...
3128       // });
3129       if (!Next || !Next->is(tok::colon))
3130         return false;
3131     }
3132     if (Left.is(Keywords.kw_in))
3133       return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
3134     if (Right.is(Keywords.kw_in))
3135       return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
3136     if (Right.is(Keywords.kw_as))
3137       return false; // must not break before as in 'x as type' casts
3138     if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
3139       // extends and infer can appear as keywords in conditional types:
3140       //   https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
3141       // do not break before them, as the expressions are subject to ASI.
3142       return false;
3143     }
3144     if (Left.is(Keywords.kw_as))
3145       return true;
3146     if (Left.is(TT_JsNonNullAssertion))
3147       return true;
3148     if (Left.is(Keywords.kw_declare) &&
3149         Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
3150                       Keywords.kw_function, tok::kw_class, tok::kw_enum,
3151                       Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
3152                       Keywords.kw_let, tok::kw_const))
3153       // See grammar for 'declare' statements at:
3154       // https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#A.10
3155       return false;
3156     if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
3157         Right.isOneOf(tok::identifier, tok::string_literal))
3158       return false; // must not break in "module foo { ...}"
3159     if (Right.is(TT_TemplateString) && Right.closesScope())
3160       return false;
3161     if (Left.is(TT_TemplateString) && Left.opensScope())
3162       return true;
3163   }
3164 
3165   if (Left.is(tok::at))
3166     return false;
3167   if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
3168     return false;
3169   if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
3170     return !Right.is(tok::l_paren);
3171   if (Right.is(TT_PointerOrReference))
3172     return Line.IsMultiVariableDeclStmt ||
3173            (Style.PointerAlignment == FormatStyle::PAS_Right &&
3174             (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
3175   if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
3176       Right.is(tok::kw_operator))
3177     return true;
3178   if (Left.is(TT_PointerOrReference))
3179     return false;
3180   if (Right.isTrailingComment())
3181     // We rely on MustBreakBefore being set correctly here as we should not
3182     // change the "binding" behavior of a comment.
3183     // The first comment in a braced lists is always interpreted as belonging to
3184     // the first list element. Otherwise, it should be placed outside of the
3185     // list.
3186     return Left.BlockKind == BK_BracedInit ||
3187            (Left.is(TT_CtorInitializerColon) &&
3188             Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
3189   if (Left.is(tok::question) && Right.is(tok::colon))
3190     return false;
3191   if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
3192     return Style.BreakBeforeTernaryOperators;
3193   if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
3194     return !Style.BreakBeforeTernaryOperators;
3195   if (Left.is(TT_InheritanceColon))
3196     return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
3197   if (Right.is(TT_InheritanceColon))
3198     return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
3199   if (Right.is(TT_ObjCMethodExpr) && !Right.is(tok::r_square) &&
3200       Left.isNot(TT_SelectorName))
3201     return true;
3202 
3203   if (Right.is(tok::colon) &&
3204       !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon))
3205     return false;
3206   if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
3207     if (Style.Language == FormatStyle::LK_Proto ||
3208         Style.Language == FormatStyle::LK_TextProto) {
3209       if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
3210         return false;
3211       // Prevent cases like:
3212       //
3213       // submessage:
3214       //     { key: valueeeeeeeeeeee }
3215       //
3216       // when the snippet does not fit into one line.
3217       // Prefer:
3218       //
3219       // submessage: {
3220       //   key: valueeeeeeeeeeee
3221       // }
3222       //
3223       // instead, even if it is longer by one line.
3224       //
3225       // Note that this allows allows the "{" to go over the column limit
3226       // when the column limit is just between ":" and "{", but that does
3227       // not happen too often and alternative formattings in this case are
3228       // not much better.
3229       //
3230       // The code covers the cases:
3231       //
3232       // submessage: { ... }
3233       // submessage: < ... >
3234       // repeated: [ ... ]
3235       if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
3236            Right.is(TT_DictLiteral)) ||
3237           Right.is(TT_ArrayInitializerLSquare))
3238         return false;
3239     }
3240     return true;
3241   }
3242   if (Right.is(tok::r_square) && Right.MatchingParen &&
3243       Right.MatchingParen->is(TT_ProtoExtensionLSquare))
3244     return false;
3245   if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
3246                                     Right.Next->is(TT_ObjCMethodExpr)))
3247     return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
3248   if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
3249     return true;
3250   if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen))
3251     return true;
3252   if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
3253                     TT_OverloadedOperator))
3254     return false;
3255   if (Left.is(TT_RangeBasedForLoopColon))
3256     return true;
3257   if (Right.is(TT_RangeBasedForLoopColon))
3258     return false;
3259   if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
3260     return true;
3261   if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
3262       Left.is(tok::kw_operator))
3263     return false;
3264   if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
3265       Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0)
3266     return false;
3267   if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
3268       !Style.Cpp11BracedListStyle)
3269     return false;
3270   if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen))
3271     return false;
3272   if (Left.is(tok::l_paren) && Left.Previous &&
3273       (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen)))
3274     return false;
3275   if (Right.is(TT_ImplicitStringLiteral))
3276     return false;
3277 
3278   if (Right.is(tok::r_paren) || Right.is(TT_TemplateCloser))
3279     return false;
3280   if (Right.is(tok::r_square) && Right.MatchingParen &&
3281       Right.MatchingParen->is(TT_LambdaLSquare))
3282     return false;
3283 
3284   // We only break before r_brace if there was a corresponding break before
3285   // the l_brace, which is tracked by BreakBeforeClosingBrace.
3286   if (Right.is(tok::r_brace))
3287     return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
3288 
3289   // Allow breaking after a trailing annotation, e.g. after a method
3290   // declaration.
3291   if (Left.is(TT_TrailingAnnotation))
3292     return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
3293                           tok::less, tok::coloncolon);
3294 
3295   if (Right.is(tok::kw___attribute) ||
3296       (Right.is(tok::l_square) && Right.is(TT_AttributeSquare)))
3297     return true;
3298 
3299   if (Left.is(tok::identifier) && Right.is(tok::string_literal))
3300     return true;
3301 
3302   if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
3303     return true;
3304 
3305   if (Left.is(TT_CtorInitializerColon))
3306     return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon;
3307   if (Right.is(TT_CtorInitializerColon))
3308     return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
3309   if (Left.is(TT_CtorInitializerComma) &&
3310       Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
3311     return false;
3312   if (Right.is(TT_CtorInitializerComma) &&
3313       Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
3314     return true;
3315   if (Left.is(TT_InheritanceComma) &&
3316       Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma)
3317     return false;
3318   if (Right.is(TT_InheritanceComma) &&
3319       Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma)
3320     return true;
3321   if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
3322       (Left.is(tok::less) && Right.is(tok::less)))
3323     return false;
3324   if (Right.is(TT_BinaryOperator) &&
3325       Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
3326       (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
3327        Right.getPrecedence() != prec::Assignment))
3328     return true;
3329   if (Left.is(TT_ArrayInitializerLSquare))
3330     return true;
3331   if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
3332     return true;
3333   if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
3334       !Left.isOneOf(tok::arrowstar, tok::lessless) &&
3335       Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
3336       (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
3337        Left.getPrecedence() == prec::Assignment))
3338     return true;
3339   if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) ||
3340       (Left.is(tok::r_square) && Right.is(TT_AttributeSquare)))
3341     return false;
3342   return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
3343                       tok::kw_class, tok::kw_struct, tok::comment) ||
3344          Right.isMemberAccess() ||
3345          Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
3346                        tok::colon, tok::l_square, tok::at) ||
3347          (Left.is(tok::r_paren) &&
3348           Right.isOneOf(tok::identifier, tok::kw_const)) ||
3349          (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
3350          (Left.is(TT_TemplateOpener) && !Right.is(TT_TemplateCloser));
3351 }
3352 
3353 void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
3354   llvm::errs() << "AnnotatedTokens(L=" << Line.Level << "):\n";
3355   const FormatToken *Tok = Line.First;
3356   while (Tok) {
3357     llvm::errs() << " M=" << Tok->MustBreakBefore
3358                  << " C=" << Tok->CanBreakBefore
3359                  << " T=" << getTokenTypeName(Tok->Type)
3360                  << " S=" << Tok->SpacesRequiredBefore
3361                  << " B=" << Tok->BlockParameterCount
3362                  << " BK=" << Tok->BlockKind << " P=" << Tok->SplitPenalty
3363                  << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength
3364                  << " PPK=" << Tok->PackingKind << " FakeLParens=";
3365     for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
3366       llvm::errs() << Tok->FakeLParens[i] << "/";
3367     llvm::errs() << " FakeRParens=" << Tok->FakeRParens;
3368     llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo();
3369     llvm::errs() << " Text='" << Tok->TokenText << "'\n";
3370     if (!Tok->Next)
3371       assert(Tok == Line.Last);
3372     Tok = Tok->Next;
3373   }
3374   llvm::errs() << "----\n";
3375 }
3376 
3377 } // namespace format
3378 } // namespace clang
3379