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