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