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 namespace clang {
21 namespace format {
22 
23 namespace {
24 
25 /// \brief A parser that gathers additional information about tokens.
26 ///
27 /// The \c TokenAnnotator tries to match parenthesis and square brakets and
28 /// store a parenthesis levels. It also tries to resolve matching "<" and ">"
29 /// into template parameter lists.
30 class AnnotatingParser {
31 public:
32   AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
33                    IdentifierInfo &Ident_in)
34       : Style(Style), Line(Line), CurrentToken(Line.First),
35         KeywordVirtualFound(false), AutoFound(false), Ident_in(Ident_in) {
36     Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
37   }
38 
39 private:
40   bool parseAngle() {
41     if (CurrentToken == NULL)
42       return false;
43     ScopedContextCreator ContextCreator(*this, tok::less, 10);
44     FormatToken *Left = CurrentToken->Previous;
45     Contexts.back().IsExpression = false;
46     while (CurrentToken != NULL) {
47       if (CurrentToken->is(tok::greater)) {
48         Left->MatchingParen = CurrentToken;
49         CurrentToken->MatchingParen = Left;
50         CurrentToken->Type = TT_TemplateCloser;
51         next();
52         return true;
53       }
54       if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
55                                 tok::question, tok::colon))
56         return false;
57       // If a && or || is found and interpreted as a binary operator, this set
58       // of angles is likely part of something like "a < b && c > d". If the
59       // angles are inside an expression, the ||/&& might also be a binary
60       // operator that was misinterpreted because we are parsing template
61       // parameters.
62       // FIXME: This is getting out of hand, write a decent parser.
63       if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
64           (CurrentToken->Previous->Type == TT_BinaryOperator ||
65            Contexts[Contexts.size() - 2].IsExpression) &&
66           Line.First->isNot(tok::kw_template))
67         return false;
68       updateParameterCount(Left, CurrentToken);
69       if (!consumeToken())
70         return false;
71     }
72     return false;
73   }
74 
75   bool parseParens(bool LookForDecls = false) {
76     if (CurrentToken == NULL)
77       return false;
78     ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
79 
80     // FIXME: This is a bit of a hack. Do better.
81     Contexts.back().ColonIsForRangeExpr =
82         Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
83 
84     bool StartsObjCMethodExpr = false;
85     FormatToken *Left = CurrentToken->Previous;
86     if (CurrentToken->is(tok::caret)) {
87       // ^( starts a block.
88       Left->Type = TT_ObjCBlockLParen;
89     } else if (FormatToken *MaybeSel = Left->Previous) {
90       // @selector( starts a selector.
91       if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
92           MaybeSel->Previous->is(tok::at)) {
93         StartsObjCMethodExpr = true;
94       }
95     }
96 
97     if (Left->Previous && Left->Previous->isOneOf(tok::kw_static_assert,
98                                                   tok::kw_if, tok::kw_while)) {
99       // static_assert, if and while usually contain expressions.
100       Contexts.back().IsExpression = true;
101     } else if (Left->Previous && Left->Previous->is(tok::r_square) &&
102                Left->Previous->MatchingParen &&
103                Left->Previous->MatchingParen->Type == TT_LambdaLSquare) {
104       // This is a parameter list of a lambda expression.
105       Contexts.back().IsExpression = false;
106     }
107 
108     if (StartsObjCMethodExpr) {
109       Contexts.back().ColonIsObjCMethodExpr = true;
110       Left->Type = TT_ObjCMethodExpr;
111     }
112 
113     bool MightBeFunctionType = CurrentToken->is(tok::star);
114     bool HasMultipleLines = false;
115     bool HasMultipleParametersOnALine = false;
116     while (CurrentToken != NULL) {
117       // LookForDecls is set when "if (" has been seen. Check for
118       // 'identifier' '*' 'identifier' followed by not '=' -- this
119       // '*' has to be a binary operator but determineStarAmpUsage() will
120       // categorize it as an unary operator, so set the right type here.
121       if (LookForDecls && CurrentToken->Next) {
122         FormatToken *Prev = CurrentToken->getPreviousNonComment();
123         if (Prev) {
124           FormatToken *PrevPrev = Prev->getPreviousNonComment();
125           FormatToken *Next = CurrentToken->Next;
126           if (PrevPrev && PrevPrev->is(tok::identifier) &&
127               Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
128               CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
129             Prev->Type = TT_BinaryOperator;
130             LookForDecls = false;
131           }
132         }
133       }
134 
135       if (CurrentToken->Previous->Type == TT_PointerOrReference &&
136           CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
137                                                     tok::coloncolon))
138         MightBeFunctionType = true;
139       if (CurrentToken->is(tok::r_paren)) {
140         if (MightBeFunctionType && CurrentToken->Next &&
141             (CurrentToken->Next->is(tok::l_paren) ||
142              (CurrentToken->Next->is(tok::l_square) &&
143               !Contexts.back().IsExpression)))
144           Left->Type = TT_FunctionTypeLParen;
145         Left->MatchingParen = CurrentToken;
146         CurrentToken->MatchingParen = Left;
147 
148         if (StartsObjCMethodExpr) {
149           CurrentToken->Type = TT_ObjCMethodExpr;
150           if (Contexts.back().FirstObjCSelectorName != NULL) {
151             Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
152                 Contexts.back().LongestObjCSelectorName;
153           }
154         }
155 
156         if (!HasMultipleLines)
157           Left->PackingKind = PPK_Inconclusive;
158         else if (HasMultipleParametersOnALine)
159           Left->PackingKind = PPK_BinPacked;
160         else
161           Left->PackingKind = PPK_OnePerLine;
162 
163         next();
164         return true;
165       }
166       if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
167         return false;
168       updateParameterCount(Left, CurrentToken);
169       if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
170           !CurrentToken->Next->HasUnescapedNewline &&
171           !CurrentToken->Next->isTrailingComment())
172         HasMultipleParametersOnALine = true;
173       if (!consumeToken())
174         return false;
175       if (CurrentToken && CurrentToken->HasUnescapedNewline)
176         HasMultipleLines = true;
177     }
178     return false;
179   }
180 
181   bool parseSquare() {
182     if (!CurrentToken)
183       return false;
184 
185     // A '[' could be an index subscript (after an identifier or after
186     // ')' or ']'), it could be the start of an Objective-C method
187     // expression, or it could the the start of an Objective-C array literal.
188     FormatToken *Left = CurrentToken->Previous;
189     FormatToken *Parent = Left->getPreviousNonComment();
190     bool StartsObjCMethodExpr =
191         Contexts.back().CanBeExpression && Left->Type != TT_LambdaLSquare &&
192         (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
193                                     tok::kw_return, tok::kw_throw) ||
194          Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn ||
195          Parent->Type == TT_CastRParen ||
196          getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
197     ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
198     Contexts.back().IsExpression = true;
199     bool ColonFound = false;
200 
201     if (StartsObjCMethodExpr) {
202       Contexts.back().ColonIsObjCMethodExpr = true;
203       Left->Type = TT_ObjCMethodExpr;
204     } else if (Parent && Parent->is(tok::at)) {
205       Left->Type = TT_ArrayInitializerLSquare;
206     } else if (Left->Type == TT_Unknown) {
207       Left->Type = TT_ArraySubscriptLSquare;
208     }
209 
210     while (CurrentToken != NULL) {
211       if (CurrentToken->is(tok::r_square)) {
212         if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
213             Left->Type == TT_ObjCMethodExpr) {
214           // An ObjC method call is rarely followed by an open parenthesis.
215           // FIXME: Do we incorrectly label ":" with this?
216           StartsObjCMethodExpr = false;
217           Left->Type = TT_Unknown;
218         }
219         if (StartsObjCMethodExpr) {
220           CurrentToken->Type = TT_ObjCMethodExpr;
221           // determineStarAmpUsage() thinks that '*' '[' is allocating an
222           // array of pointers, but if '[' starts a selector then '*' is a
223           // binary operator.
224           if (Parent != NULL && Parent->Type == TT_PointerOrReference)
225             Parent->Type = TT_BinaryOperator;
226         }
227         Left->MatchingParen = CurrentToken;
228         CurrentToken->MatchingParen = Left;
229         if (Contexts.back().FirstObjCSelectorName != NULL)
230           Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
231               Contexts.back().LongestObjCSelectorName;
232         next();
233         return true;
234       }
235       if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
236         return false;
237       if (CurrentToken->is(tok::colon))
238         ColonFound = true;
239       if (CurrentToken->is(tok::comma) &&
240           (Left->Type == TT_ArraySubscriptLSquare ||
241            (Left->Type == TT_ObjCMethodExpr && !ColonFound)))
242         Left->Type = TT_ArrayInitializerLSquare;
243       updateParameterCount(Left, CurrentToken);
244       if (!consumeToken())
245         return false;
246     }
247     return false;
248   }
249 
250   bool parseBrace() {
251     if (CurrentToken != NULL) {
252       FormatToken *Left = CurrentToken->Previous;
253       ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
254       Contexts.back().ColonIsDictLiteral = true;
255 
256       while (CurrentToken != NULL) {
257         if (CurrentToken->is(tok::r_brace)) {
258           Left->MatchingParen = CurrentToken;
259           CurrentToken->MatchingParen = Left;
260           next();
261           return true;
262         }
263         if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
264           return false;
265         updateParameterCount(Left, CurrentToken);
266         if (CurrentToken->is(tok::colon))
267           Left->Type = TT_DictLiteral;
268         if (!consumeToken())
269           return false;
270       }
271     }
272     // No closing "}" found, this probably starts a definition.
273     Line.StartsDefinition = true;
274     return true;
275   }
276 
277   void updateParameterCount(FormatToken *Left, FormatToken *Current) {
278     if (Current->is(tok::comma)) {
279       ++Left->ParameterCount;
280       if (!Left->Role)
281         Left->Role.reset(new CommaSeparatedList(Style));
282       Left->Role->CommaFound(Current);
283     } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
284       Left->ParameterCount = 1;
285     }
286   }
287 
288   bool parseConditional() {
289     while (CurrentToken != NULL) {
290       if (CurrentToken->is(tok::colon)) {
291         CurrentToken->Type = TT_ConditionalExpr;
292         next();
293         return true;
294       }
295       if (!consumeToken())
296         return false;
297     }
298     return false;
299   }
300 
301   bool parseTemplateDeclaration() {
302     if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
303       CurrentToken->Type = TT_TemplateOpener;
304       next();
305       if (!parseAngle())
306         return false;
307       if (CurrentToken != NULL)
308         CurrentToken->Previous->ClosesTemplateDeclaration = true;
309       return true;
310     }
311     return false;
312   }
313 
314   bool consumeToken() {
315     FormatToken *Tok = CurrentToken;
316     next();
317     switch (Tok->Tok.getKind()) {
318     case tok::plus:
319     case tok::minus:
320       if (Tok->Previous == NULL && Line.MustBeDeclaration)
321         Tok->Type = TT_ObjCMethodSpecifier;
322       break;
323     case tok::colon:
324       if (Tok->Previous == NULL)
325         return false;
326       // Colons from ?: are handled in parseConditional().
327       if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1) {
328         Tok->Type = TT_CtorInitializerColon;
329       } else if (Contexts.back().ColonIsDictLiteral) {
330         Tok->Type = TT_DictLiteral;
331       } else if (Contexts.back().ColonIsObjCMethodExpr ||
332                  Line.First->Type == TT_ObjCMethodSpecifier) {
333         Tok->Type = TT_ObjCMethodExpr;
334         Tok->Previous->Type = TT_ObjCSelectorName;
335         if (Tok->Previous->ColumnWidth >
336             Contexts.back().LongestObjCSelectorName) {
337           Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
338         }
339         if (Contexts.back().FirstObjCSelectorName == NULL)
340           Contexts.back().FirstObjCSelectorName = Tok->Previous;
341       } else if (Contexts.back().ColonIsForRangeExpr) {
342         Tok->Type = TT_RangeBasedForLoopColon;
343       } else if (CurrentToken != NULL &&
344                  CurrentToken->is(tok::numeric_constant)) {
345         Tok->Type = TT_BitFieldColon;
346       } else if (Contexts.size() == 1 && Line.First->isNot(tok::kw_enum)) {
347         Tok->Type = TT_InheritanceColon;
348       } else if (Contexts.back().ContextKind == tok::l_paren) {
349         Tok->Type = TT_InlineASMColon;
350       }
351       break;
352     case tok::kw_if:
353     case tok::kw_while:
354       if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
355         next();
356         if (!parseParens(/*LookForDecls=*/true))
357           return false;
358       }
359       break;
360     case tok::kw_for:
361       Contexts.back().ColonIsForRangeExpr = true;
362       next();
363       if (!parseParens())
364         return false;
365       break;
366     case tok::l_paren:
367       if (!parseParens())
368         return false;
369       if (Line.MustBeDeclaration && Contexts.size() == 1 &&
370           !Contexts.back().IsExpression)
371         Line.MightBeFunctionDecl = true;
372       break;
373     case tok::l_square:
374       if (!parseSquare())
375         return false;
376       break;
377     case tok::l_brace:
378       if (!parseBrace())
379         return false;
380       break;
381     case tok::less:
382       if (Tok->Previous && !Tok->Previous->Tok.isLiteral() && parseAngle())
383         Tok->Type = TT_TemplateOpener;
384       else {
385         Tok->Type = TT_BinaryOperator;
386         CurrentToken = Tok;
387         next();
388       }
389       break;
390     case tok::r_paren:
391     case tok::r_square:
392       return false;
393     case tok::r_brace:
394       // Lines can start with '}'.
395       if (Tok->Previous != NULL)
396         return false;
397       break;
398     case tok::greater:
399       Tok->Type = TT_BinaryOperator;
400       break;
401     case tok::kw_operator:
402       while (CurrentToken &&
403              !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
404         if (CurrentToken->isOneOf(tok::star, tok::amp))
405           CurrentToken->Type = TT_PointerOrReference;
406         consumeToken();
407         if (CurrentToken && CurrentToken->Previous->Type == TT_BinaryOperator)
408           CurrentToken->Previous->Type = TT_OverloadedOperator;
409       }
410       if (CurrentToken) {
411         CurrentToken->Type = TT_OverloadedOperatorLParen;
412         if (CurrentToken->Previous->Type == TT_BinaryOperator)
413           CurrentToken->Previous->Type = TT_OverloadedOperator;
414       }
415       break;
416     case tok::question:
417       parseConditional();
418       break;
419     case tok::kw_template:
420       parseTemplateDeclaration();
421       break;
422     case tok::identifier:
423       if (Line.First->is(tok::kw_for) &&
424           Tok->Tok.getIdentifierInfo() == &Ident_in)
425         Tok->Type = TT_ObjCForIn;
426       break;
427     case tok::comma:
428       if (Contexts.back().FirstStartOfName)
429         Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
430       if (Contexts.back().InCtorInitializer)
431         Tok->Type = TT_CtorInitializerComma;
432       break;
433     default:
434       break;
435     }
436     return true;
437   }
438 
439   void parseIncludeDirective() {
440     next();
441     if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
442       next();
443       while (CurrentToken != NULL) {
444         if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
445           CurrentToken->Type = TT_ImplicitStringLiteral;
446         next();
447       }
448     } else {
449       while (CurrentToken != NULL) {
450         if (CurrentToken->is(tok::string_literal))
451           // Mark these string literals as "implicit" literals, too, so that
452           // they are not split or line-wrapped.
453           CurrentToken->Type = TT_ImplicitStringLiteral;
454         next();
455       }
456     }
457   }
458 
459   void parseWarningOrError() {
460     next();
461     // We still want to format the whitespace left of the first token of the
462     // warning or error.
463     next();
464     while (CurrentToken != NULL) {
465       CurrentToken->Type = TT_ImplicitStringLiteral;
466       next();
467     }
468   }
469 
470   void parsePreprocessorDirective() {
471     next();
472     if (CurrentToken == NULL)
473       return;
474     if (CurrentToken->Tok.is(tok::numeric_constant)) {
475       CurrentToken->SpacesRequiredBefore = 1;
476       return;
477     }
478     // Hashes in the middle of a line can lead to any strange token
479     // sequence.
480     if (CurrentToken->Tok.getIdentifierInfo() == NULL)
481       return;
482     switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
483     case tok::pp_include:
484     case tok::pp_import:
485       parseIncludeDirective();
486       break;
487     case tok::pp_error:
488     case tok::pp_warning:
489       parseWarningOrError();
490       break;
491     case tok::pp_if:
492     case tok::pp_elif:
493       parseLine();
494       break;
495     default:
496       break;
497     }
498     while (CurrentToken != NULL)
499       next();
500   }
501 
502 public:
503   LineType parseLine() {
504     if (CurrentToken->is(tok::hash)) {
505       parsePreprocessorDirective();
506       return LT_PreprocessorDirective;
507     }
508     while (CurrentToken != NULL) {
509       if (CurrentToken->is(tok::kw_virtual))
510         KeywordVirtualFound = true;
511       if (!consumeToken())
512         return LT_Invalid;
513     }
514     if (KeywordVirtualFound)
515       return LT_VirtualFunctionDecl;
516 
517     if (Line.First->Type == TT_ObjCMethodSpecifier) {
518       if (Contexts.back().FirstObjCSelectorName != NULL)
519         Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
520             Contexts.back().LongestObjCSelectorName;
521       return LT_ObjCMethodDecl;
522     }
523 
524     return LT_Other;
525   }
526 
527 private:
528   void next() {
529     if (CurrentToken != NULL) {
530       determineTokenType(*CurrentToken);
531       CurrentToken->BindingStrength = Contexts.back().BindingStrength;
532     }
533 
534     if (CurrentToken != NULL)
535       CurrentToken = CurrentToken->Next;
536 
537     if (CurrentToken != NULL) {
538       // Reset token type in case we have already looked at it and then
539       // recovered from an error (e.g. failure to find the matching >).
540       if (CurrentToken->Type != TT_LambdaLSquare &&
541           CurrentToken->Type != TT_FunctionLBrace &&
542           CurrentToken->Type != TT_ImplicitStringLiteral)
543         CurrentToken->Type = TT_Unknown;
544       if (CurrentToken->Role)
545         CurrentToken->Role.reset(NULL);
546       CurrentToken->FakeLParens.clear();
547       CurrentToken->FakeRParens = 0;
548     }
549   }
550 
551   /// \brief A struct to hold information valid in a specific context, e.g.
552   /// a pair of parenthesis.
553   struct Context {
554     Context(tok::TokenKind ContextKind, unsigned BindingStrength,
555             bool IsExpression)
556         : ContextKind(ContextKind), BindingStrength(BindingStrength),
557           LongestObjCSelectorName(0), ColonIsForRangeExpr(false),
558           ColonIsDictLiteral(false), ColonIsObjCMethodExpr(false),
559           FirstObjCSelectorName(NULL), FirstStartOfName(NULL),
560           IsExpression(IsExpression), CanBeExpression(true),
561           InCtorInitializer(false) {}
562 
563     tok::TokenKind ContextKind;
564     unsigned BindingStrength;
565     unsigned LongestObjCSelectorName;
566     bool ColonIsForRangeExpr;
567     bool ColonIsDictLiteral;
568     bool ColonIsObjCMethodExpr;
569     FormatToken *FirstObjCSelectorName;
570     FormatToken *FirstStartOfName;
571     bool IsExpression;
572     bool CanBeExpression;
573     bool InCtorInitializer;
574   };
575 
576   /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
577   /// of each instance.
578   struct ScopedContextCreator {
579     AnnotatingParser &P;
580 
581     ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
582                          unsigned Increase)
583         : P(P) {
584       P.Contexts.push_back(Context(ContextKind,
585                                    P.Contexts.back().BindingStrength + Increase,
586                                    P.Contexts.back().IsExpression));
587     }
588 
589     ~ScopedContextCreator() { P.Contexts.pop_back(); }
590   };
591 
592   void determineTokenType(FormatToken &Current) {
593     if (Current.getPrecedence() == prec::Assignment &&
594         !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
595         (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
596       Contexts.back().IsExpression = true;
597       for (FormatToken *Previous = Current.Previous;
598            Previous && !Previous->isOneOf(tok::comma, tok::semi);
599            Previous = Previous->Previous) {
600         if (Previous->is(tok::r_square))
601           Previous = Previous->MatchingParen;
602         if (Previous->Type == TT_BinaryOperator &&
603             Previous->isOneOf(tok::star, tok::amp)) {
604           Previous->Type = TT_PointerOrReference;
605         }
606       }
607     } else if (Current.isOneOf(tok::kw_return, tok::kw_throw) ||
608                (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
609                 !Line.InPPDirective &&
610                 (!Current.Previous ||
611                  !Current.Previous->isOneOf(tok::kw_for, tok::kw_catch)))) {
612       Contexts.back().IsExpression = true;
613     } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
614       for (FormatToken *Previous = Current.Previous;
615            Previous && Previous->isOneOf(tok::star, tok::amp);
616            Previous = Previous->Previous)
617         Previous->Type = TT_PointerOrReference;
618     } else if (Current.Previous &&
619                Current.Previous->Type == TT_CtorInitializerColon) {
620       Contexts.back().IsExpression = true;
621       Contexts.back().InCtorInitializer = true;
622     } else if (Current.is(tok::kw_new)) {
623       Contexts.back().CanBeExpression = false;
624     } else if (Current.is(tok::semi) || Current.is(tok::exclaim)) {
625       // This should be the condition or increment in a for-loop.
626       Contexts.back().IsExpression = true;
627     }
628 
629     if (Current.Type == TT_Unknown) {
630       // Line.MightBeFunctionDecl can only be true after the parentheses of a
631       // function declaration have been found. In this case, 'Current' is a
632       // trailing token of this declaration and thus cannot be a name.
633       if (isStartOfName(Current) && !Line.MightBeFunctionDecl) {
634         Contexts.back().FirstStartOfName = &Current;
635         Current.Type = TT_StartOfName;
636       } else if (Current.is(tok::kw_auto)) {
637         AutoFound = true;
638       } else if (Current.is(tok::arrow) && AutoFound &&
639                  Line.MustBeDeclaration) {
640         Current.Type = TT_TrailingReturnArrow;
641       } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
642         Current.Type =
643             determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
644                                                Contexts.back().IsExpression);
645       } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
646         Current.Type = determinePlusMinusCaretUsage(Current);
647       } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
648         Current.Type = determineIncrementUsage(Current);
649       } else if (Current.is(tok::exclaim)) {
650         Current.Type = TT_UnaryOperator;
651       } else if (Current.isBinaryOperator() &&
652                  (!Current.Previous ||
653                   Current.Previous->isNot(tok::l_square))) {
654         Current.Type = TT_BinaryOperator;
655       } else if (Current.is(tok::comment)) {
656         if (Current.TokenText.startswith("//"))
657           Current.Type = TT_LineComment;
658         else
659           Current.Type = TT_BlockComment;
660       } else if (Current.is(tok::r_paren)) {
661         FormatToken *LeftOfParens = NULL;
662         if (Current.MatchingParen)
663           LeftOfParens = Current.MatchingParen->getPreviousNonComment();
664         bool IsCast = false;
665         bool ParensAreEmpty = Current.Previous == Current.MatchingParen;
666         bool ParensAreType = !Current.Previous ||
667                              Current.Previous->Type == TT_PointerOrReference ||
668                              Current.Previous->Type == TT_TemplateCloser ||
669                              isSimpleTypeSpecifier(*Current.Previous);
670         bool ParensCouldEndDecl =
671             Current.Next &&
672             Current.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
673         bool IsSizeOfOrAlignOf =
674             LeftOfParens &&
675             LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
676         if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
677             (Contexts.back().IsExpression ||
678              (Current.Next && Current.Next->isBinaryOperator())))
679           IsCast = true;
680         if (Current.Next && Current.Next->isNot(tok::string_literal) &&
681             (Current.Next->Tok.isLiteral() ||
682              Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
683           IsCast = true;
684         // If there is an identifier after the (), it is likely a cast, unless
685         // there is also an identifier before the ().
686         if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL ||
687                              LeftOfParens->is(tok::kw_return)) &&
688             LeftOfParens->Type != TT_OverloadedOperator &&
689             LeftOfParens->Type != TT_TemplateCloser && Current.Next &&
690             Current.Next->is(tok::identifier))
691           IsCast = true;
692         if (IsCast && !ParensAreEmpty)
693           Current.Type = TT_CastRParen;
694       } else if (Current.is(tok::at) && Current.Next) {
695         switch (Current.Next->Tok.getObjCKeywordID()) {
696         case tok::objc_interface:
697         case tok::objc_implementation:
698         case tok::objc_protocol:
699           Current.Type = TT_ObjCDecl;
700           break;
701         case tok::objc_property:
702           Current.Type = TT_ObjCProperty;
703           break;
704         default:
705           break;
706         }
707       } else if (Current.is(tok::period)) {
708         FormatToken *PreviousNoComment = Current.getPreviousNonComment();
709         if (PreviousNoComment &&
710             PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
711           Current.Type = TT_DesignatedInitializerPeriod;
712       }
713     }
714   }
715 
716   /// \brief Take a guess at whether \p Tok starts a name of a function or
717   /// variable declaration.
718   ///
719   /// This is a heuristic based on whether \p Tok is an identifier following
720   /// something that is likely a type.
721   bool isStartOfName(const FormatToken &Tok) {
722     if (Tok.isNot(tok::identifier) || Tok.Previous == NULL)
723       return false;
724 
725     // Skip "const" as it does not have an influence on whether this is a name.
726     FormatToken *PreviousNotConst = Tok.Previous;
727     while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const))
728       PreviousNotConst = PreviousNotConst->Previous;
729 
730     if (PreviousNotConst == NULL)
731       return false;
732 
733     bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
734                        PreviousNotConst->Previous &&
735                        PreviousNotConst->Previous->is(tok::hash);
736 
737     if (PreviousNotConst->Type == TT_TemplateCloser)
738       return PreviousNotConst && PreviousNotConst->MatchingParen &&
739              PreviousNotConst->MatchingParen->Previous &&
740              PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
741 
742     return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
743            PreviousNotConst->Type == TT_PointerOrReference ||
744            isSimpleTypeSpecifier(*PreviousNotConst);
745   }
746 
747   /// \brief Return the type of the given token assuming it is * or &.
748   TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression) {
749     const FormatToken *PrevToken = Tok.getPreviousNonComment();
750     if (PrevToken == NULL)
751       return TT_UnaryOperator;
752 
753     const FormatToken *NextToken = Tok.getNextNonComment();
754     if (NextToken == NULL)
755       return TT_Unknown;
756 
757     if (PrevToken->is(tok::coloncolon) ||
758         (PrevToken->is(tok::l_paren) && !IsExpression))
759       return TT_PointerOrReference;
760 
761     if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
762                            tok::comma, tok::semi, tok::kw_return, tok::colon,
763                            tok::equal, tok::kw_delete, tok::kw_sizeof) ||
764         PrevToken->Type == TT_BinaryOperator ||
765         PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
766       return TT_UnaryOperator;
767 
768     if (NextToken->is(tok::l_square))
769       return TT_PointerOrReference;
770 
771     if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
772         PrevToken->MatchingParen->Previous &&
773         PrevToken->MatchingParen->Previous->is(tok::kw_typeof))
774       return TT_PointerOrReference;
775 
776     if (PrevToken->Tok.isLiteral() ||
777         PrevToken->isOneOf(tok::r_paren, tok::r_square) ||
778         NextToken->Tok.isLiteral() || NextToken->isUnaryOperator())
779       return TT_BinaryOperator;
780 
781     // It is very unlikely that we are going to find a pointer or reference type
782     // definition on the RHS of an assignment.
783     if (IsExpression)
784       return TT_BinaryOperator;
785 
786     return TT_PointerOrReference;
787   }
788 
789   TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
790     const FormatToken *PrevToken = Tok.getPreviousNonComment();
791     if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
792       return TT_UnaryOperator;
793 
794     // Use heuristics to recognize unary operators.
795     if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
796                            tok::question, tok::colon, tok::kw_return,
797                            tok::kw_case, tok::at, tok::l_brace))
798       return TT_UnaryOperator;
799 
800     // There can't be two consecutive binary operators.
801     if (PrevToken->Type == TT_BinaryOperator)
802       return TT_UnaryOperator;
803 
804     // Fall back to marking the token as binary operator.
805     return TT_BinaryOperator;
806   }
807 
808   /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
809   TokenType determineIncrementUsage(const FormatToken &Tok) {
810     const FormatToken *PrevToken = Tok.getPreviousNonComment();
811     if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
812       return TT_UnaryOperator;
813     if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
814       return TT_TrailingUnaryOperator;
815 
816     return TT_UnaryOperator;
817   }
818 
819   // FIXME: This is copy&pasted from Sema. Put it in a common place and remove
820   // duplication.
821   /// \brief Determine whether the token kind starts a simple-type-specifier.
822   bool isSimpleTypeSpecifier(const FormatToken &Tok) const {
823     switch (Tok.Tok.getKind()) {
824     case tok::kw_short:
825     case tok::kw_long:
826     case tok::kw___int64:
827     case tok::kw___int128:
828     case tok::kw_signed:
829     case tok::kw_unsigned:
830     case tok::kw_void:
831     case tok::kw_char:
832     case tok::kw_int:
833     case tok::kw_half:
834     case tok::kw_float:
835     case tok::kw_double:
836     case tok::kw_wchar_t:
837     case tok::kw_bool:
838     case tok::kw___underlying_type:
839     case tok::annot_typename:
840     case tok::kw_char16_t:
841     case tok::kw_char32_t:
842     case tok::kw_typeof:
843     case tok::kw_decltype:
844       return true;
845     default:
846       return false;
847     }
848   }
849 
850   SmallVector<Context, 8> Contexts;
851 
852   const FormatStyle &Style;
853   AnnotatedLine &Line;
854   FormatToken *CurrentToken;
855   bool KeywordVirtualFound;
856   bool AutoFound;
857   IdentifierInfo &Ident_in;
858 };
859 
860 static int PrecedenceUnaryOperator = prec::PointerToMember + 1;
861 static int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
862 
863 /// \brief Parses binary expressions by inserting fake parenthesis based on
864 /// operator precedence.
865 class ExpressionParser {
866 public:
867   ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {
868     // Skip leading "}", e.g. in "} else if (...) {".
869     if (Current->is(tok::r_brace))
870       next();
871   }
872 
873   /// \brief Parse expressions with the given operatore precedence.
874   void parse(int Precedence = 0) {
875     // Skip 'return' and ObjC selector colons as they are not part of a binary
876     // expression.
877     while (Current &&
878            (Current->is(tok::kw_return) ||
879             (Current->is(tok::colon) && Current->Type == TT_ObjCMethodExpr)))
880       next();
881 
882     if (Current == NULL || Precedence > PrecedenceArrowAndPeriod)
883       return;
884 
885     // Conditional expressions need to be parsed separately for proper nesting.
886     if (Precedence == prec::Conditional) {
887       parseConditionalExpr();
888       return;
889     }
890 
891     // Parse unary operators, which all have a higher precedence than binary
892     // operators.
893     if (Precedence == PrecedenceUnaryOperator) {
894       parseUnaryOperator();
895       return;
896     }
897 
898     FormatToken *Start = Current;
899     FormatToken *LatestOperator = NULL;
900 
901     while (Current) {
902       // Consume operators with higher precedence.
903       parse(Precedence + 1);
904 
905       int CurrentPrecedence = getCurrentPrecedence();
906 
907       if (Current && Current->Type == TT_ObjCSelectorName &&
908           Precedence == CurrentPrecedence)
909         Start = Current;
910 
911       // At the end of the line or when an operator with higher precedence is
912       // found, insert fake parenthesis and return.
913       if (Current == NULL || Current->closesScope() ||
914           (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) {
915         if (LatestOperator) {
916           if (Precedence == PrecedenceArrowAndPeriod) {
917             LatestOperator->LastInChainOfCalls = true;
918             // Call expressions don't have a binary operator precedence.
919             addFakeParenthesis(Start, prec::Unknown);
920           } else {
921             addFakeParenthesis(Start, prec::Level(Precedence));
922           }
923         }
924         return;
925       }
926 
927       // Consume scopes: (), [], <> and {}
928       if (Current->opensScope()) {
929         while (Current && !Current->closesScope()) {
930           next();
931           parse();
932         }
933         next();
934       } else {
935         // Operator found.
936         if (CurrentPrecedence == Precedence)
937           LatestOperator = Current;
938 
939         next();
940       }
941     }
942   }
943 
944 private:
945   /// \brief Gets the precedence (+1) of the given token for binary operators
946   /// and other tokens that we treat like binary operators.
947   int getCurrentPrecedence() {
948     if (Current) {
949       if (Current->Type == TT_ConditionalExpr)
950         return prec::Conditional;
951       else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon ||
952                Current->Type == TT_ObjCSelectorName)
953         return 0;
954       else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
955         return Current->getPrecedence();
956       else if (Current->isOneOf(tok::period, tok::arrow))
957         return PrecedenceArrowAndPeriod;
958     }
959     return -1;
960   }
961 
962   void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
963     Start->FakeLParens.push_back(Precedence);
964     if (Precedence > prec::Unknown)
965       Start->StartsBinaryExpression = true;
966     if (Current) {
967       ++Current->Previous->FakeRParens;
968       if (Precedence > prec::Unknown)
969         Current->Previous->EndsBinaryExpression = true;
970     }
971   }
972 
973   /// \brief Parse unary operator expressions and surround them with fake
974   /// parentheses if appropriate.
975   void parseUnaryOperator() {
976     if (Current == NULL || Current->Type != TT_UnaryOperator) {
977       parse(PrecedenceArrowAndPeriod);
978       return;
979     }
980 
981     FormatToken *Start = Current;
982     next();
983     parseUnaryOperator();
984 
985     // The actual precedence doesn't matter.
986     addFakeParenthesis(Start, prec::Unknown);
987   }
988 
989   void parseConditionalExpr() {
990     FormatToken *Start = Current;
991     parse(prec::LogicalOr);
992     if (!Current || !Current->is(tok::question))
993       return;
994     next();
995     parse(prec::LogicalOr);
996     if (!Current || Current->Type != TT_ConditionalExpr)
997       return;
998     next();
999     parseConditionalExpr();
1000     addFakeParenthesis(Start, prec::Conditional);
1001   }
1002 
1003   void next() {
1004     if (Current)
1005       Current = Current->Next;
1006     while (Current && Current->isTrailingComment())
1007       Current = Current->Next;
1008   }
1009 
1010   FormatToken *Current;
1011 };
1012 
1013 } // end anonymous namespace
1014 
1015 void
1016 TokenAnnotator::setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines) {
1017   const AnnotatedLine *NextNonCommentLine = NULL;
1018   for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
1019                                                           E = Lines.rend();
1020        I != E; ++I) {
1021     if (NextNonCommentLine && (*I)->First->is(tok::comment) &&
1022         (*I)->First->Next == NULL)
1023       (*I)->Level = NextNonCommentLine->Level;
1024     else
1025       NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : NULL;
1026 
1027     setCommentLineLevels((*I)->Children);
1028   }
1029 }
1030 
1031 void TokenAnnotator::annotate(AnnotatedLine &Line) {
1032   for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1033                                                   E = Line.Children.end();
1034        I != E; ++I) {
1035     annotate(**I);
1036   }
1037   AnnotatingParser Parser(Style, Line, Ident_in);
1038   Line.Type = Parser.parseLine();
1039   if (Line.Type == LT_Invalid)
1040     return;
1041 
1042   ExpressionParser ExprParser(Line);
1043   ExprParser.parse();
1044 
1045   if (Line.First->Type == TT_ObjCMethodSpecifier)
1046     Line.Type = LT_ObjCMethodDecl;
1047   else if (Line.First->Type == TT_ObjCDecl)
1048     Line.Type = LT_ObjCDecl;
1049   else if (Line.First->Type == TT_ObjCProperty)
1050     Line.Type = LT_ObjCProperty;
1051 
1052   Line.First->SpacesRequiredBefore = 1;
1053   Line.First->CanBreakBefore = Line.First->MustBreakBefore;
1054 }
1055 
1056 void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
1057   Line.First->TotalLength =
1058       Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth;
1059   if (!Line.First->Next)
1060     return;
1061   FormatToken *Current = Line.First->Next;
1062   bool InFunctionDecl = Line.MightBeFunctionDecl;
1063   while (Current != NULL) {
1064     if (Current->Type == TT_LineComment)
1065       Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
1066     else if (Current->SpacesRequiredBefore == 0 &&
1067              spaceRequiredBefore(Line, *Current))
1068       Current->SpacesRequiredBefore = 1;
1069 
1070     Current->MustBreakBefore =
1071         Current->MustBreakBefore || mustBreakBefore(Line, *Current);
1072 
1073     Current->CanBreakBefore =
1074         Current->MustBreakBefore || canBreakBefore(Line, *Current);
1075     if (Current->MustBreakBefore || !Current->Children.empty() ||
1076         Current->IsMultiline)
1077       Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit;
1078     else
1079       Current->TotalLength = Current->Previous->TotalLength +
1080                              Current->ColumnWidth +
1081                              Current->SpacesRequiredBefore;
1082 
1083     if (Current->Type == TT_CtorInitializerColon)
1084       InFunctionDecl = false;
1085 
1086     // FIXME: Only calculate this if CanBreakBefore is true once static
1087     // initializers etc. are sorted out.
1088     // FIXME: Move magic numbers to a better place.
1089     Current->SplitPenalty = 20 * Current->BindingStrength +
1090                             splitPenalty(Line, *Current, InFunctionDecl);
1091 
1092     Current = Current->Next;
1093   }
1094 
1095   calculateUnbreakableTailLengths(Line);
1096   for (Current = Line.First; Current != NULL; Current = Current->Next) {
1097     if (Current->Role)
1098       Current->Role->precomputeFormattingInfos(Current);
1099   }
1100 
1101   DEBUG({ printDebugInfo(Line); });
1102 
1103   for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1104                                                   E = Line.Children.end();
1105        I != E; ++I) {
1106     calculateFormattingInformation(**I);
1107   }
1108 }
1109 
1110 void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1111   unsigned UnbreakableTailLength = 0;
1112   FormatToken *Current = Line.Last;
1113   while (Current != NULL) {
1114     Current->UnbreakableTailLength = UnbreakableTailLength;
1115     if (Current->CanBreakBefore ||
1116         Current->isOneOf(tok::comment, tok::string_literal)) {
1117       UnbreakableTailLength = 0;
1118     } else {
1119       UnbreakableTailLength +=
1120           Current->ColumnWidth + Current->SpacesRequiredBefore;
1121     }
1122     Current = Current->Previous;
1123   }
1124 }
1125 
1126 unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
1127                                       const FormatToken &Tok,
1128                                       bool InFunctionDecl) {
1129   const FormatToken &Left = *Tok.Previous;
1130   const FormatToken &Right = Tok;
1131 
1132   if (Left.is(tok::semi))
1133     return 0;
1134   if (Left.is(tok::comma))
1135     return 1;
1136   if (Right.is(tok::l_square))
1137     return 150;
1138 
1139   if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) {
1140     if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
1141       return 3;
1142     if (Left.Type == TT_StartOfName)
1143       return 20;
1144     if (InFunctionDecl && Right.BindingStrength == 1)
1145       // FIXME: Clean up hack of using BindingStrength to find top-level names.
1146       return Style.PenaltyReturnTypeOnItsOwnLine;
1147     return 200;
1148   }
1149   if (Left.is(tok::equal) && Right.is(tok::l_brace))
1150     return 150;
1151   if (Left.Type == TT_CastRParen)
1152     return 100;
1153   if (Left.is(tok::coloncolon))
1154     return 500;
1155   if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1156     return 5000;
1157 
1158   if (Left.Type == TT_RangeBasedForLoopColon ||
1159       Left.Type == TT_InheritanceColon)
1160     return 2;
1161 
1162   if (Right.isMemberAccess()) {
1163     if (Left.isOneOf(tok::r_paren, tok::r_square) && Left.MatchingParen &&
1164         Left.MatchingParen->ParameterCount > 0)
1165       return 20; // Should be smaller than breaking at a nested comma.
1166     return 150;
1167   }
1168 
1169   // Breaking before a trailing 'const' or not-function-like annotation is bad.
1170   if (Left.is(tok::r_paren) && Line.Type != LT_ObjCProperty &&
1171       (Right.is(tok::kw_const) || (Right.is(tok::identifier) && Right.Next &&
1172                                    Right.Next->isNot(tok::l_paren))))
1173     return 100;
1174 
1175   // In for-loops, prefer breaking at ',' and ';'.
1176   if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
1177     return 4;
1178 
1179   // In Objective-C method expressions, prefer breaking before "param:" over
1180   // breaking after it.
1181   if (Right.Type == TT_ObjCSelectorName)
1182     return 0;
1183   if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
1184     return Line.MightBeFunctionDecl ? 50 : 500;
1185 
1186   if (Left.is(tok::l_paren) && InFunctionDecl)
1187     return 100;
1188   if (Left.opensScope())
1189     return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
1190                                    : 19;
1191 
1192   if (Right.is(tok::lessless)) {
1193     if (Left.is(tok::string_literal)) {
1194       StringRef Content = Left.TokenText;
1195       if (Content.startswith("\""))
1196         Content = Content.drop_front(1);
1197       if (Content.endswith("\""))
1198         Content = Content.drop_back(1);
1199       Content = Content.trim();
1200       if (Content.size() > 1 &&
1201           (Content.back() == ':' || Content.back() == '='))
1202         return 25;
1203     }
1204     return 1; // Breaking at a << is really cheap.
1205   }
1206   if (Left.Type == TT_ConditionalExpr)
1207     return prec::Conditional;
1208   prec::Level Level = Left.getPrecedence();
1209 
1210   if (Level != prec::Unknown)
1211     return Level;
1212 
1213   return 3;
1214 }
1215 
1216 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
1217                                           const FormatToken &Left,
1218                                           const FormatToken &Right) {
1219   if (Right.is(tok::hashhash))
1220     return Left.is(tok::hash);
1221   if (Left.isOneOf(tok::hashhash, tok::hash))
1222     return Right.is(tok::hash);
1223   if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1224     return Style.SpaceInEmptyParentheses;
1225   if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
1226     return (Right.Type == TT_CastRParen ||
1227             (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen))
1228                ? Style.SpacesInCStyleCastParentheses
1229                : Style.SpacesInParentheses;
1230   if (Style.SpacesInAngles &&
1231       ((Left.Type == TT_TemplateOpener) != (Right.Type == TT_TemplateCloser)))
1232     return true;
1233   if (Right.isOneOf(tok::semi, tok::comma))
1234     return false;
1235   if (Right.is(tok::less) &&
1236       (Left.is(tok::kw_template) ||
1237        (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1238     return true;
1239   if (Left.is(tok::arrow) || Right.is(tok::arrow))
1240     return false;
1241   if (Left.isOneOf(tok::exclaim, tok::tilde))
1242     return false;
1243   if (Left.is(tok::at) &&
1244       Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1245                     tok::numeric_constant, tok::l_paren, tok::l_brace,
1246                     tok::kw_true, tok::kw_false))
1247     return false;
1248   if (Left.is(tok::coloncolon))
1249     return false;
1250   if (Right.is(tok::coloncolon))
1251     return (Left.is(tok::less) && Style.Standard == FormatStyle::LS_Cpp03) ||
1252            !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren,
1253                          tok::r_paren, tok::less);
1254   if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
1255     return false;
1256   if (Right.is(tok::ellipsis))
1257     return Left.Tok.isLiteral();
1258   if (Left.is(tok::l_square) && Right.is(tok::amp))
1259     return false;
1260   if (Right.Type == TT_PointerOrReference)
1261     return Left.Tok.isLiteral() ||
1262            ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
1263             !Style.PointerBindsToType);
1264   if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
1265       (Left.Type != TT_PointerOrReference || Style.PointerBindsToType))
1266     return true;
1267   if (Left.Type == TT_PointerOrReference)
1268     return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
1269            ((Right.Type != TT_PointerOrReference) &&
1270             Right.isNot(tok::l_paren) && Style.PointerBindsToType &&
1271             Left.Previous &&
1272             !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
1273   if (Right.is(tok::star) && Left.is(tok::l_paren))
1274     return false;
1275   if (Left.is(tok::l_square))
1276     return Left.Type == TT_ArrayInitializerLSquare &&
1277            Right.isNot(tok::r_square);
1278   if (Right.is(tok::r_square))
1279     return Right.MatchingParen &&
1280            Right.MatchingParen->Type == TT_ArrayInitializerLSquare;
1281   if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr &&
1282       Right.Type != TT_LambdaLSquare && Left.isNot(tok::numeric_constant))
1283     return false;
1284   if (Left.is(tok::colon))
1285     return Left.Type != TT_ObjCMethodExpr;
1286   if (Right.is(tok::colon))
1287     return Right.Type != TT_ObjCMethodExpr && !Left.is(tok::question);
1288   if (Right.is(tok::l_paren)) {
1289     if (Left.is(tok::r_paren) && Left.MatchingParen &&
1290         Left.MatchingParen->Previous &&
1291         Left.MatchingParen->Previous->is(tok::kw___attribute))
1292       return true;
1293     return Line.Type == LT_ObjCDecl ||
1294            Left.isOneOf(tok::kw_return, tok::kw_new, tok::kw_delete,
1295                         tok::semi) ||
1296            (Style.SpaceAfterControlStatementKeyword &&
1297             Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
1298                          tok::kw_catch));
1299   }
1300   if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
1301     return false;
1302   if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
1303     return !Left.Children.empty(); // No spaces in "{}".
1304   if (Left.is(tok::l_brace) || Right.is(tok::r_brace))
1305     return !Style.Cpp11BracedListStyle;
1306   if (Left.Type == TT_BlockComment && Left.TokenText.endswith("=*/"))
1307     return false;
1308   if (Right.Type == TT_UnaryOperator)
1309     return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1310            (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
1311   if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) &&
1312       Right.is(tok::l_brace) && Right.getNextNonComment() &&
1313       Right.BlockKind != BK_Block)
1314     return false;
1315   if (Left.is(tok::period) || Right.is(tok::period))
1316     return false;
1317   if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
1318     return false;
1319   return true;
1320 }
1321 
1322 bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
1323                                          const FormatToken &Tok) {
1324   if (Tok.Tok.getIdentifierInfo() && Tok.Previous->Tok.getIdentifierInfo())
1325     return true; // Never ever merge two identifiers.
1326   if (Tok.Previous->Type == TT_ImplicitStringLiteral)
1327     return Tok.WhitespaceRange.getBegin() != Tok.WhitespaceRange.getEnd();
1328   if (Line.Type == LT_ObjCMethodDecl) {
1329     if (Tok.Previous->Type == TT_ObjCMethodSpecifier)
1330       return true;
1331     if (Tok.Previous->is(tok::r_paren) && Tok.is(tok::identifier))
1332       // Don't space between ')' and <id>
1333       return false;
1334   }
1335   if (Line.Type == LT_ObjCProperty &&
1336       (Tok.is(tok::equal) || Tok.Previous->is(tok::equal)))
1337     return false;
1338 
1339   if (Tok.Type == TT_TrailingReturnArrow ||
1340       Tok.Previous->Type == TT_TrailingReturnArrow)
1341     return true;
1342   if (Tok.Previous->is(tok::comma))
1343     return true;
1344   if (Tok.is(tok::comma))
1345     return false;
1346   if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
1347     return true;
1348   if (Tok.Previous->Tok.is(tok::kw_operator))
1349     return Tok.is(tok::coloncolon);
1350   if (Tok.Type == TT_OverloadedOperatorLParen)
1351     return false;
1352   if (Tok.is(tok::colon))
1353     return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
1354            Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr &&
1355            !Tok.Previous->is(tok::question);
1356   if (Tok.Previous->Type == TT_UnaryOperator ||
1357       Tok.Previous->Type == TT_CastRParen)
1358     return false;
1359   if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) {
1360     return Tok.Type == TT_TemplateCloser &&
1361            Tok.Previous->Type == TT_TemplateCloser &&
1362            (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
1363   }
1364   if (Tok.isOneOf(tok::arrowstar, tok::periodstar) ||
1365       Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar))
1366     return false;
1367   if (!Style.SpaceBeforeAssignmentOperators &&
1368       Tok.getPrecedence() == prec::Assignment)
1369     return false;
1370   if ((Tok.Type == TT_BinaryOperator && !Tok.Previous->is(tok::l_paren)) ||
1371       Tok.Previous->Type == TT_BinaryOperator)
1372     return true;
1373   if (Tok.Previous->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
1374     return false;
1375   if (Tok.is(tok::less) && Tok.Previous->isNot(tok::l_paren) &&
1376       Line.First->is(tok::hash))
1377     return true;
1378   if (Tok.Type == TT_TrailingUnaryOperator)
1379     return false;
1380   return spaceRequiredBetween(Line, *Tok.Previous, Tok);
1381 }
1382 
1383 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
1384                                      const FormatToken &Right) {
1385   if (Right.is(tok::comment)) {
1386     return Right.NewlinesBefore > 0;
1387   } else if (Right.Previous->isTrailingComment() ||
1388              (Right.is(tok::string_literal) &&
1389               Right.Previous->is(tok::string_literal))) {
1390     return true;
1391   } else if (Right.Previous->IsUnterminatedLiteral) {
1392     return true;
1393   } else if (Right.is(tok::lessless) && Right.Next &&
1394              Right.Previous->is(tok::string_literal) &&
1395              Right.Next->is(tok::string_literal)) {
1396     return true;
1397   } else if (Right.Previous->ClosesTemplateDeclaration &&
1398              Right.Previous->MatchingParen &&
1399              Right.Previous->MatchingParen->BindingStrength == 1 &&
1400              Style.AlwaysBreakTemplateDeclarations) {
1401     // FIXME: Fix horrible hack of using BindingStrength to find top-level <>.
1402     return true;
1403   } else if (Right.Type == TT_CtorInitializerComma &&
1404              Style.BreakConstructorInitializersBeforeComma &&
1405              !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
1406     return true;
1407   } else if (Right.Previous->BlockKind == BK_Block &&
1408              Right.Previous->isNot(tok::r_brace) && Right.isNot(tok::r_brace)) {
1409     return true;
1410   } else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) {
1411     return Style.BreakBeforeBraces == FormatStyle::BS_Allman;
1412   }
1413   return false;
1414 }
1415 
1416 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
1417                                     const FormatToken &Right) {
1418   const FormatToken &Left = *Right.Previous;
1419   if (Left.is(tok::at))
1420     return false;
1421   if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator))
1422     return true;
1423   if (Right.isTrailingComment())
1424     // We rely on MustBreakBefore being set correctly here as we should not
1425     // change the "binding" behavior of a comment.
1426     return false;
1427   if (Left.is(tok::question) && Right.is(tok::colon))
1428     return false;
1429   if (Right.Type == TT_ConditionalExpr || Right.is(tok::question))
1430     return Style.BreakBeforeTernaryOperators;
1431   if (Left.Type == TT_ConditionalExpr || Left.is(tok::question))
1432     return !Style.BreakBeforeTernaryOperators;
1433   if (Right.is(tok::colon) &&
1434       (Right.Type == TT_DictLiteral || Right.Type == TT_ObjCMethodExpr))
1435     return false;
1436   if (Left.is(tok::colon) &&
1437       (Left.Type == TT_DictLiteral || Left.Type == TT_ObjCMethodExpr))
1438     return true;
1439   if (Right.Type == TT_ObjCSelectorName)
1440     return true;
1441   if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
1442     return true;
1443   if (Left.ClosesTemplateDeclaration)
1444     return true;
1445   if (Right.Type == TT_RangeBasedForLoopColon ||
1446       Right.Type == TT_OverloadedOperatorLParen ||
1447       Right.Type == TT_OverloadedOperator)
1448     return false;
1449   if (Left.Type == TT_RangeBasedForLoopColon)
1450     return true;
1451   if (Right.Type == TT_RangeBasedForLoopColon)
1452     return false;
1453   if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
1454       Left.Type == TT_UnaryOperator || Left.is(tok::kw_operator))
1455     return false;
1456   if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
1457     return false;
1458   if (Left.Previous) {
1459     if (Left.is(tok::l_paren) && Right.is(tok::l_paren) &&
1460         Left.Previous->is(tok::kw___attribute))
1461       return false;
1462     if (Left.is(tok::l_paren) && (Left.Previous->Type == TT_BinaryOperator ||
1463                                   Left.Previous->Type == TT_CastRParen))
1464       return false;
1465   }
1466   if (Right.Type == TT_ImplicitStringLiteral)
1467     return false;
1468 
1469   if (Right.is(tok::r_paren) || Right.Type == TT_TemplateCloser)
1470     return false;
1471 
1472   // We only break before r_brace if there was a corresponding break before
1473   // the l_brace, which is tracked by BreakBeforeClosingBrace.
1474   if (Right.is(tok::r_brace))
1475     return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
1476 
1477   // Allow breaking after a trailing 'const', e.g. after a method declaration,
1478   // unless it is follow by ';', '{' or '='.
1479   if (Left.is(tok::kw_const) && Left.Previous != NULL &&
1480       Left.Previous->is(tok::r_paren))
1481     return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal);
1482 
1483   if (Right.is(tok::kw___attribute))
1484     return true;
1485 
1486   if (Left.is(tok::identifier) && Right.is(tok::string_literal))
1487     return true;
1488 
1489   if (Left.Type == TT_CtorInitializerComma &&
1490       Style.BreakConstructorInitializersBeforeComma)
1491     return false;
1492   if (Right.Type == TT_CtorInitializerComma &&
1493       Style.BreakConstructorInitializersBeforeComma)
1494     return true;
1495   if (Right.Type == TT_BinaryOperator && Style.BreakBeforeBinaryOperators)
1496     return true;
1497   if (Left.is(tok::greater) && Right.is(tok::greater) &&
1498       Left.Type != TT_TemplateCloser)
1499     return false;
1500   if (Left.Type == TT_ArrayInitializerLSquare)
1501     return true;
1502   return (Left.isBinaryOperator() && Left.isNot(tok::lessless) &&
1503           !Style.BreakBeforeBinaryOperators) ||
1504          Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
1505                       tok::kw_class, tok::kw_struct) ||
1506          Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon,
1507                        tok::l_square, tok::at) ||
1508          (Left.is(tok::r_paren) &&
1509           Right.isOneOf(tok::identifier, tok::kw_const, tok::kw___attribute)) ||
1510          (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
1511 }
1512 
1513 void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
1514   llvm::errs() << "AnnotatedTokens:\n";
1515   const FormatToken *Tok = Line.First;
1516   while (Tok) {
1517     llvm::errs() << " M=" << Tok->MustBreakBefore
1518                  << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
1519                  << " S=" << Tok->SpacesRequiredBefore
1520                  << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
1521                  << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
1522                  << " FakeLParens=";
1523     for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
1524       llvm::errs() << Tok->FakeLParens[i] << "/";
1525     llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
1526     if (Tok->Next == NULL)
1527       assert(Tok == Line.Last);
1528     Tok = Tok->Next;
1529   }
1530   llvm::errs() << "----\n";
1531 }
1532 
1533 } // namespace format
1534 } // namespace clang
1535