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