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