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