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