Coverage Report

Created: 2022-01-25 06:29

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