Coverage Report

Created: 2021-08-24 07:12

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