Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/TokenAnnotator.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- TokenAnnotator.cpp - Format C++ code -----------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
///
9
/// \file
10
/// This file implements a token annotator, i.e. creates
11
/// \c AnnotatedTokens out of \c FormatTokens with required extra information.
12
///
13
//===----------------------------------------------------------------------===//
14
15
#include "TokenAnnotator.h"
16
#include "FormatToken.h"
17
#include "clang/Basic/SourceManager.h"
18
#include "clang/Basic/TokenKinds.h"
19
#include "llvm/ADT/SmallPtrSet.h"
20
#include "llvm/Support/Debug.h"
21
22
#define DEBUG_TYPE "format-token-annotator"
23
24
namespace clang {
25
namespace format {
26
27
namespace {
28
29
/// Returns \c true if the token can be used as an identifier in
30
/// an Objective-C \c @selector, \c false otherwise.
31
///
32
/// Because getFormattingLangOpts() always lexes source code as
33
/// Objective-C++, C++ keywords like \c new and \c delete are
34
/// lexed as tok::kw_*, not tok::identifier, even for Objective-C.
35
///
36
/// For Objective-C and Objective-C++, both identifiers and keywords
37
/// are valid inside @selector(...) (or a macro which
38
/// invokes @selector(...)). So, we allow treat any identifier or
39
/// keyword as a potential Objective-C selector component.
40
371k
static bool canBeObjCSelectorComponent(const FormatToken &Tok) {
41
371k
  return Tok.Tok.getIdentifierInfo() != nullptr;
42
371k
}
43
44
/// With `Left` being '(', check if we're at either `[...](` or
45
/// `[...]<...>(`, where the [ opens a lambda capture list.
46
36.4k
static bool isLambdaParameterList(const FormatToken *Left) {
47
36.4k
  // Skip <...> if present.
48
36.4k
  if (Left->Previous && 
Left->Previous->is(tok::greater)36.0k
&&
49
36.4k
      
Left->Previous->MatchingParen637
&&
50
36.4k
      
Left->Previous->MatchingParen->is(TT_TemplateOpener)637
)
51
637
    Left = Left->Previous->MatchingParen;
52
36.4k
53
36.4k
  // Check for `[...]`.
54
36.4k
  return Left->Previous && 
Left->Previous->is(tok::r_square)36.0k
&&
55
36.4k
         
Left->Previous->MatchingParen1.30k
&&
56
36.4k
         
Left->Previous->MatchingParen->is(TT_LambdaLSquare)1.30k
;
57
36.4k
}
58
59
/// A parser that gathers additional information about tokens.
60
///
61
/// The \c TokenAnnotator tries to match parenthesis and square brakets and
62
/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
63
/// into template parameter lists.
64
class AnnotatingParser {
65
public:
66
  AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
67
                   const AdditionalKeywords &Keywords)
68
      : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
69
133k
        Keywords(Keywords) {
70
133k
    Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
71
133k
    resetTokenMetadata(CurrentToken);
72
133k
  }
73
74
private:
75
7.61k
  bool parseAngle() {
76
7.61k
    if (!CurrentToken || 
!CurrentToken->Previous7.60k
)
77
9
      return false;
78
7.60k
    if (NonTemplateLess.count(CurrentToken->Previous))
79
17
      return false;
80
7.59k
81
7.59k
    const FormatToken &Previous = *CurrentToken->Previous; // The '<'.
82
7.59k
    if (Previous.Previous) {
83
7.58k
      if (Previous.Previous->Tok.isLiteral())
84
84
        return false;
85
7.50k
      if (Previous.Previous->is(tok::r_paren) && 
Contexts.size() > 181
&&
86
7.50k
          
(49
!Previous.Previous->MatchingParen49
||
87
49
           !Previous.Previous->MatchingParen->is(TT_OverloadedOperatorLParen)))
88
35
        return false;
89
7.47k
    }
90
7.47k
91
7.47k
    FormatToken *Left = CurrentToken->Previous;
92
7.47k
    Left->ParentBracket = Contexts.back().ContextKind;
93
7.47k
    ScopedContextCreator ContextCreator(*this, tok::less, 12);
94
7.47k
95
7.47k
    // If this angle is in the context of an expression, we need to be more
96
7.47k
    // hesitant to detect it as opening template parameters.
97
7.47k
    bool InExprContext = Contexts.back().IsExpression;
98
7.47k
99
7.47k
    Contexts.back().IsExpression = false;
100
7.47k
    // If there's a template keyword before the opening angle bracket, this is a
101
7.47k
    // template parameter, not an argument.
102
7.47k
    Contexts.back().InTemplateArgument =
103
7.47k
        Left->Previous && 
Left->Previous->Tok.isNot(tok::kw_template)7.46k
;
104
7.47k
105
7.47k
    if (Style.Language == FormatStyle::LK_Java &&
106
7.47k
        
CurrentToken->is(tok::question)81
)
107
10
      next();
108
7.47k
109
25.1k
    while (CurrentToken) {
110
25.1k
      if (CurrentToken->is(tok::greater)) {
111
7.23k
        Left->MatchingParen = CurrentToken;
112
7.23k
        CurrentToken->MatchingParen = Left;
113
7.23k
        // In TT_Proto, we must distignuish between:
114
7.23k
        //   map<key, value>
115
7.23k
        //   msg < item: data >
116
7.23k
        //   msg: < item: data >
117
7.23k
        // In TT_TextProto, map<key, value> does not occur.
118
7.23k
        if (Style.Language == FormatStyle::LK_TextProto ||
119
7.23k
            
(7.02k
Style.Language == FormatStyle::LK_Proto7.02k
&&
Left->Previous96
&&
120
7.02k
             
Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral)96
))
121
273
          CurrentToken->Type = TT_DictLiteral;
122
6.96k
        else
123
6.96k
          CurrentToken->Type = TT_TemplateCloser;
124
7.23k
        next();
125
7.23k
        return true;
126
7.23k
      }
127
17.8k
      if (CurrentToken->is(tok::question) &&
128
17.8k
          
Style.Language == FormatStyle::LK_Java35
) {
129
4
        next();
130
4
        continue;
131
4
      }
132
17.8k
      if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
133
17.8k
          
(17.7k
CurrentToken->isOneOf(tok::colon, tok::question)17.7k
&&
InExprContext404
&&
134
17.7k
           
Style.Language != FormatStyle::LK_Proto163
&&
135
17.7k
           
Style.Language != FormatStyle::LK_TextProto45
))
136
158
        return false;
137
17.7k
      // If a && or || is found and interpreted as a binary operator, this set
138
17.7k
      // of angles is likely part of something like "a < b && c > d". If the
139
17.7k
      // angles are inside an expression, the ||/&& might also be a binary
140
17.7k
      // operator that was misinterpreted because we are parsing template
141
17.7k
      // parameters.
142
17.7k
      // FIXME: This is getting out of hand, write a decent parser.
143
17.7k
      if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
144
17.7k
          
CurrentToken->Previous->is(TT_BinaryOperator)161
&&
145
17.7k
          
Contexts[Contexts.size() - 2].IsExpression133
&&
146
17.7k
          
!Line.startsWith(tok::kw_template)28
)
147
28
        return false;
148
17.7k
      updateParameterCount(Left, CurrentToken);
149
17.7k
      if (Style.Language == FormatStyle::LK_Proto) {
150
700
        if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) {
151
700
          if (CurrentToken->is(tok::colon) ||
152
700
              
(536
CurrentToken->isOneOf(tok::l_brace, tok::less)536
&&
153
536
               
Previous->isNot(tok::colon)30
))
154
182
            Previous->Type = TT_SelectorName;
155
700
        }
156
700
      }
157
17.7k
      if (!consumeToken())
158
0
        return false;
159
17.7k
    }
160
7.47k
    
return false46
;
161
7.47k
  }
162
163
42.6k
  bool parseParens(bool LookForDecls = false) {
164
42.6k
    if (!CurrentToken)
165
12
      return false;
166
42.6k
    FormatToken *Left = CurrentToken->Previous;
167
42.6k
    Left->ParentBracket = Contexts.back().ContextKind;
168
42.6k
    ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
169
42.6k
170
42.6k
    // FIXME: This is a bit of a hack. Do better.
171
42.6k
    Contexts.back().ColonIsForRangeExpr =
172
42.6k
        Contexts.size() == 2 && 
Contexts[0].ColonIsForRangeExpr36.9k
;
173
42.6k
174
42.6k
    bool StartsObjCMethodExpr = false;
175
42.6k
    if (FormatToken *MaybeSel = Left->Previous) {
176
42.2k
      // @selector( starts a selector.
177
42.2k
      if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && 
MaybeSel->Previous18
&&
178
42.2k
          
MaybeSel->Previous->is(tok::at)18
) {
179
18
        StartsObjCMethodExpr = true;
180
18
      }
181
42.2k
    }
182
42.6k
183
42.6k
    if (Left->is(TT_OverloadedOperatorLParen)) {
184
1.44k
      Contexts.back().IsExpression = false;
185
41.2k
    } else if (Style.Language == FormatStyle::LK_JavaScript &&
186
41.2k
               
(2.05k
Line.startsWith(Keywords.kw_type, tok::identifier)2.05k
||
187
2.05k
                Line.startsWith(tok::kw_export, Keywords.kw_type,
188
2.03k
                                tok::identifier))) {
189
20
      // type X = (...);
190
20
      // export type X = (...);
191
20
      Contexts.back().IsExpression = false;
192
41.2k
    } else if (Left->Previous &&
193
41.2k
               
(40.7k
Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype,
194
40.7k
                                        tok::kw_while, tok::l_paren,
195
40.7k
                                        tok::comma) ||
196
40.7k
                
Left->Previous->isIf()39.7k
||
197
40.7k
                
Left->Previous->is(TT_BinaryOperator)37.5k
)) {
198
4.20k
      // static_assert, if and while usually contain expressions.
199
4.20k
      Contexts.back().IsExpression = true;
200
37.0k
    } else if (Style.Language == FormatStyle::LK_JavaScript && 
Left->Previous1.85k
&&
201
37.0k
               
(1.81k
Left->Previous->is(Keywords.kw_function)1.81k
||
202
1.81k
                (Left->Previous->endsSequence(tok::identifier,
203
1.55k
                                              Keywords.kw_function)))) {
204
500
      // function(...) or function f(...)
205
500
      Contexts.back().IsExpression = false;
206
36.5k
    } else if (Style.Language == FormatStyle::LK_JavaScript && 
Left->Previous1.35k
&&
207
36.5k
               
Left->Previous->is(TT_JsTypeColon)1.31k
) {
208
8
      // let x: (SomeType);
209
8
      Contexts.back().IsExpression = false;
210
36.4k
    } else if (isLambdaParameterList(Left)) {
211
1.22k
      // This is a parameter list of a lambda expression.
212
1.22k
      Contexts.back().IsExpression = false;
213
35.2k
    } else if (Line.InPPDirective &&
214
35.2k
               
(1.73k
!Left->Previous1.73k
||
!Left->Previous->is(tok::identifier)1.47k
)) {
215
306
      Contexts.back().IsExpression = true;
216
34.9k
    } else if (Contexts[Contexts.size() - 2].CaretFound) {
217
150
      // This is the parameter list of an ObjC block.
218
150
      Contexts.back().IsExpression = false;
219
34.8k
    } else if (Left->Previous && 
Left->Previous->is(tok::kw___attribute)34.6k
) {
220
77
      Left->Type = TT_AttributeParen;
221
34.7k
    } else if (Left->Previous && 
Left->Previous->is(TT_ForEachMacro)34.5k
) {
222
25
      // The first argument to a foreach macro is a declaration.
223
25
      Contexts.back().IsForEachMacro = true;
224
25
      Contexts.back().IsExpression = false;
225
34.7k
    } else if (Left->Previous && 
Left->Previous->MatchingParen34.5k
&&
226
34.7k
               
Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)1.48k
) {
227
115
      Contexts.back().IsExpression = false;
228
34.5k
    } else if (!Line.MustBeDeclaration && 
!Line.InPPDirective7.36k
) {
229
5.94k
      bool IsForOrCatch =
230
5.94k
          Left->Previous && 
Left->Previous->isOneOf(tok::kw_for, tok::kw_catch)5.84k
;
231
5.94k
      Contexts.back().IsExpression = !IsForOrCatch;
232
5.94k
    }
233
42.6k
234
42.6k
    if (StartsObjCMethodExpr) {
235
18
      Contexts.back().ColonIsObjCMethodExpr = true;
236
18
      Left->Type = TT_ObjCMethodExpr;
237
18
    }
238
42.6k
239
42.6k
    // MightBeFunctionType and ProbablyFunctionType are used for
240
42.6k
    // function pointer and reference types as well as Objective-C
241
42.6k
    // block types:
242
42.6k
    //
243
42.6k
    // void (*FunctionPointer)(void);
244
42.6k
    // void (&FunctionReference)(void);
245
42.6k
    // void (^ObjCBlock)(void);
246
42.6k
    bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
247
42.6k
    bool ProbablyFunctionType =
248
42.6k
        CurrentToken->isOneOf(tok::star, tok::amp, tok::caret);
249
42.6k
    bool HasMultipleLines = false;
250
42.6k
    bool HasMultipleParametersOnALine = false;
251
42.6k
    bool MightBeObjCForRangeLoop =
252
42.6k
        Left->Previous && 
Left->Previous->is(tok::kw_for)42.2k
;
253
42.6k
    FormatToken *PossibleObjCForInToken = nullptr;
254
112k
    while (CurrentToken) {
255
111k
      // LookForDecls is set when "if (" has been seen. Check for
256
111k
      // 'identifier' '*' 'identifier' followed by not '=' -- this
257
111k
      // '*' has to be a binary operator but determineStarAmpUsage() will
258
111k
      // categorize it as an unary operator, so set the right type here.
259
111k
      if (LookForDecls && 
CurrentToken->Next7.30k
) {
260
6.00k
        FormatToken *Prev = CurrentToken->getPreviousNonComment();
261
6.00k
        if (Prev) {
262
6.00k
          FormatToken *PrevPrev = Prev->getPreviousNonComment();
263
6.00k
          FormatToken *Next = CurrentToken->Next;
264
6.00k
          if (PrevPrev && PrevPrev->is(tok::identifier) &&
265
6.00k
              
Prev->isOneOf(tok::star, tok::amp, tok::ampamp)1.16k
&&
266
6.00k
              
CurrentToken->is(tok::identifier)332
&&
Next->isNot(tok::equal)220
) {
267
220
            Prev->Type = TT_BinaryOperator;
268
220
            LookForDecls = false;
269
220
          }
270
6.00k
        }
271
6.00k
      }
272
111k
273
111k
      if (CurrentToken->Previous->is(TT_PointerOrReference) &&
274
111k
          CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
275
2.69k
                                                    tok::coloncolon))
276
64
        ProbablyFunctionType = true;
277
111k
      if (CurrentToken->is(tok::comma))
278
7.12k
        MightBeFunctionType = false;
279
111k
      if (CurrentToken->Previous->is(TT_BinaryOperator))
280
3.95k
        Contexts.back().IsExpression = true;
281
111k
      if (CurrentToken->is(tok::r_paren)) {
282
42.6k
        if (MightBeFunctionType && 
ProbablyFunctionType30.1k
&&
CurrentToken->Next611
&&
283
42.6k
            
(590
CurrentToken->Next->is(tok::l_paren)590
||
284
590
             
(171
CurrentToken->Next->is(tok::l_square)171
&&
Line.MustBeDeclaration56
)))
285
468
          Left->Type = Left->Next->is(tok::caret) ? 
TT_ObjCBlockLParen115
286
468
                                                  : 
TT_FunctionTypeLParen353
;
287
42.6k
        Left->MatchingParen = CurrentToken;
288
42.6k
        CurrentToken->MatchingParen = Left;
289
42.6k
290
42.6k
        if (CurrentToken->Next && 
CurrentToken->Next->is(tok::l_brace)37.4k
&&
291
42.6k
            
Left->Previous10.5k
&&
Left->Previous->is(tok::l_paren)10.5k
) {
292
14
          // Detect the case where macros are used to generate lambdas or
293
14
          // function bodies, e.g.:
294
14
          //   auto my_lambda = MARCO((Type *type, int i) { .. body .. });
295
77
          for (FormatToken *Tok = Left; Tok != CurrentToken; 
Tok = Tok->Next63
) {
296
63
            if (Tok->is(TT_BinaryOperator) &&
297
63
                
Tok->isOneOf(tok::star, tok::amp, tok::ampamp)7
)
298
7
              Tok->Type = TT_PointerOrReference;
299
63
          }
300
14
        }
301
42.6k
302
42.6k
        if (StartsObjCMethodExpr) {
303
18
          CurrentToken->Type = TT_ObjCMethodExpr;
304
18
          if (Contexts.back().FirstObjCSelectorName) {
305
4
            Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
306
4
                Contexts.back().LongestObjCSelectorName;
307
4
          }
308
18
        }
309
42.6k
310
42.6k
        if (Left->is(TT_AttributeParen))
311
77
          CurrentToken->Type = TT_AttributeParen;
312
42.6k
        if (Left->Previous && 
Left->Previous->is(TT_JavaAnnotation)42.2k
)
313
10
          CurrentToken->Type = TT_JavaAnnotation;
314
42.6k
        if (Left->Previous && 
Left->Previous->is(TT_LeadingJavaAnnotation)42.2k
)
315
40
          CurrentToken->Type = TT_LeadingJavaAnnotation;
316
42.6k
        if (Left->Previous && 
Left->Previous->is(TT_AttributeSquare)42.2k
)
317
21
          CurrentToken->Type = TT_AttributeSquare;
318
42.6k
319
42.6k
        if (!HasMultipleLines)
320
41.0k
          Left->PackingKind = PPK_Inconclusive;
321
1.55k
        else if (HasMultipleParametersOnALine)
322
282
          Left->PackingKind = PPK_BinPacked;
323
1.27k
        else
324
1.27k
          Left->PackingKind = PPK_OnePerLine;
325
42.6k
326
42.6k
        next();
327
42.6k
        return true;
328
42.6k
      }
329
69.3k
      if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
330
0
        return false;
331
69.3k
332
69.3k
      if (CurrentToken->is(tok::l_brace))
333
1.39k
        Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen
334
69.3k
      if (CurrentToken->is(tok::comma) && 
CurrentToken->Next7.12k
&&
335
69.3k
          
!CurrentToken->Next->HasUnescapedNewline7.12k
&&
336
69.3k
          
!CurrentToken->Next->isTrailingComment()5.70k
)
337
5.56k
        HasMultipleParametersOnALine = true;
338
69.3k
      if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
339
69.3k
           
CurrentToken->Previous->isSimpleTypeSpecifier()68.3k
) &&
340
69.3k
          
!CurrentToken->is(tok::l_brace)4.17k
)
341
4.16k
        Contexts.back().IsExpression = false;
342
69.3k
      if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
343
1.20k
        MightBeObjCForRangeLoop = false;
344
1.20k
        if (PossibleObjCForInToken) {
345
7
          PossibleObjCForInToken->Type = TT_Unknown;
346
7
          PossibleObjCForInToken = nullptr;
347
7
        }
348
1.20k
      }
349
69.3k
      if (MightBeObjCForRangeLoop && 
CurrentToken->is(Keywords.kw_in)2.16k
) {
350
42
        PossibleObjCForInToken = CurrentToken;
351
42
        PossibleObjCForInToken->Type = TT_ObjCForIn;
352
42
      }
353
69.3k
      // When we discover a 'new', we set CanBeExpression to 'false' in order to
354
69.3k
      // parse the type correctly. Reset that after a comma.
355
69.3k
      if (CurrentToken->is(tok::comma))
356
7.12k
        Contexts.back().CanBeExpression = true;
357
69.3k
358
69.3k
      FormatToken *Tok = CurrentToken;
359
69.3k
      if (!consumeToken())
360
7
        return false;
361
69.3k
      updateParameterCount(Left, Tok);
362
69.3k
      if (CurrentToken && 
CurrentToken->HasUnescapedNewline69.3k
)
363
2.51k
        HasMultipleLines = true;
364
69.3k
    }
365
42.6k
    
return false20
;
366
42.6k
  }
367
368
4.70k
  bool isCSharpAttributeSpecifier(const FormatToken &Tok) {
369
4.70k
    if (!Style.isCSharp())
370
4.63k
      return false;
371
74
372
74
    const FormatToken *AttrTok = Tok.Next;
373
74
    if (!AttrTok)
374
0
      return false;
375
74
376
74
    // Just an empty declaration e.g. string [].
377
74
    if (AttrTok->is(tok::r_square))
378
18
      return false;
379
56
380
56
    // Move along the tokens inbetween the '[' and ']' e.g. [STAThread].
381
220
    
while (56
AttrTok && AttrTok->isNot(tok::r_square)) {
382
164
      AttrTok = AttrTok->Next;
383
164
    }
384
56
385
56
    if (!AttrTok)
386
0
      return false;
387
56
388
56
    // Move past the end of ']'.
389
56
    AttrTok = AttrTok->Next;
390
56
    if (!AttrTok)
391
50
      return false;
392
6
393
6
    // Limit this to being an access modifier that follows.
394
6
    if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
395
6
                         tok::comment, tok::kw_class, tok::kw_static,
396
6
                         tok::l_square, Keywords.kw_internal)) {
397
2
      return true;
398
2
    }
399
4
400
4
    // incase its a [XXX] retval func(....
401
4
    if (AttrTok->Next &&
402
4
        
AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)2
)
403
0
      return true;
404
4
405
4
    return false;
406
4
  }
407
408
7.59k
  bool isCpp11AttributeSpecifier(const FormatToken &Tok) {
409
7.59k
    if (!Style.isCpp() || 
!Tok.startsSequence(tok::l_square, tok::l_square)6.96k
)
410
7.40k
      return false;
411
185
    // The first square bracket is part of an ObjC array literal
412
185
    if (Tok.Previous && 
Tok.Previous->is(tok::at)110
) {
413
23
      return false;
414
23
    }
415
162
    const FormatToken *AttrTok = Tok.Next->Next;
416
162
    if (!AttrTok)
417
0
      return false;
418
162
    // C++17 '[[using ns: foo, bar(baz, blech)]]'
419
162
    // We assume nobody will name an ObjC variable 'using'.
420
162
    if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
421
9
      return true;
422
153
    if (AttrTok->isNot(tok::identifier))
423
2
      return false;
424
362
    
while (151
AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
425
285
      // ObjC message send. We assume nobody will use : in a C++11 attribute
426
285
      // specifier parameter, although this is technically valid:
427
285
      // [[foo(:)]].
428
285
      if (AttrTok->is(tok::colon) ||
429
285
          
AttrTok->startsSequence(tok::identifier, tok::identifier)284
||
430
285
          
AttrTok->startsSequence(tok::r_paren, tok::identifier)219
)
431
73
        return false;
432
212
      if (AttrTok->is(tok::ellipsis))
433
1
        return true;
434
211
      AttrTok = AttrTok->Next;
435
211
    }
436
151
    
return 77
AttrTok77
&&
AttrTok->startsSequence(tok::r_square, tok::r_square)77
;
437
151
  }
438
439
4.71k
  bool parseSquare() {
440
4.71k
    if (!CurrentToken)
441
1
      return false;
442
4.70k
443
4.70k
    // A '[' could be an index subscript (after an identifier or after
444
4.70k
    // ')' or ']'), it could be the start of an Objective-C method
445
4.70k
    // expression, it could the start of an Objective-C array literal,
446
4.70k
    // or it could be a C++ attribute specifier [[foo::bar]].
447
4.70k
    FormatToken *Left = CurrentToken->Previous;
448
4.70k
    Left->ParentBracket = Contexts.back().ContextKind;
449
4.70k
    FormatToken *Parent = Left->getPreviousNonComment();
450
4.70k
451
4.70k
    // Cases where '>' is followed by '['.
452
4.70k
    // In C++, this can happen either in array of templates (foo<int>[10])
453
4.70k
    // or when array is a nested template type (unique_ptr<type1<type2>[]>).
454
4.70k
    bool CppArrayTemplates =
455
4.70k
        Style.isCpp() && 
Parent4.09k
&&
Parent->is(TT_TemplateCloser)3.36k
&&
456
4.70k
        
(12
Contexts.back().CanBeExpression12
||
Contexts.back().IsExpression6
||
457
12
         
Contexts.back().InTemplateArgument0
);
458
4.70k
459
4.70k
    bool IsCpp11AttributeSpecifier = isCpp11AttributeSpecifier(*Left) ||
460
4.70k
                                     
Contexts.back().InCpp11AttributeSpecifier4.62k
;
461
4.70k
462
4.70k
    // Treat C# Attributes [STAThread] much like C++ attributes [[...]].
463
4.70k
    bool IsCSharp11AttributeSpecifier =
464
4.70k
        isCSharpAttributeSpecifier(*Left) ||
465
4.70k
        
Contexts.back().InCSharpAttributeSpecifier4.70k
;
466
4.70k
467
4.70k
    bool InsideInlineASM = Line.startsWith(tok::kw_asm);
468
4.70k
    bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style);
469
4.70k
    bool StartsObjCMethodExpr =
470
4.70k
        !IsCppStructuredBinding && 
!InsideInlineASM4.50k
&&
!CppArrayTemplates4.48k
&&
471
4.70k
        
Style.isCpp()4.46k
&&
!IsCpp11AttributeSpecifier3.85k
&&
472
4.70k
        
Contexts.back().CanBeExpression3.69k
&&
Left->isNot(TT_LambdaLSquare)3.61k
&&
473
4.70k
        
!CurrentToken->isOneOf(tok::l_brace, tok::r_square)2.09k
&&
474
4.70k
        
(1.82k
!Parent1.82k
||
475
1.82k
         Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
476
1.42k
                         tok::kw_return, tok::kw_throw) ||
477
1.82k
         
Parent->isUnaryOperator()1.24k
||
478
1.82k
         // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
479
1.82k
         
Parent->isOneOf(TT_ObjCForIn, TT_CastRParen)1.21k
||
480
1.82k
         (getBinOpPrecedence(Parent->Tok.getKind(), true, true) >
481
1.20k
          prec::Unknown));
482
4.70k
    bool ColonFound = false;
483
4.70k
484
4.70k
    unsigned BindingIncrease = 1;
485
4.70k
    if (IsCppStructuredBinding) {
486
208
      Left->Type = TT_StructuredBindingLSquare;
487
4.50k
    } else if (Left->is(TT_Unknown)) {
488
2.97k
      if (StartsObjCMethodExpr) {
489
843
        Left->Type = TT_ObjCMethodExpr;
490
2.13k
      } else if (IsCpp11AttributeSpecifier) {
491
160
        Left->Type = TT_AttributeSquare;
492
1.97k
      } else if (Style.Language == FormatStyle::LK_JavaScript && 
Parent348
&&
493
1.97k
                 
Contexts.back().ContextKind == tok::l_brace340
&&
494
1.97k
                 
Parent->isOneOf(tok::l_brace, tok::comma)36
) {
495
20
        Left->Type = TT_JsComputedPropertyName;
496
1.95k
      } else if (Style.isCpp() && 
Contexts.back().ContextKind == tok::l_brace1.36k
&&
497
1.95k
                 
Parent53
&&
Parent->isOneOf(tok::l_brace, tok::comma)53
) {
498
47
        Left->Type = TT_DesignatedInitializerLSquare;
499
1.90k
      } else if (IsCSharp11AttributeSpecifier) {
500
2
        Left->Type = TT_AttributeSquare;
501
1.90k
      } else if (CurrentToken->is(tok::r_square) && 
Parent374
&&
502
1.90k
                 
Parent->is(TT_TemplateCloser)374
) {
503
12
        Left->Type = TT_ArraySubscriptLSquare;
504
1.89k
      } else if (Style.Language == FormatStyle::LK_Proto ||
505
1.89k
                 
Style.Language == FormatStyle::LK_TextProto1.82k
) {
506
170
        // Square braces in LK_Proto can either be message field attributes:
507
170
        //
508
170
        // optional Aaa aaa = 1 [
509
170
        //   (aaa) = aaa
510
170
        // ];
511
170
        //
512
170
        // extensions 123 [
513
170
        //   (aaa) = aaa
514
170
        // ];
515
170
        //
516
170
        // or text proto extensions (in options):
517
170
        //
518
170
        // option (Aaa.options) = {
519
170
        //   [type.type/type] {
520
170
        //     key: value
521
170
        //   }
522
170
        // }
523
170
        //
524
170
        // or repeated fields (in options):
525
170
        //
526
170
        // option (Aaa.options) = {
527
170
        //   keys: [ 1, 2, 3 ]
528
170
        // }
529
170
        //
530
170
        // In the first and the third case we want to spread the contents inside
531
170
        // the square braces; in the second we want to keep them inline.
532
170
        Left->Type = TT_ArrayInitializerLSquare;
533
170
        if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
534
170
                                tok::equal) &&
535
170
            !Left->endsSequence(tok::l_square, tok::numeric_constant,
536
138
                                tok::identifier) &&
537
170
            
!Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)132
) {
538
78
          Left->Type = TT_ProtoExtensionLSquare;
539
78
          BindingIncrease = 10;
540
78
        }
541
1.72k
      } else if (!CppArrayTemplates && 
Parent1.70k
&&
542
1.72k
                 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
543
1.65k
                                 tok::comma, tok::l_paren, tok::l_square,
544
1.65k
                                 tok::question, tok::colon, tok::kw_return,
545
1.65k
                                 // Should only be relevant to JavaScript:
546
1.65k
                                 tok::kw_default)) {
547
242
        Left->Type = TT_ArrayInitializerLSquare;
548
1.47k
      } else {
549
1.47k
        BindingIncrease = 10;
550
1.47k
        Left->Type = TT_ArraySubscriptLSquare;
551
1.47k
      }
552
2.97k
    }
553
4.70k
554
4.70k
    ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease);
555
4.70k
    Contexts.back().IsExpression = true;
556
4.70k
    if (Style.Language == FormatStyle::LK_JavaScript && 
Parent348
&&
557
4.70k
        
Parent->is(TT_JsTypeColon)340
)
558
4
      Contexts.back().IsExpression = false;
559
4.70k
560
4.70k
    Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
561
4.70k
    Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
562
4.70k
    Contexts.back().InCSharpAttributeSpecifier = IsCSharp11AttributeSpecifier;
563
4.70k
564
14.7k
    while (CurrentToken) {
565
14.7k
      if (CurrentToken->is(tok::r_square)) {
566
4.70k
        if (IsCpp11AttributeSpecifier)
567
160
          CurrentToken->Type = TT_AttributeSquare;
568
4.70k
        if (IsCSharp11AttributeSpecifier)
569
2
          CurrentToken->Type = TT_AttributeSquare;
570
4.70k
        else if (((CurrentToken->Next &&
571
4.70k
                   
CurrentToken->Next->is(tok::l_paren)4.60k
) ||
572
4.70k
                  
(3.33k
CurrentToken->Previous3.33k
&&
573
3.33k
                   CurrentToken->Previous->Previous == Left)) &&
574
4.70k
                 
Left->is(TT_ObjCMethodExpr)2.51k
) {
575
79
          // An ObjC method call is rarely followed by an open parenthesis. It
576
79
          // also can't be composed of just one token, unless it's a macro that
577
79
          // will be expanded to more tokens.
578
79
          // FIXME: Do we incorrectly label ":" with this?
579
79
          StartsObjCMethodExpr = false;
580
79
          Left->Type = TT_Unknown;
581
79
        }
582
4.70k
        if (StartsObjCMethodExpr && 
CurrentToken->Previous != Left768
) {
583
768
          CurrentToken->Type = TT_ObjCMethodExpr;
584
768
          // If we haven't seen a colon yet, make sure the last identifier
585
768
          // before the r_square is tagged as a selector name component.
586
768
          if (!ColonFound && 
CurrentToken->Previous219
&&
587
768
              
CurrentToken->Previous->is(TT_Unknown)219
&&
588
768
              
canBeObjCSelectorComponent(*CurrentToken->Previous)54
)
589
52
            CurrentToken->Previous->Type = TT_SelectorName;
590
768
          // determineStarAmpUsage() thinks that '*' '[' is allocating an
591
768
          // array of pointers, but if '[' starts a selector then '*' is a
592
768
          // binary operator.
593
768
          if (Parent && 
Parent->is(TT_PointerOrReference)377
)
594
24
            Parent->Type = TT_BinaryOperator;
595
768
        }
596
4.70k
        // An arrow after an ObjC method expression is not a lambda arrow.
597
4.70k
        if (CurrentToken->Type == TT_ObjCMethodExpr && 
CurrentToken->Next768
&&
598
4.70k
            
CurrentToken->Next->is(TT_LambdaArrow)756
)
599
2
          CurrentToken->Next->Type = TT_Unknown;
600
4.70k
        Left->MatchingParen = CurrentToken;
601
4.70k
        CurrentToken->MatchingParen = Left;
602
4.70k
        // FirstObjCSelectorName is set when a colon is found. This does
603
4.70k
        // not work, however, when the method has no parameters.
604
4.70k
        // Here, we set FirstObjCSelectorName when the end of the method call is
605
4.70k
        // reached, in case it was not set already.
606
4.70k
        if (!Contexts.back().FirstObjCSelectorName) {
607
4.15k
          FormatToken *Previous = CurrentToken->getPreviousNonComment();
608
4.15k
          if (Previous && Previous->is(TT_SelectorName)) {
609
52
            Previous->ObjCSelectorNameParts = 1;
610
52
            Contexts.back().FirstObjCSelectorName = Previous;
611
52
          }
612
4.15k
        } else {
613
549
          Left->ParameterCount =
614
549
              Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
615
549
        }
616
4.70k
        if (Contexts.back().FirstObjCSelectorName) {
617
601
          Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
618
601
              Contexts.back().LongestObjCSelectorName;
619
601
          if (Left->BlockParameterCount > 1)
620
22
            Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
621
601
        }
622
4.70k
        next();
623
4.70k
        return true;
624
4.70k
      }
625
10.0k
      if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
626
3
        return false;
627
10.0k
      if (CurrentToken->is(tok::colon)) {
628
845
        if (IsCpp11AttributeSpecifier &&
629
845
            CurrentToken->endsSequence(tok::colon, tok::identifier,
630
9
                                       tok::kw_using)) {
631
9
          // Remember that this is a [[using ns: foo]] C++ attribute, so we
632
9
          // don't add a space before the colon (unlike other colons).
633
9
          CurrentToken->Type = TT_AttributeColon;
634
836
        } else if (Left->isOneOf(TT_ArraySubscriptLSquare,
635
836
                                 TT_DesignatedInitializerLSquare)) {
636
9
          Left->Type = TT_ObjCMethodExpr;
637
9
          StartsObjCMethodExpr = true;
638
9
          Contexts.back().ColonIsObjCMethodExpr = true;
639
9
          if (Parent && Parent->is(tok::r_paren))
640
2
            // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
641
2
            Parent->Type = TT_CastRParen;
642
9
        }
643
845
        ColonFound = true;
644
845
      }
645
10.0k
      if (CurrentToken->is(tok::comma) && 
Left->is(TT_ObjCMethodExpr)1.10k
&&
646
10.0k
          
!ColonFound28
)
647
2
        Left->Type = TT_ArrayInitializerLSquare;
648
10.0k
      FormatToken *Tok = CurrentToken;
649
10.0k
      if (!consumeToken())
650
0
        return false;
651
10.0k
      updateParameterCount(Left, Tok);
652
10.0k
    }
653
4.70k
    
return false2
;
654
4.70k
  }
655
656
29.6k
  bool parseBrace() {
657
29.6k
    if (CurrentToken) {
658
8.26k
      FormatToken *Left = CurrentToken->Previous;
659
8.26k
      Left->ParentBracket = Contexts.back().ContextKind;
660
8.26k
661
8.26k
      if (Contexts.back().CaretFound)
662
281
        Left->Type = TT_ObjCBlockLBrace;
663
8.26k
      Contexts.back().CaretFound = false;
664
8.26k
665
8.26k
      ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
666
8.26k
      Contexts.back().ColonIsDictLiteral = true;
667
8.26k
      if (Left->BlockKind == BK_BracedInit)
668
2.72k
        Contexts.back().IsExpression = true;
669
8.26k
      if (Style.Language == FormatStyle::LK_JavaScript && 
Left->Previous1.10k
&&
670
8.26k
          
Left->Previous->is(TT_JsTypeColon)1.10k
)
671
70
        Contexts.back().IsExpression = false;
672
8.26k
673
33.8k
      while (CurrentToken) {
674
33.7k
        if (CurrentToken->is(tok::r_brace)) {
675
8.12k
          Left->MatchingParen = CurrentToken;
676
8.12k
          CurrentToken->MatchingParen = Left;
677
8.12k
          next();
678
8.12k
          return true;
679
8.12k
        }
680
25.6k
        if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
681
0
          return false;
682
25.6k
        updateParameterCount(Left, CurrentToken);
683
25.6k
        if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
684
2.74k
          FormatToken *Previous = CurrentToken->getPreviousNonComment();
685
2.74k
          if (Previous->is(TT_JsTypeOptionalQuestion))
686
24
            Previous = Previous->getPreviousNonComment();
687
2.74k
          if ((CurrentToken->is(tok::colon) &&
688
2.74k
               
(1.35k
!Contexts.back().ColonIsDictLiteral1.35k
||
!Style.isCpp()1.35k
)) ||
689
2.74k
              
Style.Language == FormatStyle::LK_Proto1.49k
||
690
2.74k
              
Style.Language == FormatStyle::LK_TextProto1.37k
) {
691
1.47k
            Left->Type = TT_DictLiteral;
692
1.47k
            if (Previous->Tok.getIdentifierInfo() ||
693
1.47k
                
Previous->is(tok::string_literal)216
)
694
1.27k
              Previous->Type = TT_SelectorName;
695
1.47k
          }
696
2.74k
          if (CurrentToken->is(tok::colon) ||
697
2.74k
              
Style.Language == FormatStyle::LK_JavaScript1.38k
)
698
1.47k
            Left->Type = TT_DictLiteral;
699
2.74k
        }
700
25.6k
        if (CurrentToken->is(tok::comma) &&
701
25.6k
            
Style.Language == FormatStyle::LK_JavaScript6.53k
)
702
430
          Left->Type = TT_DictLiteral;
703
25.6k
        if (!consumeToken())
704
0
          return false;
705
25.6k
      }
706
8.26k
    }
707
29.6k
    
return true21.4k
;
708
29.6k
  }
709
710
122k
  void updateParameterCount(FormatToken *Left, FormatToken *Current) {
711
122k
    // For ObjC methods, the number of parameters is calculated differently as
712
122k
    // method declarations have a different structure (the parameters are not
713
122k
    // inside a bracket scope).
714
122k
    if (Current->is(tok::l_brace) && 
Current->BlockKind == BK_Block2.93k
)
715
1.54k
      ++Left->BlockParameterCount;
716
122k
    if (Current->is(tok::comma)) {
717
15.8k
      ++Left->ParameterCount;
718
15.8k
      if (!Left->Role)
719
8.48k
        Left->Role.reset(new CommaSeparatedList(Style));
720
15.8k
      Left->Role->CommaFound(Current);
721
106k
    } else if (Left->ParameterCount == 0 && 
Current->isNot(tok::comment)39.5k
) {
722
39.0k
      Left->ParameterCount = 1;
723
39.0k
    }
724
122k
  }
725
726
699
  bool parseConditional() {
727
1.75k
    while (CurrentToken) {
728
1.75k
      if (CurrentToken->is(tok::colon)) {
729
685
        CurrentToken->Type = TT_ConditionalExpr;
730
685
        next();
731
685
        return true;
732
685
      }
733
1.06k
      if (!consumeToken())
734
14
        return false;
735
1.06k
    }
736
699
    
return false0
;
737
699
  }
738
739
2.69k
  bool parseTemplateDeclaration() {
740
2.69k
    if (CurrentToken && CurrentToken->is(tok::less)) {
741
2.64k
      CurrentToken->Type = TT_TemplateOpener;
742
2.64k
      next();
743
2.64k
      if (!parseAngle())
744
0
        return false;
745
2.64k
      if (CurrentToken)
746
2.64k
        CurrentToken->Previous->ClosesTemplateDeclaration = true;
747
2.64k
      return true;
748
2.64k
    }
749
48
    return false;
750
48
  }
751
752
471k
  bool consumeToken() {
753
471k
    FormatToken *Tok = CurrentToken;
754
471k
    next();
755
471k
    switch (Tok->Tok.getKind()) {
756
2.36k
    case tok::plus:
757
2.36k
    case tok::minus:
758
2.36k
      if (!Tok->Previous && 
Line.MustBeDeclaration432
)
759
418
        Tok->Type = TT_ObjCMethodSpecifier;
760
2.36k
      break;
761
7.93k
    case tok::colon:
762
7.93k
      if (!Tok->Previous)
763
3
        return false;
764
7.93k
      // Colons from ?: are handled in parseConditional().
765
7.93k
      if (Style.Language == FormatStyle::LK_JavaScript) {
766
980
        if (Contexts.back().ColonIsForRangeExpr || // colon in for loop
767
980
            
(972
Contexts.size() == 1972
&& // switch/case labels
768
972
             
!Line.First->isOneOf(tok::kw_enum, tok::kw_case)302
) ||
769
980
            
Contexts.back().ContextKind == tok::l_paren686
|| // function params
770
980
            
Contexts.back().ContextKind == tok::l_square570
|| // array type
771
980
            
(566
!Contexts.back().IsExpression566
&&
772
566
             
Contexts.back().ContextKind == tok::l_brace118
) || // object type
773
980
            
(464
Contexts.size() == 1464
&&
774
528
             
Line.MustBeDeclaration16
)) { // method/property declaration
775
528
          Contexts.back().IsExpression = false;
776
528
          Tok->Type = TT_JsTypeColon;
777
528
          break;
778
528
        }
779
7.40k
      }
780
7.40k
      if (Contexts.back().ColonIsDictLiteral ||
781
7.40k
          
Style.Language == FormatStyle::LK_Proto6.15k
||
782
7.40k
          
Style.Language == FormatStyle::LK_TextProto5.98k
) {
783
2.39k
        Tok->Type = TT_DictLiteral;
784
2.39k
        if (Style.Language == FormatStyle::LK_TextProto) {
785
1.34k
          if (FormatToken *Previous = Tok->getPreviousNonComment())
786
1.34k
            Previous->Type = TT_SelectorName;
787
1.34k
        }
788
5.00k
      } else if (Contexts.back().ColonIsObjCMethodExpr ||
789
5.00k
                 
Line.startsWith(TT_ObjCMethodSpecifier)4.17k
) {
790
1.22k
        Tok->Type = TT_ObjCMethodExpr;
791
1.22k
        const FormatToken *BeforePrevious = Tok->Previous->Previous;
792
1.22k
        // Ensure we tag all identifiers in method declarations as
793
1.22k
        // TT_SelectorName.
794
1.22k
        bool UnknownIdentifierInMethodDeclaration =
795
1.22k
            Line.startsWith(TT_ObjCMethodSpecifier) &&
796
1.22k
            
Tok->Previous->is(tok::identifier)391
&&
Tok->Previous->is(TT_Unknown)383
;
797
1.22k
        if (!BeforePrevious ||
798
1.22k
            // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
799
1.22k
            !(BeforePrevious->is(TT_CastRParen) ||
800
1.22k
              
(1.01k
BeforePrevious->is(TT_ObjCMethodExpr)1.01k
&&
801
1.01k
               
BeforePrevious->is(tok::colon)93
)) ||
802
1.22k
            
BeforePrevious->is(tok::r_square)223
||
803
1.22k
            
Contexts.back().LongestObjCSelectorName == 0223
||
804
1.22k
            
UnknownIdentifierInMethodDeclaration28
) {
805
1.19k
          Tok->Previous->Type = TT_SelectorName;
806
1.19k
          if (!Contexts.back().FirstObjCSelectorName)
807
760
            Contexts.back().FirstObjCSelectorName = Tok->Previous;
808
435
          else if (Tok->Previous->ColumnWidth >
809
435
                   Contexts.back().LongestObjCSelectorName)
810
296
            Contexts.back().LongestObjCSelectorName =
811
296
                Tok->Previous->ColumnWidth;
812
1.19k
          Tok->Previous->ParameterIndex =
813
1.19k
              Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
814
1.19k
          ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
815
1.19k
        }
816
3.78k
      } else if (Contexts.back().ColonIsForRangeExpr) {
817
102
        Tok->Type = TT_RangeBasedForLoopColon;
818
3.68k
      } else if (CurrentToken && 
CurrentToken->is(tok::numeric_constant)2.12k
) {
819
35
        Tok->Type = TT_BitFieldColon;
820
3.64k
      } else if (Contexts.size() == 1 &&
821
3.64k
                 
!Line.First->isOneOf(tok::kw_enum, tok::kw_case)3.53k
) {
822
2.94k
        FormatToken *Prev = Tok->getPreviousNonComment();
823
2.94k
        if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept))
824
1.05k
          Tok->Type = TT_CtorInitializerColon;
825
1.89k
        else if (Prev->is(tok::kw_try)) {
826
15
          // Member initializer list within function try block.
827
15
          FormatToken *PrevPrev = Prev->getPreviousNonComment();
828
15
          if (PrevPrev && 
PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept)11
)
829
11
            Tok->Type = TT_CtorInitializerColon;
830
15
        } else
831
1.87k
          Tok->Type = TT_InheritanceColon;
832
2.94k
      } else 
if (705
canBeObjCSelectorComponent(*Tok->Previous)705
&&
Tok->Next193
&&
833
705
                 
(83
Tok->Next->isOneOf(tok::r_paren, tok::comma)83
||
834
83
                  
(59
canBeObjCSelectorComponent(*Tok->Next)59
&&
Tok->Next->Next59
&&
835
59
                   Tok->Next->Next->is(tok::colon)))) {
836
38
        // This handles a special macro in ObjC code where selectors including
837
38
        // the colon are passed as macro arguments.
838
38
        Tok->Type = TT_ObjCMethodExpr;
839
667
      } else if (Contexts.back().ContextKind == tok::l_paren) {
840
58
        Tok->Type = TT_InlineASMColon;
841
58
      }
842
7.40k
      break;
843
7.40k
    case tok::pipe:
844
2.87k
    case tok::amp:
845
2.87k
      // | and & in declarations/type expressions represent union and
846
2.87k
      // intersection types, respectively.
847
2.87k
      if (Style.Language == FormatStyle::LK_JavaScript &&
848
2.87k
          
!Contexts.back().IsExpression124
)
849
108
        Tok->Type = TT_JsTypeOperator;
850
2.87k
      break;
851
2.87k
    case tok::kw_if:
852
2.54k
    case tok::kw_while:
853
2.54k
      if (Tok->is(tok::kw_if) && 
CurrentToken2.12k
&&
854
2.54k
          
CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)2.12k
)
855
385
        next();
856
2.54k
      if (CurrentToken && 
CurrentToken->is(tok::l_paren)2.53k
) {
857
2.50k
        next();
858
2.50k
        if (!parseParens(/*LookForDecls=*/true))
859
0
          return false;
860
2.54k
      }
861
2.54k
      break;
862
2.54k
    case tok::kw_for:
863
621
      if (Style.Language == FormatStyle::LK_JavaScript) {
864
60
        // x.for and {for: ...}
865
60
        if ((Tok->Previous && 
Tok->Previous->is(tok::period)12
) ||
866
60
            
(52
Tok->Next52
&&
Tok->Next->is(tok::colon)52
))
867
12
          break;
868
48
        // JS' for await ( ...
869
48
        if (CurrentToken && CurrentToken->is(Keywords.kw_await))
870
8
          next();
871
48
      }
872
621
      Contexts.back().ColonIsForRangeExpr = true;
873
609
      next();
874
609
      if (!parseParens())
875
3
        return false;
876
606
      break;
877
38.6k
    case tok::l_paren:
878
38.6k
      // When faced with 'operator()()', the kw_operator handler incorrectly
879
38.6k
      // marks the first l_paren as a OverloadedOperatorLParen. Here, we make
880
38.6k
      // the first two parens OverloadedOperators and the second l_paren an
881
38.6k
      // OverloadedOperatorLParen.
882
38.6k
      if (Tok->Previous && 
Tok->Previous->is(tok::r_paren)38.1k
&&
883
38.6k
          
Tok->Previous->MatchingParen745
&&
884
38.6k
          
Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)745
) {
885
146
        Tok->Previous->Type = TT_OverloadedOperator;
886
146
        Tok->Previous->MatchingParen->Type = TT_OverloadedOperator;
887
146
        Tok->Type = TT_OverloadedOperatorLParen;
888
146
      }
889
38.6k
890
38.6k
      if (!parseParens())
891
33
        return false;
892
38.5k
      if (Line.MustBeDeclaration && 
Contexts.size() == 130.7k
&&
893
38.5k
          
!Contexts.back().IsExpression26.2k
&&
!Line.startsWith(TT_ObjCProperty)21.6k
&&
894
38.5k
          
(21.6k
!Tok->Previous21.6k
||
895
21.6k
           !Tok->Previous->isOneOf(tok::kw_decltype, tok::kw___attribute,
896
21.5k
                                   TT_LeadingJavaAnnotation)))
897
21.4k
        Line.MightBeFunctionDecl = true;
898
38.5k
      break;
899
38.5k
    case tok::l_square:
900
4.71k
      if (!parseSquare())
901
6
        return false;
902
4.70k
      break;
903
29.6k
    case tok::l_brace:
904
29.6k
      if (Style.Language == FormatStyle::LK_TextProto) {
905
311
        FormatToken *Previous = Tok->getPreviousNonComment();
906
311
        if (Previous && Previous->Type != TT_DictLiteral)
907
186
          Previous->Type = TT_SelectorName;
908
311
      }
909
29.6k
      if (!parseBrace())
910
0
        return false;
911
29.6k
      break;
912
29.6k
    case tok::less:
913
4.96k
      if (parseAngle()) {
914
4.59k
        Tok->Type = TT_TemplateOpener;
915
4.59k
        // In TT_Proto, we must distignuish between:
916
4.59k
        //   map<key, value>
917
4.59k
        //   msg < item: data >
918
4.59k
        //   msg: < item: data >
919
4.59k
        // In TT_TextProto, map<key, value> does not occur.
920
4.59k
        if (Style.Language == FormatStyle::LK_TextProto ||
921
4.59k
            
(4.37k
Style.Language == FormatStyle::LK_Proto4.37k
&&
Tok->Previous96
&&
922
4.37k
             
Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral)96
)) {
923
273
          Tok->Type = TT_DictLiteral;
924
273
          FormatToken *Previous = Tok->getPreviousNonComment();
925
273
          if (Previous && Previous->Type != TT_DictLiteral)
926
143
            Previous->Type = TT_SelectorName;
927
273
        }
928
4.59k
      } else {
929
377
        Tok->Type = TT_BinaryOperator;
930
377
        NonTemplateLess.insert(Tok);
931
377
        CurrentToken = Tok;
932
377
        next();
933
377
      }
934
4.96k
      break;
935
29.6k
    case tok::r_paren:
936
26
    case tok::r_square:
937
26
      return false;
938
21.3k
    case tok::r_brace:
939
21.3k
      // Lines can start with '}'.
940
21.3k
      if (Tok->Previous)
941
6
        return false;
942
21.3k
      break;
943
21.3k
    case tok::greater:
944
453
      if (Style.Language != FormatStyle::LK_TextProto)
945
453
        Tok->Type = TT_BinaryOperator;
946
453
      if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
947
14
        Tok->SpacesRequiredBefore = 1;
948
453
      break;
949
21.3k
    case tok::kw_operator:
950
1.33k
      if (Style.Language == FormatStyle::LK_TextProto ||
951
1.33k
          
Style.Language == FormatStyle::LK_Proto1.32k
)
952
18
        break;
953
2.88k
      
while (1.31k
CurrentToken &&
954
2.88k
             
!CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)2.87k
) {
955
1.56k
        if (CurrentToken->isOneOf(tok::star, tok::amp))
956
329
          CurrentToken->Type = TT_PointerOrReference;
957
1.56k
        consumeToken();
958
1.56k
        if (CurrentToken && CurrentToken->Previous->isOneOf(
959
1.56k
                                TT_BinaryOperator, TT_UnaryOperator, tok::comma,
960
1.56k
                                tok::star, tok::arrow, tok::amp, tok::ampamp))
961
881
          CurrentToken->Previous->Type = TT_OverloadedOperator;
962
1.56k
      }
963
1.31k
      if (CurrentToken) {
964
1.31k
        CurrentToken->Type = TT_OverloadedOperatorLParen;
965
1.31k
        if (CurrentToken->Previous->is(TT_BinaryOperator))
966
0
          CurrentToken->Previous->Type = TT_OverloadedOperator;
967
1.31k
      }
968
1.31k
      break;
969
1.31k
    case tok::question:
970
751
      if (Style.Language == FormatStyle::LK_JavaScript && 
Tok->Next102
&&
971
751
          Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
972
102
                             tok::r_brace)) {
973
48
        // Question marks before semicolons, colons, etc. indicate optional
974
48
        // types (fields, parameters), e.g.
975
48
        //   function(x?: string, y?) {...}
976
48
        //   class X { y?; }
977
48
        Tok->Type = TT_JsTypeOptionalQuestion;
978
48
        break;
979
48
      }
980
703
      // Declarations cannot be conditional expressions, this can only be part
981
703
      // of a type declaration.
982
703
      if (Line.MustBeDeclaration && 
!Contexts.back().IsExpression607
&&
983
703
          
Style.Language == FormatStyle::LK_JavaScript242
)
984
4
        break;
985
699
      parseConditional();
986
699
      break;
987
2.69k
    case tok::kw_template:
988
2.69k
      parseTemplateDeclaration();
989
2.69k
      break;
990
17.4k
    case tok::comma:
991
17.4k
      if (Contexts.back().InCtorInitializer)
992
926
        Tok->Type = TT_CtorInitializerComma;
993
16.5k
      else if (Contexts.back().InInheritanceList)
994
168
        Tok->Type = TT_InheritanceComma;
995
16.3k
      else if (Contexts.back().FirstStartOfName &&
996
16.3k
               
(1.61k
Contexts.size() == 11.61k
||
Line.startsWith(tok::kw_for)1.36k
)) {
997
299
        Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
998
299
        Line.IsMultiVariableDeclStmt = true;
999
299
      }
1000
17.4k
      if (Contexts.back().IsForEachMacro)
1001
21
        Contexts.back().IsExpression = true;
1002
17.4k
      break;
1003
134k
    case tok::identifier:
1004
134k
      if (Tok->isOneOf(Keywords.kw___has_include,
1005
134k
                       Keywords.kw___has_include_next)) {
1006
35
        parseHasInclude();
1007
35
      }
1008
134k
      break;
1009
198k
    default:
1010
198k
      break;
1011
471k
    }
1012
471k
    return true;
1013
471k
  }
1014
1015
3.56k
  void parseIncludeDirective() {
1016
3.56k
    if (CurrentToken && CurrentToken->is(tok::less)) {
1017
519
      next();
1018
2.28k
      while (CurrentToken) {
1019
1.76k
        // Mark tokens up to the trailing line comments as implicit string
1020
1.76k
        // literals.
1021
1.76k
        if (CurrentToken->isNot(tok::comment) &&
1022
1.76k
            
!CurrentToken->TokenText.startswith("//")1.73k
)
1023
1.73k
          CurrentToken->Type = TT_ImplicitStringLiteral;
1024
1.76k
        next();
1025
1.76k
      }
1026
519
    }
1027
3.56k
  }
1028
1029
47
  void parseWarningOrError() {
1030
47
    next();
1031
47
    // We still want to format the whitespace left of the first token of the
1032
47
    // warning or error.
1033
47
    next();
1034
113
    while (CurrentToken) {
1035
66
      CurrentToken->Type = TT_ImplicitStringLiteral;
1036
66
      next();
1037
66
    }
1038
47
  }
1039
1040
37
  void parsePragma() {
1041
37
    next(); // Consume "pragma".
1042
37
    if (CurrentToken &&
1043
37
        CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) {
1044
13
      bool IsMark = CurrentToken->is(Keywords.kw_mark);
1045
13
      next(); // Consume "mark".
1046
13
      next(); // Consume first token (so we fix leading whitespace).
1047
76
      while (CurrentToken) {
1048
63
        if (IsMark || 
CurrentToken->Previous->is(TT_BinaryOperator)30
)
1049
53
          CurrentToken->Type = TT_ImplicitStringLiteral;
1050
63
        next();
1051
63
      }
1052
13
    }
1053
37
  }
1054
1055
35
  void parseHasInclude() {
1056
35
    if (!CurrentToken || !CurrentToken->is(tok::l_paren))
1057
0
      return;
1058
35
    next(); // '('
1059
35
    parseIncludeDirective();
1060
35
    next(); // ')'
1061
35
  }
1062
1063
8.24k
  LineType parsePreprocessorDirective() {
1064
8.24k
    bool IsFirstToken = CurrentToken->IsFirst;
1065
8.24k
    LineType Type = LT_PreprocessorDirective;
1066
8.24k
    next();
1067
8.24k
    if (!CurrentToken)
1068
10
      return Type;
1069
8.23k
1070
8.23k
    if (Style.Language == FormatStyle::LK_JavaScript && 
IsFirstToken4
) {
1071
4
      // JavaScript files can contain shebang lines of the form:
1072
4
      // #!/usr/bin/env node
1073
4
      // Treat these like C++ #include directives.
1074
36
      while (CurrentToken) {
1075
32
        // Tokens cannot be comments here.
1076
32
        CurrentToken->Type = TT_ImplicitStringLiteral;
1077
32
        next();
1078
32
      }
1079
4
      return LT_ImportStatement;
1080
4
    }
1081
8.23k
1082
8.23k
    if (CurrentToken->Tok.is(tok::numeric_constant)) {
1083
10
      CurrentToken->SpacesRequiredBefore = 1;
1084
10
      return Type;
1085
10
    }
1086
8.22k
    // Hashes in the middle of a line can lead to any strange token
1087
8.22k
    // sequence.
1088
8.22k
    if (!CurrentToken->Tok.getIdentifierInfo())
1089
10
      return Type;
1090
8.21k
    switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1091
3.41k
    case tok::pp_include:
1092
3.41k
    case tok::pp_include_next:
1093
3.41k
    case tok::pp_import:
1094
3.41k
      next();
1095
3.41k
      parseIncludeDirective();
1096
3.41k
      Type = LT_ImportStatement;
1097
3.41k
      break;
1098
3.41k
    case tok::pp_error:
1099
47
    case tok::pp_warning:
1100
47
      parseWarningOrError();
1101
47
      break;
1102
47
    case tok::pp_pragma:
1103
37
      parsePragma();
1104
37
      break;
1105
455
    case tok::pp_if:
1106
455
    case tok::pp_elif:
1107
455
      Contexts.back().IsExpression = true;
1108
455
      next();
1109
455
      parseLine();
1110
455
      break;
1111
4.25k
    default:
1112
4.25k
      break;
1113
8.21k
    }
1114
19.4k
    
while (8.21k
CurrentToken) {
1115
11.2k
      FormatToken *Tok = CurrentToken;
1116
11.2k
      next();
1117
11.2k
      if (Tok->is(tok::l_paren))
1118
969
        parseParens();
1119
10.3k
      else if (Tok->isOneOf(Keywords.kw___has_include,
1120
10.3k
                            Keywords.kw___has_include_next))
1121
0
        parseHasInclude();
1122
11.2k
    }
1123
8.21k
    return Type;
1124
8.21k
  }
1125
1126
public:
1127
133k
  LineType parseLine() {
1128
133k
    if (!CurrentToken)
1129
21
      return LT_Invalid;
1130
133k
    NonTemplateLess.clear();
1131
133k
    if (CurrentToken->is(tok::hash))
1132
8.24k
      return parsePreprocessorDirective();
1133
125k
1134
125k
    // Directly allow to 'import <string-literal>' to support protocol buffer
1135
125k
    // definitions (github.com/google/protobuf) or missing "#" (either way we
1136
125k
    // should not break the line).
1137
125k
    IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1138
125k
    if ((Style.Language == FormatStyle::LK_Java &&
1139
125k
         
CurrentToken->is(Keywords.kw_package)962
) ||
1140
125k
        
(125k
Info125k
&&
Info->getPPKeywordID() == tok::pp_import62.3k
&&
1141
125k
         
CurrentToken->Next307
&&
1142
125k
         CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1143
307
                                     tok::kw_static))) {
1144
105
      next();
1145
105
      parseIncludeDirective();
1146
105
      return LT_ImportStatement;
1147
105
    }
1148
125k
1149
125k
    // If this line starts and ends in '<' and '>', respectively, it is likely
1150
125k
    // part of "#define <a/b.h>".
1151
125k
    if (CurrentToken->is(tok::less) && 
Line.Last->is(tok::greater)10
) {
1152
7
      parseIncludeDirective();
1153
7
      return LT_ImportStatement;
1154
7
    }
1155
125k
1156
125k
    // In .proto files, top-level options and package statements are very
1157
125k
    // similar to import statements and should not be line-wrapped.
1158
125k
    if (Style.Language == FormatStyle::LK_Proto && 
Line.Level == 0422
&&
1159
125k
        
CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)374
) {
1160
105
      next();
1161
105
      if (CurrentToken && CurrentToken->is(tok::identifier)) {
1162
67
        while (CurrentToken)
1163
60
          next();
1164
7
        return LT_ImportStatement;
1165
7
      }
1166
125k
    }
1167
125k
1168
125k
    bool KeywordVirtualFound = false;
1169
125k
    bool ImportStatement = false;
1170
125k
1171
125k
    // import {...} from '...';
1172
125k
    if (Style.Language == FormatStyle::LK_JavaScript &&
1173
125k
        
CurrentToken->is(Keywords.kw_import)7.30k
)
1174
195
      ImportStatement = true;
1175
125k
1176
471k
    while (CurrentToken) {
1177
346k
      if (CurrentToken->is(tok::kw_virtual))
1178
207
        KeywordVirtualFound = true;
1179
346k
      if (Style.Language == FormatStyle::LK_JavaScript) {
1180
21.7k
        // export {...} from '...';
1181
21.7k
        // An export followed by "from 'some string';" is a re-export from
1182
21.7k
        // another module identified by a URI and is treated as a
1183
21.7k
        // LT_ImportStatement (i.e. prevent wraps on it for long URIs).
1184
21.7k
        // Just "export {...};" or "export class ..." should not be treated as
1185
21.7k
        // an import in this sense.
1186
21.7k
        if (Line.First->is(tok::kw_export) &&
1187
21.7k
            
CurrentToken->is(Keywords.kw_from)936
&&
CurrentToken->Next28
&&
1188
21.7k
            
CurrentToken->Next->isStringLiteral()28
)
1189
28
          ImportStatement = true;
1190
21.7k
        if (isClosureImportStatement(*CurrentToken))
1191
24
          ImportStatement = true;
1192
21.7k
      }
1193
346k
      if (!consumeToken())
1194
56
        return LT_Invalid;
1195
346k
    }
1196
125k
    
if (125k
KeywordVirtualFound125k
)
1197
207
      return LT_VirtualFunctionDecl;
1198
125k
    if (ImportStatement)
1199
247
      return LT_ImportStatement;
1200
124k
1201
124k
    if (Line.startsWith(TT_ObjCMethodSpecifier)) {
1202
418
      if (Contexts.back().FirstObjCSelectorName)
1203
203
        Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
1204
203
            Contexts.back().LongestObjCSelectorName;
1205
418
      return LT_ObjCMethodDecl;
1206
418
    }
1207
124k
1208
124k
    return LT_Other;
1209
124k
  }
1210
1211
private:
1212
21.7k
  bool isClosureImportStatement(const FormatToken &Tok) {
1213
21.7k
    // FIXME: Closure-library specific stuff should not be hard-coded but be
1214
21.7k
    // configurable.
1215
21.7k
    return Tok.TokenText == "goog" && 
Tok.Next48
&&
Tok.Next->is(tok::period)48
&&
1216
21.7k
           
Tok.Next->Next48
&&
1217
21.7k
           
(48
Tok.Next->Next->TokenText == "module"48
||
1218
48
            
Tok.Next->Next->TokenText == "provide"40
||
1219
48
            
Tok.Next->Next->TokenText == "require"36
||
1220
48
            
Tok.Next->Next->TokenText == "requireType"28
||
1221
48
            
Tok.Next->Next->TokenText == "forwardDeclare"24
) &&
1222
21.7k
           
Tok.Next->Next->Next28
&&
Tok.Next->Next->Next->is(tok::l_paren)28
;
1223
21.7k
  }
1224
1225
701k
  void resetTokenMetadata(FormatToken *Token) {
1226
701k
    if (!Token)
1227
133k
      return;
1228
568k
1229
568k
    // Reset token type in case we have already looked at it and then
1230
568k
    // recovered from an error (e.g. failure to find the matching >).
1231
568k
    if (!CurrentToken->isOneOf(
1232
568k
            TT_LambdaLSquare, TT_LambdaLBrace, TT_ForEachMacro,
1233
568k
            TT_TypenameMacro, TT_FunctionLBrace, TT_ImplicitStringLiteral,
1234
568k
            TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow, TT_NamespaceMacro,
1235
568k
            TT_OverloadedOperator, TT_RegexLiteral, TT_TemplateString,
1236
568k
            TT_ObjCStringLiteral))
1237
554k
      CurrentToken->Type = TT_Unknown;
1238
568k
    CurrentToken->Role.reset();
1239
568k
    CurrentToken->MatchingParen = nullptr;
1240
568k
    CurrentToken->FakeLParens.clear();
1241
568k
    CurrentToken->FakeRParens = 0;
1242
568k
  }
1243
1244
568k
  void next() {
1245
568k
    if (CurrentToken) {
1246
567k
      CurrentToken->NestingLevel = Contexts.size() - 1;
1247
567k
      CurrentToken->BindingStrength = Contexts.back().BindingStrength;
1248
567k
      modifyContext(*CurrentToken);
1249
567k
      determineTokenType(*CurrentToken);
1250
567k
      CurrentToken = CurrentToken->Next;
1251
567k
    }
1252
568k
1253
568k
    resetTokenMetadata(CurrentToken);
1254
568k
  }
1255
1256
  /// A struct to hold information valid in a specific context, e.g.
1257
  /// a pair of parenthesis.
1258
  struct Context {
1259
    Context(tok::TokenKind ContextKind, unsigned BindingStrength,
1260
            bool IsExpression)
1261
        : ContextKind(ContextKind), BindingStrength(BindingStrength),
1262
196k
          IsExpression(IsExpression) {}
1263
1264
    tok::TokenKind ContextKind;
1265
    unsigned BindingStrength;
1266
    bool IsExpression;
1267
    unsigned LongestObjCSelectorName = 0;
1268
    bool ColonIsForRangeExpr = false;
1269
    bool ColonIsDictLiteral = false;
1270
    bool ColonIsObjCMethodExpr = false;
1271
    FormatToken *FirstObjCSelectorName = nullptr;
1272
    FormatToken *FirstStartOfName = nullptr;
1273
    bool CanBeExpression = true;
1274
    bool InTemplateArgument = false;
1275
    bool InCtorInitializer = false;
1276
    bool InInheritanceList = false;
1277
    bool CaretFound = false;
1278
    bool IsForEachMacro = false;
1279
    bool InCpp11AttributeSpecifier = false;
1280
    bool InCSharpAttributeSpecifier = false;
1281
  };
1282
1283
  /// Puts a new \c Context onto the stack \c Contexts for the lifetime
1284
  /// of each instance.
1285
  struct ScopedContextCreator {
1286
    AnnotatingParser &P;
1287
1288
    ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
1289
                         unsigned Increase)
1290
63.1k
        : P(P) {
1291
63.1k
      P.Contexts.push_back(Context(ContextKind,
1292
63.1k
                                   P.Contexts.back().BindingStrength + Increase,
1293
63.1k
                                   P.Contexts.back().IsExpression));
1294
63.1k
    }
1295
1296
63.1k
    ~ScopedContextCreator() { P.Contexts.pop_back(); }
1297
  };
1298
1299
567k
  void modifyContext(const FormatToken &Current) {
1300
567k
    if (Current.getPrecedence() == prec::Assignment &&
1301
567k
        
!Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return)11.1k
&&
1302
567k
        // Type aliases use `type X = ...;` in TypeScript and can be exported
1303
567k
        // using `export type ...`.
1304
567k
        
!(10.2k
Style.Language == FormatStyle::LK_JavaScript10.2k
&&
1305
10.2k
          
(1.58k
Line.startsWith(Keywords.kw_type, tok::identifier)1.58k
||
1306
1.58k
           Line.startsWith(tok::kw_export, Keywords.kw_type,
1307
1.53k
                           tok::identifier))) &&
1308
567k
        
(10.2k
!Current.Previous10.2k
||
Current.Previous->isNot(tok::kw_operator)10.2k
)) {
1309
10.1k
      Contexts.back().IsExpression = true;
1310
10.1k
      if (!Line.startsWith(TT_UnaryOperator)) {
1311
10.0k
        for (FormatToken *Previous = Current.Previous;
1312
20.9k
             Previous && 
Previous->Previous20.9k
&&
1313
20.9k
             
!Previous->Previous->isOneOf(tok::comma, tok::semi)13.8k
;
1314
13.0k
             
Previous = Previous->Previous10.8k
) {
1315
13.0k
          if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
1316
1.11k
            Previous = Previous->MatchingParen;
1317
1.11k
            if (!Previous)
1318
3
              break;
1319
13.0k
          }
1320
13.0k
          if (Previous->opensScope())
1321
2.11k
            break;
1322
10.8k
          if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
1323
10.8k
              
Previous->isOneOf(tok::star, tok::amp, tok::ampamp)80
&&
1324
10.8k
              
Previous->Previous63
&&
Previous->Previous->isNot(tok::equal)63
)
1325
63
            Previous->Type = TT_PointerOrReference;
1326
10.8k
        }
1327
10.0k
      }
1328
557k
    } else if (Current.is(tok::lessless) &&
1329
557k
               
(982
!Current.Previous982
||
!Current.Previous->is(tok::kw_operator)982
)) {
1330
958
      Contexts.back().IsExpression = true;
1331
556k
    } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
1332
3.72k
      Contexts.back().IsExpression = true;
1333
553k
    } else if (Current.is(TT_TrailingReturnArrow)) {
1334
0
      Contexts.back().IsExpression = false;
1335
553k
    } else if (Current.is(TT_LambdaArrow) || 
Current.is(Keywords.kw_assert)552k
) {
1336
504
      Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
1337
552k
    } else if (Current.Previous &&
1338
552k
               
Current.Previous->is(TT_CtorInitializerColon)423k
) {
1339
1.06k
      Contexts.back().IsExpression = true;
1340
1.06k
      Contexts.back().InCtorInitializer = true;
1341
551k
    } else if (Current.Previous && 
Current.Previous->is(TT_InheritanceColon)422k
) {
1342
796
      Contexts.back().InInheritanceList = true;
1343
550k
    } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
1344
68.4k
      for (FormatToken *Previous = Current.Previous;
1345
70.0k
           Previous && 
Previous->isOneOf(tok::star, tok::amp)70.0k
;
1346
68.4k
           
Previous = Previous->Previous1.62k
)
1347
1.62k
        Previous->Type = TT_PointerOrReference;
1348
68.4k
      if (Line.MustBeDeclaration && 
!Contexts.front().InCtorInitializer54.9k
)
1349
51.3k
        Contexts.back().IsExpression = false;
1350
482k
    } else if (Current.is(tok::kw_new)) {
1351
407
      Contexts.back().CanBeExpression = false;
1352
481k
    } else if (Current.is(tok::semi) ||
1353
481k
               
(438k
Current.is(tok::exclaim)438k
&&
Current.Previous360
&&
1354
438k
                
!Current.Previous->is(tok::kw_operator)349
)) {
1355
43.8k
      // This should be the condition or increment in a for-loop.
1356
43.8k
      // But not operator !() (can't use TT_OverloadedOperator here as its not
1357
43.8k
      // been annotated yet).
1358
43.8k
      Contexts.back().IsExpression = true;
1359
43.8k
    }
1360
567k
  }
1361
1362
42
  static FormatToken *untilMatchingParen(FormatToken *Current) {
1363
42
    // Used when `MatchingParen` is not yet established.
1364
42
    int ParenLevel = 0;
1365
308
    while (Current) {
1366
308
      if (Current->is(tok::l_paren))
1367
63
        ParenLevel++;
1368
308
      if (Current->is(tok::r_paren))
1369
63
        ParenLevel--;
1370
308
      if (ParenLevel < 1)
1371
42
        break;
1372
266
      Current = Current->Next;
1373
266
    }
1374
42
    return Current;
1375
42
  }
1376
1377
460k
  static bool isDeductionGuide(FormatToken &Current) {
1378
460k
    // Look for a deduction guide template<T> A(...) -> A<...>;
1379
460k
    if (Current.Previous && 
Current.Previous->is(tok::r_paren)330k
&&
1380
460k
        
Current.startsSequence(tok::arrow, tok::identifier, tok::less)13.9k
) {
1381
112
      // Find the TemplateCloser.
1382
112
      FormatToken *TemplateCloser = Current.Next->Next;
1383
112
      int NestingLevel = 0;
1384
441
      while (TemplateCloser) {
1385
441
        // Skip over an expressions in parens  A<(3 < 2)>;
1386
441
        if (TemplateCloser->is(tok::l_paren)) {
1387
42
          // No Matching Paren yet so skip to matching paren
1388
42
          TemplateCloser = untilMatchingParen(TemplateCloser);
1389
42
        }
1390
441
        if (TemplateCloser->is(tok::less))
1391
119
          NestingLevel++;
1392
441
        if (TemplateCloser->is(tok::greater))
1393
119
          NestingLevel--;
1394
441
        if (NestingLevel < 1)
1395
112
          break;
1396
329
        TemplateCloser = TemplateCloser->Next;
1397
329
      }
1398
112
      // Assuming we have found the end of the template ensure its followed
1399
112
      // with a semi-colon.
1400
112
      if (TemplateCloser && TemplateCloser->Next &&
1401
112
          TemplateCloser->Next->is(tok::semi) &&
1402
112
          
Current.Previous->MatchingParen98
) {
1403
98
        // Determine if the identifier `A` prior to the A<..>; is the same as
1404
98
        // prior to the A(..)
1405
98
        FormatToken *LeadingIdentifier =
1406
98
            Current.Previous->MatchingParen->Previous;
1407
98
1408
98
        // Differentiate a deduction guide by seeing the
1409
98
        // > of the template prior to the leading identifier.
1410
98
        if (LeadingIdentifier) {
1411
98
          FormatToken *PriorLeadingIdentifier = LeadingIdentifier->Previous;
1412
98
          // Skip back past explicit decoration
1413
98
          if (PriorLeadingIdentifier &&
1414
98
              
PriorLeadingIdentifier->is(tok::kw_explicit)84
)
1415
14
            PriorLeadingIdentifier = PriorLeadingIdentifier->Previous;
1416
98
1417
98
          return (PriorLeadingIdentifier &&
1418
98
                  
PriorLeadingIdentifier->is(TT_TemplateCloser)84
&&
1419
98
                  
LeadingIdentifier->TokenText == Current.Next->TokenText84
);
1420
98
        }
1421
460k
      }
1422
112
    }
1423
460k
    return false;
1424
460k
  }
1425
1426
567k
  void determineTokenType(FormatToken &Current) {
1427
567k
    if (!Current.is(TT_Unknown))
1428
29.4k
      // The token type is already known.
1429
29.4k
      return;
1430
538k
1431
538k
    if (Style.Language == FormatStyle::LK_JavaScript) {
1432
31.8k
      if (Current.is(tok::exclaim)) {
1433
92
        if (Current.Previous &&
1434
92
            
(88
Current.Previous->isOneOf(tok::identifier, tok::kw_namespace,
1435
88
                                       tok::r_paren, tok::r_square,
1436
88
                                       tok::r_brace) ||
1437
88
             
Current.Previous->Tok.isLiteral()20
)) {
1438
68
          Current.Type = TT_JsNonNullAssertion;
1439
68
          return;
1440
68
        }
1441
24
        if (Current.Next &&
1442
24
            Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
1443
0
          Current.Type = TT_JsNonNullAssertion;
1444
0
          return;
1445
0
        }
1446
538k
      }
1447
31.8k
    }
1448
538k
1449
538k
    // Line.MightBeFunctionDecl can only be true after the parentheses of a
1450
538k
    // function declaration have been found. In this case, 'Current' is a
1451
538k
    // trailing token of this declaration and thus cannot be a name.
1452
538k
    if (Current.is(Keywords.kw_instanceof)) {
1453
22
      Current.Type = TT_BinaryOperator;
1454
538k
    } else if (isStartOfName(Current) &&
1455
538k
               
(33.1k
!Line.MightBeFunctionDecl33.1k
||
Current.NestingLevel != 0563
)) {
1456
32.8k
      Contexts.back().FirstStartOfName = &Current;
1457
32.8k
      Current.Type = TT_StartOfName;
1458
505k
    } else if (Current.is(tok::semi)) {
1459
43.5k
      // Reset FirstStartOfName after finding a semicolon so that a for loop
1460
43.5k
      // with multiple increment statements is not confused with a for loop
1461
43.5k
      // having multiple variable declarations.
1462
43.5k
      Contexts.back().FirstStartOfName = nullptr;
1463
462k
    } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
1464
1.56k
      AutoFound = true;
1465
460k
    } else if (Current.is(tok::arrow) &&
1466
460k
               
Style.Language == FormatStyle::LK_Java1.06k
) {
1467
10
      Current.Type = TT_LambdaArrow;
1468
460k
    } else if (Current.is(tok::arrow) && 
AutoFound1.05k
&&
Line.MustBeDeclaration256
&&
1469
460k
               
Current.NestingLevel == 0249
&&
1470
460k
               
!Current.Previous->is(tok::kw_operator)242
) {
1471
228
      // not auto operator->() -> xxx;
1472
228
      Current.Type = TT_TrailingReturnArrow;
1473
228
1474
460k
    } else if (isDeductionGuide(Current)) {
1475
84
      // Deduction guides trailing arrow " A(...) -> A<T>;".
1476
84
      Current.Type = TT_TrailingReturnArrow;
1477
460k
    } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
1478
10.1k
      Current.Type = determineStarAmpUsage(Current,
1479
10.1k
                                           Contexts.back().CanBeExpression &&
1480
10.1k
                                               
Contexts.back().IsExpression10.0k
,
1481
10.1k
                                           Contexts.back().InTemplateArgument);
1482
450k
    } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
1483
2.86k
      Current.Type = determinePlusMinusCaretUsage(Current);
1484
2.86k
      if (Current.is(TT_UnaryOperator) && 
Current.is(tok::caret)1.14k
)
1485
415
        Contexts.back().CaretFound = true;
1486
447k
    } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
1487
705
      Current.Type = determineIncrementUsage(Current);
1488
446k
    } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
1489
478
      Current.Type = TT_UnaryOperator;
1490
446k
    } else if (Current.is(tok::question)) {
1491
765
      if (Style.Language == FormatStyle::LK_JavaScript &&
1492
765
          
Line.MustBeDeclaration102
&&
!Contexts.back().IsExpression24
) {
1493
16
        // In JavaScript, `interface X { foo?(): bar; }` is an optional method
1494
16
        // on the interface, not a ternary expression.
1495
16
        Current.Type = TT_JsTypeOptionalQuestion;
1496
749
      } else {
1497
749
        Current.Type = TT_ConditionalExpr;
1498
749
      }
1499
445k
    } else if (Current.isBinaryOperator() &&
1500
445k
               
(19.6k
!Current.Previous19.6k
||
Current.Previous->isNot(tok::l_square)19.6k
) &&
1501
445k
               
(19.5k
!Current.is(tok::greater)19.5k
&&
1502
19.5k
                
Style.Language != FormatStyle::LK_TextProto19.1k
)) {
1503
18.9k
      Current.Type = TT_BinaryOperator;
1504
426k
    } else if (Current.is(tok::comment)) {
1505
9.42k
      if (Current.TokenText.startswith("/*")) {
1506
1.47k
        if (Current.TokenText.endswith("*/"))
1507
1.46k
          Current.Type = TT_BlockComment;
1508
3
        else
1509
3
          // The lexer has for some reason determined a comment here. But we
1510
3
          // cannot really handle it, if it isn't properly terminated.
1511
3
          Current.Tok.setKind(tok::unknown);
1512
7.95k
      } else {
1513
7.95k
        Current.Type = TT_LineComment;
1514
7.95k
      }
1515
416k
    } else if (Current.is(tok::r_paren)) {
1516
42.5k
      if (rParenEndsCast(Current))
1517
1.25k
        Current.Type = TT_CastRParen;
1518
42.5k
      if (Current.MatchingParen && 
Current.Next42.4k
&&
1519
42.5k
          
!Current.Next->isBinaryOperator()37.3k
&&
1520
42.5k
          !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
1521
35.0k
                                 tok::comma, tok::period, tok::arrow,
1522
35.0k
                                 tok::coloncolon))
1523
5.44k
        if (FormatToken *AfterParen = Current.MatchingParen->Next) {
1524
5.44k
          // Make sure this isn't the return type of an Obj-C block declaration
1525
5.44k
          if (AfterParen->Tok.isNot(tok::caret)) {
1526
5.33k
            if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
1527
5.21k
              if (BeforeParen->is(tok::identifier) &&
1528
5.21k
                  
!BeforeParen->is(TT_TypenameMacro)2.43k
&&
1529
5.21k
                  
BeforeParen->TokenText == BeforeParen->TokenText.upper()2.37k
&&
1530
5.21k
                  
(242
!BeforeParen->Previous242
||
1531
242
                   
BeforeParen->Previous->ClosesTemplateDeclaration198
))
1532
65
                Current.Type = TT_FunctionAnnotationRParen;
1533
5.33k
          }
1534
5.44k
        }
1535
374k
    } else if (Current.is(tok::at) && 
Current.Next1.03k
&&
1536
374k
               
Style.Language != FormatStyle::LK_JavaScript1.03k
&&
1537
374k
               
Style.Language != FormatStyle::LK_Java989
) {
1538
893
      // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
1539
893
      // marks declarations and properties that need special formatting.
1540
893
      switch (Current.Next->Tok.getObjCKeywordID()) {
1541
198
      case tok::objc_interface:
1542
198
      case tok::objc_implementation:
1543
198
      case tok::objc_protocol:
1544
198
        Current.Type = TT_ObjCDecl;
1545
198
        break;
1546
198
      case tok::objc_property:
1547
80
        Current.Type = TT_ObjCProperty;
1548
80
        break;
1549
615
      default:
1550
615
        break;
1551
373k
      }
1552
373k
    } else if (Current.is(tok::period)) {
1553
3.10k
      FormatToken *PreviousNoComment = Current.getPreviousNonComment();
1554
3.10k
      if (PreviousNoComment &&
1555
3.10k
          PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
1556
126
        Current.Type = TT_DesignatedInitializerPeriod;
1557
2.97k
      else if (Style.Language == FormatStyle::LK_Java && 
Current.Previous92
&&
1558
2.97k
               Current.Previous->isOneOf(TT_JavaAnnotation,
1559
92
                                         TT_LeadingJavaAnnotation)) {
1560
28
        Current.Type = Current.Previous->Type;
1561
28
      }
1562
370k
    } else if (canBeObjCSelectorComponent(Current) &&
1563
370k
               // FIXME(bug 36976): ObjC return types shouldn't use
1564
370k
               // TT_CastRParen.
1565
370k
               
Current.Previous177k
&&
Current.Previous->is(TT_CastRParen)116k
&&
1566
370k
               
Current.Previous->MatchingParen982
&&
1567
370k
               
Current.Previous->MatchingParen->Previous982
&&
1568
370k
               Current.Previous->MatchingParen->Previous->is(
1569
971
                   TT_ObjCMethodSpecifier)) {
1570
371
      // This is the first part of an Objective-C selector name. (If there's no
1571
371
      // colon after this, this is the only place which annotates the identifier
1572
371
      // as a selector.)
1573
371
      Current.Type = TT_SelectorName;
1574
370k
    } else if (Current.isOneOf(tok::identifier, tok::kw_const,
1575
370k
                               tok::kw_noexcept) &&
1576
370k
               
Current.Previous114k
&&
1577
370k
               
!Current.Previous->isOneOf(tok::equal, tok::at)94.6k
&&
1578
370k
               
Line.MightBeFunctionDecl90.5k
&&
Contexts.size() == 19.57k
) {
1579
4.97k
      // Line.MightBeFunctionDecl can only be true after the parentheses of a
1580
4.97k
      // function declaration have been found.
1581
4.97k
      Current.Type = TT_TrailingAnnotation;
1582
365k
    } else if ((Style.Language == FormatStyle::LK_Java ||
1583
365k
                
Style.Language == FormatStyle::LK_JavaScript362k
) &&
1584
365k
               
Current.Previous24.4k
) {
1585
16.3k
      if (Current.Previous->is(tok::at) &&
1586
16.3k
          
Current.isNot(Keywords.kw_interface)140
) {
1587
132
        const FormatToken &AtToken = *Current.Previous;
1588
132
        const FormatToken *Previous = AtToken.getPreviousNonComment();
1589
132
        if (!Previous || 
Previous->is(TT_LeadingJavaAnnotation)48
)
1590
106
          Current.Type = TT_LeadingJavaAnnotation;
1591
26
        else
1592
26
          Current.Type = TT_JavaAnnotation;
1593
16.2k
      } else if (Current.Previous->is(tok::period) &&
1594
16.2k
                 Current.Previous->isOneOf(TT_JavaAnnotation,
1595
656
                                           TT_LeadingJavaAnnotation)) {
1596
28
        Current.Type = Current.Previous->Type;
1597
28
      }
1598
16.3k
    }
1599
538k
  }
1600
1601
  /// Take a guess at whether \p Tok starts a name of a function or
1602
  /// variable declaration.
1603
  ///
1604
  /// This is a heuristic based on whether \p Tok is an identifier following
1605
  /// something that is likely a type.
1606
538k
  bool isStartOfName(const FormatToken &Tok) {
1607
538k
    if (Tok.isNot(tok::identifier) || 
!Tok.Previous145k
)
1608
412k
      return false;
1609
126k
1610
126k
    if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
1611
126k
                              Keywords.kw_as))
1612
144
      return false;
1613
125k
    if (Style.Language == FormatStyle::LK_JavaScript &&
1614
125k
        
Tok.Previous->is(Keywords.kw_in)7.30k
)
1615
32
      return false;
1616
125k
1617
125k
    // Skip "const" as it does not have an influence on whether this is a name.
1618
125k
    FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
1619
127k
    while (PreviousNotConst && 
PreviousNotConst->is(tok::kw_const)126k
)
1620
1.19k
      PreviousNotConst = PreviousNotConst->getPreviousNonComment();
1621
125k
1622
125k
    if (!PreviousNotConst)
1623
243
      return false;
1624
125k
1625
125k
    bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
1626
125k
                       
PreviousNotConst->Previous11.6k
&&
1627
125k
                       
PreviousNotConst->Previous->is(tok::hash)8.29k
;
1628
125k
1629
125k
    if (PreviousNotConst->is(TT_TemplateCloser))
1630
1.57k
      return PreviousNotConst && PreviousNotConst->MatchingParen &&
1631
1.57k
             PreviousNotConst->MatchingParen->Previous &&
1632
1.57k
             PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
1633
1.57k
             
PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template)1.56k
;
1634
124k
1635
124k
    if (PreviousNotConst->is(tok::r_paren) && 
PreviousNotConst->MatchingParen1.63k
&&
1636
124k
        
PreviousNotConst->MatchingParen->Previous1.63k
&&
1637
124k
        
PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype)1.60k
)
1638
21
      return true;
1639
123k
1640
123k
    return (!IsPPKeyword &&
1641
123k
            
PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto)121k
) ||
1642
123k
           
PreviousNotConst->is(TT_PointerOrReference)113k
||
1643
123k
           
PreviousNotConst->isSimpleTypeSpecifier()110k
;
1644
123k
  }
1645
1646
  /// Determine whether ')' is ending a cast.
1647
42.5k
  bool rParenEndsCast(const FormatToken &Tok) {
1648
42.5k
    // C-style casts are only used in C++, C# and Java.
1649
42.5k
    if (!Style.isCSharp() && 
!Style.isCpp()42.3k
&&
1650
42.5k
        
Style.Language != FormatStyle::LK_Java2.46k
)
1651
2.17k
      return false;
1652
40.3k
1653
40.3k
    // Empty parens aren't casts and there are no casts at the end of the line.
1654
40.3k
    if (Tok.Previous == Tok.MatchingParen || 
!Tok.Next22.5k
||
!Tok.MatchingParen18.5k
)
1655
21.7k
      return false;
1656
18.5k
1657
18.5k
    FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
1658
18.5k
    if (LeftOfParens) {
1659
18.3k
      // If there is a closing parenthesis left of the current parentheses,
1660
18.3k
      // look past it as these might be chained casts.
1661
18.3k
      if (LeftOfParens->is(tok::r_paren)) {
1662
471
        if (!LeftOfParens->MatchingParen ||
1663
471
            !LeftOfParens->MatchingParen->Previous)
1664
35
          return false;
1665
436
        LeftOfParens = LeftOfParens->MatchingParen->Previous;
1666
436
      }
1667
18.3k
1668
18.3k
      // If there is an identifier (or with a few exceptions a keyword) right
1669
18.3k
      // before the parentheses, this is unlikely to be a cast.
1670
18.3k
      
if (18.3k
LeftOfParens->Tok.getIdentifierInfo()18.3k
&&
1671
18.3k
          !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
1672
14.7k
                                 tok::kw_delete))
1673
14.5k
        return false;
1674
3.82k
1675
3.82k
      // Certain other tokens right before the parentheses are also signals that
1676
3.82k
      // this cannot be a cast.
1677
3.82k
      if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
1678
3.82k
                                TT_TemplateCloser, tok::ellipsis))
1679
918
        return false;
1680
3.08k
    }
1681
3.08k
1682
3.08k
    if (Tok.Next->is(tok::question))
1683
36
      return false;
1684
3.04k
1685
3.04k
    // Functions which end with decorations like volatile, noexcept are unlikely
1686
3.04k
    // to be casts.
1687
3.04k
    if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
1688
3.04k
                          tok::kw_throw, tok::arrow, Keywords.kw_override,
1689
3.04k
                          Keywords.kw_final) ||
1690
3.04k
        
isCpp11AttributeSpecifier(*Tok.Next)2.88k
)
1691
174
      return false;
1692
2.87k
1693
2.87k
    // As Java has no function types, a "(" after the ")" likely means that this
1694
2.87k
    // is a cast.
1695
2.87k
    if (Style.Language == FormatStyle::LK_Java && 
Tok.Next->is(tok::l_paren)12
)
1696
2
      return true;
1697
2.87k
1698
2.87k
    // If a (non-string) literal follows, this is likely a cast.
1699
2.87k
    if (Tok.Next->isNot(tok::string_literal) &&
1700
2.87k
        (Tok.Next->Tok.isLiteral() ||
1701
2.87k
         
Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)2.78k
))
1702
105
      return true;
1703
2.76k
1704
2.76k
    // Heuristically try to determine whether the parentheses contain a type.
1705
2.76k
    bool ParensAreType =
1706
2.76k
        !Tok.Previous ||
1707
2.76k
        Tok.Previous->isOneOf(TT_PointerOrReference, TT_TemplateCloser) ||
1708
2.76k
        
Tok.Previous->isSimpleTypeSpecifier()2.46k
;
1709
2.76k
    bool ParensCouldEndDecl =
1710
2.76k
        Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
1711
2.76k
    if (ParensAreType && 
!ParensCouldEndDecl649
)
1712
607
      return true;
1713
2.16k
1714
2.16k
    // At this point, we heuristically assume that there are no casts at the
1715
2.16k
    // start of the line. We assume that we have found most cases where there
1716
2.16k
    // are by the logic above, e.g. "(void)x;".
1717
2.16k
    if (!LeftOfParens)
1718
143
      return false;
1719
2.01k
1720
2.01k
    // Certain token types inside the parentheses mean that this can't be a
1721
2.01k
    // cast.
1722
5.88k
    
for (const FormatToken *Token = Tok.MatchingParen->Next; 2.01k
Token != &Tok;
1723
3.86k
         Token = Token->Next)
1724
4.51k
      if (Token->is(TT_BinaryOperator))
1725
656
        return false;
1726
2.01k
1727
2.01k
    // If the following token is an identifier or 'this', this is a cast. All
1728
2.01k
    // cases where this can be something else are handled above.
1729
2.01k
    
if (1.36k
Tok.Next->isOneOf(tok::identifier, tok::kw_this)1.36k
)
1730
482
      return true;
1731
880
1732
880
    if (!Tok.Next->Next)
1733
190
      return false;
1734
690
1735
690
    // If the next token after the parenthesis is a unary operator, assume
1736
690
    // that this is cast, unless there are unexpected tokens inside the
1737
690
    // parenthesis.
1738
690
    bool NextIsUnary =
1739
690
        Tok.Next->isUnaryOperator() || 
Tok.Next->isOneOf(tok::amp, tok::star)641
;
1740
690
    if (!NextIsUnary || 
Tok.Next->is(tok::plus)77
||
1741
690
        
!Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant)70
)
1742
627
      return false;
1743
63
    // Search for unexpected tokens.
1744
147
    
for (FormatToken *Prev = Tok.Previous; 63
Prev != Tok.MatchingParen;
1745
84
         Prev = Prev->Previous) {
1746
84
      if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
1747
0
        return false;
1748
84
    }
1749
63
    return true;
1750
63
  }
1751
1752
  /// Return the type of the given token assuming it is * or &.
1753
  TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
1754
10.1k
                                  bool InTemplateArgument) {
1755
10.1k
    if (Style.Language == FormatStyle::LK_JavaScript)
1756
86
      return TT_BinaryOperator;
1757
10.0k
1758
10.0k
    const FormatToken *PrevToken = Tok.getPreviousNonComment();
1759
10.0k
    if (!PrevToken)
1760
119
      return TT_UnaryOperator;
1761
9.96k
1762
9.96k
    const FormatToken *NextToken = Tok.getNextNonComment();
1763
9.96k
    if (!NextToken ||
1764
9.96k
        NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_const,
1765
9.95k
                           tok::kw_noexcept) ||
1766
9.96k
        
(9.64k
NextToken->is(tok::l_brace)9.64k
&&
!NextToken->getNextNonComment()70
))
1767
357
      return TT_PointerOrReference;
1768
9.60k
1769
9.60k
    if (PrevToken->is(tok::coloncolon))
1770
67
      return TT_PointerOrReference;
1771
9.53k
1772
9.53k
    if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
1773
9.53k
                           tok::comma, tok::semi, tok::kw_return, tok::colon,
1774
9.53k
                           tok::equal, tok::kw_delete, tok::kw_sizeof,
1775
9.53k
                           tok::kw_throw) ||
1776
9.53k
        PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
1777
8.10k
                           TT_UnaryOperator, TT_CastRParen))
1778
1.65k
      return TT_UnaryOperator;
1779
7.88k
1780
7.88k
    if (NextToken->is(tok::l_square) && 
NextToken->isNot(TT_LambdaLSquare)243
)
1781
236
      return TT_PointerOrReference;
1782
7.64k
    if (NextToken->is(tok::kw_operator) && 
!IsExpression164
)
1783
157
      return TT_PointerOrReference;
1784
7.49k
    if (NextToken->isOneOf(tok::comma, tok::semi))
1785
320
      return TT_PointerOrReference;
1786
7.17k
1787
7.17k
    if (PrevToken->is(tok::r_paren) && 
PrevToken->MatchingParen378
) {
1788
378
      FormatToken *TokenBeforeMatchingParen =
1789
378
          PrevToken->MatchingParen->getPreviousNonComment();
1790
378
      if (TokenBeforeMatchingParen &&
1791
378
          TokenBeforeMatchingParen->isOneOf(tok::kw_typeof, tok::kw_decltype,
1792
364
                                            TT_TypenameMacro))
1793
84
        return TT_PointerOrReference;
1794
7.08k
    }
1795
7.08k
1796
7.08k
    if (PrevToken->Tok.isLiteral() ||
1797
7.08k
        PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
1798
6.98k
                           tok::kw_false, tok::r_brace) ||
1799
7.08k
        
NextToken->Tok.isLiteral()6.65k
||
1800
7.08k
        
NextToken->isOneOf(tok::kw_true, tok::kw_false)6.59k
||
1801
7.08k
        
NextToken->isUnaryOperator()6.59k
||
1802
7.08k
        // If we know we're in a template argument, there are no named
1803
7.08k
        // declarations. Thus, having an identifier on the right-hand side
1804
7.08k
        // indicates a binary operator.
1805
7.08k
        
(6.43k
InTemplateArgument6.43k
&&
NextToken->Tok.isAnyIdentifier()504
))
1806
690
      return TT_BinaryOperator;
1807
6.39k
1808
6.39k
    // "&&(" is quite unlikely to be two successive unary "&".
1809
6.39k
    if (Tok.is(tok::ampamp) && 
NextToken->is(tok::l_paren)780
)
1810
217
      return TT_BinaryOperator;
1811
6.18k
1812
6.18k
    // This catches some cases where evaluation order is used as control flow:
1813
6.18k
    //   aaa && aaa->f();
1814
6.18k
    if (NextToken->Tok.isAnyIdentifier()) {
1815
4.26k
      const FormatToken *NextNextToken = NextToken->getNextNonComment();
1816
4.26k
      if (NextNextToken && 
NextNextToken->is(tok::arrow)4.22k
)
1817
14
        return TT_BinaryOperator;
1818
6.16k
    }
1819
6.16k
1820
6.16k
    // It is very unlikely that we are going to find a pointer or reference type
1821
6.16k
    // definition on the RHS of an assignment.
1822
6.16k
    if (IsExpression && 
!Contexts.back().CaretFound1.34k
)
1823
1.33k
      return TT_BinaryOperator;
1824
4.83k
1825
4.83k
    return TT_PointerOrReference;
1826
4.83k
  }
1827
1828
2.86k
  TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
1829
2.86k
    const FormatToken *PrevToken = Tok.getPreviousNonComment();
1830
2.86k
    if (!PrevToken)
1831
435
      return TT_UnaryOperator;
1832
2.43k
1833
2.43k
    if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
1834
74
      // This must be a sequence of leading unary operators.
1835
74
      return TT_UnaryOperator;
1836
2.35k
1837
2.35k
    // Use heuristics to recognize unary operators.
1838
2.35k
    if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
1839
2.35k
                           tok::question, tok::colon, tok::kw_return,
1840
2.35k
                           tok::kw_case, tok::at, tok::l_brace, tok::kw_throw,
1841
2.35k
                           tok::kw_co_return, tok::kw_co_yield))
1842
559
      return TT_UnaryOperator;
1843
1.79k
1844
1.79k
    // There can't be two consecutive binary operators.
1845
1.79k
    if (PrevToken->is(TT_BinaryOperator))
1846
74
      return TT_UnaryOperator;
1847
1.72k
1848
1.72k
    // Fall back to marking the token as binary operator.
1849
1.72k
    return TT_BinaryOperator;
1850
1.72k
  }
1851
1852
  /// Determine whether ++/-- are pre- or post-increments/-decrements.
1853
705
  TokenType determineIncrementUsage(const FormatToken &Tok) {
1854
705
    const FormatToken *PrevToken = Tok.getPreviousNonComment();
1855
705
    if (!PrevToken || 
PrevToken->is(TT_CastRParen)670
)
1856
49
      return TT_UnaryOperator;
1857
656
    if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
1858
244
      return TT_TrailingUnaryOperator;
1859
412
1860
412
    return TT_UnaryOperator;
1861
412
  }
1862
1863
  SmallVector<Context, 8> Contexts;
1864
1865
  const FormatStyle &Style;
1866
  AnnotatedLine &Line;
1867
  FormatToken *CurrentToken;
1868
  bool AutoFound;
1869
  const AdditionalKeywords &Keywords;
1870
1871
  // Set of "<" tokens that do not open a template parameter list. If parseAngle
1872
  // determines that a specific token can't be a template opener, it will make
1873
  // same decision irrespective of the decisions for tokens leading up to it.
1874
  // Store this information to prevent this from causing exponential runtime.
1875
  llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
1876
};
1877
1878
static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
1879
static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
1880
1881
/// Parses binary expressions by inserting fake parenthesis based on
1882
/// operator precedence.
1883
class ExpressionParser {
1884
public:
1885
  ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
1886
                   AnnotatedLine &Line)
1887
133k
      : Style(Style), Keywords(Keywords), Current(Line.First) {}
1888
1889
  /// Parse expressions with the given operator precedence.
1890
4.66M
  void parse(int Precedence = 0) {
1891
4.66M
    // Skip 'return' and ObjC selector colons as they are not part of a binary
1892
4.66M
    // expression.
1893
4.67M
    while (Current && 
(4.64M
Current->is(tok::kw_return)4.64M
||
1894
4.64M
                       
(4.64M
Current->is(tok::colon)4.64M
&&
1895
4.64M
                        
Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)10.4k
)))
1896
7.28k
      next();
1897
4.66M
1898
4.66M
    if (!Current || 
Precedence > PrecedenceArrowAndPeriod4.64M
)
1899
570k
      return;
1900
4.09M
1901
4.09M
    // Conditional expressions need to be parsed separately for proper nesting.
1902
4.09M
    if (Precedence == prec::Conditional) {
1903
231k
      parseConditionalExpr();
1904
231k
      return;
1905
231k
    }
1906
3.86M
1907
3.86M
    // Parse unary operators, which all have a higher precedence than binary
1908
3.86M
    // operators.
1909
3.86M
    if (Precedence == PrecedenceUnaryOperator) {
1910
236k
      parseUnaryOperator();
1911
236k
      return;
1912
236k
    }
1913
3.62M
1914
3.62M
    FormatToken *Start = Current;
1915
3.62M
    FormatToken *LatestOperator = nullptr;
1916
3.62M
    unsigned OperatorIndex = 0;
1917
3.62M
1918
4.11M
    while (Current) {
1919
3.97M
      // Consume operators with higher precedence.
1920
3.97M
      parse(Precedence + 1);
1921
3.97M
1922
3.97M
      int CurrentPrecedence = getCurrentPrecedence();
1923
3.97M
1924
3.97M
      if (Current && 
Current->is(TT_SelectorName)2.64M
&&
1925
3.97M
          
Precedence == CurrentPrecedence60.9k
) {
1926
4.12k
        if (LatestOperator)
1927
1.14k
          addFakeParenthesis(Start, prec::Level(Precedence));
1928
4.12k
        Start = Current;
1929
4.12k
      }
1930
3.97M
1931
3.97M
      // At the end of the line or when an operator with higher precedence is
1932
3.97M
      // found, insert fake parenthesis and return.
1933
3.97M
      if (!Current ||
1934
3.97M
          
(2.64M
Current->closesScope()2.64M
&&
1935
2.64M
           
(1.02M
Current->MatchingParen1.02M
||
Current->is(TT_TemplateString)23.0k
)) ||
1936
3.97M
          
(1.64M
CurrentPrecedence != -11.64M
&&
CurrentPrecedence < Precedence1.25M
) ||
1937
3.97M
          
(483k
CurrentPrecedence == prec::Conditional483k
&&
1938
3.49M
           
Precedence == prec::Assignment699
&&
Current->is(tok::colon)699
)) {
1939
3.49M
        break;
1940
3.49M
      }
1941
482k
1942
482k
      // Consume scopes: (), [], <> and {}
1943
482k
      if (Current->opensScope()) {
1944
83.9k
        // In fragment of a JavaScript template string can look like '}..${' and
1945
83.9k
        // thus close a scope and open a new one at the same time.
1946
167k
        while (Current && 
(146k
!Current->closesScope()146k
||
Current->opensScope()62.4k
)) {
1947
83.9k
          next();
1948
83.9k
          parse();
1949
83.9k
        }
1950
83.9k
        next();
1951
398k
      } else {
1952
398k
        // Operator found.
1953
398k
        if (CurrentPrecedence == Precedence) {
1954
88.5k
          if (LatestOperator)
1955
12.3k
            LatestOperator->NextOperator = Current;
1956
88.5k
          LatestOperator = Current;
1957
88.5k
          Current->OperatorIndex = OperatorIndex;
1958
88.5k
          ++OperatorIndex;
1959
88.5k
        }
1960
398k
        next(/*SkipPastLeadingComments=*/Precedence > 0);
1961
398k
      }
1962
482k
    }
1963
3.62M
1964
3.62M
    if (LatestOperator && 
(76.2k
Current76.2k
||
Precedence > 045.6k
)) {
1965
33.5k
      // LatestOperator->LastOperator = true;
1966
33.5k
      if (Precedence == PrecedenceArrowAndPeriod) {
1967
3.34k
        // Call expressions don't have a binary operator precedence.
1968
3.34k
        addFakeParenthesis(Start, prec::Unknown);
1969
30.1k
      } else {
1970
30.1k
        addFakeParenthesis(Start, prec::Level(Precedence));
1971
30.1k
      }
1972
33.5k
    }
1973
3.62M
  }
1974
1975
private:
1976
  /// Gets the precedence (+1) of the given token for binary operators
1977
  /// and other tokens that we treat like binary operators.
1978
3.97M
  int getCurrentPrecedence() {
1979
3.97M
    if (Current) {
1980
2.64M
      const FormatToken *NextNonComment = Current->getNextNonComment();
1981
2.64M
      if (Current->is(TT_ConditionalExpr))
1982
18.8k
        return prec::Conditional;
1983
2.62M
      if (NextNonComment && 
Current->is(TT_SelectorName)1.76M
&&
1984
2.62M
          
(60.9k
NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon)60.9k
||
1985
60.9k
           
(23.8k
(23.8k
Style.Language == FormatStyle::LK_Proto23.8k
||
1986
23.8k
             
Style.Language == FormatStyle::LK_TextProto23.7k
) &&
1987
23.8k
            
NextNonComment->is(tok::less)544
)))
1988
37.0k
        return prec::Assignment;
1989
2.59M
      if (Current->is(TT_JsComputedPropertyName))
1990
280
        return prec::Assignment;
1991
2.59M
      if (Current->is(TT_LambdaArrow))
1992
7.60k
        return prec::Comma;
1993
2.58M
      if (Current->is(TT_JsFatArrow))
1994
2.15k
        return prec::Assignment;
1995
2.58M
      if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
1996
2.58M
          
(1.85M
Current->is(tok::comment)1.85M
&&
NextNonComment622
&&
1997
1.85M
           
NextNonComment->is(TT_SelectorName)607
))
1998
721k
        return 0;
1999
1.85M
      if (Current->is(TT_RangeBasedForLoopColon))
2000
1.53k
        return prec::Comma;
2001
1.85M
      if ((Style.Language == FormatStyle::LK_Java ||
2002
1.85M
           
Style.Language == FormatStyle::LK_JavaScript1.84M
) &&
2003
1.85M
          
Current->is(Keywords.kw_instanceof)136k
)
2004
154
        return prec::Relational;
2005
1.85M
      if (Style.Language == FormatStyle::LK_JavaScript &&
2006
1.85M
          
Current->isOneOf(Keywords.kw_in, Keywords.kw_as)122k
)
2007
1.06k
        return prec::Relational;
2008
1.85M
      if (Current->is(TT_BinaryOperator) || 
Current->is(tok::comma)1.65M
)
2009
458k
        return Current->getPrecedence();
2010
1.39M
      if (Current->isOneOf(tok::period, tok::arrow))
2011
4.37k
        return PrecedenceArrowAndPeriod;
2012
1.39M
      if ((Style.Language == FormatStyle::LK_Java ||
2013
1.39M
           
Style.Language == FormatStyle::LK_JavaScript1.38M
) &&
2014
1.39M
          Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
2015
92.1k
                           Keywords.kw_throws))
2016
1.57k
        return 0;
2017
2.72M
    }
2018
2.72M
    return -1;
2019
2.72M
  }
2020
2021
38.1k
  void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
2022
38.1k
    Start->FakeLParens.push_back(Precedence);
2023
38.1k
    if (Precedence > prec::Unknown)
2024
30.4k
      Start->StartsBinaryExpression = true;
2025
38.1k
    if (Current) {
2026
35.1k
      FormatToken *Previous = Current->Previous;
2027
35.3k
      while (Previous->is(tok::comment) && 
Previous->Previous259
)
2028
259
        Previous = Previous->Previous;
2029
35.1k
      ++Previous->FakeRParens;
2030
35.1k
      if (Precedence > prec::Unknown)
2031
27.8k
        Previous->EndsBinaryExpression = true;
2032
35.1k
    }
2033
38.1k
  }
2034
2035
  /// Parse unary operator expressions and surround them with fake
2036
  /// parentheses if appropriate.
2037
236k
  void parseUnaryOperator() {
2038
236k
    llvm::SmallVector<FormatToken *, 2> Tokens;
2039
239k
    while (Current && Current->is(TT_UnaryOperator)) {
2040
2.74k
      Tokens.push_back(Current);
2041
2.74k
      next();
2042
2.74k
    }
2043
236k
    parse(PrecedenceArrowAndPeriod);
2044
236k
    for (FormatToken *Token : llvm::reverse(Tokens))
2045
2.74k
      // The actual precedence doesn't matter.
2046
2.74k
      addFakeParenthesis(Token, prec::Unknown);
2047
236k
  }
2048
2049
231k
  void parseConditionalExpr() {
2050
233k
    while (Current && 
Current->isTrailingComment()231k
) {
2051
2.33k
      next();
2052
2.33k
    }
2053
231k
    FormatToken *Start = Current;
2054
231k
    parse(prec::LogicalOr);
2055
231k
    if (!Current || 
!Current->is(tok::question)140k
)
2056
230k
      return;
2057
711
    next();
2058
711
    parse(prec::Assignment);
2059
711
    if (!Current || 
Current->isNot(TT_ConditionalExpr)702
)
2060
26
      return;
2061
685
    next();
2062
685
    parse(prec::Assignment);
2063
685
    addFakeParenthesis(Start, prec::Conditional);
2064
685
  }
2065
2066
580k
  void next(bool SkipPastLeadingComments = true) {
2067
580k
    if (Current)
2068
558k
      Current = Current->Next;
2069
586k
    while (Current &&
2070
586k
           
(432k
Current->NewlinesBefore == 0432k
||
SkipPastLeadingComments13.3k
) &&
2071
586k
           
Current->isTrailingComment()432k
)
2072
6.50k
      Current = Current->Next;
2073
580k
  }
2074
2075
  const FormatStyle &Style;
2076
  const AdditionalKeywords &Keywords;
2077
  FormatToken *Current;
2078
};
2079
2080
} // end anonymous namespace
2081
2082
void TokenAnnotator::setCommentLineLevels(
2083
70.7k
    SmallVectorImpl<AnnotatedLine *> &Lines) {
2084
70.7k
  const AnnotatedLine *NextNonCommentLine = nullptr;
2085
70.7k
  for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
2086
70.7k
                                                          E = Lines.rend();
2087
126k
       I != E; 
++I55.9k
) {
2088
55.9k
    bool CommentLine = true;
2089
57.3k
    for (const FormatToken *Tok = (*I)->First; Tok; 
Tok = Tok->Next1.41k
) {
2090
56.4k
      if (!Tok->is(tok::comment)) {
2091
55.0k
        CommentLine = false;
2092
55.0k
        break;
2093
55.0k
      }
2094
56.4k
    }
2095
55.9k
2096
55.9k
    // If the comment is currently aligned with the line immediately following
2097
55.9k
    // it, that's probably intentional and we should keep it.
2098
55.9k
    if (NextNonCommentLine && 
CommentLine31.7k
&&
2099
55.9k
        
NextNonCommentLine->First->NewlinesBefore <= 1729
&&
2100
55.9k
        NextNonCommentLine->First->OriginalColumn ==
2101
674
            (*I)->First->OriginalColumn) {
2102
365
      // Align comments for preprocessor lines with the # in column 0 if
2103
365
      // preprocessor lines are not indented. Otherwise, align with the next
2104
365
      // line.
2105
365
      (*I)->Level =
2106
365
          (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
2107
365
           
(349
NextNonCommentLine->Type == LT_PreprocessorDirective349
||
2108
349
            
NextNonCommentLine->Type == LT_ImportStatement328
))
2109
365
              ? 
042
2110
365
              : 
NextNonCommentLine->Level323
;
2111
55.5k
    } else {
2112
55.5k
      NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? 
(*I)47.0k
:
nullptr8.56k
;
2113
55.5k
    }
2114
55.9k
2115
55.9k
    setCommentLineLevels((*I)->Children);
2116
55.9k
  }
2117
70.7k
}
2118
2119
133k
static unsigned maxNestingDepth(const AnnotatedLine &Line) {
2120
133k
  unsigned Result = 0;
2121
700k
  for (const auto *Tok = Line.First; Tok != nullptr; 
Tok = Tok->Next567k
)
2122
567k
    Result = std::max(Result, Tok->NestingLevel);
2123
133k
  return Result;
2124
133k
}
2125
2126
133k
void TokenAnnotator::annotate(AnnotatedLine &Line) {
2127
133k
  for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
2128
133k
                                                  E = Line.Children.end();
2129
136k
       I != E; 
++I2.86k
) {
2130
2.86k
    annotate(**I);
2131
2.86k
  }
2132
133k
  AnnotatingParser Parser(Style, Line, Keywords);
2133
133k
  Line.Type = Parser.parseLine();
2134
133k
2135
133k
  // With very deep nesting, ExpressionParser uses lots of stack and the
2136
133k
  // formatting algorithm is very slow. We're not going to do a good job here
2137
133k
  // anyway - it's probably generated code being formatted by mistake.
2138
133k
  // Just skip the whole line.
2139
133k
  if (maxNestingDepth(Line) > 50)
2140
3
    Line.Type = LT_Invalid;
2141
133k
2142
133k
  if (Line.Type == LT_Invalid)
2143
59
    return;
2144
133k
2145
133k
  ExpressionParser ExprParser(Style, Keywords, Line);
2146
133k
  ExprParser.parse();
2147
133k
2148
133k
  if (Line.startsWith(TT_ObjCMethodSpecifier))
2149
418
    Line.Type = LT_ObjCMethodDecl;
2150
132k
  else if (Line.startsWith(TT_ObjCDecl))
2151
192
    Line.Type = LT_ObjCDecl;
2152
132k
  else if (Line.startsWith(TT_ObjCProperty))
2153
80
    Line.Type = LT_ObjCProperty;
2154
133k
2155
133k
  Line.First->SpacesRequiredBefore = 1;
2156
133k
  Line.First->CanBreakBefore = Line.First->MustBreakBefore;
2157
133k
}
2158
2159
// This function heuristically determines whether 'Current' starts the name of a
2160
// function declaration.
2161
static bool isFunctionDeclarationName(const FormatToken &Current,
2162
189k
                                      const AnnotatedLine &Line) {
2163
189k
  auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
2164
731
    for (; Next; 
Next = Next->Next374
) {
2165
731
      if (Next->is(TT_OverloadedOperatorLParen))
2166
339
        return Next;
2167
392
      if (Next->is(TT_OverloadedOperator))
2168
251
        continue;
2169
141
      if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
2170
78
        // For 'new[]' and 'delete[]'.
2171
78
        if (Next->Next &&
2172
78
            Next->Next->startsSequence(tok::l_square, tok::r_square))
2173
9
          Next = Next->Next->Next;
2174
78
        continue;
2175
78
      }
2176
63
      if (Next->startsSequence(tok::l_square, tok::r_square)) {
2177
21
        // For operator[]().
2178
21
        Next = Next->Next;
2179
21
        continue;
2180
21
      }
2181
42
      if ((Next->isSimpleTypeSpecifier() || 
Next->is(tok::identifier)18
) &&
2182
42
          
Next->Next24
&&
Next->Next->isOneOf(tok::star, tok::amp, tok::ampamp)24
) {
2183
24
        // For operator void*(), operator char*(), operator Foo*().
2184
24
        Next = Next->Next;
2185
24
        continue;
2186
24
      }
2187
18
2188
18
      break;
2189
18
    }
2190
357
    
return nullptr18
;
2191
357
  };
2192
189k
2193
189k
  // Find parentheses of parameter list.
2194
189k
  const FormatToken *Next = Current.Next;
2195
189k
  if (Current.is(tok::kw_operator)) {
2196
522
    if (Current.Previous && Current.Previous->is(tok::coloncolon))
2197
217
      return false;
2198
305
    Next = skipOperatorName(Next);
2199
188k
  } else {
2200
188k
    if (!Current.is(TT_StartOfName) || 
Current.NestingLevel != 013.2k
)
2201
177k
      return false;
2202
11.0k
    
for (; 10.8k
Next;
Next = Next->Next208
) {
2203
10.7k
      if (Next->is(TT_TemplateOpener)) {
2204
85
        Next = Next->MatchingParen;
2205
10.7k
      } else if (Next->is(tok::coloncolon)) {
2206
178
        Next = Next->Next;
2207
178
        if (!Next)
2208
0
          return false;
2209
178
        if (Next->is(tok::kw_operator)) {
2210
52
          Next = skipOperatorName(Next->Next);
2211
52
          break;
2212
52
        }
2213
126
        if (!Next->is(tok::identifier))
2214
3
          return false;
2215
10.5k
      } else if (Next->is(tok::l_paren)) {
2216
4.80k
        break;
2217
5.72k
      } else {
2218
5.72k
        return false;
2219
5.72k
      }
2220
10.7k
    }
2221
10.8k
  }
2222
189k
2223
189k
  // Check whether parameter list can belong to a function declaration.
2224
189k
  
if (5.39k
!Next5.39k
||
!Next->is(tok::l_paren)5.14k
||
!Next->MatchingParen5.14k
)
2225
259
    return false;
2226
5.14k
  // If the lines ends with "{", this is likely an function definition.
2227
5.14k
  if (Line.Last->is(tok::l_brace))
2228
2.56k
    return true;
2229
2.57k
  if (Next->Next == Next->MatchingParen)
2230
1.61k
    return true; // Empty parentheses.
2231
963
  // If there is an &/&& after the r_paren, this is likely a function.
2232
963
  if (Next->MatchingParen->Next &&
2233
963
      
Next->MatchingParen->Next->is(TT_PointerOrReference)602
)
2234
66
    return true;
2235
1.38k
  
for (const FormatToken *Tok = Next->Next; 897
Tok && Tok != Next->MatchingParen;
2236
1.29k
       
Tok = Tok->Next492
) {
2237
1.29k
    if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && 
Tok->MatchingParen50
) {
2238
50
      Tok = Tok->MatchingParen;
2239
50
      continue;
2240
50
    }
2241
1.24k
    if (Tok->is(tok::kw_const) || 
Tok->isSimpleTypeSpecifier()1.17k
||
2242
1.24k
        
Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)653
)
2243
783
      return true;
2244
460
    if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
2245
460
        
Tok->Tok.isLiteral()449
)
2246
18
      return false;
2247
460
  }
2248
897
  
return false96
;
2249
897
}
2250
2251
4.84k
bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
2252
4.84k
  assert(Line.MightBeFunctionDecl);
2253
4.84k
2254
4.84k
  if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
2255
4.84k
       Style.AlwaysBreakAfterReturnType ==
2256
4.82k
           FormatStyle::RTBS_TopLevelDefinitions) &&
2257
4.84k
      
Line.Level > 025
)
2258
12
    return false;
2259
4.83k
2260
4.83k
  switch (Style.AlwaysBreakAfterReturnType) {
2261
4.64k
  case FormatStyle::RTBS_None:
2262
4.64k
    return false;
2263
19
  case FormatStyle::RTBS_All:
2264
19
  case FormatStyle::RTBS_TopLevel:
2265
19
    return true;
2266
162
  case FormatStyle::RTBS_AllDefinitions:
2267
162
  case FormatStyle::RTBS_TopLevelDefinitions:
2268
162
    return Line.mightBeFunctionDefinition();
2269
0
  }
2270
0
2271
0
  return false;
2272
0
}
2273
2274
55.9k
void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
2275
55.9k
  for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
2276
55.9k
                                                  E = Line.Children.end();
2277
57.2k
       I != E; 
++I1.28k
) {
2278
1.28k
    calculateFormattingInformation(**I);
2279
1.28k
  }
2280
55.9k
2281
55.9k
  Line.First->TotalLength =
2282
55.9k
      Line.First->IsMultiline ? 
Style.ColumnLimit141
2283
55.9k
                              : 
Line.FirstStartColumn + Line.First->ColumnWidth55.8k
;
2284
55.9k
  FormatToken *Current = Line.First->Next;
2285
55.9k
  bool InFunctionDecl = Line.MightBeFunctionDecl;
2286
244k
  while (Current) {
2287
189k
    if (isFunctionDeclarationName(*Current, Line))
2288
5.02k
      Current->Type = TT_FunctionDeclarationName;
2289
189k
    if (Current->is(TT_LineComment)) {
2290
2.37k
      if (Current->Previous->BlockKind == BK_BracedInit &&
2291
2.37k
          
Current->Previous->opensScope()30
)
2292
27
        Current->SpacesRequiredBefore =
2293
27
            (Style.Cpp11BracedListStyle && 
!Style.SpacesInParentheses22
) ?
019
:
18
;
2294
2.34k
      else
2295
2.34k
        Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
2296
2.37k
2297
2.37k
      // If we find a trailing comment, iterate backwards to determine whether
2298
2.37k
      // it seems to relate to a specific parameter. If so, break before that
2299
2.37k
      // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
2300
2.37k
      // to the previous line in:
2301
2.37k
      //   SomeFunction(a,
2302
2.37k
      //                b, // comment
2303
2.37k
      //                c);
2304
2.37k
      if (!Current->HasUnescapedNewline) {
2305
5.26k
        for (FormatToken *Parameter = Current->Previous; Parameter;
2306
4.62k
             
Parameter = Parameter->Previous3.59k
) {
2307
4.62k
          if (Parameter->isOneOf(tok::comment, tok::r_brace))
2308
953
            break;
2309
3.67k
          if (Parameter->Previous && 
Parameter->Previous->is(tok::comma)3.03k
) {
2310
76
            if (!Parameter->Previous->is(TT_CtorInitializerComma) &&
2311
76
                
Parameter->HasUnescapedNewline73
)
2312
14
              Parameter->MustBreakBefore = true;
2313
76
            break;
2314
76
          }
2315
3.67k
        }
2316
1.66k
      }
2317
186k
    } else if (Current->SpacesRequiredBefore == 0 &&
2318
186k
               
spaceRequiredBefore(Line, *Current)186k
) {
2319
79.2k
      Current->SpacesRequiredBefore = 1;
2320
79.2k
    }
2321
189k
2322
189k
    Current->MustBreakBefore =
2323
189k
        Current->MustBreakBefore || 
mustBreakBefore(Line, *Current)188k
;
2324
189k
2325
189k
    if (!Current->MustBreakBefore && 
InFunctionDecl185k
&&
2326
189k
        
Current->is(TT_FunctionDeclarationName)63.4k
)
2327
4.84k
      Current->MustBreakBefore = mustBreakForReturnType(Line);
2328
189k
2329
189k
    Current->CanBreakBefore =
2330
189k
        Current->MustBreakBefore || 
canBreakBefore(Line, *Current)185k
;
2331
189k
    unsigned ChildSize = 0;
2332
189k
    if (Current->Previous->Children.size() == 1) {
2333
696
      FormatToken &LastOfChild = *Current->Previous->Children[0]->Last;
2334
696
      ChildSize = LastOfChild.isTrailingComment() ? 
Style.ColumnLimit92
2335
696
                                                  : 
LastOfChild.TotalLength + 1604
;
2336
696
    }
2337
189k
    const FormatToken *Prev = Current->Previous;
2338
189k
    if (Current->MustBreakBefore || 
Prev->Children.size() > 1185k
||
2339
189k
        
(185k
Prev->Children.size() == 1185k
&&
2340
185k
         
Prev->Children[0]->First->MustBreakBefore487
) ||
2341
189k
        
Current->IsMultiline185k
)
2342
3.70k
      Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
2343
185k
    else
2344
185k
      Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
2345
185k
                             ChildSize + Current->SpacesRequiredBefore;
2346
189k
2347
189k
    if (Current->is(TT_CtorInitializerColon))
2348
444
      InFunctionDecl = false;
2349
189k
2350
189k
    // FIXME: Only calculate this if CanBreakBefore is true once static
2351
189k
    // initializers etc. are sorted out.
2352
189k
    // FIXME: Move magic numbers to a better place.
2353
189k
2354
189k
    // Reduce penalty for aligning ObjC method arguments using the colon
2355
189k
    // alignment as this is the canonical way (still prefer fitting everything
2356
189k
    // into one line if possible). Trying to fit a whole expression into one
2357
189k
    // line should not force other line breaks (e.g. when ObjC method
2358
189k
    // expression is a part of other expression).
2359
189k
    Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
2360
189k
    if (Style.Language == FormatStyle::LK_ObjC &&
2361
189k
        
Current->is(TT_SelectorName)44.2k
&&
Current->ParameterIndex > 0839
) {
2362
261
      if (Current->ParameterIndex == 1)
2363
145
        Current->SplitPenalty += 5 * Current->BindingStrength;
2364
188k
    } else {
2365
188k
      Current->SplitPenalty += 20 * Current->BindingStrength;
2366
188k
    }
2367
189k
2368
189k
    Current = Current->Next;
2369
189k
  }
2370
55.9k
2371
55.9k
  calculateUnbreakableTailLengths(Line);
2372
55.9k
  unsigned IndentLevel = Line.Level;
2373
300k
  for (Current = Line.First; Current != nullptr; 
Current = Current->Next244k
) {
2374
244k
    if (Current->Role)
2375
3.74k
      Current->Role->precomputeFormattingInfos(Current);
2376
244k
    if (Current->MatchingParen &&
2377
244k
        
Current->MatchingParen->opensBlockOrBlockTypeList(Style)55.1k
) {
2378
3.24k
      assert(IndentLevel > 0);
2379
3.24k
      --IndentLevel;
2380
3.24k
    }
2381
244k
    Current->IndentLevel = IndentLevel;
2382
244k
    if (Current->opensBlockOrBlockTypeList(Style))
2383
11.8k
      ++IndentLevel;
2384
244k
  }
2385
55.9k
2386
55.9k
  LLVM_DEBUG({ printDebugInfo(Line); });
2387
55.9k
}
2388
2389
55.9k
void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
2390
55.9k
  unsigned UnbreakableTailLength = 0;
2391
55.9k
  FormatToken *Current = Line.Last;
2392
300k
  while (Current) {
2393
244k
    Current->UnbreakableTailLength = UnbreakableTailLength;
2394
244k
    if (Current->CanBreakBefore ||
2395
244k
        
Current->isOneOf(tok::comment, tok::string_literal)175k
) {
2396
72.8k
      UnbreakableTailLength = 0;
2397
172k
    } else {
2398
172k
      UnbreakableTailLength +=
2399
172k
          Current->ColumnWidth + Current->SpacesRequiredBefore;
2400
172k
    }
2401
244k
    Current = Current->Previous;
2402
244k
  }
2403
55.9k
}
2404
2405
unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
2406
                                      const FormatToken &Tok,
2407
189k
                                      bool InFunctionDecl) {
2408
189k
  const FormatToken &Left = *Tok.Previous;
2409
189k
  const FormatToken &Right = Tok;
2410
189k
2411
189k
  if (Left.is(tok::semi))
2412
720
    return 0;
2413
188k
2414
188k
  if (Style.Language == FormatStyle::LK_Java) {
2415
3.38k
    if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
2416
24
      return 1;
2417
3.35k
    if (Right.is(Keywords.kw_implements))
2418
18
      return 2;
2419
3.33k
    if (Left.is(tok::comma) && 
Left.NestingLevel == 0130
)
2420
8
      return 3;
2421
184k
  } else if (Style.Language == FormatStyle::LK_JavaScript) {
2422
12.6k
    if (Right.is(Keywords.kw_function) && 
Left.isNot(tok::comma)176
)
2423
164
      return 100;
2424
12.4k
    if (Left.is(TT_JsTypeColon))
2425
262
      return 35;
2426
12.2k
    if ((Left.is(TT_TemplateString) && 
Left.TokenText.endswith("${")124
) ||
2427
12.2k
        
(12.1k
Right.is(TT_TemplateString)12.1k
&&
Right.TokenText.startswith("}")122
))
2428
98
      return 100;
2429
12.1k
    // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
2430
12.1k
    if (Left.opensScope() && 
Right.closesScope()1.71k
)
2431
759
      return 200;
2432
186k
  }
2433
186k
2434
186k
  if (Right.is(tok::identifier) && 
Right.Next54.6k
&&
Right.Next->is(TT_DictLiteral)52.7k
)
2435
1.81k
    return 1;
2436
185k
  if (Right.is(tok::l_square)) {
2437
1.94k
    if (Style.Language == FormatStyle::LK_Proto)
2438
66
      return 1;
2439
1.87k
    if (Left.is(tok::r_square))
2440
58
      return 200;
2441
1.82k
    // Slightly prefer formatting local lambda definitions like functions.
2442
1.82k
    if (Right.is(TT_LambdaLSquare) && 
Left.is(tok::equal)522
)
2443
158
      return 35;
2444
1.66k
    if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
2445
1.66k
                       TT_ArrayInitializerLSquare,
2446
1.66k
                       TT_DesignatedInitializerLSquare, TT_AttributeSquare))
2447
772
      return 500;
2448
184k
  }
2449
184k
2450
184k
  if (Left.is(tok::coloncolon) ||
2451
184k
      
(181k
Right.is(tok::period)181k
&&
Style.Language == FormatStyle::LK_Proto1.79k
))
2452
2.95k
    return 500;
2453
181k
  if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
2454
181k
      
Right.is(tok::kw_operator)167k
) {
2455
13.5k
    if (Line.startsWith(tok::kw_for) && 
Right.PartOfMultiVariableDeclStmt210
)
2456
18
      return 3;
2457
13.5k
    if (Left.is(TT_StartOfName))
2458
161
      return 110;
2459
13.3k
    if (InFunctionDecl && 
Right.NestingLevel == 06.79k
)
2460
4.98k
      return Style.PenaltyReturnTypeOnItsOwnLine;
2461
8.35k
    return 200;
2462
8.35k
  }
2463
167k
  if (Right.is(TT_PointerOrReference))
2464
2.61k
    return 190;
2465
164k
  if (Right.is(TT_LambdaArrow))
2466
223
    return 110;
2467
164k
  if (Left.is(tok::equal) && 
Right.is(tok::l_brace)4.69k
)
2468
517
    return 160;
2469
164k
  if (Left.is(TT_CastRParen))
2470
709
    return 100;
2471
163k
  if (Left.isOneOf(tok::kw_class, tok::kw_struct))
2472
2.63k
    return 5000;
2473
160k
  if (Left.is(tok::comment))
2474
1.34k
    return 1000;
2475
159k
2476
159k
  if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
2477
159k
                   TT_CtorInitializerColon))
2478
817
    return 2;
2479
158k
2480
158k
  if (Right.isMemberAccess()) {
2481
1.84k
    // Breaking before the "./->" of a chained call/member access is reasonably
2482
1.84k
    // cheap, as formatting those with one call per line is generally
2483
1.84k
    // desirable. In particular, it should be cheaper to break before the call
2484
1.84k
    // than it is to break inside a call's parameters, which could lead to weird
2485
1.84k
    // "hanging" indents. The exception is the very last "./->" to support this
2486
1.84k
    // frequent pattern:
2487
1.84k
    //
2488
1.84k
    //   aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
2489
1.84k
    //       dddddddd);
2490
1.84k
    //
2491
1.84k
    // which might otherwise be blown up onto many lines. Here, clang-format
2492
1.84k
    // won't produce "hanging" indents anyway as there is no other trailing
2493
1.84k
    // call.
2494
1.84k
    //
2495
1.84k
    // Also apply higher penalty is not a call as that might lead to a wrapping
2496
1.84k
    // like:
2497
1.84k
    //
2498
1.84k
    //   aaaaaaa
2499
1.84k
    //       .aaaaaaaaa.bbbbbbbb(cccccccc);
2500
1.84k
    return !Right.NextOperator || 
!Right.NextOperator->Previous->closesScope()504
2501
1.84k
               ? 
1501.60k
2502
1.84k
               : 
35241
;
2503
1.84k
  }
2504
156k
2505
156k
  if (Right.is(TT_TrailingAnnotation) &&
2506
156k
      
(1.40k
!Right.Next1.40k
||
Right.Next->isNot(tok::l_paren)1.38k
)) {
2507
802
    // Moving trailing annotations to the next line is fine for ObjC method
2508
802
    // declarations.
2509
802
    if (Line.startsWith(TT_ObjCMethodSpecifier))
2510
2
      return 10;
2511
800
    // Generally, breaking before a trailing annotation is bad unless it is
2512
800
    // function-like. It seems to be especially preferable to keep standard
2513
800
    // annotations (i.e. "const", "final" and "override") on the same line.
2514
800
    // Use a slightly higher penalty after ")" so that annotations like
2515
800
    // "const override" are kept together.
2516
800
    bool is_short_annotation = Right.TokenText.size() < 10;
2517
800
    return (Left.is(tok::r_paren) ? 
100311
:
120489
) + (is_short_annotation ?
50662
:
0138
);
2518
800
  }
2519
156k
2520
156k
  // In for-loops, prefer breaking at ',' and ';'.
2521
156k
  if (Line.startsWith(tok::kw_for) && 
Left.is(tok::equal)2.58k
)
2522
131
    return 4;
2523
155k
2524
155k
  // In Objective-C method expressions, prefer breaking before "param:" over
2525
155k
  // breaking after it.
2526
155k
  if (Right.is(TT_SelectorName))
2527
967
    return 0;
2528
155k
  if (Left.is(tok::colon) && 
Left.is(TT_ObjCMethodExpr)3.40k
)
2529
947
    return Line.MightBeFunctionDecl ? 
50243
:
500704
;
2530
154k
2531
154k
  // In Objective-C type declarations, avoid breaking after the category's
2532
154k
  // open paren (we'll prefer breaking after the protocol list's opening
2533
154k
  // angle bracket, if present).
2534
154k
  if (Line.Type == LT_ObjCDecl && 
Left.is(tok::l_paren)560
&&
Left.Previous41
&&
2535
154k
      
Left.Previous->isOneOf(tok::identifier, tok::greater)41
)
2536
41
    return 500;
2537
154k
2538
154k
  if (Left.is(tok::l_paren) && 
InFunctionDecl17.6k
&&
2539
154k
      
Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign10.2k
)
2540
10.0k
    return 100;
2541
144k
  if (Left.is(tok::l_paren) && 
Left.Previous7.64k
&&
2542
144k
      
(7.49k
Left.Previous->is(tok::kw_for)7.49k
||
Left.Previous->isIf()7.21k
))
2543
1.19k
    return 1000;
2544
142k
  if (Left.is(tok::equal) && 
InFunctionDecl4.04k
)
2545
396
    return 110;
2546
142k
  if (Right.is(tok::r_brace))
2547
3.45k
    return 1;
2548
138k
  if (Left.is(TT_TemplateOpener))
2549
2.83k
    return 100;
2550
136k
  if (Left.opensScope()) {
2551
10.6k
    if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign)
2552
478
      return 0;
2553
10.1k
    if (Left.is(tok::l_brace) && 
!Style.Cpp11BracedListStyle1.72k
)
2554
136
      return 19;
2555
10.0k
    return Left.ParameterCount > 1 ? 
Style.PenaltyBreakBeforeFirstCallParameter1.94k
2556
10.0k
                                   : 
198.11k
;
2557
10.0k
  }
2558
125k
  if (Left.is(TT_JavaAnnotation))
2559
32
    return 50;
2560
125k
2561
125k
  if (Left.is(TT_UnaryOperator))
2562
1.33k
    return 60;
2563
124k
  if (Left.isOneOf(tok::plus, tok::comma) && 
Left.Previous7.99k
&&
2564
124k
      
Left.Previous->isLabelString()7.91k
&&
2565
124k
      
(27
Left.NextOperator27
||
Left.OperatorIndex != 012
))
2566
21
    return 50;
2567
124k
  if (Right.is(tok::plus) && 
Left.isLabelString()648
&&
2568
124k
      
(15
Right.NextOperator15
||
Right.OperatorIndex != 09
))
2569
9
    return 25;
2570
124k
  if (Left.is(tok::comma))
2571
7.26k
    return 1;
2572
116k
  if (Right.is(tok::lessless) && 
Left.isLabelString()411
&&
2573
116k
      
(66
Right.NextOperator66
||
Right.OperatorIndex != 124
))
2574
54
    return 25;
2575
116k
  if (Right.is(tok::lessless)) {
2576
357
    // Breaking at a << is really cheap.
2577
357
    if (!Left.is(tok::r_paren) || 
Right.OperatorIndex > 0116
)
2578
253
      // Slightly prefer to break before the first one in log-like statements.
2579
253
      return 2;
2580
104
    return 1;
2581
104
  }
2582
116k
  if (Left.ClosesTemplateDeclaration)
2583
999
    return Style.PenaltyBreakTemplateDeclaration;
2584
115k
  if (Left.is(TT_ConditionalExpr))
2585
648
    return prec::Conditional;
2586
114k
  prec::Level Level = Left.getPrecedence();
2587
114k
  if (Level == prec::Unknown)
2588
104k
    Level = Right.getPrecedence();
2589
114k
  if (Level == prec::Assignment)
2590
8.70k
    return Style.PenaltyBreakAssignment;
2591
106k
  if (Level != prec::Unknown)
2592
23.1k
    return Level;
2593
82.9k
2594
82.9k
  return 3;
2595
82.9k
}
2596
2597
14.3k
bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
2598
14.3k
  return Style.SpaceBeforeParens == FormatStyle::SBPO_Always ||
2599
14.3k
         
(14.1k
Style.SpaceBeforeParens == FormatStyle::SBPO_NonEmptyParentheses14.1k
&&
2600
14.1k
          
Right.ParameterCount > 0111
);
2601
14.3k
}
2602
2603
/// Returns \c true if the token is followed by a boolean condition, \c false
2604
/// otherwise.
2605
66
static bool isKeywordWithCondition(const FormatToken &Tok) {
2606
66
  return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
2607
66
                     tok::kw_constexpr, tok::kw_catch);
2608
66
}
2609
2610
bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
2611
                                          const FormatToken &Left,
2612
106k
                                          const FormatToken &Right) {
2613
106k
  if (Left.is(tok::kw_return) && 
Right.isNot(tok::semi)908
)
2614
749
    return true;
2615
106k
  if (Left.is(Keywords.kw_assert) && 
Style.Language == FormatStyle::LK_Java2
)
2616
2
    return true;
2617
106k
  if (Style.ObjCSpaceAfterProperty && 
Line.Type == LT_ObjCProperty377
&&
2618
106k
      
Left.Tok.getObjCKeywordID() == tok::objc_property32
)
2619
4
    return true;
2620
106k
  if (Right.is(tok::hashhash))
2621
30
    return Left.is(tok::hash);
2622
106k
  if (Left.isOneOf(tok::hashhash, tok::hash))
2623
3.04k
    return Right.is(tok::hash);
2624
103k
  if ((Left.is(tok::l_paren) && 
Right.is(tok::r_paren)18.2k
) ||
2625
103k
      
(95.3k
Left.is(tok::l_brace)95.3k
&&
Left.BlockKind != BK_Block3.57k
&&
2626
95.3k
       
Right.is(tok::r_brace)1.85k
&&
Right.BlockKind != BK_Block269
))
2627
8.02k
    return Style.SpaceInEmptyParentheses;
2628
95.0k
  if (Style.SpacesInConditionalStatement) {
2629
165
    if (Left.is(tok::l_paren) && 
Left.Previous33
&&
2630
165
        
isKeywordWithCondition(*Left.Previous)33
)
2631
30
      return true;
2632
135
    if (Right.is(tok::r_paren) && 
Right.MatchingParen33
&&
2633
135
        
Right.MatchingParen->Previous33
&&
2634
135
        
isKeywordWithCondition(*Right.MatchingParen->Previous)33
)
2635
30
      return true;
2636
95.0k
  }
2637
95.0k
  if (Left.is(tok::l_paren) || 
Right.is(tok::r_paren)84.5k
)
2638
20.9k
    return (Right.is(TT_CastRParen) ||
2639
20.9k
            
(20.2k
Left.MatchingParen20.2k
&&
Left.MatchingParen->is(TT_CastRParen)11.8k
))
2640
20.9k
               ? 
Style.SpacesInCStyleCastParentheses1.43k
2641
20.9k
               : 
Style.SpacesInParentheses19.5k
;
2642
74.0k
  if (Right.isOneOf(tok::semi, tok::comma))
2643
17.5k
    return false;
2644
56.5k
  if (Right.is(tok::less) && 
Line.Type == LT_ObjCDecl2.87k
) {
2645
45
    bool IsLightweightGeneric = Right.MatchingParen &&
2646
45
                                Right.MatchingParen->Next &&
2647
45
                                
Right.MatchingParen->Next->is(tok::colon)32
;
2648
45
    return !IsLightweightGeneric && 
Style.ObjCSpaceBeforeProtocolList40
;
2649
45
  }
2650
56.5k
  if (Right.is(tok::less) && 
Left.is(tok::kw_template)2.83k
)
2651
1.00k
    return Style.SpaceAfterTemplateKeyword;
2652
55.5k
  if (Left.isOneOf(tok::exclaim, tok::tilde))
2653
0
    return false;
2654
55.5k
  if (Left.is(tok::at) &&
2655
55.5k
      Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
2656
843
                    tok::numeric_constant, tok::l_paren, tok::l_brace,
2657
843
                    tok::kw_true, tok::kw_false))
2658
729
    return false;
2659
54.7k
  if (Left.is(tok::colon))
2660
4.21k
    return !Left.is(TT_ObjCMethodExpr);
2661
50.5k
  if (Left.is(tok::coloncolon))
2662
2.72k
    return false;
2663
47.8k
  if (Left.is(tok::less) || 
Right.isOneOf(tok::greater, tok::less)47.5k
) {
2664
2.18k
    if (Style.Language == FormatStyle::LK_TextProto ||
2665
2.18k
        
(1.82k
Style.Language == FormatStyle::LK_Proto1.82k
&&
2666
1.82k
         
(112
Left.is(TT_DictLiteral)112
||
Right.is(TT_DictLiteral)58
))) {
2667
454
      // Format empty list as `<>`.
2668
454
      if (Left.is(tok::less) && 
Right.is(tok::greater)273
)
2669
56
        return false;
2670
398
      return !Style.Cpp11BracedListStyle;
2671
398
    }
2672
1.73k
    return false;
2673
1.73k
  }
2674
45.6k
  if (Right.is(tok::ellipsis))
2675
102
    return Left.Tok.isLiteral() || 
(96
Left.is(tok::identifier)96
&&
Left.Previous42
&&
2676
96
                                    
Left.Previous->is(tok::kw_case)42
);
2677
45.5k
  if (Left.is(tok::l_square) && 
Right.is(tok::amp)2.43k
)
2678
69
    return Style.SpacesInSquareBrackets;
2679
45.4k
  if (Right.is(TT_PointerOrReference)) {
2680
2.49k
    if (Left.is(tok::r_paren) && 
Line.MightBeFunctionDecl144
) {
2681
141
      if (!Left.MatchingParen)
2682
0
        return true;
2683
141
      FormatToken *TokenBeforeMatchingParen =
2684
141
          Left.MatchingParen->getPreviousNonComment();
2685
141
      if (!TokenBeforeMatchingParen ||
2686
141
          !TokenBeforeMatchingParen->isOneOf(tok::kw_typeof, tok::kw_decltype,
2687
141
                                             TT_TypenameMacro))
2688
108
        return true;
2689
2.38k
    }
2690
2.38k
    return (Left.Tok.isLiteral() ||
2691
2.38k
            (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
2692
2.38k
             
(2.20k
Style.PointerAlignment != FormatStyle::PAS_Left2.20k
||
2693
2.20k
              
(339
Line.IsMultiVariableDeclStmt339
&&
2694
339
               
(12
Left.NestingLevel == 012
||
2695
12
                
(6
Left.NestingLevel == 16
&&
Line.First->is(tok::kw_for)6
))))));
2696
2.38k
  }
2697
43.0k
  if (Right.is(TT_FunctionTypeLParen) && 
Left.isNot(tok::l_paren)138
&&
2698
43.0k
      
(138
!Left.is(TT_PointerOrReference)138
||
2699
138
       
(12
Style.PointerAlignment != FormatStyle::PAS_Right12
&&
2700
12
        
!Line.IsMultiVariableDeclStmt6
)))
2701
132
    return true;
2702
42.8k
  if (Left.is(TT_PointerOrReference))
2703
1.58k
    return Right.Tok.isLiteral() || Right.is(TT_BlockComment) ||
2704
1.58k
           
(1.57k
Right.isOneOf(Keywords.kw_override, Keywords.kw_final)1.57k
&&
2705
1.57k
            
!Right.is(TT_StartOfName)6
) ||
2706
1.58k
           
(1.57k
Right.is(tok::l_brace)1.57k
&&
Right.BlockKind == BK_Block30
) ||
2707
1.58k
           
(1.54k
!Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
2708
1.54k
                           tok::l_paren) &&
2709
1.54k
            
(1.48k
Style.PointerAlignment != FormatStyle::PAS_Right1.48k
&&
2710
1.48k
             
!Line.IsMultiVariableDeclStmt207
) &&
2711
1.54k
            
Left.Previous198
&&
2712
1.54k
            !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon,
2713
198
                                    tok::l_square));
2714
41.2k
  if (Right.is(tok::star) && 
Left.is(tok::l_paren)69
)
2715
0
    return false;
2716
41.2k
  if (Right.isOneOf(tok::star, tok::amp, tok::ampamp) &&
2717
41.2k
      
(117
Left.is(tok::identifier)117
||
Left.isSimpleTypeSpecifier()117
) &&
2718
41.2k
      // Space between the type and the * in:
2719
41.2k
      //   operator void*()
2720
41.2k
      //   operator char*()
2721
41.2k
      //   operator /*comment*/ const char*()
2722
41.2k
      //   operator volatile /*comment*/ char*()
2723
41.2k
      //   operator Foo*()
2724
41.2k
      // dependent on PointerAlignment style.
2725
41.2k
      
Left.Previous84
&&
2726
41.2k
      
(84
Left.Previous->endsSequence(tok::kw_operator)84
||
2727
84
       
Left.Previous->endsSequence(tok::kw_const, tok::kw_operator)18
||
2728
84
       
Left.Previous->endsSequence(tok::kw_volatile, tok::kw_operator)9
))
2729
84
    return (Style.PointerAlignment != FormatStyle::PAS_Left);
2730
41.2k
  const auto SpaceRequiredForArrayInitializerLSquare =
2731
41.2k
      [](const FormatToken &LSquareTok, const FormatStyle &Style) {
2732
354
        return Style.SpacesInContainerLiterals ||
2733
354
               
(286
(286
Style.Language == FormatStyle::LK_Proto286
||
2734
286
                 
Style.Language == FormatStyle::LK_TextProto174
) &&
2735
286
                
!Style.Cpp11BracedListStyle164
&&
2736
286
                LSquareTok.endsSequence(tok::l_square, tok::colon,
2737
160
                                        TT_SelectorName));
2738
354
      };
2739
41.2k
  if (Left.is(tok::l_square))
2740
2.36k
    return (Left.is(TT_ArrayInitializerLSquare) && 
Right.isNot(tok::r_square)223
&&
2741
2.36k
            
SpaceRequiredForArrayInitializerLSquare(Left, Style)192
) ||
2742
2.36k
           
(2.28k
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
2743
2.28k
                         TT_LambdaLSquare) &&
2744
2.28k
            
Style.SpacesInSquareBrackets1.37k
&&
Right.isNot(tok::r_square)56
);
2745
38.8k
  if (Right.is(tok::r_square))
2746
1.69k
    return Right.MatchingParen &&
2747
1.69k
           ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
2748
1.69k
             SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
2749
162
                                                     Style)) ||
2750
1.69k
            
(1.62k
Style.SpacesInSquareBrackets1.62k
&&
2751
1.62k
             Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
2752
47
                                          TT_StructuredBindingLSquare,
2753
47
                                          TT_LambdaLSquare)) ||
2754
1.69k
            
Right.MatchingParen->is(TT_AttributeParen)1.57k
);
2755
37.1k
  if (Right.is(tok::l_square) &&
2756
37.1k
      !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
2757
855
                     TT_DesignatedInitializerLSquare,
2758
855
                     TT_StructuredBindingLSquare, TT_AttributeSquare) &&
2759
37.1k
      
!Left.isOneOf(tok::numeric_constant, TT_DictLiteral)718
&&
2760
37.1k
      
!(641
!Left.is(tok::r_square)641
&&
Style.SpaceBeforeSquareBrackets583
&&
2761
641
        
Right.is(TT_ArraySubscriptLSquare)30
))
2762
611
    return false;
2763
36.5k
  if (Left.is(tok::l_brace) && 
Right.is(tok::r_brace)3.32k
)
2764
1.15k
    return !Left.Children.empty(); // No spaces in "{}".
2765
35.3k
  if ((Left.is(tok::l_brace) && 
Left.BlockKind != BK_Block2.16k
) ||
2766
35.3k
      
(33.8k
Right.is(tok::r_brace)33.8k
&&
Right.MatchingParen2.04k
&&
2767
33.8k
       
Right.MatchingParen->BlockKind != BK_Block2.04k
))
2768
3.09k
    return Style.Cpp11BracedListStyle ? 
Style.SpacesInParentheses1.96k
:
true1.13k
;
2769
32.2k
  if (Left.is(TT_BlockComment))
2770
215
    // No whitespace in x(/*foo=*/1), except for JavaScript.
2771
215
    return Style.Language == FormatStyle::LK_JavaScript ||
2772
215
           
!Left.TokenText.endswith("=*/")197
;
2773
32.0k
  if (Right.is(tok::l_paren)) {
2774
15.1k
    if ((Left.is(tok::r_paren) && 
Left.is(TT_AttributeParen)239
) ||
2775
15.1k
        
(15.1k
Left.is(tok::r_square)15.1k
&&
Left.is(TT_AttributeSquare)554
))
2776
12
      return true;
2777
15.1k
    return Line.Type == LT_ObjCDecl || 
Left.is(tok::semi)15.0k
||
2778
15.1k
           
(15.0k
Style.SpaceBeforeParens != FormatStyle::SBPO_Never15.0k
&&
2779
15.0k
            
(14.9k
Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while,
2780
14.9k
                          tok::kw_switch, tok::kw_case, TT_ForEachMacro,
2781
14.9k
                          TT_ObjCForIn) ||
2782
14.9k
             
Left.isIf(Line.Type != LT_PreprocessorDirective)14.3k
||
2783
14.9k
             
(13.5k
Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch,
2784
13.5k
                           tok::kw_new, tok::kw_delete) &&
2785
13.5k
              
(88
!Left.Previous88
||
Left.Previous->isNot(tok::period)60
)))) ||
2786
15.1k
           
(13.5k
spaceRequiredBeforeParens(Right)13.5k
&&
2787
13.5k
            
(167
Left.is(tok::identifier)167
||
Left.isFunctionLikeKeyword()75
||
2788
167
             
Left.is(tok::r_paren)21
||
Left.isSimpleTypeSpecifier()15
||
2789
167
             
(9
Left.is(tok::r_square)9
&&
Left.MatchingParen6
&&
2790
9
              
Left.MatchingParen->is(TT_LambdaLSquare)6
)) &&
2791
13.5k
            
Line.Type != LT_PreprocessorDirective164
);
2792
15.1k
  }
2793
16.9k
  if (Left.is(tok::at) && 
Right.Tok.getObjCKeywordID() != tok::objc_not_keyword68
)
2794
62
    return false;
2795
16.8k
  if (Right.is(TT_UnaryOperator))
2796
139
    return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
2797
139
           
(135
Left.isNot(tok::colon)135
||
Left.isNot(TT_ObjCMethodExpr)0
);
2798
16.7k
  if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
2799
16.7k
                    tok::r_paren) ||
2800
16.7k
       
Left.isSimpleTypeSpecifier()3.32k
) &&
2801
16.7k
      
Right.is(tok::l_brace)13.4k
&&
Right.getNextNonComment()8.99k
&&
2802
16.7k
      
Right.BlockKind != BK_Block1.84k
)
2803
302
    return false;
2804
16.4k
  if (Left.is(tok::period) || Right.is(tok::period))
2805
0
    return false;
2806
16.4k
  if (Right.is(tok::hash) && 
Left.is(tok::identifier)16
&&
Left.TokenText == "L"7
)
2807
1
    return false;
2808
16.4k
  if (Left.is(TT_TemplateCloser) && 
Left.MatchingParen1.83k
&&
2809
16.4k
      
Left.MatchingParen->Previous1.83k
&&
2810
16.4k
      
(1.83k
Left.MatchingParen->Previous->is(tok::period)1.83k
||
2811
1.83k
       
Left.MatchingParen->Previous->is(tok::coloncolon)1.82k
))
2812
11
    // Java call to generic function with explicit type:
2813
11
    // A.<B<C<...>>>DoSomething();
2814
11
    // A::<B<C<...>>>DoSomething();  // With a Java 8 method reference.
2815
11
    return false;
2816
16.4k
  if (Left.is(TT_TemplateCloser) && 
Right.is(tok::l_square)1.82k
)
2817
0
    return false;
2818
16.4k
  if (Left.is(tok::l_brace) && 
Left.endsSequence(TT_DictLiteral, tok::at)579
)
2819
6
    // Objective-C dictionary literal -> no space after opening brace.
2820
6
    return false;
2821
16.4k
  if (Right.is(tok::r_brace) && 
Right.MatchingParen532
&&
2822
16.4k
      
Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)531
)
2823
10
    // Objective-C dictionary literal -> no space before closing brace.
2824
10
    return false;
2825
16.4k
  if (Right.Type == TT_TrailingAnnotation &&
2826
16.4k
      
Right.isOneOf(tok::amp, tok::ampamp)366
&&
2827
16.4k
      
Left.isOneOf(tok::kw_const, tok::kw_volatile)0
&&
2828
16.4k
      
(0
!Right.Next0
||
Right.Next->is(tok::semi)0
))
2829
0
    // Match const and volatile ref-qualifiers without any additional
2830
0
    // qualifiers such as
2831
0
    // void Fn() const &;
2832
0
    return Style.PointerAlignment != FormatStyle::PAS_Left;
2833
16.4k
  return true;
2834
16.4k
}
2835
2836
bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
2837
186k
                                         const FormatToken &Right) {
2838
186k
  const FormatToken &Left = *Right.Previous;
2839
186k
  if (Right.Tok.getIdentifierInfo() && 
Left.Tok.getIdentifierInfo()65.0k
)
2840
23.1k
    return true; // Never ever merge two identifiers.
2841
162k
  if (Style.isCpp()) {
2842
138k
    if (Left.is(tok::kw_operator))
2843
402
      return Right.is(tok::coloncolon);
2844
138k
    if (Right.is(tok::l_brace) && 
Right.BlockKind == BK_BracedInit9.54k
&&
2845
138k
        
!Left.opensScope()844
&&
Style.SpaceBeforeCpp11BracedList675
)
2846
9
      return true;
2847
24.4k
  } else if (Style.Language == FormatStyle::LK_Proto ||
2848
24.4k
             
Style.Language == FormatStyle::LK_TextProto20.7k
) {
2849
8.98k
    if (Right.is(tok::period) &&
2850
8.98k
        Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
2851
376
                     Keywords.kw_repeated, Keywords.kw_extend))
2852
4
      return true;
2853
8.98k
    if (Right.is(tok::l_paren) &&
2854
8.98k
        
Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)138
)
2855
102
      return true;
2856
8.88k
    if (Right.isOneOf(tok::l_brace, tok::less) && 
Left.is(TT_SelectorName)848
)
2857
361
      return true;
2858
8.52k
    // Slashes occur in text protocol extension syntax: [type/type] { ... }.
2859
8.52k
    if (Left.is(tok::slash) || 
Right.is(tok::slash)8.50k
)
2860
36
      return false;
2861
8.48k
    if (Left.MatchingParen &&
2862
8.48k
        
Left.MatchingParen->is(TT_ProtoExtensionLSquare)1.87k
&&
2863
8.48k
        
Right.isOneOf(tok::l_brace, tok::less)14
)
2864
4
      return !Style.Cpp11BracedListStyle;
2865
8.48k
    // A percent is probably part of a formatting specification, such as %lld.
2866
8.48k
    if (Left.is(tok::percent))
2867
8
      return false;
2868
8.47k
    // Preserve the existence of a space before a percent for cases like 0x%04x
2869
8.47k
    // and "%d %d"
2870
8.47k
    if (Left.is(tok::numeric_constant) && 
Right.is(tok::percent)491
)
2871
4
      return Right.WhitespaceRange.getEnd() != Right.WhitespaceRange.getBegin();
2872
15.4k
  } else if (Style.isCSharp()) {
2873
1.39k
    // space between type and variable e.g. Dictionary<string,string> foo;
2874
1.39k
    if (Left.is(TT_TemplateCloser) && 
Right.is(TT_StartOfName)8
)
2875
8
      return true;
2876
1.38k
    // space between keywords and paren e.g. "using ("
2877
1.38k
    if (Right.is(tok::l_paren))
2878
184
      if (Left.is(tok::kw_using))
2879
24
        return Style.SpaceBeforeParens == FormatStyle::SBPO_ControlStatements ||
2880
24
               
spaceRequiredBeforeParens(Right)18
;
2881
1.36k
    // space between ']' and '{'
2882
1.36k
    if (Left.is(tok::r_square) && 
Right.is(tok::l_brace)24
)
2883
4
      return true;
2884
1.36k
    // space before '{' in "new MyType {"
2885
1.36k
    if (Right.is(tok::l_brace) && 
Left.Previous108
&&
2886
1.36k
        
Left.Previous->is(tok::kw_new)104
)
2887
8
      return true;
2888
14.0k
  } else if (Style.Language == FormatStyle::LK_JavaScript) {
2889
11.2k
    if (Left.is(TT_JsFatArrow))
2890
74
      return true;
2891
11.1k
    // for await ( ...
2892
11.1k
    if (Right.is(tok::l_paren) && 
Left.is(Keywords.kw_await)1.00k
&&
Left.Previous6
&&
2893
11.1k
        
Left.Previous->is(tok::kw_for)4
)
2894
4
      return true;
2895
11.1k
    if (Left.is(Keywords.kw_async) && 
Right.is(tok::l_paren)6
&&
2896
11.1k
        
Right.MatchingParen4
) {
2897
4
      const FormatToken *Next = Right.MatchingParen->getNextNonComment();
2898
4
      // An async arrow function, for example: `x = async () => foo();`,
2899
4
      // as opposed to calling a function called async: `x = async();`
2900
4
      if (Next && Next->is(TT_JsFatArrow))
2901
2
        return true;
2902
11.1k
    }
2903
11.1k
    if ((Left.is(TT_TemplateString) && 
Left.TokenText.endswith("${")124
) ||
2904
11.1k
        
(11.1k
Right.is(TT_TemplateString)11.1k
&&
Right.TokenText.startswith("}")120
))
2905
98
      return false;
2906
11.0k
    // In tagged template literals ("html`bar baz`"), there is no space between
2907
11.0k
    // the tag identifier and the template string. getIdentifierInfo makes sure
2908
11.0k
    // that the identifier is not a pseudo keyword like `yield`, either.
2909
11.0k
    if (Left.is(tok::identifier) && 
Keywords.IsJavaScriptIdentifier(Left)3.91k
&&
2910
11.0k
        
Right.is(TT_TemplateString)3.50k
)
2911
4
      return false;
2912
11.0k
    if (Right.is(tok::star) &&
2913
11.0k
        
Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)18
)
2914
12
      return false;
2915
11.0k
    if (Right.isOneOf(tok::l_brace, tok::l_square) &&
2916
11.0k
        Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
2917
1.01k
                     Keywords.kw_extends, Keywords.kw_implements))
2918
12
      return true;
2919
11.0k
    if (Right.is(tok::l_paren)) {
2920
1.00k
      // JS methods can use some keywords as names (e.g. `delete()`).
2921
1.00k
      if (Line.MustBeDeclaration && 
Left.Tok.getIdentifierInfo()66
)
2922
58
        return false;
2923
943
      // Valid JS method names can include keywords, e.g. `foo.delete()` or
2924
943
      // `bar.instanceof()`. Recognize call positions by preceding period.
2925
943
      if (Left.Previous && 
Left.Previous->is(tok::period)667
&&
2926
943
          
Left.Tok.getIdentifierInfo()184
)
2927
182
        return false;
2928
761
      // Additional unary JavaScript operators that need a space after.
2929
761
      if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
2930
761
                       tok::kw_void))
2931
8
        return true;
2932
10.8k
    }
2933
10.8k
    // `foo as const;` casts into a const type.
2934
10.8k
    if (Left.endsSequence(tok::kw_const, Keywords.kw_as)) {
2935
2
      return false;
2936
2
    }
2937
10.7k
    if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
2938
10.7k
                      tok::kw_const) ||
2939
10.7k
         // "of" is only a keyword if it appears after another identifier
2940
10.7k
         // (e.g. as "const x of y" in a for loop), or after a destructuring
2941
10.7k
         // operation (const [x, y] of z, const {a, b} of c).
2942
10.7k
         
(10.7k
Left.is(Keywords.kw_of)10.7k
&&
Left.Previous12
&&
2943
10.7k
          
(10
Left.Previous->Tok.is(tok::identifier)10
||
2944
10
           
Left.Previous->isOneOf(tok::r_square, tok::r_brace)8
))) &&
2945
10.7k
        
(30
!Left.Previous30
||
!Left.Previous->is(tok::period)16
))
2946
30
      return true;
2947
10.7k
    if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && 
Left.Previous30
&&
2948
10.7k
        
Left.Previous->is(tok::period)12
&&
Right.is(tok::l_paren)2
)
2949
0
      return false;
2950
10.7k
    if (Left.is(Keywords.kw_as) &&
2951
10.7k
        
Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)8
)
2952
8
      return true;
2953
10.7k
    if (Left.is(tok::kw_default) && 
Left.Previous12
&&
2954
10.7k
        
Left.Previous->is(tok::kw_export)10
)
2955
10
      return true;
2956
10.7k
    if (Left.is(Keywords.kw_is) && 
Right.is(tok::l_brace)6
)
2957
4
      return true;
2958
10.7k
    if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
2959
288
      return false;
2960
10.4k
    if (Left.is(TT_JsTypeOperator) || 
Right.is(TT_JsTypeOperator)10.4k
)
2961
108
      return false;
2962
10.3k
    if ((Left.is(tok::l_brace) || 
Right.is(tok::r_brace)9.84k
) &&
2963
10.3k
        
Line.First->isOneOf(Keywords.kw_import, tok::kw_export)836
)
2964
212
      return false;
2965
10.1k
    if (Left.is(tok::ellipsis))
2966
4
      return false;
2967
10.1k
    if (Left.is(TT_TemplateCloser) &&
2968
10.1k
        !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
2969
44
                       Keywords.kw_implements, Keywords.kw_extends))
2970
14
      // Type assertions ('<type>expr') are not followed by whitespace. Other
2971
14
      // locations that should have whitespace following are identified by the
2972
14
      // above set of follower tokens.
2973
14
      return false;
2974
10.1k
    if (Right.is(TT_JsNonNullAssertion))
2975
34
      return false;
2976
10.0k
    if (Left.is(TT_JsNonNullAssertion) &&
2977
10.0k
        
Right.isOneOf(Keywords.kw_as, Keywords.kw_in)32
)
2978
4
      return true; // "x! as string", "x! in y"
2979
2.78k
  } else if (Style.Language == FormatStyle::LK_Java) {
2980
2.72k
    if (Left.is(tok::r_square) && 
Right.is(tok::l_brace)18
)
2981
10
      return true;
2982
2.71k
    if (Left.is(Keywords.kw_synchronized) && 
Right.is(tok::l_paren)2
)
2983
2
      return Style.SpaceBeforeParens != FormatStyle::SBPO_Never;
2984
2.71k
    if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
2985
2.71k
                      tok::kw_protected) ||
2986
2.71k
         Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
2987
2.69k
                      Keywords.kw_native)) &&
2988
2.71k
        
Right.is(TT_TemplateOpener)20
)
2989
14
      return true;
2990
160k
  }
2991
160k
  if (Left.is(TT_ImplicitStringLiteral))
2992
521
    return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
2993
160k
  if (Line.Type == LT_ObjCMethodDecl) {
2994
2.68k
    if (Left.is(TT_ObjCMethodSpecifier))
2995
281
      return true;
2996
2.40k
    if (Left.is(tok::r_paren) && 
canBeObjCSelectorComponent(Right)526
)
2997
498
      // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a
2998
498
      // keyword in Objective-C, and '+ (instancetype)new;' is a standard class
2999
498
      // method declaration.
3000
498
      return false;
3001
159k
  }
3002
159k
  if (Line.Type == LT_ObjCProperty &&
3003
159k
      
(477
Right.is(tok::equal)477
||
Left.is(tok::equal)466
))
3004
22
    return false;
3005
159k
3006
159k
  if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
3007
159k
      
Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)159k
)
3008
714
    return true;
3009
158k
  if (Right.is(TT_OverloadedOperatorLParen))
3010
548
    return spaceRequiredBeforeParens(Right);
3011
158k
  if (Left.is(tok::comma))
3012
7.50k
    return true;
3013
150k
  if (Right.is(tok::comma))
3014
7.81k
    return false;
3015
142k
  if (Right.is(TT_ObjCBlockLParen))
3016
54
    return true;
3017
142k
  if (Right.is(TT_CtorInitializerColon))
3018
444
    return Style.SpaceBeforeCtorInitializerColon;
3019
142k
  if (Right.is(TT_InheritanceColon) && 
!Style.SpaceBeforeInheritanceColon728
)
3020
30
    return false;
3021
142k
  if (Right.is(TT_RangeBasedForLoopColon) &&
3022
142k
      
!Style.SpaceBeforeRangeBasedForLoopColon47
)
3023
6
    return false;
3024
142k
  if (Right.is(tok::colon)) {
3025
4.45k
    if (Line.First->isOneOf(tok::kw_case, tok::kw_default) ||
3026
4.45k
        
!Right.getNextNonComment()4.15k
||
Right.getNextNonComment()->is(tok::semi)3.80k
)
3027
652
      return false;
3028
3.80k
    if (Right.is(TT_ObjCMethodExpr))
3029
951
      return false;
3030
2.85k
    if (Left.is(tok::question))
3031
19
      return false;
3032
2.83k
    if (Right.is(TT_InlineASMColon) && 
Left.is(tok::coloncolon)22
)
3033
3
      return false;
3034
2.82k
    if (Right.is(TT_DictLiteral))
3035
2.14k
      return Style.SpacesInContainerLiterals;
3036
680
    if (Right.is(TT_AttributeColon))
3037
3
      return false;
3038
677
    return true;
3039
677
  }
3040
137k
  if (Left.is(TT_UnaryOperator)) {
3041
1.32k
    if (!Right.is(tok::l_paren)) {
3042
1.20k
      // The alternative operators for ~ and ! are "compl" and "not".
3043
1.20k
      // If they are used instead, we do not want to combine them with
3044
1.20k
      // the token to the right, unless that is a left paren.
3045
1.20k
      if (Left.is(tok::exclaim) && 
Left.TokenText == "not"88
)
3046
3
        return true;
3047
1.20k
      if (Left.is(tok::tilde) && 
Left.TokenText == "compl"83
)
3048
3
        return true;
3049
1.19k
      // Lambda captures allow for a lone &, so "&]" needs to be properly
3050
1.19k
      // handled.
3051
1.19k
      if (Left.is(tok::amp) && 
Right.is(tok::r_square)244
)
3052
27
        return Style.SpacesInSquareBrackets;
3053
1.29k
    }
3054
1.29k
    return (Style.SpaceAfterLogicalNot && 
Left.is(tok::exclaim)15
) ||
3055
1.29k
           
Right.is(TT_BinaryOperator)1.27k
;
3056
1.29k
  }
3057
136k
3058
136k
  // If the next token is a binary operator or a selector name, we have
3059
136k
  // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
3060
136k
  if (Left.is(TT_CastRParen))
3061
245
    return Style.SpaceAfterCStyleCast ||
3062
245
           
Right.isOneOf(TT_BinaryOperator, TT_SelectorName)229
;
3063
136k
3064
136k
  if (Left.is(tok::greater) && 
Right.is(tok::greater)3.06k
) {
3065
247
    if (Style.Language == FormatStyle::LK_TextProto ||
3066
247
        
(215
Style.Language == FormatStyle::LK_Proto215
&&
Left.is(TT_DictLiteral)4
))
3067
36
      return !Style.Cpp11BracedListStyle;
3068
211
    return Right.is(TT_TemplateCloser) && 
Left.is(TT_TemplateCloser)153
&&
3069
211
           
(153
Style.Standard < FormatStyle::LS_Cpp11153
||
Style.SpacesInAngles126
);
3070
211
  }
3071
136k
  if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
3072
136k
      
Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar)135k
||
3073
136k
      
(133k
Right.is(tok::period)133k
&&
Right.isNot(TT_DesignatedInitializerPeriod)1.69k
))
3074
4.10k
    return false;
3075
131k
  if (!Style.SpaceBeforeAssignmentOperators && 
Left.isNot(TT_TemplateCloser)84
&&
3076
131k
      
Right.getPrecedence() == prec::Assignment75
)
3077
9
    return false;
3078
131k
  if (Style.Language == FormatStyle::LK_Java && 
Right.is(tok::coloncolon)2.02k
&&
3079
131k
      
(4
Left.is(tok::identifier)4
||
Left.is(tok::kw_this)1
))
3080
4
    return false;
3081
131k
  if (Right.is(tok::coloncolon) && 
Left.is(tok::identifier)2.66k
)
3082
2.41k
    // Generally don't remove existing spaces between an identifier and "::".
3083
2.41k
    // The identifier might actually be a macro name such as ALWAYS_INLINE. If
3084
2.41k
    // this turns out to be too lenient, add analysis of the identifier itself.
3085
2.41k
    return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
3086
129k
  if (Right.is(tok::coloncolon) &&
3087
129k
      
!Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)245
)
3088
216
    return (Left.is(TT_TemplateOpener) &&
3089
216
            
Style.Standard < FormatStyle::LS_Cpp1120
) ||
3090
216
           !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
3091
207
                          tok::kw___super, TT_TemplateCloser,
3092
207
                          TT_TemplateOpener)) ||
3093
216
           
(162
Left.is(tok ::l_paren)162
&&
Style.SpacesInParentheses0
);
3094
129k
  if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
3095
5.45k
    return Style.SpacesInAngles;
3096
123k
  // Space before TT_StructuredBindingLSquare.
3097
123k
  if (Right.is(TT_StructuredBindingLSquare))
3098
85
    return !Left.isOneOf(tok::amp, tok::ampamp) ||
3099
85
           
Style.PointerAlignment != FormatStyle::PAS_Right51
;
3100
123k
  // Space before & or && following a TT_StructuredBindingLSquare.
3101
123k
  if (Right.Next && 
Right.Next->is(TT_StructuredBindingLSquare)94.1k
&&
3102
123k
      
Right.isOneOf(tok::amp, tok::ampamp)53
)
3103
51
    return Style.PointerAlignment != FormatStyle::PAS_Left;
3104
123k
  if ((Right.is(TT_BinaryOperator) && 
!Left.is(tok::l_paren)7.88k
) ||
3105
123k
      
(115k
Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr)115k
&&
3106
115k
       
!Right.is(tok::r_paren)8.47k
))
3107
16.3k
    return true;
3108
107k
  if (Left.is(TT_TemplateCloser) && 
Right.is(tok::l_paren)2.41k
&&
3109
107k
      
Right.isNot(TT_FunctionTypeLParen)246
)
3110
243
    return spaceRequiredBeforeParens(Right);
3111
107k
  if (Right.is(TT_TemplateOpener) && 
Left.is(tok::r_paren)2.75k
&&
3112
107k
      
Left.MatchingParen27
&&
Left.MatchingParen->is(TT_OverloadedOperatorLParen)27
)
3113
9
    return false;
3114
107k
  if (Right.is(tok::less) && 
Left.isNot(tok::l_paren)2.89k
&&
3115
107k
      
Line.startsWith(tok::hash)2.87k
)
3116
0
    return true;
3117
107k
  if (Right.is(TT_TrailingUnaryOperator))
3118
95
    return false;
3119
107k
  if (Left.is(TT_RegexLiteral))
3120
132
    return false;
3121
106k
  return spaceRequiredBetween(Line, Left, Right);
3122
106k
}
3123
3124
// Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
3125
367k
static bool isAllmanBrace(const FormatToken &Tok) {
3126
367k
  return Tok.is(tok::l_brace) && 
Tok.BlockKind == BK_Block14.4k
&&
3127
367k
         
!Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral)10.9k
;
3128
367k
}
3129
3130
// Returns 'true' if 'Tok' is an function argument.
3131
633
static bool IsFunctionArgument(const FormatToken &Tok) {
3132
633
  return Tok.MatchingParen && Tok.MatchingParen->Next &&
3133
633
         
Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren)612
;
3134
633
}
3135
3136
static bool
3137
isItAnEmptyLambdaAllowed(const FormatToken &Tok,
3138
417
                         FormatStyle::ShortLambdaStyle ShortLambdaOption) {
3139
417
  return Tok.Children.empty() && 
ShortLambdaOption != FormatStyle::SLS_None81
;
3140
417
}
3141
3142
static bool
3143
isItAInlineLambdaAllowed(const FormatToken &Tok,
3144
285
                         FormatStyle::ShortLambdaStyle ShortLambdaOption) {
3145
285
  return (ShortLambdaOption == FormatStyle::SLS_Inline &&
3146
285
          
IsFunctionArgument(Tok)42
) ||
3147
285
         
(ShortLambdaOption == FormatStyle::SLS_All)249
;
3148
285
}
3149
3150
186
static bool isOneChildWithoutMustBreakBefore(const FormatToken &Tok) {
3151
186
  if (Tok.Children.size() != 1)
3152
24
    return false;
3153
162
  FormatToken *curElt = Tok.Children[0]->First;
3154
1.03k
    while (curElt) {
3155
876
      if (curElt->MustBreakBefore)
3156
6
        return false;
3157
870
      curElt = curElt->Next;
3158
870
    }
3159
162
  
return true156
;
3160
162
}
3161
static bool
3162
8.57k
isAllmanLambdaBrace(const FormatToken &Tok) {
3163
8.57k
  return (Tok.is(tok::l_brace) && 
Tok.BlockKind == BK_Block450
&&
3164
8.57k
      
!Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral)429
);
3165
8.57k
}
3166
3167
static bool
3168
isAllmanBraceIncludedBreakableLambda(const FormatToken &Tok,
3169
5.29k
                            FormatStyle::ShortLambdaStyle ShortLambdaOption) {
3170
5.29k
  if (!isAllmanLambdaBrace(Tok))
3171
4.97k
    return false;
3172
321
3173
321
  if (isItAnEmptyLambdaAllowed(Tok, ShortLambdaOption))
3174
36
    return false;
3175
285
3176
285
  return !isItAInlineLambdaAllowed(Tok, ShortLambdaOption) ||
3177
285
         
!isOneChildWithoutMustBreakBefore(Tok)186
;
3178
285
}
3179
3180
bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
3181
188k
                                     const FormatToken &Right) {
3182
188k
  const FormatToken &Left = *Right.Previous;
3183
188k
  if (Right.NewlinesBefore > 1 && 
Style.MaxEmptyLinesToKeep > 066
)
3184
63
    return true;
3185
188k
3186
188k
  if (Style.Language == FormatStyle::LK_JavaScript) {
3187
12.7k
    // FIXME: This might apply to other languages and token kinds.
3188
12.7k
    if (Right.is(tok::string_literal) && 
Left.is(tok::plus)520
&&
Left.Previous4
&&
3189
12.7k
        
Left.Previous->is(tok::string_literal)4
)
3190
2
      return true;
3191
12.7k
    if (Left.is(TT_DictLiteral) && 
Left.is(tok::l_brace)472
&&
Line.Level == 0250
&&
3192
12.7k
        
Left.Previous232
&&
Left.Previous->is(tok::equal)232
&&
3193
12.7k
        Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
3194
86
                            tok::kw_const) &&
3195
12.7k
        // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
3196
12.7k
        // above.
3197
12.7k
        
!Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)86
)
3198
26
      // Object literals on the top level of a file are treated as "enum-style".
3199
26
      // Each key/value pair is put on a separate line, instead of bin-packing.
3200
26
      return true;
3201
12.6k
    if (Left.is(tok::l_brace) && 
Line.Level == 0499
&&
3202
12.6k
        
(464
Line.startsWith(tok::kw_enum)464
||
3203
464
         
Line.startsWith(tok::kw_const, tok::kw_enum)460
||
3204
464
         
Line.startsWith(tok::kw_export, tok::kw_enum)458
||
3205
464
         
Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum)454
))
3206
12
      // JavaScript top-level enum key/value pairs are put on separate lines
3207
12
      // instead of bin-packing.
3208
12
      return true;
3209
12.6k
    if (Right.is(tok::r_brace) && 
Left.is(tok::l_brace)525
&&
Left.Previous199
&&
3210
12.6k
        
Left.Previous->is(TT_JsFatArrow)199
) {
3211
39
      // JS arrow function (=> {...}).
3212
39
      switch (Style.AllowShortLambdasOnASingleLine) {
3213
4
      case FormatStyle::SLS_All:
3214
4
        return false;
3215
4
      case FormatStyle::SLS_None:
3216
4
        return true;
3217
25
      case FormatStyle::SLS_Empty:
3218
25
        return !Left.Children.empty();
3219
6
      case FormatStyle::SLS_Inline:
3220
6
        // allow one-lining inline (e.g. in function call args) and empty arrow
3221
6
        // functions.
3222
6
        return (Left.NestingLevel == 0 && 
Line.Level == 04
) &&
3223
6
               
!Left.Children.empty()4
;
3224
0
      }
3225
0
      llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum");
3226
0
    }
3227
12.6k
3228
12.6k
    if (Right.is(tok::r_brace) && 
Left.is(tok::l_brace)486
&&
3229
12.6k
        
!Left.Children.empty()160
)
3230
136
      // Support AllowShortFunctionsOnASingleLine for JavaScript.
3231
136
      return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
3232
136
             
Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty128
||
3233
136
             
(34
Left.NestingLevel == 034
&&
Line.Level == 012
&&
3234
34
              Style.AllowShortFunctionsOnASingleLine &
3235
8
                  FormatStyle::SFS_InlineOnly);
3236
176k
  } else if (Style.Language == FormatStyle::LK_Java) {
3237
3.38k
    if (Right.is(tok::plus) && 
Left.is(tok::string_literal)8
&&
Right.Next4
&&
3238
3.38k
        
Right.Next->is(tok::string_literal)4
)
3239
4
      return true;
3240
172k
  } else if (Style.Language == FormatStyle::LK_Cpp ||
3241
172k
             
Style.Language == FormatStyle::LK_ObjC55.4k
||
3242
172k
             
Style.Language == FormatStyle::LK_Proto11.2k
||
3243
172k
             
Style.Language == FormatStyle::LK_TableGen7.32k
||
3244
172k
             
Style.Language == FormatStyle::LK_TextProto7.26k
) {
3245
170k
    if (Left.isStringLiteral() && 
Right.isStringLiteral()1.32k
)
3246
143
      return true;
3247
188k
  }
3248
188k
3249
188k
  // If the last token before a '}', ']', or ')' is a comma or a trailing
3250
188k
  // comment, the intention is to insert a line break after it in order to make
3251
188k
  // shuffling around entries easier. Import statements, especially in
3252
188k
  // JavaScript, can be an exception to this rule.
3253
188k
  if (Style.JavaScriptWrapImports || 
Line.Type != LT_ImportStatement12.4k
) {
3254
187k
    const FormatToken *BeforeClosingBrace = nullptr;
3255
187k
    if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
3256
187k
         
(184k
Style.Language == FormatStyle::LK_JavaScript184k
&&
3257
184k
          
Left.is(tok::l_paren)11.4k
)) &&
3258
187k
        
Left.BlockKind != BK_Block4.74k
&&
Left.MatchingParen3.10k
)
3259
3.09k
      BeforeClosingBrace = Left.MatchingParen->Previous;
3260
184k
    else if (Right.MatchingParen &&
3261
184k
             
(52.9k
Right.MatchingParen->isOneOf(tok::l_brace,
3262
52.9k
                                           TT_ArrayInitializerLSquare) ||
3263
52.9k
              
(49.5k
Style.Language == FormatStyle::LK_JavaScript49.5k
&&
3264
49.5k
               
Right.MatchingParen->is(tok::l_paren)2.22k
)))
3265
3.90k
      BeforeClosingBrace = &Left;
3266
187k
    if (BeforeClosingBrace && 
(7.00k
BeforeClosingBrace->is(tok::comma)7.00k
||
3267
7.00k
                               
BeforeClosingBrace->isTrailingComment()6.66k
))
3268
456
      return true;
3269
188k
  }
3270
188k
3271
188k
  if (Right.is(tok::comment))
3272
2.66k
    return Left.BlockKind != BK_BracedInit &&
3273
2.66k
           
Left.isNot(TT_CtorInitializerColon)2.63k
&&
3274
2.66k
           
(2.63k
Right.NewlinesBefore > 02.63k
&&
Right.HasUnescapedNewline703
);
3275
185k
  if (Left.isTrailingComment())
3276
562
    return true;
3277
184k
  if (Right.Previous->IsUnterminatedLiteral)
3278
2
    return true;
3279
184k
  if (Right.is(tok::lessless) && 
Right.Next410
&&
3280
184k
      
Right.Previous->is(tok::string_literal)404
&&
3281
184k
      
Right.Next->is(tok::string_literal)104
)
3282
19
    return true;
3283
184k
  if (Right.Previous->ClosesTemplateDeclaration &&
3284
184k
      
Right.Previous->MatchingParen930
&&
3285
184k
      
Right.Previous->MatchingParen->NestingLevel == 0930
&&
3286
184k
      
Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes894
)
3287
73
    return true;
3288
184k
  if (Right.is(TT_CtorInitializerComma) &&
3289
184k
      
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma381
&&
3290
184k
      
!Style.ConstructorInitializerAllOnOneLineOrOnePerLine120
)
3291
45
    return true;
3292
184k
  if (Right.is(TT_CtorInitializerColon) &&
3293
184k
      
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma441
&&
3294
184k
      
!Style.ConstructorInitializerAllOnOneLineOrOnePerLine115
)
3295
46
    return true;
3296
184k
  // Break only if we have multiple inheritance.
3297
184k
  if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
3298
184k
      
Right.is(TT_InheritanceComma)185
)
3299
12
    return true;
3300
184k
  if (Right.is(tok::string_literal) && 
Right.TokenText.startswith("R\"")2.76k
)
3301
110
    // Multiline raw string literals are special wrt. line breaks. The author
3302
110
    // has made a deliberate choice and might have aligned the contents of the
3303
110
    // string literal accordingly. Thus, we try keep existing line breaks.
3304
110
    return Right.IsMultiline && 
Right.NewlinesBefore > 024
;
3305
184k
  if ((Right.Previous->is(tok::l_brace) ||
3306
184k
       
(181k
Right.Previous->is(tok::less)181k
&&
Right.Previous->Previous3.45k
&&
3307
181k
        
Right.Previous->Previous->is(tok::equal)3.44k
)) &&
3308
184k
      
Right.NestingLevel == 13.37k
&&
Style.Language == FormatStyle::LK_Proto2.10k
) {
3309
98
    // Don't put enums or option definitions onto single lines in protocol
3310
98
    // buffers.
3311
98
    return true;
3312
98
  }
3313
184k
  if (Right.is(TT_InlineASMBrace))
3314
5
    return Right.HasUnescapedNewline;
3315
184k
3316
184k
  auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
3317
184k
  if (Style.BraceWrapping.BeforeLambdaBody &&
3318
184k
      
(2.67k
isAllmanBraceIncludedBreakableLambda(Left, ShortLambdaOption)2.67k
||
3319
2.67k
       
isAllmanBraceIncludedBreakableLambda(Right, ShortLambdaOption)2.61k
)) {
3320
129
      return true;
3321
129
  }
3322
184k
3323
184k
  if (isAllmanBrace(Left) || 
isAllmanBrace(Right)183k
)
3324
9.35k
    return (Line.startsWith(tok::kw_enum) && 
Style.BraceWrapping.AfterEnum1.21k
) ||
3325
9.35k
           
(9.33k
Line.startsWith(tok::kw_typedef, tok::kw_enum)9.33k
&&
3326
9.33k
            
Style.BraceWrapping.AfterEnum36
) ||
3327
9.35k
           
(9.32k
Line.startsWith(tok::kw_class)9.32k
&&
Style.BraceWrapping.AfterClass841
) ||
3328
9.35k
           
(9.32k
Line.startsWith(tok::kw_struct)9.32k
&&
Style.BraceWrapping.AfterStruct606
);
3329
174k
  if (Left.is(TT_ObjCBlockLBrace) &&
3330
174k
      
Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never139
)
3331
120
    return true;
3332
174k
3333
174k
  if (Left.is(TT_LambdaLBrace)) {
3334
591
    if (IsFunctionArgument(Left) &&
3335
591
        
Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline217
)
3336
30
      return false;
3337
561
3338
561
    if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
3339
561
        
Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline555
||
3340
561
        
(552
!Left.Children.empty()552
&&
3341
552
         
Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty414
))
3342
12
      return true;
3343
174k
  }
3344
174k
3345
174k
  // Put multiple Java annotation on a new line.
3346
174k
  if ((Style.Language == FormatStyle::LK_Java ||
3347
174k
       
Style.Language == FormatStyle::LK_JavaScript171k
) &&
3348
174k
      
Left.is(TT_LeadingJavaAnnotation)14.6k
&&
3349
174k
      
Right.isNot(TT_LeadingJavaAnnotation)152
&&
Right.isNot(tok::l_paren)120
&&
3350
174k
      
(86
Line.Last->is(tok::l_brace)86
||
Style.BreakAfterJavaFieldAnnotations28
))
3351
62
    return true;
3352
174k
3353
174k
  if (Right.is(TT_ProtoExtensionLSquare))
3354
54
    return true;
3355
174k
3356
174k
  // In text proto instances if a submessage contains at least 2 entries and at
3357
174k
  // least one of them is a submessage, like A { ... B { ... } ... },
3358
174k
  // put all of the entries of A on separate lines by forcing the selector of
3359
174k
  // the submessage B to be put on a newline.
3360
174k
  //
3361
174k
  // Example: these can stay on one line:
3362
174k
  // a { scalar_1: 1 scalar_2: 2 }
3363
174k
  // a { b { key: value } }
3364
174k
  //
3365
174k
  // and these entries need to be on a new line even if putting them all in one
3366
174k
  // line is under the column limit:
3367
174k
  // a {
3368
174k
  //   scalar: 1
3369
174k
  //   b { key: value }
3370
174k
  // }
3371
174k
  //
3372
174k
  // We enforce this by breaking before a submessage field that has previous
3373
174k
  // siblings, *and* breaking before a field that follows a submessage field.
3374
174k
  //
3375
174k
  // Be careful to exclude the case  [proto.ext] { ... } since the `]` is
3376
174k
  // the TT_SelectorName there, but we don't want to break inside the brackets.
3377
174k
  //
3378
174k
  // Another edge case is @submessage { key: value }, which is a common
3379
174k
  // substitution placeholder. In this case we want to keep `@` and `submessage`
3380
174k
  // together.
3381
174k
  //
3382
174k
  // We ensure elsewhere that extensions are always on their own line.
3383
174k
  if ((Style.Language == FormatStyle::LK_Proto ||
3384
174k
       
Style.Language == FormatStyle::LK_TextProto170k
) &&
3385
174k
      
Right.is(TT_SelectorName)9.12k
&&
!Right.is(tok::r_square)1.48k
&&
Right.Next1.40k
) {
3386
1.40k
    // Keep `@submessage` together in:
3387
1.40k
    // @submessage { key: value }
3388
1.40k
    if (Right.Previous && Right.Previous->is(tok::at))
3389
12
      return false;
3390
1.39k
    // Look for the scope opener after selector in cases like:
3391
1.39k
    // selector { ...
3392
1.39k
    // selector: { ...
3393
1.39k
    // selector: @base { ...
3394
1.39k
    FormatToken *LBrace = Right.Next;
3395
1.39k
    if (LBrace && LBrace->is(tok::colon)) {
3396
1.20k
      LBrace = LBrace->Next;
3397
1.20k
      if (LBrace && LBrace->is(tok::at)) {
3398
2
        LBrace = LBrace->Next;
3399
2
        if (LBrace)
3400
2
          LBrace = LBrace->Next;
3401
2
      }
3402
1.20k
    }
3403
1.39k
    if (LBrace &&
3404
1.39k
        // The scope opener is one of {, [, <:
3405
1.39k
        // selector { ... }
3406
1.39k
        // selector [ ... ]
3407
1.39k
        // selector < ... >
3408
1.39k
        //
3409
1.39k
        // In case of selector { ... }, the l_brace is TT_DictLiteral.
3410
1.39k
        // In case of an empty selector {}, the l_brace is not TT_DictLiteral,
3411
1.39k
        // so we check for immediately following r_brace.
3412
1.39k
        ((LBrace->is(tok::l_brace) &&
3413
1.39k
          
(229
LBrace->is(TT_DictLiteral)229
||
3414
229
           
(60
LBrace->Next60
&&
LBrace->Next->is(tok::r_brace)60
))) ||
3415
1.39k
         
LBrace->is(TT_ArrayInitializerLSquare)1.18k
||
LBrace->is(tok::less)1.14k
)) {
3416
428
      // If Left.ParameterCount is 0, then this submessage entry is not the
3417
428
      // first in its parent submessage, and we want to break before this entry.
3418
428
      // If Left.ParameterCount is greater than 0, then its parent submessage
3419
428
      // might contain 1 or more entries and we want to break before this entry
3420
428
      // if it contains at least 2 entries. We deal with this case later by
3421
428
      // detecting and breaking before the next entry in the parent submessage.
3422
428
      if (Left.ParameterCount == 0)
3423
323
        return true;
3424
1.07k
      // However, if this submessage is the first entry in its parent
3425
1.07k
      // submessage, Left.ParameterCount might be 1 in some cases.
3426
1.07k
      // We deal with this case later by detecting an entry
3427
1.07k
      // following a closing paren of this submessage.
3428
1.07k
    }
3429
1.07k
3430
1.07k
    // If this is an entry immediately following a submessage, it will be
3431
1.07k
    // preceded by a closing paren of that submessage, like in:
3432
1.07k
    //     left---.  .---right
3433
1.07k
    //            v  v
3434
1.07k
    // sub: { ... } key: value
3435
1.07k
    // If there was a comment between `}` an `key` above, then `key` would be
3436
1.07k
    // put on a new line anyways.
3437
1.07k
    if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
3438
105
      return true;
3439
174k
  }
3440
174k
3441
174k
  // Deal with lambda arguments in C++ - we want consistent line breaks whether
3442
174k
  // they happen to be at arg0, arg1 or argN. The selection is a bit nuanced
3443
174k
  // as aggressive line breaks are placed when the lambda is not the last arg.
3444
174k
  if ((Style.Language == FormatStyle::LK_Cpp ||
3445
174k
       
Style.Language == FormatStyle::LK_ObjC66.9k
) &&
3446
174k
      
Left.is(tok::l_paren)149k
&&
Left.BlockParameterCount > 016.4k
&&
3447
174k
      
!Right.isOneOf(tok::l_paren, TT_LambdaLSquare)298
) {
3448
115
    // Multiple lambdas in the same function call force line breaks.
3449
115
    if (Left.BlockParameterCount > 1)
3450
3
      return true;
3451
112
3452
112
    // A lambda followed by another arg forces a line break.
3453
112
    if (!Left.Role)
3454
13
      return false;
3455
99
    auto Comma = Left.Role->lastComma();
3456
99
    if (!Comma)
3457
0
      return false;
3458
99
    auto Next = Comma->getNextNonComment();
3459
99
    if (!Next)
3460
0
      return false;
3461
99
    if (!Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret))
3462
15
      return true;
3463
174k
  }
3464
174k
3465
174k
  return false;
3466
174k
}
3467
3468
bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
3469
185k
                                    const FormatToken &Right) {
3470
185k
  const FormatToken &Left = *Right.Previous;
3471
185k
3472
185k
  // Language-specific stuff.
3473
185k
  if (Style.Language == FormatStyle::LK_Java) {
3474
3.31k
    if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
3475
3.31k
                     Keywords.kw_implements))
3476
42
      return false;
3477
3.27k
    if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
3478
3.27k
                      Keywords.kw_implements))
3479
42
      return true;
3480
182k
  } else if (Style.Language == FormatStyle::LK_JavaScript) {
3481
12.2k
    const FormatToken *NonComment = Right.getPreviousNonComment();
3482
12.2k
    if (NonComment &&
3483
12.2k
        NonComment->isOneOf(
3484
12.2k
            tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
3485
12.2k
            tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
3486
12.2k
            tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
3487
12.2k
            Keywords.kw_readonly, Keywords.kw_abstract, Keywords.kw_get,
3488
12.2k
            Keywords.kw_set, Keywords.kw_async, Keywords.kw_await))
3489
344
      return false; // Otherwise automatic semicolon insertion would trigger.
3490
11.9k
    if (Right.NestingLevel == 0 &&
3491
11.9k
        
(6.86k
Left.Tok.getIdentifierInfo()6.86k
||
3492
6.86k
         
Left.isOneOf(tok::r_square, tok::r_paren)3.29k
) &&
3493
11.9k
        
Right.isOneOf(tok::l_square, tok::l_paren)4.46k
)
3494
759
      return false; // Otherwise automatic semicolon insertion would trigger.
3495
11.1k
    if (Left.is(TT_JsFatArrow) && 
Right.is(tok::l_brace)74
)
3496
40
      return false;
3497
11.1k
    if (Left.is(TT_JsTypeColon))
3498
262
      return true;
3499
10.8k
    // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
3500
10.8k
    if (Left.is(tok::exclaim) && 
Right.is(tok::colon)48
)
3501
4
      return false;
3502
10.8k
    // Look for is type annotations like:
3503
10.8k
    // function f(): a is B { ... }
3504
10.8k
    // Do not break before is in these cases.
3505
10.8k
    if (Right.is(Keywords.kw_is)) {
3506
6
      const FormatToken *Next = Right.getNextNonComment();
3507
6
      // If `is` is followed by a colon, it's likely that it's a dict key, so
3508
6
      // ignore it for this check.
3509
6
      // For example this is common in Polymer:
3510
6
      // Polymer({
3511
6
      //   is: 'name',
3512
6
      //   ...
3513
6
      // });
3514
6
      if (!Next || !Next->is(tok::colon))
3515
4
        return false;
3516
10.8k
    }
3517
10.8k
    if (Left.is(Keywords.kw_in))
3518
18
      return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
3519
10.8k
    if (Right.is(Keywords.kw_in))
3520
20
      return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
3521
10.8k
    if (Right.is(Keywords.kw_as))
3522
52
      return false; // must not break before as in 'x as type' casts
3523
10.7k
    if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
3524
14
      // extends and infer can appear as keywords in conditional types:
3525
14
      //   https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
3526
14
      // do not break before them, as the expressions are subject to ASI.
3527
14
      return false;
3528
14
    }
3529
10.7k
    if (Left.is(Keywords.kw_as))
3530
44
      return true;
3531
10.7k
    if (Left.is(TT_JsNonNullAssertion))
3532
26
      return true;
3533
10.7k
    if (Left.is(Keywords.kw_declare) &&
3534
10.7k
        Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
3535
28
                      Keywords.kw_function, tok::kw_class, tok::kw_enum,
3536
28
                      Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
3537
28
                      Keywords.kw_let, tok::kw_const))
3538
24
      // See grammar for 'declare' statements at:
3539
24
      // https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#A.10
3540
24
      return false;
3541
10.6k
    if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
3542
10.6k
        
Right.isOneOf(tok::identifier, tok::string_literal)18
)
3543
12
      return false; // must not break in "module foo { ...}"
3544
10.6k
    if (Right.is(TT_TemplateString) && 
Right.closesScope()122
)
3545
50
      return false;
3546
10.6k
    // Don't split tagged template literal so there is a break between the tag
3547
10.6k
    // identifier and template string.
3548
10.6k
    if (Left.is(tok::identifier) && 
Right.is(TT_TemplateString)3.97k
) {
3549
4
      return false;
3550
4
    }
3551
10.6k
    if (Left.is(TT_TemplateString) && 
Left.opensScope()122
)
3552
48
      return true;
3553
183k
  }
3554
183k
3555
183k
  if (Left.is(tok::at))
3556
843
    return false;
3557
182k
  if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
3558
104
    return false;
3559
182k
  if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
3560
140
    return !Right.is(tok::l_paren);
3561
182k
  if (Right.is(TT_PointerOrReference))
3562
2.64k
    return Line.IsMultiVariableDeclStmt ||
3563
2.64k
           
(2.59k
Style.PointerAlignment == FormatStyle::PAS_Right2.59k
&&
3564
2.59k
            
(2.07k
!Right.Next2.07k
||
Right.Next->isNot(TT_FunctionDeclarationName)2.07k
));
3565
179k
  if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
3566
179k
      
Right.is(tok::kw_operator)166k
)
3567
13.3k
    return true;
3568
166k
  if (Left.is(TT_PointerOrReference))
3569
1.10k
    return false;
3570
165k
  if (Right.isTrailingComment())
3571
1.77k
    // We rely on MustBreakBefore being set correctly here as we should not
3572
1.77k
    // change the "binding" behavior of a comment.
3573
1.77k
    // The first comment in a braced lists is always interpreted as belonging to
3574
1.77k
    // the first list element. Otherwise, it should be placed outside of the
3575
1.77k
    // list.
3576
1.77k
    return Left.BlockKind == BK_BracedInit ||
3577
1.77k
           
(1.75k
Left.is(TT_CtorInitializerColon)1.75k
&&
3578
1.75k
            
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon2
);
3579
163k
  if (Left.is(tok::question) && 
Right.is(tok::colon)347
)
3580
37
    return false;
3581
163k
  if (Right.is(TT_ConditionalExpr) || 
Right.is(tok::question)163k
)
3582
627
    return Style.BreakBeforeTernaryOperators;
3583
163k
  if (Left.is(TT_ConditionalExpr) || 
Left.is(tok::question)162k
)
3584
627
    return !Style.BreakBeforeTernaryOperators;
3585
162k
  if (Left.is(TT_InheritanceColon))
3586
317
    return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
3587
162k
  if (Right.is(TT_InheritanceColon))
3588
728
    return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
3589
161k
  if (Right.is(TT_ObjCMethodExpr) && 
!Right.is(tok::r_square)1.84k
&&
3590
161k
      
Left.isNot(TT_SelectorName)1.27k
)
3591
381
    return true;
3592
161k
3593
161k
  if (Right.is(tok::colon) &&
3594
161k
      
!Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)4.02k
)
3595
3.60k
    return false;
3596
157k
  if (Left.is(tok::colon) && 
Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)3.65k
) {
3597
3.07k
    if (Style.Language == FormatStyle::LK_Proto ||
3598
3.07k
        
Style.Language == FormatStyle::LK_TextProto2.57k
) {
3599
1.84k
      if (!Style.AlwaysBreakBeforeMultilineStrings && 
Right.isStringLiteral()1.84k
)
3600
271
        return false;
3601
1.57k
      // Prevent cases like:
3602
1.57k
      //
3603
1.57k
      // submessage:
3604
1.57k
      //     { key: valueeeeeeeeeeee }
3605
1.57k
      //
3606
1.57k
      // when the snippet does not fit into one line.
3607
1.57k
      // Prefer:
3608
1.57k
      //
3609
1.57k
      // submessage: {
3610
1.57k
      //   key: valueeeeeeeeeeee
3611
1.57k
      // }
3612
1.57k
      //
3613
1.57k
      // instead, even if it is longer by one line.
3614
1.57k
      //
3615
1.57k
      // Note that this allows allows the "{" to go over the column limit
3616
1.57k
      // when the column limit is just between ":" and "{", but that does
3617
1.57k
      // not happen too often and alternative formattings in this case are
3618
1.57k
      // not much better.
3619
1.57k
      //
3620
1.57k
      // The code covers the cases:
3621
1.57k
      //
3622
1.57k
      // submessage: { ... }
3623
1.57k
      // submessage: < ... >
3624
1.57k
      // repeated: [ ... ]
3625
1.57k
      if (((Right.is(tok::l_brace) || 
Right.is(tok::less)1.39k
) &&
3626
1.57k
           
Right.is(TT_DictLiteral)311
) ||
3627
1.57k
          
Right.is(TT_ArrayInitializerLSquare)1.29k
)
3628
330
        return false;
3629
2.47k
    }
3630
2.47k
    return true;
3631
2.47k
  }
3632
154k
  if (Right.is(tok::r_square) && 
Right.MatchingParen2.40k
&&
3633
154k
      
Right.MatchingParen->is(TT_ProtoExtensionLSquare)2.40k
)
3634
78
    return false;
3635
154k
  if (Right.is(TT_SelectorName) || 
(152k
Right.is(tok::identifier)152k
&&
Right.Next35.9k
&&
3636
152k
                                    
Right.Next->is(TT_ObjCMethodExpr)34.5k
))
3637
2.25k
    return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
3638
152k
  if (Left.is(tok::r_paren) && 
Line.Type == LT_ObjCProperty15.1k
)
3639
45
    return true;
3640
151k
  if (Left.ClosesTemplateDeclaration || 
Left.is(TT_FunctionAnnotationRParen)151k
)
3641
880
    return true;
3642
151k
  if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
3643
151k
                    TT_OverloadedOperator))
3644
1.06k
    return false;
3645
150k
  if (Left.is(TT_RangeBasedForLoopColon))
3646
47
    return true;
3647
150k
  if (Right.is(TT_RangeBasedForLoopColon))
3648
0
    return false;
3649
150k
  if (Left.is(TT_TemplateCloser) && 
Right.is(TT_TemplateOpener)1.18k
)
3650
10
    return true;
3651
149k
  if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
3652
149k
      
Left.is(tok::kw_operator)147k
)
3653
2.69k
    return false;
3654
147k
  if (Left.is(tok::equal) && 
!Right.isOneOf(tok::kw_default, tok::kw_delete)4.75k
&&
3655
147k
      
Line.Type == LT_VirtualFunctionDecl4.71k
&&
Left.NestingLevel == 013
)
3656
10
    return false;
3657
147k
  if (Left.is(tok::equal) && 
Right.is(tok::l_brace)4.74k
&&
3658
147k
      
!Style.Cpp11BracedListStyle477
)
3659
117
    return false;
3660
147k
  if (Left.is(tok::l_paren) && 
Left.is(TT_AttributeParen)18.0k
)
3661
33
    return false;
3662
147k
  if (Left.is(tok::l_paren) && 
Left.Previous18.0k
&&
3663
147k
      
(Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))17.8k
)
3664
437
    return false;
3665
146k
  if (Right.is(TT_ImplicitStringLiteral))
3666
720
    return false;
3667
145k
3668
145k
  if (Right.is(tok::r_paren) || 
Right.is(TT_TemplateCloser)128k
)
3669
20.1k
    return false;
3670
125k
  if (Right.is(tok::r_square) && 
Right.MatchingParen2.29k
&&
3671
125k
      
Right.MatchingParen->is(TT_LambdaLSquare)2.29k
)
3672
625
    return false;
3673
125k
3674
125k
  // We only break before r_brace if there was a corresponding break before
3675
125k
  // the l_brace, which is tracked by BreakBeforeClosingBrace.
3676
125k
  if (Right.is(tok::r_brace))
3677
3.18k
    return Right.MatchingParen && 
Right.MatchingParen->BlockKind == BK_Block3.16k
;
3678
122k
3679
122k
  // Allow breaking after a trailing annotation, e.g. after a method
3680
122k
  // declaration.
3681
122k
  if (Left.is(TT_TrailingAnnotation))
3682
1.95k
    return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
3683
1.95k
                          tok::less, tok::coloncolon);
3684
120k
3685
120k
  if (Right.is(tok::kw___attribute) ||
3686
120k
      
(120k
Right.is(tok::l_square)120k
&&
Right.is(TT_AttributeSquare)1.27k
))
3687
83
    return true;
3688
119k
3689
119k
  if (Left.is(tok::identifier) && 
Right.is(tok::string_literal)39.4k
)
3690
1.10k
    return true;
3691
118k
3692
118k
  if (Right.is(tok::identifier) && 
Right.Next34.1k
&&
Right.Next->is(TT_DictLiteral)32.7k
)
3693
45
    return true;
3694
118k
3695
118k
  if (Left.is(TT_CtorInitializerColon))
3696
442
    return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon;
3697
118k
  if (Right.is(TT_CtorInitializerColon))
3698
392
    return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
3699
117k
  if (Left.is(TT_CtorInitializerComma) &&
3700
117k
      
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma369
)
3701
120
    return false;
3702
117k
  if (Right.is(TT_CtorInitializerComma) &&
3703
117k
      
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma333
)
3704
75
    return true;
3705
117k
  if (Left.is(TT_InheritanceComma) &&
3706
117k
      
Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma76
)
3707
12
    return false;
3708
117k
  if (Right.is(TT_InheritanceComma) &&
3709
117k
      
Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma62
)
3710
0
    return true;
3711
117k
  if ((Left.is(tok::greater) && 
Right.is(tok::greater)231
) ||
3712
117k
      
(117k
Left.is(tok::less)117k
&&
Right.is(tok::less)3.01k
))
3713
128
    return false;
3714
117k
  if (Right.is(TT_BinaryOperator) &&
3715
117k
      
Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None7.86k
&&
3716
117k
      
(364
Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All364
||
3717
364
       
Right.getPrecedence() != prec::Assignment249
))
3718
300
    return true;
3719
117k
  if (Left.is(TT_ArrayInitializerLSquare))
3720
174
    return true;
3721
117k
  if (Right.is(tok::kw_typename) && 
Left.isNot(tok::kw_const)785
)
3722
782
    return true;
3723
116k
  if ((Left.isBinaryOperator() || 
Left.is(TT_BinaryOperator)106k
) &&
3724
116k
      
!Left.isOneOf(tok::arrowstar, tok::lessless)10.1k
&&
3725
116k
      
Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All9.76k
&&
3726
116k
      
(9.63k
Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None9.63k
||
3727
9.63k
       
Left.getPrecedence() == prec::Assignment330
))
3728
9.37k
    return true;
3729
107k
  if ((Left.is(TT_AttributeSquare) && 
Right.is(tok::l_square)100
) ||
3730
107k
      
(107k
Left.is(tok::r_square)107k
&&
Right.is(TT_AttributeSquare)1.98k
))
3731
32
    return false;
3732
106k
3733
106k
  auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
3734
106k
  if (Style.BraceWrapping.BeforeLambdaBody) {
3735
1.64k
    if (isAllmanLambdaBrace(Left))
3736
0
      return !isItAnEmptyLambdaAllowed(Left, ShortLambdaOption);
3737
1.64k
    if (isAllmanLambdaBrace(Right))
3738
96
      return !isItAnEmptyLambdaAllowed(Right, ShortLambdaOption);
3739
106k
  }
3740
106k
3741
106k
  return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
3742
106k
                      tok::kw_class, tok::kw_struct, tok::comment) ||
3743
106k
         
Right.isMemberAccess()93.0k
||
3744
106k
         Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
3745
91.0k
                       tok::colon, tok::l_square, tok::at) ||
3746
106k
         
(89.6k
Style.BraceWrapping.BeforeLambdaBody89.6k
&&
Right.is(tok::l_brace)1.29k
) ||
3747
106k
         
(89.6k
Left.is(tok::r_paren)89.6k
&&
3748
89.6k
          
Right.isOneOf(tok::identifier, tok::kw_const)12.5k
) ||
3749
106k
         
(88.9k
Left.is(tok::l_paren)88.9k
&&
!Right.is(tok::r_paren)9.70k
) ||
3750
106k
         
(79.2k
Left.is(TT_TemplateOpener)79.2k
&&
!Right.is(TT_TemplateCloser)83
);
3751
106k
}
3752
3753
0
void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
3754
0
  llvm::errs() << "AnnotatedTokens(L=" << Line.Level << "):\n";
3755
0
  const FormatToken *Tok = Line.First;
3756
0
  while (Tok) {
3757
0
    llvm::errs() << " M=" << Tok->MustBreakBefore
3758
0
                 << " C=" << Tok->CanBreakBefore
3759
0
                 << " T=" << getTokenTypeName(Tok->Type)
3760
0
                 << " S=" << Tok->SpacesRequiredBefore
3761
0
                 << " B=" << Tok->BlockParameterCount
3762
0
                 << " BK=" << Tok->BlockKind << " P=" << Tok->SplitPenalty
3763
0
                 << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength
3764
0
                 << " PPK=" << Tok->PackingKind << " FakeLParens=";
3765
0
    for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
3766
0
      llvm::errs() << Tok->FakeLParens[i] << "/";
3767
0
    llvm::errs() << " FakeRParens=" << Tok->FakeRParens;
3768
0
    llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo();
3769
0
    llvm::errs() << " Text='" << Tok->TokenText << "'\n";
3770
0
    if (!Tok->Next)
3771
0
      assert(Tok == Line.Last);
3772
0
    Tok = Tok->Next;
3773
0
  }
3774
0
  llvm::errs() << "----\n";
3775
0
}
3776
3777
} // namespace format
3778
} // namespace clang