Coverage Report

Created: 2022-07-16 07:03

/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 line starts with a token that can start a statement
30
/// with an initializer.
31
4.23k
static bool startsWithInitStatement(const AnnotatedLine &Line) {
32
4.23k
  return Line.startsWith(tok::kw_for) || 
Line.startsWith(tok::kw_if)3.78k
||
33
4.23k
         
Line.startsWith(tok::kw_switch)3.74k
;
34
4.23k
}
35
36
/// Returns \c true if the token can be used as an identifier in
37
/// an Objective-C \c \@selector, \c false otherwise.
38
///
39
/// Because getFormattingLangOpts() always lexes source code as
40
/// Objective-C++, C++ keywords like \c new and \c delete are
41
/// lexed as tok::kw_*, not tok::identifier, even for Objective-C.
42
///
43
/// For Objective-C and Objective-C++, both identifiers and keywords
44
/// are valid inside @selector(...) (or a macro which
45
/// invokes @selector(...)). So, we allow treat any identifier or
46
/// keyword as a potential Objective-C selector component.
47
619k
static bool canBeObjCSelectorComponent(const FormatToken &Tok) {
48
619k
  return Tok.Tok.getIdentifierInfo() != nullptr;
49
619k
}
50
51
/// With `Left` being '(', check if we're at either `[...](` or
52
/// `[...]<...>(`, where the [ opens a lambda capture list.
53
67.8k
static bool isLambdaParameterList(const FormatToken *Left) {
54
  // Skip <...> if present.
55
67.8k
  if (Left->Previous && 
Left->Previous->is(tok::greater)66.9k
&&
56
67.8k
      
Left->Previous->MatchingParen1.66k
&&
57
67.8k
      
Left->Previous->MatchingParen->is(TT_TemplateOpener)1.66k
) {
58
1.66k
    Left = Left->Previous->MatchingParen;
59
1.66k
  }
60
61
  // Check for `[...]`.
62
67.8k
  return Left->Previous && 
Left->Previous->is(tok::r_square)66.9k
&&
63
67.8k
         
Left->Previous->MatchingParen2.56k
&&
64
67.8k
         
Left->Previous->MatchingParen->is(TT_LambdaLSquare)2.56k
;
65
67.8k
}
66
67
/// Returns \c true if the token is followed by a boolean condition, \c false
68
/// otherwise.
69
195
static bool isKeywordWithCondition(const FormatToken &Tok) {
70
195
  return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
71
195
                     tok::kw_constexpr, tok::kw_catch);
72
195
}
73
74
/// A parser that gathers additional information about tokens.
75
///
76
/// The \c TokenAnnotator tries to match parenthesis and square brakets and
77
/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
78
/// into template parameter lists.
79
class AnnotatingParser {
80
public:
81
  AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
82
                   const AdditionalKeywords &Keywords)
83
      : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
84
254k
        Keywords(Keywords) {
85
254k
    Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
86
254k
    resetTokenMetadata();
87
254k
  }
88
89
private:
90
18.8k
  bool parseAngle() {
91
18.8k
    if (!CurrentToken || 
!CurrentToken->Previous18.8k
)
92
9
      return false;
93
18.8k
    if (NonTemplateLess.count(CurrentToken->Previous))
94
30
      return false;
95
96
18.7k
    const FormatToken &Previous = *CurrentToken->Previous; // The '<'.
97
18.7k
    if (Previous.Previous) {
98
18.7k
      if (Previous.Previous->Tok.isLiteral())
99
135
        return false;
100
18.6k
      if (Previous.Previous->is(tok::r_paren) && 
Contexts.size() > 1135
&&
101
18.6k
          
(63
!Previous.Previous->MatchingParen63
||
102
63
           !Previous.Previous->MatchingParen->is(
103
63
               TT_OverloadedOperatorLParen))) {
104
45
        return false;
105
45
      }
106
18.6k
    }
107
108
18.5k
    FormatToken *Left = CurrentToken->Previous;
109
18.5k
    Left->ParentBracket = Contexts.back().ContextKind;
110
18.5k
    ScopedContextCreator ContextCreator(*this, tok::less, 12);
111
112
    // If this angle is in the context of an expression, we need to be more
113
    // hesitant to detect it as opening template parameters.
114
18.5k
    bool InExprContext = Contexts.back().IsExpression;
115
116
18.5k
    Contexts.back().IsExpression = false;
117
    // If there's a template keyword before the opening angle bracket, this is a
118
    // template parameter, not an argument.
119
18.5k
    if (Left->Previous && 
Left->Previous->isNot(tok::kw_template)18.5k
)
120
13.5k
      Contexts.back().ContextType = Context::TemplateArgument;
121
122
18.5k
    if (Style.Language == FormatStyle::LK_Java &&
123
18.5k
        
CurrentToken->is(tok::question)81
) {
124
10
      next();
125
10
    }
126
127
58.8k
    while (CurrentToken) {
128
58.7k
      if (CurrentToken->is(tok::greater)) {
129
        // Try to do a better job at looking for ">>" within the condition of
130
        // a statement. Conservatively insert spaces between consecutive ">"
131
        // tokens to prevent splitting right bitshift operators and potentially
132
        // altering program semantics. This check is overly conservative and
133
        // will prevent spaces from being inserted in select nested template
134
        // parameter cases, but should not alter program semantics.
135
18.0k
        if (CurrentToken->Next && 
CurrentToken->Next->is(tok::greater)17.7k
&&
136
18.0k
            
Left->ParentBracket != tok::less1.45k
&&
137
18.0k
            
(81
isKeywordWithCondition(*Line.First)81
||
138
81
             CurrentToken->getStartOfNonWhitespace() ==
139
27
                 CurrentToken->Next->getStartOfNonWhitespace().getLocWithOffset(
140
63
                     -1))) {
141
63
          return false;
142
63
        }
143
17.9k
        Left->MatchingParen = CurrentToken;
144
17.9k
        CurrentToken->MatchingParen = Left;
145
        // In TT_Proto, we must distignuish between:
146
        //   map<key, value>
147
        //   msg < item: data >
148
        //   msg: < item: data >
149
        // In TT_TextProto, map<key, value> does not occur.
150
17.9k
        if (Style.Language == FormatStyle::LK_TextProto ||
151
17.9k
            
(17.7k
Style.Language == FormatStyle::LK_Proto17.7k
&&
Left->Previous96
&&
152
17.7k
             
Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral)96
)) {
153
273
          CurrentToken->setType(TT_DictLiteral);
154
17.7k
        } else {
155
17.7k
          CurrentToken->setType(TT_TemplateCloser);
156
17.7k
        }
157
17.9k
        next();
158
17.9k
        return true;
159
18.0k
      }
160
40.6k
      if (CurrentToken->is(tok::question) &&
161
40.6k
          
Style.Language == FormatStyle::LK_Java47
) {
162
4
        next();
163
4
        continue;
164
4
      }
165
40.6k
      if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
166
40.6k
          
(40.3k
CurrentToken->isOneOf(tok::colon, tok::question)40.3k
&&
InExprContext434
&&
167
40.3k
           
!Style.isCSharp()173
&&
Style.Language != FormatStyle::LK_Proto167
&&
168
40.3k
           
Style.Language != FormatStyle::LK_TextProto49
)) {
169
381
        return false;
170
381
      }
171
      // If a && or || is found and interpreted as a binary operator, this set
172
      // of angles is likely part of something like "a < b && c > d". If the
173
      // angles are inside an expression, the ||/&& might also be a binary
174
      // operator that was misinterpreted because we are parsing template
175
      // parameters.
176
      // FIXME: This is getting out of hand, write a decent parser.
177
40.2k
      if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
178
40.2k
          
CurrentToken->Previous->is(TT_BinaryOperator)228
&&
179
40.2k
          
Contexts[Contexts.size() - 2].IsExpression191
&&
180
40.2k
          
!Line.startsWith(tok::kw_template)38
) {
181
38
        return false;
182
38
      }
183
40.2k
      updateParameterCount(Left, CurrentToken);
184
40.2k
      if (Style.Language == FormatStyle::LK_Proto) {
185
700
        if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) {
186
700
          if (CurrentToken->is(tok::colon) ||
187
700
              
(536
CurrentToken->isOneOf(tok::l_brace, tok::less)536
&&
188
536
               
Previous->isNot(tok::colon)30
)) {
189
182
            Previous->setType(TT_SelectorName);
190
182
          }
191
700
        }
192
700
      }
193
40.2k
      if (!consumeToken())
194
0
        return false;
195
40.2k
    }
196
140
    return false;
197
18.5k
  }
198
199
72
  bool parseUntouchableParens() {
200
465
    while (CurrentToken) {
201
465
      CurrentToken->Finalized = true;
202
465
      switch (CurrentToken->Tok.getKind()) {
203
30
      case tok::l_paren:
204
30
        next();
205
30
        if (!parseUntouchableParens())
206
0
          return false;
207
30
        continue;
208
72
      case tok::r_paren:
209
72
        next();
210
72
        return true;
211
363
      default:
212
        // no-op
213
363
        break;
214
465
      }
215
363
      next();
216
363
    }
217
0
    return false;
218
72
  }
219
220
83.6k
  bool parseParens(bool LookForDecls = false) {
221
83.6k
    if (!CurrentToken)
222
9
      return false;
223
83.6k
    assert(CurrentToken->Previous && "Unknown previous token");
224
0
    FormatToken &OpeningParen = *CurrentToken->Previous;
225
83.6k
    assert(OpeningParen.is(tok::l_paren));
226
0
    FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
227
83.6k
    OpeningParen.ParentBracket = Contexts.back().ContextKind;
228
83.6k
    ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
229
230
    // FIXME: This is a bit of a hack. Do better.
231
83.6k
    Contexts.back().ColonIsForRangeExpr =
232
83.6k
        Contexts.size() == 2 && 
Contexts[0].ColonIsForRangeExpr72.3k
;
233
234
83.6k
    if (OpeningParen.Previous &&
235
83.6k
        
OpeningParen.Previous->is(TT_UntouchableMacroFunc)82.7k
) {
236
42
      OpeningParen.Finalized = true;
237
42
      return parseUntouchableParens();
238
42
    }
239
240
83.5k
    bool StartsObjCMethodExpr = false;
241
83.5k
    if (FormatToken *MaybeSel = OpeningParen.Previous) {
242
      // @selector( starts a selector.
243
82.7k
      if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && 
MaybeSel->Previous54
&&
244
82.7k
          
MaybeSel->Previous->is(tok::at)54
) {
245
54
        StartsObjCMethodExpr = true;
246
54
      }
247
82.7k
    }
248
249
83.5k
    if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
250
      // Find the previous kw_operator token.
251
3.53k
      FormatToken *Prev = &OpeningParen;
252
13.9k
      while (!Prev->is(tok::kw_operator)) {
253
10.4k
        Prev = Prev->Previous;
254
10.4k
        assert(Prev && "Expect a kw_operator prior to the OperatorLParen!");
255
10.4k
      }
256
257
      // If faced with "a.operator*(argument)" or "a->operator*(argument)",
258
      // i.e. the operator is called as a member function,
259
      // then the argument must be an expression.
260
3.53k
      bool OperatorCalledAsMemberFunction =
261
3.53k
          Prev->Previous && 
Prev->Previous->isOneOf(tok::period, tok::arrow)3.15k
;
262
3.53k
      Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
263
80.0k
    } else if (Style.isJavaScript() &&
264
80.0k
               
(2.15k
Line.startsWith(Keywords.kw_type, tok::identifier)2.15k
||
265
2.15k
                Line.startsWith(tok::kw_export, Keywords.kw_type,
266
2.13k
                                tok::identifier))) {
267
      // type X = (...);
268
      // export type X = (...);
269
20
      Contexts.back().IsExpression = false;
270
80.0k
    } else if (OpeningParen.Previous &&
271
80.0k
               
(79.1k
OpeningParen.Previous->isOneOf(tok::kw_static_assert,
272
79.1k
                                               tok::kw_while, tok::l_paren,
273
79.1k
                                               tok::comma, TT_BinaryOperator) ||
274
79.1k
                
OpeningParen.Previous->isIf()75.1k
)) {
275
      // static_assert, if and while usually contain expressions.
276
11.6k
      Contexts.back().IsExpression = true;
277
68.3k
    } else if (Style.isJavaScript() && 
OpeningParen.Previous1.94k
&&
278
68.3k
               
(1.90k
OpeningParen.Previous->is(Keywords.kw_function)1.90k
||
279
1.90k
                (OpeningParen.Previous->endsSequence(tok::identifier,
280
1.64k
                                                     Keywords.kw_function)))) {
281
      // function(...) or function f(...)
282
532
      Contexts.back().IsExpression = false;
283
67.8k
    } else if (Style.isJavaScript() && 
OpeningParen.Previous1.41k
&&
284
67.8k
               
OpeningParen.Previous->is(TT_JsTypeColon)1.37k
) {
285
      // let x: (SomeType);
286
8
      Contexts.back().IsExpression = false;
287
67.8k
    } else if (isLambdaParameterList(&OpeningParen)) {
288
      // This is a parameter list of a lambda expression.
289
2.45k
      Contexts.back().IsExpression = false;
290
65.3k
    } else if (Line.InPPDirective &&
291
65.3k
               
(2.79k
!OpeningParen.Previous2.79k
||
292
2.79k
                
!OpeningParen.Previous->is(tok::identifier)2.22k
)) {
293
820
      Contexts.back().IsExpression = true;
294
64.5k
    } else if (Contexts[Contexts.size() - 2].CaretFound) {
295
      // This is the parameter list of an ObjC block.
296
259
      Contexts.back().IsExpression = false;
297
64.2k
    } else if (OpeningParen.Previous &&
298
64.2k
               
OpeningParen.Previous->is(TT_ForEachMacro)64.0k
) {
299
      // The first argument to a foreach macro is a declaration.
300
257
      Contexts.back().ContextType = Context::ForEachMacro;
301
257
      Contexts.back().IsExpression = false;
302
64.0k
    } else if (OpeningParen.Previous && 
OpeningParen.Previous->MatchingParen63.7k
&&
303
64.0k
               
OpeningParen.Previous->MatchingParen->is(TT_ObjCBlockLParen)3.36k
) {
304
173
      Contexts.back().IsExpression = false;
305
63.8k
    } else if (!Line.MustBeDeclaration && 
!Line.InPPDirective14.7k
) {
306
12.7k
      bool IsForOrCatch =
307
12.7k
          OpeningParen.Previous &&
308
12.7k
          
OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch)12.5k
;
309
12.7k
      Contexts.back().IsExpression = !IsForOrCatch;
310
12.7k
    }
311
312
    // Infer the role of the l_paren based on the previous token if we haven't
313
    // detected one one yet.
314
83.5k
    if (PrevNonComment && 
OpeningParen.is(TT_Unknown)82.7k
) {
315
78.3k
      if (PrevNonComment->is(tok::kw___attribute)) {
316
338
        OpeningParen.setType(TT_AttributeParen);
317
78.0k
      } else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
318
78.0k
                                         tok::kw_typeof, tok::kw__Atomic,
319
78.0k
                                         tok::kw___underlying_type)) {
320
1.47k
        OpeningParen.setType(TT_TypeDeclarationParen);
321
        // decltype() and typeof() usually contain expressions.
322
1.47k
        if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
323
727
          Contexts.back().IsExpression = true;
324
1.47k
      }
325
78.3k
    }
326
327
83.5k
    if (StartsObjCMethodExpr) {
328
54
      Contexts.back().ColonIsObjCMethodExpr = true;
329
54
      OpeningParen.setType(TT_ObjCMethodExpr);
330
54
    }
331
332
    // MightBeFunctionType and ProbablyFunctionType are used for
333
    // function pointer and reference types as well as Objective-C
334
    // block types:
335
    //
336
    // void (*FunctionPointer)(void);
337
    // void (&FunctionReference)(void);
338
    // void (&&FunctionReference)(void);
339
    // void (^ObjCBlock)(void);
340
83.5k
    bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
341
83.5k
    bool ProbablyFunctionType =
342
83.5k
        CurrentToken->isOneOf(tok::star, tok::amp, tok::ampamp, tok::caret);
343
83.5k
    bool HasMultipleLines = false;
344
83.5k
    bool HasMultipleParametersOnALine = false;
345
83.5k
    bool MightBeObjCForRangeLoop =
346
83.5k
        OpeningParen.Previous && 
OpeningParen.Previous->is(tok::kw_for)82.7k
;
347
83.5k
    FormatToken *PossibleObjCForInToken = nullptr;
348
231k
    while (CurrentToken) {
349
      // LookForDecls is set when "if (" has been seen. Check for
350
      // 'identifier' '*' 'identifier' followed by not '=' -- this
351
      // '*' has to be a binary operator but determineStarAmpUsage() will
352
      // categorize it as an unary operator, so set the right type here.
353
231k
      if (LookForDecls && 
CurrentToken->Next22.2k
) {
354
18.0k
        FormatToken *Prev = CurrentToken->getPreviousNonComment();
355
18.0k
        if (Prev) {
356
18.0k
          FormatToken *PrevPrev = Prev->getPreviousNonComment();
357
18.0k
          FormatToken *Next = CurrentToken->Next;
358
18.0k
          if (PrevPrev && PrevPrev->is(tok::identifier) &&
359
18.0k
              
Prev->isOneOf(tok::star, tok::amp, tok::ampamp)2.72k
&&
360
18.0k
              
CurrentToken->is(tok::identifier)487
&&
Next->isNot(tok::equal)324
) {
361
324
            Prev->setType(TT_BinaryOperator);
362
324
            LookForDecls = false;
363
324
          }
364
18.0k
        }
365
18.0k
      }
366
367
231k
      if (CurrentToken->Previous->is(TT_PointerOrReference) &&
368
231k
          CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
369
7.26k
                                                    tok::coloncolon)) {
370
80
        ProbablyFunctionType = true;
371
80
      }
372
231k
      if (CurrentToken->is(tok::comma))
373
13.2k
        MightBeFunctionType = false;
374
231k
      if (CurrentToken->Previous->is(TT_BinaryOperator))
375
7.89k
        Contexts.back().IsExpression = true;
376
231k
      if (CurrentToken->is(tok::r_paren)) {
377
83.5k
        if (OpeningParen.isNot(TT_CppCastLParen) && 
MightBeFunctionType83.2k
&&
378
83.5k
            
ProbablyFunctionType60.2k
&&
CurrentToken->Next1.15k
&&
379
83.5k
            
(1.12k
CurrentToken->Next->is(tok::l_paren)1.12k
||
380
1.12k
             
(433
CurrentToken->Next->is(tok::l_square)433
&&
381
814
              
Line.MustBeDeclaration135
))) {
382
814
          OpeningParen.setType(OpeningParen.Next->is(tok::caret)
383
814
                                   ? 
TT_ObjCBlockLParen173
384
814
                                   : 
TT_FunctionTypeLParen641
);
385
814
        }
386
83.5k
        OpeningParen.MatchingParen = CurrentToken;
387
83.5k
        CurrentToken->MatchingParen = &OpeningParen;
388
389
83.5k
        if (CurrentToken->Next && 
CurrentToken->Next->is(tok::l_brace)72.3k
&&
390
83.5k
            
OpeningParen.Previous20.1k
&&
OpeningParen.Previous->is(tok::l_paren)20.1k
) {
391
          // Detect the case where macros are used to generate lambdas or
392
          // function bodies, e.g.:
393
          //   auto my_lambda = MACRO((Type *type, int i) { .. body .. });
394
99
          for (FormatToken *Tok = &OpeningParen; Tok != CurrentToken;
395
81
               Tok = Tok->Next) {
396
81
            if (Tok->is(TT_BinaryOperator) &&
397
81
                
Tok->isOneOf(tok::star, tok::amp, tok::ampamp)9
) {
398
9
              Tok->setType(TT_PointerOrReference);
399
9
            }
400
81
          }
401
18
        }
402
403
83.5k
        if (StartsObjCMethodExpr) {
404
54
          CurrentToken->setType(TT_ObjCMethodExpr);
405
54
          if (Contexts.back().FirstObjCSelectorName) {
406
12
            Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
407
12
                Contexts.back().LongestObjCSelectorName;
408
12
          }
409
54
        }
410
411
83.5k
        if (OpeningParen.is(TT_AttributeParen))
412
338
          CurrentToken->setType(TT_AttributeParen);
413
83.5k
        if (OpeningParen.is(TT_TypeDeclarationParen))
414
1.47k
          CurrentToken->setType(TT_TypeDeclarationParen);
415
83.5k
        if (OpeningParen.Previous &&
416
83.5k
            
OpeningParen.Previous->is(TT_JavaAnnotation)82.7k
) {
417
20
          CurrentToken->setType(TT_JavaAnnotation);
418
20
        }
419
83.5k
        if (OpeningParen.Previous &&
420
83.5k
            
OpeningParen.Previous->is(TT_LeadingJavaAnnotation)82.7k
) {
421
40
          CurrentToken->setType(TT_LeadingJavaAnnotation);
422
40
        }
423
83.5k
        if (OpeningParen.Previous &&
424
83.5k
            
OpeningParen.Previous->is(TT_AttributeSquare)82.7k
) {
425
27
          CurrentToken->setType(TT_AttributeSquare);
426
27
        }
427
428
83.5k
        if (!HasMultipleLines)
429
81.5k
          OpeningParen.setPackingKind(PPK_Inconclusive);
430
2.00k
        else if (HasMultipleParametersOnALine)
431
426
          OpeningParen.setPackingKind(PPK_BinPacked);
432
1.58k
        else
433
1.58k
          OpeningParen.setPackingKind(PPK_OnePerLine);
434
435
83.5k
        next();
436
83.5k
        return true;
437
83.5k
      }
438
148k
      if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
439
0
        return false;
440
441
148k
      if (CurrentToken->is(tok::l_brace) && 
OpeningParen.is(TT_ObjCBlockLParen)2.76k
)
442
0
        OpeningParen.setType(TT_Unknown);
443
148k
      if (CurrentToken->is(tok::comma) && 
CurrentToken->Next13.2k
&&
444
148k
          
!CurrentToken->Next->HasUnescapedNewline13.2k
&&
445
148k
          
!CurrentToken->Next->isTrailingComment()11.5k
) {
446
11.3k
        HasMultipleParametersOnALine = true;
447
11.3k
      }
448
148k
      bool ProbablyFunctionTypeLParen =
449
148k
          (CurrentToken->is(tok::l_paren) && 
CurrentToken->Next8.69k
&&
450
148k
           
CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret)8.69k
);
451
148k
      if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
452
148k
           
CurrentToken->Previous->isSimpleTypeSpecifier()144k
) &&
453
148k
          
!(12.5k
CurrentToken->is(tok::l_brace)12.5k
||
454
12.5k
            
(12.4k
CurrentToken->is(tok::l_paren)12.4k
&&
!ProbablyFunctionTypeLParen619
))) {
455
12.0k
        Contexts.back().IsExpression = false;
456
12.0k
      }
457
148k
      if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
458
3.60k
        MightBeObjCForRangeLoop = false;
459
3.60k
        if (PossibleObjCForInToken) {
460
9
          PossibleObjCForInToken->setType(TT_Unknown);
461
9
          PossibleObjCForInToken = nullptr;
462
9
        }
463
3.60k
      }
464
148k
      if (MightBeObjCForRangeLoop && 
CurrentToken->is(Keywords.kw_in)7.33k
) {
465
91
        PossibleObjCForInToken = CurrentToken;
466
91
        PossibleObjCForInToken->setType(TT_ObjCForIn);
467
91
      }
468
      // When we discover a 'new', we set CanBeExpression to 'false' in order to
469
      // parse the type correctly. Reset that after a comma.
470
148k
      if (CurrentToken->is(tok::comma))
471
13.2k
        Contexts.back().CanBeExpression = true;
472
473
148k
      FormatToken *Tok = CurrentToken;
474
148k
      if (!consumeToken())
475
7
        return false;
476
148k
      updateParameterCount(&OpeningParen, Tok);
477
148k
      if (CurrentToken && 
CurrentToken->HasUnescapedNewline148k
)
478
3.13k
        HasMultipleLines = true;
479
148k
    }
480
20
    return false;
481
83.5k
  }
482
483
10.0k
  bool isCSharpAttributeSpecifier(const FormatToken &Tok) {
484
10.0k
    if (!Style.isCSharp())
485
9.85k
      return false;
486
487
    // `identifier[i]` is not an attribute.
488
190
    if (Tok.Previous && 
Tok.Previous->is(tok::identifier)104
)
489
34
      return false;
490
491
    // Chains of [] in `identifier[i][j][k]` are not attributes.
492
156
    if (Tok.Previous && 
Tok.Previous->is(tok::r_square)70
) {
493
14
      auto *MatchingParen = Tok.Previous->MatchingParen;
494
14
      if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
495
6
        return false;
496
14
    }
497
498
150
    const FormatToken *AttrTok = Tok.Next;
499
150
    if (!AttrTok)
500
0
      return false;
501
502
    // Just an empty declaration e.g. string [].
503
150
    if (AttrTok->is(tok::r_square))
504
22
      return false;
505
506
    // Move along the tokens inbetween the '[' and ']' e.g. [STAThread].
507
442
    
while (128
AttrTok && AttrTok->isNot(tok::r_square))
508
314
      AttrTok = AttrTok->Next;
509
510
128
    if (!AttrTok)
511
0
      return false;
512
513
    // Allow an attribute to be the only content of a file.
514
128
    AttrTok = AttrTok->Next;
515
128
    if (!AttrTok)
516
74
      return true;
517
518
    // Limit this to being an access modifier that follows.
519
54
    if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
520
54
                         tok::comment, tok::kw_class, tok::kw_static,
521
54
                         tok::l_square, Keywords.kw_internal)) {
522
18
      return true;
523
18
    }
524
525
    // incase its a [XXX] retval func(....
526
36
    if (AttrTok->Next &&
527
36
        
AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)34
) {
528
2
      return true;
529
2
    }
530
531
34
    return false;
532
36
  }
533
534
16.3k
  bool isCpp11AttributeSpecifier(const FormatToken &Tok) {
535
16.3k
    if (!Style.isCpp() || 
!Tok.startsSequence(tok::l_square, tok::l_square)15.5k
)
536
15.4k
      return false;
537
    // The first square bracket is part of an ObjC array literal
538
916
    if (Tok.Previous && 
Tok.Previous->is(tok::at)613
)
539
33
      return false;
540
883
    const FormatToken *AttrTok = Tok.Next->Next;
541
883
    if (!AttrTok)
542
0
      return false;
543
    // C++17 '[[using ns: foo, bar(baz, blech)]]'
544
    // We assume nobody will name an ObjC variable 'using'.
545
883
    if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
546
11
      return true;
547
872
    if (AttrTok->isNot(tok::identifier))
548
6
      return false;
549
2.12k
    
while (866
AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
550
      // ObjC message send. We assume nobody will use : in a C++11 attribute
551
      // specifier parameter, although this is technically valid:
552
      // [[foo(:)]].
553
1.42k
      if (AttrTok->is(tok::colon) ||
554
1.42k
          
AttrTok->startsSequence(tok::identifier, tok::identifier)1.42k
||
555
1.42k
          
AttrTok->startsSequence(tok::r_paren, tok::identifier)1.27k
) {
556
165
        return false;
557
165
      }
558
1.26k
      if (AttrTok->is(tok::ellipsis))
559
1
        return true;
560
1.26k
      AttrTok = AttrTok->Next;
561
1.26k
    }
562
700
    return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
563
866
  }
564
565
10.0k
  bool parseSquare() {
566
10.0k
    if (!CurrentToken)
567
3
      return false;
568
569
    // A '[' could be an index subscript (after an identifier or after
570
    // ')' or ']'), it could be the start of an Objective-C method
571
    // expression, it could the start of an Objective-C array literal,
572
    // or it could be a C++ attribute specifier [[foo::bar]].
573
10.0k
    FormatToken *Left = CurrentToken->Previous;
574
10.0k
    Left->ParentBracket = Contexts.back().ContextKind;
575
10.0k
    FormatToken *Parent = Left->getPreviousNonComment();
576
577
    // Cases where '>' is followed by '['.
578
    // In C++, this can happen either in array of templates (foo<int>[10])
579
    // or when array is a nested template type (unique_ptr<type1<type2>[]>).
580
10.0k
    bool CppArrayTemplates =
581
10.0k
        Style.isCpp() && 
Parent9.29k
&&
Parent->is(TT_TemplateCloser)7.61k
&&
582
10.0k
        
(22
Contexts.back().CanBeExpression22
||
Contexts.back().IsExpression6
||
583
22
         
Contexts.back().ContextType == Context::TemplateArgument0
);
584
585
10.0k
    bool IsCpp11AttributeSpecifier = isCpp11AttributeSpecifier(*Left) ||
586
10.0k
                                     
Contexts.back().InCpp11AttributeSpecifier9.33k
;
587
588
    // Treat C# Attributes [STAThread] much like C++ attributes [[...]].
589
10.0k
    bool IsCSharpAttributeSpecifier =
590
10.0k
        isCSharpAttributeSpecifier(*Left) ||
591
10.0k
        
Contexts.back().InCSharpAttributeSpecifier9.95k
;
592
593
10.0k
    bool InsideInlineASM = Line.startsWith(tok::kw_asm);
594
10.0k
    bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style);
595
10.0k
    bool StartsObjCMethodExpr =
596
10.0k
        !IsCppStructuredBinding && 
!InsideInlineASM9.79k
&&
!CppArrayTemplates9.71k
&&
597
10.0k
        
Style.isCpp()9.69k
&&
!IsCpp11AttributeSpecifier8.94k
&&
598
10.0k
        
!IsCSharpAttributeSpecifier7.53k
&&
Contexts.back().CanBeExpression7.53k
&&
599
10.0k
        
Left->isNot(TT_LambdaLSquare)7.34k
&&
600
10.0k
        
!CurrentToken->isOneOf(tok::l_brace, tok::r_square)4.41k
&&
601
10.0k
        
(3.61k
!Parent3.61k
||
602
3.61k
         Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
603
2.67k
                         tok::kw_return, tok::kw_throw) ||
604
3.61k
         
Parent->isUnaryOperator()2.22k
||
605
         // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
606
3.61k
         
Parent->isOneOf(TT_ObjCForIn, TT_CastRParen)2.14k
||
607
3.61k
         (getBinOpPrecedence(Parent->Tok.getKind(), true, true) >
608
2.10k
          prec::Unknown));
609
10.0k
    bool ColonFound = false;
610
611
10.0k
    unsigned BindingIncrease = 1;
612
10.0k
    if (IsCppStructuredBinding) {
613
256
      Left->setType(TT_StructuredBindingLSquare);
614
9.79k
    } else if (Left->is(TT_Unknown)) {
615
6.86k
      if (StartsObjCMethodExpr) {
616
1.95k
        Left->setType(TT_ObjCMethodExpr);
617
4.91k
      } else if (InsideInlineASM) {
618
75
        Left->setType(TT_InlineASMSymbolicNameLSquare);
619
4.84k
      } else if (IsCpp11AttributeSpecifier) {
620
1.42k
        Left->setType(TT_AttributeSquare);
621
3.41k
      } else if (Style.isJavaScript() && 
Parent348
&&
622
3.41k
                 
Contexts.back().ContextKind == tok::l_brace340
&&
623
3.41k
                 
Parent->isOneOf(tok::l_brace, tok::comma)36
) {
624
20
        Left->setType(TT_JsComputedPropertyName);
625
3.39k
      } else if (Style.isCpp() && 
Contexts.back().ContextKind == tok::l_brace2.66k
&&
626
3.39k
                 
Parent83
&&
Parent->isOneOf(tok::l_brace, tok::comma)83
) {
627
65
        Left->setType(TT_DesignatedInitializerLSquare);
628
3.33k
      } else if (IsCSharpAttributeSpecifier) {
629
94
        Left->setType(TT_AttributeSquare);
630
3.23k
      } else if (CurrentToken->is(tok::r_square) && 
Parent931
&&
631
3.23k
                 
Parent->is(TT_TemplateCloser)931
) {
632
14
        Left->setType(TT_ArraySubscriptLSquare);
633
3.22k
      } else if (Style.Language == FormatStyle::LK_Proto ||
634
3.22k
                 
Style.Language == FormatStyle::LK_TextProto3.15k
) {
635
        // Square braces in LK_Proto can either be message field attributes:
636
        //
637
        // optional Aaa aaa = 1 [
638
        //   (aaa) = aaa
639
        // ];
640
        //
641
        // extensions 123 [
642
        //   (aaa) = aaa
643
        // ];
644
        //
645
        // or text proto extensions (in options):
646
        //
647
        // option (Aaa.options) = {
648
        //   [type.type/type] {
649
        //     key: value
650
        //   }
651
        // }
652
        //
653
        // or repeated fields (in options):
654
        //
655
        // option (Aaa.options) = {
656
        //   keys: [ 1, 2, 3 ]
657
        // }
658
        //
659
        // In the first and the third case we want to spread the contents inside
660
        // the square braces; in the second we want to keep them inline.
661
170
        Left->setType(TT_ArrayInitializerLSquare);
662
170
        if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
663
170
                                tok::equal) &&
664
170
            !Left->endsSequence(tok::l_square, tok::numeric_constant,
665
138
                                tok::identifier) &&
666
170
            
!Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)132
) {
667
78
          Left->setType(TT_ProtoExtensionLSquare);
668
78
          BindingIncrease = 10;
669
78
        }
670
3.05k
      } else if (!CppArrayTemplates && 
Parent3.04k
&&
671
3.05k
                 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
672
3.03k
                                 tok::comma, tok::l_paren, tok::l_square,
673
3.03k
                                 tok::question, tok::colon, tok::kw_return,
674
                                 // Should only be relevant to JavaScript:
675
3.03k
                                 tok::kw_default)) {
676
371
        Left->setType(TT_ArrayInitializerLSquare);
677
2.68k
      } else {
678
2.68k
        BindingIncrease = 10;
679
2.68k
        Left->setType(TT_ArraySubscriptLSquare);
680
2.68k
      }
681
6.86k
    }
682
683
10.0k
    ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease);
684
10.0k
    Contexts.back().IsExpression = true;
685
10.0k
    if (Style.isJavaScript() && 
Parent348
&&
Parent->is(TT_JsTypeColon)340
)
686
4
      Contexts.back().IsExpression = false;
687
688
10.0k
    Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
689
10.0k
    Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
690
10.0k
    Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
691
692
30.1k
    while (CurrentToken) {
693
30.1k
      if (CurrentToken->is(tok::r_square)) {
694
10.0k
        if (IsCpp11AttributeSpecifier)
695
1.42k
          CurrentToken->setType(TT_AttributeSquare);
696
10.0k
        if (IsCSharpAttributeSpecifier) {
697
94
          CurrentToken->setType(TT_AttributeSquare);
698
9.94k
        } else if (((CurrentToken->Next &&
699
9.94k
                     
CurrentToken->Next->is(tok::l_paren)9.79k
) ||
700
9.94k
                    
(7.33k
CurrentToken->Previous7.33k
&&
701
7.33k
                     CurrentToken->Previous->Previous == Left)) &&
702
9.94k
                   
Left->is(TT_ObjCMethodExpr)4.95k
) {
703
          // An ObjC method call is rarely followed by an open parenthesis. It
704
          // also can't be composed of just one token, unless it's a macro that
705
          // will be expanded to more tokens.
706
          // FIXME: Do we incorrectly label ":" with this?
707
101
          StartsObjCMethodExpr = false;
708
101
          Left->setType(TT_Unknown);
709
101
        }
710
10.0k
        if (StartsObjCMethodExpr && 
CurrentToken->Previous != Left1.85k
) {
711
1.85k
          CurrentToken->setType(TT_ObjCMethodExpr);
712
          // If we haven't seen a colon yet, make sure the last identifier
713
          // before the r_square is tagged as a selector name component.
714
1.85k
          if (!ColonFound && 
CurrentToken->Previous403
&&
715
1.85k
              
CurrentToken->Previous->is(TT_Unknown)403
&&
716
1.85k
              
canBeObjCSelectorComponent(*CurrentToken->Previous)96
) {
717
90
            CurrentToken->Previous->setType(TT_SelectorName);
718
90
          }
719
          // determineStarAmpUsage() thinks that '*' '[' is allocating an
720
          // array of pointers, but if '[' starts a selector then '*' is a
721
          // binary operator.
722
1.85k
          if (Parent && 
Parent->is(TT_PointerOrReference)929
)
723
48
            Parent->overwriteFixedType(TT_BinaryOperator);
724
1.85k
        }
725
        // An arrow after an ObjC method expression is not a lambda arrow.
726
10.0k
        if (CurrentToken->getType() == TT_ObjCMethodExpr &&
727
10.0k
            
CurrentToken->Next1.85k
&&
CurrentToken->Next->is(TT_LambdaArrow)1.81k
) {
728
6
          CurrentToken->Next->overwriteFixedType(TT_Unknown);
729
6
        }
730
10.0k
        Left->MatchingParen = CurrentToken;
731
10.0k
        CurrentToken->MatchingParen = Left;
732
        // FirstObjCSelectorName is set when a colon is found. This does
733
        // not work, however, when the method has no parameters.
734
        // Here, we set FirstObjCSelectorName when the end of the method call is
735
        // reached, in case it was not set already.
736
10.0k
        if (!Contexts.back().FirstObjCSelectorName) {
737
8.58k
          FormatToken *Previous = CurrentToken->getPreviousNonComment();
738
8.58k
          if (Previous && Previous->is(TT_SelectorName)) {
739
90
            Previous->ObjCSelectorNameParts = 1;
740
90
            Contexts.back().FirstObjCSelectorName = Previous;
741
90
          }
742
8.58k
        } else {
743
1.45k
          Left->ParameterCount =
744
1.45k
              Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
745
1.45k
        }
746
10.0k
        if (Contexts.back().FirstObjCSelectorName) {
747
1.54k
          Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
748
1.54k
              Contexts.back().LongestObjCSelectorName;
749
1.54k
          if (Left->BlockParameterCount > 1)
750
60
            Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
751
1.54k
        }
752
10.0k
        next();
753
10.0k
        return true;
754
10.0k
      }
755
20.0k
      if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
756
3
        return false;
757
20.0k
      if (CurrentToken->is(tok::colon)) {
758
2.21k
        if (IsCpp11AttributeSpecifier &&
759
2.21k
            CurrentToken->endsSequence(tok::colon, tok::identifier,
760
11
                                       tok::kw_using)) {
761
          // Remember that this is a [[using ns: foo]] C++ attribute, so we
762
          // don't add a space before the colon (unlike other colons).
763
11
          CurrentToken->setType(TT_AttributeColon);
764
2.20k
        } else if (Left->isOneOf(TT_ArraySubscriptLSquare,
765
2.20k
                                 TT_DesignatedInitializerLSquare)) {
766
15
          Left->setType(TT_ObjCMethodExpr);
767
15
          StartsObjCMethodExpr = true;
768
15
          Contexts.back().ColonIsObjCMethodExpr = true;
769
15
          if (Parent && Parent->is(tok::r_paren)) {
770
            // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
771
6
            Parent->setType(TT_CastRParen);
772
6
          }
773
15
        }
774
2.21k
        ColonFound = true;
775
2.21k
      }
776
20.0k
      if (CurrentToken->is(tok::comma) && 
Left->is(TT_ObjCMethodExpr)1.44k
&&
777
20.0k
          
!ColonFound84
) {
778
6
        Left->setType(TT_ArrayInitializerLSquare);
779
6
      }
780
20.0k
      FormatToken *Tok = CurrentToken;
781
20.0k
      if (!consumeToken())
782
0
        return false;
783
20.0k
      updateParameterCount(Left, Tok);
784
20.0k
    }
785
6
    return false;
786
10.0k
  }
787
788
1.53k
  bool couldBeInStructArrayInitializer() const {
789
1.53k
    if (Contexts.size() < 2)
790
0
      return false;
791
    // We want to back up no more then 2 context levels i.e.
792
    // . { { <-
793
1.53k
    const auto End = std::next(Contexts.rbegin(), 2);
794
1.53k
    auto Last = Contexts.rbegin();
795
1.53k
    unsigned Depth = 0;
796
4.59k
    for (; Last != End; 
++Last3.06k
)
797
3.06k
      if (Last->ContextKind == tok::l_brace)
798
3.06k
        ++Depth;
799
1.53k
    return Depth == 2 && Last->ContextKind != tok::l_brace;
800
1.53k
  }
801
802
53.4k
  bool parseBrace() {
803
53.4k
    if (!CurrentToken)
804
37.0k
      return true;
805
806
16.3k
    assert(CurrentToken->Previous);
807
0
    FormatToken &OpeningBrace = *CurrentToken->Previous;
808
16.3k
    assert(OpeningBrace.is(tok::l_brace));
809
0
    OpeningBrace.ParentBracket = Contexts.back().ContextKind;
810
811
16.3k
    if (Contexts.back().CaretFound)
812
477
      OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
813
16.3k
    Contexts.back().CaretFound = false;
814
815
16.3k
    ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
816
16.3k
    Contexts.back().ColonIsDictLiteral = true;
817
16.3k
    if (OpeningBrace.is(BK_BracedInit))
818
6.41k
      Contexts.back().IsExpression = true;
819
16.3k
    if (Style.isJavaScript() && 
OpeningBrace.Previous1.23k
&&
820
16.3k
        
OpeningBrace.Previous->is(TT_JsTypeColon)1.23k
) {
821
70
      Contexts.back().IsExpression = false;
822
70
    }
823
824
16.3k
    unsigned CommaCount = 0;
825
64.4k
    while (CurrentToken) {
826
63.9k
      if (CurrentToken->is(tok::r_brace)) {
827
15.7k
        assert(OpeningBrace.Optional == CurrentToken->Optional);
828
0
        OpeningBrace.MatchingParen = CurrentToken;
829
15.7k
        CurrentToken->MatchingParen = &OpeningBrace;
830
15.7k
        if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
831
2.00k
          if (OpeningBrace.ParentBracket == tok::l_brace &&
832
2.00k
              
couldBeInStructArrayInitializer()1.53k
&&
CommaCount > 01.29k
) {
833
1.22k
            Contexts.back().ContextType = Context::StructArrayInitializer;
834
1.22k
          }
835
2.00k
        }
836
15.7k
        next();
837
15.7k
        return true;
838
15.7k
      }
839
48.1k
      if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
840
0
        return false;
841
48.1k
      updateParameterCount(&OpeningBrace, CurrentToken);
842
48.1k
      if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
843
4.98k
        FormatToken *Previous = CurrentToken->getPreviousNonComment();
844
4.98k
        if (Previous->is(TT_JsTypeOptionalQuestion))
845
24
          Previous = Previous->getPreviousNonComment();
846
4.98k
        if ((CurrentToken->is(tok::colon) &&
847
4.98k
             
(1.56k
!Contexts.back().ColonIsDictLiteral1.56k
||
!Style.isCpp()1.56k
)) ||
848
4.98k
            
Style.Language == FormatStyle::LK_Proto3.66k
||
849
4.98k
            
Style.Language == FormatStyle::LK_TextProto3.54k
) {
850
1.54k
          OpeningBrace.setType(TT_DictLiteral);
851
1.54k
          if (Previous->Tok.getIdentifierInfo() ||
852
1.54k
              
Previous->is(tok::string_literal)285
) {
853
1.34k
            Previous->setType(TT_SelectorName);
854
1.34k
          }
855
1.54k
        }
856
4.98k
        if (CurrentToken->is(tok::colon) && 
OpeningBrace.is(TT_Unknown)1.56k
)
857
193
          OpeningBrace.setType(TT_DictLiteral);
858
4.78k
        else if (Style.isJavaScript())
859
670
          OpeningBrace.overwriteFixedType(TT_DictLiteral);
860
4.98k
      }
861
48.1k
      if (CurrentToken->is(tok::comma)) {
862
13.6k
        if (Style.isJavaScript())
863
441
          OpeningBrace.overwriteFixedType(TT_DictLiteral);
864
13.6k
        ++CommaCount;
865
13.6k
      }
866
48.1k
      if (!consumeToken())
867
0
        return false;
868
48.1k
    }
869
555
    return true;
870
16.3k
  }
871
872
256k
  void updateParameterCount(FormatToken *Left, FormatToken *Current) {
873
    // For ObjC methods, the number of parameters is calculated differently as
874
    // method declarations have a different structure (the parameters are not
875
    // inside a bracket scope).
876
256k
    if (Current->is(tok::l_brace) && 
Current->is(BK_Block)6.51k
)
877
2.55k
      ++Left->BlockParameterCount;
878
256k
    if (Current->is(tok::comma)) {
879
30.6k
      ++Left->ParameterCount;
880
30.6k
      if (!Left->Role)
881
17.1k
        Left->Role.reset(new CommaSeparatedList(Style));
882
30.6k
      Left->Role->CommaFound(Current);
883
226k
    } else if (Left->ParameterCount == 0 && 
Current->isNot(tok::comment)85.2k
) {
884
84.1k
      Left->ParameterCount = 1;
885
84.1k
    }
886
256k
  }
887
888
2.31k
  bool parseConditional() {
889
5.38k
    while (CurrentToken) {
890
5.38k
      if (CurrentToken->is(tok::colon)) {
891
2.30k
        CurrentToken->setType(TT_ConditionalExpr);
892
2.30k
        next();
893
2.30k
        return true;
894
2.30k
      }
895
3.08k
      if (!consumeToken())
896
12
        return false;
897
3.08k
    }
898
0
    return false;
899
2.31k
  }
900
901
5.05k
  bool parseTemplateDeclaration() {
902
5.05k
    if (CurrentToken && CurrentToken->is(tok::less)) {
903
4.99k
      CurrentToken->setType(TT_TemplateOpener);
904
4.99k
      next();
905
4.99k
      if (!parseAngle())
906
0
        return false;
907
4.99k
      if (CurrentToken)
908
4.98k
        CurrentToken->Previous->ClosesTemplateDeclaration = true;
909
4.99k
      return true;
910
4.99k
    }
911
56
    return false;
912
5.05k
  }
913
914
932k
  bool consumeToken() {
915
932k
    FormatToken *Tok = CurrentToken;
916
932k
    next();
917
932k
    switch (Tok->Tok.getKind()) {
918
3.18k
    case tok::plus:
919
4.88k
    case tok::minus:
920
4.88k
      if (!Tok->Previous && 
Line.MustBeDeclaration802
)
921
776
        Tok->setType(TT_ObjCMethodSpecifier);
922
4.88k
      break;
923
14.1k
    case tok::colon:
924
14.1k
      if (!Tok->Previous)
925
3
        return false;
926
      // Colons from ?: are handled in parseConditional().
927
14.1k
      if (Style.isJavaScript()) {
928
1.00k
        if (Contexts.back().ColonIsForRangeExpr || // colon in for loop
929
1.00k
            
(998
Contexts.size() == 1998
&& // switch/case labels
930
998
             
!Line.First->isOneOf(tok::kw_enum, tok::kw_case)320
) ||
931
1.00k
            
Contexts.back().ContextKind == tok::l_paren694
|| // function params
932
1.00k
            
Contexts.back().ContextKind == tok::l_square570
|| // array type
933
1.00k
            
(566
!Contexts.back().IsExpression566
&&
934
566
             
Contexts.back().ContextKind == tok::l_brace118
) || // object type
935
1.00k
            
(464
Contexts.size() == 1464
&&
936
554
             
Line.MustBeDeclaration16
)) { // method/property declaration
937
554
          Contexts.back().IsExpression = false;
938
554
          Tok->setType(TT_JsTypeColon);
939
554
          break;
940
554
        }
941
13.1k
      } else if (Style.isCSharp()) {
942
28
        if (Contexts.back().InCSharpAttributeSpecifier) {
943
2
          Tok->setType(TT_AttributeColon);
944
2
          break;
945
2
        }
946
26
        if (Contexts.back().ContextKind == tok::l_paren) {
947
14
          Tok->setType(TT_CSharpNamedArgumentColon);
948
14
          break;
949
14
        }
950
26
      }
951
13.5k
      if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
952
13.5k
          
Line.First->startsSequence(tok::kw_export, Keywords.kw_module)13.5k
||
953
13.5k
          
Line.First->startsSequence(tok::kw_export, Keywords.kw_import)13.5k
) {
954
81
        Tok->setType(TT_ModulePartitionColon);
955
13.4k
      } else if (Contexts.back().ColonIsDictLiteral ||
956
13.4k
                 
Style.Language == FormatStyle::LK_Proto12.0k
||
957
13.4k
                 
Style.Language == FormatStyle::LK_TextProto11.8k
) {
958
2.61k
        Tok->setType(TT_DictLiteral);
959
2.61k
        if (Style.Language == FormatStyle::LK_TextProto) {
960
1.34k
          if (FormatToken *Previous = Tok->getPreviousNonComment())
961
1.34k
            Previous->setType(TT_SelectorName);
962
1.34k
        }
963
10.8k
      } else if (Contexts.back().ColonIsObjCMethodExpr ||
964
10.8k
                 
Line.startsWith(TT_ObjCMethodSpecifier)8.66k
) {
965
2.83k
        Tok->setType(TT_ObjCMethodExpr);
966
2.83k
        const FormatToken *BeforePrevious = Tok->Previous->Previous;
967
        // Ensure we tag all identifiers in method declarations as
968
        // TT_SelectorName.
969
2.83k
        bool UnknownIdentifierInMethodDeclaration =
970
2.83k
            Line.startsWith(TT_ObjCMethodSpecifier) &&
971
2.83k
            
Tok->Previous->is(tok::identifier)643
&&
Tok->Previous->is(TT_Unknown)619
;
972
2.83k
        if (!BeforePrevious ||
973
            // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
974
2.83k
            !(BeforePrevious->is(TT_CastRParen) ||
975
2.83k
              
(2.51k
BeforePrevious->is(TT_ObjCMethodExpr)2.51k
&&
976
2.51k
               
BeforePrevious->is(tok::colon)231
)) ||
977
2.83k
            
BeforePrevious->is(tok::r_square)353
||
978
2.83k
            
Contexts.back().LongestObjCSelectorName == 0353
||
979
2.83k
            
UnknownIdentifierInMethodDeclaration58
) {
980
2.78k
          Tok->Previous->setType(TT_SelectorName);
981
2.78k
          if (!Contexts.back().FirstObjCSelectorName) {
982
1.79k
            Contexts.back().FirstObjCSelectorName = Tok->Previous;
983
1.79k
          } else 
if (988
Tok->Previous->ColumnWidth >
984
988
                     Contexts.back().LongestObjCSelectorName) {
985
703
            Contexts.back().LongestObjCSelectorName =
986
703
                Tok->Previous->ColumnWidth;
987
703
          }
988
2.78k
          Tok->Previous->ParameterIndex =
989
2.78k
              Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
990
2.78k
          ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
991
2.78k
        }
992
8.02k
      } else if (Contexts.back().ColonIsForRangeExpr) {
993
880
        Tok->setType(TT_RangeBasedForLoopColon);
994
7.14k
      } else if (CurrentToken && 
CurrentToken->is(tok::numeric_constant)3.81k
) {
995
344
        Tok->setType(TT_BitFieldColon);
996
6.80k
      } else if (Contexts.size() == 1 &&
997
6.80k
                 !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
998
6.53k
                                      tok::kw_default)) {
999
5.08k
        FormatToken *Prev = Tok->getPreviousNonComment();
1000
5.08k
        if (!Prev)
1001
0
          break;
1002
5.08k
        if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept)) {
1003
1.67k
          Tok->setType(TT_CtorInitializerColon);
1004
3.40k
        } else if (Prev->is(tok::kw_try)) {
1005
          // Member initializer list within function try block.
1006
44
          FormatToken *PrevPrev = Prev->getPreviousNonComment();
1007
44
          if (!PrevPrev)
1008
4
            break;
1009
40
          if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1010
40
            Tok->setType(TT_CtorInitializerColon);
1011
3.36k
        } else {
1012
3.36k
          Tok->setType(TT_InheritanceColon);
1013
3.36k
        }
1014
5.08k
      } else 
if (1.71k
canBeObjCSelectorComponent(*Tok->Previous)1.71k
&&
Tok->Next724
&&
1015
1.71k
                 
(274
Tok->Next->isOneOf(tok::r_paren, tok::comma)274
||
1016
274
                  
(209
canBeObjCSelectorComponent(*Tok->Next)209
&&
Tok->Next->Next119
&&
1017
209
                   
Tok->Next->Next->is(tok::colon)119
))) {
1018
        // This handles a special macro in ObjC code where selectors including
1019
        // the colon are passed as macro arguments.
1020
101
        Tok->setType(TT_ObjCMethodExpr);
1021
1.61k
      } else if (Contexts.back().ContextKind == tok::l_paren) {
1022
126
        Tok->setType(TT_InlineASMColon);
1023
126
      }
1024
13.5k
      break;
1025
13.5k
    case tok::pipe:
1026
7.64k
    case tok::amp:
1027
      // | and & in declarations/type expressions represent union and
1028
      // intersection types, respectively.
1029
7.64k
      if (Style.isJavaScript() && 
!Contexts.back().IsExpression128
)
1030
112
        Tok->setType(TT_JsTypeOperator);
1031
7.64k
      break;
1032
7.76k
    case tok::kw_if:
1033
7.76k
      if (CurrentToken &&
1034
7.76k
          
CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)7.75k
) {
1035
657
        next();
1036
657
      }
1037
7.76k
      LLVM_FALLTHROUGH;
1038
8.62k
    case tok::kw_while:
1039
8.62k
      if (CurrentToken && 
CurrentToken->is(tok::l_paren)8.60k
) {
1040
8.36k
        next();
1041
8.36k
        if (!parseParens(/*LookForDecls=*/true))
1042
0
          return false;
1043
8.36k
      }
1044
8.62k
      break;
1045
8.62k
    case tok::kw_for:
1046
1.92k
      if (Style.isJavaScript()) {
1047
        // x.for and {for: ...}
1048
60
        if ((Tok->Previous && 
Tok->Previous->is(tok::period)12
) ||
1049
60
            
(52
Tok->Next52
&&
Tok->Next->is(tok::colon)52
)) {
1050
12
          break;
1051
12
        }
1052
        // JS' for await ( ...
1053
48
        if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1054
8
          next();
1055
48
      }
1056
1.90k
      if (Style.isCpp() && 
CurrentToken1.84k
&&
CurrentToken->is(tok::kw_co_await)1.84k
)
1057
27
        next();
1058
1.90k
      Contexts.back().ColonIsForRangeExpr = true;
1059
1.90k
      if (!CurrentToken || 
CurrentToken->isNot(tok::l_paren)1.90k
)
1060
7
        return false;
1061
1.90k
      next();
1062
1.90k
      if (!parseParens())
1063
0
        return false;
1064
1.90k
      break;
1065
72.0k
    case tok::l_paren:
1066
      // When faced with 'operator()()', the kw_operator handler incorrectly
1067
      // marks the first l_paren as a OverloadedOperatorLParen. Here, we make
1068
      // the first two parens OverloadedOperators and the second l_paren an
1069
      // OverloadedOperatorLParen.
1070
72.0k
      if (Tok->Previous && 
Tok->Previous->is(tok::r_paren)71.2k
&&
1071
72.0k
          
Tok->Previous->MatchingParen1.69k
&&
1072
72.0k
          
Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)1.69k
) {
1073
292
        Tok->Previous->setType(TT_OverloadedOperator);
1074
292
        Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1075
292
        Tok->setType(TT_OverloadedOperatorLParen);
1076
292
      }
1077
1078
72.0k
      if (!parseParens())
1079
33
        return false;
1080
71.9k
      if (Line.MustBeDeclaration && 
Contexts.size() == 156.1k
&&
1081
71.9k
          
!Contexts.back().IsExpression47.5k
&&
!Line.startsWith(TT_ObjCProperty)39.1k
&&
1082
71.9k
          
!Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)39.0k
&&
1083
71.9k
          
(37.9k
!Tok->Previous37.9k
||
1084
37.9k
           !Tok->Previous->isOneOf(tok::kw___attribute,
1085
37.8k
                                   TT_LeadingJavaAnnotation))) {
1086
37.6k
        Line.MightBeFunctionDecl = true;
1087
37.6k
      }
1088
71.9k
      break;
1089
10.0k
    case tok::l_square:
1090
10.0k
      if (!parseSquare())
1091
12
        return false;
1092
10.0k
      break;
1093
53.4k
    case tok::l_brace:
1094
53.4k
      if (Style.Language == FormatStyle::LK_TextProto) {
1095
311
        FormatToken *Previous = Tok->getPreviousNonComment();
1096
311
        if (Previous && Previous->getType() != TT_DictLiteral)
1097
186
          Previous->setType(TT_SelectorName);
1098
311
      }
1099
53.4k
      if (!parseBrace())
1100
0
        return false;
1101
53.4k
      break;
1102
53.4k
    case tok::less:
1103
13.8k
      if (parseAngle()) {
1104
12.9k
        Tok->setType(TT_TemplateOpener);
1105
        // In TT_Proto, we must distignuish between:
1106
        //   map<key, value>
1107
        //   msg < item: data >
1108
        //   msg: < item: data >
1109
        // In TT_TextProto, map<key, value> does not occur.
1110
12.9k
        if (Style.Language == FormatStyle::LK_TextProto ||
1111
12.9k
            
(12.7k
Style.Language == FormatStyle::LK_Proto12.7k
&&
Tok->Previous96
&&
1112
12.7k
             
Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral)96
)) {
1113
273
          Tok->setType(TT_DictLiteral);
1114
273
          FormatToken *Previous = Tok->getPreviousNonComment();
1115
273
          if (Previous && Previous->getType() != TT_DictLiteral)
1116
143
            Previous->setType(TT_SelectorName);
1117
273
        }
1118
12.9k
      } else {
1119
841
        Tok->setType(TT_BinaryOperator);
1120
841
        NonTemplateLess.insert(Tok);
1121
841
        CurrentToken = Tok;
1122
841
        next();
1123
841
      }
1124
13.8k
      break;
1125
24
    case tok::r_paren:
1126
24
    case tok::r_square:
1127
24
      return false;
1128
37.2k
    case tok::r_brace:
1129
      // Lines can start with '}'.
1130
37.2k
      if (Tok->Previous)
1131
6
        return false;
1132
37.2k
      break;
1133
37.2k
    case tok::greater:
1134
1.11k
      if (Style.Language != FormatStyle::LK_TextProto)
1135
1.11k
        Tok->setType(TT_BinaryOperator);
1136
1.11k
      if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1137
18
        Tok->SpacesRequiredBefore = 1;
1138
1.11k
      break;
1139
3.33k
    case tok::kw_operator:
1140
3.33k
      if (Style.Language == FormatStyle::LK_TextProto ||
1141
3.33k
          
Style.Language == FormatStyle::LK_Proto3.32k
) {
1142
18
        break;
1143
18
      }
1144
8.51k
      
while (3.31k
CurrentToken &&
1145
8.51k
             
!CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)8.50k
) {
1146
5.23k
        if (CurrentToken->isOneOf(tok::star, tok::amp))
1147
1.44k
          CurrentToken->setType(TT_PointerOrReference);
1148
5.23k
        consumeToken();
1149
5.23k
        if (CurrentToken && CurrentToken->is(tok::comma) &&
1150
5.23k
            
CurrentToken->Previous->isNot(tok::kw_operator)36
) {
1151
36
          break;
1152
36
        }
1153
5.19k
        if (CurrentToken && CurrentToken->Previous->isOneOf(
1154
5.19k
                                TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1155
5.19k
                                tok::star, tok::arrow, tok::amp, tok::ampamp)) {
1156
2.78k
          CurrentToken->Previous->setType(TT_OverloadedOperator);
1157
2.78k
        }
1158
5.19k
      }
1159
3.31k
      if (CurrentToken && 
CurrentToken->is(tok::l_paren)3.31k
)
1160
3.24k
        CurrentToken->setType(TT_OverloadedOperatorLParen);
1161
3.31k
      if (CurrentToken && 
CurrentToken->Previous->is(TT_BinaryOperator)3.31k
)
1162
9
        CurrentToken->Previous->setType(TT_OverloadedOperator);
1163
3.31k
      break;
1164
2.39k
    case tok::question:
1165
2.39k
      if (Style.isJavaScript() && 
Tok->Next108
&&
1166
2.39k
          Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1167
108
                             tok::r_brace)) {
1168
        // Question marks before semicolons, colons, etc. indicate optional
1169
        // types (fields, parameters), e.g.
1170
        //   function(x?: string, y?) {...}
1171
        //   class X { y?; }
1172
48
        Tok->setType(TT_JsTypeOptionalQuestion);
1173
48
        break;
1174
48
      }
1175
      // Declarations cannot be conditional expressions, this can only be part
1176
      // of a type declaration.
1177
2.34k
      if (Line.MustBeDeclaration && 
!Contexts.back().IsExpression2.11k
&&
1178
2.34k
          
Style.isJavaScript()372
) {
1179
4
        break;
1180
4
      }
1181
2.33k
      if (Style.isCSharp()) {
1182
        // `Type?)`, `Type?>`, `Type? name;` and `Type? name =` can only be
1183
        // nullable types.
1184
        // Line.MustBeDeclaration will be true for `Type? name;`.
1185
24
        if ((!Contexts.back().IsExpression && 
Line.MustBeDeclaration22
) ||
1186
24
            
(4
Tok->Next4
&&
Tok->Next->isOneOf(tok::r_paren, tok::greater)4
) ||
1187
24
            
(4
Tok->Next4
&&
Tok->Next->is(tok::identifier)4
&&
Tok->Next->Next2
&&
1188
22
             
Tok->Next->Next->is(tok::equal)2
)) {
1189
22
          Tok->setType(TT_CSharpNullable);
1190
22
          break;
1191
22
        }
1192
24
      }
1193
2.31k
      parseConditional();
1194
2.31k
      break;
1195
5.05k
    case tok::kw_template:
1196
5.05k
      parseTemplateDeclaration();
1197
5.05k
      break;
1198
33.8k
    case tok::comma:
1199
33.8k
      switch (Contexts.back().ContextType) {
1200
1.53k
      case Context::CtorInitializer:
1201
1.53k
        Tok->setType(TT_CtorInitializerComma);
1202
1.53k
        break;
1203
266
      case Context::InheritanceList:
1204
266
        Tok->setType(TT_InheritanceComma);
1205
266
        break;
1206
32.0k
      default:
1207
32.0k
        if (Contexts.back().FirstStartOfName &&
1208
32.0k
            
(4.84k
Contexts.size() == 14.84k
||
startsWithInitStatement(Line)4.18k
)) {
1209
1.15k
          Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
1210
1.15k
          Line.IsMultiVariableDeclStmt = true;
1211
1.15k
        }
1212
32.0k
        break;
1213
33.8k
      }
1214
33.8k
      if (Contexts.back().ContextType == Context::ForEachMacro)
1215
240
        Contexts.back().IsExpression = true;
1216
33.8k
      break;
1217
255k
    case tok::identifier:
1218
255k
      if (Tok->isOneOf(Keywords.kw___has_include,
1219
255k
                       Keywords.kw___has_include_next)) {
1220
45
        parseHasInclude();
1221
45
      }
1222
255k
      if (Style.isCSharp() && 
Tok->is(Keywords.kw_where)2.43k
&&
Tok->Next12
&&
1223
255k
          
Tok->Next->isNot(tok::l_paren)12
) {
1224
12
        Tok->setType(TT_CSharpGenericTypeConstraint);
1225
12
        parseCSharpGenericTypeConstraint();
1226
12
      }
1227
255k
      break;
1228
2.95k
    case tok::arrow:
1229
2.95k
      if (Tok->isNot(TT_LambdaArrow) && 
Tok->Previous2.08k
&&
1230
2.95k
          
Tok->Previous->is(tok::kw_noexcept)2.08k
) {
1231
27
        Tok->setType(TT_TrailingReturnArrow);
1232
27
      }
1233
2.95k
      break;
1234
404k
    default:
1235
404k
      break;
1236
932k
    }
1237
932k
    return true;
1238
932k
  }
1239
1240
12
  void parseCSharpGenericTypeConstraint() {
1241
12
    int OpenAngleBracketsCount = 0;
1242
144
    while (CurrentToken) {
1243
132
      if (CurrentToken->is(tok::less)) {
1244
        // parseAngle is too greedy and will consume the whole line.
1245
14
        CurrentToken->setType(TT_TemplateOpener);
1246
14
        ++OpenAngleBracketsCount;
1247
14
        next();
1248
118
      } else if (CurrentToken->is(tok::greater)) {
1249
14
        CurrentToken->setType(TT_TemplateCloser);
1250
14
        --OpenAngleBracketsCount;
1251
14
        next();
1252
104
      } else if (CurrentToken->is(tok::comma) && 
OpenAngleBracketsCount == 016
) {
1253
        // We allow line breaks after GenericTypeConstraintComma's
1254
        // so do not flag commas in Generics as GenericTypeConstraintComma's.
1255
12
        CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1256
12
        next();
1257
92
      } else if (CurrentToken->is(Keywords.kw_where)) {
1258
0
        CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1259
0
        next();
1260
92
      } else if (CurrentToken->is(tok::colon)) {
1261
12
        CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1262
12
        next();
1263
80
      } else {
1264
80
        next();
1265
80
      }
1266
132
    }
1267
12
  }
1268
1269
4.06k
  void parseIncludeDirective() {
1270
4.06k
    if (CurrentToken && 
CurrentToken->is(tok::less)4.06k
) {
1271
685
      next();
1272
3.07k
      while (CurrentToken) {
1273
        // Mark tokens up to the trailing line comments as implicit string
1274
        // literals.
1275
2.39k
        if (CurrentToken->isNot(tok::comment) &&
1276
2.39k
            
!CurrentToken->TokenText.startswith("//")2.35k
) {
1277
2.35k
          CurrentToken->setType(TT_ImplicitStringLiteral);
1278
2.35k
        }
1279
2.39k
        next();
1280
2.39k
      }
1281
685
    }
1282
4.06k
  }
1283
1284
57
  void parseWarningOrError() {
1285
57
    next();
1286
    // We still want to format the whitespace left of the first token of the
1287
    // warning or error.
1288
57
    next();
1289
123
    while (CurrentToken) {
1290
66
      CurrentToken->setType(TT_ImplicitStringLiteral);
1291
66
      next();
1292
66
    }
1293
57
  }
1294
1295
65
  void parsePragma() {
1296
65
    next(); // Consume "pragma".
1297
65
    if (CurrentToken &&
1298
65
        CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1299
64
                              Keywords.kw_region)) {
1300
27
      bool IsMark = CurrentToken->is(Keywords.kw_mark);
1301
27
      next();
1302
27
      next(); // Consume first token (so we fix leading whitespace).
1303
156
      while (CurrentToken) {
1304
129
        if (IsMark || 
CurrentToken->Previous->is(TT_BinaryOperator)96
)
1305
57
          CurrentToken->setType(TT_ImplicitStringLiteral);
1306
129
        next();
1307
129
      }
1308
27
    }
1309
65
  }
1310
1311
45
  void parseHasInclude() {
1312
45
    if (!CurrentToken || !CurrentToken->is(tok::l_paren))
1313
0
      return;
1314
45
    next(); // '('
1315
45
    parseIncludeDirective();
1316
45
    next(); // ')'
1317
45
  }
1318
1319
13.4k
  LineType parsePreprocessorDirective() {
1320
13.4k
    bool IsFirstToken = CurrentToken->IsFirst;
1321
13.4k
    LineType Type = LT_PreprocessorDirective;
1322
13.4k
    next();
1323
13.4k
    if (!CurrentToken)
1324
24
      return Type;
1325
1326
13.4k
    if (Style.isJavaScript() && 
IsFirstToken4
) {
1327
      // JavaScript files can contain shebang lines of the form:
1328
      // #!/usr/bin/env node
1329
      // Treat these like C++ #include directives.
1330
36
      while (CurrentToken) {
1331
        // Tokens cannot be comments here.
1332
32
        CurrentToken->setType(TT_ImplicitStringLiteral);
1333
32
        next();
1334
32
      }
1335
4
      return LT_ImportStatement;
1336
4
    }
1337
1338
13.4k
    if (CurrentToken->is(tok::numeric_constant)) {
1339
12
      CurrentToken->SpacesRequiredBefore = 1;
1340
12
      return Type;
1341
12
    }
1342
    // Hashes in the middle of a line can lead to any strange token
1343
    // sequence.
1344
13.4k
    if (!CurrentToken->Tok.getIdentifierInfo())
1345
12
      return Type;
1346
    // In Verilog macro expansions start with a backtick just like preprocessor
1347
    // directives. Thus we stop if the word is not a preprocessor directive.
1348
13.3k
    if (Style.isVerilog() && 
!Keywords.isVerilogPPDirective(*CurrentToken)43
)
1349
15
      return LT_Invalid;
1350
13.3k
    switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1351
3.75k
    case tok::pp_include:
1352
3.76k
    case tok::pp_include_next:
1353
3.82k
    case tok::pp_import:
1354
3.82k
      next();
1355
3.82k
      parseIncludeDirective();
1356
3.82k
      Type = LT_ImportStatement;
1357
3.82k
      break;
1358
51
    case tok::pp_error:
1359
57
    case tok::pp_warning:
1360
57
      parseWarningOrError();
1361
57
      break;
1362
65
    case tok::pp_pragma:
1363
65
      parsePragma();
1364
65
      break;
1365
1.25k
    case tok::pp_if:
1366
1.36k
    case tok::pp_elif:
1367
1.36k
      Contexts.back().IsExpression = true;
1368
1.36k
      next();
1369
1.36k
      parseLine();
1370
1.36k
      break;
1371
8.06k
    default:
1372
8.06k
      break;
1373
13.3k
    }
1374
30.7k
    
while (13.3k
CurrentToken) {
1375
17.3k
      FormatToken *Tok = CurrentToken;
1376
17.3k
      next();
1377
17.3k
      if (Tok->is(tok::l_paren)) {
1378
1.32k
        parseParens();
1379
16.0k
      } else if (Tok->isOneOf(Keywords.kw___has_include,
1380
16.0k
                              Keywords.kw___has_include_next)) {
1381
0
        parseHasInclude();
1382
0
      }
1383
17.3k
    }
1384
13.3k
    return Type;
1385
13.3k
  }
1386
1387
public:
1388
256k
  LineType parseLine() {
1389
256k
    if (!CurrentToken)
1390
27
      return LT_Invalid;
1391
256k
    NonTemplateLess.clear();
1392
256k
    if (CurrentToken->is(tok::hash)) {
1393
      // We were not yet allowed to use C++17 optional when this was being
1394
      // written. So we used LT_Invalid to mark that the line is not a
1395
      // preprocessor directive.
1396
13.4k
      auto Type = parsePreprocessorDirective();
1397
13.4k
      if (Type != LT_Invalid)
1398
13.4k
        return Type;
1399
13.4k
    }
1400
1401
    // Directly allow to 'import <string-literal>' to support protocol buffer
1402
    // definitions (github.com/google/protobuf) or missing "#" (either way we
1403
    // should not break the line).
1404
242k
    IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1405
242k
    if ((Style.Language == FormatStyle::LK_Java &&
1406
242k
         
CurrentToken->is(Keywords.kw_package)1.02k
) ||
1407
242k
        
(242k
Info242k
&&
Info->getPPKeywordID() == tok::pp_import121k
&&
1408
242k
         
CurrentToken->Next528
&&
1409
242k
         CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1410
519
                                     tok::kw_static))) {
1411
194
      next();
1412
194
      parseIncludeDirective();
1413
194
      return LT_ImportStatement;
1414
194
    }
1415
1416
    // If this line starts and ends in '<' and '>', respectively, it is likely
1417
    // part of "#define <a/b.h>".
1418
242k
    if (CurrentToken->is(tok::less) && 
Line.Last->is(tok::greater)12
) {
1419
9
      parseIncludeDirective();
1420
9
      return LT_ImportStatement;
1421
9
    }
1422
1423
    // In .proto files, top-level options and package statements are very
1424
    // similar to import statements and should not be line-wrapped.
1425
242k
    if (Style.Language == FormatStyle::LK_Proto && 
Line.Level == 0422
&&
1426
242k
        
CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)374
) {
1427
105
      next();
1428
105
      if (CurrentToken && CurrentToken->is(tok::identifier)) {
1429
67
        while (CurrentToken)
1430
60
          next();
1431
7
        return LT_ImportStatement;
1432
7
      }
1433
105
    }
1434
1435
242k
    bool KeywordVirtualFound = false;
1436
242k
    bool ImportStatement = false;
1437
1438
    // import {...} from '...';
1439
242k
    if (Style.isJavaScript() && 
CurrentToken->is(Keywords.kw_import)8.61k
)
1440
280
      ImportStatement = true;
1441
1442
909k
    while (CurrentToken) {
1443
667k
      if (CurrentToken->is(tok::kw_virtual))
1444
248
        KeywordVirtualFound = true;
1445
667k
      if (Style.isJavaScript()) {
1446
        // export {...} from '...';
1447
        // An export followed by "from 'some string';" is a re-export from
1448
        // another module identified by a URI and is treated as a
1449
        // LT_ImportStatement (i.e. prevent wraps on it for long URIs).
1450
        // Just "export {...};" or "export class ..." should not be treated as
1451
        // an import in this sense.
1452
25.4k
        if (Line.First->is(tok::kw_export) &&
1453
25.4k
            
CurrentToken->is(Keywords.kw_from)1.19k
&&
CurrentToken->Next35
&&
1454
25.4k
            
CurrentToken->Next->isStringLiteral()35
) {
1455
35
          ImportStatement = true;
1456
35
        }
1457
25.4k
        if (isClosureImportStatement(*CurrentToken))
1458
24
          ImportStatement = true;
1459
25.4k
      }
1460
667k
      if (!consumeToken())
1461
66
        return LT_Invalid;
1462
667k
    }
1463
242k
    if (KeywordVirtualFound)
1464
248
      return LT_VirtualFunctionDecl;
1465
242k
    if (ImportStatement)
1466
339
      return LT_ImportStatement;
1467
1468
241k
    if (Line.startsWith(TT_ObjCMethodSpecifier)) {
1469
776
      if (Contexts.back().FirstObjCSelectorName) {
1470
321
        Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
1471
321
            Contexts.back().LongestObjCSelectorName;
1472
321
      }
1473
776
      return LT_ObjCMethodDecl;
1474
776
    }
1475
1476
241k
    for (const auto &ctx : Contexts)
1477
241k
      if (ctx.ContextType == Context::StructArrayInitializer)
1478
471
        return LT_ArrayOfStructInitializer;
1479
1480
240k
    return LT_Other;
1481
241k
  }
1482
1483
private:
1484
25.4k
  bool isClosureImportStatement(const FormatToken &Tok) {
1485
    // FIXME: Closure-library specific stuff should not be hard-coded but be
1486
    // configurable.
1487
25.4k
    return Tok.TokenText == "goog" && 
Tok.Next48
&&
Tok.Next->is(tok::period)48
&&
1488
25.4k
           
Tok.Next->Next48
&&
1489
25.4k
           
(48
Tok.Next->Next->TokenText == "module"48
||
1490
48
            
Tok.Next->Next->TokenText == "provide"40
||
1491
48
            
Tok.Next->Next->TokenText == "require"36
||
1492
48
            
Tok.Next->Next->TokenText == "requireType"28
||
1493
48
            
Tok.Next->Next->TokenText == "forwardDeclare"24
) &&
1494
25.4k
           
Tok.Next->Next->Next28
&&
Tok.Next->Next->Next->is(tok::l_paren)28
;
1495
25.4k
  }
1496
1497
1.37M
  void resetTokenMetadata() {
1498
1.37M
    if (!CurrentToken)
1499
254k
      return;
1500
1501
    // Reset token type in case we have already looked at it and then
1502
    // recovered from an error (e.g. failure to find the matching >).
1503
1.11M
    if (!CurrentToken->isTypeFinalized() &&
1504
1.11M
        !CurrentToken->isOneOf(
1505
1.07M
            TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
1506
1.07M
            TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
1507
1.07M
            TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
1508
1.07M
            TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
1509
1.07M
            TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
1510
1.07M
            TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
1511
1.07M
            TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
1512
1.07M
            TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
1513
1.07M
            TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
1514
1.07M
            TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
1515
1.07M
            TT_CompoundRequirementLBrace, TT_BracedListLBrace)) {
1516
1.07M
      CurrentToken->setType(TT_Unknown);
1517
1.07M
    }
1518
1.11M
    CurrentToken->Role.reset();
1519
1.11M
    CurrentToken->MatchingParen = nullptr;
1520
1.11M
    CurrentToken->FakeLParens.clear();
1521
1.11M
    CurrentToken->FakeRParens = 0;
1522
1.11M
  }
1523
1524
1.11M
  void next() {
1525
1.11M
    if (!CurrentToken)
1526
45
      return;
1527
1528
1.11M
    CurrentToken->NestingLevel = Contexts.size() - 1;
1529
1.11M
    CurrentToken->BindingStrength = Contexts.back().BindingStrength;
1530
1.11M
    modifyContext(*CurrentToken);
1531
1.11M
    determineTokenType(*CurrentToken);
1532
1.11M
    CurrentToken = CurrentToken->Next;
1533
1534
1.11M
    resetTokenMetadata();
1535
1.11M
  }
1536
1537
  /// A struct to hold information valid in a specific context, e.g.
1538
  /// a pair of parenthesis.
1539
  struct Context {
1540
    Context(tok::TokenKind ContextKind, unsigned BindingStrength,
1541
            bool IsExpression)
1542
        : ContextKind(ContextKind), BindingStrength(BindingStrength),
1543
383k
          IsExpression(IsExpression) {}
1544
1545
    tok::TokenKind ContextKind;
1546
    unsigned BindingStrength;
1547
    bool IsExpression;
1548
    unsigned LongestObjCSelectorName = 0;
1549
    bool ColonIsForRangeExpr = false;
1550
    bool ColonIsDictLiteral = false;
1551
    bool ColonIsObjCMethodExpr = false;
1552
    FormatToken *FirstObjCSelectorName = nullptr;
1553
    FormatToken *FirstStartOfName = nullptr;
1554
    bool CanBeExpression = true;
1555
    bool CaretFound = false;
1556
    bool InCpp11AttributeSpecifier = false;
1557
    bool InCSharpAttributeSpecifier = false;
1558
    enum {
1559
      Unknown,
1560
      // Like the part after `:` in a constructor.
1561
      //   Context(...) : IsExpression(IsExpression)
1562
      CtorInitializer,
1563
      // Like in the parentheses in a foreach.
1564
      ForEachMacro,
1565
      // Like the inheritance list in a class declaration.
1566
      //   class Input : public IO
1567
      InheritanceList,
1568
      // Like in the braced list.
1569
      //   int x[] = {};
1570
      StructArrayInitializer,
1571
      // Like in `static_cast<int>`.
1572
      TemplateArgument,
1573
    } ContextType = Unknown;
1574
  };
1575
1576
  /// Puts a new \c Context onto the stack \c Contexts for the lifetime
1577
  /// of each instance.
1578
  struct ScopedContextCreator {
1579
    AnnotatingParser &P;
1580
1581
    ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
1582
                         unsigned Increase)
1583
128k
        : P(P) {
1584
128k
      P.Contexts.push_back(Context(ContextKind,
1585
128k
                                   P.Contexts.back().BindingStrength + Increase,
1586
128k
                                   P.Contexts.back().IsExpression));
1587
128k
    }
1588
1589
128k
    ~ScopedContextCreator() {
1590
128k
      if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1591
2.41k
        if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
1592
1.71k
          P.Contexts.pop_back();
1593
1.71k
          P.Contexts.back().ContextType = Context::StructArrayInitializer;
1594
1.71k
          return;
1595
1.71k
        }
1596
2.41k
      }
1597
126k
      P.Contexts.pop_back();
1598
126k
    }
1599
  };
1600
1601
1.11M
  void modifyContext(const FormatToken &Current) {
1602
1.11M
    auto AssignmentStartsExpression = [&]() {
1603
1.11M
      if (Current.getPrecedence() != prec::Assignment)
1604
1.09M
        return false;
1605
1606
23.5k
      if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
1607
717
        return false;
1608
22.8k
      if (Line.First->is(tok::kw_template)) {
1609
1.56k
        assert(Current.Previous);
1610
1.56k
        if (Current.Previous->is(tok::kw_operator)) {
1611
          // `template ... operator=` cannot be an expression.
1612
217
          return false;
1613
217
        }
1614
1615
        // `template` keyword can start a variable template.
1616
1.34k
        const FormatToken *Tok = Line.First->getNextNonComment();
1617
1.34k
        assert(Tok); // Current token is on the same line.
1618
1.34k
        if (Tok->isNot(TT_TemplateOpener)) {
1619
          // Explicit template instantiations do not have `<>`.
1620
0
          return false;
1621
0
        }
1622
1623
1.34k
        Tok = Tok->MatchingParen;
1624
1.34k
        if (!Tok)
1625
117
          return false;
1626
1.23k
        Tok = Tok->getNextNonComment();
1627
1.23k
        if (!Tok)
1628
0
          return false;
1629
1630
1.23k
        if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_concept,
1631
1.23k
                         tok::kw_struct, tok::kw_using)) {
1632
1.11k
          return false;
1633
1.11k
        }
1634
1635
112
        return true;
1636
1.23k
      }
1637
1638
      // Type aliases use `type X = ...;` in TypeScript and can be exported
1639
      // using `export type ...`.
1640
21.2k
      if (Style.isJavaScript() &&
1641
21.2k
          
(1.82k
Line.startsWith(Keywords.kw_type, tok::identifier)1.82k
||
1642
1.82k
           Line.startsWith(tok::kw_export, Keywords.kw_type,
1643
1.78k
                           tok::identifier))) {
1644
52
        return false;
1645
52
      }
1646
1647
21.2k
      return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
1648
21.2k
    };
1649
1650
1.11M
    if (AssignmentStartsExpression()) {
1651
21.1k
      Contexts.back().IsExpression = true;
1652
21.1k
      if (!Line.startsWith(TT_UnaryOperator)) {
1653
21.0k
        for (FormatToken *Previous = Current.Previous;
1654
44.4k
             Previous && Previous->Previous &&
1655
44.4k
             
!Previous->Previous->isOneOf(tok::comma, tok::semi)29.7k
;
1656
28.1k
             
Previous = Previous->Previous23.4k
) {
1657
28.1k
          if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
1658
2.40k
            Previous = Previous->MatchingParen;
1659
2.40k
            if (!Previous)
1660
9
              break;
1661
2.40k
          }
1662
28.1k
          if (Previous->opensScope())
1663
4.72k
            break;
1664
23.4k
          if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
1665
23.4k
              
Previous->isOneOf(tok::star, tok::amp, tok::ampamp)156
&&
1666
23.4k
              
Previous->Previous126
&&
Previous->Previous->isNot(tok::equal)126
) {
1667
126
            Previous->setType(TT_PointerOrReference);
1668
126
          }
1669
23.4k
        }
1670
21.0k
      }
1671
1.09M
    } else if (Current.is(tok::lessless) &&
1672
1.09M
               
(1.29k
!Current.Previous1.29k
||
!Current.Previous->is(tok::kw_operator)1.29k
)) {
1673
1.26k
      Contexts.back().IsExpression = true;
1674
1.09M
    } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
1675
7.80k
      Contexts.back().IsExpression = true;
1676
1.08M
    } else if (Current.is(TT_TrailingReturnArrow)) {
1677
0
      Contexts.back().IsExpression = false;
1678
1.08M
    } else if (Current.is(TT_LambdaArrow) || 
Current.is(Keywords.kw_assert)1.08M
) {
1679
866
      Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
1680
1.08M
    } else if (Current.Previous &&
1681
1.08M
               
Current.Previous->is(TT_CtorInitializerColon)841k
) {
1682
1.71k
      Contexts.back().IsExpression = true;
1683
1.71k
      Contexts.back().ContextType = Context::CtorInitializer;
1684
1.08M
    } else if (Current.Previous && 
Current.Previous->is(TT_InheritanceColon)839k
) {
1685
1.10k
      Contexts.back().ContextType = Context::InheritanceList;
1686
1.08M
    } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
1687
137k
      for (FormatToken *Previous = Current.Previous;
1688
140k
           Previous && 
Previous->isOneOf(tok::star, tok::amp)140k
;
1689
137k
           
Previous = Previous->Previous3.22k
) {
1690
3.22k
        Previous->setType(TT_PointerOrReference);
1691
3.22k
      }
1692
137k
      if (Line.MustBeDeclaration &&
1693
137k
          
Contexts.front().ContextType != Context::CtorInitializer111k
) {
1694
106k
        Contexts.back().IsExpression = false;
1695
106k
      }
1696
948k
    } else if (Current.is(tok::kw_new)) {
1697
814
      Contexts.back().CanBeExpression = false;
1698
947k
    } else if (Current.is(tok::semi) ||
1699
947k
               
(860k
Current.is(tok::exclaim)860k
&&
Current.Previous853
&&
1700
860k
                
!Current.Previous->is(tok::kw_operator)839
)) {
1701
      // This should be the condition or increment in a for-loop.
1702
      // But not operator !() (can't use TT_OverloadedOperator here as its not
1703
      // been annotated yet).
1704
87.7k
      Contexts.back().IsExpression = true;
1705
87.7k
    }
1706
1.11M
  }
1707
1708
54
  static FormatToken *untilMatchingParen(FormatToken *Current) {
1709
    // Used when `MatchingParen` is not yet established.
1710
54
    int ParenLevel = 0;
1711
396
    while (Current) {
1712
396
      if (Current->is(tok::l_paren))
1713
81
        ++ParenLevel;
1714
396
      if (Current->is(tok::r_paren))
1715
81
        --ParenLevel;
1716
396
      if (ParenLevel < 1)
1717
54
        break;
1718
342
      Current = Current->Next;
1719
342
    }
1720
54
    return Current;
1721
54
  }
1722
1723
878k
  static bool isDeductionGuide(FormatToken &Current) {
1724
    // Look for a deduction guide template<T> A(...) -> A<...>;
1725
878k
    if (Current.Previous && 
Current.Previous->is(tok::r_paren)632k
&&
1726
878k
        
Current.startsSequence(tok::arrow, tok::identifier, tok::less)24.4k
) {
1727
      // Find the TemplateCloser.
1728
283
      FormatToken *TemplateCloser = Current.Next->Next;
1729
283
      int NestingLevel = 0;
1730
1.01k
      while (TemplateCloser) {
1731
        // Skip over an expressions in parens  A<(3 < 2)>;
1732
1.01k
        if (TemplateCloser->is(tok::l_paren)) {
1733
          // No Matching Paren yet so skip to matching paren
1734
54
          TemplateCloser = untilMatchingParen(TemplateCloser);
1735
54
          if (!TemplateCloser)
1736
0
            break;
1737
54
        }
1738
1.01k
        if (TemplateCloser->is(tok::less))
1739
292
          ++NestingLevel;
1740
1.01k
        if (TemplateCloser->is(tok::greater))
1741
292
          --NestingLevel;
1742
1.01k
        if (NestingLevel < 1)
1743
283
          break;
1744
734
        TemplateCloser = TemplateCloser->Next;
1745
734
      }
1746
      // Assuming we have found the end of the template ensure its followed
1747
      // with a semi-colon.
1748
283
      if (TemplateCloser && TemplateCloser->Next &&
1749
283
          TemplateCloser->Next->is(tok::semi) &&
1750
283
          
Current.Previous->MatchingParen265
) {
1751
        // Determine if the identifier `A` prior to the A<..>; is the same as
1752
        // prior to the A(..)
1753
265
        FormatToken *LeadingIdentifier =
1754
265
            Current.Previous->MatchingParen->Previous;
1755
1756
        // Differentiate a deduction guide by seeing the
1757
        // > of the template prior to the leading identifier.
1758
265
        if (LeadingIdentifier) {
1759
265
          FormatToken *PriorLeadingIdentifier = LeadingIdentifier->Previous;
1760
          // Skip back past explicit decoration
1761
265
          if (PriorLeadingIdentifier &&
1762
265
              
PriorLeadingIdentifier->is(tok::kw_explicit)247
) {
1763
18
            PriorLeadingIdentifier = PriorLeadingIdentifier->Previous;
1764
18
          }
1765
1766
265
          return PriorLeadingIdentifier &&
1767
265
                 
(247
PriorLeadingIdentifier->is(TT_TemplateCloser)247
||
1768
247
                  
PriorLeadingIdentifier->ClosesRequiresClause10
) &&
1769
265
                 
LeadingIdentifier->TokenText == Current.Next->TokenText247
;
1770
265
        }
1771
265
      }
1772
283
    }
1773
878k
    return false;
1774
878k
  }
1775
1776
1.11M
  void determineTokenType(FormatToken &Current) {
1777
1.11M
    if (!Current.is(TT_Unknown)) {
1778
      // The token type is already known.
1779
84.2k
      return;
1780
84.2k
    }
1781
1782
1.03M
    if ((Style.isJavaScript() || 
Style.isCSharp()999k
) &&
1783
1.03M
        
Current.is(tok::exclaim)43.9k
) {
1784
356
      if (Current.Previous) {
1785
352
        bool IsIdentifier =
1786
352
            Style.isJavaScript()
1787
352
                ? Keywords.IsJavaScriptIdentifier(
1788
284
                      *Current.Previous, /* AcceptIdentifierName= */ true)
1789
352
                : 
Current.Previous->is(tok::identifier)68
;
1790
352
        if (IsIdentifier ||
1791
352
            Current.Previous->isOneOf(
1792
106
                tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
1793
106
                tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
1794
106
                Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
1795
352
            
Current.Previous->Tok.isLiteral()56
) {
1796
300
          Current.setType(TT_NonNullAssertion);
1797
300
          return;
1798
300
        }
1799
352
      }
1800
56
      if (Current.Next &&
1801
56
          Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
1802
0
        Current.setType(TT_NonNullAssertion);
1803
0
        return;
1804
0
      }
1805
56
    }
1806
1807
    // Line.MightBeFunctionDecl can only be true after the parentheses of a
1808
    // function declaration have been found. In this case, 'Current' is a
1809
    // trailing token of this declaration and thus cannot be a name.
1810
1.03M
    if (Current.is(Keywords.kw_instanceof)) {
1811
22
      Current.setType(TT_BinaryOperator);
1812
1.03M
    } else if (isStartOfName(Current) &&
1813
1.03M
               
(64.8k
!Line.MightBeFunctionDecl64.8k
||
Current.NestingLevel != 01.03k
)) {
1814
64.2k
      Contexts.back().FirstStartOfName = &Current;
1815
64.2k
      Current.setType(TT_StartOfName);
1816
970k
    } else if (Current.is(tok::semi)) {
1817
      // Reset FirstStartOfName after finding a semicolon so that a for loop
1818
      // with multiple increment statements is not confused with a for loop
1819
      // having multiple variable declarations.
1820
86.9k
      Contexts.back().FirstStartOfName = nullptr;
1821
883k
    } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
1822
3.90k
      AutoFound = true;
1823
879k
    } else if (Current.is(tok::arrow) &&
1824
879k
               
Style.Language == FormatStyle::LK_Java2.09k
) {
1825
10
      Current.setType(TT_LambdaArrow);
1826
879k
    } else if (Current.is(tok::arrow) && 
AutoFound2.08k
&&
Line.MustBeDeclaration622
&&
1827
879k
               
Current.NestingLevel == 0613
&&
1828
879k
               
!Current.Previous->isOneOf(tok::kw_operator, tok::identifier)604
) {
1829
      // not auto operator->() -> xxx;
1830
577
      Current.setType(TT_TrailingReturnArrow);
1831
879k
    } else if (Current.is(tok::arrow) && 
Current.Previous1.51k
&&
1832
879k
               
Current.Previous->is(tok::r_brace)1.51k
) {
1833
      // Concept implicit conversion constraint needs to be treated like
1834
      // a trailing return type  ... } -> <type>.
1835
109
      Current.setType(TT_TrailingReturnArrow);
1836
878k
    } else if (isDeductionGuide(Current)) {
1837
      // Deduction guides trailing arrow " A(...) -> A<T>;".
1838
247
      Current.setType(TT_TrailingReturnArrow);
1839
878k
    } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
1840
24.3k
      Current.setType(determineStarAmpUsage(
1841
24.3k
          Current,
1842
24.3k
          Contexts.back().CanBeExpression && 
Contexts.back().IsExpression24.2k
,
1843
24.3k
          Contexts.back().ContextType == Context::TemplateArgument));
1844
854k
    } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
1845
5.75k
      Current.setType(determinePlusMinusCaretUsage(Current));
1846
5.75k
      if (Current.is(TT_UnaryOperator) && 
Current.is(tok::caret)2.36k
)
1847
682
        Contexts.back().CaretFound = true;
1848
848k
    } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
1849
1.69k
      Current.setType(determineIncrementUsage(Current));
1850
846k
    } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
1851
873
      Current.setType(TT_UnaryOperator);
1852
846k
    } else if (Current.is(tok::question)) {
1853
2.40k
      if (Style.isJavaScript() && 
Line.MustBeDeclaration108
&&
1854
2.40k
          
!Contexts.back().IsExpression24
) {
1855
        // In JavaScript, `interface X { foo?(): bar; }` is an optional method
1856
        // on the interface, not a ternary expression.
1857
16
        Current.setType(TT_JsTypeOptionalQuestion);
1858
2.38k
      } else {
1859
2.38k
        Current.setType(TT_ConditionalExpr);
1860
2.38k
      }
1861
843k
    } else if (Current.isBinaryOperator() &&
1862
843k
               
(43.6k
!Current.Previous43.6k
||
Current.Previous->isNot(tok::l_square)43.6k
) &&
1863
843k
               
(43.5k
!Current.is(tok::greater)43.5k
&&
1864
43.5k
                
Style.Language != FormatStyle::LK_TextProto42.4k
)) {
1865
42.2k
      Current.setType(TT_BinaryOperator);
1866
801k
    } else if (Current.is(tok::comment)) {
1867
16.1k
      if (Current.TokenText.startswith("/*")) {
1868
3.14k
        if (Current.TokenText.endswith("*/")) {
1869
3.13k
          Current.setType(TT_BlockComment);
1870
3.13k
        } else {
1871
          // The lexer has for some reason determined a comment here. But we
1872
          // cannot really handle it, if it isn't properly terminated.
1873
3
          Current.Tok.setKind(tok::unknown);
1874
3
        }
1875
13.0k
      } else {
1876
13.0k
        Current.setType(TT_LineComment);
1877
13.0k
      }
1878
785k
    } else if (Current.is(tok::l_paren)) {
1879
79.8k
      if (lParenStartsCppCast(Current))
1880
255
        Current.setType(TT_CppCastLParen);
1881
705k
    } else if (Current.is(tok::r_paren)) {
1882
81.7k
      if (rParenEndsCast(Current))
1883
2.84k
        Current.setType(TT_CastRParen);
1884
81.7k
      if (Current.MatchingParen && 
Current.Next81.5k
&&
1885
81.7k
          
!Current.Next->isBinaryOperator()70.4k
&&
1886
81.7k
          !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
1887
66.0k
                                 tok::comma, tok::period, tok::arrow,
1888
66.0k
                                 tok::coloncolon)) {
1889
11.5k
        if (FormatToken *AfterParen = Current.MatchingParen->Next) {
1890
          // Make sure this isn't the return type of an Obj-C block declaration
1891
11.5k
          if (AfterParen->isNot(tok::caret)) {
1892
11.3k
            if (FormatToken *BeforeParen = Current.MatchingParen->Previous) {
1893
11.0k
              if (BeforeParen->is(tok::identifier) &&
1894
11.0k
                  
!BeforeParen->is(TT_TypenameMacro)4.67k
&&
1895
11.0k
                  
BeforeParen->TokenText == BeforeParen->TokenText.upper()4.67k
&&
1896
11.0k
                  
(407
!BeforeParen->Previous407
||
1897
407
                   
BeforeParen->Previous->ClosesTemplateDeclaration317
)) {
1898
117
                Current.setType(TT_FunctionAnnotationRParen);
1899
117
              }
1900
11.0k
            }
1901
11.3k
          }
1902
11.5k
        }
1903
11.5k
      }
1904
623k
    } else if (Current.is(tok::at) && 
Current.Next2.34k
&&
!Style.isJavaScript()2.34k
&&
1905
623k
               
Style.Language != FormatStyle::LK_Java2.28k
) {
1906
      // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
1907
      // marks declarations and properties that need special formatting.
1908
2.19k
      switch (Current.Next->Tok.getObjCKeywordID()) {
1909
308
      case tok::objc_interface:
1910
431
      case tok::objc_implementation:
1911
506
      case tok::objc_protocol:
1912
506
        Current.setType(TT_ObjCDecl);
1913
506
        break;
1914
124
      case tok::objc_property:
1915
124
        Current.setType(TT_ObjCProperty);
1916
124
        break;
1917
1.56k
      default:
1918
1.56k
        break;
1919
2.19k
      }
1920
621k
    } else if (Current.is(tok::period)) {
1921
5.00k
      FormatToken *PreviousNoComment = Current.getPreviousNonComment();
1922
5.00k
      if (PreviousNoComment &&
1923
5.00k
          PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
1924
279
        Current.setType(TT_DesignatedInitializerPeriod);
1925
4.73k
      } else if (Style.Language == FormatStyle::LK_Java && 
Current.Previous92
&&
1926
4.73k
                 Current.Previous->isOneOf(TT_JavaAnnotation,
1927
92
                                           TT_LeadingJavaAnnotation)) {
1928
28
        Current.setType(Current.Previous->getType());
1929
28
      }
1930
616k
    } else if (canBeObjCSelectorComponent(Current) &&
1931
               // FIXME(bug 36976): ObjC return types shouldn't use
1932
               // TT_CastRParen.
1933
616k
               
Current.Previous347k
&&
Current.Previous->is(TT_CastRParen)231k
&&
1934
616k
               
Current.Previous->MatchingParen1.80k
&&
1935
616k
               
Current.Previous->MatchingParen->Previous1.80k
&&
1936
616k
               Current.Previous->MatchingParen->Previous->is(
1937
1.79k
                   TT_ObjCMethodSpecifier)) {
1938
      // This is the first part of an Objective-C selector name. (If there's no
1939
      // colon after this, this is the only place which annotates the identifier
1940
      // as a selector.)
1941
671
      Current.setType(TT_SelectorName);
1942
615k
    } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
1943
615k
                               tok::kw_requires) &&
1944
615k
               
Current.Previous216k
&&
1945
615k
               
!Current.Previous->isOneOf(tok::equal, tok::at)177k
&&
1946
615k
               
Line.MightBeFunctionDecl168k
&&
Contexts.size() == 116.3k
) {
1947
      // Line.MightBeFunctionDecl can only be true after the parentheses of a
1948
      // function declaration have been found.
1949
8.81k
      Current.setType(TT_TrailingAnnotation);
1950
606k
    } else if ((Style.Language == FormatStyle::LK_Java ||
1951
606k
                
Style.isJavaScript()604k
) &&
1952
606k
               
Current.Previous24.1k
) {
1953
14.7k
      if (Current.Previous->is(tok::at) &&
1954
14.7k
          
Current.isNot(Keywords.kw_interface)150
) {
1955
142
        const FormatToken &AtToken = *Current.Previous;
1956
142
        const FormatToken *Previous = AtToken.getPreviousNonComment();
1957
142
        if (!Previous || 
Previous->is(TT_LeadingJavaAnnotation)58
)
1958
106
          Current.setType(TT_LeadingJavaAnnotation);
1959
36
        else
1960
36
          Current.setType(TT_JavaAnnotation);
1961
14.6k
      } else if (Current.Previous->is(tok::period) &&
1962
14.6k
                 Current.Previous->isOneOf(TT_JavaAnnotation,
1963
679
                                           TT_LeadingJavaAnnotation)) {
1964
28
        Current.setType(Current.Previous->getType());
1965
28
      }
1966
14.7k
    }
1967
1.03M
  }
1968
1969
  /// Take a guess at whether \p Tok starts a name of a function or
1970
  /// variable declaration.
1971
  ///
1972
  /// This is a heuristic based on whether \p Tok is an identifier following
1973
  /// something that is likely a type.
1974
1.03M
  bool isStartOfName(const FormatToken &Tok) {
1975
1.03M
    if (Tok.isNot(tok::identifier) || 
!Tok.Previous270k
)
1976
801k
      return false;
1977
1978
233k
    if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
1979
233k
                              Keywords.kw_as)) {
1980
147
      return false;
1981
147
    }
1982
233k
    if (Style.isJavaScript() && 
Tok.Previous->is(Keywords.kw_in)8.10k
)
1983
32
      return false;
1984
1985
    // Skip "const" as it does not have an influence on whether this is a name.
1986
233k
    FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
1987
1988
    // For javascript const can be like "let" or "var"
1989
233k
    if (!Style.isJavaScript())
1990
229k
      
while (225k
PreviousNotConst &&
PreviousNotConst->is(tok::kw_const)228k
)
1991
4.01k
        PreviousNotConst = PreviousNotConst->getPreviousNonComment();
1992
1993
233k
    if (!PreviousNotConst)
1994
699
      return false;
1995
1996
232k
    if (PreviousNotConst->ClosesRequiresClause)
1997
140
      return false;
1998
1999
232k
    bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2000
232k
                       
PreviousNotConst->Previous18.6k
&&
2001
232k
                       
PreviousNotConst->Previous->is(tok::hash)13.9k
;
2002
2003
232k
    if (PreviousNotConst->is(TT_TemplateCloser)) {
2004
3.09k
      return PreviousNotConst && PreviousNotConst->MatchingParen &&
2005
3.09k
             PreviousNotConst->MatchingParen->Previous &&
2006
3.09k
             PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2007
3.09k
             
PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template)3.08k
;
2008
3.09k
    }
2009
2010
229k
    if (PreviousNotConst->is(tok::r_paren) &&
2011
229k
        
PreviousNotConst->is(TT_TypeDeclarationParen)3.23k
) {
2012
234
      return true;
2013
234
    }
2014
2015
    // If is a preprocess keyword like #define.
2016
229k
    if (IsPPKeyword)
2017
4.54k
      return false;
2018
2019
    // int a or auto a.
2020
224k
    if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto))
2021
16.9k
      return true;
2022
2023
    // *a or &a or &&a.
2024
207k
    if (PreviousNotConst->is(TT_PointerOrReference))
2025
9.93k
      return true;
2026
2027
    // MyClass a;
2028
197k
    if (PreviousNotConst->isSimpleTypeSpecifier())
2029
34.8k
      return true;
2030
2031
    // const a = in JavaScript.
2032
162k
    return Style.isJavaScript() && 
PreviousNotConst->is(tok::kw_const)6.01k
;
2033
197k
  }
2034
2035
  /// Determine whether '(' is starting a C++ cast.
2036
79.8k
  bool lParenStartsCppCast(const FormatToken &Tok) {
2037
    // C-style casts are only used in C++.
2038
79.8k
    if (!Style.isCpp())
2039
3.34k
      return false;
2040
2041
76.5k
    FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2042
76.5k
    if (LeftOfParens && 
LeftOfParens->is(TT_TemplateCloser)75.7k
&&
2043
76.5k
        
LeftOfParens->MatchingParen1.63k
) {
2044
1.63k
      auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2045
1.63k
      if (Prev &&
2046
1.63k
          Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2047
1.63k
                        tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2048
        // FIXME: Maybe we should handle identifiers ending with "_cast",
2049
        // e.g. any_cast?
2050
255
        return true;
2051
255
      }
2052
1.63k
    }
2053
76.2k
    return false;
2054
76.5k
  }
2055
2056
  /// Determine whether ')' is ending a cast.
2057
81.7k
  bool rParenEndsCast(const FormatToken &Tok) {
2058
    // C-style casts are only used in C++, C# and Java.
2059
81.7k
    if (!Style.isCSharp() && 
!Style.isCpp()81.0k
&&
2060
81.7k
        
Style.Language != FormatStyle::LK_Java2.63k
) {
2061
2.33k
      return false;
2062
2.33k
    }
2063
2064
    // Empty parens aren't casts and there are no casts at the end of the line.
2065
79.3k
    if (Tok.Previous == Tok.MatchingParen || 
!Tok.Next47.1k
||
!Tok.MatchingParen37.9k
)
2066
41.4k
      return false;
2067
2068
37.8k
    FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
2069
37.8k
    if (LeftOfParens) {
2070
      // If there is a closing parenthesis left of the current
2071
      // parentheses, look past it as these might be chained casts.
2072
37.5k
      if (LeftOfParens->is(tok::r_paren) &&
2073
37.5k
          
LeftOfParens->isNot(TT_CastRParen)1.15k
) {
2074
780
        if (!LeftOfParens->MatchingParen ||
2075
780
            !LeftOfParens->MatchingParen->Previous) {
2076
27
          return false;
2077
27
        }
2078
753
        LeftOfParens = LeftOfParens->MatchingParen->Previous;
2079
753
      }
2080
2081
37.4k
      if (LeftOfParens->is(tok::r_square)) {
2082
        //   delete[] (void *)ptr;
2083
830
        auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2084
830
          if (Tok->isNot(tok::r_square))
2085
0
            return nullptr;
2086
2087
830
          Tok = Tok->getPreviousNonComment();
2088
830
          if (!Tok || Tok->isNot(tok::l_square))
2089
219
            return nullptr;
2090
2091
611
          Tok = Tok->getPreviousNonComment();
2092
611
          if (!Tok || 
Tok->isNot(tok::kw_delete)447
)
2093
571
            return nullptr;
2094
40
          return Tok;
2095
611
        };
2096
830
        if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2097
40
          LeftOfParens = MaybeDelete;
2098
830
      }
2099
2100
      // The Condition directly below this one will see the operator arguments
2101
      // as a (void *foo) cast.
2102
      //   void operator delete(void *foo) ATTRIB;
2103
37.4k
      if (LeftOfParens->Tok.getIdentifierInfo() && 
LeftOfParens->Previous28.8k
&&
2104
37.4k
          
LeftOfParens->Previous->is(tok::kw_operator)18.1k
) {
2105
315
        return false;
2106
315
      }
2107
2108
      // If there is an identifier (or with a few exceptions a keyword) right
2109
      // before the parentheses, this is unlikely to be a cast.
2110
37.1k
      if (LeftOfParens->Tok.getIdentifierInfo() &&
2111
37.1k
          !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2112
28.5k
                                 tok::kw_delete)) {
2113
28.4k
        return false;
2114
28.4k
      }
2115
2116
      // Certain other tokens right before the parentheses are also signals that
2117
      // this cannot be a cast.
2118
8.76k
      if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2119
8.76k
                                TT_TemplateCloser, tok::ellipsis)) {
2120
2.67k
        return false;
2121
2.67k
      }
2122
8.76k
    }
2123
2124
6.46k
    if (Tok.Next->is(tok::question))
2125
55
      return false;
2126
2127
    // `foreach((A a, B b) in someList)` should not be seen as a cast.
2128
6.41k
    if (Tok.Next->is(Keywords.kw_in) && 
Style.isCSharp()2
)
2129
2
      return false;
2130
2131
    // Functions which end with decorations like volatile, noexcept are unlikely
2132
    // to be casts.
2133
6.40k
    if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2134
6.40k
                          tok::kw_requires, tok::kw_throw, tok::arrow,
2135
6.40k
                          Keywords.kw_override, Keywords.kw_final) ||
2136
6.40k
        
isCpp11AttributeSpecifier(*Tok.Next)6.34k
) {
2137
69
      return false;
2138
69
    }
2139
2140
    // As Java has no function types, a "(" after the ")" likely means that this
2141
    // is a cast.
2142
6.34k
    if (Style.Language == FormatStyle::LK_Java && 
Tok.Next->is(tok::l_paren)12
)
2143
2
      return true;
2144
2145
    // If a (non-string) literal follows, this is likely a cast.
2146
6.33k
    if (Tok.Next->isNot(tok::string_literal) &&
2147
6.33k
        (Tok.Next->Tok.isLiteral() ||
2148
6.33k
         
Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)6.21k
)) {
2149
153
      return true;
2150
153
    }
2151
2152
    // Heuristically try to determine whether the parentheses contain a type.
2153
6.18k
    auto IsQualifiedPointerOrReference = [](FormatToken *T) {
2154
      // This is used to handle cases such as x = (foo *const)&y;
2155
5.34k
      assert(!T->isSimpleTypeSpecifier() && "Should have already been checked");
2156
      // Strip trailing qualifiers such as const or volatile when checking
2157
      // whether the parens could be a cast to a pointer/reference type.
2158
6.09k
      while (T) {
2159
6.09k
        if (T->is(TT_AttributeParen)) {
2160
          // Handle `x = (foo *__attribute__((foo)))&v;`:
2161
63
          if (T->MatchingParen && T->MatchingParen->Previous &&
2162
63
              T->MatchingParen->Previous->is(tok::kw___attribute)) {
2163
63
            T = T->MatchingParen->Previous->Previous;
2164
63
            continue;
2165
63
          }
2166
6.03k
        } else if (T->is(TT_AttributeSquare)) {
2167
          // Handle `x = (foo *[[clang::foo]])&v;`:
2168
72
          if (T->MatchingParen && T->MatchingParen->Previous) {
2169
72
            T = T->MatchingParen->Previous;
2170
72
            continue;
2171
72
          }
2172
5.96k
        } else if (T->canBePointerOrReferenceQualifier()) {
2173
620
          T = T->Previous;
2174
620
          continue;
2175
620
        }
2176
5.34k
        break;
2177
6.09k
      }
2178
5.34k
      return T && T->is(TT_PointerOrReference);
2179
5.34k
    };
2180
6.18k
    bool ParensAreType =
2181
6.18k
        !Tok.Previous ||
2182
6.18k
        Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2183
6.18k
        
Tok.Previous->isSimpleTypeSpecifier()6.05k
||
2184
6.18k
        
IsQualifiedPointerOrReference(Tok.Previous)5.34k
;
2185
6.18k
    bool ParensCouldEndDecl =
2186
6.18k
        Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2187
6.18k
    if (ParensAreType && 
!ParensCouldEndDecl1.70k
)
2188
1.64k
      return true;
2189
2190
    // At this point, we heuristically assume that there are no casts at the
2191
    // start of the line. We assume that we have found most cases where there
2192
    // are by the logic above, e.g. "(void)x;".
2193
4.54k
    if (!LeftOfParens)
2194
244
      return false;
2195
2196
    // Certain token types inside the parentheses mean that this can't be a
2197
    // cast.
2198
13.8k
    
for (const FormatToken *Token = Tok.MatchingParen->Next; 4.29k
Token != &Tok;
2199
10.7k
         
Token = Token->Next9.54k
) {
2200
10.7k
      if (Token->is(TT_BinaryOperator))
2201
1.22k
        return false;
2202
10.7k
    }
2203
2204
    // If the following token is an identifier or 'this', this is a cast. All
2205
    // cases where this can be something else are handled above.
2206
3.07k
    if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
2207
901
      return true;
2208
2209
    // Look for a cast `( x ) (`.
2210
2.17k
    if (Tok.Next->is(tok::l_paren) && 
Tok.Previous172
&&
Tok.Previous->Previous172
) {
2211
172
      if (Tok.Previous->is(tok::identifier) &&
2212
172
          
Tok.Previous->Previous->is(tok::l_paren)136
) {
2213
64
        return true;
2214
64
      }
2215
172
    }
2216
2217
2.11k
    if (!Tok.Next->Next)
2218
425
      return false;
2219
2220
    // If the next token after the parenthesis is a unary operator, assume
2221
    // that this is cast, unless there are unexpected tokens inside the
2222
    // parenthesis.
2223
1.68k
    bool NextIsUnary =
2224
1.68k
        Tok.Next->isUnaryOperator() || 
Tok.Next->isOneOf(tok::amp, tok::star)1.62k
;
2225
1.68k
    if (!NextIsUnary || 
Tok.Next->is(tok::plus)99
||
2226
1.68k
        
!Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant)90
) {
2227
1.60k
      return false;
2228
1.60k
    }
2229
    // Search for unexpected tokens.
2230
189
    
for (FormatToken *Prev = Tok.Previous; 81
Prev != Tok.MatchingParen;
2231
108
         Prev = Prev->Previous) {
2232
108
      if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2233
0
        return false;
2234
108
    }
2235
81
    return true;
2236
81
  }
2237
2238
  /// Returns true if the token is used as a unary operator.
2239
27.1k
  bool determineUnaryOperatorByUsage(const FormatToken &Tok) {
2240
27.1k
    const FormatToken *PrevToken = Tok.getPreviousNonComment();
2241
27.1k
    if (!PrevToken)
2242
805
      return true;
2243
2244
    // These keywords are deliberately not included here because they may
2245
    // precede only one of unary star/amp and plus/minus but not both.  They are
2246
    // either included in determineStarAmpUsage or determinePlusMinusCaretUsage.
2247
    //
2248
    // @ - It may be followed by a unary `-` in Objective-C literals. We don't
2249
    //   know how they can be followed by a star or amp.
2250
26.3k
    if (PrevToken->isOneOf(
2251
26.3k
            TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2252
26.3k
            tok::equal, tok::question, tok::l_square, tok::l_brace,
2253
26.3k
            tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2254
26.3k
            tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2255
3.91k
      return true;
2256
3.91k
    }
2257
2258
    // We put sizeof here instead of only in determineStarAmpUsage. In the cases
2259
    // where the unary `+` operator is overloaded, it is reasonable to write
2260
    // things like `sizeof +x`. Like commit 446d6ec996c6c3.
2261
22.4k
    if (PrevToken->is(tok::kw_sizeof))
2262
56
      return true;
2263
2264
    // A sequence of leading unary operators.
2265
22.3k
    if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2266
547
      return true;
2267
2268
    // There can't be two consecutive binary operators.
2269
21.7k
    if (PrevToken->is(TT_BinaryOperator))
2270
176
      return true;
2271
2272
21.6k
    return false;
2273
21.7k
  }
2274
2275
  /// Return the type of the given token assuming it is * or &.
2276
  TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
2277
24.3k
                                  bool InTemplateArgument) {
2278
24.3k
    if (Style.isJavaScript())
2279
93
      return TT_BinaryOperator;
2280
2281
    // && in C# must be a binary operator.
2282
24.2k
    if (Style.isCSharp() && 
Tok.is(tok::ampamp)14
)
2283
10
      return TT_BinaryOperator;
2284
2285
24.2k
    const FormatToken *PrevToken = Tok.getPreviousNonComment();
2286
24.2k
    if (!PrevToken)
2287
180
      return TT_UnaryOperator;
2288
2289
24.0k
    const FormatToken *NextToken = Tok.getNextNonComment();
2290
2291
24.0k
    if (InTemplateArgument && 
NextToken1.98k
&&
NextToken->is(tok::kw_noexcept)1.98k
)
2292
9
      return TT_BinaryOperator;
2293
2294
24.0k
    if (!NextToken ||
2295
24.0k
        
NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_noexcept)24.0k
||
2296
24.0k
        
NextToken->canBePointerOrReferenceQualifier()23.5k
||
2297
24.0k
        
(22.0k
NextToken->is(tok::l_brace)22.0k
&&
!NextToken->getNextNonComment()189
)) {
2298
2.13k
      return TT_PointerOrReference;
2299
2.13k
    }
2300
2301
21.9k
    if (PrevToken->is(tok::coloncolon))
2302
85
      return TT_PointerOrReference;
2303
2304
21.8k
    if (PrevToken->is(tok::r_paren) && 
PrevToken->is(TT_TypeDeclarationParen)1.51k
)
2305
463
      return TT_PointerOrReference;
2306
2307
21.3k
    if (determineUnaryOperatorByUsage(Tok))
2308
3.14k
      return TT_UnaryOperator;
2309
2310
18.2k
    if (NextToken->is(tok::l_square) && 
NextToken->isNot(TT_LambdaLSquare)397
)
2311
379
      return TT_PointerOrReference;
2312
17.8k
    if (NextToken->is(tok::kw_operator) && 
!IsExpression454
)
2313
445
      return TT_PointerOrReference;
2314
17.3k
    if (NextToken->isOneOf(tok::comma, tok::semi))
2315
778
      return TT_PointerOrReference;
2316
2317
    // After right braces, star tokens are likely to be pointers to struct,
2318
    // union, or class.
2319
    //   struct {} *ptr;
2320
    // This by itself is not sufficient to distinguish from multiplication
2321
    // following a brace-initialized expression, as in:
2322
    // int i = int{42} * 2;
2323
    // In the struct case, the part of the struct declaration until the `{` and
2324
    // the `}` are put on separate unwrapped lines; in the brace-initialized
2325
    // case, the matching `{` is on the same unwrapped line, so check for the
2326
    // presence of the matching brace to distinguish between those.
2327
16.6k
    if (PrevToken->is(tok::r_brace) && 
Tok.is(tok::star)187
&&
2328
16.6k
        
!PrevToken->MatchingParen94
)
2329
84
      return TT_PointerOrReference;
2330
2331
    // For "} &&"
2332
16.5k
    if (PrevToken->is(tok::r_brace) && 
Tok.is(tok::ampamp)103
) {
2333
93
      const FormatToken *MatchingLBrace = PrevToken->MatchingParen;
2334
2335
      // We check whether there is a TemplateCloser(">") to indicate it's a
2336
      // template or not. If it's not a template, "&&" is likely a reference
2337
      // operator.
2338
      //   struct {} &&ref = {};
2339
93
      if (!MatchingLBrace)
2340
84
        return TT_PointerOrReference;
2341
9
      FormatToken *BeforeLBrace = MatchingLBrace->getPreviousNonComment();
2342
9
      if (!BeforeLBrace || BeforeLBrace->isNot(TT_TemplateCloser))
2343
0
        return TT_PointerOrReference;
2344
2345
      // If it is a template, "&&" is a binary operator.
2346
      //   enable_if<>{} && ...
2347
9
      return TT_BinaryOperator;
2348
9
    }
2349
2350
16.4k
    if (PrevToken->Tok.isLiteral() ||
2351
16.4k
        PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
2352
16.2k
                           tok::kw_false, tok::r_brace)) {
2353
753
      return TT_BinaryOperator;
2354
753
    }
2355
2356
15.6k
    const FormatToken *NextNonParen = NextToken;
2357
16.3k
    while (NextNonParen && NextNonParen->is(tok::l_paren))
2358
710
      NextNonParen = NextNonParen->getNextNonComment();
2359
15.6k
    if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
2360
15.6k
                         
NextNonParen->isOneOf(tok::kw_true, tok::kw_false)15.5k
||
2361
15.6k
                         
NextNonParen->isUnaryOperator()15.5k
)) {
2362
430
      return TT_BinaryOperator;
2363
430
    }
2364
2365
    // If we know we're in a template argument, there are no named declarations.
2366
    // Thus, having an identifier on the right-hand side indicates a binary
2367
    // operator.
2368
15.2k
    if (InTemplateArgument && 
NextToken->Tok.isAnyIdentifier()1.08k
)
2369
172
      return TT_BinaryOperator;
2370
2371
    // "&&(" is quite unlikely to be two successive unary "&".
2372
15.0k
    if (Tok.is(tok::ampamp) && 
NextToken->is(tok::l_paren)2.12k
)
2373
412
      return TT_BinaryOperator;
2374
2375
    // This catches some cases where evaluation order is used as control flow:
2376
    //   aaa && aaa->f();
2377
14.6k
    if (NextToken->Tok.isAnyIdentifier()) {
2378
11.0k
      const FormatToken *NextNextToken = NextToken->getNextNonComment();
2379
11.0k
      if (NextNextToken && 
NextNextToken->is(tok::arrow)10.9k
)
2380
18
        return TT_BinaryOperator;
2381
11.0k
    }
2382
2383
    // It is very unlikely that we are going to find a pointer or reference type
2384
    // definition on the RHS of an assignment.
2385
14.6k
    if (IsExpression && 
!Contexts.back().CaretFound2.36k
)
2386
2.35k
      return TT_BinaryOperator;
2387
2388
12.3k
    return TT_PointerOrReference;
2389
14.6k
  }
2390
2391
5.75k
  TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
2392
5.75k
    if (determineUnaryOperatorByUsage(Tok))
2393
2.35k
      return TT_UnaryOperator;
2394
2395
3.40k
    const FormatToken *PrevToken = Tok.getPreviousNonComment();
2396
3.40k
    if (!PrevToken)
2397
0
      return TT_UnaryOperator;
2398
2399
3.40k
    if (PrevToken->is(tok::at))
2400
12
      return TT_UnaryOperator;
2401
2402
    // Fall back to marking the token as binary operator.
2403
3.39k
    return TT_BinaryOperator;
2404
3.40k
  }
2405
2406
  /// Determine whether ++/-- are pre- or post-increments/-decrements.
2407
1.69k
  TokenType determineIncrementUsage(const FormatToken &Tok) {
2408
1.69k
    const FormatToken *PrevToken = Tok.getPreviousNonComment();
2409
1.69k
    if (!PrevToken || 
PrevToken->is(TT_CastRParen)1.43k
)
2410
284
      return TT_UnaryOperator;
2411
1.41k
    if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
2412
651
      return TT_TrailingUnaryOperator;
2413
2414
763
    return TT_UnaryOperator;
2415
1.41k
  }
2416
2417
  SmallVector<Context, 8> Contexts;
2418
2419
  const FormatStyle &Style;
2420
  AnnotatedLine &Line;
2421
  FormatToken *CurrentToken;
2422
  bool AutoFound;
2423
  const AdditionalKeywords &Keywords;
2424
2425
  // Set of "<" tokens that do not open a template parameter list. If parseAngle
2426
  // determines that a specific token can't be a template opener, it will make
2427
  // same decision irrespective of the decisions for tokens leading up to it.
2428
  // Store this information to prevent this from causing exponential runtime.
2429
  llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
2430
};
2431
2432
static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
2433
static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
2434
2435
/// Parses binary expressions by inserting fake parenthesis based on
2436
/// operator precedence.
2437
class ExpressionParser {
2438
public:
2439
  ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
2440
                   AnnotatedLine &Line)
2441
254k
      : Style(Style), Keywords(Keywords), Line(Line), Current(Line.First) {}
2442
2443
  /// Parse expressions with the given operator precedence.
2444
9.17M
  void parse(int Precedence = 0) {
2445
    // Skip 'return' and ObjC selector colons as they are not part of a binary
2446
    // expression.
2447
9.18M
    while (Current && 
(9.14M
Current->is(tok::kw_return)9.14M
||
2448
9.14M
                       
(9.13M
Current->is(tok::colon)9.13M
&&
2449
9.13M
                        
Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)19.1k
))) {
2450
13.1k
      next();
2451
13.1k
    }
2452
2453
9.17M
    if (!Current || 
Precedence > PrecedenceArrowAndPeriod9.13M
)
2454
1.12M
      return;
2455
2456
    // Conditional expressions need to be parsed separately for proper nesting.
2457
8.04M
    if (Precedence == prec::Conditional) {
2458
454k
      parseConditionalExpr();
2459
454k
      return;
2460
454k
    }
2461
2462
    // Parse unary operators, which all have a higher precedence than binary
2463
    // operators.
2464
7.59M
    if (Precedence == PrecedenceUnaryOperator) {
2465
464k
      parseUnaryOperator();
2466
464k
      return;
2467
464k
    }
2468
2469
7.13M
    FormatToken *Start = Current;
2470
7.13M
    FormatToken *LatestOperator = nullptr;
2471
7.13M
    unsigned OperatorIndex = 0;
2472
2473
8.08M
    while (Current) {
2474
      // Consume operators with higher precedence.
2475
7.82M
      parse(Precedence + 1);
2476
2477
7.82M
      int CurrentPrecedence = getCurrentPrecedence();
2478
2479
7.82M
      if (Precedence == CurrentPrecedence && 
Current173k
&&
2480
7.82M
          
Current->is(TT_SelectorName)173k
) {
2481
6.01k
        if (LatestOperator)
2482
1.70k
          addFakeParenthesis(Start, prec::Level(Precedence));
2483
6.01k
        Start = Current;
2484
6.01k
      }
2485
2486
      // At the end of the line or when an operator with higher precedence is
2487
      // found, insert fake parenthesis and return.
2488
7.82M
      if (!Current ||
2489
7.82M
          
(5.31M
Current->closesScope()5.31M
&&
2490
5.31M
           
(2.06M
Current->MatchingParen2.06M
||
Current->is(TT_TemplateString)39.0k
)) ||
2491
7.82M
          
(3.28M
CurrentPrecedence != -13.28M
&&
CurrentPrecedence < Precedence2.50M
) ||
2492
7.82M
          
(952k
CurrentPrecedence == prec::Conditional952k
&&
2493
6.87M
           
Precedence == prec::Assignment2.41k
&&
Current->is(tok::colon)2.41k
)) {
2494
6.87M
        break;
2495
6.87M
      }
2496
2497
      // Consume scopes: (), [], <> and {}
2498
      // In addition to that we handle require clauses as scope, so that the
2499
      // constraints in that are correctly indented.
2500
950k
      if (Current->opensScope() ||
2501
950k
          Current->isOneOf(TT_RequiresClause,
2502
785k
                           TT_RequiresClauseInARequiresExpression)) {
2503
        // In fragment of a JavaScript template string can look like '}..${' and
2504
        // thus close a scope and open a new one at the same time.
2505
331k
        while (Current && 
(292k
!Current->closesScope()292k
||
Current->opensScope()126k
)) {
2506
165k
          next();
2507
165k
          parse();
2508
165k
        }
2509
165k
        next();
2510
784k
      } else {
2511
        // Operator found.
2512
784k
        if (CurrentPrecedence == Precedence) {
2513
173k
          if (LatestOperator)
2514
21.5k
            LatestOperator->NextOperator = Current;
2515
173k
          LatestOperator = Current;
2516
173k
          Current->OperatorIndex = OperatorIndex;
2517
173k
          ++OperatorIndex;
2518
173k
        }
2519
784k
        next(/*SkipPastLeadingComments=*/Precedence > 0);
2520
784k
      }
2521
950k
    }
2522
2523
7.13M
    if (LatestOperator && 
(152k
Current152k
||
Precedence > 089.1k
)) {
2524
      // The requires clauses do not neccessarily end in a semicolon or a brace,
2525
      // but just go over to struct/class or a function declaration, we need to
2526
      // intervene so that the fake right paren is inserted correctly.
2527
67.5k
      auto End =
2528
67.5k
          (Start->Previous &&
2529
67.5k
           Start->Previous->isOneOf(TT_RequiresClause,
2530
40.1k
                                    TT_RequiresClauseInARequiresExpression))
2531
67.5k
              ? 
[this]()219
{
2532
219
                  auto Ret = Current ? 
Current183
:
Line.Last36
;
2533
2.12k
                  while (!Ret->ClosesRequiresClause && 
Ret->Previous1.93k
)
2534
1.90k
                    Ret = Ret->Previous;
2535
219
                  return Ret;
2536
219
                }()
2537
67.5k
              : 
nullptr67.3k
;
2538
2539
67.5k
      if (Precedence == PrecedenceArrowAndPeriod) {
2540
        // Call expressions don't have a binary operator precedence.
2541
6.15k
        addFakeParenthesis(Start, prec::Unknown, End);
2542
61.3k
      } else {
2543
61.3k
        addFakeParenthesis(Start, prec::Level(Precedence), End);
2544
61.3k
      }
2545
67.5k
    }
2546
7.13M
  }
2547
2548
private:
2549
  /// Gets the precedence (+1) of the given token for binary operators
2550
  /// and other tokens that we treat like binary operators.
2551
7.82M
  int getCurrentPrecedence() {
2552
7.82M
    if (Current) {
2553
5.31M
      const FormatToken *NextNonComment = Current->getNextNonComment();
2554
5.31M
      if (Current->is(TT_ConditionalExpr))
2555
62.6k
        return prec::Conditional;
2556
5.25M
      if (NextNonComment && 
Current->is(TT_SelectorName)3.54M
&&
2557
5.25M
          
(90.9k
NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon)90.9k
||
2558
90.9k
           
(52.9k
(52.9k
Style.Language == FormatStyle::LK_Proto52.9k
||
2559
52.9k
             
Style.Language == FormatStyle::LK_TextProto52.8k
) &&
2560
52.9k
            
NextNonComment->is(tok::less)544
))) {
2561
38.0k
        return prec::Assignment;
2562
38.0k
      }
2563
5.21M
      if (Current->is(TT_JsComputedPropertyName))
2564
280
        return prec::Assignment;
2565
5.21M
      if (Current->is(TT_LambdaArrow))
2566
12.9k
        return prec::Comma;
2567
5.20M
      if (Current->is(TT_FatArrow))
2568
3.36k
        return prec::Assignment;
2569
5.19M
      if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
2570
5.19M
          
(3.75M
Current->is(tok::comment)3.75M
&&
NextNonComment1.41k
&&
2571
3.75M
           
NextNonComment->is(TT_SelectorName)1.38k
)) {
2572
1.44M
        return 0;
2573
1.44M
      }
2574
3.75M
      if (Current->is(TT_RangeBasedForLoopColon))
2575
13.2k
        return prec::Comma;
2576
3.73M
      if ((Style.Language == FormatStyle::LK_Java || 
Style.isJavaScript()3.72M
) &&
2577
3.73M
          
Current->is(Keywords.kw_instanceof)149k
) {
2578
154
        return prec::Relational;
2579
154
      }
2580
3.73M
      if (Style.isJavaScript() &&
2581
3.73M
          
Current->isOneOf(Keywords.kw_in, Keywords.kw_as)134k
) {
2582
1.08k
        return prec::Relational;
2583
1.08k
      }
2584
3.73M
      if (Current->is(TT_BinaryOperator) || 
Current->is(tok::comma)3.32M
)
2585
921k
        return Current->getPrecedence();
2586
2.81M
      if (Current->isOneOf(tok::period, tok::arrow))
2587
7.44k
        return PrecedenceArrowAndPeriod;
2588
2.80M
      if ((Style.Language == FormatStyle::LK_Java || 
Style.isJavaScript()2.79M
) &&
2589
2.80M
          Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
2590
100k
                           Keywords.kw_throws)) {
2591
2.14k
        return 0;
2592
2.14k
      }
2593
2.80M
    }
2594
5.32M
    return -1;
2595
7.82M
  }
2596
2597
  void addFakeParenthesis(FormatToken *Start, prec::Level Precedence,
2598
76.8k
                          FormatToken *End = nullptr) {
2599
76.8k
    Start->FakeLParens.push_back(Precedence);
2600
76.8k
    if (Precedence > prec::Unknown)
2601
61.1k
      Start->StartsBinaryExpression = true;
2602
76.8k
    if (!End && 
Current76.5k
)
2603
71.8k
      End = Current->getPreviousNonComment();
2604
76.8k
    if (End) {
2605
72.0k
      ++End->FakeRParens;
2606
72.0k
      if (Precedence > prec::Unknown)
2607
57.2k
        End->EndsBinaryExpression = true;
2608
72.0k
    }
2609
76.8k
  }
2610
2611
  /// Parse unary operator expressions and surround them with fake
2612
  /// parentheses if appropriate.
2613
464k
  void parseUnaryOperator() {
2614
464k
    llvm::SmallVector<FormatToken *, 2> Tokens;
2615
469k
    while (Current && Current->is(TT_UnaryOperator)) {
2616
5.26k
      Tokens.push_back(Current);
2617
5.26k
      next();
2618
5.26k
    }
2619
464k
    parse(PrecedenceArrowAndPeriod);
2620
464k
    for (FormatToken *Token : llvm::reverse(Tokens)) {
2621
      // The actual precedence doesn't matter.
2622
5.26k
      addFakeParenthesis(Token, prec::Unknown);
2623
5.26k
    }
2624
464k
  }
2625
2626
454k
  void parseConditionalExpr() {
2627
459k
    while (Current && 
Current->isTrailingComment()454k
)
2628
4.80k
      next();
2629
454k
    FormatToken *Start = Current;
2630
454k
    parse(prec::LogicalOr);
2631
454k
    if (!Current || 
!Current->is(tok::question)282k
)
2632
452k
      return;
2633
2.33k
    next();
2634
2.33k
    parse(prec::Assignment);
2635
2.33k
    if (!Current || 
Current->isNot(TT_ConditionalExpr)2.32k
)
2636
26
      return;
2637
2.30k
    next();
2638
2.30k
    parse(prec::Assignment);
2639
2.30k
    addFakeParenthesis(Start, prec::Conditional);
2640
2.30k
  }
2641
2642
1.14M
  void next(bool SkipPastLeadingComments = true) {
2643
1.14M
    if (Current)
2644
1.10M
      Current = Current->Next;
2645
1.15M
    while (Current &&
2646
1.15M
           
(860k
Current->NewlinesBefore == 0860k
||
SkipPastLeadingComments19.9k
) &&
2647
1.15M
           
Current->isTrailingComment()860k
) {
2648
9.97k
      Current = Current->Next;
2649
9.97k
    }
2650
1.14M
  }
2651
2652
  const FormatStyle &Style;
2653
  const AdditionalKeywords &Keywords;
2654
  const AnnotatedLine &Line;
2655
  FormatToken *Current;
2656
};
2657
2658
} // end anonymous namespace
2659
2660
void TokenAnnotator::setCommentLineLevels(
2661
108k
    SmallVectorImpl<AnnotatedLine *> &Lines) const {
2662
108k
  const AnnotatedLine *NextNonCommentLine = nullptr;
2663
108k
  for (AnnotatedLine *Line : llvm::reverse(Lines)) {
2664
86.2k
    assert(Line->First);
2665
2666
    // If the comment is currently aligned with the line immediately following
2667
    // it, that's probably intentional and we should keep it.
2668
86.2k
    if (NextNonCommentLine && 
Line->isComment()49.6k
&&
2669
86.2k
        
NextNonCommentLine->First->NewlinesBefore <= 11.39k
&&
2670
86.2k
        NextNonCommentLine->First->OriginalColumn ==
2671
1.21k
            Line->First->OriginalColumn) {
2672
      // Align comments for preprocessor lines with the # in column 0 if
2673
      // preprocessor lines are not indented. Otherwise, align with the next
2674
      // line.
2675
830
      Line->Level =
2676
830
          (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
2677
830
           
(814
NextNonCommentLine->Type == LT_PreprocessorDirective814
||
2678
814
            
NextNonCommentLine->Type == LT_ImportStatement755
))
2679
830
              ? 
089
2680
830
              : 
NextNonCommentLine->Level741
;
2681
85.4k
    } else {
2682
85.4k
      NextNonCommentLine = Line->First->isNot(tok::r_brace) ? 
Line72.6k
:
nullptr12.7k
;
2683
85.4k
    }
2684
2685
86.2k
    setCommentLineLevels(Line->Children);
2686
86.2k
  }
2687
108k
}
2688
2689
254k
static unsigned maxNestingDepth(const AnnotatedLine &Line) {
2690
254k
  unsigned Result = 0;
2691
1.37M
  for (const auto *Tok = Line.First; Tok != nullptr; 
Tok = Tok->Next1.11M
)
2692
1.11M
    Result = std::max(Result, Tok->NestingLevel);
2693
254k
  return Result;
2694
254k
}
2695
2696
254k
void TokenAnnotator::annotate(AnnotatedLine &Line) const {
2697
254k
  for (auto &Child : Line.Children)
2698
6.77k
    annotate(*Child);
2699
2700
254k
  AnnotatingParser Parser(Style, Line, Keywords);
2701
254k
  Line.Type = Parser.parseLine();
2702
2703
  // With very deep nesting, ExpressionParser uses lots of stack and the
2704
  // formatting algorithm is very slow. We're not going to do a good job here
2705
  // anyway - it's probably generated code being formatted by mistake.
2706
  // Just skip the whole line.
2707
254k
  if (maxNestingDepth(Line) > 50)
2708
3
    Line.Type = LT_Invalid;
2709
2710
254k
  if (Line.Type == LT_Invalid)
2711
69
    return;
2712
2713
254k
  ExpressionParser ExprParser(Style, Keywords, Line);
2714
254k
  ExprParser.parse();
2715
2716
254k
  if (Line.startsWith(TT_ObjCMethodSpecifier))
2717
776
    Line.Type = LT_ObjCMethodDecl;
2718
253k
  else if (Line.startsWith(TT_ObjCDecl))
2719
470
    Line.Type = LT_ObjCDecl;
2720
253k
  else if (Line.startsWith(TT_ObjCProperty))
2721
124
    Line.Type = LT_ObjCProperty;
2722
2723
254k
  Line.First->SpacesRequiredBefore = 1;
2724
254k
  Line.First->CanBreakBefore = Line.First->MustBreakBefore;
2725
254k
}
2726
2727
// This function heuristically determines whether 'Current' starts the name of a
2728
// function declaration.
2729
static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
2730
298k
                                      const AnnotatedLine &Line) {
2731
298k
  auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
2732
1.30k
    for (; Next; 
Next = Next->Next661
) {
2733
1.30k
      if (Next->is(TT_OverloadedOperatorLParen))
2734
587
        return Next;
2735
718
      if (Next->is(TT_OverloadedOperator))
2736
508
        continue;
2737
210
      if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
2738
        // For 'new[]' and 'delete[]'.
2739
90
        if (Next->Next &&
2740
90
            Next->Next->startsSequence(tok::l_square, tok::r_square)) {
2741
12
          Next = Next->Next->Next;
2742
12
        }
2743
90
        continue;
2744
90
      }
2745
120
      if (Next->startsSequence(tok::l_square, tok::r_square)) {
2746
        // For operator[]().
2747
21
        Next = Next->Next;
2748
21
        continue;
2749
21
      }
2750
99
      if ((Next->isSimpleTypeSpecifier() || 
Next->is(tok::identifier)66
) &&
2751
99
          
Next->Next39
&&
Next->Next->isOneOf(tok::star, tok::amp, tok::ampamp)39
) {
2752
        // For operator void*(), operator char*(), operator Foo*().
2753
33
        Next = Next->Next;
2754
33
        continue;
2755
33
      }
2756
66
      if (Next->is(TT_TemplateOpener) && 
Next->MatchingParen9
) {
2757
9
        Next = Next->MatchingParen;
2758
9
        continue;
2759
9
      }
2760
2761
57
      break;
2762
66
    }
2763
57
    return nullptr;
2764
644
  };
2765
2766
  // Find parentheses of parameter list.
2767
298k
  const FormatToken *Next = Current.Next;
2768
298k
  if (Current.is(tok::kw_operator)) {
2769
998
    if (Current.Previous && Current.Previous->is(tok::coloncolon))
2770
430
      return false;
2771
568
    Next = skipOperatorName(Next);
2772
297k
  } else {
2773
297k
    if (!Current.is(TT_StartOfName) || 
Current.NestingLevel != 020.9k
)
2774
281k
      return false;
2775
16.4k
    
for (; 16.1k
Next;
Next = Next->Next300
) {
2776
16.1k
      if (Next->is(TT_TemplateOpener)) {
2777
117
        Next = Next->MatchingParen;
2778
16.0k
      } else if (Next->is(tok::coloncolon)) {
2779
262
        Next = Next->Next;
2780
262
        if (!Next)
2781
0
          return false;
2782
262
        if (Next->is(tok::kw_operator)) {
2783
76
          Next = skipOperatorName(Next->Next);
2784
76
          break;
2785
76
        }
2786
186
        if (!Next->is(tok::identifier))
2787
3
          return false;
2788
15.8k
      } else if (Next->is(tok::l_paren)) {
2789
6.47k
        break;
2790
9.34k
      } else {
2791
9.34k
        return false;
2792
9.34k
      }
2793
16.1k
    }
2794
16.1k
  }
2795
2796
  // Check whether parameter list can belong to a function declaration.
2797
7.38k
  if (!Next || 
!Next->is(tok::l_paren)7.05k
||
!Next->MatchingParen7.05k
)
2798
335
    return false;
2799
  // If the lines ends with "{", this is likely a function definition.
2800
7.05k
  if (Line.Last->is(tok::l_brace))
2801
3.53k
    return true;
2802
3.52k
  if (Next->Next == Next->MatchingParen)
2803
2.01k
    return true; // Empty parentheses.
2804
  // If there is an &/&& after the r_paren, this is likely a function.
2805
1.50k
  if (Next->MatchingParen->Next &&
2806
1.50k
      
Next->MatchingParen->Next->is(TT_PointerOrReference)1.03k
) {
2807
138
    return true;
2808
138
  }
2809
2810
  // Check for K&R C function definitions (and C++ function definitions with
2811
  // unnamed parameters), e.g.:
2812
  //   int f(i)
2813
  //   {
2814
  //     return i + 1;
2815
  //   }
2816
  //   bool g(size_t = 0, bool b = false)
2817
  //   {
2818
  //     return !b;
2819
  //   }
2820
1.36k
  if (IsCpp && 
Next->Next1.30k
&&
Next->Next->is(tok::identifier)1.30k
&&
2821
1.36k
      
!Line.endsWith(tok::semi)413
) {
2822
100
    return true;
2823
100
  }
2824
2825
2.00k
  
for (const FormatToken *Tok = Next->Next; 1.26k
Tok && Tok != Next->MatchingParen;
2826
1.85k
       
Tok = Tok->Next742
) {
2827
1.85k
    if (Tok->is(TT_TypeDeclarationParen))
2828
9
      return true;
2829
1.85k
    if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && 
Tok->MatchingParen49
) {
2830
49
      Tok = Tok->MatchingParen;
2831
49
      continue;
2832
49
    }
2833
1.80k
    if (Tok->is(tok::kw_const) || 
Tok->isSimpleTypeSpecifier()1.64k
||
2834
1.80k
        
Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)869
) {
2835
1.09k
      return true;
2836
1.09k
    }
2837
711
    if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
2838
711
        
Tok->Tok.isLiteral()700
) {
2839
18
      return false;
2840
18
    }
2841
711
  }
2842
148
  return false;
2843
1.26k
}
2844
2845
6.68k
bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
2846
6.68k
  assert(Line.MightBeFunctionDecl);
2847
2848
6.68k
  if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
2849
6.68k
       Style.AlwaysBreakAfterReturnType ==
2850
6.66k
           FormatStyle::RTBS_TopLevelDefinitions) &&
2851
6.68k
      
Line.Level > 028
) {
2852
15
    return false;
2853
15
  }
2854
2855
6.66k
  switch (Style.AlwaysBreakAfterReturnType) {
2856
6.42k
  case FormatStyle::RTBS_None:
2857
6.42k
    return false;
2858
24
  case FormatStyle::RTBS_All:
2859
31
  case FormatStyle::RTBS_TopLevel:
2860
31
    return true;
2861
204
  case FormatStyle::RTBS_AllDefinitions:
2862
210
  case FormatStyle::RTBS_TopLevelDefinitions:
2863
210
    return Line.mightBeFunctionDefinition();
2864
6.66k
  }
2865
2866
0
  return false;
2867
6.66k
}
2868
2869
87.3k
void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
2870
87.3k
  for (AnnotatedLine *ChildLine : Line.Children)
2871
2.44k
    calculateFormattingInformation(*ChildLine);
2872
2873
87.3k
  Line.First->TotalLength =
2874
87.3k
      Line.First->IsMultiline ? 
Style.ColumnLimit222
2875
87.3k
                              : 
Line.FirstStartColumn + Line.First->ColumnWidth87.1k
;
2876
87.3k
  FormatToken *Current = Line.First->Next;
2877
87.3k
  bool InFunctionDecl = Line.MightBeFunctionDecl;
2878
87.3k
  bool AlignArrayOfStructures =
2879
87.3k
      (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
2880
87.3k
       
Line.Type == LT_ArrayOfStructInitializer332
);
2881
87.3k
  if (AlignArrayOfStructures)
2882
157
    calculateArrayInitializerColumnList(Line);
2883
2884
386k
  while (Current) {
2885
298k
    if (isFunctionDeclarationName(Style.isCpp(), *Current, Line))
2886
6.88k
      Current->setType(TT_FunctionDeclarationName);
2887
298k
    const FormatToken *Prev = Current->Previous;
2888
298k
    if (Current->is(TT_LineComment)) {
2889
3.27k
      if (Prev->is(BK_BracedInit) && 
Prev->opensScope()32
) {
2890
29
        Current->SpacesRequiredBefore =
2891
29
            (Style.Cpp11BracedListStyle && 
!Style.SpacesInParentheses24
) ?
021
:
18
;
2892
3.24k
      } else {
2893
3.24k
        Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
2894
3.24k
      }
2895
2896
      // If we find a trailing comment, iterate backwards to determine whether
2897
      // it seems to relate to a specific parameter. If so, break before that
2898
      // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
2899
      // to the previous line in:
2900
      //   SomeFunction(a,
2901
      //                b, // comment
2902
      //                c);
2903
3.27k
      if (!Current->HasUnescapedNewline) {
2904
7.55k
        for (FormatToken *Parameter = Current->Previous; Parameter;
2905
6.65k
             
Parameter = Parameter->Previous5.31k
) {
2906
6.65k
          if (Parameter->isOneOf(tok::comment, tok::r_brace))
2907
1.25k
            break;
2908
5.39k
          if (Parameter->Previous && 
Parameter->Previous->is(tok::comma)4.49k
) {
2909
82
            if (!Parameter->Previous->is(TT_CtorInitializerComma) &&
2910
82
                
Parameter->HasUnescapedNewline79
) {
2911
14
              Parameter->MustBreakBefore = true;
2912
14
            }
2913
82
            break;
2914
82
          }
2915
5.39k
        }
2916
2.24k
      }
2917
295k
    } else if (Current->SpacesRequiredBefore == 0 &&
2918
295k
               
spaceRequiredBefore(Line, *Current)294k
) {
2919
125k
      Current->SpacesRequiredBefore = 1;
2920
125k
    }
2921
2922
298k
    const auto &Children = Prev->Children;
2923
298k
    if (!Children.empty() && 
Children.back()->Last->is(TT_LineComment)1.45k
) {
2924
102
      Current->MustBreakBefore = true;
2925
298k
    } else {
2926
298k
      Current->MustBreakBefore =
2927
298k
          Current->MustBreakBefore || 
mustBreakBefore(Line, *Current)298k
;
2928
298k
      if (!Current->MustBreakBefore && 
InFunctionDecl293k
&&
2929
298k
          
Current->is(TT_FunctionDeclarationName)93.0k
) {
2930
6.68k
        Current->MustBreakBefore = mustBreakForReturnType(Line);
2931
6.68k
      }
2932
298k
    }
2933
2934
298k
    Current->CanBreakBefore =
2935
298k
        Current->MustBreakBefore || 
canBreakBefore(Line, *Current)293k
;
2936
298k
    unsigned ChildSize = 0;
2937
298k
    if (Prev->Children.size() == 1) {
2938
1.00k
      FormatToken &LastOfChild = *Prev->Children[0]->Last;
2939
1.00k
      ChildSize = LastOfChild.isTrailingComment() ? 
Style.ColumnLimit92
2940
1.00k
                                                  : 
LastOfChild.TotalLength + 1916
;
2941
1.00k
    }
2942
298k
    if (Current->MustBreakBefore || 
Prev->Children.size() > 1293k
||
2943
298k
        
(293k
Prev->Children.size() == 1293k
&&
2944
293k
         
Prev->Children[0]->First->MustBreakBefore707
) ||
2945
298k
        
Current->IsMultiline293k
) {
2946
5.71k
      Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
2947
293k
    } else {
2948
293k
      Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
2949
293k
                             ChildSize + Current->SpacesRequiredBefore;
2950
293k
    }
2951
2952
298k
    if (Current->is(TT_CtorInitializerColon))
2953
569
      InFunctionDecl = false;
2954
2955
    // FIXME: Only calculate this if CanBreakBefore is true once static
2956
    // initializers etc. are sorted out.
2957
    // FIXME: Move magic numbers to a better place.
2958
2959
    // Reduce penalty for aligning ObjC method arguments using the colon
2960
    // alignment as this is the canonical way (still prefer fitting everything
2961
    // into one line if possible). Trying to fit a whole expression into one
2962
    // line should not force other line breaks (e.g. when ObjC method
2963
    // expression is a part of other expression).
2964
298k
    Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
2965
298k
    if (Style.Language == FormatStyle::LK_ObjC &&
2966
298k
        
Current->is(TT_SelectorName)75.8k
&&
Current->ParameterIndex > 0887
) {
2967
273
      if (Current->ParameterIndex == 1)
2968
153
        Current->SplitPenalty += 5 * Current->BindingStrength;
2969
298k
    } else {
2970
298k
      Current->SplitPenalty += 20 * Current->BindingStrength;
2971
298k
    }
2972
2973
298k
    Current = Current->Next;
2974
298k
  }
2975
2976
87.3k
  calculateUnbreakableTailLengths(Line);
2977
87.3k
  unsigned IndentLevel = Line.Level;
2978
473k
  for (Current = Line.First; Current != nullptr; 
Current = Current->Next386k
) {
2979
386k
    if (Current->Role)
2980
5.96k
      Current->Role->precomputeFormattingInfos(Current);
2981
386k
    if (Current->MatchingParen &&
2982
386k
        
Current->MatchingParen->opensBlockOrBlockTypeList(Style)88.9k
&&
2983
386k
        
IndentLevel > 04.14k
) {
2984
4.14k
      --IndentLevel;
2985
4.14k
    }
2986
386k
    Current->IndentLevel = IndentLevel;
2987
386k
    if (Current->opensBlockOrBlockTypeList(Style))
2988
17.3k
      ++IndentLevel;
2989
386k
  }
2990
2991
87.3k
  LLVM_DEBUG({ printDebugInfo(Line); });
2992
87.3k
}
2993
2994
void TokenAnnotator::calculateUnbreakableTailLengths(
2995
87.3k
    AnnotatedLine &Line) const {
2996
87.3k
  unsigned UnbreakableTailLength = 0;
2997
87.3k
  FormatToken *Current = Line.Last;
2998
473k
  while (Current) {
2999
386k
    Current->UnbreakableTailLength = UnbreakableTailLength;
3000
386k
    if (Current->CanBreakBefore ||
3001
386k
        
Current->isOneOf(tok::comment, tok::string_literal)278k
) {
3002
112k
      UnbreakableTailLength = 0;
3003
273k
    } else {
3004
273k
      UnbreakableTailLength +=
3005
273k
          Current->ColumnWidth + Current->SpacesRequiredBefore;
3006
273k
    }
3007
386k
    Current = Current->Previous;
3008
386k
  }
3009
87.3k
}
3010
3011
void TokenAnnotator::calculateArrayInitializerColumnList(
3012
157
    AnnotatedLine &Line) const {
3013
157
  if (Line.First == Line.Last)
3014
0
    return;
3015
157
  auto *CurrentToken = Line.First;
3016
157
  CurrentToken->ArrayInitializerLineStart = true;
3017
157
  unsigned Depth = 0;
3018
1.17k
  while (CurrentToken != nullptr && CurrentToken != Line.Last) {
3019
1.02k
    if (CurrentToken->is(tok::l_brace)) {
3020
157
      CurrentToken->IsArrayInitializer = true;
3021
157
      if (CurrentToken->Next != nullptr)
3022
157
        CurrentToken->Next->MustBreakBefore = true;
3023
157
      CurrentToken =
3024
157
          calculateInitializerColumnList(Line, CurrentToken->Next, Depth + 1);
3025
863
    } else {
3026
863
      CurrentToken = CurrentToken->Next;
3027
863
    }
3028
1.02k
  }
3029
157
}
3030
3031
FormatToken *TokenAnnotator::calculateInitializerColumnList(
3032
157
    AnnotatedLine &Line, FormatToken *CurrentToken, unsigned Depth) const {
3033
4.20k
  while (CurrentToken != nullptr && CurrentToken != Line.Last) {
3034
4.05k
    if (CurrentToken->is(tok::l_brace))
3035
510
      ++Depth;
3036
3.54k
    else if (CurrentToken->is(tok::r_brace))
3037
667
      --Depth;
3038
4.05k
    if (Depth == 2 && 
CurrentToken->isOneOf(tok::l_brace, tok::comma)2.76k
) {
3039
1.32k
      CurrentToken = CurrentToken->Next;
3040
1.32k
      if (CurrentToken == nullptr)
3041
0
        break;
3042
1.32k
      CurrentToken->StartsColumn = true;
3043
1.32k
      CurrentToken = CurrentToken->Previous;
3044
1.32k
    }
3045
4.05k
    CurrentToken = CurrentToken->Next;
3046
4.05k
  }
3047
157
  return CurrentToken;
3048
157
}
3049
3050
unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
3051
                                      const FormatToken &Tok,
3052
298k
                                      bool InFunctionDecl) const {
3053
298k
  const FormatToken &Left = *Tok.Previous;
3054
298k
  const FormatToken &Right = Tok;
3055
3056
298k
  if (Left.is(tok::semi))
3057
1.31k
    return 0;
3058
3059
297k
  if (Style.Language == FormatStyle::LK_Java) {
3060
3.51k
    if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
3061
24
      return 1;
3062
3.49k
    if (Right.is(Keywords.kw_implements))
3063
18
      return 2;
3064
3.47k
    if (Left.is(tok::comma) && 
Left.NestingLevel == 0130
)
3065
8
      return 3;
3066
294k
  } else if (Style.isJavaScript()) {
3067
14.0k
    if (Right.is(Keywords.kw_function) && 
Left.isNot(tok::comma)190
)
3068
178
      return 100;
3069
13.8k
    if (Left.is(TT_JsTypeColon))
3070
275
      return 35;
3071
13.6k
    if ((Left.is(TT_TemplateString) && 
Left.TokenText.endswith("${")124
) ||
3072
13.6k
        
(13.5k
Right.is(TT_TemplateString)13.5k
&&
Right.TokenText.startswith("}")122
)) {
3073
98
      return 100;
3074
98
    }
3075
    // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
3076
13.5k
    if (Left.opensScope() && 
Right.closesScope()1.82k
)
3077
804
      return 200;
3078
13.5k
  }
3079
3080
296k
  if (Right.is(tok::identifier) && 
Right.Next81.1k
&&
Right.Next->is(TT_DictLiteral)78.2k
)
3081
1.81k
    return 1;
3082
294k
  if (Right.is(tok::l_square)) {
3083
2.97k
    if (Style.Language == FormatStyle::LK_Proto)
3084
66
      return 1;
3085
2.91k
    if (Left.is(tok::r_square))
3086
81
      return 200;
3087
    // Slightly prefer formatting local lambda definitions like functions.
3088
2.83k
    if (Right.is(TT_LambdaLSquare) && 
Left.is(tok::equal)794
)
3089
277
      return 35;
3090
2.55k
    if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
3091
2.55k
                       TT_ArrayInitializerLSquare,
3092
2.55k
                       TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
3093
1.08k
      return 500;
3094
1.08k
    }
3095
2.55k
  }
3096
3097
292k
  if (Left.is(tok::coloncolon) ||
3098
292k
      
(289k
Right.is(tok::period)289k
&&
Style.Language == FormatStyle::LK_Proto2.37k
)) {
3099
4.08k
    return 500;
3100
4.08k
  }
3101
288k
  if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
3102
288k
      
Right.is(tok::kw_operator)267k
) {
3103
21.4k
    if (Line.startsWith(tok::kw_for) && 
Right.PartOfMultiVariableDeclStmt675
)
3104
140
      return 3;
3105
21.2k
    if (Left.is(TT_StartOfName))
3106
208
      return 110;
3107
21.0k
    if (InFunctionDecl && 
Right.NestingLevel == 010.0k
)
3108
6.86k
      return Style.PenaltyReturnTypeOnItsOwnLine;
3109
14.2k
    return 200;
3110
21.0k
  }
3111
267k
  if (Right.is(TT_PointerOrReference))
3112
5.28k
    return 190;
3113
262k
  if (Right.is(TT_LambdaArrow))
3114
295
    return 110;
3115
261k
  if (Left.is(tok::equal) && 
Right.is(tok::l_brace)7.89k
)
3116
789
    return 160;
3117
261k
  if (Left.is(TT_CastRParen))
3118
996
    return 100;
3119
260k
  if (Left.isOneOf(tok::kw_class, tok::kw_struct))
3120
3.56k
    return 5000;
3121
256k
  if (Left.is(tok::comment))
3122
2.04k
    return 1000;
3123
3124
254k
  if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
3125
254k
                   TT_CtorInitializerColon)) {
3126
1.25k
    return 2;
3127
1.25k
  }
3128
3129
253k
  if (Right.isMemberAccess()) {
3130
    // Breaking before the "./->" of a chained call/member access is reasonably
3131
    // cheap, as formatting those with one call per line is generally
3132
    // desirable. In particular, it should be cheaper to break before the call
3133
    // than it is to break inside a call's parameters, which could lead to weird
3134
    // "hanging" indents. The exception is the very last "./->" to support this
3135
    // frequent pattern:
3136
    //
3137
    //   aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
3138
    //       dddddddd);
3139
    //
3140
    // which might otherwise be blown up onto many lines. Here, clang-format
3141
    // won't produce "hanging" indents anyway as there is no other trailing
3142
    // call.
3143
    //
3144
    // Also apply higher penalty is not a call as that might lead to a wrapping
3145
    // like:
3146
    //
3147
    //   aaaaaaa
3148
    //       .aaaaaaaaa.bbbbbbbb(cccccccc);
3149
2.42k
    return !Right.NextOperator || 
!Right.NextOperator->Previous->closesScope()538
3150
2.42k
               ? 
1502.16k
3151
2.42k
               : 
35263
;
3152
2.42k
  }
3153
3154
250k
  if (Right.is(TT_TrailingAnnotation) &&
3155
250k
      
(1.90k
!Right.Next1.90k
||
Right.Next->isNot(tok::l_paren)1.86k
)) {
3156
    // Moving trailing annotations to the next line is fine for ObjC method
3157
    // declarations.
3158
1.22k
    if (Line.startsWith(TT_ObjCMethodSpecifier))
3159
2
      return 10;
3160
    // Generally, breaking before a trailing annotation is bad unless it is
3161
    // function-like. It seems to be especially preferable to keep standard
3162
    // annotations (i.e. "const", "final" and "override") on the same line.
3163
    // Use a slightly higher penalty after ")" so that annotations like
3164
    // "const override" are kept together.
3165
1.22k
    bool is_short_annotation = Right.TokenText.size() < 10;
3166
1.22k
    return (Left.is(tok::r_paren) ? 
100431
:
120796
) + (is_short_annotation ?
501.06k
:
0165
);
3167
1.22k
  }
3168
3169
  // In for-loops, prefer breaking at ',' and ';'.
3170
249k
  if (Line.startsWith(tok::kw_for) && 
Left.is(tok::equal)6.74k
)
3171
402
    return 4;
3172
3173
  // In Objective-C method expressions, prefer breaking before "param:" over
3174
  // breaking after it.
3175
249k
  if (Right.is(TT_SelectorName))
3176
1.08k
    return 0;
3177
248k
  if (Left.is(tok::colon) && 
Left.is(TT_ObjCMethodExpr)4.27k
)
3178
993
    return Line.MightBeFunctionDecl ? 
50237
:
500756
;
3179
3180
  // In Objective-C type declarations, avoid breaking after the category's
3181
  // open paren (we'll prefer breaking after the protocol list's opening
3182
  // angle bracket, if present).
3183
247k
  if (Line.Type == LT_ObjCDecl && 
Left.is(tok::l_paren)650
&&
Left.Previous41
&&
3184
247k
      
Left.Previous->isOneOf(tok::identifier, tok::greater)41
) {
3185
41
    return 500;
3186
41
  }
3187
3188
246k
  if (Left.is(tok::l_paren) && 
Style.PenaltyBreakOpenParenthesis != 028.3k
)
3189
4
    return Style.PenaltyBreakOpenParenthesis;
3190
246k
  if (Left.is(tok::l_paren) && 
InFunctionDecl28.3k
&&
3191
246k
      
Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign14.5k
) {
3192
14.3k
    return 100;
3193
14.3k
  }
3194
232k
  if (Left.is(tok::l_paren) && 
Left.Previous14.0k
&&
3195
232k
      
(13.8k
Left.Previous->is(tok::kw_for)13.8k
||
Left.Previous->isIf()13.1k
)) {
3196
3.33k
    return 1000;
3197
3.33k
  }
3198
229k
  if (Left.is(tok::equal) && 
InFunctionDecl6.70k
)
3199
802
    return 110;
3200
228k
  if (Right.is(tok::r_brace))
3201
5.52k
    return 1;
3202
222k
  if (Left.is(TT_TemplateOpener))
3203
5.58k
    return 100;
3204
217k
  if (Left.opensScope()) {
3205
    // If we aren't aligning after opening parens/braces we can always break
3206
    // here unless the style does not want us to place all arguments on the
3207
    // next line.
3208
17.6k
    if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
3209
17.6k
        
(487
Left.ParameterCount <= 1487
||
Style.AllowAllArgumentsOnNextLine95
)) {
3210
485
      return 0;
3211
485
    }
3212
17.1k
    if (Left.is(tok::l_brace) && 
!Style.Cpp11BracedListStyle3.19k
)
3213
157
      return 19;
3214
16.9k
    return Left.ParameterCount > 1 ? 
Style.PenaltyBreakBeforeFirstCallParameter3.27k
3215
16.9k
                                   : 
1913.7k
;
3216
17.1k
  }
3217
199k
  if (Left.is(TT_JavaAnnotation))
3218
41
    return 50;
3219
3220
199k
  if (Left.is(TT_UnaryOperator))
3221
2.12k
    return 60;
3222
197k
  if (Left.isOneOf(tok::plus, tok::comma) && 
Left.Previous12.1k
&&
3223
197k
      
Left.Previous->isLabelString()12.0k
&&
3224
197k
      
(27
Left.NextOperator27
||
Left.OperatorIndex != 012
)) {
3225
21
    return 50;
3226
21
  }
3227
197k
  if (Right.is(tok::plus) && 
Left.isLabelString()990
&&
3228
197k
      
(15
Right.NextOperator15
||
Right.OperatorIndex != 09
)) {
3229
9
    return 25;
3230
9
  }
3231
197k
  if (Left.is(tok::comma))
3232
11.0k
    return 1;
3233
186k
  if (Right.is(tok::lessless) && 
Left.isLabelString()423
&&
3234
186k
      
(66
Right.NextOperator66
||
Right.OperatorIndex != 124
)) {
3235
54
    return 25;
3236
54
  }
3237
186k
  if (Right.is(tok::lessless)) {
3238
    // Breaking at a << is really cheap.
3239
369
    if (!Left.is(tok::r_paren) || 
Right.OperatorIndex > 0119
) {
3240
      // Slightly prefer to break before the first one in log-like statements.
3241
265
      return 2;
3242
265
    }
3243
104
    return 1;
3244
369
  }
3245
186k
  if (Left.ClosesTemplateDeclaration)
3246
1.65k
    return Style.PenaltyBreakTemplateDeclaration;
3247
184k
  if (Left.ClosesRequiresClause)
3248
264
    return 0;
3249
184k
  if (Left.is(TT_ConditionalExpr))
3250
1.60k
    return prec::Conditional;
3251
182k
  prec::Level Level = Left.getPrecedence();
3252
182k
  if (Level == prec::Unknown)
3253
165k
    Level = Right.getPrecedence();
3254
182k
  if (Level == prec::Assignment)
3255
14.4k
    return Style.PenaltyBreakAssignment;
3256
168k
  if (Level != prec::Unknown)
3257
39.1k
    return Level;
3258
3259
129k
  return 3;
3260
168k
}
3261
3262
20.6k
bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
3263
20.6k
  if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
3264
166
    return true;
3265
20.5k
  if (Right.is(TT_OverloadedOperatorLParen) &&
3266
20.5k
      
Style.SpaceBeforeParensOptions.AfterOverloadedOperator1.01k
) {
3267
9
    return true;
3268
9
  }
3269
20.5k
  if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
3270
20.5k
      
Right.ParameterCount > 0243
) {
3271
161
    return true;
3272
161
  }
3273
20.3k
  return false;
3274
20.5k
}
3275
3276
bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
3277
                                          const FormatToken &Left,
3278
169k
                                          const FormatToken &Right) const {
3279
169k
  if (Left.is(tok::kw_return) &&
3280
169k
      
!Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)1.46k
) {
3281
994
    return true;
3282
994
  }
3283
168k
  if (Style.isJson() && 
Left.is(tok::string_literal)216
&&
Right.is(tok::colon)14
)
3284
0
    return false;
3285
168k
  if (Left.is(Keywords.kw_assert) && 
Style.Language == FormatStyle::LK_Java4
)
3286
2
    return true;
3287
168k
  if (Style.ObjCSpaceAfterProperty && 
Line.Type == LT_ObjCProperty404
&&
3288
168k
      
Left.Tok.getObjCKeywordID() == tok::objc_property32
) {
3289
4
    return true;
3290
4
  }
3291
168k
  if (Right.is(tok::hashhash))
3292
60
    return Left.is(tok::hash);
3293
168k
  if (Left.isOneOf(tok::hashhash, tok::hash))
3294
3.37k
    return Right.is(tok::hash);
3295
165k
  if ((Left.is(tok::l_paren) && 
Right.is(tok::r_paren)28.8k
) ||
3296
165k
      
(153k
Left.is(tok::l_brace)153k
&&
Left.isNot(BK_Block)5.63k
&&
3297
153k
       
Right.is(tok::r_brace)3.11k
&&
Right.isNot(BK_Block)450
)) {
3298
11.8k
    return Style.SpaceInEmptyParentheses;
3299
11.8k
  }
3300
153k
  if (Style.SpacesInConditionalStatement) {
3301
267
    const FormatToken *LeftParen = nullptr;
3302
267
    if (Left.is(tok::l_paren))
3303
57
      LeftParen = &Left;
3304
210
    else if (Right.is(tok::r_paren) && 
Right.MatchingParen57
)
3305
57
      LeftParen = Right.MatchingParen;
3306
267
    if (LeftParen && 
LeftParen->Previous114
&&
3307
267
        
isKeywordWithCondition(*LeftParen->Previous)114
) {
3308
108
      return true;
3309
108
    }
3310
267
  }
3311
3312
  // auto{x} auto(x)
3313
153k
  if (Left.is(tok::kw_auto) && 
Right.isOneOf(tok::l_paren, tok::l_brace)185
)
3314
51
    return false;
3315
3316
  // operator co_await(x)
3317
153k
  if (Right.is(tok::l_paren) && 
Left.is(tok::kw_co_await)26.4k
&&
Left.Previous18
&&
3318
153k
      
Left.Previous->is(tok::kw_operator)15
) {
3319
6
    return false;
3320
6
  }
3321
  // co_await (x), co_yield (x), co_return (x)
3322
153k
  if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
3323
153k
      
!Right.isOneOf(tok::semi, tok::r_paren)81
) {
3324
63
    return true;
3325
63
  }
3326
3327
153k
  if (Left.is(tok::l_paren) || 
Right.is(tok::r_paren)135k
) {
3328
34.7k
    return (Right.is(TT_CastRParen) ||
3329
34.7k
            
(33.7k
Left.MatchingParen33.7k
&&
Left.MatchingParen->is(TT_CastRParen)19.6k
))
3330
34.7k
               ? 
Style.SpacesInCStyleCastParentheses2.00k
3331
34.7k
               : 
Style.SpacesInParentheses32.7k
;
3332
34.7k
  }
3333
118k
  if (Right.isOneOf(tok::semi, tok::comma))
3334
28.5k
    return false;
3335
89.9k
  if (Right.is(tok::less) && 
Line.Type == LT_ObjCDecl5.48k
) {
3336
65
    bool IsLightweightGeneric = Right.MatchingParen &&
3337
65
                                Right.MatchingParen->Next &&
3338
65
                                
Right.MatchingParen->Next->is(tok::colon)46
;
3339
65
    return !IsLightweightGeneric && 
Style.ObjCSpaceBeforeProtocolList58
;
3340
65
  }
3341
89.9k
  if (Right.is(tok::less) && 
Left.is(tok::kw_template)5.41k
)
3342
1.65k
    return Style.SpaceAfterTemplateKeyword;
3343
88.2k
  if (Left.isOneOf(tok::exclaim, tok::tilde))
3344
6
    return false;
3345
88.2k
  if (Left.is(tok::at) &&
3346
88.2k
      Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
3347
881
                    tok::numeric_constant, tok::l_paren, tok::l_brace,
3348
881
                    tok::kw_true, tok::kw_false)) {
3349
765
    return false;
3350
765
  }
3351
87.5k
  if (Left.is(tok::colon))
3352
4.86k
    return !Left.is(TT_ObjCMethodExpr);
3353
82.6k
  if (Left.is(tok::coloncolon))
3354
3.83k
    return false;
3355
78.8k
  if (Left.is(tok::less) || 
Right.isOneOf(tok::greater, tok::less)78.4k
) {
3356
4.15k
    if (Style.Language == FormatStyle::LK_TextProto ||
3357
4.15k
        
(3.79k
Style.Language == FormatStyle::LK_Proto3.79k
&&
3358
3.79k
         
(112
Left.is(TT_DictLiteral)112
||
Right.is(TT_DictLiteral)58
))) {
3359
      // Format empty list as `<>`.
3360
454
      if (Left.is(tok::less) && 
Right.is(tok::greater)273
)
3361
56
        return false;
3362
398
      return !Style.Cpp11BracedListStyle;
3363
454
    }
3364
3.70k
    return false;
3365
4.15k
  }
3366
74.6k
  if (Right.is(tok::ellipsis)) {
3367
159
    return Left.Tok.isLiteral() || 
(153
Left.is(tok::identifier)153
&&
Left.Previous69
&&
3368
153
                                    
Left.Previous->is(tok::kw_case)69
);
3369
159
  }
3370
74.4k
  if (Left.is(tok::l_square) && 
Right.is(tok::amp)3.45k
)
3371
105
    return Style.SpacesInSquareBrackets;
3372
74.3k
  if (Right.is(TT_PointerOrReference)) {
3373
5.13k
    if (Left.is(tok::r_paren) && 
Line.MightBeFunctionDecl430
) {
3374
309
      if (!Left.MatchingParen)
3375
0
        return true;
3376
309
      FormatToken *TokenBeforeMatchingParen =
3377
309
          Left.MatchingParen->getPreviousNonComment();
3378
309
      if (!TokenBeforeMatchingParen || !Left.is(TT_TypeDeclarationParen))
3379
252
        return true;
3380
309
    }
3381
    // Add a space if the previous token is a pointer qualifier or the closing
3382
    // parenthesis of __attribute__(()) expression and the style requires spaces
3383
    // after pointer qualifiers.
3384
4.88k
    if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
3385
4.88k
         
Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both4.82k
) &&
3386
4.88k
        
(84
Left.is(TT_AttributeParen)84
||
3387
84
         
Left.canBePointerOrReferenceQualifier()78
)) {
3388
42
      return true;
3389
42
    }
3390
4.84k
    if (Left.Tok.isLiteral())
3391
0
      return true;
3392
    // for (auto a = 0, b = 0; const auto & c : {1, 2, 3})
3393
4.84k
    if (Left.isTypeOrIdentifier() && 
Right.Next3.88k
&&
Right.Next->Next3.88k
&&
3394
4.84k
        
Right.Next->Next->is(TT_RangeBasedForLoopColon)3.83k
) {
3395
211
      return getTokenPointerOrReferenceAlignment(Right) !=
3396
211
             FormatStyle::PAS_Left;
3397
211
    }
3398
4.63k
    return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
3399
4.63k
           
(4.42k
getTokenPointerOrReferenceAlignment(Right) !=
3400
4.42k
                FormatStyle::PAS_Left ||
3401
4.42k
            
(724
Line.IsMultiVariableDeclStmt724
&&
3402
724
             
(66
Left.NestingLevel == 066
||
3403
66
              
(60
Left.NestingLevel == 160
&&
startsWithInitStatement(Line)27
))));
3404
4.84k
  }
3405
69.2k
  if (Right.is(TT_FunctionTypeLParen) && 
Left.isNot(tok::l_paren)202
&&
3406
69.2k
      
(202
!Left.is(TT_PointerOrReference)202
||
3407
202
       
(12
getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right12
&&
3408
196
        
!Line.IsMultiVariableDeclStmt6
))) {
3409
196
    return true;
3410
196
  }
3411
69.0k
  if (Left.is(TT_PointerOrReference)) {
3412
    // Add a space if the next token is a pointer qualifier and the style
3413
    // requires spaces before pointer qualifiers.
3414
3.70k
    if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
3415
3.70k
         
Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both3.65k
) &&
3416
3.70k
        
Right.canBePointerOrReferenceQualifier()72
) {
3417
42
      return true;
3418
42
    }
3419
    // & 1
3420
3.66k
    if (Right.Tok.isLiteral())
3421
0
      return true;
3422
    // & /* comment
3423
3.66k
    if (Right.is(TT_BlockComment))
3424
2
      return true;
3425
    // foo() -> const Bar * override/final
3426
3.66k
    if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final) &&
3427
3.66k
        
!Right.is(TT_StartOfName)6
) {
3428
0
      return true;
3429
0
    }
3430
    // & {
3431
3.66k
    if (Right.is(tok::l_brace) && 
Right.is(BK_Block)63
)
3432
63
      return true;
3433
    // for (auto a = 0, b = 0; const auto& c : {1, 2, 3})
3434
3.60k
    if (Left.Previous && Left.Previous->isTypeOrIdentifier() && 
Right.Next2.80k
&&
3435
3.60k
        
Right.Next->is(TT_RangeBasedForLoopColon)2.78k
) {
3436
211
      return getTokenPointerOrReferenceAlignment(Left) !=
3437
211
             FormatStyle::PAS_Right;
3438
211
    }
3439
3.39k
    if (Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
3440
3.39k
                      tok::l_paren)) {
3441
67
      return false;
3442
67
    }
3443
3.32k
    if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
3444
2.58k
      return false;
3445
    // FIXME: Setting IsMultiVariableDeclStmt for the whole line is error-prone,
3446
    // because it does not take into account nested scopes like lambdas.
3447
    // In multi-variable declaration statements, attach */& to the variable
3448
    // independently of the style. However, avoid doing it if we are in a nested
3449
    // scope, e.g. lambda. We still need to special-case statements with
3450
    // initializers.
3451
734
    if (Line.IsMultiVariableDeclStmt &&
3452
734
        
(67
Left.NestingLevel == Line.First->NestingLevel67
||
3453
67
         
(60
(Left.NestingLevel == Line.First->NestingLevel + 1)60
&&
3454
60
          
startsWithInitStatement(Line)30
))) {
3455
25
      return false;
3456
25
    }
3457
709
    return Left.Previous && !Left.Previous->isOneOf(
3458
709
                                tok::l_paren, tok::coloncolon, tok::l_square);
3459
734
  }
3460
  // Ensure right pointer alignment with ellipsis e.g. int *...P
3461
65.3k
  if (Left.is(tok::ellipsis) && 
Left.Previous105
&&
3462
65.3k
      
Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp)105
) {
3463
18
    return Style.PointerAlignment != FormatStyle::PAS_Right;
3464
18
  }
3465
3466
65.3k
  if (Right.is(tok::star) && 
Left.is(tok::l_paren)310
)
3467
0
    return false;
3468
65.3k
  if (Left.is(tok::star) && 
Right.isOneOf(tok::star, tok::amp, tok::ampamp)334
)
3469
114
    return false;
3470
65.2k
  if (Right.isOneOf(tok::star, tok::amp, tok::ampamp)) {
3471
377
    const FormatToken *Previous = &Left;
3472
707
    while (Previous && !Previous->is(tok::kw_operator)) {
3473
524
      if (Previous->is(tok::identifier) || 
Previous->isSimpleTypeSpecifier()458
) {
3474
183
        Previous = Previous->getPreviousNonComment();
3475
183
        continue;
3476
183
      }
3477
341
      if (Previous->is(TT_TemplateCloser) && 
Previous->MatchingParen138
) {
3478
138
        Previous = Previous->MatchingParen->getPreviousNonComment();
3479
138
        continue;
3480
138
      }
3481
203
      if (Previous->is(tok::coloncolon)) {
3482
9
        Previous = Previous->getPreviousNonComment();
3483
9
        continue;
3484
9
      }
3485
194
      break;
3486
203
    }
3487
    // Space between the type and the * in:
3488
    //   operator void*()
3489
    //   operator char*()
3490
    //   operator void const*()
3491
    //   operator void volatile*()
3492
    //   operator /*comment*/ const char*()
3493
    //   operator volatile /*comment*/ char*()
3494
    //   operator Foo*()
3495
    //   operator C<T>*()
3496
    //   operator std::Foo*()
3497
    //   operator C<T>::D<U>*()
3498
    // dependent on PointerAlignment style.
3499
377
    if (Previous) {
3500
377
      if (Previous->endsSequence(tok::kw_operator))
3501
183
        return Style.PointerAlignment != FormatStyle::PAS_Left;
3502
194
      if (Previous->is(tok::kw_const) || 
Previous->is(tok::kw_volatile)56
) {
3503
147
        return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
3504
147
               (Style.SpaceAroundPointerQualifiers ==
3505
78
                FormatStyle::SAPQ_After) ||
3506
147
               
(Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both)75
;
3507
147
      }
3508
194
    }
3509
377
  }
3510
64.8k
  const auto SpaceRequiredForArrayInitializerLSquare =
3511
64.8k
      [](const FormatToken &LSquareTok, const FormatStyle &Style) {
3512
394
        return Style.SpacesInContainerLiterals ||
3513
394
               
(286
(286
Style.Language == FormatStyle::LK_Proto286
||
3514
286
                 
Style.Language == FormatStyle::LK_TextProto174
) &&
3515
286
                
!Style.Cpp11BracedListStyle164
&&
3516
286
                LSquareTok.endsSequence(tok::l_square, tok::colon,
3517
160
                                        TT_SelectorName));
3518
394
      };
3519
64.8k
  if (Left.is(tok::l_square)) {
3520
3.34k
    return (Left.is(TT_ArrayInitializerLSquare) && 
Right.isNot(tok::r_square)255
&&
3521
3.34k
            
SpaceRequiredForArrayInitializerLSquare(Left, Style)214
) ||
3522
3.34k
           
(3.24k
Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
3523
3.24k
                         TT_LambdaLSquare) &&
3524
3.24k
            
Style.SpacesInSquareBrackets1.84k
&&
Right.isNot(tok::r_square)56
);
3525
3.34k
  }
3526
61.5k
  if (Right.is(tok::r_square)) {
3527
2.28k
    return Right.MatchingParen &&
3528
2.28k
           ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
3529
2.28k
             SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
3530
180
                                                     Style)) ||
3531
2.28k
            
(2.19k
Style.SpacesInSquareBrackets2.19k
&&
3532
2.19k
             Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
3533
47
                                          TT_StructuredBindingLSquare,
3534
47
                                          TT_LambdaLSquare)) ||
3535
2.28k
            
Right.MatchingParen->is(TT_AttributeParen)2.14k
);
3536
2.28k
  }
3537
59.2k
  if (Right.is(tok::l_square) &&
3538
59.2k
      !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
3539
1.32k
                     TT_DesignatedInitializerLSquare,
3540
1.32k
                     TT_StructuredBindingLSquare, TT_AttributeSquare) &&
3541
59.2k
      
!Left.isOneOf(tok::numeric_constant, TT_DictLiteral)1.02k
&&
3542
59.2k
      
!(945
!Left.is(tok::r_square)945
&&
Style.SpaceBeforeSquareBrackets874
&&
3543
945
        
Right.is(TT_ArraySubscriptLSquare)30
)) {
3544
915
    return false;
3545
915
  }
3546
58.3k
  if (Left.is(tok::l_brace) && 
Right.is(tok::r_brace)5.19k
)
3547
1.77k
    return !Left.Children.empty(); // No spaces in "{}".
3548
56.5k
  if ((Left.is(tok::l_brace) && 
Left.isNot(BK_Block)3.42k
) ||
3549
56.5k
      
(53.8k
Right.is(tok::r_brace)53.8k
&&
Right.MatchingParen3.20k
&&
3550
53.8k
       
Right.MatchingParen->isNot(BK_Block)3.20k
)) {
3551
5.24k
    return Style.Cpp11BracedListStyle ? 
Style.SpacesInParentheses4.08k
:
true1.15k
;
3552
5.24k
  }
3553
51.3k
  if (Left.is(TT_BlockComment)) {
3554
    // No whitespace in x(/*foo=*/1), except for JavaScript.
3555
351
    return Style.isJavaScript() || 
!Left.TokenText.endswith("=*/")330
;
3556
351
  }
3557
3558
  // Space between template and attribute.
3559
  // e.g. template <typename T> [[nodiscard]] ...
3560
50.9k
  if (Left.is(TT_TemplateCloser) && 
Right.is(TT_AttributeSquare)3.67k
)
3561
4
    return true;
3562
  // Space before parentheses common for all languages
3563
50.9k
  if (Right.is(tok::l_paren)) {
3564
25.4k
    if (Left.is(TT_TemplateCloser) && 
Right.isNot(TT_FunctionTypeLParen)582
)
3565
582
      return spaceRequiredBeforeParens(Right);
3566
24.8k
    if (Left.isOneOf(TT_RequiresClause,
3567
24.8k
                     TT_RequiresClauseInARequiresExpression)) {
3568
59
      return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
3569
59
             
spaceRequiredBeforeParens(Right)47
;
3570
59
    }
3571
24.8k
    if (Left.is(TT_RequiresExpression)) {
3572
203
      return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
3573
203
             
spaceRequiredBeforeParens(Right)179
;
3574
203
    }
3575
24.6k
    if ((Left.is(tok::r_paren) && 
Left.is(TT_AttributeParen)423
) ||
3576
24.6k
        
(24.6k
Left.is(tok::r_square)24.6k
&&
Left.is(TT_AttributeSquare)883
)) {
3577
12
      return true;
3578
12
    }
3579
24.6k
    if (Left.is(TT_ForEachMacro)) {
3580
91
      return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
3581
91
             
spaceRequiredBeforeParens(Right)13
;
3582
91
    }
3583
24.5k
    if (Left.is(TT_IfMacro)) {
3584
414
      return Style.SpaceBeforeParensOptions.AfterIfMacros ||
3585
414
             
spaceRequiredBeforeParens(Right)12
;
3586
414
    }
3587
24.1k
    if (Line.Type == LT_ObjCDecl)
3588
41
      return true;
3589
24.0k
    if (Left.is(tok::semi))
3590
3
      return true;
3591
24.0k
    if (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
3592
24.0k
                     tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
3593
24.0k
        
Left.isIf(Line.Type != LT_PreprocessorDirective)22.9k
) {
3594
3.39k
      return Style.SpaceBeforeParensOptions.AfterControlStatements ||
3595
3.39k
             
spaceRequiredBeforeParens(Right)66
;
3596
3.39k
    }
3597
3598
    // TODO add Operator overloading specific Options to
3599
    // SpaceBeforeParensOptions
3600
20.6k
    if (Right.is(TT_OverloadedOperatorLParen))
3601
1.00k
      return spaceRequiredBeforeParens(Right);
3602
    // Function declaration or definition
3603
19.6k
    if (Line.MightBeFunctionDecl && 
(Left.is(TT_FunctionDeclarationName))12.5k
) {
3604
5.89k
      if (Line.mightBeFunctionDefinition()) {
3605
4.00k
        return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
3606
4.00k
               
spaceRequiredBeforeParens(Right)4.00k
;
3607
4.00k
      } else {
3608
1.89k
        return Style.SpaceBeforeParensOptions.AfterFunctionDeclarationName ||
3609
1.89k
               
spaceRequiredBeforeParens(Right)1.86k
;
3610
1.89k
      }
3611
5.89k
    }
3612
    // Lambda
3613
13.7k
    if (Line.Type != LT_PreprocessorDirective && 
Left.is(tok::r_square)13.2k
&&
3614
13.7k
        
Left.MatchingParen841
&&
Left.MatchingParen->is(TT_LambdaLSquare)841
) {
3615
810
      return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
3616
810
             
spaceRequiredBeforeParens(Right)807
;
3617
810
    }
3618
12.9k
    if (!Left.Previous || 
Left.Previous->isNot(tok::period)7.67k
) {
3619
12.0k
      if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
3620
111
        return Style.SpaceBeforeParensOptions.AfterControlStatements ||
3621
111
               
spaceRequiredBeforeParens(Right)0
;
3622
111
      }
3623
11.9k
      if (Left.isOneOf(tok::kw_new, tok::kw_delete)) {
3624
17
        return ((!Line.MightBeFunctionDecl || 
!Left.Previous6
) &&
3625
17
                
Style.SpaceBeforeParens != FormatStyle::SBPO_Never15
) ||
3626
17
               
spaceRequiredBeforeParens(Right)2
;
3627
17
      }
3628
3629
11.9k
      if (Left.is(tok::r_square) && 
Left.MatchingParen31
&&
3630
11.9k
          
Left.MatchingParen->Previous31
&&
3631
11.9k
          
Left.MatchingParen->Previous->is(tok::kw_delete)29
) {
3632
5
        return (Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
3633
5
               
spaceRequiredBeforeParens(Right)0
;
3634
5
      }
3635
11.9k
    }
3636
    // Handle builtins like identifiers.
3637
12.8k
    if (Line.Type != LT_PreprocessorDirective &&
3638
12.8k
        
(12.3k
Left.Tok.getIdentifierInfo()12.3k
||
Left.is(tok::r_paren)578
)) {
3639
12.0k
      return spaceRequiredBeforeParens(Right);
3640
12.0k
    }
3641
736
    return false;
3642
12.8k
  }
3643
25.4k
  if (Left.is(tok::at) && 
Right.Tok.getObjCKeywordID() != tok::objc_not_keyword68
)
3644
62
    return false;
3645
25.4k
  if (Right.is(TT_UnaryOperator)) {
3646
209
    return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
3647
209
           
(205
Left.isNot(tok::colon)205
||
Left.isNot(TT_ObjCMethodExpr)0
);
3648
209
  }
3649
25.2k
  if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
3650
25.2k
                    tok::r_paren) ||
3651
25.2k
       
Left.isSimpleTypeSpecifier()5.64k
) &&
3652
25.2k
      
Right.is(tok::l_brace)19.7k
&&
Right.getNextNonComment()12.6k
&&
3653
25.2k
      
Right.isNot(BK_Block)2.77k
) {
3654
568
    return false;
3655
568
  }
3656
24.6k
  if (Left.is(tok::period) || Right.is(tok::period))
3657
0
    return false;
3658
  // u#str, U#str, L#str, u8#str
3659
  // uR#str, UR#str, LR#str, u8R#str
3660
24.6k
  if (Right.is(tok::hash) && 
Left.is(tok::identifier)23
&&
3661
24.6k
      
(14
Left.TokenText == "L"14
||
Left.TokenText == "u"13
||
3662
14
       
Left.TokenText == "U"12
||
Left.TokenText == "u8"11
||
3663
14
       
Left.TokenText == "LR"10
||
Left.TokenText == "uR"9
||
3664
14
       
Left.TokenText == "UR"8
||
Left.TokenText == "u8R"7
)) {
3665
8
    return false;
3666
8
  }
3667
24.6k
  if (Left.is(TT_TemplateCloser) && 
Left.MatchingParen3.02k
&&
3668
24.6k
      
Left.MatchingParen->Previous3.02k
&&
3669
24.6k
      
(3.02k
Left.MatchingParen->Previous->is(tok::period)3.02k
||
3670
3.02k
       
Left.MatchingParen->Previous->is(tok::coloncolon)3.01k
)) {
3671
    // Java call to generic function with explicit type:
3672
    // A.<B<C<...>>>DoSomething();
3673
    // A::<B<C<...>>>DoSomething();  // With a Java 8 method reference.
3674
11
    return false;
3675
11
  }
3676
24.6k
  if (Left.is(TT_TemplateCloser) && 
Right.is(tok::l_square)3.01k
)
3677
0
    return false;
3678
24.6k
  if (Left.is(tok::l_brace) && 
Left.endsSequence(TT_DictLiteral, tok::at)750
) {
3679
    // Objective-C dictionary literal -> no space after opening brace.
3680
6
    return false;
3681
6
  }
3682
24.6k
  if (Right.is(tok::r_brace) && 
Right.MatchingParen635
&&
3683
24.6k
      
Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)634
) {
3684
    // Objective-C dictionary literal -> no space before closing brace.
3685
10
    return false;
3686
10
  }
3687
24.6k
  if (Right.getType() == TT_TrailingAnnotation &&
3688
24.6k
      
Right.isOneOf(tok::amp, tok::ampamp)543
&&
3689
24.6k
      
Left.isOneOf(tok::kw_const, tok::kw_volatile)0
&&
3690
24.6k
      
(0
!Right.Next0
||
Right.Next->is(tok::semi)0
)) {
3691
    // Match const and volatile ref-qualifiers without any additional
3692
    // qualifiers such as
3693
    // void Fn() const &;
3694
0
    return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
3695
0
  }
3696
3697
24.6k
  return true;
3698
24.6k
}
3699
3700
bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
3701
294k
                                         const FormatToken &Right) const {
3702
294k
  const FormatToken &Left = *Right.Previous;
3703
3704
  // If the token is finalized don't touch it (as it could be in a
3705
  // clang-format-off section).
3706
294k
  if (Left.Finalized)
3707
2.19k
    return Right.hasWhitespaceBefore();
3708
3709
  // Never ever merge two words.
3710
292k
  if (Keywords.isWordLike(Right) && 
Keywords.isWordLike(Left)100k
)
3711
36.0k
    return true;
3712
3713
  // Leave a space between * and /* to avoid C4138 `comment end` found outside
3714
  // of comment.
3715
256k
  if (Left.is(tok::star) && 
Right.is(tok::comment)4.86k
)
3716
14
    return true;
3717
3718
256k
  if (Style.isCpp()) {
3719
    // Space between import <iostream>.
3720
    // or import .....;
3721
226k
    if (Left.is(Keywords.kw_import) && 
Right.isOneOf(tok::less, tok::ellipsis)46
)
3722
18
      return true;
3723
    // Space between `module :` and `import :`.
3724
225k
    if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
3725
225k
        
Right.is(TT_ModulePartitionColon)34
) {
3726
9
      return true;
3727
9
    }
3728
    // No space between import foo:bar but keep a space between import :bar;
3729
225k
    if (Left.is(tok::identifier) && 
Right.is(TT_ModulePartitionColon)72.9k
)
3730
21
      return false;
3731
    // No space between :bar;
3732
225k
    if (Left.is(TT_ModulePartitionColon) &&
3733
225k
        
Right.isOneOf(tok::identifier, tok::kw_private)30
) {
3734
30
      return false;
3735
30
    }
3736
225k
    if (Left.is(tok::ellipsis) && 
Right.is(tok::identifier)253
&&
3737
225k
        
Line.First->is(Keywords.kw_import)81
) {
3738
3
      return false;
3739
3
    }
3740
    // Space in __attribute__((attr)) ::type.
3741
225k
    if (Left.is(TT_AttributeParen) && 
Right.is(tok::coloncolon)224
)
3742
3
      return true;
3743
3744
225k
    if (Left.is(tok::kw_operator))
3745
749
      return Right.is(tok::coloncolon);
3746
225k
    if (Right.is(tok::l_brace) && 
Right.is(BK_BracedInit)14.9k
&&
3747
225k
        
!Left.opensScope()1.83k
&&
Style.SpaceBeforeCpp11BracedList1.45k
) {
3748
9
      return true;
3749
9
    }
3750
225k
    if (Left.is(tok::less) && 
Left.is(TT_OverloadedOperator)5.76k
&&
3751
225k
        
Right.is(TT_TemplateOpener)15
) {
3752
6
      return true;
3753
6
    }
3754
225k
  } else 
if (30.4k
Style.Language == FormatStyle::LK_Proto30.4k
||
3755
30.4k
             
Style.Language == FormatStyle::LK_TextProto26.7k
) {
3756
8.98k
    if (Right.is(tok::period) &&
3757
8.98k
        Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
3758
376
                     Keywords.kw_repeated, Keywords.kw_extend)) {
3759
4
      return true;
3760
4
    }
3761
8.98k
    if (Right.is(tok::l_paren) &&
3762
8.98k
        
Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)138
) {
3763
102
      return true;
3764
102
    }
3765
8.88k
    if (Right.isOneOf(tok::l_brace, tok::less) && 
Left.is(TT_SelectorName)848
)
3766
361
      return true;
3767
    // Slashes occur in text protocol extension syntax: [type/type] { ... }.
3768
8.52k
    if (Left.is(tok::slash) || 
Right.is(tok::slash)8.50k
)
3769
36
      return false;
3770
8.48k
    if (Left.MatchingParen &&
3771
8.48k
        
Left.MatchingParen->is(TT_ProtoExtensionLSquare)1.87k
&&
3772
8.48k
        
Right.isOneOf(tok::l_brace, tok::less)14
) {
3773
4
      return !Style.Cpp11BracedListStyle;
3774
4
    }
3775
    // A percent is probably part of a formatting specification, such as %lld.
3776
8.48k
    if (Left.is(tok::percent))
3777
8
      return false;
3778
    // Preserve the existence of a space before a percent for cases like 0x%04x
3779
    // and "%d %d"
3780
8.47k
    if (Left.is(tok::numeric_constant) && 
Right.is(tok::percent)491
)
3781
4
      return Right.hasWhitespaceBefore();
3782
21.4k
  } else if (Style.isJson()) {
3783
449
    if (Right.is(tok::colon))
3784
73
      return false;
3785
21.0k
  } else if (Style.isCSharp()) {
3786
    // Require spaces around '{' and  before '}' unless they appear in
3787
    // interpolated strings. Interpolated strings are merged into a single token
3788
    // so cannot have spaces inserted by this function.
3789
3790
    // No space between 'this' and '['
3791
5.16k
    if (Left.is(tok::kw_this) && 
Right.is(tok::l_square)2
)
3792
2
      return false;
3793
3794
    // No space between 'new' and '('
3795
5.16k
    if (Left.is(tok::kw_new) && 
Right.is(tok::l_paren)12
)
3796
6
      return false;
3797
3798
    // Space before { (including space within '{ {').
3799
5.15k
    if (Right.is(tok::l_brace))
3800
410
      return true;
3801
3802
    // Spaces inside braces.
3803
4.74k
    if (Left.is(tok::l_brace) && 
Right.isNot(tok::r_brace)132
)
3804
104
      return true;
3805
3806
4.64k
    if (Left.isNot(tok::l_brace) && 
Right.is(tok::r_brace)4.61k
)
3807
104
      return true;
3808
3809
    // Spaces around '=>'.
3810
4.54k
    if (Left.is(TT_FatArrow) || 
Right.is(TT_FatArrow)4.47k
)
3811
152
      return true;
3812
3813
    // No spaces around attribute target colons
3814
4.38k
    if (Left.is(TT_AttributeColon) || 
Right.is(TT_AttributeColon)4.38k
)
3815
4
      return false;
3816
3817
    // space between type and variable e.g. Dictionary<string,string> foo;
3818
4.38k
    if (Left.is(TT_TemplateCloser) && 
Right.is(TT_StartOfName)32
)
3819
12
      return true;
3820
3821
    // spaces inside square brackets.
3822
4.37k
    if (Left.is(tok::l_square) || 
Right.is(tok::r_square)4.18k
)
3823
321
      return Style.SpacesInSquareBrackets;
3824
3825
    // No space before ? in nullable types.
3826
4.05k
    if (Right.is(TT_CSharpNullable))
3827
20
      return false;
3828
3829
    // No space before null forgiving '!'.
3830
4.03k
    if (Right.is(TT_NonNullAssertion))
3831
36
      return false;
3832
3833
    // No space between consecutive commas '[,,]'.
3834
3.99k
    if (Left.is(tok::comma) && 
Right.is(tok::comma)166
)
3835
4
      return false;
3836
3837
    // space after var in `var (key, value)`
3838
3.99k
    if (Left.is(Keywords.kw_var) && 
Right.is(tok::l_paren)2
)
3839
2
      return true;
3840
3841
    // space between keywords and paren e.g. "using ("
3842
3.98k
    if (Right.is(tok::l_paren)) {
3843
618
      if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
3844
618
                       Keywords.kw_lock)) {
3845
46
        return Style.SpaceBeforeParensOptions.AfterControlStatements ||
3846
46
               
spaceRequiredBeforeParens(Right)18
;
3847
46
      }
3848
618
    }
3849
3850
    // space between method modifier and opening parenthesis of a tuple return
3851
    // type
3852
3.94k
    if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
3853
3.94k
                     tok::kw_virtual, tok::kw_extern, tok::kw_static,
3854
3.94k
                     Keywords.kw_internal, Keywords.kw_abstract,
3855
3.94k
                     Keywords.kw_sealed, Keywords.kw_override,
3856
3.94k
                     Keywords.kw_async, Keywords.kw_unsafe) &&
3857
3.94k
        
Right.is(tok::l_paren)22
) {
3858
22
      return true;
3859
22
    }
3860
15.8k
  } else if (Style.isJavaScript()) {
3861
12.3k
    if (Left.is(TT_FatArrow))
3862
77
      return true;
3863
    // for await ( ...
3864
12.2k
    if (Right.is(tok::l_paren) && 
Left.is(Keywords.kw_await)1.06k
&&
Left.Previous6
&&
3865
12.2k
        
Left.Previous->is(tok::kw_for)4
) {
3866
4
      return true;
3867
4
    }
3868
12.2k
    if (Left.is(Keywords.kw_async) && 
Right.is(tok::l_paren)6
&&
3869
12.2k
        
Right.MatchingParen4
) {
3870
4
      const FormatToken *Next = Right.MatchingParen->getNextNonComment();
3871
      // An async arrow function, for example: `x = async () => foo();`,
3872
      // as opposed to calling a function called async: `x = async();`
3873
4
      if (Next && Next->is(TT_FatArrow))
3874
2
        return true;
3875
4
    }
3876
12.2k
    if ((Left.is(TT_TemplateString) && 
Left.TokenText.endswith("${")124
) ||
3877
12.2k
        
(12.2k
Right.is(TT_TemplateString)12.2k
&&
Right.TokenText.startswith("}")120
)) {
3878
98
      return false;
3879
98
    }
3880
    // In tagged template literals ("html`bar baz`"), there is no space between
3881
    // the tag identifier and the template string.
3882
12.1k
    if (Keywords.IsJavaScriptIdentifier(Left,
3883
12.1k
                                        /* AcceptIdentifierName= */ false) &&
3884
12.1k
        
Right.is(TT_TemplateString)3.88k
) {
3885
4
      return false;
3886
4
    }
3887
12.1k
    if (Right.is(tok::star) &&
3888
12.1k
        
Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)19
) {
3889
12
      return false;
3890
12
    }
3891
12.1k
    if (Right.isOneOf(tok::l_brace, tok::l_square) &&
3892
12.1k
        Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
3893
1.10k
                     Keywords.kw_extends, Keywords.kw_implements)) {
3894
20
      return true;
3895
20
    }
3896
12.1k
    if (Right.is(tok::l_paren)) {
3897
      // JS methods can use some keywords as names (e.g. `delete()`).
3898
1.05k
      if (Line.MustBeDeclaration && 
Left.Tok.getIdentifierInfo()87
)
3899
72
        return false;
3900
      // Valid JS method names can include keywords, e.g. `foo.delete()` or
3901
      // `bar.instanceof()`. Recognize call positions by preceding period.
3902
981
      if (Left.Previous && 
Left.Previous->is(tok::period)701
&&
3903
981
          
Left.Tok.getIdentifierInfo()189
) {
3904
187
        return false;
3905
187
      }
3906
      // Additional unary JavaScript operators that need a space after.
3907
794
      if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
3908
794
                       tok::kw_void)) {
3909
8
        return true;
3910
8
      }
3911
794
    }
3912
    // `foo as const;` casts into a const type.
3913
11.8k
    if (Left.endsSequence(tok::kw_const, Keywords.kw_as))
3914
2
      return false;
3915
11.8k
    if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
3916
11.8k
                      tok::kw_const) ||
3917
         // "of" is only a keyword if it appears after another identifier
3918
         // (e.g. as "const x of y" in a for loop), or after a destructuring
3919
         // operation (const [x, y] of z, const {a, b} of c).
3920
11.8k
         
(11.8k
Left.is(Keywords.kw_of)11.8k
&&
Left.Previous12
&&
3921
11.8k
          
(10
Left.Previous->is(tok::identifier)10
||
3922
10
           
Left.Previous->isOneOf(tok::r_square, tok::r_brace)8
))) &&
3923
11.8k
        
(30
!Left.Previous30
||
!Left.Previous->is(tok::period)16
)) {
3924
30
      return true;
3925
30
    }
3926
11.8k
    if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && 
Left.Previous30
&&
3927
11.8k
        
Left.Previous->is(tok::period)12
&&
Right.is(tok::l_paren)2
) {
3928
0
      return false;
3929
0
    }
3930
11.8k
    if (Left.is(Keywords.kw_as) &&
3931
11.8k
        
Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)8
) {
3932
8
      return true;
3933
8
    }
3934
11.8k
    if (Left.is(tok::kw_default) && 
Left.Previous12
&&
3935
11.8k
        
Left.Previous->is(tok::kw_export)10
) {
3936
10
      return true;
3937
10
    }
3938
11.8k
    if (Left.is(Keywords.kw_is) && 
Right.is(tok::l_brace)6
)
3939
4
      return true;
3940
11.8k
    if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
3941
301
      return false;
3942
11.5k
    if (Left.is(TT_JsTypeOperator) || 
Right.is(TT_JsTypeOperator)11.4k
)
3943
112
      return false;
3944
11.4k
    if ((Left.is(tok::l_brace) || 
Right.is(tok::r_brace)10.8k
) &&
3945
11.4k
        
Line.First->isOneOf(Keywords.kw_import, tok::kw_export)902
) {
3946
260
      return false;
3947
260
    }
3948
11.1k
    if (Left.is(tok::ellipsis))
3949
4
      return false;
3950
11.1k
    if (Left.is(TT_TemplateCloser) &&
3951
11.1k
        !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
3952
54
                       Keywords.kw_implements, Keywords.kw_extends)) {
3953
      // Type assertions ('<type>expr') are not followed by whitespace. Other
3954
      // locations that should have whitespace following are identified by the
3955
      // above set of follower tokens.
3956
22
      return false;
3957
22
    }
3958
11.1k
    if (Right.is(TT_NonNullAssertion))
3959
132
      return false;
3960
11.0k
    if (Left.is(TT_NonNullAssertion) &&
3961
11.0k
        
Right.isOneOf(Keywords.kw_as, Keywords.kw_in)130
) {
3962
4
      return true; // "x! as string", "x! in y"
3963
4
    }
3964
11.0k
  } else 
if (3.50k
Style.Language == FormatStyle::LK_Java3.50k
) {
3965
2.81k
    if (Left.is(tok::r_square) && 
Right.is(tok::l_brace)18
)
3966
10
      return true;
3967
2.80k
    if (Left.is(Keywords.kw_synchronized) && 
Right.is(tok::l_paren)6
) {
3968
6
      return Style.SpaceBeforeParensOptions.AfterControlStatements ||
3969
6
             
spaceRequiredBeforeParens(Right)0
;
3970
6
    }
3971
2.79k
    if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
3972
2.79k
                      tok::kw_protected) ||
3973
2.79k
         Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
3974
2.78k
                      Keywords.kw_native)) &&
3975
2.79k
        
Right.is(TT_TemplateOpener)20
) {
3976
14
      return true;
3977
14
    }
3978
2.79k
  } else 
if (693
Style.isVerilog()693
) {
3979
    // Don't add space within a delay like `#0`.
3980
637
    if (!Left.is(TT_BinaryOperator) &&
3981
637
        
Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)562
) {
3982
31
      return false;
3983
31
    }
3984
    // Add space after a delay.
3985
606
    if (!Right.is(tok::semi) &&
3986
606
        
(500
Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash)500
||
3987
500
         Left.endsSequence(tok::numeric_constant,
3988
497
                           Keywords.kw_verilogHashHash) ||
3989
500
         
(497
Left.is(tok::r_paren)497
&&
Left.MatchingParen24
&&
3990
497
          
Left.MatchingParen->endsSequence(tok::l_paren, tok::at)24
))) {
3991
3
      return true;
3992
3
    }
3993
606
  }
3994
252k
  if (Left.is(TT_ImplicitStringLiteral))
3995
588
    return Right.hasWhitespaceBefore();
3996
251k
  if (Line.Type == LT_ObjCMethodDecl) {
3997
2.66k
    if (Left.is(TT_ObjCMethodSpecifier))
3998
283
      return true;
3999
2.38k
    if (Left.is(tok::r_paren) && 
canBeObjCSelectorComponent(Right)524
) {
4000
      // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a
4001
      // keyword in Objective-C, and '+ (instancetype)new;' is a standard class
4002
      // method declaration.
4003
500
      return false;
4004
500
    }
4005
2.38k
  }
4006
250k
  if (Line.Type == LT_ObjCProperty &&
4007
250k
      
(477
Right.is(tok::equal)477
||
Left.is(tok::equal)466
)) {
4008
22
    return false;
4009
22
  }
4010
4011
250k
  if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
4012
250k
      
Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)250k
) {
4013
1.23k
    return true;
4014
1.23k
  }
4015
249k
  if (Left.is(tok::comma) && 
!Right.is(TT_OverloadedOperatorLParen)11.3k
)
4016
11.3k
    return true;
4017
238k
  if (Right.is(tok::comma))
4018
11.7k
    return false;
4019
226k
  if (Right.is(TT_ObjCBlockLParen))
4020
58
    return true;
4021
226k
  if (Right.is(TT_CtorInitializerColon))
4022
548
    return Style.SpaceBeforeCtorInitializerColon;
4023
226k
  if (Right.is(TT_InheritanceColon) && 
!Style.SpaceBeforeInheritanceColon1.14k
)
4024
33
    return false;
4025
226k
  if (Right.is(TT_RangeBasedForLoopColon) &&
4026
226k
      
!Style.SpaceBeforeRangeBasedForLoopColon290
) {
4027
9
    return false;
4028
9
  }
4029
226k
  if (Left.is(TT_BitFieldColon)) {
4030
114
    return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
4031
114
           
Style.BitFieldColonSpacing == FormatStyle::BFCS_After42
;
4032
114
  }
4033
225k
  if (Right.is(tok::colon)) {
4034
6.01k
    if (Line.First->isOneOf(tok::kw_default, tok::kw_case))
4035
472
      return Style.SpaceBeforeCaseColon;
4036
5.54k
    const FormatToken *Next = Right.getNextNonComment();
4037
5.54k
    if (!Next || 
Next->is(tok::semi)4.78k
)
4038
764
      return false;
4039
4.78k
    if (Right.is(TT_ObjCMethodExpr))
4040
993
      return false;
4041
3.78k
    if (Left.is(tok::question))
4042
19
      return false;
4043
3.76k
    if (Right.is(TT_InlineASMColon) && 
Left.is(tok::coloncolon)40
)
4044
3
      return false;
4045
3.76k
    if (Right.is(TT_DictLiteral))
4046
2.14k
      return Style.SpacesInContainerLiterals;
4047
1.61k
    if (Right.is(TT_AttributeColon))
4048
3
      return false;
4049
1.61k
    if (Right.is(TT_CSharpNamedArgumentColon))
4050
14
      return false;
4051
1.60k
    if (Right.is(TT_BitFieldColon)) {
4052
114
      return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
4053
114
             
Style.BitFieldColonSpacing == FormatStyle::BFCS_Before42
;
4054
114
    }
4055
1.48k
    return true;
4056
1.60k
  }
4057
  // Do not merge "- -" into "--".
4058
219k
  if ((Left.isOneOf(tok::minus, tok::minusminus) &&
4059
219k
       
Right.isOneOf(tok::minus, tok::minusminus)516
) ||
4060
219k
      
(219k
Left.isOneOf(tok::plus, tok::plusplus)219k
&&
4061
219k
       
Right.isOneOf(tok::plus, tok::plusplus)1.38k
)) {
4062
9
    return true;
4063
9
  }
4064
219k
  if (Left.is(TT_UnaryOperator)) {
4065
2.06k
    if (!Right.is(tok::l_paren)) {
4066
      // The alternative operators for ~ and ! are "compl" and "not".
4067
      // If they are used instead, we do not want to combine them with
4068
      // the token to the right, unless that is a left paren.
4069
1.90k
      if (Left.is(tok::exclaim) && 
Left.TokenText == "not"161
)
4070
3
        return true;
4071
1.89k
      if (Left.is(tok::tilde) && 
Left.TokenText == "compl"109
)
4072
3
        return true;
4073
      // Lambda captures allow for a lone &, so "&]" needs to be properly
4074
      // handled.
4075
1.89k
      if (Left.is(tok::amp) && 
Right.is(tok::r_square)347
)
4076
63
        return Style.SpacesInSquareBrackets;
4077
1.89k
    }
4078
1.99k
    return (Style.SpaceAfterLogicalNot && 
Left.is(tok::exclaim)15
) ||
4079
1.99k
           
Right.is(TT_BinaryOperator)1.98k
;
4080
2.06k
  }
4081
4082
  // If the next token is a binary operator or a selector name, we have
4083
  // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
4084
217k
  if (Left.is(TT_CastRParen)) {
4085
530
    return Style.SpaceAfterCStyleCast ||
4086
530
           
Right.isOneOf(TT_BinaryOperator, TT_SelectorName)442
;
4087
530
  }
4088
4089
217k
  auto ShouldAddSpacesInAngles = [this, &Right]() {
4090
10.9k
    if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
4091
117
      return true;
4092
10.7k
    if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
4093
129
      return Right.hasWhitespaceBefore();
4094
10.6k
    return false;
4095
10.7k
  };
4096
4097
217k
  if (Left.is(tok::greater) && 
Right.is(tok::greater)5.83k
) {
4098
481
    if (Style.Language == FormatStyle::LK_TextProto ||
4099
481
        
(449
Style.Language == FormatStyle::LK_Proto449
&&
Left.is(TT_DictLiteral)4
)) {
4100
36
      return !Style.Cpp11BracedListStyle;
4101
36
    }
4102
445
    return Right.is(TT_TemplateCloser) && 
Left.is(TT_TemplateCloser)351
&&
4103
445
           
(351
(Style.Standard < FormatStyle::LS_Cpp11)351
||
4104
351
            
ShouldAddSpacesInAngles()315
);
4105
481
  }
4106
216k
  if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
4107
216k
      
Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar)216k
||
4108
216k
      
(213k
Right.is(tok::period)213k
&&
Right.isNot(TT_DesignatedInitializerPeriod)2.21k
)) {
4109
5.26k
    return false;
4110
5.26k
  }
4111
211k
  if (!Style.SpaceBeforeAssignmentOperators && 
Left.isNot(TT_TemplateCloser)96
&&
4112
211k
      
Right.getPrecedence() == prec::Assignment87
) {
4113
13
    return false;
4114
13
  }
4115
211k
  if (Style.Language == FormatStyle::LK_Java && 
Right.is(tok::coloncolon)2.06k
&&
4116
211k
      
(4
Left.is(tok::identifier)4
||
Left.is(tok::kw_this)1
)) {
4117
4
    return false;
4118
4
  }
4119
211k
  if (Right.is(tok::coloncolon) && 
Left.is(tok::identifier)3.73k
) {
4120
    // Generally don't remove existing spaces between an identifier and "::".
4121
    // The identifier might actually be a macro name such as ALWAYS_INLINE. If
4122
    // this turns out to be too lenient, add analysis of the identifier itself.
4123
3.34k
    return Right.hasWhitespaceBefore();
4124
3.34k
  }
4125
208k
  if (Right.is(tok::coloncolon) &&
4126
208k
      
!Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)382
) {
4127
    // Put a space between < and :: in vector< ::std::string >
4128
347
    return (Left.is(TT_TemplateOpener) &&
4129
347
            
(50
(Style.Standard < FormatStyle::LS_Cpp11)50
||
4130
50
             
ShouldAddSpacesInAngles()35
)) ||
4131
347
           !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
4132
320
                          tok::kw___super, TT_TemplateOpener,
4133
320
                          TT_TemplateCloser)) ||
4134
347
           
(266
Left.is(tok::l_paren)266
&&
Style.SpacesInParentheses0
);
4135
347
  }
4136
207k
  if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
4137
10.5k
    return ShouldAddSpacesInAngles();
4138
  // Space before TT_StructuredBindingLSquare.
4139
197k
  if (Right.is(TT_StructuredBindingLSquare)) {
4140
85
    return !Left.isOneOf(tok::amp, tok::ampamp) ||
4141
85
           
getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right51
;
4142
85
  }
4143
  // Space before & or && following a TT_StructuredBindingLSquare.
4144
197k
  if (Right.Next && 
Right.Next->is(TT_StructuredBindingLSquare)151k
&&
4145
197k
      
Right.isOneOf(tok::amp, tok::ampamp)53
) {
4146
51
    return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
4147
51
  }
4148
197k
  if ((Right.is(TT_BinaryOperator) && 
!Left.is(tok::l_paren)12.7k
) ||
4149
197k
      
(184k
Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr)184k
&&
4150
184k
       
!Right.is(tok::r_paren)14.1k
)) {
4151
26.9k
    return true;
4152
26.9k
  }
4153
170k
  if (Right.is(TT_TemplateOpener) && 
Left.is(tok::r_paren)5.35k
&&
4154
170k
      
Left.MatchingParen27
&&
4155
170k
      
Left.MatchingParen->is(TT_OverloadedOperatorLParen)27
) {
4156
9
    return false;
4157
9
  }
4158
170k
  if (Right.is(tok::less) && 
Left.isNot(tok::l_paren)5.49k
&&
4159
170k
      
Line.startsWith(tok::hash)5.48k
) {
4160
0
    return true;
4161
0
  }
4162
170k
  if (Right.is(TT_TrailingUnaryOperator))
4163
199
    return false;
4164
169k
  if (Left.is(TT_RegexLiteral))
4165
132
    return false;
4166
169k
  return spaceRequiredBetween(Line, Left, Right);
4167
169k
}
4168
4169
// Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
4170
579k
static bool isAllmanBrace(const FormatToken &Tok) {
4171
579k
  return Tok.is(tok::l_brace) && 
Tok.is(BK_Block)22.0k
&&
4172
579k
         
!Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral)16.1k
;
4173
579k
}
4174
4175
// Returns 'true' if 'Tok' is a function argument.
4176
920
static bool IsFunctionArgument(const FormatToken &Tok) {
4177
920
  return Tok.MatchingParen && Tok.MatchingParen->Next &&
4178
920
         
Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren)839
;
4179
920
}
4180
4181
static bool
4182
isItAnEmptyLambdaAllowed(const FormatToken &Tok,
4183
189
                         FormatStyle::ShortLambdaStyle ShortLambdaOption) {
4184
189
  return Tok.Children.empty() && 
ShortLambdaOption != FormatStyle::SLS_None24
;
4185
189
}
4186
4187
378
static bool isAllmanLambdaBrace(const FormatToken &Tok) {
4188
378
  return Tok.is(tok::l_brace) && 
Tok.is(BK_Block)189
&&
4189
378
         
!Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral)189
;
4190
378
}
4191
4192
// Returns the first token on the line that is not a comment.
4193
13.8k
static const FormatToken *getFirstNonComment(const AnnotatedLine &Line) {
4194
13.8k
  const FormatToken *Next = Line.First;
4195
13.8k
  if (!Next)
4196
0
    return Next;
4197
13.8k
  if (Next->is(tok::comment))
4198
45
    Next = Next->getNextNonComment();
4199
13.8k
  return Next;
4200
13.8k
}
4201
4202
bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
4203
298k
                                     const FormatToken &Right) const {
4204
298k
  const FormatToken &Left = *Right.Previous;
4205
298k
  if (Right.NewlinesBefore > 1 && 
Style.MaxEmptyLinesToKeep > 069
)
4206
66
    return true;
4207
4208
298k
  if (Style.isCSharp()) {
4209
6.28k
    if (Left.is(TT_FatArrow) && 
Right.is(tok::l_brace)90
&&
4210
6.28k
        
Style.BraceWrapping.AfterFunction28
) {
4211
20
      return true;
4212
20
    }
4213
6.26k
    if (Right.is(TT_CSharpNamedArgumentColon) ||
4214
6.26k
        
Left.is(TT_CSharpNamedArgumentColon)6.25k
) {
4215
28
      return false;
4216
28
    }
4217
6.23k
    if (Right.is(TT_CSharpGenericTypeConstraint))
4218
0
      return true;
4219
6.23k
    if (Right.Next && 
Right.Next->is(TT_FatArrow)5.09k
&&
4220
6.23k
        
(70
Right.is(tok::numeric_constant)70
||
4221
70
         
(66
Right.is(tok::identifier)66
&&
Right.TokenText == "_"34
))) {
4222
6
      return true;
4223
6
    }
4224
4225
    // Break after C# [...] and before public/protected/private/internal.
4226
6.23k
    if (Left.is(TT_AttributeSquare) && 
Left.is(tok::r_square)111
&&
4227
6.23k
        
(20
Right.isAccessSpecifier(/*ColonRequired=*/false)20
||
4228
20
         
Right.is(Keywords.kw_internal)12
)) {
4229
10
      return true;
4230
10
    }
4231
    // Break between ] and [ but only when there are really 2 attributes.
4232
6.22k
    if (Left.is(TT_AttributeSquare) && 
Right.is(TT_AttributeSquare)101
&&
4233
6.22k
        
Left.is(tok::r_square)6
&&
Right.is(tok::l_square)6
) {
4234
6
      return true;
4235
6
    }
4236
4237
292k
  } else if (Style.isJavaScript()) {
4238
    // FIXME: This might apply to other languages and token kinds.
4239
14.0k
    if (Right.is(tok::string_literal) && 
Left.is(tok::plus)560
&&
Left.Previous4
&&
4240
14.0k
        
Left.Previous->is(tok::string_literal)4
) {
4241
2
      return true;
4242
2
    }
4243
14.0k
    if (Left.is(TT_DictLiteral) && 
Left.is(tok::l_brace)477
&&
Line.Level == 0255
&&
4244
14.0k
        
Left.Previous237
&&
Left.Previous->is(tok::equal)237
&&
4245
14.0k
        Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
4246
86
                            tok::kw_const) &&
4247
        // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
4248
        // above.
4249
14.0k
        
!Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)86
) {
4250
      // Object literals on the top level of a file are treated as "enum-style".
4251
      // Each key/value pair is put on a separate line, instead of bin-packing.
4252
26
      return true;
4253
26
    }
4254
14.0k
    if (Left.is(tok::l_brace) && 
Line.Level == 0522
&&
4255
14.0k
        
(479
Line.startsWith(tok::kw_enum)479
||
4256
479
         
Line.startsWith(tok::kw_const, tok::kw_enum)475
||
4257
479
         
Line.startsWith(tok::kw_export, tok::kw_enum)473
||
4258
479
         
Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum)469
)) {
4259
      // JavaScript top-level enum key/value pairs are put on separate lines
4260
      // instead of bin-packing.
4261
12
      return true;
4262
12
    }
4263
14.0k
    if (Right.is(tok::r_brace) && 
Left.is(tok::l_brace)548
&&
Left.Previous193
&&
4264
14.0k
        
Left.Previous->is(TT_FatArrow)193
) {
4265
      // JS arrow function (=> {...}).
4266
40
      switch (Style.AllowShortLambdasOnASingleLine) {
4267
4
      case FormatStyle::SLS_All:
4268
4
        return false;
4269
4
      case FormatStyle::SLS_None:
4270
4
        return true;
4271
26
      case FormatStyle::SLS_Empty:
4272
26
        return !Left.Children.empty();
4273
6
      case FormatStyle::SLS_Inline:
4274
        // allow one-lining inline (e.g. in function call args) and empty arrow
4275
        // functions.
4276
6
        return (Left.NestingLevel == 0 && 
Line.Level == 04
) &&
4277
6
               
!Left.Children.empty()4
;
4278
40
      }
4279
0
      llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum");
4280
0
    }
4281
4282
14.0k
    if (Right.is(tok::r_brace) && 
Left.is(tok::l_brace)508
&&
4283
14.0k
        
!Left.Children.empty()153
) {
4284
      // Support AllowShortFunctionsOnASingleLine for JavaScript.
4285
116
      return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
4286
116
             
Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty108
||
4287
116
             
(34
Left.NestingLevel == 034
&&
Line.Level == 012
&&
4288
34
              Style.AllowShortFunctionsOnASingleLine &
4289
8
                  FormatStyle::SFS_InlineOnly);
4290
116
    }
4291
278k
  } else if (Style.Language == FormatStyle::LK_Java) {
4292
3.52k
    if (Right.is(tok::plus) && 
Left.is(tok::string_literal)8
&&
Right.Next4
&&
4293
3.52k
        
Right.Next->is(tok::string_literal)4
) {
4294
4
      return true;
4295
4
    }
4296
274k
  } else if (Style.Language == FormatStyle::LK_Cpp ||
4297
274k
             
Style.Language == FormatStyle::LK_ObjC86.3k
||
4298
274k
             
Style.Language == FormatStyle::LK_Proto10.6k
||
4299
274k
             
Style.Language == FormatStyle::LK_TableGen6.68k
||
4300
274k
             
Style.Language == FormatStyle::LK_TextProto6.62k
) {
4301
273k
    if (Left.isStringLiteral() && 
Right.isStringLiteral()1.85k
)
4302
174
      return true;
4303
273k
  }
4304
4305
  // Basic JSON newline processing.
4306
297k
  if (Style.isJson()) {
4307
    // Always break after a JSON record opener.
4308
    // {
4309
    // }
4310
449
    if (Left.is(TT_DictLiteral) && 
Left.is(tok::l_brace)112
)
4311
43
      return true;
4312
    // Always break after a JSON array opener.
4313
    // [
4314
    // ]
4315
406
    if (Left.is(TT_ArrayInitializerLSquare) && 
Left.is(tok::l_square)24
&&
4316
406
        
!Right.is(tok::r_square)24
) {
4317
20
      return true;
4318
20
    }
4319
    // Always break after successive entries.
4320
    // 1,
4321
    // 2
4322
386
    if (Left.is(tok::comma))
4323
46
      return true;
4324
386
  }
4325
4326
  // If the last token before a '}', ']', or ')' is a comma or a trailing
4327
  // comment, the intention is to insert a line break after it in order to make
4328
  // shuffling around entries easier. Import statements, especially in
4329
  // JavaScript, can be an exception to this rule.
4330
297k
  if (Style.JavaScriptWrapImports || 
Line.Type != LT_ImportStatement13.6k
) {
4331
296k
    const FormatToken *BeforeClosingBrace = nullptr;
4332
296k
    if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
4333
296k
         
(291k
Style.isJavaScript()291k
&&
Left.is(tok::l_paren)12.6k
)) &&
4334
296k
        
Left.isNot(BK_Block)6.82k
&&
Left.MatchingParen4.38k
) {
4335
4.37k
      BeforeClosingBrace = Left.MatchingParen->Previous;
4336
292k
    } else if (Right.MatchingParen &&
4337
292k
               
(85.8k
Right.MatchingParen->isOneOf(tok::l_brace,
4338
85.8k
                                             TT_ArrayInitializerLSquare) ||
4339
85.8k
                
(80.5k
Style.isJavaScript()80.5k
&&
4340
80.5k
                 
Right.MatchingParen->is(tok::l_paren)2.33k
))) {
4341
5.85k
      BeforeClosingBrace = &Left;
4342
5.85k
    }
4343
296k
    if (BeforeClosingBrace && 
(10.2k
BeforeClosingBrace->is(tok::comma)10.2k
||
4344
10.2k
                               
BeforeClosingBrace->isTrailingComment()9.81k
)) {
4345
575
      return true;
4346
575
    }
4347
296k
  }
4348
4349
297k
  if (Right.is(tok::comment)) {
4350
3.76k
    return Left.isNot(BK_BracedInit) && 
Left.isNot(TT_CtorInitializerColon)3.74k
&&
4351
3.76k
           
(3.72k
Right.NewlinesBefore > 03.72k
&&
Right.HasUnescapedNewline1.02k
);
4352
3.76k
  }
4353
293k
  if (Left.isTrailingComment())
4354
753
    return true;
4355
292k
  if (Left.IsUnterminatedLiteral)
4356
2
    return true;
4357
292k
  if (Right.is(tok::lessless) && 
Right.Next422
&&
Left.is(tok::string_literal)416
&&
4358
292k
      
Right.Next->is(tok::string_literal)104
) {
4359
19
    return true;
4360
19
  }
4361
292k
  if (Right.is(TT_RequiresClause)) {
4362
264
    switch (Style.RequiresClausePosition) {
4363
123
    case FormatStyle::RCPS_OwnLine:
4364
180
    case FormatStyle::RCPS_WithFollowing:
4365
180
      return true;
4366
84
    default:
4367
84
      break;
4368
264
    }
4369
264
  }
4370
  // Can break after template<> declaration
4371
292k
  if (Left.ClosesTemplateDeclaration && 
Left.MatchingParen1.47k
&&
4372
292k
      
Left.MatchingParen->NestingLevel == 01.47k
) {
4373
    // Put concepts on the next line e.g.
4374
    // template<typename T>
4375
    // concept ...
4376
1.43k
    if (Right.is(tok::kw_concept))
4377
209
      return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
4378
1.22k
    return Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes;
4379
1.43k
  }
4380
291k
  if (Left.ClosesRequiresClause && 
Right.isNot(tok::semi)264
) {
4381
245
    switch (Style.RequiresClausePosition) {
4382
113
    case FormatStyle::RCPS_OwnLine:
4383
140
    case FormatStyle::RCPS_WithPreceding:
4384
140
      return true;
4385
105
    default:
4386
105
      break;
4387
245
    }
4388
245
  }
4389
290k
  if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
4390
471
    if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
4391
471
        (Left.is(TT_CtorInitializerComma) ||
4392
471
         
Right.is(TT_CtorInitializerColon)462
)) {
4393
21
      return true;
4394
21
    }
4395
4396
450
    if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
4397
450
        
Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)0
) {
4398
0
      return true;
4399
0
    }
4400
450
  }
4401
290k
  if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
4402
290k
      
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma248k
&&
4403
290k
      
Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)2.03k
) {
4404
112
    return true;
4405
112
  }
4406
  // Break only if we have multiple inheritance.
4407
290k
  if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
4408
290k
      
Right.is(TT_InheritanceComma)200
) {
4409
12
    return true;
4410
12
  }
4411
290k
  if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
4412
290k
      
Left.is(TT_InheritanceComma)180
) {
4413
18
    return true;
4414
18
  }
4415
290k
  if (Right.is(tok::string_literal) && 
Right.TokenText.startswith("R\"")3.45k
) {
4416
    // Multiline raw string literals are special wrt. line breaks. The author
4417
    // has made a deliberate choice and might have aligned the contents of the
4418
    // string literal accordingly. Thus, we try keep existing line breaks.
4419
123
    return Right.IsMultiline && 
Right.NewlinesBefore > 036
;
4420
123
  }
4421
290k
  if ((Left.is(tok::l_brace) || 
(285k
Left.is(tok::less)285k
&&
Left.Previous6.34k
&&
4422
285k
                                 
Left.Previous->is(tok::equal)6.33k
)) &&
4423
290k
      
Right.NestingLevel == 15.23k
&&
Style.Language == FormatStyle::LK_Proto3.04k
) {
4424
    // Don't put enums or option definitions onto single lines in protocol
4425
    // buffers.
4426
98
    return true;
4427
98
  }
4428
290k
  if (Right.is(TT_InlineASMBrace))
4429
7
    return Right.HasUnescapedNewline;
4430
4431
290k
  if (isAllmanBrace(Left) || 
isAllmanBrace(Right)289k
) {
4432
13.8k
    auto FirstNonComment = getFirstNonComment(Line);
4433
13.8k
    bool AccessSpecifier =
4434
13.8k
        FirstNonComment &&
4435
13.8k
        FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,
4436
13.8k
                                 tok::kw_private, tok::kw_protected);
4437
4438
13.8k
    if (Style.BraceWrapping.AfterEnum) {
4439
100
      if (Line.startsWith(tok::kw_enum) ||
4440
100
          
Line.startsWith(tok::kw_typedef, tok::kw_enum)46
) {
4441
60
        return true;
4442
60
      }
4443
      // Ensure BraceWrapping for `public enum A {`.
4444
40
      if (AccessSpecifier && 
FirstNonComment->Next10
&&
4445
40
          
FirstNonComment->Next->is(tok::kw_enum)10
) {
4446
0
        return true;
4447
0
      }
4448
40
    }
4449
4450
    // Ensure BraceWrapping for `public interface A {`.
4451
13.8k
    if (Style.BraceWrapping.AfterClass &&
4452
13.8k
        
(84
(84
AccessSpecifier84
&&
FirstNonComment->Next22
&&
4453
84
          
FirstNonComment->Next->is(Keywords.kw_interface)22
) ||
4454
84
         
Line.startsWith(Keywords.kw_interface)62
)) {
4455
24
      return true;
4456
24
    }
4457
4458
13.7k
    return (Line.startsWith(tok::kw_class) && 
Style.BraceWrapping.AfterClass1.04k
) ||
4459
13.7k
           (Line.startsWith(tok::kw_struct) && 
Style.BraceWrapping.AfterStruct858
);
4460
13.8k
  }
4461
4462
276k
  if (Left.is(TT_ObjCBlockLBrace) &&
4463
276k
      
Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never153
) {
4464
134
    return true;
4465
134
  }
4466
4467
  // Ensure wrapping after __attribute__((XX)) and @interface etc.
4468
276k
  if (Left.is(TT_AttributeParen) && 
Right.is(TT_ObjCDecl)224
)
4469
6
    return true;
4470
4471
276k
  if (Left.is(TT_LambdaLBrace)) {
4472
920
    if (IsFunctionArgument(Left) &&
4473
920
        
Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline306
) {
4474
30
      return false;
4475
30
    }
4476
4477
890
    if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
4478
890
        
Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline845
||
4479
890
        
(839
!Left.Children.empty()839
&&
4480
839
         
Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty625
)) {
4481
72
      return true;
4482
72
    }
4483
890
  }
4484
4485
276k
  if (Style.BraceWrapping.BeforeLambdaBody && 
Right.is(TT_LambdaLBrace)5.63k
&&
4486
276k
      
Left.isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser)198
) {
4487
9
    return true;
4488
9
  }
4489
4490
  // Put multiple Java annotation on a new line.
4491
276k
  if ((Style.Language == FormatStyle::LK_Java || 
Style.isJavaScript()273k
) &&
4492
276k
      
Left.is(TT_LeadingJavaAnnotation)16.0k
&&
4493
276k
      
Right.isNot(TT_LeadingJavaAnnotation)152
&&
Right.isNot(tok::l_paren)120
&&
4494
276k
      
(86
Line.Last->is(tok::l_brace)86
||
Style.BreakAfterJavaFieldAnnotations28
)) {
4495
62
    return true;
4496
62
  }
4497
4498
276k
  if (Right.is(TT_ProtoExtensionLSquare))
4499
54
    return true;
4500
4501
  // In text proto instances if a submessage contains at least 2 entries and at
4502
  // least one of them is a submessage, like A { ... B { ... } ... },
4503
  // put all of the entries of A on separate lines by forcing the selector of
4504
  // the submessage B to be put on a newline.
4505
  //
4506
  // Example: these can stay on one line:
4507
  // a { scalar_1: 1 scalar_2: 2 }
4508
  // a { b { key: value } }
4509
  //
4510
  // and these entries need to be on a new line even if putting them all in one
4511
  // line is under the column limit:
4512
  // a {
4513
  //   scalar: 1
4514
  //   b { key: value }
4515
  // }
4516
  //
4517
  // We enforce this by breaking before a submessage field that has previous
4518
  // siblings, *and* breaking before a field that follows a submessage field.
4519
  //
4520
  // Be careful to exclude the case  [proto.ext] { ... } since the `]` is
4521
  // the TT_SelectorName there, but we don't want to break inside the brackets.
4522
  //
4523
  // Another edge case is @submessage { key: value }, which is a common
4524
  // substitution placeholder. In this case we want to keep `@` and `submessage`
4525
  // together.
4526
  //
4527
  // We ensure elsewhere that extensions are always on their own line.
4528
276k
  if ((Style.Language == FormatStyle::LK_Proto ||
4529
276k
       
Style.Language == FormatStyle::LK_TextProto272k
) &&
4530
276k
      
Right.is(TT_SelectorName)9.11k
&&
!Right.is(tok::r_square)1.48k
&&
Right.Next1.40k
) {
4531
    // Keep `@submessage` together in:
4532
    // @submessage { key: value }
4533
1.40k
    if (Left.is(tok::at))
4534
12
      return false;
4535
    // Look for the scope opener after selector in cases like:
4536
    // selector { ...
4537
    // selector: { ...
4538
    // selector: @base { ...
4539
1.39k
    FormatToken *LBrace = Right.Next;
4540
1.39k
    if (LBrace && LBrace->is(tok::colon)) {
4541
1.20k
      LBrace = LBrace->Next;
4542
1.20k
      if (LBrace && LBrace->is(tok::at)) {
4543
2
        LBrace = LBrace->Next;
4544
2
        if (LBrace)
4545
2
          LBrace = LBrace->Next;
4546
2
      }
4547
1.20k
    }
4548
1.39k
    if (LBrace &&
4549
        // The scope opener is one of {, [, <:
4550
        // selector { ... }
4551
        // selector [ ... ]
4552
        // selector < ... >
4553
        //
4554
        // In case of selector { ... }, the l_brace is TT_DictLiteral.
4555
        // In case of an empty selector {}, the l_brace is not TT_DictLiteral,
4556
        // so we check for immediately following r_brace.
4557
1.39k
        ((LBrace->is(tok::l_brace) &&
4558
1.39k
          
(229
LBrace->is(TT_DictLiteral)229
||
4559
229
           
(60
LBrace->Next60
&&
LBrace->Next->is(tok::r_brace)60
))) ||
4560
1.39k
         
LBrace->is(TT_ArrayInitializerLSquare)1.18k
||
LBrace->is(tok::less)1.14k
)) {
4561
      // If Left.ParameterCount is 0, then this submessage entry is not the
4562
      // first in its parent submessage, and we want to break before this entry.
4563
      // If Left.ParameterCount is greater than 0, then its parent submessage
4564
      // might contain 1 or more entries and we want to break before this entry
4565
      // if it contains at least 2 entries. We deal with this case later by
4566
      // detecting and breaking before the next entry in the parent submessage.
4567
428
      if (Left.ParameterCount == 0)
4568
323
        return true;
4569
      // However, if this submessage is the first entry in its parent
4570
      // submessage, Left.ParameterCount might be 1 in some cases.
4571
      // We deal with this case later by detecting an entry
4572
      // following a closing paren of this submessage.
4573
428
    }
4574
4575
    // If this is an entry immediately following a submessage, it will be
4576
    // preceded by a closing paren of that submessage, like in:
4577
    //     left---.  .---right
4578
    //            v  v
4579
    // sub: { ... } key: value
4580
    // If there was a comment between `}` an `key` above, then `key` would be
4581
    // put on a new line anyways.
4582
1.07k
    if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
4583
105
      return true;
4584
1.07k
  }
4585
4586
  // Deal with lambda arguments in C++ - we want consistent line breaks whether
4587
  // they happen to be at arg0, arg1 or argN. The selection is a bit nuanced
4588
  // as aggressive line breaks are placed when the lambda is not the last arg.
4589
275k
  if ((Style.Language == FormatStyle::LK_Cpp ||
4590
275k
       
Style.Language == FormatStyle::LK_ObjC103k
) &&
4591
275k
      
Left.is(tok::l_paren)244k
&&
Left.BlockParameterCount > 026.6k
&&
4592
275k
      
!Right.isOneOf(tok::l_paren, TT_LambdaLSquare)445
) {
4593
    // Multiple lambdas in the same function call force line breaks.
4594
199
    if (Left.BlockParameterCount > 1)
4595
6
      return true;
4596
4597
    // A lambda followed by another arg forces a line break.
4598
193
    if (!Left.Role)
4599
52
      return false;
4600
141
    auto Comma = Left.Role->lastComma();
4601
141
    if (!Comma)
4602
0
      return false;
4603
141
    auto Next = Comma->getNextNonComment();
4604
141
    if (!Next)
4605
0
      return false;
4606
141
    if (!Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret))
4607
15
      return true;
4608
141
  }
4609
4610
275k
  return false;
4611
275k
}
4612
4613
bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
4614
293k
                                    const FormatToken &Right) const {
4615
293k
  const FormatToken &Left = *Right.Previous;
4616
  // Language-specific stuff.
4617
293k
  if (Style.isCSharp()) {
4618
6.19k
    if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
4619
6.19k
        
Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)6.17k
) {
4620
32
      return false;
4621
32
    }
4622
    // Only break after commas for generic type constraints.
4623
6.16k
    if (Line.First->is(TT_CSharpGenericTypeConstraint))
4624
132
      return Left.is(TT_CSharpGenericTypeConstraintComma);
4625
    // Keep nullable operators attached to their identifiers.
4626
6.03k
    if (Right.is(TT_CSharpNullable))
4627
20
      return false;
4628
287k
  } else if (Style.Language == FormatStyle::LK_Java) {
4629
3.45k
    if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
4630
3.45k
                     Keywords.kw_implements)) {
4631
42
      return false;
4632
42
    }
4633
3.40k
    if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
4634
3.40k
                      Keywords.kw_implements)) {
4635
42
      return true;
4636
42
    }
4637
283k
  } else if (Style.isJavaScript()) {
4638
13.6k
    const FormatToken *NonComment = Right.getPreviousNonComment();
4639
13.6k
    if (NonComment &&
4640
13.6k
        NonComment->isOneOf(
4641
13.6k
            tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
4642
13.6k
            tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
4643
13.6k
            tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
4644
13.6k
            Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
4645
13.6k
            Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
4646
13.6k
            Keywords.kw_await)) {
4647
394
      return false; // Otherwise automatic semicolon insertion would trigger.
4648
394
    }
4649
13.3k
    if (Right.NestingLevel == 0 &&
4650
13.3k
        
(7.94k
Left.Tok.getIdentifierInfo()7.94k
||
4651
7.94k
         
Left.isOneOf(tok::r_square, tok::r_paren)3.80k
) &&
4652
13.3k
        
Right.isOneOf(tok::l_square, tok::l_paren)5.08k
) {
4653
788
      return false; // Otherwise automatic semicolon insertion would trigger.
4654
788
    }
4655
12.5k
    if (NonComment && 
NonComment->is(tok::identifier)12.5k
&&
4656
12.5k
        
NonComment->TokenText == "asserts"4.59k
) {
4657
2
      return false;
4658
2
    }
4659
12.5k
    if (Left.is(TT_FatArrow) && 
Right.is(tok::l_brace)77
)
4660
43
      return false;
4661
12.4k
    if (Left.is(TT_JsTypeColon))
4662
275
      return true;
4663
    // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
4664
12.1k
    if (Left.is(tok::exclaim) && 
Right.is(tok::colon)146
)
4665
4
      return false;
4666
    // Look for is type annotations like:
4667
    // function f(): a is B { ... }
4668
    // Do not break before is in these cases.
4669
12.1k
    if (Right.is(Keywords.kw_is)) {
4670
8
      const FormatToken *Next = Right.getNextNonComment();
4671
      // If `is` is followed by a colon, it's likely that it's a dict key, so
4672
      // ignore it for this check.
4673
      // For example this is common in Polymer:
4674
      // Polymer({
4675
      //   is: 'name',
4676
      //   ...
4677
      // });
4678
8
      if (!Next || !Next->is(tok::colon))
4679
6
        return false;
4680
8
    }
4681
12.1k
    if (Left.is(Keywords.kw_in))
4682
18
      return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
4683
12.1k
    if (Right.is(Keywords.kw_in))
4684
20
      return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
4685
12.1k
    if (Right.is(Keywords.kw_as))
4686
53
      return false; // must not break before as in 'x as type' casts
4687
12.0k
    if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
4688
      // extends and infer can appear as keywords in conditional types:
4689
      //   https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
4690
      // do not break before them, as the expressions are subject to ASI.
4691
32
      return false;
4692
32
    }
4693
12.0k
    if (Left.is(Keywords.kw_as))
4694
45
      return true;
4695
12.0k
    if (Left.is(TT_NonNullAssertion))
4696
124
      return true;
4697
11.8k
    if (Left.is(Keywords.kw_declare) &&
4698
11.8k
        Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
4699
28
                      Keywords.kw_function, tok::kw_class, tok::kw_enum,
4700
28
                      Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
4701
28
                      Keywords.kw_let, tok::kw_const)) {
4702
      // See grammar for 'declare' statements at:
4703
      // https://github.com/Microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md#A.10
4704
24
      return false;
4705
24
    }
4706
11.8k
    if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
4707
11.8k
        
Right.isOneOf(tok::identifier, tok::string_literal)18
) {
4708
12
      return false; // must not break in "module foo { ...}"
4709
12
    }
4710
11.8k
    if (Right.is(TT_TemplateString) && 
Right.closesScope()122
)
4711
50
      return false;
4712
    // Don't split tagged template literal so there is a break between the tag
4713
    // identifier and template string.
4714
11.8k
    if (Left.is(tok::identifier) && 
Right.is(TT_TemplateString)4.40k
)
4715
4
      return false;
4716
11.8k
    if (Left.is(TT_TemplateString) && 
Left.opensScope()122
)
4717
48
      return true;
4718
11.8k
  }
4719
4720
291k
  if (Left.is(tok::at))
4721
881
    return false;
4722
290k
  if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
4723
157
    return false;
4724
290k
  if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
4725
147
    return !Right.is(tok::l_paren);
4726
290k
  if (Right.is(TT_PointerOrReference)) {
4727
5.31k
    return Line.IsMultiVariableDeclStmt ||
4728
5.31k
           
(5.08k
getTokenPointerOrReferenceAlignment(Right) ==
4729
5.08k
                FormatStyle::PAS_Right &&
4730
5.08k
            
(3.85k
!Right.Next3.85k
||
Right.Next->isNot(TT_FunctionDeclarationName)3.85k
));
4731
5.31k
  }
4732
284k
  if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
4733
284k
      
Right.is(tok::kw_operator)264k
) {
4734
21.2k
    return true;
4735
21.2k
  }
4736
263k
  if (Left.is(TT_PointerOrReference))
4737
2.08k
    return false;
4738
261k
  if (Right.isTrailingComment()) {
4739
    // We rely on MustBreakBefore being set correctly here as we should not
4740
    // change the "binding" behavior of a comment.
4741
    // The first comment in a braced lists is always interpreted as belonging to
4742
    // the first list element. Otherwise, it should be placed outside of the
4743
    // list.
4744
2.44k
    return Left.is(BK_BracedInit) ||
4745
2.44k
           
(2.41k
Left.is(TT_CtorInitializerColon)2.41k
&&
Right.NewlinesBefore > 016
&&
4746
2.41k
            
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon4
);
4747
2.44k
  }
4748
259k
  if (Left.is(tok::question) && 
Right.is(tok::colon)843
)
4749
37
    return false;
4750
259k
  if (Right.is(TT_ConditionalExpr) || 
Right.is(tok::question)257k
)
4751
1.58k
    return Style.BreakBeforeTernaryOperators;
4752
257k
  if (Left.is(TT_ConditionalExpr) || 
Left.is(tok::question)255k
)
4753
1.60k
    return !Style.BreakBeforeTernaryOperators;
4754
255k
  if (Left.is(TT_InheritanceColon))
4755
377
    return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
4756
255k
  if (Right.is(TT_InheritanceColon))
4757
1.14k
    return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
4758
254k
  if (Right.is(TT_ObjCMethodExpr) && 
!Right.is(tok::r_square)1.94k
&&
4759
254k
      
Left.isNot(TT_SelectorName)1.32k
) {
4760
387
    return true;
4761
387
  }
4762
4763
253k
  if (Right.is(tok::colon) &&
4764
253k
      
!Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)4.89k
) {
4765
4.38k
    return false;
4766
4.38k
  }
4767
249k
  if (Left.is(tok::colon) && 
Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)4.37k
) {
4768
3.18k
    if (Style.Language == FormatStyle::LK_Proto ||
4769
3.18k
        
Style.Language == FormatStyle::LK_TextProto2.68k
) {
4770
1.84k
      if (!Style.AlwaysBreakBeforeMultilineStrings && 
Right.isStringLiteral()1.84k
)
4771
269
        return false;
4772
      // Prevent cases like:
4773
      //
4774
      // submessage:
4775
      //     { key: valueeeeeeeeeeee }
4776
      //
4777
      // when the snippet does not fit into one line.
4778
      // Prefer:
4779
      //
4780
      // submessage: {
4781
      //   key: valueeeeeeeeeeee
4782
      // }
4783
      //
4784
      // instead, even if it is longer by one line.
4785
      //
4786
      // Note that this allows allows the "{" to go over the column limit
4787
      // when the column limit is just between ":" and "{", but that does
4788
      // not happen too often and alternative formattings in this case are
4789
      // not much better.
4790
      //
4791
      // The code covers the cases:
4792
      //
4793
      // submessage: { ... }
4794
      // submessage: < ... >
4795
      // repeated: [ ... ]
4796
1.57k
      if (((Right.is(tok::l_brace) || 
Right.is(tok::less)1.39k
) &&
4797
1.57k
           
Right.is(TT_DictLiteral)311
) ||
4798
1.57k
          
Right.is(TT_ArrayInitializerLSquare)1.30k
) {
4799
330
        return false;
4800
330
      }
4801
1.57k
    }
4802
2.58k
    return true;
4803
3.18k
  }
4804
246k
  if (Right.is(tok::r_square) && 
Right.MatchingParen3.61k
&&
4805
246k
      
Right.MatchingParen->is(TT_ProtoExtensionLSquare)3.61k
) {
4806
78
    return false;
4807
78
  }
4808
246k
  if (Right.is(TT_SelectorName) || 
(244k
Right.is(tok::identifier)244k
&&
Right.Next53.9k
&&
4809
244k
                                    
Right.Next->is(TT_ObjCMethodExpr)51.6k
)) {
4810
2.30k
    return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
4811
2.30k
  }
4812
244k
  if (Left.is(tok::r_paren) && 
Line.Type == LT_ObjCProperty23.6k
)
4813
45
    return true;
4814
243k
  if (Right.is(tok::kw_concept))
4815
36
    return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
4816
243k
  if (Right.is(TT_RequiresClause))
4817
84
    return true;
4818
243k
  if (Left.ClosesTemplateDeclaration || 
Left.is(TT_FunctionAnnotationRParen)242k
)
4819
1.16k
    return true;
4820
242k
  if (Left.ClosesRequiresClause)
4821
124
    return true;
4822
242k
  if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
4823
242k
                    TT_OverloadedOperator)) {