Coverage Report

Created: 2021-08-24 07:12

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