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/Support/Debug.h"
19 
20 #define DEBUG_TYPE "format-token-annotator"
21 
22 namespace clang {
23 namespace format {
24 
25 namespace {
26 
27 /// \brief A parser that gathers additional information about tokens.
28 ///
29 /// The \c TokenAnnotator tries to match parenthesis and square brakets and
30 /// store a parenthesis levels. It also tries to resolve matching "<" and ">"
31 /// into template parameter lists.
32 class AnnotatingParser {
33 public:
34   AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
35                    const AdditionalKeywords &Keywords)
36       : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
37         Keywords(Keywords) {
38     Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
39     resetTokenMetadata(CurrentToken);
40   }
41 
42 private:
43   bool parseAngle() {
44     if (!CurrentToken)
45       return false;
46     ScopedContextCreator ContextCreator(*this, tok::less, 10);
47     FormatToken *Left = CurrentToken->Previous;
48     Contexts.back().IsExpression = false;
49     // If there's a template keyword before the opening angle bracket, this is a
50     // template parameter, not an argument.
51     Contexts.back().InTemplateArgument =
52         Left->Previous && Left->Previous->Tok.isNot(tok::kw_template);
53 
54     if (Style.Language == FormatStyle::LK_Java &&
55         CurrentToken->is(tok::question))
56       next();
57 
58     while (CurrentToken) {
59       if (CurrentToken->is(tok::greater)) {
60         Left->MatchingParen = CurrentToken;
61         CurrentToken->MatchingParen = Left;
62         CurrentToken->Type = TT_TemplateCloser;
63         next();
64         return true;
65       }
66       if (CurrentToken->is(tok::question) &&
67           Style.Language == FormatStyle::LK_Java) {
68         next();
69         continue;
70       }
71       if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
72                                 tok::colon, tok::question))
73         return false;
74       // If a && or || is found and interpreted as a binary operator, this set
75       // of angles is likely part of something like "a < b && c > d". If the
76       // angles are inside an expression, the ||/&& might also be a binary
77       // operator that was misinterpreted because we are parsing template
78       // parameters.
79       // FIXME: This is getting out of hand, write a decent parser.
80       if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
81           CurrentToken->Previous->is(TT_BinaryOperator) &&
82           Contexts[Contexts.size() - 2].IsExpression &&
83           Line.First->isNot(tok::kw_template))
84         return false;
85       updateParameterCount(Left, CurrentToken);
86       if (!consumeToken())
87         return false;
88     }
89     return false;
90   }
91 
92   bool parseParens(bool LookForDecls = false) {
93     if (!CurrentToken)
94       return false;
95     ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
96 
97     // FIXME: This is a bit of a hack. Do better.
98     Contexts.back().ColonIsForRangeExpr =
99         Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
100 
101     bool StartsObjCMethodExpr = false;
102     FormatToken *Left = CurrentToken->Previous;
103     if (CurrentToken->is(tok::caret)) {
104       // (^ can start a block type.
105       Left->Type = TT_ObjCBlockLParen;
106     } else if (FormatToken *MaybeSel = Left->Previous) {
107       // @selector( starts a selector.
108       if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
109           MaybeSel->Previous->is(tok::at)) {
110         StartsObjCMethodExpr = true;
111       }
112     }
113 
114     if (Left->Previous &&
115         (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_if,
116                                  tok::kw_while, tok::l_paren, tok::comma) ||
117          Left->Previous->is(TT_BinaryOperator))) {
118       // static_assert, if and while usually contain expressions.
119       Contexts.back().IsExpression = true;
120     } else if (Line.InPPDirective &&
121                (!Left->Previous ||
122                 !Left->Previous->isOneOf(tok::identifier,
123                                          TT_OverloadedOperator))) {
124       Contexts.back().IsExpression = true;
125     } else if (Left->Previous && Left->Previous->is(tok::r_square) &&
126                Left->Previous->MatchingParen &&
127                Left->Previous->MatchingParen->is(TT_LambdaLSquare)) {
128       // This is a parameter list of a lambda expression.
129       Contexts.back().IsExpression = false;
130     } else if (Contexts[Contexts.size() - 2].CaretFound) {
131       // This is the parameter list of an ObjC block.
132       Contexts.back().IsExpression = false;
133     } else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
134       Left->Type = TT_AttributeParen;
135     } else if (Left->Previous && Left->Previous->IsForEachMacro) {
136       // The first argument to a foreach macro is a declaration.
137       Contexts.back().IsForEachMacro = true;
138       Contexts.back().IsExpression = false;
139     } else if (Left->Previous && Left->Previous->MatchingParen &&
140                Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
141       Contexts.back().IsExpression = false;
142     }
143 
144     if (StartsObjCMethodExpr) {
145       Contexts.back().ColonIsObjCMethodExpr = true;
146       Left->Type = TT_ObjCMethodExpr;
147     }
148 
149     bool MightBeFunctionType = CurrentToken->is(tok::star);
150     bool HasMultipleLines = false;
151     bool HasMultipleParametersOnALine = false;
152     while (CurrentToken) {
153       // LookForDecls is set when "if (" has been seen. Check for
154       // 'identifier' '*' 'identifier' followed by not '=' -- this
155       // '*' has to be a binary operator but determineStarAmpUsage() will
156       // categorize it as an unary operator, so set the right type here.
157       if (LookForDecls && CurrentToken->Next) {
158         FormatToken *Prev = CurrentToken->getPreviousNonComment();
159         if (Prev) {
160           FormatToken *PrevPrev = Prev->getPreviousNonComment();
161           FormatToken *Next = CurrentToken->Next;
162           if (PrevPrev && PrevPrev->is(tok::identifier) &&
163               Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
164               CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
165             Prev->Type = TT_BinaryOperator;
166             LookForDecls = false;
167           }
168         }
169       }
170 
171       if (CurrentToken->Previous->is(TT_PointerOrReference) &&
172           CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
173                                                     tok::coloncolon))
174         MightBeFunctionType = true;
175       if (CurrentToken->Previous->is(TT_BinaryOperator))
176         Contexts.back().IsExpression = true;
177       if (CurrentToken->is(tok::r_paren)) {
178         if (MightBeFunctionType && CurrentToken->Next &&
179             (CurrentToken->Next->is(tok::l_paren) ||
180              (CurrentToken->Next->is(tok::l_square) &&
181               !Contexts.back().IsExpression)))
182           Left->Type = TT_FunctionTypeLParen;
183         Left->MatchingParen = CurrentToken;
184         CurrentToken->MatchingParen = Left;
185 
186         if (StartsObjCMethodExpr) {
187           CurrentToken->Type = TT_ObjCMethodExpr;
188           if (Contexts.back().FirstObjCSelectorName) {
189             Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
190                 Contexts.back().LongestObjCSelectorName;
191           }
192         }
193 
194         if (Left->is(TT_AttributeParen))
195           CurrentToken->Type = TT_AttributeParen;
196         if (Left->Previous && Left->Previous->is(TT_JavaAnnotation))
197           CurrentToken->Type = TT_JavaAnnotation;
198         if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation))
199           CurrentToken->Type = TT_LeadingJavaAnnotation;
200 
201         if (!HasMultipleLines)
202           Left->PackingKind = PPK_Inconclusive;
203         else if (HasMultipleParametersOnALine)
204           Left->PackingKind = PPK_BinPacked;
205         else
206           Left->PackingKind = PPK_OnePerLine;
207 
208         next();
209         return true;
210       }
211       if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
212         return false;
213       else if (CurrentToken->is(tok::l_brace))
214         Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen
215       if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
216           !CurrentToken->Next->HasUnescapedNewline &&
217           !CurrentToken->Next->isTrailingComment())
218         HasMultipleParametersOnALine = true;
219       if (CurrentToken->isOneOf(tok::kw_const, tok::kw_auto) ||
220           CurrentToken->isSimpleTypeSpecifier())
221         Contexts.back().IsExpression = false;
222       FormatToken *Tok = CurrentToken;
223       if (!consumeToken())
224         return false;
225       updateParameterCount(Left, Tok);
226       if (CurrentToken && CurrentToken->HasUnescapedNewline)
227         HasMultipleLines = true;
228     }
229     return false;
230   }
231 
232   bool parseSquare() {
233     if (!CurrentToken)
234       return false;
235 
236     // A '[' could be an index subscript (after an identifier or after
237     // ')' or ']'), it could be the start of an Objective-C method
238     // expression, or it could the the start of an Objective-C array literal.
239     FormatToken *Left = CurrentToken->Previous;
240     FormatToken *Parent = Left->getPreviousNonComment();
241     bool StartsObjCMethodExpr =
242         Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
243         CurrentToken->isNot(tok::l_brace) &&
244         (!Parent ||
245          Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
246                          tok::kw_return, tok::kw_throw) ||
247          Parent->isUnaryOperator() ||
248          Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
249          getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
250     ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
251     Contexts.back().IsExpression = true;
252     bool ColonFound = false;
253 
254     if (StartsObjCMethodExpr) {
255       Contexts.back().ColonIsObjCMethodExpr = true;
256       Left->Type = TT_ObjCMethodExpr;
257     } else if (Parent && Parent->is(tok::at)) {
258       Left->Type = TT_ArrayInitializerLSquare;
259     } else if (Left->is(TT_Unknown)) {
260       Left->Type = TT_ArraySubscriptLSquare;
261     }
262 
263     while (CurrentToken) {
264       if (CurrentToken->is(tok::r_square)) {
265         if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
266             Left->is(TT_ObjCMethodExpr)) {
267           // An ObjC method call is rarely followed by an open parenthesis.
268           // FIXME: Do we incorrectly label ":" with this?
269           StartsObjCMethodExpr = false;
270           Left->Type = TT_Unknown;
271         }
272         if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
273           CurrentToken->Type = TT_ObjCMethodExpr;
274           // determineStarAmpUsage() thinks that '*' '[' is allocating an
275           // array of pointers, but if '[' starts a selector then '*' is a
276           // binary operator.
277           if (Parent && Parent->is(TT_PointerOrReference))
278             Parent->Type = TT_BinaryOperator;
279         }
280         Left->MatchingParen = CurrentToken;
281         CurrentToken->MatchingParen = Left;
282         if (Contexts.back().FirstObjCSelectorName) {
283           Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
284               Contexts.back().LongestObjCSelectorName;
285           if (Left->BlockParameterCount > 1)
286             Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
287         }
288         next();
289         return true;
290       }
291       if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
292         return false;
293       if (CurrentToken->is(tok::colon)) {
294         if (Left->is(TT_ArraySubscriptLSquare)) {
295           Left->Type = TT_ObjCMethodExpr;
296           StartsObjCMethodExpr = true;
297           Contexts.back().ColonIsObjCMethodExpr = true;
298           if (Parent && Parent->is(tok::r_paren))
299             Parent->Type = TT_CastRParen;
300         }
301         ColonFound = true;
302       }
303       if (CurrentToken->is(tok::comma) &&
304           Style.Language != FormatStyle::LK_Proto &&
305           (Left->is(TT_ArraySubscriptLSquare) ||
306            (Left->is(TT_ObjCMethodExpr) && !ColonFound)))
307         Left->Type = TT_ArrayInitializerLSquare;
308       FormatToken* Tok = CurrentToken;
309       if (!consumeToken())
310         return false;
311       updateParameterCount(Left, Tok);
312     }
313     return false;
314   }
315 
316   bool parseBrace() {
317     if (CurrentToken) {
318       FormatToken *Left = CurrentToken->Previous;
319 
320       if (Contexts.back().CaretFound)
321         Left->Type = TT_ObjCBlockLBrace;
322       Contexts.back().CaretFound = false;
323 
324       ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
325       Contexts.back().ColonIsDictLiteral = true;
326       if (Left->BlockKind == BK_BracedInit)
327         Contexts.back().IsExpression = true;
328 
329       while (CurrentToken) {
330         if (CurrentToken->is(tok::r_brace)) {
331           Left->MatchingParen = CurrentToken;
332           CurrentToken->MatchingParen = Left;
333           next();
334           return true;
335         }
336         if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
337           return false;
338         updateParameterCount(Left, CurrentToken);
339         if (CurrentToken->isOneOf(tok::colon, tok::l_brace)) {
340           FormatToken *Previous = CurrentToken->getPreviousNonComment();
341           if ((CurrentToken->is(tok::colon) ||
342                Style.Language == FormatStyle::LK_Proto) &&
343               Previous->is(tok::identifier))
344             Previous->Type = TT_SelectorName;
345           if (CurrentToken->is(tok::colon))
346             Left->Type = TT_DictLiteral;
347         }
348         if (!consumeToken())
349           return false;
350       }
351     }
352     return true;
353   }
354 
355   void updateParameterCount(FormatToken *Left, FormatToken *Current) {
356     if (Current->is(TT_LambdaLSquare) ||
357         (Current->is(tok::caret) && Current->is(TT_UnaryOperator)) ||
358         (Style.Language == FormatStyle::LK_JavaScript &&
359          Current->is(Keywords.kw_function))) {
360       ++Left->BlockParameterCount;
361     }
362     if (Current->is(tok::comma)) {
363       ++Left->ParameterCount;
364       if (!Left->Role)
365         Left->Role.reset(new CommaSeparatedList(Style));
366       Left->Role->CommaFound(Current);
367     } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
368       Left->ParameterCount = 1;
369     }
370   }
371 
372   bool parseConditional() {
373     while (CurrentToken) {
374       if (CurrentToken->is(tok::colon)) {
375         CurrentToken->Type = TT_ConditionalExpr;
376         next();
377         return true;
378       }
379       if (!consumeToken())
380         return false;
381     }
382     return false;
383   }
384 
385   bool parseTemplateDeclaration() {
386     if (CurrentToken && CurrentToken->is(tok::less)) {
387       CurrentToken->Type = TT_TemplateOpener;
388       next();
389       if (!parseAngle())
390         return false;
391       if (CurrentToken)
392         CurrentToken->Previous->ClosesTemplateDeclaration = true;
393       return true;
394     }
395     return false;
396   }
397 
398   bool consumeToken() {
399     FormatToken *Tok = CurrentToken;
400     next();
401     switch (Tok->Tok.getKind()) {
402     case tok::plus:
403     case tok::minus:
404       if (!Tok->Previous && Line.MustBeDeclaration)
405         Tok->Type = TT_ObjCMethodSpecifier;
406       break;
407     case tok::colon:
408       if (!Tok->Previous)
409         return false;
410       // Colons from ?: are handled in parseConditional().
411       if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1 &&
412           Line.First->isNot(tok::kw_case)) {
413         Tok->Type = TT_CtorInitializerColon;
414       } else if (Contexts.back().ColonIsDictLiteral) {
415         Tok->Type = TT_DictLiteral;
416       } else if (Contexts.back().ColonIsObjCMethodExpr ||
417                  Line.First->is(TT_ObjCMethodSpecifier)) {
418         Tok->Type = TT_ObjCMethodExpr;
419         Tok->Previous->Type = TT_SelectorName;
420         if (Tok->Previous->ColumnWidth >
421             Contexts.back().LongestObjCSelectorName) {
422           Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
423         }
424         if (!Contexts.back().FirstObjCSelectorName)
425           Contexts.back().FirstObjCSelectorName = Tok->Previous;
426       } else if (Contexts.back().ColonIsForRangeExpr) {
427         Tok->Type = TT_RangeBasedForLoopColon;
428       } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
429         Tok->Type = TT_BitFieldColon;
430       } else if (Contexts.size() == 1 &&
431                  !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
432         Tok->Type = TT_InheritanceColon;
433       } else if (Tok->Previous->is(tok::identifier) && Tok->Next &&
434                  Tok->Next->isOneOf(tok::r_paren, tok::comma)) {
435         // This handles a special macro in ObjC code where selectors including
436         // the colon are passed as macro arguments.
437         Tok->Type = TT_ObjCMethodExpr;
438       } else if (Contexts.back().ContextKind == tok::l_paren) {
439         Tok->Type = TT_InlineASMColon;
440       }
441       break;
442     case tok::kw_if:
443     case tok::kw_while:
444       if (CurrentToken && CurrentToken->is(tok::l_paren)) {
445         next();
446         if (!parseParens(/*LookForDecls=*/true))
447           return false;
448       }
449       break;
450     case tok::kw_for:
451       Contexts.back().ColonIsForRangeExpr = true;
452       next();
453       if (!parseParens())
454         return false;
455       break;
456     case tok::l_paren:
457       if (!parseParens())
458         return false;
459       if (Line.MustBeDeclaration && Contexts.size() == 1 &&
460           !Contexts.back().IsExpression && Line.First->isNot(TT_ObjCProperty) &&
461           (!Tok->Previous || Tok->Previous->isNot(tok::kw_decltype)))
462         Line.MightBeFunctionDecl = true;
463       break;
464     case tok::l_square:
465       if (!parseSquare())
466         return false;
467       break;
468     case tok::l_brace:
469       if (!parseBrace())
470         return false;
471       break;
472     case tok::less:
473       if ((!Tok->Previous || !Tok->Previous->Tok.isLiteral()) && parseAngle())
474         Tok->Type = TT_TemplateOpener;
475       else {
476         Tok->Type = TT_BinaryOperator;
477         CurrentToken = Tok;
478         next();
479       }
480       break;
481     case tok::r_paren:
482     case tok::r_square:
483       return false;
484     case tok::r_brace:
485       // Lines can start with '}'.
486       if (Tok->Previous)
487         return false;
488       break;
489     case tok::greater:
490       Tok->Type = TT_BinaryOperator;
491       break;
492     case tok::kw_operator:
493       while (CurrentToken &&
494              !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
495         if (CurrentToken->isOneOf(tok::star, tok::amp))
496           CurrentToken->Type = TT_PointerOrReference;
497         consumeToken();
498         if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
499           CurrentToken->Previous->Type = TT_OverloadedOperator;
500       }
501       if (CurrentToken) {
502         CurrentToken->Type = TT_OverloadedOperatorLParen;
503         if (CurrentToken->Previous->is(TT_BinaryOperator))
504           CurrentToken->Previous->Type = TT_OverloadedOperator;
505       }
506       break;
507     case tok::question:
508       parseConditional();
509       break;
510     case tok::kw_template:
511       parseTemplateDeclaration();
512       break;
513     case tok::identifier:
514       if (Line.First->is(tok::kw_for) && Tok->is(Keywords.kw_in))
515         Tok->Type = TT_ObjCForIn;
516       break;
517     case tok::comma:
518       if (Contexts.back().FirstStartOfName)
519         Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
520       if (Contexts.back().InCtorInitializer)
521         Tok->Type = TT_CtorInitializerComma;
522       if (Contexts.back().IsForEachMacro)
523         Contexts.back().IsExpression = true;
524       break;
525     default:
526       break;
527     }
528     return true;
529   }
530 
531   void parseIncludeDirective() {
532     if (CurrentToken && CurrentToken->is(tok::less)) {
533       next();
534       while (CurrentToken) {
535         if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
536           CurrentToken->Type = TT_ImplicitStringLiteral;
537         next();
538       }
539     }
540   }
541 
542   void parseWarningOrError() {
543     next();
544     // We still want to format the whitespace left of the first token of the
545     // warning or error.
546     next();
547     while (CurrentToken) {
548       CurrentToken->Type = TT_ImplicitStringLiteral;
549       next();
550     }
551   }
552 
553   void parsePragma() {
554     next(); // Consume "pragma".
555     if (CurrentToken && CurrentToken->TokenText == "mark") {
556       next(); // Consume "mark".
557       next(); // Consume first token (so we fix leading whitespace).
558       while (CurrentToken) {
559         CurrentToken->Type = TT_ImplicitStringLiteral;
560         next();
561       }
562     }
563   }
564 
565   LineType parsePreprocessorDirective() {
566     LineType Type = LT_PreprocessorDirective;
567     next();
568     if (!CurrentToken)
569       return Type;
570     if (CurrentToken->Tok.is(tok::numeric_constant)) {
571       CurrentToken->SpacesRequiredBefore = 1;
572       return Type;
573     }
574     // Hashes in the middle of a line can lead to any strange token
575     // sequence.
576     if (!CurrentToken->Tok.getIdentifierInfo())
577       return Type;
578     switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
579     case tok::pp_include:
580     case tok::pp_import:
581       next();
582       parseIncludeDirective();
583       Type = LT_ImportStatement;
584       break;
585     case tok::pp_error:
586     case tok::pp_warning:
587       parseWarningOrError();
588       break;
589     case tok::pp_pragma:
590       parsePragma();
591       break;
592     case tok::pp_if:
593     case tok::pp_elif:
594       Contexts.back().IsExpression = true;
595       parseLine();
596       break;
597     default:
598       break;
599     }
600     while (CurrentToken)
601       next();
602     return Type;
603   }
604 
605 public:
606   LineType parseLine() {
607     if (CurrentToken->is(tok::hash)) {
608       return parsePreprocessorDirective();
609     }
610 
611     // Directly allow to 'import <string-literal>' to support protocol buffer
612     // definitions (code.google.com/p/protobuf) or missing "#" (either way we
613     // should not break the line).
614     IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
615     if ((Style.Language == FormatStyle::LK_Java &&
616          CurrentToken->is(Keywords.kw_package)) ||
617         (Info && Info->getPPKeywordID() == tok::pp_import &&
618          CurrentToken->Next &&
619          CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier))) {
620       next();
621       parseIncludeDirective();
622       return LT_ImportStatement;
623     }
624 
625     // If this line starts and ends in '<' and '>', respectively, it is likely
626     // part of "#define <a/b.h>".
627     if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
628       parseIncludeDirective();
629       return LT_ImportStatement;
630     }
631 
632     bool KeywordVirtualFound = false;
633     bool ImportStatement = false;
634     while (CurrentToken) {
635       if (CurrentToken->is(tok::kw_virtual))
636         KeywordVirtualFound = true;
637       if (IsImportStatement(*CurrentToken))
638         ImportStatement = true;
639       if (!consumeToken())
640         return LT_Invalid;
641     }
642     if (KeywordVirtualFound)
643       return LT_VirtualFunctionDecl;
644     if (ImportStatement)
645       return LT_ImportStatement;
646 
647     if (Line.First->is(TT_ObjCMethodSpecifier)) {
648       if (Contexts.back().FirstObjCSelectorName)
649         Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
650             Contexts.back().LongestObjCSelectorName;
651       return LT_ObjCMethodDecl;
652     }
653 
654     return LT_Other;
655   }
656 
657 private:
658   bool IsImportStatement(const FormatToken &Tok) {
659     // FIXME: Closure-library specific stuff should not be hard-coded but be
660     // configurable.
661     return Style.Language == FormatStyle::LK_JavaScript &&
662            Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
663            Tok.Next->Next && (Tok.Next->Next->TokenText == "module" ||
664                               Tok.Next->Next->TokenText == "require" ||
665                               Tok.Next->Next->TokenText == "provide") &&
666            Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
667   }
668 
669   void resetTokenMetadata(FormatToken *Token) {
670     if (!Token)
671       return;
672 
673     // Reset token type in case we have already looked at it and then
674     // recovered from an error (e.g. failure to find the matching >).
675     if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_FunctionLBrace,
676                                TT_ImplicitStringLiteral, TT_RegexLiteral,
677                                TT_TrailingReturnArrow))
678       CurrentToken->Type = TT_Unknown;
679     CurrentToken->Role.reset();
680     CurrentToken->FakeLParens.clear();
681     CurrentToken->FakeRParens = 0;
682   }
683 
684   void next() {
685     if (CurrentToken) {
686       CurrentToken->NestingLevel = Contexts.size() - 1;
687       CurrentToken->BindingStrength = Contexts.back().BindingStrength;
688       modifyContext(*CurrentToken);
689       determineTokenType(*CurrentToken);
690       CurrentToken = CurrentToken->Next;
691     }
692 
693     resetTokenMetadata(CurrentToken);
694   }
695 
696   /// \brief A struct to hold information valid in a specific context, e.g.
697   /// a pair of parenthesis.
698   struct Context {
699     Context(tok::TokenKind ContextKind, unsigned BindingStrength,
700             bool IsExpression)
701         : ContextKind(ContextKind), BindingStrength(BindingStrength),
702           LongestObjCSelectorName(0), ColonIsForRangeExpr(false),
703           ColonIsDictLiteral(false), ColonIsObjCMethodExpr(false),
704           FirstObjCSelectorName(nullptr), FirstStartOfName(nullptr),
705           IsExpression(IsExpression), CanBeExpression(true),
706           InTemplateArgument(false), InCtorInitializer(false),
707           CaretFound(false), IsForEachMacro(false) {}
708 
709     tok::TokenKind ContextKind;
710     unsigned BindingStrength;
711     unsigned LongestObjCSelectorName;
712     bool ColonIsForRangeExpr;
713     bool ColonIsDictLiteral;
714     bool ColonIsObjCMethodExpr;
715     FormatToken *FirstObjCSelectorName;
716     FormatToken *FirstStartOfName;
717     bool IsExpression;
718     bool CanBeExpression;
719     bool InTemplateArgument;
720     bool InCtorInitializer;
721     bool CaretFound;
722     bool IsForEachMacro;
723   };
724 
725   /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
726   /// of each instance.
727   struct ScopedContextCreator {
728     AnnotatingParser &P;
729 
730     ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
731                          unsigned Increase)
732         : P(P) {
733       P.Contexts.push_back(Context(ContextKind,
734                                    P.Contexts.back().BindingStrength + Increase,
735                                    P.Contexts.back().IsExpression));
736     }
737 
738     ~ScopedContextCreator() { P.Contexts.pop_back(); }
739   };
740 
741   void modifyContext(const FormatToken &Current) {
742     if (Current.getPrecedence() == prec::Assignment &&
743         !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
744         (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
745       Contexts.back().IsExpression = true;
746       for (FormatToken *Previous = Current.Previous;
747            Previous && !Previous->isOneOf(tok::comma, tok::semi);
748            Previous = Previous->Previous) {
749         if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
750           Previous = Previous->MatchingParen;
751           if (!Previous)
752             break;
753         }
754         if ((Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator)) &&
755             Previous->isOneOf(tok::star, tok::amp) && Previous->Previous &&
756             Previous->Previous->isNot(tok::equal)) {
757           Previous->Type = TT_PointerOrReference;
758         }
759       }
760     } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
761       Contexts.back().IsExpression = true;
762     } else if (Current.is(TT_TrailingReturnArrow)) {
763       Contexts.back().IsExpression = false;
764     } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
765                !Line.InPPDirective &&
766                (!Current.Previous ||
767                 Current.Previous->isNot(tok::kw_decltype))) {
768       bool ParametersOfFunctionType =
769           Current.Previous && Current.Previous->is(tok::r_paren) &&
770           Current.Previous->MatchingParen &&
771           Current.Previous->MatchingParen->is(TT_FunctionTypeLParen);
772       bool IsForOrCatch = Current.Previous &&
773                           Current.Previous->isOneOf(tok::kw_for, tok::kw_catch);
774       Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch;
775     } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
776       for (FormatToken *Previous = Current.Previous;
777            Previous && Previous->isOneOf(tok::star, tok::amp);
778            Previous = Previous->Previous)
779         Previous->Type = TT_PointerOrReference;
780       if (Line.MustBeDeclaration)
781         Contexts.back().IsExpression = Contexts.front().InCtorInitializer;
782     } else if (Current.Previous &&
783                Current.Previous->is(TT_CtorInitializerColon)) {
784       Contexts.back().IsExpression = true;
785       Contexts.back().InCtorInitializer = true;
786     } else if (Current.is(tok::kw_new)) {
787       Contexts.back().CanBeExpression = false;
788     } else if (Current.is(tok::semi) || Current.is(tok::exclaim)) {
789       // This should be the condition or increment in a for-loop.
790       Contexts.back().IsExpression = true;
791     }
792   }
793 
794   void determineTokenType(FormatToken &Current) {
795     if (!Current.is(TT_Unknown))
796       // The token type is already known.
797       return;
798 
799     // Line.MightBeFunctionDecl can only be true after the parentheses of a
800     // function declaration have been found. In this case, 'Current' is a
801     // trailing token of this declaration and thus cannot be a name.
802     if (Current.is(Keywords.kw_instanceof)) {
803       Current.Type = TT_BinaryOperator;
804     } else if (isStartOfName(Current) &&
805                (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
806       Contexts.back().FirstStartOfName = &Current;
807       Current.Type = TT_StartOfName;
808     } else if (Current.is(tok::kw_auto)) {
809       AutoFound = true;
810     } else if (Current.is(tok::arrow) &&
811                Style.Language == FormatStyle::LK_Java) {
812       Current.Type = TT_LambdaArrow;
813     } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration &&
814                Current.NestingLevel == 0) {
815       Current.Type = TT_TrailingReturnArrow;
816     } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
817       Current.Type =
818           determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
819                                              Contexts.back().IsExpression,
820                                 Contexts.back().InTemplateArgument);
821     } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
822       Current.Type = determinePlusMinusCaretUsage(Current);
823       if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
824         Contexts.back().CaretFound = true;
825     } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
826       Current.Type = determineIncrementUsage(Current);
827     } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
828       Current.Type = TT_UnaryOperator;
829     } else if (Current.is(tok::question)) {
830       Current.Type = TT_ConditionalExpr;
831     } else if (Current.isBinaryOperator() &&
832                (!Current.Previous || Current.Previous->isNot(tok::l_square))) {
833       Current.Type = TT_BinaryOperator;
834     } else if (Current.is(tok::comment)) {
835       if (Current.TokenText.startswith("//"))
836         Current.Type = TT_LineComment;
837       else
838         Current.Type = TT_BlockComment;
839     } else if (Current.is(tok::r_paren)) {
840       if (rParenEndsCast(Current))
841         Current.Type = TT_CastRParen;
842     } else if (Current.is(tok::at) && Current.Next) {
843       switch (Current.Next->Tok.getObjCKeywordID()) {
844       case tok::objc_interface:
845       case tok::objc_implementation:
846       case tok::objc_protocol:
847         Current.Type = TT_ObjCDecl;
848         break;
849       case tok::objc_property:
850         Current.Type = TT_ObjCProperty;
851         break;
852       default:
853         break;
854       }
855     } else if (Current.is(tok::period)) {
856       FormatToken *PreviousNoComment = Current.getPreviousNonComment();
857       if (PreviousNoComment &&
858           PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
859         Current.Type = TT_DesignatedInitializerPeriod;
860     } else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
861                Current.Previous &&
862                !Current.Previous->isOneOf(tok::equal, tok::at) &&
863                Line.MightBeFunctionDecl && Contexts.size() == 1) {
864       // Line.MightBeFunctionDecl can only be true after the parentheses of a
865       // function declaration have been found.
866       Current.Type = TT_TrailingAnnotation;
867     } else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
868                Current.Previous->is(tok::at) &&
869                Current.isNot(Keywords.kw_interface)) {
870       const FormatToken &AtToken = *Current.Previous;
871       const FormatToken *Previous = AtToken.getPreviousNonComment();
872       if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
873         Current.Type = TT_LeadingJavaAnnotation;
874       else
875         Current.Type = TT_JavaAnnotation;
876     }
877   }
878 
879   /// \brief Take a guess at whether \p Tok starts a name of a function or
880   /// variable declaration.
881   ///
882   /// This is a heuristic based on whether \p Tok is an identifier following
883   /// something that is likely a type.
884   bool isStartOfName(const FormatToken &Tok) {
885     if (Tok.isNot(tok::identifier) || !Tok.Previous)
886       return false;
887 
888     // Skip "const" as it does not have an influence on whether this is a name.
889     FormatToken *PreviousNotConst = Tok.Previous;
890     while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
891       PreviousNotConst = PreviousNotConst->Previous;
892 
893     if (!PreviousNotConst)
894       return false;
895 
896     bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
897                        PreviousNotConst->Previous &&
898                        PreviousNotConst->Previous->is(tok::hash);
899 
900     if (PreviousNotConst->is(TT_TemplateCloser))
901       return PreviousNotConst && PreviousNotConst->MatchingParen &&
902              PreviousNotConst->MatchingParen->Previous &&
903              PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
904              PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
905 
906     if (PreviousNotConst->is(tok::r_paren) && PreviousNotConst->MatchingParen &&
907         PreviousNotConst->MatchingParen->Previous &&
908         PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype))
909       return true;
910 
911     return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
912            PreviousNotConst->is(TT_PointerOrReference) ||
913            PreviousNotConst->isSimpleTypeSpecifier();
914   }
915 
916   /// \brief Determine whether ')' is ending a cast.
917   bool rParenEndsCast(const FormatToken &Tok) {
918     FormatToken *LeftOfParens = nullptr;
919     if (Tok.MatchingParen)
920       LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
921     if (LeftOfParens && LeftOfParens->is(tok::r_paren) &&
922         LeftOfParens->MatchingParen)
923       LeftOfParens = LeftOfParens->MatchingParen->Previous;
924     if (LeftOfParens && LeftOfParens->is(tok::r_square) &&
925         LeftOfParens->MatchingParen &&
926         LeftOfParens->MatchingParen->is(TT_LambdaLSquare))
927       return false;
928     if (Tok.Next) {
929       if (Style.Language == FormatStyle::LK_JavaScript &&
930           Tok.Next->is(Keywords.kw_in))
931         return false;
932       if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
933         return true;
934     }
935     bool IsCast = false;
936     bool ParensAreEmpty = Tok.Previous == Tok.MatchingParen;
937     bool ParensAreType =
938         !Tok.Previous ||
939         Tok.Previous->isOneOf(TT_PointerOrReference, TT_TemplateCloser) ||
940         Tok.Previous->isSimpleTypeSpecifier();
941     bool ParensCouldEndDecl =
942         Tok.Next && Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
943     bool IsSizeOfOrAlignOf =
944         LeftOfParens && LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
945     if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
946         ((Contexts.size() > 1 && Contexts[Contexts.size() - 2].IsExpression) ||
947          (Tok.Next && Tok.Next->isBinaryOperator())))
948       IsCast = true;
949     else if (Tok.Next && Tok.Next->isNot(tok::string_literal) &&
950              (Tok.Next->Tok.isLiteral() ||
951               Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
952       IsCast = true;
953     // If there is an identifier after the (), it is likely a cast, unless
954     // there is also an identifier before the ().
955     else if (LeftOfParens && Tok.Next &&
956              (LeftOfParens->Tok.getIdentifierInfo() == nullptr ||
957               LeftOfParens->is(tok::kw_return)) &&
958              !LeftOfParens->isOneOf(TT_OverloadedOperator, tok::at,
959                                     TT_TemplateCloser)) {
960       if (Tok.Next->isOneOf(tok::identifier, tok::numeric_constant)) {
961         IsCast = true;
962       } else {
963         // Use heuristics to recognize c style casting.
964         FormatToken *Prev = Tok.Previous;
965         if (Prev && Prev->isOneOf(tok::amp, tok::star))
966           Prev = Prev->Previous;
967 
968         if (Prev && Tok.Next && Tok.Next->Next) {
969           bool NextIsUnary = Tok.Next->isUnaryOperator() ||
970                              Tok.Next->isOneOf(tok::amp, tok::star);
971           IsCast =
972               NextIsUnary && !Tok.Next->is(tok::plus) &&
973               Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant);
974         }
975 
976         for (; Prev != Tok.MatchingParen; Prev = Prev->Previous) {
977           if (!Prev || !Prev->isOneOf(tok::kw_const, tok::identifier)) {
978             IsCast = false;
979             break;
980           }
981         }
982       }
983     }
984     return IsCast && !ParensAreEmpty;
985   }
986 
987   /// \brief Return the type of the given token assuming it is * or &.
988   TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
989                                   bool InTemplateArgument) {
990     if (Style.Language == FormatStyle::LK_JavaScript)
991       return TT_BinaryOperator;
992 
993     const FormatToken *PrevToken = Tok.getPreviousNonComment();
994     if (!PrevToken)
995       return TT_UnaryOperator;
996 
997     const FormatToken *NextToken = Tok.getNextNonComment();
998     if (!NextToken ||
999         (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
1000       return TT_Unknown;
1001 
1002     if (PrevToken->is(tok::coloncolon))
1003       return TT_PointerOrReference;
1004 
1005     if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
1006                            tok::comma, tok::semi, tok::kw_return, tok::colon,
1007                            tok::equal, tok::kw_delete, tok::kw_sizeof) ||
1008         PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
1009                            TT_UnaryOperator, TT_CastRParen))
1010       return TT_UnaryOperator;
1011 
1012     if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
1013       return TT_PointerOrReference;
1014     if (NextToken->isOneOf(tok::kw_operator, tok::comma))
1015       return TT_PointerOrReference;
1016 
1017     if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
1018         PrevToken->MatchingParen->Previous &&
1019         PrevToken->MatchingParen->Previous->isOneOf(tok::kw_typeof,
1020                                                     tok::kw_decltype))
1021       return TT_PointerOrReference;
1022 
1023     if (PrevToken->Tok.isLiteral() ||
1024         PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
1025                            tok::kw_false, tok::r_brace) ||
1026         NextToken->Tok.isLiteral() ||
1027         NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
1028         NextToken->isUnaryOperator() ||
1029         // If we know we're in a template argument, there are no named
1030         // declarations. Thus, having an identifier on the right-hand side
1031         // indicates a binary operator.
1032         (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
1033       return TT_BinaryOperator;
1034 
1035     // "&&(" is quite unlikely to be two successive unary "&".
1036     if (Tok.is(tok::ampamp) && NextToken && NextToken->is(tok::l_paren))
1037       return TT_BinaryOperator;
1038 
1039     // This catches some cases where evaluation order is used as control flow:
1040     //   aaa && aaa->f();
1041     const FormatToken *NextNextToken = NextToken->getNextNonComment();
1042     if (NextNextToken && NextNextToken->is(tok::arrow))
1043       return TT_BinaryOperator;
1044 
1045     // It is very unlikely that we are going to find a pointer or reference type
1046     // definition on the RHS of an assignment.
1047     if (IsExpression && !Contexts.back().CaretFound)
1048       return TT_BinaryOperator;
1049 
1050     return TT_PointerOrReference;
1051   }
1052 
1053   TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
1054     const FormatToken *PrevToken = Tok.getPreviousNonComment();
1055     if (!PrevToken || PrevToken->is(TT_CastRParen))
1056       return TT_UnaryOperator;
1057 
1058     // Use heuristics to recognize unary operators.
1059     if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
1060                            tok::question, tok::colon, tok::kw_return,
1061                            tok::kw_case, tok::at, tok::l_brace))
1062       return TT_UnaryOperator;
1063 
1064     // There can't be two consecutive binary operators.
1065     if (PrevToken->is(TT_BinaryOperator))
1066       return TT_UnaryOperator;
1067 
1068     // Fall back to marking the token as binary operator.
1069     return TT_BinaryOperator;
1070   }
1071 
1072   /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
1073   TokenType determineIncrementUsage(const FormatToken &Tok) {
1074     const FormatToken *PrevToken = Tok.getPreviousNonComment();
1075     if (!PrevToken || PrevToken->is(TT_CastRParen))
1076       return TT_UnaryOperator;
1077     if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
1078       return TT_TrailingUnaryOperator;
1079 
1080     return TT_UnaryOperator;
1081   }
1082 
1083   SmallVector<Context, 8> Contexts;
1084 
1085   const FormatStyle &Style;
1086   AnnotatedLine &Line;
1087   FormatToken *CurrentToken;
1088   bool AutoFound;
1089   const AdditionalKeywords &Keywords;
1090 };
1091 
1092 static int PrecedenceUnaryOperator = prec::PointerToMember + 1;
1093 static int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
1094 
1095 /// \brief Parses binary expressions by inserting fake parenthesis based on
1096 /// operator precedence.
1097 class ExpressionParser {
1098 public:
1099   ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
1100                    AnnotatedLine &Line)
1101       : Style(Style), Keywords(Keywords), Current(Line.First) {}
1102 
1103   /// \brief Parse expressions with the given operatore precedence.
1104   void parse(int Precedence = 0) {
1105     // Skip 'return' and ObjC selector colons as they are not part of a binary
1106     // expression.
1107     while (Current && (Current->is(tok::kw_return) ||
1108                        (Current->is(tok::colon) &&
1109                         Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))))
1110       next();
1111 
1112     if (!Current || Precedence > PrecedenceArrowAndPeriod)
1113       return;
1114 
1115     // Conditional expressions need to be parsed separately for proper nesting.
1116     if (Precedence == prec::Conditional) {
1117       parseConditionalExpr();
1118       return;
1119     }
1120 
1121     // Parse unary operators, which all have a higher precedence than binary
1122     // operators.
1123     if (Precedence == PrecedenceUnaryOperator) {
1124       parseUnaryOperator();
1125       return;
1126     }
1127 
1128     FormatToken *Start = Current;
1129     FormatToken *LatestOperator = nullptr;
1130     unsigned OperatorIndex = 0;
1131 
1132     while (Current) {
1133       // Consume operators with higher precedence.
1134       parse(Precedence + 1);
1135 
1136       int CurrentPrecedence = getCurrentPrecedence();
1137 
1138       if (Current && Current->is(TT_SelectorName) &&
1139           Precedence == CurrentPrecedence) {
1140         if (LatestOperator)
1141           addFakeParenthesis(Start, prec::Level(Precedence));
1142         Start = Current;
1143       }
1144 
1145       // At the end of the line or when an operator with higher precedence is
1146       // found, insert fake parenthesis and return.
1147       if (!Current || (Current->closesScope() && Current->MatchingParen) ||
1148           (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
1149           (CurrentPrecedence == prec::Conditional &&
1150            Precedence == prec::Assignment && Current->is(tok::colon))) {
1151         break;
1152       }
1153 
1154       // Consume scopes: (), [], <> and {}
1155       if (Current->opensScope()) {
1156         while (Current && !Current->closesScope()) {
1157           next();
1158           parse();
1159         }
1160         next();
1161       } else {
1162         // Operator found.
1163         if (CurrentPrecedence == Precedence) {
1164           LatestOperator = Current;
1165           Current->OperatorIndex = OperatorIndex;
1166           ++OperatorIndex;
1167         }
1168         next(/*SkipPastLeadingComments=*/Precedence > 0);
1169       }
1170     }
1171 
1172     if (LatestOperator && (Current || Precedence > 0)) {
1173       LatestOperator->LastOperator = true;
1174       if (Precedence == PrecedenceArrowAndPeriod) {
1175         // Call expressions don't have a binary operator precedence.
1176         addFakeParenthesis(Start, prec::Unknown);
1177       } else {
1178         addFakeParenthesis(Start, prec::Level(Precedence));
1179       }
1180     }
1181   }
1182 
1183 private:
1184   /// \brief Gets the precedence (+1) of the given token for binary operators
1185   /// and other tokens that we treat like binary operators.
1186   int getCurrentPrecedence() {
1187     if (Current) {
1188       const FormatToken *NextNonComment = Current->getNextNonComment();
1189       if (Current->is(TT_ConditionalExpr))
1190         return prec::Conditional;
1191       else if (NextNonComment && NextNonComment->is(tok::colon) &&
1192                NextNonComment->is(TT_DictLiteral))
1193         return prec::Comma;
1194       else if (Current->is(TT_LambdaArrow))
1195         return prec::Comma;
1196       else if (Current->isOneOf(tok::semi, TT_InlineASMColon,
1197                                 TT_SelectorName) ||
1198                (Current->is(tok::comment) && NextNonComment &&
1199                 NextNonComment->is(TT_SelectorName)))
1200         return 0;
1201       else if (Current->is(TT_RangeBasedForLoopColon))
1202         return prec::Comma;
1203       else if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
1204         return Current->getPrecedence();
1205       else if (Current->isOneOf(tok::period, tok::arrow))
1206         return PrecedenceArrowAndPeriod;
1207       else if (Style.Language == FormatStyle::LK_Java &&
1208                Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
1209                                 Keywords.kw_throws))
1210         return 0;
1211     }
1212     return -1;
1213   }
1214 
1215   void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
1216     Start->FakeLParens.push_back(Precedence);
1217     if (Precedence > prec::Unknown)
1218       Start->StartsBinaryExpression = true;
1219     if (Current) {
1220       FormatToken *Previous = Current->Previous;
1221       while (Previous->is(tok::comment) && Previous->Previous)
1222         Previous = Previous->Previous;
1223       ++Previous->FakeRParens;
1224       if (Precedence > prec::Unknown)
1225         Previous->EndsBinaryExpression = true;
1226     }
1227   }
1228 
1229   /// \brief Parse unary operator expressions and surround them with fake
1230   /// parentheses if appropriate.
1231   void parseUnaryOperator() {
1232     if (!Current || Current->isNot(TT_UnaryOperator)) {
1233       parse(PrecedenceArrowAndPeriod);
1234       return;
1235     }
1236 
1237     FormatToken *Start = Current;
1238     next();
1239     parseUnaryOperator();
1240 
1241     // The actual precedence doesn't matter.
1242     addFakeParenthesis(Start, prec::Unknown);
1243   }
1244 
1245   void parseConditionalExpr() {
1246     while (Current && Current->isTrailingComment()) {
1247       next();
1248     }
1249     FormatToken *Start = Current;
1250     parse(prec::LogicalOr);
1251     if (!Current || !Current->is(tok::question))
1252       return;
1253     next();
1254     parse(prec::Assignment);
1255     if (!Current || Current->isNot(TT_ConditionalExpr))
1256       return;
1257     next();
1258     parse(prec::Assignment);
1259     addFakeParenthesis(Start, prec::Conditional);
1260   }
1261 
1262   void next(bool SkipPastLeadingComments = true) {
1263     if (Current)
1264       Current = Current->Next;
1265     while (Current &&
1266            (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
1267            Current->isTrailingComment())
1268       Current = Current->Next;
1269   }
1270 
1271   const FormatStyle &Style;
1272   const AdditionalKeywords &Keywords;
1273   FormatToken *Current;
1274 };
1275 
1276 } // end anonymous namespace
1277 
1278 void
1279 TokenAnnotator::setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines) {
1280   const AnnotatedLine *NextNonCommentLine = nullptr;
1281   for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
1282                                                           E = Lines.rend();
1283        I != E; ++I) {
1284     if (NextNonCommentLine && (*I)->First->is(tok::comment) &&
1285         (*I)->First->Next == nullptr)
1286       (*I)->Level = NextNonCommentLine->Level;
1287     else
1288       NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr;
1289 
1290     setCommentLineLevels((*I)->Children);
1291   }
1292 }
1293 
1294 void TokenAnnotator::annotate(AnnotatedLine &Line) {
1295   for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1296                                                   E = Line.Children.end();
1297        I != E; ++I) {
1298     annotate(**I);
1299   }
1300   AnnotatingParser Parser(Style, Line, Keywords);
1301   Line.Type = Parser.parseLine();
1302   if (Line.Type == LT_Invalid)
1303     return;
1304 
1305   ExpressionParser ExprParser(Style, Keywords, Line);
1306   ExprParser.parse();
1307 
1308   if (Line.First->is(TT_ObjCMethodSpecifier))
1309     Line.Type = LT_ObjCMethodDecl;
1310   else if (Line.First->is(TT_ObjCDecl))
1311     Line.Type = LT_ObjCDecl;
1312   else if (Line.First->is(TT_ObjCProperty))
1313     Line.Type = LT_ObjCProperty;
1314 
1315   Line.First->SpacesRequiredBefore = 1;
1316   Line.First->CanBreakBefore = Line.First->MustBreakBefore;
1317 }
1318 
1319 // This function heuristically determines whether 'Current' starts the name of a
1320 // function declaration.
1321 static bool isFunctionDeclarationName(const FormatToken &Current) {
1322   if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0)
1323     return false;
1324   const FormatToken *Next = Current.Next;
1325   for (; Next; Next = Next->Next) {
1326     if (Next->is(TT_TemplateOpener)) {
1327       Next = Next->MatchingParen;
1328     } else if (Next->is(tok::coloncolon)) {
1329       Next = Next->Next;
1330       if (!Next || !Next->is(tok::identifier))
1331         return false;
1332     } else if (Next->is(tok::l_paren)) {
1333       break;
1334     } else {
1335       return false;
1336     }
1337   }
1338   if (!Next)
1339     return false;
1340   assert(Next->is(tok::l_paren));
1341   if (Next->Next == Next->MatchingParen)
1342     return true;
1343   for (const FormatToken *Tok = Next->Next; Tok != Next->MatchingParen;
1344        Tok = Tok->Next) {
1345     if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
1346         Tok->isOneOf(TT_PointerOrReference, TT_StartOfName))
1347       return true;
1348     if (Tok->isOneOf(tok::l_brace, tok::string_literal) || Tok->Tok.isLiteral())
1349       return false;
1350   }
1351   return false;
1352 }
1353 
1354 void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
1355   for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1356                                                   E = Line.Children.end();
1357        I != E; ++I) {
1358     calculateFormattingInformation(**I);
1359   }
1360 
1361   Line.First->TotalLength =
1362       Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth;
1363   if (!Line.First->Next)
1364     return;
1365   FormatToken *Current = Line.First->Next;
1366   bool InFunctionDecl = Line.MightBeFunctionDecl;
1367   while (Current) {
1368     if (isFunctionDeclarationName(*Current))
1369       Current->Type = TT_FunctionDeclarationName;
1370     if (Current->is(TT_LineComment)) {
1371       if (Current->Previous->BlockKind == BK_BracedInit &&
1372           Current->Previous->opensScope())
1373         Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1;
1374       else
1375         Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
1376 
1377       // If we find a trailing comment, iterate backwards to determine whether
1378       // it seems to relate to a specific parameter. If so, break before that
1379       // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
1380       // to the previous line in:
1381       //   SomeFunction(a,
1382       //                b, // comment
1383       //                c);
1384       if (!Current->HasUnescapedNewline) {
1385         for (FormatToken *Parameter = Current->Previous; Parameter;
1386              Parameter = Parameter->Previous) {
1387           if (Parameter->isOneOf(tok::comment, tok::r_brace))
1388             break;
1389           if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
1390             if (!Parameter->Previous->is(TT_CtorInitializerComma) &&
1391                 Parameter->HasUnescapedNewline)
1392               Parameter->MustBreakBefore = true;
1393             break;
1394           }
1395         }
1396       }
1397     } else if (Current->SpacesRequiredBefore == 0 &&
1398                spaceRequiredBefore(Line, *Current)) {
1399       Current->SpacesRequiredBefore = 1;
1400     }
1401 
1402     Current->MustBreakBefore =
1403         Current->MustBreakBefore || mustBreakBefore(Line, *Current);
1404 
1405     if (Style.AlwaysBreakAfterDefinitionReturnType &&
1406         InFunctionDecl && Current->is(TT_FunctionDeclarationName) &&
1407         !Line.Last->isOneOf(tok::semi, tok::comment))  // Only for definitions.
1408       // FIXME: Line.Last points to other characters than tok::semi
1409       // and tok::lbrace.
1410       Current->MustBreakBefore = true;
1411 
1412     Current->CanBreakBefore =
1413         Current->MustBreakBefore || canBreakBefore(Line, *Current);
1414     unsigned ChildSize = 0;
1415     if (Current->Previous->Children.size() == 1) {
1416       FormatToken &LastOfChild = *Current->Previous->Children[0]->Last;
1417       ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
1418                                                   : LastOfChild.TotalLength + 1;
1419     }
1420     const FormatToken *Prev= Current->Previous;
1421     if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
1422         (Prev->Children.size() == 1 &&
1423          Prev->Children[0]->First->MustBreakBefore) ||
1424         Current->IsMultiline)
1425       Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
1426     else
1427       Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
1428                              ChildSize + Current->SpacesRequiredBefore;
1429 
1430     if (Current->is(TT_CtorInitializerColon))
1431       InFunctionDecl = false;
1432 
1433     // FIXME: Only calculate this if CanBreakBefore is true once static
1434     // initializers etc. are sorted out.
1435     // FIXME: Move magic numbers to a better place.
1436     Current->SplitPenalty = 20 * Current->BindingStrength +
1437                             splitPenalty(Line, *Current, InFunctionDecl);
1438 
1439     Current = Current->Next;
1440   }
1441 
1442   calculateUnbreakableTailLengths(Line);
1443   for (Current = Line.First; Current != nullptr; Current = Current->Next) {
1444     if (Current->Role)
1445       Current->Role->precomputeFormattingInfos(Current);
1446   }
1447 
1448   DEBUG({ printDebugInfo(Line); });
1449 }
1450 
1451 void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1452   unsigned UnbreakableTailLength = 0;
1453   FormatToken *Current = Line.Last;
1454   while (Current) {
1455     Current->UnbreakableTailLength = UnbreakableTailLength;
1456     if (Current->CanBreakBefore ||
1457         Current->isOneOf(tok::comment, tok::string_literal)) {
1458       UnbreakableTailLength = 0;
1459     } else {
1460       UnbreakableTailLength +=
1461           Current->ColumnWidth + Current->SpacesRequiredBefore;
1462     }
1463     Current = Current->Previous;
1464   }
1465 }
1466 
1467 unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
1468                                       const FormatToken &Tok,
1469                                       bool InFunctionDecl) {
1470   const FormatToken &Left = *Tok.Previous;
1471   const FormatToken &Right = Tok;
1472 
1473   if (Left.is(tok::semi))
1474     return 0;
1475 
1476   if (Style.Language == FormatStyle::LK_Java) {
1477     if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
1478       return 1;
1479     if (Right.is(Keywords.kw_implements))
1480       return 2;
1481     if (Left.is(tok::comma) && Left.NestingLevel == 0)
1482       return 3;
1483   } else if (Style.Language == FormatStyle::LK_JavaScript) {
1484     if (Right.is(Keywords.kw_function))
1485       return 100;
1486   }
1487 
1488   if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next &&
1489                               Right.Next->is(TT_DictLiteral)))
1490     return 1;
1491   if (Right.is(tok::l_square)) {
1492     if (Style.Language == FormatStyle::LK_Proto)
1493       return 1;
1494     if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare))
1495       return 500;
1496   }
1497 
1498   if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
1499       Right.is(tok::kw_operator)) {
1500     if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
1501       return 3;
1502     if (Left.is(TT_StartOfName))
1503       return 20;
1504     if (InFunctionDecl && Right.NestingLevel == 0)
1505       return Style.PenaltyReturnTypeOnItsOwnLine;
1506     return 200;
1507   }
1508   if (Left.is(tok::equal) && Right.is(tok::l_brace))
1509     return 150;
1510   if (Left.is(TT_CastRParen))
1511     return 100;
1512   if (Left.is(tok::coloncolon) ||
1513       (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
1514     return 500;
1515   if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1516     return 5000;
1517 
1518   if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon))
1519     return 2;
1520 
1521   if (Right.isMemberAccess()) {
1522     if (Left.is(tok::r_paren) && Left.MatchingParen &&
1523         Left.MatchingParen->ParameterCount > 0)
1524       return 20; // Should be smaller than breaking at a nested comma.
1525     return 150;
1526   }
1527 
1528   if (Right.is(TT_TrailingAnnotation) &&
1529       (!Right.Next || Right.Next->isNot(tok::l_paren))) {
1530     // Moving trailing annotations to the next line is fine for ObjC method
1531     // declarations.
1532     if (Line.First->is(TT_ObjCMethodSpecifier))
1533 
1534       return 10;
1535     // Generally, breaking before a trailing annotation is bad unless it is
1536     // function-like. It seems to be especially preferable to keep standard
1537     // annotations (i.e. "const", "final" and "override") on the same line.
1538     // Use a slightly higher penalty after ")" so that annotations like
1539     // "const override" are kept together.
1540     bool is_short_annotation = Right.TokenText.size() < 10;
1541     return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
1542   }
1543 
1544   // In for-loops, prefer breaking at ',' and ';'.
1545   if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
1546     return 4;
1547 
1548   // In Objective-C method expressions, prefer breaking before "param:" over
1549   // breaking after it.
1550   if (Right.is(TT_SelectorName))
1551     return 0;
1552   if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
1553     return Line.MightBeFunctionDecl ? 50 : 500;
1554 
1555   if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
1556     return 100;
1557   if (Left.is(tok::equal) && InFunctionDecl)
1558     return 110;
1559   if (Right.is(tok::r_brace))
1560     return 1;
1561   if (Left.is(TT_TemplateOpener))
1562     return 100;
1563   if (Left.opensScope()) {
1564     if (!Style.AlignAfterOpenBracket)
1565       return 0;
1566     return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
1567                                    : 19;
1568   }
1569 
1570   if (Right.is(tok::lessless)) {
1571     if (Left.is(tok::string_literal)) {
1572       StringRef Content = Left.TokenText;
1573       if (Content.startswith("\""))
1574         Content = Content.drop_front(1);
1575       if (Content.endswith("\""))
1576         Content = Content.drop_back(1);
1577       Content = Content.trim();
1578       if (Content.size() > 1 &&
1579           (Content.back() == ':' || Content.back() == '='))
1580         return 25;
1581     }
1582     return 1; // Breaking at a << is really cheap.
1583   }
1584   if (Left.is(TT_ConditionalExpr))
1585     return prec::Conditional;
1586   prec::Level Level = Left.getPrecedence();
1587 
1588   if (Level != prec::Unknown)
1589     return Level;
1590 
1591   return 3;
1592 }
1593 
1594 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
1595                                           const FormatToken &Left,
1596                                           const FormatToken &Right) {
1597   if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
1598     return true;
1599   if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
1600       Left.Tok.getObjCKeywordID() == tok::objc_property)
1601     return true;
1602   if (Right.is(tok::hashhash))
1603     return Left.is(tok::hash);
1604   if (Left.isOneOf(tok::hashhash, tok::hash))
1605     return Right.is(tok::hash);
1606   if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1607     return Style.SpaceInEmptyParentheses;
1608   if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
1609     return (Right.is(TT_CastRParen )||
1610             (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
1611                ? Style.SpacesInCStyleCastParentheses
1612                : Style.SpacesInParentheses;
1613   if (Right.isOneOf(tok::semi, tok::comma))
1614     return false;
1615   if (Right.is(tok::less) &&
1616       (Left.isOneOf(tok::kw_template, tok::r_paren) ||
1617        (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1618     return true;
1619   if (Left.isOneOf(tok::exclaim, tok::tilde))
1620     return false;
1621   if (Left.is(tok::at) &&
1622       Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1623                     tok::numeric_constant, tok::l_paren, tok::l_brace,
1624                     tok::kw_true, tok::kw_false))
1625     return false;
1626   if (Left.is(tok::coloncolon))
1627     return false;
1628   if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
1629     return false;
1630   if (Right.is(tok::ellipsis))
1631     return Left.Tok.isLiteral();
1632   if (Left.is(tok::l_square) && Right.is(tok::amp))
1633     return false;
1634   if (Right.is(TT_PointerOrReference))
1635     return Left.Tok.isLiteral() ||
1636            (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
1637             Style.PointerAlignment != FormatStyle::PAS_Left);
1638   if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
1639       (!Left.is(TT_PointerOrReference) ||
1640        Style.PointerAlignment != FormatStyle::PAS_Right))
1641     return true;
1642   if (Left.is(TT_PointerOrReference))
1643     return Right.Tok.isLiteral() || Right.is(TT_BlockComment) ||
1644            (!Right.isOneOf(TT_PointerOrReference, tok::l_paren) &&
1645             Style.PointerAlignment != FormatStyle::PAS_Right && Left.Previous &&
1646             !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
1647   if (Right.is(tok::star) && Left.is(tok::l_paren))
1648     return false;
1649   if (Left.is(tok::l_square))
1650     return (Left.is(TT_ArrayInitializerLSquare) &&
1651             Style.SpacesInContainerLiterals && Right.isNot(tok::r_square)) ||
1652            (Left.is(TT_ArraySubscriptLSquare) && Style.SpacesInSquareBrackets &&
1653             Right.isNot(tok::r_square));
1654   if (Right.is(tok::r_square))
1655     return Right.MatchingParen &&
1656            ((Style.SpacesInContainerLiterals &&
1657              Right.MatchingParen->is(TT_ArrayInitializerLSquare)) ||
1658             (Style.SpacesInSquareBrackets &&
1659              Right.MatchingParen->is(TT_ArraySubscriptLSquare)));
1660   if (Right.is(tok::l_square) &&
1661       !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare) &&
1662       !Left.isOneOf(tok::numeric_constant, TT_DictLiteral))
1663     return false;
1664   if (Left.is(tok::colon))
1665     return !Left.is(TT_ObjCMethodExpr);
1666   if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
1667     return !Left.Children.empty(); // No spaces in "{}".
1668   if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) ||
1669       (Right.is(tok::r_brace) && Right.MatchingParen &&
1670        Right.MatchingParen->BlockKind != BK_Block))
1671     return !Style.Cpp11BracedListStyle;
1672   if (Left.is(TT_BlockComment))
1673     return !Left.TokenText.endswith("=*/");
1674   if (Right.is(tok::l_paren)) {
1675     if (Left.is(tok::r_paren) && Left.is(TT_AttributeParen))
1676       return true;
1677     return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
1678            (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
1679             (Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while,
1680                           tok::kw_switch, tok::kw_case) ||
1681              (Left.isOneOf(tok::kw_catch, tok::kw_new, tok::kw_delete) &&
1682               (!Left.Previous || Left.Previous->isNot(tok::period))) ||
1683              Left.IsForEachMacro)) ||
1684            (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
1685             (Left.is(tok::identifier) || Left.isFunctionLikeKeyword()) &&
1686             Line.Type != LT_PreprocessorDirective);
1687   }
1688   if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
1689     return false;
1690   if (Right.is(TT_UnaryOperator))
1691     return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1692            (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
1693   if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
1694                     tok::r_paren) ||
1695        Left.isSimpleTypeSpecifier()) &&
1696       Right.is(tok::l_brace) && Right.getNextNonComment() &&
1697       Right.BlockKind != BK_Block)
1698     return false;
1699   if (Left.is(tok::period) || Right.is(tok::period))
1700     return false;
1701   if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
1702     return false;
1703   if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
1704       Left.MatchingParen->Previous &&
1705       Left.MatchingParen->Previous->is(tok::period))
1706     // A.<B>DoSomething();
1707     return false;
1708   if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
1709     return false;
1710   return true;
1711 }
1712 
1713 bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
1714                                          const FormatToken &Right) {
1715   const FormatToken &Left = *Right.Previous;
1716   if (Style.Language == FormatStyle::LK_Proto) {
1717     if (Right.is(tok::period) &&
1718         Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
1719                      Keywords.kw_repeated))
1720       return true;
1721     if (Right.is(tok::l_paren) &&
1722         Left.isOneOf(Keywords.kw_returns, Keywords.kw_option))
1723       return true;
1724   } else if (Style.Language == FormatStyle::LK_JavaScript) {
1725     if (Left.is(Keywords.kw_var))
1726       return true;
1727   } else if (Style.Language == FormatStyle::LK_Java) {
1728     if (Left.is(tok::r_square) && Right.is(tok::l_brace))
1729       return true;
1730     if (Left.is(TT_LambdaArrow) || Right.is(TT_LambdaArrow))
1731       return true;
1732     if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
1733       return Style.SpaceBeforeParens != FormatStyle::SBPO_Never;
1734     if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
1735                       tok::kw_protected) ||
1736          Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract)) &&
1737         Right.is(TT_TemplateOpener))
1738       return true;
1739   }
1740   if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
1741     return true; // Never ever merge two identifiers.
1742   if (Left.is(TT_ImplicitStringLiteral))
1743     return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
1744   if (Line.Type == LT_ObjCMethodDecl) {
1745     if (Left.is(TT_ObjCMethodSpecifier))
1746       return true;
1747     if (Left.is(tok::r_paren) && Right.is(tok::identifier))
1748       // Don't space between ')' and <id>
1749       return false;
1750   }
1751   if (Line.Type == LT_ObjCProperty &&
1752       (Right.is(tok::equal) || Left.is(tok::equal)))
1753     return false;
1754 
1755   if (Right.is(TT_TrailingReturnArrow) || Left.is(TT_TrailingReturnArrow))
1756     return true;
1757   if (Left.is(tok::comma))
1758     return true;
1759   if (Right.is(tok::comma))
1760     return false;
1761   if (Right.isOneOf(TT_CtorInitializerColon, TT_ObjCBlockLParen))
1762     return true;
1763   if (Left.is(tok::kw_operator))
1764     return Right.is(tok::coloncolon);
1765   if (Right.is(TT_OverloadedOperatorLParen))
1766     return false;
1767   if (Right.is(tok::colon))
1768     return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
1769            Right.getNextNonComment() && Right.isNot(TT_ObjCMethodExpr) &&
1770            !Left.is(tok::question) &&
1771            !(Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon)) &&
1772            (Right.isNot(TT_DictLiteral) || Style.SpacesInContainerLiterals);
1773   if (Left.is(TT_UnaryOperator))
1774     return Right.is(TT_BinaryOperator);
1775   if (Left.is(TT_CastRParen))
1776     return Style.SpaceAfterCStyleCast || Right.is(TT_BinaryOperator);
1777   if (Left.is(tok::greater) && Right.is(tok::greater)) {
1778     return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
1779            (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
1780   }
1781   if (Right.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
1782       Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar))
1783     return false;
1784   if (!Style.SpaceBeforeAssignmentOperators &&
1785       Right.getPrecedence() == prec::Assignment)
1786     return false;
1787   if (Right.is(tok::coloncolon) && Left.isNot(tok::l_brace))
1788     return (Left.is(TT_TemplateOpener) &&
1789             Style.Standard == FormatStyle::LS_Cpp03) ||
1790            !(Left.isOneOf(tok::identifier, tok::l_paren, tok::r_paren) ||
1791              Left.isOneOf(TT_TemplateCloser, TT_TemplateOpener));
1792   if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
1793     return Style.SpacesInAngles;
1794   if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) ||
1795       Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr))
1796     return true;
1797   if (Left.is(TT_TemplateCloser) && Right.is(tok::l_paren))
1798     return Style.SpaceBeforeParens == FormatStyle::SBPO_Always;
1799   if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
1800       Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen))
1801     return false;
1802   if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
1803       Line.First->is(tok::hash))
1804     return true;
1805   if (Right.is(TT_TrailingUnaryOperator))
1806     return false;
1807   if (Left.is(TT_RegexLiteral))
1808     return false;
1809   return spaceRequiredBetween(Line, Left, Right);
1810 }
1811 
1812 // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
1813 static bool isAllmanBrace(const FormatToken &Tok) {
1814   return Tok.is(tok::l_brace) && Tok.BlockKind == BK_Block &&
1815          !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
1816 }
1817 
1818 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
1819                                      const FormatToken &Right) {
1820   const FormatToken &Left = *Right.Previous;
1821   if (Right.NewlinesBefore > 1)
1822     return true;
1823 
1824   // If the last token before a '}' is a comma or a trailing comment, the
1825   // intention is to insert a line break after it in order to make shuffling
1826   // around entries easier.
1827   const FormatToken *BeforeClosingBrace = nullptr;
1828   if (Left.is(tok::l_brace) && Left.BlockKind != BK_Block && Left.MatchingParen)
1829     BeforeClosingBrace = Left.MatchingParen->Previous;
1830   else if (Right.is(tok::r_brace) && Right.BlockKind != BK_Block)
1831     BeforeClosingBrace = &Left;
1832   if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
1833                              BeforeClosingBrace->isTrailingComment()))
1834     return true;
1835 
1836   if (Right.is(tok::comment)) {
1837     return Left.BlockKind != BK_BracedInit &&
1838            Left.isNot(TT_CtorInitializerColon) &&
1839            (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
1840   } else if (Right.Previous->isTrailingComment() ||
1841              (Right.isStringLiteral() && Right.Previous->isStringLiteral())) {
1842     return true;
1843   } else if (Right.Previous->IsUnterminatedLiteral) {
1844     return true;
1845   } else if (Right.is(tok::lessless) && Right.Next &&
1846              Right.Previous->is(tok::string_literal) &&
1847              Right.Next->is(tok::string_literal)) {
1848     return true;
1849   } else if (Right.Previous->ClosesTemplateDeclaration &&
1850              Right.Previous->MatchingParen &&
1851              Right.Previous->MatchingParen->NestingLevel == 0 &&
1852              Style.AlwaysBreakTemplateDeclarations) {
1853     return true;
1854   } else if ((Right.isOneOf(TT_CtorInitializerComma,
1855                             TT_CtorInitializerColon)) &&
1856              Style.BreakConstructorInitializersBeforeComma &&
1857              !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
1858     return true;
1859   } else if (Right.is(tok::string_literal) &&
1860              Right.TokenText.startswith("R\"")) {
1861     // Raw string literals are special wrt. line breaks. The author has made a
1862     // deliberate choice and might have aligned the contents of the string
1863     // literal accordingly. Thus, we try keep existing line breaks.
1864     return Right.NewlinesBefore > 0;
1865   } else if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 &&
1866              Style.Language == FormatStyle::LK_Proto) {
1867     // Don't put enums onto single lines in protocol buffers.
1868     return true;
1869   } else if (Style.Language == FormatStyle::LK_JavaScript &&
1870              Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
1871              !Left.Children.empty()) {
1872     // Support AllowShortFunctionsOnASingleLine for JavaScript.
1873     return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
1874            (Left.NestingLevel == 0 && Line.Level == 0 &&
1875             Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline);
1876   } else if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
1877     return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1878            Style.BreakBeforeBraces == FormatStyle::BS_GNU;
1879   } else if (Style.Language == FormatStyle::LK_Proto &&
1880              Left.isNot(tok::l_brace) && Right.is(TT_SelectorName)) {
1881     return true;
1882   } else if (Left.is(TT_ObjCBlockLBrace) &&
1883              !Style.AllowShortBlocksOnASingleLine) {
1884     return true;
1885   }
1886 
1887   if (Style.Language == FormatStyle::LK_JavaScript) {
1888     // FIXME: This might apply to other languages and token kinds.
1889     if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
1890         Left.Previous->is(tok::char_constant))
1891       return true;
1892     if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) &&
1893         Left.NestingLevel == 0)
1894       return true;
1895   } else if (Style.Language == FormatStyle::LK_Java) {
1896     if (Left.is(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
1897         Line.Last->is(tok::l_brace))
1898       return true;
1899     if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
1900         Right.Next->is(tok::string_literal))
1901       return true;
1902   }
1903 
1904   return false;
1905 }
1906 
1907 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
1908                                     const FormatToken &Right) {
1909   const FormatToken &Left = *Right.Previous;
1910 
1911   if (Style.Language == FormatStyle::LK_Java) {
1912     if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
1913                      Keywords.kw_implements))
1914       return false;
1915     if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
1916                       Keywords.kw_implements))
1917       return true;
1918   }
1919 
1920   if (Left.is(tok::at))
1921     return false;
1922   if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
1923     return false;
1924   if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
1925     return true;
1926   if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
1927       Right.is(tok::kw_operator))
1928     return true;
1929   if (Right.isTrailingComment())
1930     // We rely on MustBreakBefore being set correctly here as we should not
1931     // change the "binding" behavior of a comment.
1932     // The first comment in a braced lists is always interpreted as belonging to
1933     // the first list element. Otherwise, it should be placed outside of the
1934     // list.
1935     return Left.BlockKind == BK_BracedInit;
1936   if (Left.is(tok::question) && Right.is(tok::colon))
1937     return false;
1938   if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
1939     return Style.BreakBeforeTernaryOperators;
1940   if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
1941     return !Style.BreakBeforeTernaryOperators;
1942   if (Right.is(TT_InheritanceColon))
1943     return true;
1944   if (Right.is(tok::colon) &&
1945       !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon))
1946     return false;
1947   if (Left.is(tok::colon) && (Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)))
1948     return true;
1949   if (Right.is(TT_SelectorName))
1950     return true;
1951   if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
1952     return true;
1953   if (Left.ClosesTemplateDeclaration)
1954     return true;
1955   if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
1956                     TT_OverloadedOperator))
1957     return false;
1958   if (Left.is(TT_RangeBasedForLoopColon))
1959     return true;
1960   if (Right.is(TT_RangeBasedForLoopColon))
1961     return false;
1962   if (Left.isOneOf(TT_PointerOrReference, TT_TemplateCloser,
1963                    TT_UnaryOperator) ||
1964       Left.is(tok::kw_operator))
1965     return false;
1966   if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
1967     return false;
1968   if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen))
1969     return false;
1970   if (Left.is(tok::l_paren) && Left.Previous &&
1971       (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen) ||
1972        Left.Previous->is(tok::kw_if)))
1973     return false;
1974   if (Right.is(TT_ImplicitStringLiteral))
1975     return false;
1976 
1977   if (Right.is(tok::r_paren) || Right.is(TT_TemplateCloser))
1978     return false;
1979 
1980   // We only break before r_brace if there was a corresponding break before
1981   // the l_brace, which is tracked by BreakBeforeClosingBrace.
1982   if (Right.is(tok::r_brace))
1983     return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
1984 
1985   // Allow breaking after a trailing annotation, e.g. after a method
1986   // declaration.
1987   if (Left.is(TT_TrailingAnnotation))
1988     return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
1989                           tok::less, tok::coloncolon);
1990 
1991   if (Right.is(tok::kw___attribute))
1992     return true;
1993 
1994   if (Left.is(tok::identifier) && Right.is(tok::string_literal))
1995     return true;
1996 
1997   if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
1998     return true;
1999 
2000   if (Left.is(TT_CtorInitializerComma) &&
2001       Style.BreakConstructorInitializersBeforeComma)
2002     return false;
2003   if (Right.is(TT_CtorInitializerComma) &&
2004       Style.BreakConstructorInitializersBeforeComma)
2005     return true;
2006   if (Left.is(tok::greater) && Right.is(tok::greater) &&
2007       Left.isNot(TT_TemplateCloser))
2008     return false;
2009   if (Right.is(TT_BinaryOperator) &&
2010       Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
2011       (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
2012        Right.getPrecedence() != prec::Assignment))
2013     return true;
2014   if (Left.is(TT_ArrayInitializerLSquare))
2015     return true;
2016   if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
2017     return true;
2018   if (Left.isBinaryOperator() && !Left.isOneOf(tok::arrowstar, tok::lessless) &&
2019       Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
2020       (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
2021        Left.getPrecedence() == prec::Assignment))
2022     return true;
2023   return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
2024                       tok::kw_class, tok::kw_struct) ||
2025          Right.isMemberAccess() || Right.is(TT_TrailingReturnArrow) ||
2026          Right.isOneOf(tok::lessless, tok::colon, tok::l_square, tok::at) ||
2027          (Left.is(tok::r_paren) &&
2028           Right.isOneOf(tok::identifier, tok::kw_const)) ||
2029          (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
2030 }
2031 
2032 void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
2033   llvm::errs() << "AnnotatedTokens:\n";
2034   const FormatToken *Tok = Line.First;
2035   while (Tok) {
2036     llvm::errs() << " M=" << Tok->MustBreakBefore
2037                  << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
2038                  << " S=" << Tok->SpacesRequiredBefore
2039                  << " B=" << Tok->BlockParameterCount
2040                  << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
2041                  << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
2042                  << " FakeLParens=";
2043     for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
2044       llvm::errs() << Tok->FakeLParens[i] << "/";
2045     llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
2046     if (!Tok->Next)
2047       assert(Tok == Line.Last);
2048     Tok = Tok->Next;
2049   }
2050   llvm::errs() << "----\n";
2051 }
2052 
2053 } // namespace format
2054 } // namespace clang
2055