Coverage Report

Created: 2021-01-23 06:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/UnwrappedLineParser.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- UnwrappedLineParser.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 contains the implementation of the UnwrappedLineParser,
11
/// which turns a stream of tokens into UnwrappedLines.
12
///
13
//===----------------------------------------------------------------------===//
14
15
#include "UnwrappedLineParser.h"
16
#include "FormatToken.h"
17
#include "llvm/ADT/STLExtras.h"
18
#include "llvm/Support/Debug.h"
19
#include "llvm/Support/raw_ostream.h"
20
21
#include <algorithm>
22
23
#define DEBUG_TYPE "format-parser"
24
25
namespace clang {
26
namespace format {
27
28
class FormatTokenSource {
29
public:
30
46.1k
  virtual ~FormatTokenSource() {}
31
  virtual FormatToken *getNextToken() = 0;
32
33
  virtual unsigned getPosition() = 0;
34
  virtual FormatToken *setPosition(unsigned Position) = 0;
35
};
36
37
namespace {
38
39
class ScopedDeclarationState {
40
public:
41
  ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
42
                         bool MustBeDeclaration)
43
66.6k
      : Line(Line), Stack(Stack) {
44
66.6k
    Line.MustBeDeclaration = MustBeDeclaration;
45
66.6k
    Stack.push_back(MustBeDeclaration);
46
66.6k
  }
47
66.6k
  ~ScopedDeclarationState() {
48
66.6k
    Stack.pop_back();
49
66.6k
    if (!Stack.empty())
50
26.9k
      Line.MustBeDeclaration = Stack.back();
51
39.7k
    else
52
39.7k
      Line.MustBeDeclaration = true;
53
66.6k
  }
54
55
private:
56
  UnwrappedLine &Line;
57
  std::vector<bool> &Stack;
58
};
59
60
38.8k
static bool isLineComment(const FormatToken &FormatTok) {
61
38.8k
  return FormatTok.is(tok::comment) && 
!FormatTok.TokenText.startswith("/*")13.6k
;
62
38.8k
}
63
64
// Checks if \p FormatTok is a line comment that continues the line comment
65
// \p Previous. The original column of \p MinColumnToken is used to determine
66
// whether \p FormatTok is indented enough to the right to continue \p Previous.
67
static bool continuesLineComment(const FormatToken &FormatTok,
68
                                 const FormatToken *Previous,
69
16.9k
                                 const FormatToken *MinColumnToken) {
70
16.9k
  if (!Previous || 
!MinColumnToken16.9k
)
71
10
    return false;
72
16.9k
  unsigned MinContinueColumn =
73
15.2k
      MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 
01.71k
: 1);
74
16.9k
  return isLineComment(FormatTok) && 
FormatTok.NewlinesBefore == 18.71k
&&
75
4.04k
         isLineComment(*Previous) &&
76
1.74k
         FormatTok.OriginalColumn >= MinContinueColumn;
77
16.9k
}
78
79
class ScopedMacroState : public FormatTokenSource {
80
public:
81
  ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
82
                   FormatToken *&ResetToken)
83
      : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
84
        PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
85
8.55k
        Token(nullptr), PreviousToken(nullptr) {
86
8.55k
    FakeEOF.Tok.startToken();
87
8.55k
    FakeEOF.Tok.setKind(tok::eof);
88
8.55k
    TokenSource = this;
89
8.55k
    Line.Level = 0;
90
8.55k
    Line.InPPDirective = true;
91
8.55k
  }
92
93
8.55k
  ~ScopedMacroState() override {
94
8.55k
    TokenSource = PreviousTokenSource;
95
8.55k
    ResetToken = Token;
96
8.55k
    Line.InPPDirective = false;
97
8.55k
    Line.Level = PreviousLineLevel;
98
8.55k
  }
99
100
40.3k
  FormatToken *getNextToken() override {
101
    // The \c UnwrappedLineParser guards against this by never calling
102
    // \c getNextToken() after it has encountered the first eof token.
103
40.3k
    assert(!eof());
104
40.3k
    PreviousToken = Token;
105
40.3k
    Token = PreviousTokenSource->getNextToken();
106
40.3k
    if (eof())
107
7.17k
      return &FakeEOF;
108
33.1k
    return Token;
109
33.1k
  }
110
111
1.07k
  unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
112
113
110
  FormatToken *setPosition(unsigned Position) override {
114
110
    PreviousToken = nullptr;
115
110
    Token = PreviousTokenSource->setPosition(Position);
116
110
    return Token;
117
110
  }
118
119
private:
120
80.6k
  bool eof() {
121
80.6k
    return Token && 
Token->HasUnescapedNewline72.0k
&&
122
7.21k
           !continuesLineComment(*Token, PreviousToken,
123
7.21k
                                 /*MinColumnToken=*/PreviousToken);
124
80.6k
  }
125
126
  FormatToken FakeEOF;
127
  UnwrappedLine &Line;
128
  FormatTokenSource *&TokenSource;
129
  FormatToken *&ResetToken;
130
  unsigned PreviousLineLevel;
131
  FormatTokenSource *PreviousTokenSource;
132
133
  FormatToken *Token;
134
  FormatToken *PreviousToken;
135
};
136
137
} // end anonymous namespace
138
139
class ScopedLineState {
140
public:
141
  ScopedLineState(UnwrappedLineParser &Parser,
142
                  bool SwitchToPreprocessorLines = false)
143
11.2k
      : Parser(Parser), OriginalLines(Parser.CurrentLines) {
144
11.2k
    if (SwitchToPreprocessorLines)
145
1.76k
      Parser.CurrentLines = &Parser.PreprocessorDirectives;
146
9.49k
    else if (!Parser.Line->Tokens.empty())
147
2.70k
      Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
148
11.2k
    PreBlockLine = std::move(Parser.Line);
149
11.2k
    Parser.Line = std::make_unique<UnwrappedLine>();
150
11.2k
    Parser.Line->Level = PreBlockLine->Level;
151
11.2k
    Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
152
11.2k
  }
153
154
11.2k
  ~ScopedLineState() {
155
11.2k
    if (!Parser.Line->Tokens.empty()) {
156
3
      Parser.addUnwrappedLine();
157
3
    }
158
11.2k
    assert(Parser.Line->Tokens.empty());
159
11.2k
    Parser.Line = std::move(PreBlockLine);
160
11.2k
    if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
161
1.76k
      Parser.MustBreakBeforeNextToken = true;
162
11.2k
    Parser.CurrentLines = OriginalLines;
163
11.2k
  }
164
165
private:
166
  UnwrappedLineParser &Parser;
167
168
  std::unique_ptr<UnwrappedLine> PreBlockLine;
169
  SmallVectorImpl<UnwrappedLine> *OriginalLines;
170
};
171
172
class CompoundStatementIndenter {
173
public:
174
  CompoundStatementIndenter(UnwrappedLineParser *Parser,
175
                            const FormatStyle &Style, unsigned &LineLevel)
176
      : CompoundStatementIndenter(Parser, LineLevel,
177
                                  Style.BraceWrapping.AfterControlStatement,
178
3.33k
                                  Style.BraceWrapping.IndentBraces) {}
179
  CompoundStatementIndenter(UnwrappedLineParser *Parser, unsigned &LineLevel,
180
                            bool WrapBrace, bool IndentBrace)
181
3.64k
      : LineLevel(LineLevel), OldLineLevel(LineLevel) {
182
3.64k
    if (WrapBrace)
183
663
      Parser->addUnwrappedLine();
184
3.64k
    if (IndentBrace)
185
119
      ++LineLevel;
186
3.64k
  }
187
3.64k
  ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
188
189
private:
190
  unsigned &LineLevel;
191
  unsigned OldLineLevel;
192
};
193
194
namespace {
195
196
class IndexedTokenSource : public FormatTokenSource {
197
public:
198
  IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
199
37.5k
      : Tokens(Tokens), Position(-1) {}
200
201
740k
  FormatToken *getNextToken() override {
202
740k
    ++Position;
203
740k
    return Tokens[Position];
204
740k
  }
205
206
13.8k
  unsigned getPosition() override {
207
13.8k
    assert(Position >= 0);
208
13.8k
    return Position;
209
13.8k
  }
210
211
12.8k
  FormatToken *setPosition(unsigned P) override {
212
12.8k
    Position = P;
213
12.8k
    return Tokens[Position];
214
12.8k
  }
215
216
37.8k
  void reset() { Position = -1; }
217
218
private:
219
  ArrayRef<FormatToken *> Tokens;
220
  int Position;
221
};
222
223
} // end anonymous namespace
224
225
UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
226
                                         const AdditionalKeywords &Keywords,
227
                                         unsigned FirstStartColumn,
228
                                         ArrayRef<FormatToken *> Tokens,
229
                                         UnwrappedLineConsumer &Callback)
230
    : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
231
      CurrentLines(&Lines), Style(Style), Keywords(Keywords),
232
      CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
233
      Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
234
      IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
235
                       ? IG_Rejected
236
                       : IG_Inited),
237
37.5k
      IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
238
239
37.8k
void UnwrappedLineParser::reset() {
240
37.8k
  PPBranchLevel = -1;
241
37.8k
  IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None
242
37.6k
                     ? IG_Rejected
243
207
                     : IG_Inited;
244
37.8k
  IncludeGuardToken = nullptr;
245
37.8k
  Line.reset(new UnwrappedLine);
246
37.8k
  CommentsBeforeNextToken.clear();
247
37.8k
  FormatTok = nullptr;
248
37.8k
  MustBreakBeforeNextToken = false;
249
37.8k
  PreprocessorDirectives.clear();
250
37.8k
  CurrentLines = &Lines;
251
37.8k
  DeclarationScopeStack.clear();
252
37.8k
  PPStack.clear();
253
37.8k
  Line->FirstStartColumn = FirstStartColumn;
254
37.8k
}
255
256
37.5k
void UnwrappedLineParser::parse() {
257
37.5k
  IndexedTokenSource TokenSource(AllTokens);
258
37.5k
  Line->FirstStartColumn = FirstStartColumn;
259
37.8k
  do {
260
37.8k
    LLVM_DEBUG(llvm::dbgs() << "----\n");
261
37.8k
    reset();
262
37.8k
    Tokens = &TokenSource;
263
37.8k
    TokenSource.reset();
264
265
37.8k
    readToken();
266
37.8k
    parseFile();
267
268
    // If we found an include guard then all preprocessor directives (other than
269
    // the guard) are over-indented by one.
270
37.8k
    if (IncludeGuard == IG_Found)
271
34
      for (auto &Line : Lines)
272
177
        if (Line.InPPDirective && 
Line.Level > 0130
)
273
62
          --Line.Level;
274
275
    // Create line with eof token.
276
37.8k
    pushToken(FormatTok);
277
37.8k
    addUnwrappedLine();
278
279
37.8k
    for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
280
37.8k
                                                  E = Lines.end();
281
184k
         I != E; 
++I146k
) {
282
146k
      Callback.consumeUnwrappedLine(*I);
283
146k
    }
284
37.8k
    Callback.finishRun();
285
37.8k
    Lines.clear();
286
38.5k
    while (!PPLevelBranchIndex.empty() &&
287
975
           PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
288
696
      PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
289
696
      PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
290
696
    }
291
37.8k
    if (!PPLevelBranchIndex.empty()) {
292
279
      ++PPLevelBranchIndex.back();
293
279
      assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
294
279
      assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
295
279
    }
296
37.8k
  } while (!PPLevelBranchIndex.empty());
297
37.5k
}
298
299
40.0k
void UnwrappedLineParser::parseFile() {
300
  // The top-level context in a file always has declarations, except for pre-
301
  // processor directives and JavaScript files.
302
40.0k
  bool MustBeDeclaration =
303
40.0k
      !Line->InPPDirective && 
Style.Language != FormatStyle::LK_JavaScript37.8k
;
304
40.0k
  ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
305
40.0k
                                          MustBeDeclaration);
306
40.0k
  if (Style.Language == FormatStyle::LK_TextProto)
307
606
    parseBracedList();
308
39.4k
  else
309
39.4k
    parseLevel(/*HasOpeningBrace=*/false);
310
  // Make sure to format the remaining tokens.
311
  //
312
  // LK_TextProto is special since its top-level is parsed as the body of a
313
  // braced list, which does not necessarily have natural line separators such
314
  // as a semicolon. Comments after the last entry that have been determined to
315
  // not belong to that line, as in:
316
  //   key: value
317
  //   // endfile comment
318
  // do not have a chance to be put on a line of their own until this point.
319
  // Here we add this newline before end-of-file comments.
320
40.0k
  if (Style.Language == FormatStyle::LK_TextProto &&
321
606
      !CommentsBeforeNextToken.empty())
322
13
    addUnwrappedLine();
323
40.0k
  flushComments(true);
324
40.0k
  addUnwrappedLine();
325
40.0k
}
326
327
14
void UnwrappedLineParser::parseCSharpGenericTypeConstraint() {
328
140
  do {
329
140
    switch (FormatTok->Tok.getKind()) {
330
14
    case tok::l_brace:
331
14
      return;
332
126
    default:
333
126
      if (FormatTok->is(Keywords.kw_where)) {
334
4
        addUnwrappedLine();
335
4
        nextToken();
336
4
        parseCSharpGenericTypeConstraint();
337
4
        break;
338
4
      }
339
122
      nextToken();
340
122
      break;
341
140
    }
342
126
  } while (!eof());
343
14
}
344
345
54
void UnwrappedLineParser::parseCSharpAttribute() {
346
54
  int UnpairedSquareBrackets = 1;
347
254
  do {
348
254
    switch (FormatTok->Tok.getKind()) {
349
56
    case tok::r_square:
350
56
      nextToken();
351
56
      --UnpairedSquareBrackets;
352
56
      if (UnpairedSquareBrackets == 0) {
353
54
        addUnwrappedLine();
354
54
        return;
355
54
      }
356
2
      break;
357
2
    case tok::l_square:
358
2
      ++UnpairedSquareBrackets;
359
2
      nextToken();
360
2
      break;
361
196
    default:
362
196
      nextToken();
363
196
      break;
364
200
    }
365
200
  } while (!eof());
366
54
}
367
368
66.0k
void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
369
66.0k
  bool SwitchLabelEncountered = false;
370
100k
  do {
371
100k
    tok::TokenKind kind = FormatTok->Tok.getKind();
372
100k
    if (FormatTok->getType() == TT_MacroBlockBegin) {
373
28
      kind = tok::l_brace;
374
100k
    } else if (FormatTok->getType() == TT_MacroBlockEnd) {
375
28
      kind = tok::r_brace;
376
28
    }
377
378
100k
    switch (kind) {
379
0
    case tok::comment:
380
0
      nextToken();
381
0
      addUnwrappedLine();
382
0
      break;
383
732
    case tok::l_brace:
384
      // FIXME: Add parameter whether this can happen - if this happens, we must
385
      // be in a non-declaration context.
386
732
      if (!FormatTok->is(TT_MacroBlockBegin) && 
tryToParseBracedList()704
)
387
7
        continue;
388
725
      parseBlock(/*MustBeDeclaration=*/false);
389
725
      addUnwrappedLine();
390
725
      break;
391
26.5k
    case tok::r_brace:
392
26.5k
      if (HasOpeningBrace)
393
26.4k
        return;
394
63
      nextToken();
395
63
      addUnwrappedLine();
396
63
      break;
397
121
    case tok::kw_default: {
398
121
      unsigned StoredPosition = Tokens->getPosition();
399
121
      FormatToken *Next;
400
124
      do {
401
124
        Next = Tokens->getNextToken();
402
124
      } while (Next && Next->is(tok::comment));
403
121
      FormatTok = Tokens->setPosition(StoredPosition);
404
121
      if (Next && Next->isNot(tok::colon)) {
405
        // default not followed by ':' is not a case label; treat it like
406
        // an identifier.
407
2
        parseStructuralElement();
408
2
        break;
409
2
      }
410
      // Else, if it is 'default:', fall through to the case handling.
411
119
      LLVM_FALLTHROUGH;
412
119
    }
413
692
    case tok::kw_case:
414
692
      if (Style.Language == FormatStyle::LK_JavaScript &&
415
12
          Line->MustBeDeclaration) {
416
        // A 'case: string' style field declaration.
417
8
        parseStructuralElement();
418
8
        break;
419
8
      }
420
684
      if (!SwitchLabelEncountered &&
421
436
          (Style.IndentCaseLabels || 
(389
Line->InPPDirective389
&&
Line->Level == 121
)))
422
61
        ++Line->Level;
423
684
      SwitchLabelEncountered = true;
424
684
      parseStructuralElement();
425
684
      break;
426
994
    case tok::l_square:
427
994
      if (Style.isCSharp()) {
428
54
        nextToken();
429
54
        parseCSharpAttribute();
430
54
        break;
431
54
      }
432
940
      LLVM_FALLTHROUGH;
433
72.7k
    default:
434
72.7k
      parseStructuralElement();
435
72.7k
      break;
436
74.2k
    }
437
74.2k
  } while (!eof());
438
66.0k
}
439
440
12.5k
void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
441
  // We'll parse forward through the tokens until we hit
442
  // a closing brace or eof - note that getNextToken() will
443
  // parse macros, so this will magically work inside macro
444
  // definitions, too.
445
12.5k
  unsigned StoredPosition = Tokens->getPosition();
446
12.5k
  FormatToken *Tok = FormatTok;
447
12.5k
  const FormatToken *PrevTok = Tok->Previous;
448
  // Keep a stack of positions of lbrace tokens. We will
449
  // update information about whether an lbrace starts a
450
  // braced init list or a different block during the loop.
451
12.5k
  SmallVector<FormatToken *, 8> LBraceStack;
452
12.5k
  assert(Tok->Tok.is(tok::l_brace));
453
93.0k
  do {
454
    // Get next non-comment token.
455
93.0k
    FormatToken *NextTok;
456
93.0k
    unsigned ReadTokens = 0;
457
94.4k
    do {
458
94.4k
      NextTok = Tokens->getNextToken();
459
94.4k
      ++ReadTokens;
460
94.4k
    } while (NextTok->is(tok::comment));
461
462
93.0k
    switch (Tok->Tok.getKind()) {
463
14.6k
    case tok::l_brace:
464
14.6k
      if (Style.Language == FormatStyle::LK_JavaScript && 
PrevTok736
) {
465
726
        if (PrevTok->isOneOf(tok::colon, tok::less))
466
          // A ':' indicates this code is in a type, or a braced list
467
          // following a label in an object literal ({a: {b: 1}}).
468
          // A '<' could be an object used in a comparison, but that is nonsense
469
          // code (can never return true), so more likely it is a generic type
470
          // argument (`X<{a: string; b: number}>`).
471
          // The code below could be confused by semicolons between the
472
          // individual members in a type member list, which would normally
473
          // trigger BK_Block. In both cases, this must be parsed as an inline
474
          // braced init.
475
94
          Tok->setBlockKind(BK_BracedInit);
476
632
        else if (PrevTok->is(tok::r_paren))
477
          // `) { }` can only occur in function or method declarations in JS.
478
336
          Tok->setBlockKind(BK_Block);
479
13.8k
      } else {
480
13.8k
        Tok->setBlockKind(BK_Unknown);
481
13.8k
      }
482
14.6k
      LBraceStack.push_back(Tok);
483
14.6k
      break;
484
14.5k
    case tok::r_brace:
485
14.5k
      if (LBraceStack.empty())
486
0
        break;
487
14.5k
      if (LBraceStack.back()->is(BK_Unknown)) {
488
7.21k
        bool ProbablyBracedList = false;
489
7.21k
        if (Style.Language == FormatStyle::LK_Proto) {
490
36
          ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
491
7.18k
        } else {
492
          // Using OriginalColumn to distinguish between ObjC methods and
493
          // binary operators is a bit hacky.
494
7.18k
          bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
495
14
                                  NextTok->OriginalColumn == 0;
496
497
          // If there is a comma, semicolon or right paren after the closing
498
          // brace, we assume this is a braced initializer list.  Note that
499
          // regardless how we mark inner braces here, we will overwrite the
500
          // BlockKind later if we parse a braced list (where all blocks
501
          // inside are by default braced lists), or when we explicitly detect
502
          // blocks (for example while parsing lambdas).
503
          // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
504
          // braced list in JS.
505
7.18k
          ProbablyBracedList =
506
7.18k
              (Style.Language == FormatStyle::LK_JavaScript &&
507
242
               NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
508
242
                                Keywords.kw_as)) ||
509
7.16k
              (Style.isCpp() && 
NextTok->is(tok::l_paren)6.73k
) ||
510
7.13k
              NextTok->isOneOf(tok::comma, tok::period, tok::colon,
511
7.13k
                               tok::r_paren, tok::r_square, tok::l_brace,
512
7.13k
                               tok::ellipsis) ||
513
6.56k
              (NextTok->is(tok::identifier) &&
514
190
               !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
515
6.55k
              (NextTok->is(tok::semi) &&
516
1.62k
               (!ExpectClassBody || 
LBraceStack.size() != 1557
)) ||
517
5.46k
              (NextTok->isBinaryOperator() && 
!NextIsObjCMethod59
);
518
7.18k
          if (!Style.isCSharp() && 
NextTok->is(tok::l_square)7.06k
) {
519
            // We can have an array subscript after a braced init
520
            // list, but C++11 attributes are expected after blocks.
521
14
            NextTok = Tokens->getNextToken();
522
14
            ++ReadTokens;
523
14
            ProbablyBracedList = NextTok->isNot(tok::l_square);
524
14
          }
525
7.18k
        }
526
7.21k
        if (ProbablyBracedList) {
527
1.81k
          Tok->setBlockKind(BK_BracedInit);
528
1.81k
          LBraceStack.back()->setBlockKind(BK_BracedInit);
529
5.40k
        } else {
530
5.40k
          Tok->setBlockKind(BK_Block);
531
5.40k
          LBraceStack.back()->setBlockKind(BK_Block);
532
5.40k
        }
533
7.21k
      }
534
14.5k
      LBraceStack.pop_back();
535
14.5k
      break;
536
18.9k
    case tok::identifier:
537
18.9k
      if (!Tok->is(TT_StatementMacro))
538
18.9k
        break;
539
14
      LLVM_FALLTHROUGH;
540
99
    case tok::at:
541
8.44k
    case tok::semi:
542
8.99k
    case tok::kw_if:
543
9.10k
    case tok::kw_while:
544
9.19k
    case tok::kw_for:
545
9.27k
    case tok::kw_switch:
546
9.29k
    case tok::kw_try:
547
9.29k
    case tok::kw___try:
548
9.29k
      if (!LBraceStack.empty() && LBraceStack.back()->is(BK_Unknown))
549
6.89k
        LBraceStack.back()->setBlockKind(BK_Block);
550
9.29k
      break;
551
35.6k
    default:
552
35.6k
      break;
553
93.0k
    }
554
93.0k
    PrevTok = Tok;
555
93.0k
    Tok = NextTok;
556
93.0k
  } while (Tok->Tok.isNot(tok::eof) && 
!LBraceStack.empty()86.3k
);
557
558
  // Assume other blocks for all unclosed opening braces.
559
12.6k
  
for (unsigned i = 0, e = LBraceStack.size(); 12.5k
i != e;
++i109
) {
560
109
    if (LBraceStack[i]->is(BK_Unknown))
561
70
      LBraceStack[i]->setBlockKind(BK_Block);
562
109
  }
563
564
12.5k
  FormatTok = Tokens->setPosition(StoredPosition);
565
12.5k
}
566
567
template <class T>
568
664
static inline void hash_combine(std::size_t &seed, const T &v) {
569
664
  std::hash<T> hasher;
570
664
  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
571
664
}
572
573
47.7k
size_t UnwrappedLineParser::computePPHash() const {
574
47.7k
  size_t h = 0;
575
332
  for (const auto &i : PPStack) {
576
332
    hash_combine(h, size_t(i.Kind));
577
332
    hash_combine(h, i.Line);
578
332
  }
579
47.7k
  return h;
580
47.7k
}
581
582
void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
583
23.9k
                                     bool MunchSemi) {
584
23.9k
  assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
585
23.9k
         "'{' or macro block token expected");
586
23.9k
  const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
587
23.9k
  FormatTok->setBlockKind(BK_Block);
588
589
23.9k
  size_t PPStartHash = computePPHash();
590
591
23.9k
  unsigned InitialLevel = Line->Level;
592
20.5k
  nextToken(/*LevelDifference=*/AddLevel ? 1 : 
03.38k
);
593
594
23.9k
  if (MacroBlock && 
FormatTok->is(tok::l_paren)28
)
595
7
    parseParens();
596
597
23.9k
  size_t NbPreprocessorDirectives =
598
23.7k
      CurrentLines == &Lines ? PreprocessorDirectives.size() : 
0143
;
599
23.9k
  addUnwrappedLine();
600
23.9k
  size_t OpeningLineIndex =
601
23.9k
      CurrentLines->empty()
602
0
          ? (UnwrappedLine::kInvalidIndex)
603
23.9k
          : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
604
605
23.9k
  ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
606
23.9k
                                          MustBeDeclaration);
607
23.9k
  if (AddLevel)
608
20.5k
    ++Line->Level;
609
23.9k
  parseLevel(/*HasOpeningBrace=*/true);
610
611
23.9k
  if (eof())
612
131
    return;
613
614
23.7k
  if (MacroBlock ? 
!FormatTok->is(TT_MacroBlockEnd)28
615
23.7k
                 : !FormatTok->is(tok::r_brace)) {
616
0
    Line->Level = InitialLevel;
617
0
    FormatTok->setBlockKind(BK_Block);
618
0
    return;
619
0
  }
620
621
23.7k
  size_t PPEndHash = computePPHash();
622
623
  // Munch the closing brace.
624
20.4k
  nextToken(/*LevelDifference=*/AddLevel ? -1 : 
03.36k
);
625
626
23.7k
  if (MacroBlock && 
FormatTok->is(tok::l_paren)28
)
627
7
    parseParens();
628
629
23.7k
  if (FormatTok->is(tok::arrow)) {
630
    // Following the } we can find a trailing return type arrow
631
    // as part of an implicit conversion constraint.
632
98
    nextToken();
633
98
    parseStructuralElement();
634
98
  }
635
636
23.7k
  if (MunchSemi && 
FormatTok->Tok.is(tok::semi)17.8k
)
637
367
    nextToken();
638
639
23.7k
  Line->Level = InitialLevel;
640
641
23.7k
  if (PPStartHash == PPEndHash) {
642
23.7k
    Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
643
23.7k
    if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
644
      // Update the opening line to add the forward reference as well
645
23.7k
      (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
646
23.7k
          CurrentLines->size() - 1;
647
23.7k
    }
648
23.7k
  }
649
23.7k
}
650
651
380
static bool isGoogScope(const UnwrappedLine &Line) {
652
  // FIXME: Closure-library specific stuff should not be hard-coded but be
653
  // configurable.
654
380
  if (Line.Tokens.size() < 4)
655
0
    return false;
656
380
  auto I = Line.Tokens.begin();
657
380
  if (I->Tok->TokenText != "goog")
658
372
    return false;
659
8
  ++I;
660
8
  if (I->Tok->isNot(tok::period))
661
0
    return false;
662
8
  ++I;
663
8
  if (I->Tok->TokenText != "scope")
664
0
    return false;
665
8
  ++I;
666
8
  return I->Tok->is(tok::l_paren);
667
8
}
668
669
static bool isIIFE(const UnwrappedLine &Line,
670
372
                   const AdditionalKeywords &Keywords) {
671
  // Look for the start of an immediately invoked anonymous function.
672
  // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
673
  // This is commonly done in JavaScript to create a new, anonymous scope.
674
  // Example: (function() { ... })()
675
372
  if (Line.Tokens.size() < 3)
676
0
    return false;
677
372
  auto I = Line.Tokens.begin();
678
372
  if (I->Tok->isNot(tok::l_paren))
679
352
    return false;
680
20
  ++I;
681
20
  if (I->Tok->isNot(Keywords.kw_function))
682
0
    return false;
683
20
  ++I;
684
20
  return I->Tok->is(tok::l_paren);
685
20
}
686
687
static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
688
9.37k
                                   const FormatToken &InitialToken) {
689
9.37k
  if (InitialToken.isOneOf(tok::kw_namespace, TT_NamespaceMacro))
690
3.40k
    return Style.BraceWrapping.AfterNamespace;
691
5.96k
  if (InitialToken.is(tok::kw_class))
692
3.05k
    return Style.BraceWrapping.AfterClass;
693
2.91k
  if (InitialToken.is(tok::kw_union))
694
116
    return Style.BraceWrapping.AfterUnion;
695
2.79k
  if (InitialToken.is(tok::kw_struct))
696
2.76k
    return Style.BraceWrapping.AfterStruct;
697
36
  return false;
698
36
}
699
700
2.70k
void UnwrappedLineParser::parseChildBlock() {
701
2.70k
  FormatTok->setBlockKind(BK_Block);
702
2.70k
  nextToken();
703
2.70k
  {
704
2.70k
    bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript &&
705
380
                       (isGoogScope(*Line) || 
isIIFE(*Line, Keywords)372
));
706
2.70k
    ScopedLineState LineState(*this);
707
2.70k
    ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
708
2.70k
                                            /*MustBeDeclaration=*/false);
709
2.67k
    Line->Level += SkipIndent ? 
024
: 1;
710
2.70k
    parseLevel(/*HasOpeningBrace=*/true);
711
2.70k
    flushComments(isOnNewLine(*FormatTok));
712
2.67k
    Line->Level -= SkipIndent ? 
024
: 1;
713
2.70k
  }
714
2.70k
  nextToken();
715
2.70k
}
716
717
8.55k
void UnwrappedLineParser::parsePPDirective() {
718
8.55k
  assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
719
8.55k
  ScopedMacroState MacroState(*Line, Tokens, FormatTok);
720
721
8.55k
  nextToken();
722
723
8.55k
  if (!FormatTok->Tok.getIdentifierInfo()) {
724
34
    parsePPUnknown();
725
34
    return;
726
34
  }
727
728
8.52k
  switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
729
2.21k
  case tok::pp_define:
730
2.21k
    parsePPDefine();
731
2.21k
    return;
732
411
  case tok::pp_if:
733
411
    parsePPIf(/*IfDef=*/false);
734
411
    break;
735
499
  case tok::pp_ifdef:
736
609
  case tok::pp_ifndef:
737
609
    parsePPIf(/*IfDef=*/true);
738
609
    break;
739
499
  case tok::pp_else:
740
499
    parsePPElse();
741
499
    break;
742
70
  case tok::pp_elif:
743
70
    parsePPElIf();
744
70
    break;
745
965
  case tok::pp_endif:
746
965
    parsePPEndIf();
747
965
    break;
748
3.75k
  default:
749
3.75k
    parsePPUnknown();
750
3.75k
    break;
751
8.52k
  }
752
8.52k
}
753
754
1.67k
void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
755
1.67k
  size_t Line = CurrentLines->size();
756
1.67k
  if (CurrentLines == &PreprocessorDirectives)
757
771
    Line += Lines.size();
758
759
1.67k
  if (Unreachable ||
760
1.00k
      (!PPStack.empty() && 
PPStack.back().Kind == PP_Unreachable146
))
761
735
    PPStack.push_back({PP_Unreachable, Line});
762
935
  else
763
935
    PPStack.push_back({PP_Conditional, Line});
764
1.67k
}
765
766
1.04k
void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
767
1.04k
  ++PPBranchLevel;
768
1.04k
  assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
769
1.04k
  if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
770
696
    PPLevelBranchIndex.push_back(0);
771
696
    PPLevelBranchCount.push_back(0);
772
696
  }
773
1.04k
  PPChainBranchIndex.push(0);
774
1.04k
  bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
775
1.04k
  conditionalCompilationCondition(Unreachable || 
Skip990
);
776
1.04k
}
777
778
623
void UnwrappedLineParser::conditionalCompilationAlternative() {
779
623
  if (!PPStack.empty())
780
616
    PPStack.pop_back();
781
623
  assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
782
623
  if (!PPChainBranchIndex.empty())
783
616
    ++PPChainBranchIndex.top();
784
623
  conditionalCompilationCondition(
785
623
      PPBranchLevel >= 0 && 
!PPChainBranchIndex.empty()616
&&
786
616
      PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
787
623
}
788
789
992
void UnwrappedLineParser::conditionalCompilationEnd() {
790
992
  assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
791
992
  if (PPBranchLevel >= 0 && 
!PPChainBranchIndex.empty()979
) {
792
979
    if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
793
642
      PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
794
642
    }
795
979
  }
796
  // Guard against #endif's without #if.
797
992
  if (PPBranchLevel > -1)
798
979
    --PPBranchLevel;
799
992
  if (!PPChainBranchIndex.empty())
800
979
    PPChainBranchIndex.pop();
801
992
  if (!PPStack.empty())
802
979
    PPStack.pop_back();
803
992
}
804
805
1.02k
void UnwrappedLineParser::parsePPIf(bool IfDef) {
806
1.02k
  bool IfNDef = FormatTok->is(tok::pp_ifndef);
807
1.02k
  nextToken();
808
1.02k
  bool Unreachable = false;
809
1.02k
  if (!IfDef && 
(411
FormatTok->is(tok::kw_false)411
||
FormatTok->TokenText == "0"408
))
810
54
    Unreachable = true;
811
1.02k
  if (IfDef && 
!IfNDef609
&&
FormatTok->TokenText == "SWIG"499
)
812
3
    Unreachable = true;
813
1.02k
  conditionalCompilationStart(Unreachable);
814
1.02k
  FormatToken *IfCondition = FormatTok;
815
  // If there's a #ifndef on the first line, and the only lines before it are
816
  // comments, it could be an include guard.
817
1.02k
  bool MaybeIncludeGuard = IfNDef;
818
1.02k
  if (IncludeGuard == IG_Inited && 
MaybeIncludeGuard258
)
819
93
    for (auto &Line : Lines) {
820
17
      if (!Line.Tokens.front().Tok->is(tok::comment)) {
821
10
        MaybeIncludeGuard = false;
822
10
        IncludeGuard = IG_Rejected;
823
10
        break;
824
10
      }
825
17
    }
826
1.02k
  --PPBranchLevel;
827
1.02k
  parsePPUnknown();
828
1.02k
  ++PPBranchLevel;
829
1.02k
  if (IncludeGuard == IG_Inited && 
MaybeIncludeGuard248
) {
830
83
    IncludeGuard = IG_IfNdefed;
831
83
    IncludeGuardToken = IfCondition;
832
83
  }
833
1.02k
}
834
835
569
void UnwrappedLineParser::parsePPElse() {
836
  // If a potential include guard has an #else, it's not an include guard.
837
569
  if (IncludeGuard == IG_Defined && 
PPBranchLevel == 028
)
838
28
    IncludeGuard = IG_Rejected;
839
569
  conditionalCompilationAlternative();
840
569
  if (PPBranchLevel > -1)
841
562
    --PPBranchLevel;
842
569
  parsePPUnknown();
843
569
  ++PPBranchLevel;
844
569
}
845
846
70
void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
847
848
965
void UnwrappedLineParser::parsePPEndIf() {
849
965
  conditionalCompilationEnd();
850
965
  parsePPUnknown();
851
  // If the #endif of a potential include guard is the last thing in the file,
852
  // then we found an include guard.
853
965
  unsigned TokenPosition = Tokens->getPosition();
854
965
  FormatToken *PeekNext = AllTokens[TokenPosition];
855
965
  if (IncludeGuard == IG_Defined && 
PPBranchLevel == -148
&&
856
41
      PeekNext->is(tok::eof) &&
857
34
      Style.IndentPPDirectives != FormatStyle::PPDIS_None)
858
34
    IncludeGuard = IG_Found;
859
965
}
860
861
2.21k
void UnwrappedLineParser::parsePPDefine() {
862
2.21k
  nextToken();
863
864
2.21k
  if (!FormatTok->Tok.getIdentifierInfo()) {
865
7
    IncludeGuard = IG_Rejected;
866
7
    IncludeGuardToken = nullptr;
867
7
    parsePPUnknown();
868
7
    return;
869
7
  }
870
871
2.21k
  if (IncludeGuard == IG_IfNdefed &&
872
76
      IncludeGuardToken->TokenText == FormatTok->TokenText) {
873
69
    IncludeGuard = IG_Defined;
874
69
    IncludeGuardToken = nullptr;
875
73
    for (auto &Line : Lines) {
876
73
      if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
877
0
        IncludeGuard = IG_Rejected;
878
0
        break;
879
0
      }
880
73
    }
881
69
  }
882
883
2.21k
  nextToken();
884
2.21k
  if (FormatTok->Tok.getKind() == tok::l_paren &&
885
1.07k
      FormatTok->WhitespaceRange.getBegin() ==
886
945
          FormatTok->WhitespaceRange.getEnd()) {
887
945
    parseParens();
888
945
  }
889
2.21k
  if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
890
341
    Line->Level += PPBranchLevel + 1;
891
2.21k
  addUnwrappedLine();
892
2.21k
  ++Line->Level;
893
894
  // Errors during a preprocessor directive can only affect the layout of the
895
  // preprocessor directive, and thus we ignore them. An alternative approach
896
  // would be to use the same approach we use on the file level (no
897
  // re-indentation if there was a structural error) within the macro
898
  // definition.
899
2.21k
  parseFile();
900
2.21k
}
901
902
6.34k
void UnwrappedLineParser::parsePPUnknown() {
903
13.0k
  do {
904
13.0k
    nextToken();
905
13.0k
  } while (!eof());
906
6.34k
  if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
907
694
    Line->Level += PPBranchLevel + 1;
908
6.34k
  addUnwrappedLine();
909
6.34k
}
910
911
// Here we exclude certain tokens that are not usually the first token in an
912
// unwrapped line. This is used in attempt to distinguish macro calls without
913
// trailing semicolons from other constructs split to several lines.
914
476
static bool tokenCanStartNewLine(const FormatToken &Tok) {
915
  // Semicolon can be a null-statement, l_square can be a start of a macro or
916
  // a C++11 attribute, but this doesn't seem to be common.
917
476
  return Tok.isNot(tok::semi) && 
Tok.isNot(tok::l_brace)473
&&
918
467
         Tok.isNot(TT_AttributeSquare) &&
919
         // Tokens that can only be used as binary operators and a part of
920
         // overloaded operator names.
921
467
         Tok.isNot(tok::period) && 
Tok.isNot(tok::periodstar)422
&&
922
422
         Tok.isNot(tok::arrow) && 
Tok.isNot(tok::arrowstar)416
&&
923
413
         Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
924
410
         Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
925
410
         Tok.isNot(tok::lessless) && 
Tok.isNot(tok::greatergreater)380
&&
926
380
         Tok.isNot(tok::equal) && 
Tok.isNot(tok::plusequal)365
&&
927
362
         Tok.isNot(tok::minusequal) && 
Tok.isNot(tok::starequal)359
&&
928
356
         Tok.isNot(tok::slashequal) && 
Tok.isNot(tok::percentequal)353
&&
929
350
         Tok.isNot(tok::ampequal) && 
Tok.isNot(tok::pipeequal)347
&&
930
344
         Tok.isNot(tok::caretequal) && 
Tok.isNot(tok::greatergreaterequal)341
&&
931
338
         Tok.isNot(tok::lesslessequal) &&
932
         // Colon is used in labels, base class lists, initializer lists,
933
         // range-based for loops, ternary operator, but should never be the
934
         // first token in an unwrapped line.
935
335
         Tok.isNot(tok::colon) &&
936
         // 'noexcept' is a trailing annotation.
937
214
         Tok.isNot(tok::kw_noexcept);
938
476
}
939
940
static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
941
4.93k
                          const FormatToken *FormatTok) {
942
  // FIXME: This returns true for C/C++ keywords like 'struct'.
943
4.93k
  return FormatTok->is(tok::identifier) &&
944
1.19k
         (FormatTok->Tok.getIdentifierInfo() == nullptr ||
945
1.18k
          !FormatTok->isOneOf(
946
1.18k
              Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
947
1.18k
              Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
948
1.18k
              Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
949
1.18k
              Keywords.kw_let, Keywords.kw_var, tok::kw_const,
950
1.18k
              Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
951
1.18k
              Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws,
952
1.18k
              Keywords.kw_from));
953
4.93k
}
954
955
static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
956
5.08k
                                 const FormatToken *FormatTok) {
957
5.08k
  return FormatTok->Tok.isLiteral() ||
958
4.89k
         FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
959
4.88k
         mustBeJSIdent(Keywords, FormatTok);
960
5.08k
}
961
962
// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
963
// when encountered after a value (see mustBeJSIdentOrValue).
964
static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
965
176
                           const FormatToken *FormatTok) {
966
176
  return FormatTok->isOneOf(
967
176
      tok::kw_return, Keywords.kw_yield,
968
      // conditionals
969
176
      tok::kw_if, tok::kw_else,
970
      // loops
971
176
      tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
972
      // switch/case
973
176
      tok::kw_switch, tok::kw_case,
974
      // exceptions
975
176
      tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
976
      // declaration
977
176
      tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
978
176
      Keywords.kw_async, Keywords.kw_function,
979
      // import/export
980
176
      Keywords.kw_import, tok::kw_export);
981
176
}
982
983
// readTokenWithJavaScriptASI reads the next token and terminates the current
984
// line if JavaScript Automatic Semicolon Insertion must
985
// happen between the current token and the next token.
986
//
987
// This method is conservative - it cannot cover all edge cases of JavaScript,
988
// but only aims to correctly handle certain well known cases. It *must not*
989
// return true in speculative cases.
990
30.9k
void UnwrappedLineParser::readTokenWithJavaScriptASI() {
991
30.9k
  FormatToken *Previous = FormatTok;
992
30.9k
  readToken();
993
30.9k
  FormatToken *Next = FormatTok;
994
995
30.9k
  bool IsOnSameLine =
996
30.9k
      CommentsBeforeNextToken.empty()
997
30.8k
          ? Next->NewlinesBefore == 0
998
72
          : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
999
30.9k
  if (IsOnSameLine)
1000
28.4k
    return;
1001
1002
2.55k
  bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
1003
2.55k
  bool PreviousStartsTemplateExpr =
1004
2.55k
      Previous->is(TT_TemplateString) && 
Previous->TokenText.endswith("${")14
;
1005
2.55k
  if (PreviousMustBeValue || 
Previous->is(tok::r_paren)2.38k
) {
1006
    // If the line contains an '@' sign, the previous token might be an
1007
    // annotation, which can precede another identifier/value.
1008
222
    bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
1009
1.34k
                              [](UnwrappedLineNode &LineNode) {
1010
1.34k
                                return LineNode.Tok->is(tok::at);
1011
1.34k
                              }) != Line->Tokens.end();
1012
222
    if (HasAt)
1013
16
      return;
1014
2.53k
  }
1015
2.53k
  if (Next->is(tok::exclaim) && 
PreviousMustBeValue4
)
1016
4
    return addUnwrappedLine();
1017
2.53k
  bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
1018
2.53k
  bool NextEndsTemplateExpr =
1019
2.53k
      Next->is(TT_TemplateString) && 
Next->TokenText.startswith("}")26
;
1020
2.53k
  if (NextMustBeValue && 
!NextEndsTemplateExpr855
&&
!PreviousStartsTemplateExpr853
&&
1021
841
      (PreviousMustBeValue ||
1022
825
       Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
1023
825
                         tok::minusminus)))
1024
34
    return addUnwrappedLine();
1025
2.49k
  if ((PreviousMustBeValue || 
Previous->is(tok::r_paren)2.36k
) &&
1026
176
      isJSDeclOrStmt(Keywords, Next))
1027
28
    return addUnwrappedLine();
1028
2.49k
}
1029
1030
76.5k
void UnwrappedLineParser::parseStructuralElement() {
1031
76.5k
  assert(!FormatTok->is(tok::l_brace));
1032
76.5k
  if (Style.Language == FormatStyle::LK_TableGen &&
1033
10
      FormatTok->is(tok::pp_include)) {
1034
6
    nextToken();
1035
6
    if (FormatTok->is(tok::string_literal))
1036
6
      nextToken();
1037
6
    addUnwrappedLine();
1038
6
    return;
1039
6
  }
1040
76.5k
  switch (FormatTok->Tok.getKind()) {
1041
75
  case tok::kw_asm:
1042
75
    nextToken();
1043
75
    if (FormatTok->is(tok::l_brace)) {
1044
9
      FormatTok->setType(TT_InlineASMBrace);
1045
9
      nextToken();
1046
87
      while (FormatTok && FormatTok->isNot(tok::eof)) {
1047
87
        if (FormatTok->is(tok::r_brace)) {
1048
9
          FormatTok->setType(TT_InlineASMBrace);
1049
9
          nextToken();
1050
9
          addUnwrappedLine();
1051
9
          break;
1052
9
        }
1053
78
        FormatTok->Finalized = true;
1054
78
        nextToken();
1055
78
      }
1056
9
    }
1057
75
    break;
1058
3.22k
  case tok::kw_namespace:
1059
3.22k
    parseNamespace();
1060
3.22k
    return;
1061
885
  case tok::kw_public:
1062
965
  case tok::kw_protected:
1063
1.11k
  case tok::kw_private:
1064
1.11k
    if (Style.Language == FormatStyle::LK_Java ||
1065
1.04k
        Style.Language == FormatStyle::LK_JavaScript || 
Style.isCSharp()1.02k
)
1066
211
      nextToken();
1067
907
    else
1068
907
      parseAccessSpecifier();
1069
1.11k
    return;
1070
2.09k
  case tok::kw_if:
1071
2.09k
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration40
)
1072
      // field/method declaration.
1073
4
      break;
1074
2.08k
    parseIfThenElse();
1075
2.08k
    return;
1076
711
  case tok::kw_for:
1077
1.01k
  case tok::kw_while:
1078
1.01k
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration56
)
1079
      // field/method declaration.
1080
8
      break;
1081
1.00k
    parseForOrWhileLoop();
1082
1.00k
    return;
1083
195
  case tok::kw_do:
1084
195
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration4
)
1085
      // field/method declaration.
1086
4
      break;
1087
191
    parseDoWhile();
1088
191
    return;
1089
450
  case tok::kw_switch:
1090
450
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration8
)
1091
      // 'switch: string' field declaration.
1092
4
      break;
1093
446
    parseSwitch();
1094
446
    return;
1095
266
  case tok::kw_default:
1096
266
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration4
)
1097
      // 'default: string' field declaration.
1098
4
      break;
1099
262
    nextToken();
1100
262
    if (FormatTok->is(tok::colon)) {
1101
260
      parseLabel();
1102
260
      return;
1103
260
    }
1104
    // e.g. "default void f() {}" in a Java interface.
1105
2
    break;
1106
767
  case tok::kw_case:
1107
767
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration8
)
1108
      // 'case: string' field declaration.
1109
4
      break;
1110
763
    parseCaseLabel();
1111
763
    return;
1112
161
  case tok::kw_try:
1113
189
  case tok::kw___try:
1114
189
    if (Style.Language == FormatStyle::LK_JavaScript && 
Line->MustBeDeclaration8
)
1115
      // field/method declaration.
1116
4
      break;
1117
185
    parseTryCatch();
1118
185
    return;
1119
248
  case tok::kw_extern:
1120
248
    nextToken();
1121
248
    if (FormatTok->Tok.is(tok::string_literal)) {
1122
189
      nextToken();
1123
189
      if (FormatTok->Tok.is(tok::l_brace)) {
1124
137
        if (!Style.IndentExternBlock) {
1125
109
          if (Style.BraceWrapping.AfterExternBlock) {
1126
45
            addUnwrappedLine();
1127
45
          }
1128
109
          parseBlock(/*MustBeDeclaration=*/true,
1129
109
                     /*AddLevel=*/Style.BraceWrapping.AfterExternBlock);
1130
28
        } else {
1131
28
          parseBlock(/*MustBeDeclaration=*/true,
1132
28
                     /*AddLevel=*/Style.IndentExternBlock ==
1133
28
                         FormatStyle::IEBS_Indent);
1134
28
        }
1135
137
        addUnwrappedLine();
1136
137
        return;
1137
137
      }
1138
111
    }
1139
111
    break;
1140
211
  case tok::kw_export:
1141
211
    if (Style.Language == FormatStyle::LK_JavaScript) {
1142
182
      parseJavaScriptEs6ImportExport();
1143
182
      return;
1144
182
    }
1145
29
    if (!Style.isCpp())
1146
0
      break;
1147
    // Handle C++ "(inline|export) namespace".
1148
29
    LLVM_FALLTHROUGH;
1149
85
  case tok::kw_inline:
1150
85
    nextToken();
1151
85
    if (FormatTok->Tok.is(tok::kw_namespace)) {
1152
51
      parseNamespace();
1153
51
      return;
1154
51
    }
1155
34
    break;
1156
21.2k
  case tok::identifier:
1157
21.2k
    if (FormatTok->is(TT_ForEachMacro)) {
1158
48
      parseForOrWhileLoop();
1159
48
      return;
1160
48
    }
1161
21.2k
    if (FormatTok->is(TT_MacroBlockBegin)) {
1162
0
      parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
1163
0
                 /*MunchSemi=*/false);
1164
0
      return;
1165
0
    }
1166
21.2k
    if (FormatTok->is(Keywords.kw_import)) {
1167
324
      if (Style.Language == FormatStyle::LK_JavaScript) {
1168
221
        parseJavaScriptEs6ImportExport();
1169
221
        return;
1170
221
      }
1171
103
      if (Style.Language == FormatStyle::LK_Proto) {
1172
12
        nextToken();
1173
12
        if (FormatTok->is(tok::kw_public))
1174
2
          nextToken();
1175
12
        if (!FormatTok->is(tok::string_literal))
1176
0
          return;
1177
12
        nextToken();
1178
12
        if (FormatTok->is(tok::semi))
1179
8
          nextToken();
1180
12
        addUnwrappedLine();
1181
12
        return;
1182
12
      }
1183
103
    }
1184
20.9k
    if (Style.isCpp() &&
1185
17.8k
        FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
1186
35
                           Keywords.kw_slots, Keywords.kw_qslots)) {
1187
35
      nextToken();
1188
35
      if (FormatTok->is(tok::colon)) {
1189
14
        nextToken();
1190
14
        addUnwrappedLine();
1191
14
        return;
1192
14
      }
1193
20.9k
    }
1194
20.9k
    if (Style.isCpp() && 
FormatTok->is(TT_StatementMacro)17.8k
) {
1195
70
      parseStatementMacro();
1196
70
      return;
1197
70
    }
1198
20.9k
    if (Style.isCpp() && 
FormatTok->is(TT_NamespaceMacro)17.7k
) {
1199
138
      parseNamespace();
1200
138
      return;
1201
138
    }
1202
    // In all other cases, parse the declaration.
1203
20.7k
    break;
1204
45.3k
  default:
1205
45.3k
    break;
1206
66.3k
  }
1207
286k
  
do 66.3k
{
1208
286k
    const FormatToken *Previous = FormatTok->Previous;
1209
286k
    switch (FormatTok->Tok.getKind()) {
1210
878
    case tok::at:
1211
878
      nextToken();
1212
878
      if (FormatTok->Tok.is(tok::l_brace)) {
1213
47
        nextToken();
1214
47
        parseBracedList();
1215
47
        break;
1216
831
      } else if (Style.Language == FormatStyle::LK_Java &&
1217
82
                 FormatTok->is(Keywords.kw_interface)) {
1218
8
        nextToken();
1219
8
        break;
1220
8
      }
1221
823
      switch (FormatTok->Tok.getObjCKeywordID()) {
1222
10
      case tok::objc_public:
1223
20
      case tok::objc_protected:
1224
30
      case tok::objc_package:
1225
61
      case tok::objc_private:
1226
61
        return parseAccessSpecifier();
1227
140
      case tok::objc_interface:
1228
183
      case tok::objc_implementation:
1229
183
        return parseObjCInterfaceOrImplementation();
1230
25
      case tok::objc_protocol:
1231
25
        if (parseObjCProtocol())
1232
23
          return;
1233
2
        break;
1234
179
      case tok::objc_end:
1235
179
        return; // Handled by the caller.
1236
8
      case tok::objc_optional:
1237
12
      case tok::objc_required:
1238
12
        nextToken();
1239
12
        addUnwrappedLine();
1240
12
        return;
1241
26
      case tok::objc_autoreleasepool:
1242
26
        nextToken();
1243
26
        if (FormatTok->Tok.is(tok::l_brace)) {
1244
24
          if (Style.BraceWrapping.AfterControlStatement ==
1245
24
              FormatStyle::BWACS_Always)
1246
4
            addUnwrappedLine();
1247
24
          parseBlock(/*MustBeDeclaration=*/false);
1248
24
        }
1249
26
        addUnwrappedLine();
1250
26
        return;
1251
12
      case tok::objc_synchronized:
1252
12
        nextToken();
1253
12
        if (FormatTok->Tok.is(tok::l_paren))
1254
          // Skip synchronization object
1255
10
          parseParens();
1256
12
        if (FormatTok->Tok.is(tok::l_brace)) {
1257
10
          if (Style.BraceWrapping.AfterControlStatement ==
1258
10
              FormatStyle::BWACS_Always)
1259
4
            addUnwrappedLine();
1260
10
          parseBlock(/*MustBeDeclaration=*/false);
1261
10
        }
1262
12
        addUnwrappedLine();
1263
12
        return;
1264
14
      case tok::objc_try:
1265
        // This branch isn't strictly necessary (the kw_try case below would
1266
        // do this too after the tok::at is parsed above).  But be explicit.
1267
14
        parseTryCatch();
1268
14
        return;
1269
311
      default:
1270
311
        break;
1271
313
      }
1272
313
      break;
1273
116
    case tok::kw_concept:
1274
116
      parseConcept();
1275
116
      break;
1276
161
    case tok::kw_requires:
1277
161
      parseRequires();
1278
161
      break;
1279
1.90k
    case tok::kw_enum:
1280
      // Ignore if this is part of "template <enum ...".
1281
1.90k
      if (Previous && 
Previous->is(tok::less)96
) {
1282
14
        nextToken();
1283
14
        break;
1284
14
      }
1285
1286
      // parseEnum falls through and does not yet add an unwrapped line as an
1287
      // enum definition can start a structural element.
1288
1.89k
      if (!parseEnum())
1289
42
        break;
1290
      // This only applies for C++.
1291
1.84k
      if (!Style.isCpp()) {
1292
56
        addUnwrappedLine();
1293
56
        return;
1294
56
      }
1295
1.79k
      break;
1296
810
    case tok::kw_typedef:
1297
810
      nextToken();
1298
810
      if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1299
810
                             Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS,
1300
810
                             Keywords.kw_CF_CLOSED_ENUM,
1301
810
                             Keywords.kw_NS_CLOSED_ENUM))
1302
58
        parseEnum();
1303
810
      break;
1304
2.93k
    case tok::kw_struct:
1305
3.07k
    case tok::kw_union:
1306
6.99k
    case tok::kw_class:
1307
      // parseRecord falls through and does not yet add an unwrapped line as a
1308
      // record declaration or definition can start a structural element.
1309
6.99k
      parseRecord();
1310
      // This does not apply for Java, JavaScript and C#.
1311
6.99k
      if (Style.Language == FormatStyle::LK_Java ||
1312
6.95k
          Style.Language == FormatStyle::LK_JavaScript || 
Style.isCSharp()6.72k
) {
1313
329
        if (FormatTok->is(tok::semi))
1314
8
          nextToken();
1315
329
        addUnwrappedLine();
1316
329
        return;
1317
329
      }
1318
6.66k
      break;
1319
2.06k
    case tok::period:
1320
2.06k
      nextToken();
1321
      // In Java, classes have an implicit static member "class".
1322
2.06k
      if (Style.Language == FormatStyle::LK_Java && 
FormatTok184
&&
1323
184
          FormatTok->is(tok::kw_class))
1324
4
        nextToken();
1325
2.06k
      if (Style.Language == FormatStyle::LK_JavaScript && 
FormatTok470
&&
1326
470
          FormatTok->Tok.getIdentifierInfo())
1327
        // JavaScript only has pseudo keywords, all keywords are allowed to
1328
        // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1329
450
        nextToken();
1330
2.06k
      break;
1331
47.5k
    case tok::semi:
1332
47.5k
      nextToken();
1333
47.5k
      addUnwrappedLine();
1334
47.5k
      return;
1335
480
    case tok::r_brace:
1336
480
      addUnwrappedLine();
1337
480
      return;
1338
27.6k
    case tok::l_paren:
1339
27.6k
      parseParens();
1340
27.6k
      break;
1341
1.75k
    case tok::kw_operator:
1342
1.75k
      nextToken();
1343
1.75k
      if (FormatTok->isBinaryOperator())
1344
782
        nextToken();
1345
1.75k
      break;
1346
144
    case tok::caret:
1347
144
      nextToken();
1348
144
      if (FormatTok->Tok.isAnyIdentifier() ||
1349
95
          FormatTok->isSimpleTypeSpecifier())
1350
63
        nextToken();
1351
144
      if (FormatTok->is(tok::l_paren))
1352
46
        parseParens();
1353
144
      if (FormatTok->is(tok::l_brace))
1354
79
        parseChildBlock();
1355
144
      break;
1356
10.9k
    case tok::l_brace:
1357
10.9k
      if (!tryToParsePropertyAccessor() && 
!tryToParseBracedList()10.8k
) {
1358
        // A block outside of parentheses must be the last part of a
1359
        // structural element.
1360
        // FIXME: Figure out cases where this is not true, and add projections
1361
        // for them (the one we know is missing are lambdas).
1362
9.69k
        if (Style.BraceWrapping.AfterFunction)
1363
977
          addUnwrappedLine();
1364
9.69k
        FormatTok->setType(TT_FunctionLBrace);
1365
9.69k
        parseBlock(/*MustBeDeclaration=*/false);
1366
9.69k
        addUnwrappedLine();
1367
9.69k
        return;
1368
9.69k
      }
1369
      // Otherwise this was a braced init list, and the structural
1370
      // element continues.
1371
1.22k
      break;
1372
46
    case tok::kw_try:
1373
46
      if (Style.Language == FormatStyle::LK_JavaScript &&
1374
4
          Line->MustBeDeclaration) {
1375
        // field/method declaration.
1376
4
        nextToken();
1377
4
        break;
1378
4
      }
1379
      // We arrive here when parsing function-try blocks.
1380
42
      if (Style.BraceWrapping.AfterFunction)
1381
3
        addUnwrappedLine();
1382
42
      parseTryCatch();
1383
42
      return;
1384
80.6k
    case tok::identifier: {
1385
80.6k
      if (Style.isCSharp() && 
FormatTok->is(Keywords.kw_where)537
&&
1386
2
          Line->MustBeDeclaration) {
1387
2
        addUnwrappedLine();
1388
2
        parseCSharpGenericTypeConstraint();
1389
2
        break;
1390
2
      }
1391
80.6k
      if (FormatTok->is(TT_MacroBlockEnd)) {
1392
8
        addUnwrappedLine();
1393
8
        return;
1394
8
      }
1395
1396
      // Function declarations (as opposed to function expressions) are parsed
1397
      // on their own unwrapped line by continuing this loop. Function
1398
      // expressions (functions that are not on their own line) must not create
1399
      // a new unwrapped line, so they are special cased below.
1400
80.6k
      size_t TokenCount = Line->Tokens.size();
1401
80.6k
      if (Style.Language == FormatStyle::LK_JavaScript &&
1402
5.45k
          FormatTok->is(Keywords.kw_function) &&
1403
276
          (TokenCount > 1 || 
(208
TokenCount == 1208
&& !Line->Tokens.front().Tok->is(
1404
88
                                                     Keywords.kw_async)))) {
1405
88
        tryToParseJSFunction();
1406
88
        break;
1407
88
      }
1408
80.5k
      if ((Style.Language == FormatStyle::LK_JavaScript ||
1409
75.1k
           Style.Language == FormatStyle::LK_Java) &&
1410
6.33k
          FormatTok->is(Keywords.kw_interface)) {
1411
52
        if (Style.Language == FormatStyle::LK_JavaScript) {
1412
          // In JavaScript/TypeScript, "interface" can be used as a standalone
1413
          // identifier, e.g. in `var interface = 1;`. If "interface" is
1414
          // followed by another identifier, it is very like to be an actual
1415
          // interface declaration.
1416
48
          unsigned StoredPosition = Tokens->getPosition();
1417
48
          FormatToken *Next = Tokens->getNextToken();
1418
48
          FormatTok = Tokens->setPosition(StoredPosition);
1419
48
          if (Next && !mustBeJSIdent(Keywords, Next)) {
1420
16
            nextToken();
1421
16
            break;
1422
16
          }
1423
36
        }
1424
36
        parseRecord();
1425
36
        addUnwrappedLine();
1426
36
        return;
1427
36
      }
1428
1429
80.4k
      if (Style.isCpp() && 
FormatTok->is(TT_StatementMacro)73.1k
) {
1430
7
        parseStatementMacro();
1431
7
        return;
1432
7
      }
1433
1434
      // See if the following token should start a new unwrapped line.
1435
80.4k
      StringRef Text = FormatTok->TokenText;
1436
80.4k
      nextToken();
1437
1438
      // JS doesn't have macros, and within classes colons indicate fields, not
1439
      // labels.
1440
80.4k
      if (Style.Language == FormatStyle::LK_JavaScript)
1441
5.32k
        break;
1442
1443
75.1k
      TokenCount = Line->Tokens.size();
1444
75.1k
      if (TokenCount == 1 ||
1445
57.0k
          (TokenCount == 2 && 
Line->Tokens.front().Tok->is(tok::comment)23.8k
)) {
1446
18.0k
        if (FormatTok->Tok.is(tok::colon) && 
!Line->MustBeDeclaration140
) {
1447
126
          Line->Tokens.begin()->Tok->MustBreakBefore = true;
1448
126
          parseLabel(!Style.IndentGotoLabels);
1449
126
          return;
1450
126
        }
1451
        // Recognize function-like macro usages without trailing semicolon as
1452
        // well as free-standing macros like Q_OBJECT.
1453
17.9k
        bool FunctionLike = FormatTok->is(tok::l_paren);
1454
17.9k
        if (FunctionLike)
1455
7.36k
          parseParens();
1456
1457
17.9k
        bool FollowedByNewline =
1458
17.9k
            CommentsBeforeNextToken.empty()
1459
17.9k
                ? FormatTok->NewlinesBefore > 0
1460
12
                : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1461
1462
17.9k
        if (FollowedByNewline && 
(482
Text.size() >= 5482
||
FunctionLike132
) &&
1463
476
            tokenCanStartNewLine(*FormatTok) && 
Text == Text.upper()211
) {
1464
92
          addUnwrappedLine();
1465
92
          return;
1466
92
        }
1467
74.9k
      }
1468
74.9k
      break;
1469
74.9k
    }
1470
10.1k
    case tok::equal:
1471
      // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1472
      // TT_JsFatArrow. The always start an expression or a child block if
1473
      // followed by a curly.
1474
10.1k
      if (FormatTok->is(TT_JsFatArrow)) {
1475
96
        nextToken();
1476
96
        if (FormatTok->is(tok::l_brace)) {
1477
          // C# may break after => if the next character is a newline.
1478
68
          if (Style.isCSharp() && 
Style.BraceWrapping.AfterFunction == true4
) {
1479
            // calling `addUnwrappedLine()` here causes odd parsing errors.
1480
2
            FormatTok->MustBreakBefore = true;
1481
2
          }
1482
68
          parseChildBlock();
1483
68
        }
1484
96
        break;
1485
96
      }
1486
1487
10.0k
      nextToken();
1488
10.0k
      if (FormatTok->Tok.is(tok::l_brace)) {
1489
        // Block kind should probably be set to BK_BracedInit for any language.
1490
        // C# needs this change to ensure that array initialisers and object
1491
        // initialisers are indented the same way.
1492
947
        if (Style.isCSharp())
1493
4
          FormatTok->setBlockKind(BK_BracedInit);
1494
947
        nextToken();
1495
947
        parseBracedList();
1496
9.14k
      } else if (Style.Language == FormatStyle::LK_Proto &&
1497
95
                 FormatTok->Tok.is(tok::less)) {
1498
22
        nextToken();
1499
22
        parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
1500
22
                        /*ClosingBraceKind=*/tok::greater);
1501
22
      }
1502
10.0k
      break;
1503
3.15k
    case tok::l_square:
1504
3.15k
      parseSquare();
1505
3.15k
      break;
1506
297
    case tok::kw_new:
1507
297
      parseNew();
1508
297
      break;
1509
90.8k
    default:
1510
90.8k
      nextToken();
1511
90.8k
      break;
1512
227k
    }
1513
227k
  } while (!eof());
1514
66.3k
}
1515
1516
10.9k
bool UnwrappedLineParser::tryToParsePropertyAccessor() {
1517
10.9k
  assert(FormatTok->is(tok::l_brace));
1518
10.9k
  if (!Style.isCSharp())
1519
10.7k
    return false;
1520
  // See if it's a property accessor.
1521
170
  if (FormatTok->Previous->isNot(tok::identifier))
1522
116
    return false;
1523
1524
  // See if we are inside a property accessor.
1525
  //
1526
  // Record the current tokenPosition so that we can advance and
1527
  // reset the current token. `Next` is not set yet so we need
1528
  // another way to advance along the token stream.
1529
54
  unsigned int StoredPosition = Tokens->getPosition();
1530
54
  FormatToken *Tok = Tokens->getNextToken();
1531
1532
  // A trivial property accessor is of the form:
1533
  // { [ACCESS_SPECIFIER] [get]; [ACCESS_SPECIFIER] [set] }
1534
  // Track these as they do not require line breaks to be introduced.
1535
54
  bool HasGetOrSet = false;
1536
54
  bool IsTrivialPropertyAccessor = true;
1537
184
  while (!eof()) {
1538
184
    if (Tok->isOneOf(tok::semi, tok::kw_public, tok::kw_private,
1539
184
                     tok::kw_protected, Keywords.kw_internal, Keywords.kw_get,
1540
130
                     Keywords.kw_set)) {
1541
130
      if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_set))
1542
70
        HasGetOrSet = true;
1543
130
      Tok = Tokens->getNextToken();
1544
130
      continue;
1545
130
    }
1546
54
    if (Tok->isNot(tok::r_brace))
1547
12
      IsTrivialPropertyAccessor = false;
1548
54
    break;
1549
54
  }
1550
1551
54
  if (!HasGetOrSet) {
1552
6
    Tokens->setPosition(StoredPosition);
1553
6
    return false;
1554
6
  }
1555
1556
  // Try to parse the property accessor:
1557
  // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
1558
48
  Tokens->setPosition(StoredPosition);
1559
48
  if (!IsTrivialPropertyAccessor && 
Style.BraceWrapping.AfterFunction == true10
)
1560
2
    addUnwrappedLine();
1561
48
  nextToken();
1562
208
  do {
1563
208
    switch (FormatTok->Tok.getKind()) {
1564
48
    case tok::r_brace:
1565
48
      nextToken();
1566
48
      if (FormatTok->is(tok::equal)) {
1567
66
        while (!eof() && 
FormatTok->isNot(tok::semi)60
)
1568
56
          nextToken();
1569
10
        nextToken();
1570
10
      }
1571
48
      addUnwrappedLine();
1572
48
      return true;
1573
8
    case tok::l_brace:
1574
8
      ++Line->Level;
1575
8
      parseBlock(/*MustBeDeclaration=*/true);
1576
8
      addUnwrappedLine();
1577
8
      --Line->Level;
1578
8
      break;
1579
12
    case tok::equal:
1580
12
      if (FormatTok->is(TT_JsFatArrow)) {
1581
12
        ++Line->Level;
1582
36
        do {
1583
36
          nextToken();
1584
36
        } while (!eof() && FormatTok->isNot(tok::semi));
1585
12
        nextToken();
1586
12
        addUnwrappedLine();
1587
12
        --Line->Level;
1588
12
        break;
1589
12
      }
1590
0
      nextToken();
1591
0
      break;
1592
140
    default:
1593
140
      if (FormatTok->isOneOf(Keywords.kw_get, Keywords.kw_set) &&
1594
80
          !IsTrivialPropertyAccessor) {
1595
        // Non-trivial get/set needs to be on its own line.
1596
20
        addUnwrappedLine();
1597
20
      }
1598
140
      nextToken();
1599
208
    }
1600
160
  } while (!eof());
1601
1602
  // Unreachable for well-formed code (paired '{' and '}').
1603
0
  return true;
1604
48
}
1605
1606
5.47k
bool UnwrappedLineParser::tryToParseLambda() {
1607
5.47k
  if (!Style.isCpp()) {
1608
580
    nextToken();
1609
580
    return false;
1610
580
  }
1611
4.89k
  assert(FormatTok->is(tok::l_square));
1612
4.89k
  FormatToken &LSquare = *FormatTok;
1613
4.89k
  if (!tryToParseLambdaIntroducer())
1614
2.21k
    return false;
1615
1616
2.67k
  bool SeenArrow = false;
1617
1618
7.14k
  while (FormatTok->isNot(tok::l_brace)) {
1619
5.40k
    if (FormatTok->isSimpleTypeSpecifier()) {
1620
70
      nextToken();
1621
70
      continue;
1622
70
    }
1623
5.33k
    switch (FormatTok->Tok.getKind()) {
1624
0
    case tok::l_brace:
1625
0
      break;
1626
1.48k
    case tok::l_paren:
1627
1.48k
      parseParens();
1628
1.48k
      break;
1629
23
    case tok::amp:
1630
74
    case tok::star:
1631
81
    case tok::kw_const:
1632
103
    case tok::comma:
1633
525
    case tok::less:
1634
964
    case tok::greater:
1635
1.51k
    case tok::identifier:
1636
2.02k
    case tok::numeric_constant:
1637
2.04k
    case tok::coloncolon:
1638
2.06k
    case tok::kw_class:
1639
2.07k
    case tok::kw_mutable:
1640
2.08k
    case tok::kw_noexcept:
1641
2.09k
    case tok::kw_template:
1642
2.09k
    case tok::kw_typename:
1643
2.09k
      nextToken();
1644
2.09k
      break;
1645
    // Specialization of a template with an integer parameter can contain
1646
    // arithmetic, logical, comparison and ternary operators.
1647
    //
1648
    // FIXME: This also accepts sequences of operators that are not in the scope
1649
    // of a template argument list.
1650
    //
1651
    // In a C++ lambda a template type can only occur after an arrow. We use
1652
    // this as an heuristic to distinguish between Objective-C expressions
1653
    // followed by an `a->b` expression, such as:
1654
    // ([obj func:arg] + a->b)
1655
    // Otherwise the code below would parse as a lambda.
1656
    //
1657
    // FIXME: This heuristic is incorrect for C++20 generic lambdas with
1658
    // explicit template lists: []<bool b = true && false>(U &&u){}
1659
22
    case tok::plus:
1660
40
    case tok::minus:
1661
54
    case tok::exclaim:
1662
68
    case tok::tilde:
1663
86
    case tok::slash:
1664
104
    case tok::percent:
1665
122
    case tok::lessless:
1666
140
    case tok::pipe:
1667
158
    case tok::pipepipe:
1668
178
    case tok::ampamp:
1669
180
    case tok::caret:
1670
206
    case tok::equalequal:
1671
224
    case tok::exclaimequal:
1672
242
    case tok::greaterequal:
1673
260
    case tok::lessequal:
1674
281
    case tok::question:
1675
315
    case tok::colon:
1676
329
    case tok::ellipsis:
1677
357
    case tok::kw_true:
1678
399
    case tok::kw_false:
1679
399
      if (SeenArrow) {
1680
308
        nextToken();
1681
308
        break;
1682
308
      }
1683
91
      return true;
1684
513
    case tok::arrow:
1685
      // This might or might not actually be a lambda arrow (this could be an
1686
      // ObjC method invocation followed by a dereferencing arrow). We might
1687
      // reset this back to TT_Unknown in TokenAnnotator.
1688
513
      FormatTok->setType(TT_LambdaArrow);
1689
513
      SeenArrow = true;
1690
513
      nextToken();
1691
513
      break;
1692
842
    default:
1693
842
      return true;
1694
5.33k
    }
1695
5.33k
  }
1696
1.74k
  FormatTok->setType(TT_LambdaLBrace);
1697
1.74k
  LSquare.setType(TT_LambdaLSquare);
1698
1.74k
  parseChildBlock();
1699
1.74k
  return true;
1700
2.67k
}
1701
1702
4.89k
bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1703
4.89k
  const FormatToken *Previous = FormatTok->Previous;
1704
4.89k
  if (Previous &&
1705
4.01k
      (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1706
4.01k
                         tok::kw_delete, tok::l_square) ||
1707
2.73k
       FormatTok->isCppStructuredBinding(Style) || 
Previous->closesScope()2.53k
||
1708
2.16k
       Previous->isSimpleTypeSpecifier())) {
1709
1.88k
    nextToken();
1710
1.88k
    return false;
1711
1.88k
  }
1712
3.00k
  nextToken();
1713
3.00k
  if (FormatTok->is(tok::l_square)) {
1714
333
    return false;
1715
333
  }
1716
2.67k
  parseSquare(/*LambdaIntroducer=*/true);
1717
2.67k
  return true;
1718
2.67k
}
1719
1720
288
void UnwrappedLineParser::tryToParseJSFunction() {
1721
288
  assert(FormatTok->is(Keywords.kw_function) ||
1722
288
         FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
1723
288
  if (FormatTok->is(Keywords.kw_async))
1724
0
    nextToken();
1725
  // Consume "function".
1726
288
  nextToken();
1727
1728
  // Consume * (generator function). Treat it like C++'s overloaded operators.
1729
288
  if (FormatTok->is(tok::star)) {
1730
4
    FormatTok->setType(TT_OverloadedOperator);
1731
4
    nextToken();
1732
4
  }
1733
1734
  // Consume function name.
1735
288
  if (FormatTok->is(tok::identifier))
1736
48
    nextToken();
1737
1738
288
  if (FormatTok->isNot(tok::l_paren))
1739
0
    return;
1740
1741
  // Parse formal parameter list.
1742
288
  parseParens();
1743
1744
288
  if (FormatTok->is(tok::colon)) {
1745
    // Parse a type definition.
1746
16
    nextToken();
1747
1748
    // Eat the type declaration. For braced inline object types, balance braces,
1749
    // otherwise just parse until finding an l_brace for the function body.
1750
16
    if (FormatTok->is(tok::l_brace))
1751
8
      tryToParseBracedList();
1752
8
    else
1753
16
      
while (8
!FormatTok->isOneOf(tok::l_brace, tok::semi) &&
!eof()8
)
1754
8
        nextToken();
1755
16
  }
1756
1757
288
  if (FormatTok->is(tok::semi))
1758
16
    return;
1759
1760
272
  parseChildBlock();
1761
272
}
1762
1763
13.2k
bool UnwrappedLineParser::tryToParseBracedList() {
1764
13.2k
  if (FormatTok->is(BK_Unknown))
1765
11.7k
    calculateBraceTypes();
1766
13.2k
  assert(FormatTok->isNot(BK_Unknown));
1767
13.2k
  if (FormatTok->is(BK_Block))
1768
11.6k
    return false;
1769
1.62k
  nextToken();
1770
1.62k
  parseBracedList();
1771
1.62k
  return true;
1772
1.62k
}
1773
1774
bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1775
                                          bool IsEnum,
1776
6.92k
                                          tok::TokenKind ClosingBraceKind) {
1777
6.92k
  bool HasError = false;
1778
1779
  // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1780
  // replace this by using parseAssigmentExpression() inside.
1781
37.1k
  do {
1782
37.1k
    if (Style.isCSharp()) {
1783
290
      if (FormatTok->is(TT_JsFatArrow)) {
1784
4
        nextToken();
1785
        // Fat arrows can be followed by simple expressions or by child blocks
1786
        // in curly braces.
1787
4
        if (FormatTok->is(tok::l_brace)) {
1788
4
          parseChildBlock();
1789
4
          continue;
1790
4
        }
1791
37.1k
      }
1792
290
    }
1793
37.1k
    if (Style.Language == FormatStyle::LK_JavaScript) {
1794
3.69k
      if (FormatTok->is(Keywords.kw_function) ||
1795
3.63k
          FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
1796
60
        tryToParseJSFunction();
1797
60
        continue;
1798
60
      }
1799
3.63k
      if (FormatTok->is(TT_JsFatArrow)) {
1800
12
        nextToken();
1801
        // Fat arrows can be followed by simple expressions or by child blocks
1802
        // in curly braces.
1803
12
        if (FormatTok->is(tok::l_brace)) {
1804
4
          parseChildBlock();
1805
4
          continue;
1806
4
        }
1807
3.63k
      }
1808
3.63k
      if (FormatTok->is(tok::l_brace)) {
1809
        // Could be a method inside of a braced list `{a() { return 1; }}`.
1810
32
        if (tryToParseBracedList())
1811
28
          continue;
1812
4
        parseChildBlock();
1813
4
      }
1814
3.63k
    }
1815
37.0k
    if (FormatTok->Tok.getKind() == ClosingBraceKind) {
1816
6.27k
      if (IsEnum && 
!Style.AllowShortEnumsOnASingleLine1.84k
)
1817
9
        addUnwrappedLine();
1818
6.27k
      nextToken();
1819
6.27k
      return !HasError;
1820
6.27k
    }
1821
30.7k
    switch (FormatTok->Tok.getKind()) {
1822
0
    case tok::caret:
1823
0
      nextToken();
1824
0
      if (FormatTok->is(tok::l_brace)) {
1825
0
        parseChildBlock();
1826
0
      }
1827
0
      break;
1828
489
    case tok::l_square:
1829
489
      if (Style.isCSharp())
1830
8
        parseSquare();
1831
481
      else
1832
481
        tryToParseLambda();
1833
489
      break;
1834
320
    case tok::l_paren:
1835
320
      parseParens();
1836
      // JavaScript can just have free standing methods and getters/setters in
1837
      // object literals. Detect them by a "{" following ")".
1838
320
      if (Style.Language == FormatStyle::LK_JavaScript) {
1839
40
        if (FormatTok->is(tok::l_brace))
1840
24
          parseChildBlock();
1841
40
        break;
1842
40
      }
1843
280
      break;
1844
1.12k
    case tok::l_brace:
1845
      // Assume there are no blocks inside a braced init list apart
1846
      // from the ones we explicitly parse out (like lambdas).
1847
1.12k
      FormatTok->setBlockKind(BK_BracedInit);
1848
1.12k
      nextToken();
1849
1.12k
      parseBracedList();
1850
1.12k
      break;
1851
710
    case tok::less:
1852
710
      if (Style.Language == FormatStyle::LK_Proto) {
1853
56
        nextToken();
1854
56
        parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
1855
56
                        /*ClosingBraceKind=*/tok::greater);
1856
654
      } else {
1857
654
        nextToken();
1858
654
      }
1859
710
      break;
1860
82
    case tok::semi:
1861
      // JavaScript (or more precisely TypeScript) can have semicolons in braced
1862
      // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1863
      // used for error recovery if we have otherwise determined that this is
1864
      // a braced list.
1865
82
      if (Style.Language == FormatStyle::LK_JavaScript) {
1866
36
        nextToken();
1867
36
        break;
1868
36
      }
1869
46
      HasError = true;
1870
46
      if (!ContinueOnSemicolons)
1871
25
        return !HasError;
1872
21
      nextToken();
1873
21
      break;
1874
7.00k
    case tok::comma:
1875
7.00k
      nextToken();
1876
7.00k
      if (IsEnum && 
!Style.AllowShortEnumsOnASingleLine1.31k
)
1877
20
        addUnwrappedLine();
1878
7.00k
      break;
1879
21.0k
    default:
1880
21.0k
      nextToken();
1881
21.0k
      break;
1882
30.8k
    }
1883
30.8k
  } while (!eof());
1884
623
  return false;
1885
6.92k
}
1886
1887
47.7k
void UnwrappedLineParser::parseParens() {
1888
47.7k
  assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
1889
47.7k
  nextToken();
1890
130k
  do {
1891
130k
    switch (FormatTok->Tok.getKind()) {
1892
4.61k
    case tok::l_paren:
1893
4.61k
      parseParens();
1894
4.61k
      if (Style.Language == FormatStyle::LK_Java && 
FormatTok->is(tok::l_brace)22
)
1895
10
        parseChildBlock();
1896
4.61k
      break;
1897
47.6k
    case tok::r_paren:
1898
47.6k
      nextToken();
1899
47.6k
      return;
1900
3
    case tok::r_brace:
1901
      // A "}" inside parenthesis is an error if there wasn't a matching "{".
1902
3
      return;
1903
1.37k
    case tok::l_square:
1904
1.37k
      tryToParseLambda();
1905
1.37k
      break;
1906
646
    case tok::l_brace:
1907
646
      if (!tryToParseBracedList())
1908
301
        parseChildBlock();
1909
646
      break;
1910
30
    case tok::at:
1911
30
      nextToken();
1912
30
      if (FormatTok->Tok.is(tok::l_brace)) {
1913
2
        nextToken();
1914
2
        parseBracedList();
1915
2
      }
1916
30
      break;
1917
67
    case tok::kw_class:
1918
67
      if (Style.Language == FormatStyle::LK_JavaScript)
1919
4
        parseRecord(/*ParseAsExpr=*/true);
1920
63
      else
1921
63
        nextToken();
1922
67
      break;
1923
38.7k
    case tok::identifier:
1924
38.7k
      if (Style.Language == FormatStyle::LK_JavaScript &&
1925
1.65k
          (FormatTok->is(Keywords.kw_function) ||
1926
1.51k
           FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
1927
140
        tryToParseJSFunction();
1928
38.5k
      else
1929
38.5k
        nextToken();
1930
38.7k
      break;
1931
36.9k
    default:
1932
36.9k
      nextToken();
1933
36.9k
      break;
1934
82.3k
    }
1935
82.3k
  } while (!eof());
1936
47.7k
}
1937
1938
6.29k
void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
1939
6.29k
  if (!LambdaIntroducer) {
1940
3.61k
    assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1941
3.61k
    if (tryToParseLambda())
1942
1.58k
      return;
1943
4.70k
  }
1944
13.7k
  
do 4.70k
{
1945
13.7k
    switch (FormatTok->Tok.getKind()) {
1946
341
    case tok::l_paren:
1947
341
      parseParens();
1948
341
      break;
1949
4.70k
    case tok::r_square:
1950
4.70k
      nextToken();
1951
4.70k
      return;
1952
0
    case tok::r_brace:
1953
      // A "}" inside parenthesis is an error if there wasn't a matching "{".
1954
0
      return;
1955
395
    case tok::l_square:
1956
395
      parseSquare();
1957
395
      break;
1958
220
    case tok::l_brace: {
1959
220
      if (!tryToParseBracedList())
1960
186
        parseChildBlock();
1961
220
      break;
1962
0
    }
1963
65
    case tok::at:
1964
65
      nextToken();
1965
65
      if (FormatTok->Tok.is(tok::l_brace)) {
1966
25
        nextToken();
1967
25
        parseBracedList();
1968
25
      }
1969
65
      break;
1970
7.98k
    default:
1971
7.98k
      nextToken();
1972
7.98k
      break;
1973
9.00k
    }
1974
9.00k
  } while (!eof());
1975
4.70k
}
1976
1977
2.29k
void UnwrappedLineParser::parseIfThenElse() {
1978
2.29k
  assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
1979
2.29k
  nextToken();
1980
2.29k
  if (FormatTok->Tok.isOneOf(tok::kw_constexpr, tok::identifier))
1981
385
    nextToken();
1982
2.29k
  if (FormatTok->Tok.is(tok::l_paren))
1983
2.28k
    parseParens();
1984
  // handle [[likely]] / [[unlikely]]
1985
2.29k
  if (FormatTok->is(tok::l_square) && 
tryToParseSimpleAttribute()54
)
1986
44
    parseSquare();
1987
2.29k
  bool NeedsUnwrappedLine = false;
1988
2.29k
  if (FormatTok->Tok.is(tok::l_brace)) {
1989
1.27k
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
1990
1.27k
    parseBlock(/*MustBeDeclaration=*/false);
1991
1.27k
    if (Style.BraceWrapping.BeforeElse)
1992
114
      addUnwrappedLine();
1993
1.16k
    else
1994
1.16k
      NeedsUnwrappedLine = true;
1995
1.02k
  } else {
1996
1.02k
    addUnwrappedLine();
1997
1.02k
    ++Line->Level;
1998
1.02k
    parseStructuralElement();
1999
1.02k
    --Line->Level;
2000
1.02k
  }
2001
2.29k
  if (FormatTok->Tok.is(tok::kw_else)) {
2002
552
    nextToken();
2003
    // handle [[likely]] / [[unlikely]]
2004
552
    if (FormatTok->Tok.is(tok::l_square) && 
tryToParseSimpleAttribute()11
)
2005
9
      parseSquare();
2006
552
    if (FormatTok->Tok.is(tok::l_brace)) {
2007
257
      CompoundStatementIndenter Indenter(this, Style, Line->Level);
2008
257
      parseBlock(/*MustBeDeclaration=*/false);
2009
257
      addUnwrappedLine();
2010
295
    } else if (FormatTok->Tok.is(tok::kw_if)) {
2011
207
      parseIfThenElse();
2012
88
    } else {
2013
88
      addUnwrappedLine();
2014
88
      ++Line->Level;
2015
88
      parseStructuralElement();
2016
88
      if (FormatTok->is(tok::eof))
2017
53
        addUnwrappedLine();
2018
88
      --Line->Level;
2019
88
    }
2020
1.74k
  } else if (NeedsUnwrappedLine) {
2021
865
    addUnwrappedLine();
2022
865
  }
2023
2.29k
}
2024
2025
241
void UnwrappedLineParser::parseTryCatch() {
2026
241
  assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
2027
241
  nextToken();
2028
241
  bool NeedsUnwrappedLine = false;
2029
241
  if (FormatTok->is(tok::colon)) {
2030
    // We are in a function try block, what comes is an initializer list.
2031
36
    nextToken();
2032
2033
    // In case identifiers were removed by clang-tidy, what might follow is
2034
    // multiple commas in sequence - before the first identifier.
2035
42
    while (FormatTok->is(tok::comma))
2036
6
      nextToken();
2037
2038
104
    while (FormatTok->is(tok::identifier)) {
2039
68
      nextToken();
2040
68
      if (FormatTok->is(tok::l_paren))
2041
33
        parseParens();
2042
68
      if (FormatTok->Previous && FormatTok->Previous->is(tok::identifier) &&
2043
35
          FormatTok->is(tok::l_brace)) {
2044
70
        do {
2045
70
          nextToken();
2046
70
        } while (!FormatTok->is(tok::r_brace));
2047
35
        nextToken();
2048
35
      }
2049
2050
      // In case identifiers were removed by clang-tidy, what might follow is
2051
      // multiple commas in sequence - after the first identifier.
2052
107
      while (FormatTok->is(tok::comma))
2053
39
        nextToken();
2054
68
    }
2055
36
  }
2056
  // Parse try with resource.
2057
241
  if (Style.Language == FormatStyle::LK_Java && 
FormatTok->is(tok::l_paren)12
) {
2058
4
    parseParens();
2059
4
  }
2060
241
  if (FormatTok->is(tok::l_brace)) {
2061
239
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2062
239
    parseBlock(/*MustBeDeclaration=*/false);
2063
239
    if (Style.BraceWrapping.BeforeCatch) {
2064
80
      addUnwrappedLine();
2065
159
    } else {
2066
159
      NeedsUnwrappedLine = true;
2067
159
    }
2068
2
  } else if (!FormatTok->is(tok::kw_catch)) {
2069
    // The C++ standard requires a compound-statement after a try.
2070
    // If there's none, we try to assume there's a structuralElement
2071
    // and try to continue.
2072
2
    addUnwrappedLine();
2073
2
    ++Line->Level;
2074
2
    parseStructuralElement();
2075
2
    --Line->Level;
2076
2
  }
2077
489
  while (1) {
2078
489
    if (FormatTok->is(tok::at))
2079
14
      nextToken();
2080
489
    if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
2081
489
                             tok::kw___finally) ||
2082
258
          ((Style.Language == FormatStyle::LK_Java ||
2083
242
            Style.Language == FormatStyle::LK_JavaScript) &&
2084
24
           FormatTok->is(Keywords.kw_finally)) ||
2085
250
          (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
2086
250
           FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
2087
238
      break;
2088
251
    nextToken();
2089
482
    while (FormatTok->isNot(tok::l_brace)) {
2090
234
      if (FormatTok->is(tok::l_paren)) {
2091
217
        parseParens();
2092
217
        continue;
2093
217
      }
2094
17
      if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
2095
3
        return;
2096
14
      nextToken();
2097
14
    }
2098
248
    NeedsUnwrappedLine = false;
2099
248
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2100
248
    parseBlock(/*MustBeDeclaration=*/false);
2101
248
    if (Style.BraceWrapping.BeforeCatch)
2102
80
      addUnwrappedLine();
2103
168
    else
2104
168
      NeedsUnwrappedLine = true;
2105
248
  }
2106
238
  if (NeedsUnwrappedLine)
2107
156
    addUnwrappedLine();
2108
238
}
2109
2110
3.41k
void UnwrappedLineParser::parseNamespace() {
2111
3.41k
  assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
2112
3.41k
         "'namespace' expected");
2113
2114
3.41k
  const FormatToken &InitialToken = *FormatTok;
2115
3.41k
  nextToken();
2116
3.41k
  if (InitialToken.is(TT_NamespaceMacro)) {
2117
138
    parseParens();
2118
3.27k
  } else {
2119
6.70k
    while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
2120
3.43k
                              tok::l_square)) {
2121
3.43k
      if (FormatTok->is(tok::l_square))
2122
8
        parseSquare();
2123
3.42k
      else
2124
3.42k
        nextToken();
2125
3.43k
    }
2126
3.27k
  }
2127
3.41k
  if (FormatTok->Tok.is(tok::l_brace)) {
2128
3.40k
    if (ShouldBreakBeforeBrace(Style, InitialToken))
2129
85
      addUnwrappedLine();
2130
2131
3.40k
    bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
2132
3.34k
                    (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
2133
78
                     DeclarationScopeStack.size() > 1);
2134
3.40k
    parseBlock(/*MustBeDeclaration=*/true, AddLevel);
2135
    // Munch the semicolon after a namespace. This is more common than one would
2136
    // think. Putting the semicolon into its own line is very ugly.
2137
3.40k
    if (FormatTok->Tok.is(tok::semi))
2138
1
      nextToken();
2139
3.40k
    addUnwrappedLine();
2140
3.40k
  }
2141
  // FIXME: Add error handling.
2142
3.41k
}
2143
2144
297
void UnwrappedLineParser::parseNew() {
2145
297
  assert(FormatTok->is(tok::kw_new) && "'new' expected");
2146
297
  nextToken();
2147
2148
297
  if (Style.isCSharp()) {
2149
128
    do {
2150
128
      if (FormatTok->is(tok::l_brace))
2151
18
        parseBracedList();
2152
2153
128
      if (FormatTok->isOneOf(tok::semi, tok::comma))
2154
20
        return;
2155
2156
108
      nextToken();
2157
108
    } while (!eof());
2158
32
  }
2159
2160
277
  if (Style.Language != FormatStyle::LK_Java)
2161
267
    return;
2162
2163
  // In Java, we can parse everything up to the parens, which aren't optional.
2164
32
  
do 10
{
2165
    // There should not be a ;, { or } before the new's open paren.
2166
32
    if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
2167
6
      return;
2168
2169
    // Consume the parens.
2170
26
    if (FormatTok->is(tok::l_paren)) {
2171
4
      parseParens();
2172
2173
      // If there is a class body of an anonymous class, consume that as child.
2174
4
      if (FormatTok->is(tok::l_brace))
2175
4
        parseChildBlock();
2176
4
      return;
2177
4
    }
2178
22
    nextToken();
2179
22
  } while (!eof());
2180
10
}
2181
2182
1.05k
void UnwrappedLineParser::parseForOrWhileLoop() {
2183
1.05k
  assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
2184
1.05k
         "'for', 'while' or foreach macro expected");
2185
1.05k
  nextToken();
2186
  // JS' for await ( ...
2187
1.05k
  if (Style.Language == FormatStyle::LK_JavaScript &&
2188
48
      FormatTok->is(Keywords.kw_await))
2189
8
    nextToken();
2190
1.05k
  if (FormatTok->Tok.is(tok::l_paren))
2191
1.03k
    parseParens();
2192
1.05k
  if (FormatTok->Tok.is(tok::l_brace)) {
2193
738
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2194
738
    parseBlock(/*MustBeDeclaration=*/false);
2195
738
    addUnwrappedLine();
2196
316
  } else {
2197
316
    addUnwrappedLine();
2198
316
    ++Line->Level;
2199
316
    parseStructuralElement();
2200
316
    --Line->Level;
2201
316
  }
2202
1.05k
}
2203
2204
191
void UnwrappedLineParser::parseDoWhile() {
2205
191
  assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
2206
191
  nextToken();
2207
191
  if (FormatTok->Tok.is(tok::l_brace)) {
2208
149
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2209
149
    parseBlock(/*MustBeDeclaration=*/false);
2210
149
    if (Style.BraceWrapping.BeforeWhile)
2211
14
      addUnwrappedLine();
2212
42
  } else {
2213
42
    addUnwrappedLine();
2214
42
    ++Line->Level;
2215
42
    parseStructuralElement();
2216
42
    --Line->Level;
2217
42
  }
2218
2219
  // FIXME: Add error handling.
2220
191
  if (!FormatTok->Tok.is(tok::kw_while)) {
2221
28
    addUnwrappedLine();
2222
28
    return;
2223
28
  }
2224
2225
163
  nextToken();
2226
163
  parseStructuralElement();
2227
163
}
2228
2229
1.14k
void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) {
2230
1.14k
  nextToken();
2231
1.14k
  unsigned OldLineLevel = Line->Level;
2232
1.14k
  if (Line->Level > 1 || 
(813
!Line->InPPDirective813
&&
Line->Level > 0813
))
2233
1.14k
    --Line->Level;
2234
1.14k
  if (LeftAlignLabel)
2235
21
    Line->Level = 0;
2236
2237
1.14k
  bool RemoveWhitesmithsCaseIndent =
2238
1.14k
      (!Style.IndentCaseBlocks &&
2239
1.08k
       Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths);
2240
2241
1.14k
  if (RemoveWhitesmithsCaseIndent)
2242
49
    --Line->Level;
2243
2244
1.14k
  if (!Style.IndentCaseBlocks && 
CommentsBeforeNextToken.empty()1.08k
&&
2245
1.05k
      FormatTok->Tok.is(tok::l_brace)) {
2246
2247
311
    CompoundStatementIndenter Indenter(
2248
311
        this, Line->Level, Style.BraceWrapping.AfterCaseLabel,
2249
311
        Style.BraceWrapping.IndentBraces || 
RemoveWhitesmithsCaseIndent297
);
2250
311
    parseBlock(/*MustBeDeclaration=*/false);
2251
311
    if (FormatTok->Tok.is(tok::kw_break)) {
2252
49
      if (Style.BraceWrapping.AfterControlStatement ==
2253
42
          FormatStyle::BWACS_Always) {
2254
42
        addUnwrappedLine();
2255
42
        if (RemoveWhitesmithsCaseIndent) {
2256
28
          Line->Level++;
2257
28
        }
2258
42
      }
2259
49
      parseStructuralElement();
2260
49
    }
2261
311
    addUnwrappedLine();
2262
838
  } else {
2263
838
    if (FormatTok->is(tok::semi))
2264
28
      nextToken();
2265
838
    addUnwrappedLine();
2266
838
  }
2267
1.14k
  Line->Level = OldLineLevel;
2268
1.14k
  if (FormatTok->isNot(tok::l_brace)) {
2269
1.09k
    parseStructuralElement();
2270
1.09k
    addUnwrappedLine();
2271
1.09k
  }
2272
1.14k
}
2273
2274
763
void UnwrappedLineParser::parseCaseLabel() {
2275
763
  assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
2276
2277
  // FIXME: fix handling of complex expressions here.
2278
1.65k
  do {
2279
1.65k
    nextToken();
2280
1.65k
  } while (!eof() && 
!FormatTok->Tok.is(tok::colon)1.64k
);
2281
763
  parseLabel();
2282
763
}
2283
2284
446
void UnwrappedLineParser::parseSwitch() {
2285
446
  assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
2286
446
  nextToken();
2287
446
  if (FormatTok->Tok.is(tok::l_paren))
2288
439
    parseParens();
2289
446
  if (FormatTok->Tok.is(tok::l_brace)) {
2290
425
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2291
425
    parseBlock(/*MustBeDeclaration=*/false);
2292
425
    addUnwrappedLine();
2293
21
  } else {
2294
21
    addUnwrappedLine();
2295
21
    ++Line->Level;
2296
21
    parseStructuralElement();
2297
21
    --Line->Level;
2298
21
  }
2299
446
}
2300
2301
968
void UnwrappedLineParser::parseAccessSpecifier() {
2302
968
  nextToken();
2303
  // Understand Qt's slots.
2304
968
  if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
2305
42
    nextToken();
2306
  // Otherwise, we don't know what it is, and we'd better keep the next token.
2307
968
  if (FormatTok->Tok.is(tok::colon))
2308
872
    nextToken();
2309
968
  addUnwrappedLine();
2310
968
}
2311
2312
116
void UnwrappedLineParser::parseConcept() {
2313
116
  assert(FormatTok->Tok.is(tok::kw_concept) && "'concept' expected");
2314
116
  nextToken();
2315
116
  if (!FormatTok->Tok.is(tok::identifier))
2316
4
    return;
2317
112
  nextToken();
2318
112
  if (!FormatTok->Tok.is(tok::equal))
2319
0
    return;
2320
112
  nextToken();
2321
112
  if (FormatTok->Tok.is(tok::kw_requires)) {
2322
42
    nextToken();
2323
42
    parseRequiresExpression(Line->Level);
2324
70
  } else {
2325
70
    parseConstraintExpression(Line->Level);
2326
70
  }
2327
112
}
2328
2329
203
void UnwrappedLineParser::parseRequiresExpression(unsigned int OriginalLevel) {
2330
  // requires (R range)
2331
203
  if (FormatTok->Tok.is(tok::l_paren)) {
2332
70
    parseParens();
2333
70
    if (Style.IndentRequires && 
OriginalLevel != Line->Level28
) {
2334
14
      addUnwrappedLine();
2335
14
      --Line->Level;
2336
14
    }
2337
70
  }
2338
2339
203
  if (FormatTok->Tok.is(tok::l_brace)) {
2340
49
    if (Style.BraceWrapping.AfterFunction)
2341
7
      addUnwrappedLine();
2342
49
    FormatTok->setType(TT_FunctionLBrace);
2343
49
    parseBlock(/*MustBeDeclaration=*/false);
2344
49
    addUnwrappedLine();
2345
154
  } else {
2346
154
    parseConstraintExpression(OriginalLevel);
2347
154
  }
2348
203
}
2349
2350
void UnwrappedLineParser::parseConstraintExpression(
2351
224
    unsigned int OriginalLevel) {
2352
  // requires Id<T> && Id<T> || Id<T>
2353
224
  while (
2354
476
      FormatTok->isOneOf(tok::identifier, tok::kw_requires, tok::coloncolon)) {
2355
434
    nextToken();
2356
1.07k
    while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::less,
2357
644
                              tok::greater, tok::comma, tok::ellipsis)) {
2358
644
      if (FormatTok->Tok.is(tok::less)) {
2359
364
        parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
2360
364
                        /*ClosingBraceKind=*/tok::greater);
2361
364
        continue;
2362
364
      }
2363
280
      nextToken();
2364
280
    }
2365
434
    if (FormatTok->Tok.is(tok::kw_requires)) {
2366
0
      parseRequiresExpression(OriginalLevel);
2367
0
    }
2368
434
    if (FormatTok->Tok.is(tok::less)) {
2369
0
      parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
2370
0
                      /*ClosingBraceKind=*/tok::greater);
2371
0
    }
2372
2373
434
    if (FormatTok->Tok.is(tok::l_paren)) {
2374
70
      parseParens();
2375
70
    }
2376
434
    if (FormatTok->Tok.is(tok::l_brace)) {
2377
133
      if (Style.BraceWrapping.AfterFunction)
2378
7
        addUnwrappedLine();
2379
133
      FormatTok->setType(TT_FunctionLBrace);
2380
133
      parseBlock(/*MustBeDeclaration=*/false);
2381
133
    }
2382
434
    if (FormatTok->Tok.is(tok::semi)) {
2383
      // Eat any trailing semi.
2384
21
      nextToken();
2385
21
      addUnwrappedLine();
2386
21
    }
2387
434
    if (FormatTok->Tok.is(tok::colon)) {
2388
14
      return;
2389
14
    }
2390
420
    if (!FormatTok->Tok.isOneOf(tok::ampamp, tok::pipepipe)) {
2391
168
      if (FormatTok->Previous &&
2392
147
          !FormatTok->Previous->isOneOf(tok::identifier, tok::kw_requires,
2393
140
                                        tok::coloncolon)) {
2394
140
        addUnwrappedLine();
2395
140
      }
2396
168
      if (Style.IndentRequires && 
OriginalLevel != Line->Level63
) {
2397
28
        --Line->Level;
2398
28
      }
2399
168
      break;
2400
252
    } else {
2401
252
      FormatTok->setType(TT_ConstraintJunctions);
2402
252
    }
2403
2404
252
    nextToken();
2405
252
  }
2406
224
}
2407
2408
161
void UnwrappedLineParser::parseRequires() {
2409
161
  assert(FormatTok->Tok.is(tok::kw_requires) && "'requires' expected");
2410
2411
161
  unsigned OriginalLevel = Line->Level;
2412
161
  if (FormatTok->Previous && FormatTok->Previous->is(tok::greater)) {
2413
84
    addUnwrappedLine();
2414
84
    if (Style.IndentRequires) {
2415
42
      Line->Level++;
2416
42
    }
2417
84
  }
2418
161
  nextToken();
2419
2420
161
  parseRequiresExpression(OriginalLevel);
2421
161
}
2422
2423
1.94k
bool UnwrappedLineParser::parseEnum() {
2424
  // Won't be 'enum' for NS_ENUMs.
2425
1.94k
  if (FormatTok->Tok.is(tok::kw_enum))
2426
1.89k
    nextToken();
2427
2428
  // In TypeScript, "enum" can also be used as property name, e.g. in interface
2429
  // declarations. An "enum" keyword followed by a colon would be a syntax
2430
  // error and thus assume it is just an identifier.
2431
1.94k
  if (Style.Language == FormatStyle::LK_JavaScript &&
2432
36
      FormatTok->isOneOf(tok::colon, tok::question))
2433
8
    return false;
2434
2435
  // In protobuf, "enum" can be used as a field name.
2436
1.94k
  if (Style.Language == FormatStyle::LK_Proto && 
FormatTok->is(tok::equal)8
)
2437
2
    return false;
2438
2439
  // Eat up enum class ...
2440
1.93k
  if (FormatTok->Tok.is(tok::kw_class) || 
FormatTok->Tok.is(tok::kw_struct)1.63k
)
2441
351
    nextToken();
2442
2443
3.84k
  while (FormatTok->Tok.getIdentifierInfo() ||
2444
2.00k
         FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
2445
1.93k
                            tok::greater, tok::comma, tok::question)) {
2446
1.93k
    nextToken();
2447
    // We can have macros or attributes in between 'enum' and the enum name.
2448
1.93k
    if (FormatTok->is(tok::l_paren))
2449
100
      parseParens();
2450
1.93k
    if (FormatTok->is(tok::identifier)) {
2451
162
      nextToken();
2452
      // If there are two identifiers in a row, this is likely an elaborate
2453
      // return type. In Java, this can be "implements", etc.
2454
162
      if (Style.isCpp() && 
FormatTok->is(tok::identifier)158
)
2455
32
        return false;
2456
162
    }
2457
1.93k
  }
2458
2459
  // Just a declaration or something is wrong.
2460
1.90k
  if (FormatTok->isNot(tok::l_brace))
2461
38
    return true;
2462
1.86k
  FormatTok->setBlockKind(BK_Block);
2463
2464
1.86k
  if (Style.Language == FormatStyle::LK_Java) {
2465
    // Java enums are different.
2466
20
    parseJavaEnumBody();
2467
20
    return true;
2468
20
  }
2469
1.84k
  if (Style.Language == FormatStyle::LK_Proto) {
2470
6
    parseBlock(/*MustBeDeclaration=*/true);
2471
6
    return true;
2472
6
  }
2473
2474
1.84k
  if (!Style.AllowShortEnumsOnASingleLine)
2475
9
    addUnwrappedLine();
2476
  // Parse enum body.
2477
1.84k
  nextToken();
2478
1.84k
  if (!Style.AllowShortEnumsOnASingleLine) {
2479
9
    addUnwrappedLine();
2480
9
    Line->Level += 1;
2481
9
  }
2482
1.84k
  bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true,
2483
1.84k
                                   /*IsEnum=*/true);
2484
1.84k
  if (!Style.AllowShortEnumsOnASingleLine)
2485
9
    Line->Level -= 1;
2486
1.84k
  if (HasError) {
2487
14
    if (FormatTok->is(tok::semi))
2488
7
      nextToken();
2489
14
    addUnwrappedLine();
2490
14
  }
2491
1.84k
  return true;
2492
2493
  // There is no addUnwrappedLine() here so that we fall through to parsing a
2494
  // structural element afterwards. Thus, in "enum A {} n, m;",
2495
  // "} n, m;" will end up in one unwrapped line.
2496
1.84k
}
2497
2498
namespace {
2499
// A class used to set and restore the Token position when peeking
2500
// ahead in the token source.
2501
class ScopedTokenPosition {
2502
  unsigned StoredPosition;
2503
  FormatTokenSource *Tokens;
2504
2505
public:
2506
65
  ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) {
2507
65
    assert(Tokens && "Tokens expected to not be null");
2508
65
    StoredPosition = Tokens->getPosition();
2509
65
  }
2510
2511
65
  ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); }
2512
};
2513
} // namespace
2514
2515
// Look to see if we have [[ by looking ahead, if
2516
// its not then rewind to the original position.
2517
65
bool UnwrappedLineParser::tryToParseSimpleAttribute() {
2518
65
  ScopedTokenPosition AutoPosition(Tokens);
2519
65
  FormatToken *Tok = Tokens->getNextToken();
2520
  // We already read the first [ check for the second.
2521
65
  if (Tok && !Tok->is(tok::l_square)) {
2522
6
    return false;
2523
6
  }
2524
  // Double check that the attribute is just something
2525
  // fairly simple.
2526
201
  
while (59
Tok) {
2527
201
    if (Tok->is(tok::r_square)) {
2528
59
      break;
2529
59
    }
2530
142
    Tok = Tokens->getNextToken();
2531
142
  }
2532
59
  Tok = Tokens->getNextToken();
2533
59
  if (Tok && !Tok->is(tok::r_square)) {
2534
6
    return false;
2535
6
  }
2536
53
  Tok = Tokens->getNextToken();
2537
53
  if (Tok && Tok->is(tok::semi)) {
2538
0
    return false;
2539
0
  }
2540
53
  return true;
2541
53
}
2542
2543
20
void UnwrappedLineParser::parseJavaEnumBody() {
2544
  // Determine whether the enum is simple, i.e. does not have a semicolon or
2545
  // constants with class bodies. Simple enums can be formatted like braced
2546
  // lists, contracted to a single line, etc.
2547
20
  unsigned StoredPosition = Tokens->getPosition();
2548
20
  bool IsSimple = true;
2549
20
  FormatToken *Tok = Tokens->getNextToken();
2550
128
  while (Tok) {
2551
128
    if (Tok->is(tok::r_brace))
2552
8
      break;
2553
120
    if (Tok->isOneOf(tok::l_brace, tok::semi)) {
2554
12
      IsSimple = false;
2555
12
      break;
2556
12
    }
2557
    // FIXME: This will also mark enums with braces in the arguments to enum
2558
    // constants as "not simple". This is probably fine in practice, though.
2559
108
    Tok = Tokens->getNextToken();
2560
108
  }
2561
20
  FormatTok = Tokens->setPosition(StoredPosition);
2562
2563
20
  if (IsSimple) {
2564
8
    nextToken();
2565
8
    parseBracedList();
2566
8
    addUnwrappedLine();
2567
8
    return;
2568
8
  }
2569
2570
  // Parse the body of a more complex enum.
2571
  // First add a line for everything up to the "{".
2572
12
  nextToken();
2573
12
  addUnwrappedLine();
2574
12
  ++Line->Level;
2575
2576
  // Parse the enum constants.
2577
68
  while (FormatTok) {
2578
68
    if (FormatTok->is(tok::l_brace)) {
2579
      // Parse the constant's class body.
2580
8
      parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2581
8
                 /*MunchSemi=*/false);
2582
60
    } else if (FormatTok->is(tok::l_paren)) {
2583
12
      parseParens();
2584
48
    } else if (FormatTok->is(tok::comma)) {
2585
12
      nextToken();
2586
12
      addUnwrappedLine();
2587
36
    } else if (FormatTok->is(tok::semi)) {
2588
12
      nextToken();
2589
12
      addUnwrappedLine();
2590
12
      break;
2591
24
    } else if (FormatTok->is(tok::r_brace)) {
2592
0
      addUnwrappedLine();
2593
0
      break;
2594
24
    } else {
2595
24
      nextToken();
2596
24
    }
2597
68
  }
2598
2599
  // Parse the class body after the enum's ";" if any.
2600
12
  parseLevel(/*HasOpeningBrace=*/true);
2601
12
  nextToken();
2602
12
  --Line->Level;
2603
12
  addUnwrappedLine();
2604
12
}
2605
2606
7.03k
void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
2607
7.03k
  const FormatToken &InitialToken = *FormatTok;
2608
7.03k
  nextToken();
2609
2610
  // The actual identifier can be a nested name specifier, and in macros
2611
  // it is often token-pasted.
2612
  // An [[attribute]] can be before the identifier.
2613
14.5k
  while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2614
14.5k
                            tok::kw___attribute, tok::kw___declspec,
2615
14.5k
                            tok::kw_alignas, tok::l_square, tok::r_square) ||
2616
7.04k
         ((Style.Language == FormatStyle::LK_Java ||
2617
7.00k
           Style.Language == FormatStyle::LK_JavaScript) &&
2618
7.54k
          
FormatTok->isOneOf(tok::period, tok::comma)329
)) {
2619
7.54k
    if (Style.Language == FormatStyle::LK_JavaScript &&
2620
324
        FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
2621
      // JavaScript/TypeScript supports inline object types in
2622
      // extends/implements positions:
2623
      //     class Foo implements {bar: number} { }
2624
36
      nextToken();
2625
36
      if (FormatTok->is(tok::l_brace)) {
2626
12
        tryToParseBracedList();
2627
12
        continue;
2628
12
      }
2629
7.53k
    }
2630
7.53k
    bool IsNonMacroIdentifier =
2631
7.53k
        FormatTok->is(tok::identifier) &&
2632
7.26k
        FormatTok->TokenText != FormatTok->TokenText.upper();
2633
7.53k
    nextToken();
2634
    // We can have macros or attributes in between 'class' and the class name.
2635
7.53k
    if (!IsNonMacroIdentifier) {
2636
3.74k
      if (FormatTok->Tok.is(tok::l_paren)) {
2637
56
        parseParens();
2638
3.69k
      } else if (FormatTok->is(TT_AttributeSquare)) {
2639
0
        parseSquare();
2640
        // Consume the closing TT_AttributeSquare.
2641
0
        if (FormatTok->Next && FormatTok->is(TT_AttributeSquare))
2642
0
          nextToken();
2643
0
      }
2644
3.74k
    }
2645
7.53k
  }
2646
2647
  // Note that parsing away template declarations here leads to incorrectly
2648
  // accepting function declarations as record declarations.
2649
  // In general, we cannot solve this problem. Consider:
2650
  // class A<int> B() {}
2651
  // which can be a function definition or a class definition when B() is a
2652
  // macro. If we find enough real-world cases where this is a problem, we
2653
  // can parse for the 'template' keyword in the beginning of the statement,
2654
  // and thus rule out the record production in case there is no template
2655
  // (this would still leave us with an ambiguity between template function
2656
  // and class declarations).
2657
7.03k
  if (FormatTok->isOneOf(tok::colon, tok::less)) {
2658
5.15k
    while (!eof()) {
2659
5.15k
      if (FormatTok->is(tok::l_brace)) {
2660
787
        calculateBraceTypes(/*ExpectClassBody=*/true);
2661
787
        if (!tryToParseBracedList())
2662
775
          break;
2663
4.37k
      }
2664
4.37k
      if (FormatTok->Tok.is(tok::semi))
2665
14
        return;
2666
4.36k
      if (Style.isCSharp() && 
FormatTok->is(Keywords.kw_where)56
) {
2667
8
        addUnwrappedLine();
2668
8
        nextToken();
2669
8
        parseCSharpGenericTypeConstraint();
2670
8
        break;
2671
8
      }
2672
4.35k
      nextToken();
2673
4.35k
    }
2674
804
  }
2675
7.01k
  if (FormatTok->Tok.is(tok::l_brace)) {
2676
5.96k
    if (ParseAsExpr) {
2677
4
      parseChildBlock();
2678
5.96k
    } else {
2679
5.96k
      if (ShouldBreakBeforeBrace(Style, InitialToken))
2680
353
        addUnwrappedLine();
2681
2682
5.96k
      parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2683
5.96k
                 /*MunchSemi=*/false);
2684
5.96k
    }
2685
5.96k
  }
2686
  // There is no addUnwrappedLine() here so that we fall through to parsing a
2687
  // structural element afterwards. Thus, in "class A {} n, m;",
2688
  // "} n, m;" will end up in one unwrapped line.
2689
7.01k
}
2690
2691
122
void UnwrappedLineParser::parseObjCMethod() {
2692
122
  assert(FormatTok->Tok.isOneOf(tok::l_paren, tok::identifier) &&
2693
122
         "'(' or identifier expected.");
2694
758
  do {
2695
758
    if (FormatTok->Tok.is(tok::semi)) {
2696
78
      nextToken();
2697
78
      addUnwrappedLine();
2698
78
      return;
2699
680
    } else if (FormatTok->Tok.is(tok::l_brace)) {
2700
44
      if (Style.BraceWrapping.AfterFunction)
2701
2
        addUnwrappedLine();
2702
44
      parseBlock(/*MustBeDeclaration=*/false);
2703
44
      addUnwrappedLine();
2704
44
      return;
2705
636
    } else {
2706
636
      nextToken();
2707
636
    }
2708
636
  } while (!eof());
2709
122
}
2710
2711
30
void UnwrappedLineParser::parseObjCProtocolList() {
2712
30
  assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
2713
128
  do {
2714
128
    nextToken();
2715
    // Early exit in case someone forgot a close angle.
2716
128
    if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2717
128
        FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2718
0
      return;
2719
128
  } while (!eof() && FormatTok->Tok.isNot(tok::greater));
2720
30
  nextToken(); // Skip '>'.
2721
30
}
2722
2723
202
void UnwrappedLineParser::parseObjCUntilAtEnd() {
2724
521
  do {
2725
521
    if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
2726
179
      nextToken();
2727
179
      addUnwrappedLine();
2728
179
      break;
2729
179
    }
2730
342
    if (FormatTok->is(tok::l_brace)) {
2731
2
      parseBlock(/*MustBeDeclaration=*/false);
2732
      // In ObjC interfaces, nothing should be following the "}".
2733
2
      addUnwrappedLine();
2734
340
    } else if (FormatTok->is(tok::r_brace)) {
2735
      // Ignore stray "}". parseStructuralElement doesn't consume them.
2736
2
      nextToken();
2737
2
      addUnwrappedLine();
2738
338
    } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
2739
122
      nextToken();
2740
122
      parseObjCMethod();
2741
216
    } else {
2742
216
      parseStructuralElement();
2743
216
    }
2744
342
  } while (!eof());
2745
202
}
2746
2747
183
void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
2748
183
  assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
2749
183
         FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
2750
183
  nextToken();
2751
183
  nextToken(); // interface name
2752
2753
  // @interface can be followed by a lightweight generic
2754
  // specialization list, then either a base class or a category.
2755
183
  if (FormatTok->Tok.is(tok::less)) {
2756
8
    parseObjCLightweightGenerics();
2757
8
  }
2758
183
  if (FormatTok->Tok.is(tok::colon)) {
2759
47
    nextToken();
2760
47
    nextToken(); // base class name
2761
    // The base class can also have lightweight generics applied to it.
2762
47
    if (FormatTok->Tok.is(tok::less)) {
2763
26
      parseObjCLightweightGenerics();
2764
26
    }
2765
136
  } else if (FormatTok->Tok.is(tok::l_paren))
2766
    // Skip category, if present.
2767
59
    parseParens();
2768
2769
183
  if (FormatTok->Tok.is(tok::less))
2770
26
    parseObjCProtocolList();
2771
2772
183
  if (FormatTok->Tok.is(tok::l_brace)) {
2773
71
    if (Style.BraceWrapping.AfterObjCDeclaration)
2774
21
      addUnwrappedLine();
2775
71
    parseBlock(/*MustBeDeclaration=*/true);
2776
71
  }
2777
2778
  // With instance variables, this puts '}' on its own line.  Without instance
2779
  // variables, this ends the @interface line.
2780
183
  addUnwrappedLine();
2781
2782
183
  parseObjCUntilAtEnd();
2783
183
}
2784
2785
34
void UnwrappedLineParser::parseObjCLightweightGenerics() {
2786
34
  assert(FormatTok->Tok.is(tok::less));
2787
  // Unlike protocol lists, generic parameterizations support
2788
  // nested angles:
2789
  //
2790
  // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
2791
  //     NSObject <NSCopying, NSSecureCoding>
2792
  //
2793
  // so we need to count how many open angles we have left.
2794
34
  unsigned NumOpenAngles = 1;
2795
130
  do {
2796
130
    nextToken();
2797
    // Early exit in case someone forgot a close angle.
2798
130
    if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2799
130
        FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2800
0
      break;
2801
130
    if (FormatTok->Tok.is(tok::less))
2802
4
      ++NumOpenAngles;
2803
126
    else if (FormatTok->Tok.is(tok::greater)) {
2804
38
      assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
2805
38
      --NumOpenAngles;
2806
38
    }
2807
130
  } while (!eof() && NumOpenAngles != 0);
2808
34
  nextToken(); // Skip '>'.
2809
34
}
2810
2811
// Returns true for the declaration/definition form of @protocol,
2812
// false for the expression form.
2813
25
bool UnwrappedLineParser::parseObjCProtocol() {
2814
25
  assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
2815
25
  nextToken();
2816
2817
25
  if (FormatTok->is(tok::l_paren))
2818
    // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
2819
2
    return false;
2820
2821
  // The definition/declaration form,
2822
  // @protocol Foo
2823
  // - (int)someMethod;
2824
  // @end
2825
2826
23
  nextToken(); // protocol name
2827
2828
23
  if (FormatTok->Tok.is(tok::less))
2829
4
    parseObjCProtocolList();
2830
2831
  // Check for protocol declaration.
2832
23
  if (FormatTok->Tok.is(tok::semi)) {
2833
4
    nextToken();
2834
4
    addUnwrappedLine();
2835
4
    return true;
2836
4
  }
2837
2838
19
  addUnwrappedLine();
2839
19
  parseObjCUntilAtEnd();
2840
19
  return true;
2841
19
}
2842
2843
403
void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
2844
403
  bool IsImport = FormatTok->is(Keywords.kw_import);
2845
403
  assert(IsImport || FormatTok->is(tok::kw_export));
2846
403
  nextToken();
2847
2848
  // Consume the "default" in "export default class/function".
2849
403
  if (FormatTok->is(tok::kw_default))
2850
36
    nextToken();
2851
2852
  // Consume "async function", "function" and "default function", so that these
2853
  // get parsed as free-standing JS functions, i.e. do not require a trailing
2854
  // semicolon.
2855
403
  if (FormatTok->is(Keywords.kw_async))
2856
4
    nextToken();
2857
403
  if (FormatTok->is(Keywords.kw_function)) {
2858
40
    nextToken();
2859
40
    return;
2860
40
  }
2861
2862
  // For imports, `export *`, `export {...}`, consume the rest of the line up
2863
  // to the terminating `;`. For everything else, just return and continue
2864
  // parsing the structural element, i.e. the declaration or expression for
2865
  // `export default`.
2866
363
  if (!IsImport && 
!FormatTok->isOneOf(tok::l_brace, tok::star)142
&&
2867
103
      !FormatTok->isStringLiteral())
2868
103
    return;
2869
2870
1.05k
  
while (260
!eof()) {
2871
1.05k
    if (FormatTok->is(tok::semi))
2872
252
      return;
2873
802
    if (Line->Tokens.empty()) {
2874
      // Common issue: Automatic Semicolon Insertion wrapped the line, so the
2875
      // import statement should terminate.
2876
4
      return;
2877
4
    }
2878
798
    if (FormatTok->is(tok::l_brace)) {
2879
238
      FormatTok->setBlockKind(BK_Block);
2880
238
      nextToken();
2881
238
      parseBracedList();
2882
560
    } else {
2883
560
      nextToken();
2884
560
    }
2885
798
  }
2886
260
}
2887
2888
77
void UnwrappedLineParser::parseStatementMacro() {
2889
77
  nextToken();
2890
77
  if (FormatTok->is(tok::l_paren))
2891
70
    parseParens();
2892
77
  if (FormatTok->is(tok::semi))
2893
7
    nextToken();
2894
77
  addUnwrappedLine();
2895
77
}
2896
2897
LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
2898
0
                                                 StringRef Prefix = "") {
2899
0
  llvm::dbgs() << Prefix << "Line(" << Line.Level
2900
0
               << ", FSC=" << Line.FirstStartColumn << ")"
2901
0
               << (Line.InPPDirective ? " MACRO" : "") << ": ";
2902
0
  for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2903
0
                                                    E = Line.Tokens.end();
2904
0
       I != E; ++I) {
2905
0
    llvm::dbgs() << I->Tok->Tok.getName() << "["
2906
0
                 << "T=" << (unsigned)I->Tok->getType()
2907
0
                 << ", OC=" << I->Tok->OriginalColumn << "] ";
2908
0
  }
2909
0
  for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2910
0
                                                    E = Line.Tokens.end();
2911
0
       I != E; ++I) {
2912
0
    const UnwrappedLineNode &Node = *I;
2913
0
    for (SmallVectorImpl<UnwrappedLine>::const_iterator
2914
0
             I = Node.Children.begin(),
2915
0
             E = Node.Children.end();
2916
0
         I != E; ++I) {
2917
0
      printDebugInfo(*I, "\nChild: ");
2918
0
    }
2919
0
  }
2920
0
  llvm::dbgs() << "\n";
2921
0
}
2922
2923
268k
void UnwrappedLineParser::addUnwrappedLine() {
2924
268k
  if (Line->Tokens.empty())
2925
118k
    return;
2926
149k
  LLVM_DEBUG({
2927
149k
    if (CurrentLines == &Lines)
2928
149k
      printDebugInfo(*Line);
2929
149k
  });
2930
149k
  CurrentLines->push_back(std::move(*Line));
2931
149k
  Line->Tokens.clear();
2932
149k
  Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
2933
149k
  Line->FirstStartColumn = 0;
2934
149k
  if (CurrentLines == &Lines && 
!PreprocessorDirectives.empty()144k
) {
2935
1.04k
    CurrentLines->append(
2936
1.04k
        std::make_move_iterator(PreprocessorDirectives.begin()),
2937
1.04k
        std::make_move_iterator(PreprocessorDirectives.end()));
2938
1.04k
    PreprocessorDirectives.clear();
2939
1.04k
  }
2940
  // Disconnect the current token from the last token on the previous line.
2941
149k
  FormatTok->Previous = nullptr;
2942
149k
}
2943
2944
1.08M
bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
2945
2946
620k
bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
2947
620k
  return (Line->InPPDirective || 
FormatTok.HasUnescapedNewline581k
) &&
2948
101k
         FormatTok.NewlinesBefore > 0;
2949
620k
}
2950
2951
// Checks if \p FormatTok is a line comment that continues the line comment
2952
// section on \p Line.
2953
static bool
2954
continuesLineCommentSection(const FormatToken &FormatTok,
2955
                            const UnwrappedLine &Line,
2956
14.2k
                            const llvm::Regex &CommentPragmasRegex) {
2957
14.2k
  if (Line.Tokens.empty())
2958
4.51k
    return false;
2959
2960
9.74k
  StringRef IndentContent = FormatTok.TokenText;
2961
9.74k
  if (FormatTok.TokenText.startswith("//") ||
2962
1.27k
      FormatTok.TokenText.startswith("/*"))
2963
9.67k
    IndentContent = FormatTok.TokenText.substr(2);
2964
9.74k
  if (CommentPragmasRegex.match(IndentContent))
2965
7
    return false;
2966
2967
  // If Line starts with a line comment, then FormatTok continues the comment
2968
  // section if its original column is greater or equal to the original start
2969
  // column of the line.
2970
  //
2971
  // Define the min column token of a line as follows: if a line ends in '{' or
2972
  // contains a '{' followed by a line comment, then the min column token is
2973
  // that '{'. Otherwise, the min column token of the line is the first token of
2974
  // the line.
2975
  //
2976
  // If Line starts with a token other than a line comment, then FormatTok
2977
  // continues the comment section if its original column is greater than the
2978
  // original start column of the min column token of the line.
2979
  //
2980
  // For example, the second line comment continues the first in these cases:
2981
  //
2982
  // // first line
2983
  // // second line
2984
  //
2985
  // and:
2986
  //
2987
  // // first line
2988
  //  // second line
2989
  //
2990
  // and:
2991
  //
2992
  // int i; // first line
2993
  //  // second line
2994
  //
2995
  // and:
2996
  //
2997
  // do { // first line
2998
  //      // second line
2999
  //   int i;
3000
  // } while (true);
3001
  //
3002
  // and:
3003
  //
3004
  // enum {
3005
  //   a, // first line
3006
  //    // second line
3007
  //   b
3008
  // };
3009
  //
3010
  // The second line comment doesn't continue the first in these cases:
3011
  //
3012
  //   // first line
3013
  //  // second line
3014
  //
3015
  // and:
3016
  //
3017
  // int i; // first line
3018
  // // second line
3019
  //
3020
  // and:
3021
  //
3022
  // do { // first line
3023
  //   // second line
3024
  //   int i;
3025
  // } while (true);
3026
  //
3027
  // and:
3028
  //
3029
  // enum {
3030
  //   a, // first line
3031
  //   // second line
3032
  // };
3033
9.73k
  const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
3034
3035
  // Scan for '{//'. If found, use the column of '{' as a min column for line
3036
  // comment section continuation.
3037
9.73k
  const FormatToken *PreviousToken = nullptr;
3038
43.6k
  for (const UnwrappedLineNode &Node : Line.Tokens) {
3039
43.6k
    if (PreviousToken && 
PreviousToken->is(tok::l_brace)33.9k
&&
3040
909
        isLineComment(*Node.Tok)) {
3041
201
      MinColumnToken = PreviousToken;
3042
201
      break;
3043
201
    }
3044
43.4k
    PreviousToken = Node.Tok;
3045
3046
    // Grab the last newline preceding a token in this unwrapped line.
3047
43.4k
    if (Node.Tok->NewlinesBefore > 0) {
3048
9.89k
      MinColumnToken = Node.Tok;
3049
9.89k
    }
3050
43.4k
  }
3051
9.73k
  if (PreviousToken && PreviousToken->is(tok::l_brace)) {
3052
1.87k
    MinColumnToken = PreviousToken;
3053
1.87k
  }
3054
3055
9.73k
  return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
3056
9.73k
                              MinColumnToken);
3057
9.73k
}
3058
3059
646k
void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
3060
646k
  bool JustComments = Line->Tokens.empty();
3061
646k
  for (SmallVectorImpl<FormatToken *>::const_iterator
3062
646k
           I = CommentsBeforeNextToken.begin(),
3063
646k
           E = CommentsBeforeNextToken.end();
3064
650k
       I != E; 
++I4.51k
) {
3065
    // Line comments that belong to the same line comment section are put on the
3066
    // same line since later we might want to reflow content between them.
3067
    // Additional fine-grained breaking of line comment sections is controlled
3068
    // by the class BreakableLineCommentSection in case it is desirable to keep
3069
    // several line comment sections in the same unwrapped line.
3070
    //
3071
    // FIXME: Consider putting separate line comment sections as children to the
3072
    // unwrapped line instead.
3073
4.51k
    (*I)->ContinuesLineCommentSection =
3074
4.51k
        continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
3075
4.51k
    if (isOnNewLine(**I) && 
JustComments3.46k
&&
!(*I)->ContinuesLineCommentSection3.02k
)
3076
1.61k
      addUnwrappedLine();
3077
4.51k
    pushToken(*I);
3078
4.51k
  }
3079
646k
  if (NewlineBeforeNext && 
JustComments100k
)
3080
82.6k
    addUnwrappedLine();
3081
646k
  CommentsBeforeNextToken.clear();
3082
646k
}
3083
3084
598k
void UnwrappedLineParser::nextToken(int LevelDifference) {
3085
598k
  if (eof())
3086
3.05k
    return;
3087
595k
  flushComments(isOnNewLine(*FormatTok));
3088
595k
  pushToken(FormatTok);
3089
595k
  FormatToken *Previous = FormatTok;
3090
595k
  if (Style.Language != FormatStyle::LK_JavaScript)
3091
564k
    readToken(LevelDifference);
3092
30.9k
  else
3093
30.9k
    readTokenWithJavaScriptASI();
3094
595k
  FormatTok->Previous = Previous;
3095
595k
}
3096
3097
void UnwrappedLineParser::distributeComments(
3098
    const SmallVectorImpl<FormatToken *> &Comments,
3099
641k
    const FormatToken *NextTok) {
3100
  // Whether or not a line comment token continues a line is controlled by
3101
  // the method continuesLineCommentSection, with the following caveat:
3102
  //
3103
  // Define a trail of Comments to be a nonempty proper postfix of Comments such
3104
  // that each comment line from the trail is aligned with the next token, if
3105
  // the next token exists. If a trail exists, the beginning of the maximal
3106
  // trail is marked as a start of a new comment section.
3107
  //
3108
  // For example in this code:
3109
  //
3110
  // int a; // line about a
3111
  //   // line 1 about b
3112
  //   // line 2 about b
3113
  //   int b;
3114
  //
3115
  // the two lines about b form a maximal trail, so there are two sections, the
3116
  // first one consisting of the single comment "// line about a" and the
3117
  // second one consisting of the next two comments.
3118
641k
  if (Comments.empty())
3119
633k
    return;
3120
8.27k
  bool ShouldPushCommentsInCurrentLine = true;
3121
8.27k
  bool HasTrailAlignedWithNextToken = false;
3122
8.27k
  unsigned StartOfTrailAlignedWithNextToken = 0;
3123
8.27k
  if (NextTok) {
3124
    // We are skipping the first element intentionally.
3125
10.1k
    for (unsigned i = Comments.size() - 1; i > 0; 
--i1.84k
) {
3126
1.84k
      if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
3127
1.30k
        HasTrailAlignedWithNextToken = true;
3128
1.30k
        StartOfTrailAlignedWithNextToken = i;
3129
1.30k
      }
3130
1.84k
    }
3131
8.27k
  }
3132
18.4k
  for (unsigned i = 0, e = Comments.size(); i < e; 
++i10.1k
) {
3133
10.1k
    FormatToken *FormatTok = Comments[i];
3134
10.1k
    if (HasTrailAlignedWithNextToken && 
i == StartOfTrailAlignedWithNextToken1.72k
) {
3135
381
      FormatTok->ContinuesLineCommentSection = false;
3136
9.74k
    } else {
3137
9.74k
      FormatTok->ContinuesLineCommentSection =
3138
9.74k
          continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
3139
9.74k
    }
3140
10.1k
    if (!FormatTok->ContinuesLineCommentSection &&
3141
9.94k
        (isOnNewLine(*FormatTok) || 
FormatTok->IsFirst6.49k
)) {
3142
4.49k
      ShouldPushCommentsInCurrentLine = false;
3143
4.49k
    }
3144
10.1k
    if (ShouldPushCommentsInCurrentLine) {
3145
5.60k
      pushToken(FormatTok);
3146
4.51k
    } else {
3147
4.51k
      CommentsBeforeNextToken.push_back(FormatTok);
3148
4.51k
    }
3149
10.1k
  }
3150
8.27k
}
3151
3152
632k
void UnwrappedLineParser::readToken(int LevelDifference) {
3153
632k
  SmallVector<FormatToken *, 1> Comments;
3154
645k
  do {
3155
645k
    FormatTok = Tokens->getNextToken();
3156
645k
    assert(FormatTok);
3157
654k
    while (!Line->InPPDirective && 
FormatTok->Tok.is(tok::hash)614k
&&
3158
8.57k
           (FormatTok->HasUnescapedNewline || 
FormatTok->IsFirst4.18k
)) {
3159
8.55k
      distributeComments(Comments, FormatTok);
3160
8.55k
      Comments.clear();
3161
      // If there is an unfinished unwrapped line, we flush the preprocessor
3162
      // directives only after that unwrapped line was finished later.
3163
8.55k
      bool SwitchToPreprocessorLines = !Line->Tokens.empty();
3164
8.55k
      ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
3165
8.55k
      assert((LevelDifference >= 0 ||
3166
8.55k
              static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
3167
8.55k
             "LevelDifference makes Line->Level negative");
3168
8.55k
      Line->Level += LevelDifference;
3169
      // Comments stored before the preprocessor directive need to be output
3170
      // before the preprocessor directive, at the same level as the
3171
      // preprocessor directive, as we consider them to apply to the directive.
3172
8.55k
      if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&
3173
235
          PPBranchLevel > 0)
3174
80
        Line->Level += PPBranchLevel;
3175
8.55k
      flushComments(isOnNewLine(*FormatTok));
3176
8.55k
      parsePPDirective();
3177
8.55k
    }
3178
645k
    while (FormatTok->getType() == TT_ConflictStart ||
3179
645k
           FormatTok->getType() == TT_ConflictEnd ||
3180
645k
           FormatTok->getType() == TT_ConflictAlternative) {
3181
108
      if (FormatTok->getType() == TT_ConflictStart) {
3182
27
        conditionalCompilationStart(/*Unreachable=*/false);
3183
81
      } else if (FormatTok->getType() == TT_ConflictAlternative) {
3184
54
        conditionalCompilationAlternative();
3185
27
      } else if (FormatTok->getType() == TT_ConflictEnd) {
3186
27
        conditionalCompilationEnd();
3187
27
      }
3188
108
      FormatTok = Tokens->getNextToken();
3189
108
      FormatTok->MustBreakBefore = true;
3190
108
    }
3191
3192
645k
    if (!PPStack.empty() && 
(PPStack.back().Kind == PP_Unreachable)12.5k
&&
3193
5.18k
        !Line->InPPDirective) {
3194
2.54k
      continue;
3195
2.54k
    }
3196
3197
642k
    if (!FormatTok->Tok.is(tok::comment)) {
3198
632k
      distributeComments(Comments, FormatTok);
3199
632k
      Comments.clear();
3200
632k
      return;
3201
632k
    }
3202
3203
10.1k
    Comments.push_back(FormatTok);
3204
12.6k
  } while (!eof());
3205
3206
0
  distributeComments(Comments, nullptr);
3207
0
  Comments.clear();
3208
0
}
3209
3210
642k
void UnwrappedLineParser::pushToken(FormatToken *Tok) {
3211
642k
  Line->Tokens.push_back(UnwrappedLineNode(Tok));
3212
642k
  if (MustBreakBeforeNextToken) {
3213
1.76k
    Line->Tokens.back().Tok->MustBreakBefore = true;
3214
1.76k
    MustBreakBeforeNextToken = false;
3215
1.76k
  }
3216
642k
}
3217
3218
} // end namespace format
3219
} // end namespace clang