Coverage Report

Created: 2021-01-19 06:58

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