Coverage Report

Created: 2020-09-22 08:39

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