Coverage Report

Created: 2022-01-18 06:27

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