Coverage Report

Created: 2020-09-19 12:23

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