Coverage Report

Created: 2022-05-21 09:15

/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
#include <utility>
24
25
#define DEBUG_TYPE "format-parser"
26
27
namespace clang {
28
namespace format {
29
30
class FormatTokenSource {
31
public:
32
78.1k
  virtual ~FormatTokenSource() {}
33
34
  // Returns the next token in the token stream.
35
  virtual FormatToken *getNextToken() = 0;
36
37
  // Returns the token preceding the token returned by the last call to
38
  // getNextToken() in the token stream, or nullptr if no such token exists.
39
  virtual FormatToken *getPreviousToken() = 0;
40
41
  // Returns the token that would be returned by the next call to
42
  // getNextToken().
43
  virtual FormatToken *peekNextToken() = 0;
44
45
  // Returns the token that would be returned after the next N calls to
46
  // getNextToken(). N needs to be greater than zero, and small enough that
47
  // there are still tokens. Check for tok::eof with N-1 before calling it with
48
  // N.
49
  virtual FormatToken *peekNextToken(int N) = 0;
50
51
  // Returns whether we are at the end of the file.
52
  // This can be different from whether getNextToken() returned an eof token
53
  // when the FormatTokenSource is a view on a part of the token stream.
54
  virtual bool isEOF() = 0;
55
56
  // Gets the current position in the token stream, to be used by setPosition().
57
  virtual unsigned getPosition() = 0;
58
59
  // Resets the token stream to the state it was in when getPosition() returned
60
  // Position, and return the token at that position in the stream.
61
  virtual FormatToken *setPosition(unsigned Position) = 0;
62
};
63
64
namespace {
65
66
class ScopedDeclarationState {
67
public:
68
  ScopedDeclarationState(UnwrappedLine &Line, llvm::BitVector &Stack,
69
                         bool MustBeDeclaration)
70
109k
      : Line(Line), Stack(Stack) {
71
109k
    Line.MustBeDeclaration = MustBeDeclaration;
72
109k
    Stack.push_back(MustBeDeclaration);
73
109k
  }
74
109k
  ~ScopedDeclarationState() {
75
109k
    Stack.pop_back();
76
109k
    if (!Stack.empty())
77
41.4k
      Line.MustBeDeclaration = Stack.back();
78
67.9k
    else
79
67.9k
      Line.MustBeDeclaration = true;
80
109k
  }
81
82
private:
83
  UnwrappedLine &Line;
84
  llvm::BitVector &Stack;
85
};
86
87
62.3k
static bool isLineComment(const FormatToken &FormatTok) {
88
62.3k
  return FormatTok.is(tok::comment) && 
!FormatTok.TokenText.startswith("/*")22.7k
;
89
62.3k
}
90
91
// Checks if \p FormatTok is a line comment that continues the line comment
92
// \p Previous. The original column of \p MinColumnToken is used to determine
93
// whether \p FormatTok is indented enough to the right to continue \p Previous.
94
static bool continuesLineComment(const FormatToken &FormatTok,
95
                                 const FormatToken *Previous,
96
26.8k
                                 const FormatToken *MinColumnToken) {
97
26.8k
  if (!Previous || 
!MinColumnToken26.8k
)
98
21
    return false;
99
26.8k
  unsigned MinContinueColumn =
100
26.8k
      MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 
03.24k
:
123.5k
);
101
26.8k
  return isLineComment(FormatTok) && 
FormatTok.NewlinesBefore == 113.5k
&&
102
26.8k
         
isLineComment(*Previous)6.57k
&&
103
26.8k
         
FormatTok.OriginalColumn >= MinContinueColumn3.00k
;
104
26.8k
}
105
106
class ScopedMacroState : public FormatTokenSource {
107
public:
108
  ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
109
                   FormatToken *&ResetToken)
110
      : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
111
        PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
112
13.7k
        Token(nullptr), PreviousToken(nullptr) {
113
13.7k
    FakeEOF.Tok.startToken();
114
13.7k
    FakeEOF.Tok.setKind(tok::eof);
115
13.7k
    TokenSource = this;
116
13.7k
    Line.Level = 0;
117
13.7k
    Line.InPPDirective = true;
118
13.7k
  }
119
120
13.7k
  ~ScopedMacroState() override {
121
13.7k
    TokenSource = PreviousTokenSource;
122
13.7k
    ResetToken = Token;
123
13.7k
    Line.InPPDirective = false;
124
13.7k
    Line.Level = PreviousLineLevel;
125
13.7k
  }
126
127
60.4k
  FormatToken *getNextToken() override {
128
    // The \c UnwrappedLineParser guards against this by never calling
129
    // \c getNextToken() after it has encountered the first eof token.
130
60.4k
    assert(!eof());
131
0
    PreviousToken = Token;
132
60.4k
    Token = PreviousTokenSource->getNextToken();
133
60.4k
    if (eof())
134
11.0k
      return &FakeEOF;
135
49.4k
    return Token;
136
60.4k
  }
137
138
0
  FormatToken *getPreviousToken() override {
139
0
    return PreviousTokenSource->getPreviousToken();
140
0
  }
141
142
235
  FormatToken *peekNextToken() override {
143
235
    if (eof())
144
0
      return &FakeEOF;
145
235
    return PreviousTokenSource->peekNextToken();
146
235
  }
147
148
0
  FormatToken *peekNextToken(int N) override {
149
0
    assert(N > 0);
150
0
    if (eof())
151
0
      return &FakeEOF;
152
0
    return PreviousTokenSource->peekNextToken(N);
153
0
  }
154
155
51
  bool isEOF() override { return PreviousTokenSource->isEOF(); }
156
157
192
  unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
158
159
192
  FormatToken *setPosition(unsigned Position) override {
160
192
    PreviousToken = nullptr;
161
192
    Token = PreviousTokenSource->setPosition(Position);
162
192
    return Token;
163
192
  }
164
165
private:
166
121k
  bool eof() {
167
121k
    return Token && 
Token->HasUnescapedNewline107k
&&
168
121k
           !continuesLineComment(*Token, PreviousToken,
169
11.0k
                                 /*MinColumnToken=*/PreviousToken);
170
121k
  }
171
172
  FormatToken FakeEOF;
173
  UnwrappedLine &Line;
174
  FormatTokenSource *&TokenSource;
175
  FormatToken *&ResetToken;
176
  unsigned PreviousLineLevel;
177
  FormatTokenSource *PreviousTokenSource;
178
179
  FormatToken *Token;
180
  FormatToken *PreviousToken;
181
};
182
183
} // end anonymous namespace
184
185
class ScopedLineState {
186
public:
187
  ScopedLineState(UnwrappedLineParser &Parser,
188
                  bool SwitchToPreprocessorLines = false)
189
18.1k
      : Parser(Parser), OriginalLines(Parser.CurrentLines) {
190
18.1k
    if (SwitchToPreprocessorLines)
191
4.82k
      Parser.CurrentLines = &Parser.PreprocessorDirectives;
192
13.3k
    else if (!Parser.Line->Tokens.empty())
193
4.70k
      Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
194
18.1k
    PreBlockLine = std::move(Parser.Line);
195
18.1k
    Parser.Line = std::make_unique<UnwrappedLine>();
196
18.1k
    Parser.Line->Level = PreBlockLine->Level;
197
18.1k
    Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
198
18.1k
  }
199
200
18.1k
  ~ScopedLineState() {
201
18.1k
    if (!Parser.Line->Tokens.empty())
202
3
      Parser.addUnwrappedLine();
203
18.1k
    assert(Parser.Line->Tokens.empty());
204
0
    Parser.Line = std::move(PreBlockLine);
205
18.1k
    if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
206
4.82k
      Parser.MustBreakBeforeNextToken = true;
207
18.1k
    Parser.CurrentLines = OriginalLines;
208
18.1k
  }
209
210
private:
211
  UnwrappedLineParser &Parser;
212
213
  std::unique_ptr<UnwrappedLine> PreBlockLine;
214
  SmallVectorImpl<UnwrappedLine> *OriginalLines;
215
};
216
217
class CompoundStatementIndenter {
218
public:
219
  CompoundStatementIndenter(UnwrappedLineParser *Parser,
220
                            const FormatStyle &Style, unsigned &LineLevel)
221
      : CompoundStatementIndenter(Parser, LineLevel,
222
                                  Style.BraceWrapping.AfterControlStatement,
223
7.75k
                                  Style.BraceWrapping.IndentBraces) {}
224
  CompoundStatementIndenter(UnwrappedLineParser *Parser, unsigned &LineLevel,
225
                            bool WrapBrace, bool IndentBrace)
226
8.19k
      : LineLevel(LineLevel), OldLineLevel(LineLevel) {
227
8.19k
    if (WrapBrace)
228
1.14k
      Parser->addUnwrappedLine();
229
8.19k
    if (IndentBrace)
230
108
      ++LineLevel;
231
8.19k
  }
232
8.19k
  ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
233
234
private:
235
  unsigned &LineLevel;
236
  unsigned OldLineLevel;
237
};
238
239
namespace {
240
241
class IndexedTokenSource : public FormatTokenSource {
242
public:
243
  IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
244
64.3k
      : Tokens(Tokens), Position(-1) {}
245
246
1.26M
  FormatToken *getNextToken() override {
247
1.26M
    if (Position >= 0 && 
Tokens[Position]->is(tok::eof)1.19M
) {
248
0
      LLVM_DEBUG({
249
0
        llvm::dbgs() << "Next ";
250
0
        dbgToken(Position);
251
0
      });
252
0
      return Tokens[Position];
253
0
    }
254
1.26M
    ++Position;
255
1.26M
    LLVM_DEBUG({
256
1.26M
      llvm::dbgs() << "Next ";
257
1.26M
      dbgToken(Position);
258
1.26M
    });
259
1.26M
    return Tokens[Position];
260
1.26M
  }
261
262
3.06k
  FormatToken *getPreviousToken() override {
263
3.06k
    return Position > 0 ? 
Tokens[Position - 1]2.61k
:
nullptr456
;
264
3.06k
  }
265
266
31.2k
  FormatToken *peekNextToken() override {
267
31.2k
    int Next = Position + 1;
268
31.2k
    LLVM_DEBUG({
269
31.2k
      llvm::dbgs() << "Peeking ";
270
31.2k
      dbgToken(Next);
271
31.2k
    });
272
31.2k
    return Tokens[Next];
273
31.2k
  }
274
275
26
  FormatToken *peekNextToken(int N) override {
276
26
    assert(N > 0);
277
0
    int Next = Position + N;
278
26
    LLVM_DEBUG({
279
26
      llvm::dbgs() << "Peeking (+" << (N - 1) << ") ";
280
26
      dbgToken(Next);
281
26
    });
282
26
    return Tokens[Next];
283
26
  }
284
285
51
  bool isEOF() override { return Tokens[Position]->is(tok::eof); }
286
287
21.1k
  unsigned getPosition() override {
288
21.1k
    LLVM_DEBUG(llvm::dbgs() << "Getting Position: " << Position << "\n");
289
21.1k
    assert(Position >= 0);
290
0
    return Position;
291
21.1k
  }
292
293
21.1k
  FormatToken *setPosition(unsigned P) override {
294
21.1k
    LLVM_DEBUG(llvm::dbgs() << "Setting Position: " << P << "\n");
295
21.1k
    Position = P;
296
21.1k
    return Tokens[Position];
297
21.1k
  }
298
299
65.0k
  void reset() { Position = -1; }
300
301
private:
302
0
  void dbgToken(int Position, llvm::StringRef Indent = "") {
303
0
    FormatToken *Tok = Tokens[Position];
304
0
    llvm::dbgs() << Indent << "[" << Position
305
0
                 << "] Token: " << Tok->Tok.getName() << " / " << Tok->TokenText
306
0
                 << ", Macro: " << !!Tok->MacroCtx << "\n";
307
0
  }
308
309
  ArrayRef<FormatToken *> Tokens;
310
  int Position;
311
};
312
313
} // end anonymous namespace
314
315
UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
316
                                         const AdditionalKeywords &Keywords,
317
                                         unsigned FirstStartColumn,
318
                                         ArrayRef<FormatToken *> Tokens,
319
                                         UnwrappedLineConsumer &Callback)
320
    : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
321
      CurrentLines(&Lines), Style(Style), Keywords(Keywords),
322
      CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
323
      Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
324
      IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
325
                       ? IG_Rejected
326
                       : IG_Inited),
327
64.3k
      IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
328
329
65.0k
void UnwrappedLineParser::reset() {
330
65.0k
  PPBranchLevel = -1;
331
65.0k
  IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None
332
65.0k
                     ? 
IG_Rejected64.8k
333
65.0k
                     : 
IG_Inited273
;
334
65.0k
  IncludeGuardToken = nullptr;
335
65.0k
  Line.reset(new UnwrappedLine);
336
65.0k
  CommentsBeforeNextToken.clear();
337
65.0k
  FormatTok = nullptr;
338
65.0k
  MustBreakBeforeNextToken = false;
339
65.0k
  PreprocessorDirectives.clear();
340
65.0k
  CurrentLines = &Lines;
341
65.0k
  DeclarationScopeStack.clear();
342
65.0k
  NestedTooDeep.clear();
343
65.0k
  PPStack.clear();
344
65.0k
  Line->FirstStartColumn = FirstStartColumn;
345
65.0k
}
346
347
64.3k
void UnwrappedLineParser::parse() {
348
64.3k
  IndexedTokenSource TokenSource(AllTokens);
349
64.3k
  Line->FirstStartColumn = FirstStartColumn;
350
65.0k
  do {
351
65.0k
    LLVM_DEBUG(llvm::dbgs() << "----\n");
352
65.0k
    reset();
353
65.0k
    Tokens = &TokenSource;
354
65.0k
    TokenSource.reset();
355
356
65.0k
    readToken();
357
65.0k
    parseFile();
358
359
    // If we found an include guard then all preprocessor directives (other than
360
    // the guard) are over-indented by one.
361
65.0k
    if (IncludeGuard == IG_Found)
362
42
      for (auto &Line : Lines)
363
219
        if (Line.InPPDirective && 
Line.Level > 0162
)
364
78
          --Line.Level;
365
366
    // Create line with eof token.
367
65.0k
    pushToken(FormatTok);
368
65.0k
    addUnwrappedLine();
369
370
65.0k
    for (const UnwrappedLine &Line : Lines)
371
243k
      Callback.consumeUnwrappedLine(Line);
372
373
65.0k
    Callback.finishRun();
374
65.0k
    Lines.clear();
375
66.4k
    while (!PPLevelBranchIndex.empty() &&
376
66.4k
           
PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()2.11k
) {
377
1.39k
      PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
378
1.39k
      PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
379
1.39k
    }
380
65.0k
    if (!PPLevelBranchIndex.empty()) {
381
719
      ++PPLevelBranchIndex.back();
382
719
      assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
383
0
      assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
384
719
    }
385
65.0k
  } while (!PPLevelBranchIndex.empty());
386
64.3k
}
387
388
68.3k
void UnwrappedLineParser::parseFile() {
389
  // The top-level context in a file always has declarations, except for pre-
390
  // processor directives and JavaScript files.
391
68.3k
  bool MustBeDeclaration = !Line->InPPDirective && 
!Style.isJavaScript()65.0k
;
392
68.3k
  ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
393
68.3k
                                          MustBeDeclaration);
394
68.3k
  if (Style.Language == FormatStyle::LK_TextProto)
395
607
    parseBracedList();
396
67.7k
  else
397
67.7k
    parseLevel(/*OpeningBrace=*/nullptr, /*CanContainBracedList=*/true);
398
  // Make sure to format the remaining tokens.
399
  //
400
  // LK_TextProto is special since its top-level is parsed as the body of a
401
  // braced list, which does not necessarily have natural line separators such
402
  // as a semicolon. Comments after the last entry that have been determined to
403
  // not belong to that line, as in:
404
  //   key: value
405
  //   // endfile comment
406
  // do not have a chance to be put on a line of their own until this point.
407
  // Here we add this newline before end-of-file comments.
408
68.3k
  if (Style.Language == FormatStyle::LK_TextProto &&
409
68.3k
      
!CommentsBeforeNextToken.empty()607
)
410
15
    addUnwrappedLine();
411
68.3k
  flushComments(true);
412
68.3k
  addUnwrappedLine();
413
68.3k
}
414
415
14
void UnwrappedLineParser::parseCSharpGenericTypeConstraint() {
416
140
  do {
417
140
    switch (FormatTok->Tok.getKind()) {
418
14
    case tok::l_brace:
419
14
      return;
420
126
    default:
421
126
      if (FormatTok->is(Keywords.kw_where)) {
422
4
        addUnwrappedLine();
423
4
        nextToken();
424
4
        parseCSharpGenericTypeConstraint();
425
4
        break;
426
4
      }
427
122
      nextToken();
428
122
      break;
429
140
    }
430
140
  } while (
!eof()126
);
431
14
}
432
433
76
void UnwrappedLineParser::parseCSharpAttribute() {
434
76
  int UnpairedSquareBrackets = 1;
435
322
  do {
436
322
    switch (FormatTok->Tok.getKind()) {
437
78
    case tok::r_square:
438
78
      nextToken();
439
78
      --UnpairedSquareBrackets;
440
78
      if (UnpairedSquareBrackets == 0) {
441
76
        addUnwrappedLine();
442
76
        return;
443
76
      }
444
2
      break;
445
2
    case tok::l_square:
446
2
      ++UnpairedSquareBrackets;
447
2
      nextToken();
448
2
      break;
449
242
    default:
450
242
      nextToken();
451
242
      break;
452
322
    }
453
322
  } while (
!eof()246
);
454
76
}
455
456
1.64k
bool UnwrappedLineParser::precededByCommentOrPPDirective() const {
457
1.64k
  if (!Lines.empty() && 
Lines.back().InPPDirective1.12k
)
458
12
    return true;
459
460
1.63k
  const FormatToken *Previous = Tokens->getPreviousToken();
461
1.63k
  return Previous && 
Previous->is(tok::comment)1.18k
&&
462
1.63k
         
(90
Previous->IsMultiline90
||
Previous->NewlinesBefore > 090
);
463
1.64k
}
464
465
/// \brief Parses a level, that is ???.
466
/// \param OpeningBrace Opening brace (\p nullptr if absent) of that level
467
/// \param CanContainBracedList If the content can contain (at any level) a
468
/// braced list.
469
/// \param NextLBracesType The type for left brace found in this level.
470
/// \returns true if a simple block of if/else/for/while, or false otherwise.
471
/// (A simple block has a single statement.)
472
bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
473
                                     bool CanContainBracedList,
474
                                     IfStmtKind *IfKind,
475
108k
                                     TokenType NextLBracesType) {
476
108k
  auto NextLevelLBracesType = NextLBracesType == TT_CompoundRequirementLBrace
477
108k
                                  ? 
TT_BracedListLBrace945
478
108k
                                  : 
TT_Unknown107k
;
479
108k
  const bool IsPrecededByCommentOrPPDirective =
480
108k
      !Style.RemoveBracesLLVM || 
precededByCommentOrPPDirective()1.15k
;
481
108k
  bool HasLabel = false;
482
108k
  unsigned StatementCount = 0;
483
108k
  bool SwitchLabelEncountered = false;
484
164k
  do {
485
164k
    if (FormatTok->getType() == TT_AttributeMacro) {
486
36
      nextToken();
487
36
      continue;
488
36
    }
489
164k
    tok::TokenKind kind = FormatTok->Tok.getKind();
490
164k
    if (FormatTok->getType() == TT_MacroBlockBegin)
491
36
      kind = tok::l_brace;
492
164k
    else if (FormatTok->getType() == TT_MacroBlockEnd)
493
36
      kind = tok::r_brace;
494
495
164k
    auto ParseDefault = [this, OpeningBrace, IfKind, NextLevelLBracesType,
496
164k
                         &HasLabel, &StatementCount] {
497
121k
      parseStructuralElement(IfKind, !OpeningBrace, NextLevelLBracesType,
498
121k
                             HasLabel ? 
nullptr0
: &HasLabel);
499
121k
      ++StatementCount;
500
121k
      assert(StatementCount > 0 && "StatementCount overflow!");
501
121k
    };
502
503
164k
    switch (kind) {
504
0
    case tok::comment:
505
0
      nextToken();
506
0
      addUnwrappedLine();
507
0
      break;
508
1.39k
    case tok::l_brace:
509
1.39k
      if (NextLBracesType != TT_Unknown)
510
344
        FormatTok->setFinalizedType(NextLBracesType);
511
1.05k
      else if (FormatTok->Previous &&
512
1.05k
               
FormatTok->Previous->ClosesRequiresClause252
) {
513
        // We need the 'default' case here to correctly parse a function
514
        // l_brace.
515
204
        ParseDefault();
516
204
        continue;
517
204
      }
518
1.19k
      if (CanContainBracedList && 
!FormatTok->is(TT_MacroBlockBegin)851
&&
519
1.19k
          
tryToParseBracedList()815
)
520
9
        continue;
521
1.18k
      parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
522
1.18k
                 /*MunchSemi=*/true, /*KeepBraces=*/true,
523
1.18k
                 /*UnindentWhitesmithsBraces=*/false, CanContainBracedList,
524
1.18k
                 NextLBracesType);
525
1.18k
      ++StatementCount;
526
1.18k
      assert(StatementCount > 0 && "StatementCount overflow!");
527
0
      addUnwrappedLine();
528
1.18k
      break;
529
40.9k
    case tok::r_brace:
530
40.9k
      if (OpeningBrace) {
531
40.8k
        if (!Style.RemoveBracesLLVM ||
532
40.8k
            
!OpeningBrace->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace)686
)
533
40.2k
          return false;
534
626
        if (FormatTok->isNot(tok::r_brace) || StatementCount != 1 || 
HasLabel578
||
535
626
            
IsPrecededByCommentOrPPDirective554
||
536
626
            
precededByCommentOrPPDirective()494
)
537
144
          return false;
538
482
        const FormatToken *Next = Tokens->peekNextToken();
539
482
        return Next->isNot(tok::comment) || 
Next->NewlinesBefore > 028
;
540
626
      }
541
91
      nextToken();
542
91
      addUnwrappedLine();
543
91
      break;
544
189
    case tok::kw_default: {
545
189
      unsigned StoredPosition = Tokens->getPosition();
546
189
      FormatToken *Next;
547
192
      do {
548
192
        Next = Tokens->getNextToken();
549
192
        assert(Next);
550
192
      } while (Next->is(tok::comment));
551
0
      FormatTok = Tokens->setPosition(StoredPosition);
552
189
      if (Next->isNot(tok::colon)) {
553
        // default not followed by ':' is not a case label; treat it like
554
        // an identifier.
555
2
        parseStructuralElement();
556
2
        break;
557
2
      }
558
      // Else, if it is 'default:', fall through to the case handling.
559
189
      
LLVM_FALLTHROUGH187
;
560
187
    }
561
1.00k
    case tok::kw_case:
562
1.00k
      if (Style.isJavaScript() && 
Line->MustBeDeclaration12
) {
563
        // A 'case: string' style field declaration.
564
8
        parseStructuralElement();
565
8
        break;
566
8
      }
567
1.00k
      if (!SwitchLabelEncountered &&
568
1.00k
          
(688
Style.IndentCaseLabels688
||
(579
Line->InPPDirective579
&&
Line->Level == 127
)))
569
127
        ++Line->Level;
570
1.00k
      SwitchLabelEncountered = true;
571
1.00k
      parseStructuralElement();
572
1.00k
      break;
573
1.74k
    case tok::l_square:
574
1.74k
      if (Style.isCSharp()) {
575
76
        nextToken();
576
76
        parseCSharpAttribute();
577
76
        break;
578
76
      }
579
1.67k
      if (handleCppAttributes())
580
133
        break;
581
1.67k
      
LLVM_FALLTHROUGH1.53k
;1.53k
582
120k
    default:
583
120k
      ParseDefault();
584
120k
      break;
585
164k
    }
586
164k
  } while (
!eof()123k
);
587
67.9k
  return false;
588
108k
}
589
590
18.9k
void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
591
  // We'll parse forward through the tokens until we hit
592
  // a closing brace or eof - note that getNextToken() will
593
  // parse macros, so this will magically work inside macro
594
  // definitions, too.
595
18.9k
  unsigned StoredPosition = Tokens->getPosition();
596
18.9k
  FormatToken *Tok = FormatTok;
597
18.9k
  const FormatToken *PrevTok = Tok->Previous;
598
  // Keep a stack of positions of lbrace tokens. We will
599
  // update information about whether an lbrace starts a
600
  // braced init list or a different block during the loop.
601
18.9k
  SmallVector<FormatToken *, 8> LBraceStack;
602
18.9k
  assert(Tok->is(tok::l_brace));
603
150k
  do {
604
    // Get next non-comment token.
605
150k
    FormatToken *NextTok;
606
153k
    do {
607
153k
      NextTok = Tokens->getNextToken();
608
153k
    } while (NextTok->is(tok::comment));
609
610
150k
    switch (Tok->Tok.getKind()) {
611
22.5k
    case tok::l_brace:
612
22.5k
      if (Style.isJavaScript() && 
PrevTok802
) {
613
792
        if (PrevTok->isOneOf(tok::colon, tok::less))
614
          // A ':' indicates this code is in a type, or a braced list
615
          // following a label in an object literal ({a: {b: 1}}).
616
          // A '<' could be an object used in a comparison, but that is nonsense
617
          // code (can never return true), so more likely it is a generic type
618
          // argument (`X<{a: string; b: number}>`).
619
          // The code below could be confused by semicolons between the
620
          // individual members in a type member list, which would normally
621
          // trigger BK_Block. In both cases, this must be parsed as an inline
622
          // braced init.
623
94
          Tok->setBlockKind(BK_BracedInit);
624
698
        else if (PrevTok->is(tok::r_paren))
625
          // `) { }` can only occur in function or method declarations in JS.
626
374
          Tok->setBlockKind(BK_Block);
627
21.7k
      } else {
628
21.7k
        Tok->setBlockKind(BK_Unknown);
629
21.7k
      }
630
22.5k
      LBraceStack.push_back(Tok);
631
22.5k
      break;
632
22.2k
    case tok::r_brace:
633
22.2k
      if (LBraceStack.empty())
634
0
        break;
635
22.2k
      if (LBraceStack.back()->is(BK_Unknown)) {
636
11.3k
        bool ProbablyBracedList = false;
637
11.3k
        if (Style.Language == FormatStyle::LK_Proto) {
638
36
          ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
639
11.3k
        } else {
640
          // Skip NextTok over preprocessor lines, otherwise we may not
641
          // properly diagnose the block as a braced intializer
642
          // if the comma separator appears after the pp directive.
643
11.6k
          while (NextTok->is(tok::hash)) {
644
310
            ScopedMacroState MacroState(*Line, Tokens, NextTok);
645
694
            do {
646
694
              NextTok = Tokens->getNextToken();
647
694
            } while (NextTok->isNot(tok::eof));
648
310
          }
649
650
          // Using OriginalColumn to distinguish between ObjC methods and
651
          // binary operators is a bit hacky.
652
11.3k
          bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
653
11.3k
                                  
NextTok->OriginalColumn == 018
;
654
655
          // Try to detect a braced list. Note that regardless how we mark inner
656
          // braces here, we will overwrite the BlockKind later if we parse a
657
          // braced list (where all blocks inside are by default braced lists),
658
          // or when we explicitly detect blocks (for example while parsing
659
          // lambdas).
660
661
          // If we already marked the opening brace as braced list, the closing
662
          // must also be part of it.
663
11.3k
          ProbablyBracedList = LBraceStack.back()->is(TT_BracedListLBrace);
664
665
11.3k
          ProbablyBracedList = ProbablyBracedList ||
666
11.3k
                               
(11.3k
Style.isJavaScript()11.3k
&&
667
11.3k
                                NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
668
258
                                                 Keywords.kw_as));
669
11.3k
          ProbablyBracedList = ProbablyBracedList ||
670
11.3k
                               
(11.3k
Style.isCpp()11.3k
&&
NextTok->is(tok::l_paren)10.8k
);
671
672
          // If there is a comma, semicolon or right paren after the closing
673
          // brace, we assume this is a braced initializer list.
674
          // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
675
          // braced list in JS.
676
11.3k
          ProbablyBracedList =
677
11.3k
              ProbablyBracedList ||
678
11.3k
              NextTok->isOneOf(tok::comma, tok::period, tok::colon,
679
11.3k
                               tok::r_paren, tok::r_square, tok::l_brace,
680
11.3k
                               tok::ellipsis);
681
682
11.3k
          ProbablyBracedList =
683
11.3k
              ProbablyBracedList ||
684
11.3k
              
(9.41k
NextTok->is(tok::identifier)9.41k
&&
685
9.41k
               
!PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)242
);
686
687
11.3k
          ProbablyBracedList = ProbablyBracedList ||
688
11.3k
                               
(9.40k
NextTok->is(tok::semi)9.40k
&&
689
9.40k
                                
(2.42k
!ExpectClassBody2.42k
||
LBraceStack.size() != 1735
));
690
691
11.3k
          ProbablyBracedList =
692
11.3k
              ProbablyBracedList ||
693
11.3k
              
(7.68k
NextTok->isBinaryOperator()7.68k
&&
!NextIsObjCMethod97
);
694
695
11.3k
          if (!Style.isCSharp() && 
NextTok->is(tok::l_square)11.2k
) {
696
            // We can have an array subscript after a braced init
697
            // list, but C++11 attributes are expected after blocks.
698
18
            NextTok = Tokens->getNextToken();
699
18
            ProbablyBracedList = NextTok->isNot(tok::l_square);
700
18
          }
701
11.3k
        }
702
11.3k
        if (ProbablyBracedList) {
703
3.81k
          Tok->setBlockKind(BK_BracedInit);
704
3.81k
          LBraceStack.back()->setBlockKind(BK_BracedInit);
705
7.58k
        } else {
706
7.58k
          Tok->setBlockKind(BK_Block);
707
7.58k
          LBraceStack.back()->setBlockKind(BK_Block);
708
7.58k
        }
709
11.3k
      }
710
22.2k
      LBraceStack.pop_back();
711
22.2k
      break;
712
30.4k
    case tok::identifier:
713
30.4k
      if (!Tok->is(TT_StatementMacro))
714
30.3k
        break;
715
30.4k
      
LLVM_FALLTHROUGH18
;18
716
205
    case tok::at:
717
13.6k
    case tok::semi:
718
14.5k
    case tok::kw_if:
719
14.6k
    case tok::kw_while:
720
14.9k
    case tok::kw_for:
721
15.0k
    case tok::kw_switch:
722
15.0k
    case tok::kw_try:
723
15.0k
    case tok::kw___try:
724
15.0k
      if (!LBraceStack.empty() && LBraceStack.back()->is(BK_Unknown))
725
10.4k
        LBraceStack.back()->setBlockKind(BK_Block);
726
15.0k
      break;
727
60.7k
    default:
728
60.7k
      break;
729
150k
    }
730
150k
    PrevTok = Tok;
731
150k
    Tok = NextTok;
732
150k
  } while (Tok->isNot(tok::eof) && 
!LBraceStack.empty()141k
);
733
734
  // Assume other blocks for all unclosed opening braces.
735
18.9k
  for (FormatToken *LBrace : LBraceStack)
736
247
    if (LBrace->is(BK_Unknown))
737
202
      LBrace->setBlockKind(BK_Block);
738
739
18.9k
  FormatTok = Tokens->setPosition(StoredPosition);
740
18.9k
}
741
742
template <class T>
743
1.39k
static inline void hash_combine(std::size_t &seed, const T &v) {
744
1.39k
  std::hash<T> hasher;
745
1.39k
  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
746
1.39k
}
747
748
72.5k
size_t UnwrappedLineParser::computePPHash() const {
749
72.5k
  size_t h = 0;
750
72.5k
  for (const auto &i : PPStack) {
751
698
    hash_combine(h, size_t(i.Kind));
752
698
    hash_combine(h, i.Line);
753
698
  }
754
72.5k
  return h;
755
72.5k
}
756
757
// Checks whether \p ParsedLine might fit on a single line. We must clone the
758
// tokens of \p ParsedLine before running the token annotator on it so that we
759
// can restore them afterward.
760
278
bool UnwrappedLineParser::mightFitOnOneLine(UnwrappedLine &ParsedLine) const {
761
278
  const auto ColumnLimit = Style.ColumnLimit;
762
278
  if (ColumnLimit == 0)
763
2
    return true;
764
765
276
  auto &Tokens = ParsedLine.Tokens;
766
276
  assert(!Tokens.empty());
767
0
  const auto *LastToken = Tokens.back().Tok;
768
276
  assert(LastToken);
769
770
0
  SmallVector<UnwrappedLineNode> SavedTokens(Tokens.size());
771
772
276
  int Index = 0;
773
1.19k
  for (const auto &Token : Tokens) {
774
1.19k
    assert(Token.Tok);
775
0
    auto &SavedToken = SavedTokens[Index++];
776
1.19k
    SavedToken.Tok = new FormatToken;
777
1.19k
    SavedToken.Tok->copyFrom(*Token.Tok);
778
1.19k
    SavedToken.Children = std::move(Token.Children);
779
1.19k
  }
780
781
276
  AnnotatedLine Line(ParsedLine);
782
276
  assert(Line.Last == LastToken);
783
784
0
  TokenAnnotator Annotator(Style, Keywords);
785
276
  Annotator.annotate(Line);
786
276
  Annotator.calculateFormattingInformation(Line);
787
788
276
  const int Length = LastToken->TotalLength;
789
790
276
  Index = 0;
791
1.19k
  for (auto &Token : Tokens) {
792
1.19k
    const auto &SavedToken = SavedTokens[Index++];
793
1.19k
    Token.Tok->copyFrom(*SavedToken.Tok);
794
1.19k
    Token.Children = std::move(SavedToken.Children);
795
1.19k
    delete SavedToken.Tok;
796
1.19k
  }
797
798
276
  return Line.Level * Style.IndentWidth + Length <= ColumnLimit;
799
278
}
800
801
UnwrappedLineParser::IfStmtKind UnwrappedLineParser::parseBlock(
802
    bool MustBeDeclaration, unsigned AddLevels, bool MunchSemi, bool KeepBraces,
803
    bool UnindentWhitesmithsBraces, bool CanContainBracedList,
804
36.3k
    TokenType NextLBracesType) {
805
36.3k
  assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
806
36.3k
         "'{' or macro block token expected");
807
0
  FormatToken *Tok = FormatTok;
808
36.3k
  const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
809
36.3k
  FormatTok->setBlockKind(BK_Block);
810
811
  // For Whitesmiths mode, jump to the next level prior to skipping over the
812
  // braces.
813
36.3k
  if (AddLevels > 0 && 
Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths32.3k
)
814
666
    ++Line->Level;
815
816
36.3k
  size_t PPStartHash = computePPHash();
817
818
36.3k
  unsigned InitialLevel = Line->Level;
819
36.3k
  nextToken(/*LevelDifference=*/AddLevels);
820
821
36.3k
  if (MacroBlock && 
FormatTok->is(tok::l_paren)36
)
822
9
    parseParens();
823
824
36.3k
  size_t NbPreprocessorDirectives =
825
36.3k
      CurrentLines == &Lines ? 
PreprocessorDirectives.size()35.7k
:
0639
;
826
36.3k
  addUnwrappedLine();
827
36.3k
  size_t OpeningLineIndex =
828
36.3k
      CurrentLines->empty()
829
36.3k
          ? 
(UnwrappedLine::kInvalidIndex)0
830
36.3k
          : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
831
832
  // Whitesmiths is weird here. The brace needs to be indented for the namespace
833
  // block, but the block itself may not be indented depending on the style
834
  // settings. This allows the format to back up one level in those cases.
835
36.3k
  if (UnindentWhitesmithsBraces)
836
36
    --Line->Level;
837
838
36.3k
  ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
839
36.3k
                                          MustBeDeclaration);
840
36.3k
  if (AddLevels > 0u && 
Style.BreakBeforeBraces != FormatStyle::BS_Whitesmiths32.3k
)
841
31.6k
    Line->Level += AddLevels;
842
843
36.3k
  IfStmtKind IfKind = IfStmtKind::NotIf;
844
36.3k
  const bool SimpleBlock =
845
36.3k
      parseLevel(Tok, CanContainBracedList, &IfKind, NextLBracesType);
846
847
36.3k
  if (eof())
848
273
    return IfKind;
849
850
36.1k
  if (MacroBlock ? 
!FormatTok->is(TT_MacroBlockEnd)36
851
36.1k
                 : 
!FormatTok->is(tok::r_brace)36.0k
) {
852
0
    Line->Level = InitialLevel;
853
0
    FormatTok->setBlockKind(BK_Block);
854
0
    return IfKind;
855
0
  }
856
857
36.1k
  if (SimpleBlock && 
!KeepBraces470
&&
858
36.1k
      
Tok->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace)314
) {
859
314
    assert(FormatTok->is(tok::r_brace));
860
0
    const FormatToken *Previous = Tokens->getPreviousToken();
861
314
    assert(Previous);
862
314
    if (Previous->isNot(tok::r_brace) || 
Previous->Optional60
) {
863
278
      assert(!CurrentLines->empty());
864
278
      if (mightFitOnOneLine(CurrentLines->back())) {
865
230
        Tok->MatchingParen = FormatTok;
866
230
        FormatTok->MatchingParen = Tok;
867
230
      }
868
278
    }
869
314
  }
870
871
0
  size_t PPEndHash = computePPHash();
872
873
  // Munch the closing brace.
874
36.1k
  nextToken(/*LevelDifference=*/-AddLevels);
875
876
36.1k
  if (MacroBlock && 
FormatTok->is(tok::l_paren)36
)
877
9
    parseParens();
878
879
36.1k
  if (FormatTok->is(tok::kw_noexcept)) {
880
    // A noexcept in a requires expression.
881
54
    nextToken();
882
54
  }
883
884
36.1k
  if (FormatTok->is(tok::arrow)) {
885
    // Following the } or noexcept we can find a trailing return type arrow
886
    // as part of an implicit conversion constraint.
887
118
    nextToken();
888
118
    parseStructuralElement();
889
118
  }
890
891
36.1k
  if (MunchSemi && 
FormatTok->is(tok::semi)28.2k
)
892
676
    nextToken();
893
894
36.1k
  Line->Level = InitialLevel;
895
896
36.1k
  if (PPStartHash == PPEndHash) {
897
36.0k
    Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
898
36.0k
    if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
899
      // Update the opening line to add the forward reference as well
900
36.0k
      (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
901
36.0k
          CurrentLines->size() - 1;
902
36.0k
    }
903
36.0k
  }
904
905
36.1k
  return IfKind;
906
36.1k
}
907
908
396
static bool isGoogScope(const UnwrappedLine &Line) {
909
  // FIXME: Closure-library specific stuff should not be hard-coded but be
910
  // configurable.
911
396
  if (Line.Tokens.size() < 4)
912
0
    return false;
913
396
  auto I = Line.Tokens.begin();
914
396
  if (I->Tok->TokenText != "goog")
915
388
    return false;
916
8
  ++I;
917
8
  if (I->Tok->isNot(tok::period))
918
0
    return false;
919
8
  ++I;
920
8
  if (I->Tok->TokenText != "scope")
921
0
    return false;
922
8
  ++I;
923
8
  return I->Tok->is(tok::l_paren);
924
8
}
925
926
static bool isIIFE(const UnwrappedLine &Line,
927
388
                   const AdditionalKeywords &Keywords) {
928
  // Look for the start of an immediately invoked anonymous function.
929
  // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
930
  // This is commonly done in JavaScript to create a new, anonymous scope.
931
  // Example: (function() { ... })()
932
388
  if (Line.Tokens.size() < 3)
933
0
    return false;
934
388
  auto I = Line.Tokens.begin();
935
388
  if (I->Tok->isNot(tok::l_paren))
936
368
    return false;
937
20
  ++I;
938
20
  if (I->Tok->isNot(Keywords.kw_function))
939
0
    return false;
940
20
  ++I;
941
20
  return I->Tok->is(tok::l_paren);
942
20
}
943
944
static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
945
12.1k
                                   const FormatToken &InitialToken) {
946
12.1k
  tok::TokenKind Kind = InitialToken.Tok.getKind();
947
12.1k
  if (InitialToken.is(TT_NamespaceMacro))
948
160
    Kind = tok::kw_namespace;
949
950
12.1k
  switch (Kind) {
951
4.22k
  case tok::kw_namespace:
952
4.22k
    return Style.BraceWrapping.AfterNamespace;
953
3.84k
  case tok::kw_class:
954
3.84k
    return Style.BraceWrapping.AfterClass;
955
213
  case tok::kw_union:
956
213
    return Style.BraceWrapping.AfterUnion;
957
3.70k
  case tok::kw_struct:
958
3.70k
    return Style.BraceWrapping.AfterStruct;
959
107
  case tok::kw_enum:
960
107
    return Style.BraceWrapping.AfterEnum;
961
80
  default:
962
80
    return false;
963
12.1k
  }
964
12.1k
}
965
966
void UnwrappedLineParser::parseChildBlock(
967
4.70k
    bool CanContainBracedList, clang::format::TokenType NextLBracesType) {
968
4.70k
  assert(FormatTok->is(tok::l_brace));
969
0
  FormatTok->setBlockKind(BK_Block);
970
4.70k
  const FormatToken *OpeningBrace = FormatTok;
971
4.70k
  nextToken();
972
4.70k
  {
973
4.70k
    bool SkipIndent = (Style.isJavaScript() &&
974
4.70k
                       
(396
isGoogScope(*Line)396
||
isIIFE(*Line, Keywords)388
));
975
4.70k
    ScopedLineState LineState(*this);
976
4.70k
    ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
977
4.70k
                                            /*MustBeDeclaration=*/false);
978
4.70k
    Line->Level += SkipIndent ? 
024
:
14.68k
;
979
4.70k
    parseLevel(OpeningBrace, CanContainBracedList, /*IfKind=*/nullptr,
980
4.70k
               NextLBracesType);
981
4.70k
    flushComments(isOnNewLine(*FormatTok));
982
4.70k
    Line->Level -= SkipIndent ? 
024
:
14.68k
;
983
4.70k
  }
984
4.70k
  nextToken();
985
4.70k
}
986
987
13.4k
void UnwrappedLineParser::parsePPDirective() {
988
13.4k
  assert(FormatTok->is(tok::hash) && "'#' expected");
989
0
  ScopedMacroState MacroState(*Line, Tokens, FormatTok);
990
991
13.4k
  nextToken();
992
993
13.4k
  if (!FormatTok->Tok.getIdentifierInfo()) {
994
52
    parsePPUnknown();
995
52
    return;
996
52
  }
997
998
13.3k
  switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
999
3.23k
  case tok::pp_define:
1000
3.23k
    parsePPDefine();
1001
3.23k
    return;
1002
1.25k
  case tok::pp_if:
1003
1.25k
    parsePPIf(/*IfDef=*/false);
1004
1.25k
    break;
1005
930
  case tok::pp_ifdef:
1006
1.06k
  case tok::pp_ifndef:
1007
1.06k
    parsePPIf(/*IfDef=*/true);
1008
1.06k
    break;
1009
1.35k
  case tok::pp_else:
1010
1.35k
    parsePPElse();
1011
1.35k
    break;
1012
0
  case tok::pp_elifdef:
1013
96
  case tok::pp_elifndef:
1014
210
  case tok::pp_elif:
1015
210
    parsePPElIf();
1016
210
    break;
1017
2.25k
  case tok::pp_endif:
1018
2.25k
    parsePPEndIf();
1019
2.25k
    break;
1020
4.02k
  default:
1021
4.02k
    parsePPUnknown();
1022
4.02k
    break;
1023
13.3k
  }
1024
13.3k
}
1025
1026
3.98k
void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
1027
3.98k
  size_t Line = CurrentLines->size();
1028
3.98k
  if (CurrentLines == &PreprocessorDirectives)
1029
2.57k
    Line += Lines.size();
1030
1031
3.98k
  if (Unreachable ||
1032
3.98k
      
(2.07k
!PPStack.empty()2.07k
&&
PPStack.back().Kind == PP_Unreachable342
))
1033
2.07k
    PPStack.push_back({PP_Unreachable, Line});
1034
1.91k
  else
1035
1.91k
    PPStack.push_back({PP_Conditional, Line});
1036
3.98k
}
1037
1038
2.34k
void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
1039
2.34k
  ++PPBranchLevel;
1040
2.34k
  assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
1041
2.34k
  if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
1042
1.39k
    PPLevelBranchIndex.push_back(0);
1043
1.39k
    PPLevelBranchCount.push_back(0);
1044
1.39k
  }
1045
2.34k
  PPChainBranchIndex.push(0);
1046
2.34k
  bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
1047
2.34k
  conditionalCompilationCondition(Unreachable || 
Skip1.78k
);
1048
2.34k
}
1049
1050
1.63k
void UnwrappedLineParser::conditionalCompilationAlternative() {
1051
1.63k
  if (!PPStack.empty())
1052
1.61k
    PPStack.pop_back();
1053
1.63k
  assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
1054
1.63k
  if (!PPChainBranchIndex.empty())
1055
1.61k
    ++PPChainBranchIndex.top();
1056
1.63k
  conditionalCompilationCondition(
1057
1.63k
      PPBranchLevel >= 0 && 
!PPChainBranchIndex.empty()1.61k
&&
1058
1.63k
      
PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()1.61k
);
1059
1.63k
}
1060
1061
2.27k
void UnwrappedLineParser::conditionalCompilationEnd() {
1062
2.27k
  assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
1063
2.27k
  if (PPBranchLevel >= 0 && 
!PPChainBranchIndex.empty()2.26k
) {
1064
2.26k
    if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel])
1065
1.32k
      PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
1066
2.26k
  }
1067
  // Guard against #endif's without #if.
1068
2.27k
  if (PPBranchLevel > -1)
1069
2.26k
    --PPBranchLevel;
1070
2.27k
  if (!PPChainBranchIndex.empty())
1071
2.26k
    PPChainBranchIndex.pop();
1072
2.27k
  if (!PPStack.empty())
1073
2.26k
    PPStack.pop_back();
1074
2.27k
}
1075
1076
2.32k
void UnwrappedLineParser::parsePPIf(bool IfDef) {
1077
2.32k
  bool IfNDef = FormatTok->is(tok::pp_ifndef);
1078
2.32k
  nextToken();
1079
2.32k
  bool Unreachable = false;
1080
2.32k
  if (!IfDef && 
(1.25k
FormatTok->is(tok::kw_false)1.25k
||
FormatTok->TokenText == "0"1.25k
))
1081
556
    Unreachable = true;
1082
2.32k
  if (IfDef && 
!IfNDef1.06k
&&
FormatTok->TokenText == "SWIG"930
)
1083
3
    Unreachable = true;
1084
2.32k
  conditionalCompilationStart(Unreachable);
1085
2.32k
  FormatToken *IfCondition = FormatTok;
1086
  // If there's a #ifndef on the first line, and the only lines before it are
1087
  // comments, it could be an include guard.
1088
2.32k
  bool MaybeIncludeGuard = IfNDef;
1089
2.32k
  if (IncludeGuard == IG_Inited && 
MaybeIncludeGuard336
)
1090
117
    for (auto &Line : Lines) {
1091
21
      if (!Line.Tokens.front().Tok->is(tok::comment)) {
1092
12
        MaybeIncludeGuard = false;
1093
12
        IncludeGuard = IG_Rejected;
1094
12
        break;
1095
12
      }
1096
21
    }
1097
2.32k
  --PPBranchLevel;
1098
2.32k
  parsePPUnknown();
1099
2.32k
  ++PPBranchLevel;
1100
2.32k
  if (IncludeGuard == IG_Inited && 
MaybeIncludeGuard324
) {
1101
105
    IncludeGuard = IG_IfNdefed;
1102
105
    IncludeGuardToken = IfCondition;
1103
105
  }
1104
2.32k
}
1105
1106
1.56k
void UnwrappedLineParser::parsePPElse() {
1107
  // If a potential include guard has an #else, it's not an include guard.
1108
1.56k
  if (IncludeGuard == IG_Defined && 
PPBranchLevel == 036
)
1109
36
    IncludeGuard = IG_Rejected;
1110
1.56k
  conditionalCompilationAlternative();
1111
1.56k
  if (PPBranchLevel > -1)
1112
1.55k
    --PPBranchLevel;
1113
1.56k
  parsePPUnknown();
1114
1.56k
  ++PPBranchLevel;
1115
1.56k
}
1116
1117
210
void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
1118
1119
2.25k
void UnwrappedLineParser::parsePPEndIf() {
1120
2.25k
  conditionalCompilationEnd();
1121
2.25k
  parsePPUnknown();
1122
  // If the #endif of a potential include guard is the last thing in the file,
1123
  // then we found an include guard.
1124
2.25k
  if (IncludeGuard == IG_Defined && 
PPBranchLevel == -160
&&
Tokens->isEOF()51
&&
1125
2.25k
      
Style.IndentPPDirectives != FormatStyle::PPDIS_None42
)
1126
42
    IncludeGuard = IG_Found;
1127
2.25k
}
1128
1129
3.23k
void UnwrappedLineParser::parsePPDefine() {
1130
3.23k
  nextToken();
1131
1132
3.23k
  if (!FormatTok->Tok.getIdentifierInfo()) {
1133
9
    IncludeGuard = IG_Rejected;
1134
9
    IncludeGuardToken = nullptr;
1135
9
    parsePPUnknown();
1136
9
    return;
1137
9
  }
1138
1139
3.22k
  if (IncludeGuard == IG_IfNdefed &&
1140
3.22k
      
IncludeGuardToken->TokenText == FormatTok->TokenText96
) {
1141
87
    IncludeGuard = IG_Defined;
1142
87
    IncludeGuardToken = nullptr;
1143
93
    for (auto &Line : Lines) {
1144
93
      if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
1145
0
        IncludeGuard = IG_Rejected;
1146
0
        break;
1147
0
      }
1148
93
    }
1149
87
  }
1150
1151
  // In the context of a define, even keywords should be treated as normal
1152
  // identifiers. Setting the kind to identifier is not enough, because we need
1153
  // to treat additional keywords like __except as well, which are already
1154
  // identifiers. Setting the identifier info to null interferes with include
1155
  // guard processing above, and changes preprocessing nesting.
1156
3.22k
  FormatTok->Tok.setKind(tok::identifier);
1157
3.22k
  FormatTok->Tok.setIdentifierInfo(Keywords.kw_internal_ident_after_define);
1158
3.22k
  nextToken();
1159
3.22k
  if (FormatTok->Tok.getKind() == tok::l_paren &&
1160
3.22k
      
!FormatTok->hasWhitespaceBefore()1.58k
)
1161
1.29k
    parseParens();
1162
3.22k
  if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
1163
435
    Line->Level += PPBranchLevel + 1;
1164
3.22k
  addUnwrappedLine();
1165
3.22k
  ++Line->Level;
1166
1167
  // Errors during a preprocessor directive can only affect the layout of the
1168
  // preprocessor directive, and thus we ignore them. An alternative approach
1169
  // would be to use the same approach we use on the file level (no
1170
  // re-indentation if there was a structural error) within the macro
1171
  // definition.
1172
3.22k
  parseFile();
1173
3.22k
}
1174
1175
10.2k
void UnwrappedLineParser::parsePPUnknown() {
1176
18.2k
  do {
1177
18.2k
    nextToken();
1178
18.2k
  } while (!eof());
1179
10.2k
  if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
1180
900
    Line->Level += PPBranchLevel + 1;
1181
10.2k
  addUnwrappedLine();
1182
10.2k
}
1183
1184
// Here we exclude certain tokens that are not usually the first token in an
1185
// unwrapped line. This is used in attempt to distinguish macro calls without
1186
// trailing semicolons from other constructs split to several lines.
1187
530
static bool tokenCanStartNewLine(const FormatToken &Tok) {
1188
  // Semicolon can be a null-statement, l_square can be a start of a macro or
1189
  // a C++11 attribute, but this doesn't seem to be common.
1190
530
  return Tok.isNot(tok::semi) && 
Tok.isNot(tok::l_brace)527
&&
1191
530
         
Tok.isNot(TT_AttributeSquare)516
&&
1192
         // Tokens that can only be used as binary operators and a part of
1193
         // overloaded operator names.
1194
530
         
Tok.isNot(tok::period)516
&&
Tok.isNot(tok::periodstar)471
&&
1195
530
         
Tok.isNot(tok::arrow)471
&&
Tok.isNot(tok::arrowstar)465
&&
1196
530
         
Tok.isNot(tok::less)462
&&
Tok.isNot(tok::greater)462
&&
1197
530
         
Tok.isNot(tok::slash)459
&&
Tok.isNot(tok::percent)459
&&
1198
530
         
Tok.isNot(tok::lessless)459
&&
Tok.isNot(tok::greatergreater)429
&&
1199
530
         
Tok.isNot(tok::equal)429
&&
Tok.isNot(tok::plusequal)414
&&
1200
530
         
Tok.isNot(tok::minusequal)411
&&
Tok.isNot(tok::starequal)408
&&
1201
530
         
Tok.isNot(tok::slashequal)405
&&
Tok.isNot(tok::percentequal)402
&&
1202
530
         
Tok.isNot(tok::ampequal)399
&&
Tok.isNot(tok::pipeequal)396
&&
1203
530
         
Tok.isNot(tok::caretequal)393
&&
Tok.isNot(tok::greatergreaterequal)390
&&
1204
530
         
Tok.isNot(tok::lesslessequal)387
&&
1205
         // Colon is used in labels, base class lists, initializer lists,
1206
         // range-based for loops, ternary operator, but should never be the
1207
         // first token in an unwrapped line.
1208
530
         
Tok.isNot(tok::colon)384
&&
1209
         // 'noexcept' is a trailing annotation.
1210
530
         
Tok.isNot(tok::kw_noexcept)257
;
1211
530
}
1212
1213
static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
1214
5.70k
                          const FormatToken *FormatTok) {
1215
  // FIXME: This returns true for C/C++ keywords like 'struct'.
1216
5.70k
  return FormatTok->is(tok::identifier) &&
1217
5.70k
         
(1.35k
FormatTok->Tok.getIdentifierInfo() == nullptr1.35k
||
1218
1.35k
          !FormatTok->isOneOf(
1219
1.34k
              Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
1220
1.34k
              Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
1221
1.34k
              Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
1222
1.34k
              Keywords.kw_let, Keywords.kw_var, tok::kw_const,
1223
1.34k
              Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
1224
1.34k
              Keywords.kw_instanceof, Keywords.kw_interface,
1225
1.34k
              Keywords.kw_override, Keywords.kw_throws, Keywords.kw_from));
1226
5.70k
}
1227
1228
static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
1229
5.85k
                                 const FormatToken *FormatTok) {
1230
5.85k
  return FormatTok->Tok.isLiteral() ||
1231
5.85k
         
FormatTok->isOneOf(tok::kw_true, tok::kw_false)5.65k
||
1232
5.85k
         
mustBeJSIdent(Keywords, FormatTok)5.65k
;
1233
5.85k
}
1234
1235
// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
1236
// when encountered after a value (see mustBeJSIdentOrValue).
1237
static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
1238
188
                           const FormatToken *FormatTok) {
1239
188
  return FormatTok->isOneOf(
1240
188
      tok::kw_return, Keywords.kw_yield,
1241
      // conditionals
1242
188
      tok::kw_if, tok::kw_else,
1243
      // loops
1244
188
      tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
1245
      // switch/case
1246
188
      tok::kw_switch, tok::kw_case,
1247
      // exceptions
1248
188
      tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
1249
      // declaration
1250
188
      tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
1251
188
      Keywords.kw_async, Keywords.kw_function,
1252
      // import/export
1253
188
      Keywords.kw_import, tok::kw_export);
1254
188
}
1255
1256
// Checks whether a token is a type in K&R C (aka C78).
1257
21.4k
static bool isC78Type(const FormatToken &Tok) {
1258
21.4k
  return Tok.isOneOf(tok::kw_char, tok::kw_short, tok::kw_int, tok::kw_long,
1259
21.4k
                     tok::kw_unsigned, tok::kw_float, tok::kw_double,
1260
21.4k
                     tok::identifier);
1261
21.4k
}
1262
1263
// This function checks whether a token starts the first parameter declaration
1264
// in a K&R C (aka C78) function definition, e.g.:
1265
//   int f(a, b)
1266
//   short a, b;
1267
//   {
1268
//      return a + b;
1269
//   }
1270
static bool isC78ParameterDecl(const FormatToken *Tok, const FormatToken *Next,
1271
28.4k
                               const FormatToken *FuncName) {
1272
28.4k
  assert(Tok);
1273
0
  assert(Next);
1274
0
  assert(FuncName);
1275
1276
28.4k
  if (FuncName->isNot(tok::identifier))
1277
9.94k
    return false;
1278
1279
18.5k
  const FormatToken *Prev = FuncName->Previous;
1280
18.5k
  if (!Prev || 
(18.4k
Prev->isNot(tok::star)18.4k
&&
!isC78Type(*Prev)18.2k
))
1281
15.2k
    return false;
1282
1283
3.26k
  if (!isC78Type(*Tok) &&
1284
3.26k
      
!Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union)3.18k
)
1285
3.18k
    return false;
1286
1287
81
  if (Next->isNot(tok::star) && !Next->Tok.getIdentifierInfo())
1288
54
    return false;
1289
1290
27
  Tok = Tok->Previous;
1291
27
  if (!Tok || Tok->isNot(tok::r_paren))
1292
0
    return false;
1293
1294
27
  Tok = Tok->Previous;
1295
27
  if (!Tok || Tok->isNot(tok::identifier))
1296
0
    return false;
1297
1298
27
  return Tok->Previous && Tok->Previous->isOneOf(tok::l_paren, tok::comma);
1299
27
}
1300
1301
117
void UnwrappedLineParser::parseModuleImport() {
1302
117
  nextToken();
1303
369
  while (!eof()) {
1304
351
    if (FormatTok->is(tok::colon)) {
1305
18
      FormatTok->setFinalizedType(TT_ModulePartitionColon);
1306
18
    }
1307
    // Handle import <foo/bar.h> as we would an include statement.
1308
333
    else if (FormatTok->is(tok::less)) {
1309
18
      nextToken();
1310
72
      while (!FormatTok->isOneOf(tok::semi, tok::greater, tok::eof)) {
1311
        // Mark tokens up to the trailing line comments as implicit string
1312
        // literals.
1313
54
        if (FormatTok->isNot(tok::comment) &&
1314
54
            !FormatTok->TokenText.startswith("//"))
1315
54
          FormatTok->setFinalizedType(TT_ImplicitStringLiteral);
1316
54
        nextToken();
1317
54
      }
1318
18
    }
1319
351
    if (FormatTok->is(tok::semi)) {
1320
99
      nextToken();
1321
99
      break;
1322
99
    }
1323
252
    nextToken();
1324
252
  }
1325
1326
117
  addUnwrappedLine();
1327
117
}
1328
1329
// readTokenWithJavaScriptASI reads the next token and terminates the current
1330
// line if JavaScript Automatic Semicolon Insertion must
1331
// happen between the current token and the next token.
1332
//
1333
// This method is conservative - it cannot cover all edge cases of JavaScript,
1334
// but only aims to correctly handle certain well known cases. It *must not*
1335
// return true in speculative cases.
1336
34.1k
void UnwrappedLineParser::readTokenWithJavaScriptASI() {
1337
34.1k
  FormatToken *Previous = FormatTok;
1338
34.1k
  readToken();
1339
34.1k
  FormatToken *Next = FormatTok;
1340
1341
34.1k
  bool IsOnSameLine =
1342
34.1k
      CommentsBeforeNextToken.empty()
1343
34.1k
          ? 
Next->NewlinesBefore == 034.0k
1344
34.1k
          : 
CommentsBeforeNextToken.front()->NewlinesBefore == 093
;
1345
34.1k
  if (IsOnSameLine)
1346
31.2k
    return;
1347
1348
2.93k
  bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
1349
2.93k
  bool PreviousStartsTemplateExpr =
1350
2.93k
      Previous->is(TT_TemplateString) && 
Previous->TokenText.endswith("${")14
;
1351
2.93k
  if (PreviousMustBeValue || 
Previous->is(tok::r_paren)2.76k
) {
1352
    // If the line contains an '@' sign, the previous token might be an
1353
    // annotation, which can precede another identifier/value.
1354
1.41k
    bool HasAt = llvm::any_of(Line->Tokens, [](UnwrappedLineNode &LineNode) {
1355
1.41k
      return LineNode.Tok->is(tok::at);
1356
1.41k
    });
1357
240
    if (HasAt)
1358
22
      return;
1359
240
  }
1360
2.91k
  if (Next->is(tok::exclaim) && 
PreviousMustBeValue4
)
1361
4
    return addUnwrappedLine();
1362
2.91k
  bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
1363
2.91k
  bool NextEndsTemplateExpr =
1364
2.91k
      Next->is(TT_TemplateString) && 
Next->TokenText.startswith("}")26
;
1365
2.91k
  if (NextMustBeValue && 
!NextEndsTemplateExpr912
&&
!PreviousStartsTemplateExpr910
&&
1366
2.91k
      
(898
PreviousMustBeValue898
||
1367
898
       Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
1368
882
                         tok::minusminus)))
1369
34
    return addUnwrappedLine();
1370
2.87k
  if ((PreviousMustBeValue || 
Previous->is(tok::r_paren)2.73k
) &&
1371
2.87k
      
isJSDeclOrStmt(Keywords, Next)188
)
1372
28
    return addUnwrappedLine();
1373
2.87k
}
1374
1375
void UnwrappedLineParser::parseStructuralElement(IfStmtKind *IfKind,
1376
                                                 bool IsTopLevel,
1377
                                                 TokenType NextLBracesType,
1378
130k
                                                 bool *HasLabel) {
1379
130k
  if (Style.Language == FormatStyle::LK_TableGen &&
1380
130k
      
FormatTok->is(tok::pp_include)10
) {
1381
6
    nextToken();
1382
6
    if (FormatTok->is(tok::string_literal))
1383
6
      nextToken();
1384
6
    addUnwrappedLine();
1385
6
    return;
1386
6
  }
1387
129k
  switch (FormatTok->Tok.getKind()) {
1388
92
  case tok::kw_asm:
1389
92
    nextToken();
1390
92
    if (FormatTok->is(tok::l_brace)) {
1391
10
      FormatTok->setFinalizedType(TT_InlineASMBrace);
1392
10
      nextToken();
1393
90
      while (FormatTok && FormatTok->isNot(tok::eof)) {
1394
90
        if (FormatTok->is(tok::r_brace)) {
1395
10
          FormatTok->setFinalizedType(TT_InlineASMBrace);
1396
10
          nextToken();
1397
10
          addUnwrappedLine();
1398
10
          break;
1399
10
        }
1400
80
        FormatTok->Finalized = true;
1401
80
        nextToken();
1402
80
      }
1403
10
    }
1404
92
    break;
1405
3.99k
  case tok::kw_namespace:
1406
3.99k
    parseNamespace();
1407
3.99k
    return;
1408
1.29k
  case tok::kw_public:
1409
1.64k
  case tok::kw_protected:
1410
2.60k
  case tok::kw_private:
1411
2.60k
    if (Style.Language == FormatStyle::LK_Java || 
Style.isJavaScript()2.54k
||
1412
2.60k
        
Style.isCSharp()2.49k
)
1413
359
      nextToken();
1414
2.25k
    else
1415
2.25k
      parseAccessSpecifier();
1416
2.60k
    return;
1417
5.75k
  case tok::kw_if:
1418
5.75k
    if (Style.isJavaScript() && 
Line->MustBeDeclaration40
)
1419
      // field/method declaration.
1420
4
      break;
1421
5.75k
    parseIfThenElse(IfKind);
1422
5.75k
    return;
1423
1.83k
  case tok::kw_for:
1424
2.33k
  case tok::kw_while:
1425
2.33k
    if (Style.isJavaScript() && 
Line->MustBeDeclaration56
)
1426
      // field/method declaration.
1427
8
      break;
1428
2.32k
    parseForOrWhileLoop();
1429
2.32k
    return;
1430
278
  case tok::kw_do:
1431
278
    if (Style.isJavaScript() && 
Line->MustBeDeclaration4
)
1432
      // field/method declaration.
1433
4
      break;
1434
274
    parseDoWhile();
1435
274
    return;
1436
690
  case tok::kw_switch:
1437
690
    if (Style.isJavaScript() && 
Line->MustBeDeclaration8
)
1438
      // 'switch: string' field declaration.
1439
4
      break;
1440
686
    parseSwitch();
1441
686
    return;
1442
390
  case tok::kw_default:
1443
390
    if (Style.isJavaScript() && 
Line->MustBeDeclaration4
)
1444
      // 'default: string' field declaration.
1445
4
      break;
1446
386
    nextToken();
1447
386
    if (FormatTok->is(tok::colon)) {
1448
384
      parseLabel();
1449
384
      return;
1450
384
    }
1451
    // e.g. "default void f() {}" in a Java interface.
1452
2
    break;
1453
1.07k
  case tok::kw_case:
1454
1.07k
    if (Style.isJavaScript() && 
Line->MustBeDeclaration8
) {
1455
      // 'case: string' field declaration.
1456
4
      nextToken();
1457
4
      break;
1458
4
    }
1459
1.07k
    parseCaseLabel();
1460
1.07k
    return;
1461
235
  case tok::kw_try:
1462
271
  case tok::kw___try:
1463
271
    if (Style.isJavaScript() && 
Line->MustBeDeclaration10
)
1464
      // field/method declaration.
1465
4
      break;
1466
267
    parseTryCatch();
1467
267
    return;
1468
371
  case tok::kw_extern:
1469
371
    nextToken();
1470
371
    if (FormatTok->is(tok::string_literal)) {
1471
294
      nextToken();
1472
294
      if (FormatTok->is(tok::l_brace)) {
1473
228
        if (Style.BraceWrapping.AfterExternBlock)
1474
93
          addUnwrappedLine();
1475
        // Either we indent or for backwards compatibility we follow the
1476
        // AfterExternBlock style.
1477
228
        unsigned AddLevels =
1478
228
            (Style.IndentExternBlock == FormatStyle::IEBS_Indent) ||
1479
228
                    
(165
Style.BraceWrapping.AfterExternBlock165
&&
1480
165
                     Style.IndentExternBlock ==
1481
57
                         FormatStyle::IEBS_AfterExternBlock)
1482
228
                ? 
1u102
1483
228
                : 
0u126
;
1484
228
        parseBlock(/*MustBeDeclaration=*/true, AddLevels);
1485
228
        addUnwrappedLine();
1486
228
        return;
1487
228
      }
1488
294
    }
1489
143
    break;
1490
451
  case tok::kw_export:
1491
451
    if (Style.isJavaScript()) {
1492
232
      parseJavaScriptEs6ImportExport();
1493
232
      return;
1494
232
    }
1495
219
    if (!Style.isCpp())
1496
0
      break;
1497
    // Handle C++ "(inline|export) namespace".
1498
219
    LLVM_FALLTHROUGH;
1499
436
  case tok::kw_inline:
1500
436
    nextToken();
1501
436
    if (FormatTok->is(tok::kw_namespace)) {
1502
78
      parseNamespace();
1503
78
      return;
1504
78
    }
1505
358
    break;
1506
35.5k
  case tok::identifier:
1507
35.5k
    if (FormatTok->is(TT_ForEachMacro)) {
1508
240
      parseForOrWhileLoop();
1509
240
      return;
1510
240
    }
1511
35.2k
    if (FormatTok->is(TT_MacroBlockBegin)) {
1512
0
      parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
1513
0
                 /*MunchSemi=*/false);
1514
0
      return;
1515
0
    }
1516
35.2k
    if (FormatTok->is(Keywords.kw_import)) {
1517
528
      if (Style.isJavaScript()) {
1518
322
        parseJavaScriptEs6ImportExport();
1519
322
        return;
1520
322
      }
1521
206
      if (Style.Language == FormatStyle::LK_Proto) {
1522
12
        nextToken();
1523
12
        if (FormatTok->is(tok::kw_public))
1524
2
          nextToken();
1525
12
        if (!FormatTok->is(tok::string_literal))
1526
0
          return;
1527
12
        nextToken();
1528
12
        if (FormatTok->is(tok::semi))
1529
8
          nextToken();
1530
12
        addUnwrappedLine();
1531
12
        return;
1532
12
      }
1533
194
      if (Style.isCpp()) {
1534
117
        parseModuleImport();
1535
117
        return;
1536
117
      }
1537
194
    }
1538
34.8k
    if (Style.isCpp() &&
1539
34.8k
        FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
1540
31.2k
                           Keywords.kw_slots, Keywords.kw_qslots)) {
1541
45
      nextToken();
1542
45
      if (FormatTok->is(tok::colon)) {
1543
18
        nextToken();
1544
18
        addUnwrappedLine();
1545
18
        return;
1546
18
      }
1547
45
    }
1548
34.8k
    if (Style.isCpp() && 
FormatTok->is(TT_StatementMacro)31.2k
) {
1549
90
      parseStatementMacro();
1550
90
      return;
1551
90
    }
1552
34.7k
    if (Style.isCpp() && 
FormatTok->is(TT_NamespaceMacro)31.1k
) {
1553
160
      parseNamespace();
1554
160
      return;
1555
160
    }
1556
    // In all other cases, parse the declaration.
1557
34.5k
    break;
1558
75.9k
  default:
1559
75.9k
    break;
1560
129k
  }
1561
482k
  
do 111k
{
1562
482k
    const FormatToken *Previous = FormatTok->Previous;
1563
482k
    switch (FormatTok->Tok.getKind()) {
1564
1.95k
    case tok::at:
1565
1.95k
      nextToken();
1566
1.95k
      if (FormatTok->is(tok::l_brace)) {
1567
139
        nextToken();
1568
139
        parseBracedList();
1569
139
        break;
1570
1.81k
      } else if (Style.Language == FormatStyle::LK_Java &&
1571
1.81k
                 
FormatTok->is(Keywords.kw_interface)82
) {
1572
8
        nextToken();
1573
8
        break;
1574
8
      }
1575
1.81k
      switch (FormatTok->Tok.getObjCKeywordID()) {
1576
30
      case tok::objc_public:
1577
60
      case tok::objc_protected:
1578
90
      case tok::objc_package:
1579
147
      case tok::objc_private:
1580
147
        return parseAccessSpecifier();
1581
314
      case tok::objc_interface:
1582
437
      case tok::objc_implementation:
1583
437
        return parseObjCInterfaceOrImplementation();
1584
75
      case tok::objc_protocol:
1585
75
        if (parseObjCProtocol())
1586
69
          return;
1587
6
        break;
1588
433
      case tok::objc_end:
1589
433
        return; // Handled by the caller.
1590
20
      case tok::objc_optional:
1591
32
      case tok::objc_required:
1592
32
        nextToken();
1593
32
        addUnwrappedLine();
1594
32
        return;
1595
54
      case tok::objc_autoreleasepool:
1596
54
        nextToken();
1597
54
        if (FormatTok->is(tok::l_brace)) {
1598
48
          if (Style.BraceWrapping.AfterControlStatement ==
1599
48
              FormatStyle::BWACS_Always)
1600
12
            addUnwrappedLine();
1601
48
          parseBlock();
1602
48
        }
1603
54
        addUnwrappedLine();
1604
54
        return;
1605
36
      case tok::objc_synchronized:
1606
36
        nextToken();
1607
36
        if (FormatTok->is(tok::l_paren))
1608
          // Skip synchronization object
1609
30
          parseParens();
1610
36
        if (FormatTok->is(tok::l_brace)) {
1611
30
          if (Style.BraceWrapping.AfterControlStatement ==
1612
30
              FormatStyle::BWACS_Always)
1613
12
            addUnwrappedLine();
1614
30
          parseBlock();
1615
30
        }
1616
36
        addUnwrappedLine();
1617
36
        return;
1618
28
      case tok::objc_try:
1619
        // This branch isn't strictly necessary (the kw_try case below would
1620
        // do this too after the tok::at is parsed above).  But be explicit.
1621
28
        parseTryCatch();
1622
28
        return;
1623
569
      default:
1624
569
        break;
1625
1.81k
      }
1626
575
      break;
1627
621
    case tok::kw_concept:
1628
621
      parseConcept();
1629
621
      return;
1630
1.01k
    case tok::kw_requires: {
1631
1.01k
      if (Style.isCpp()) {
1632
1.00k
        bool ParsedClause = parseRequires();
1633
1.00k
        if (ParsedClause)
1634
969
          return;
1635
1.00k
      } else {
1636
4
        nextToken();
1637
4
      }
1638
42
      break;
1639
1.01k
    }
1640
2.48k
    case tok::kw_enum:
1641
      // Ignore if this is part of "template <enum ...".
1642
2.48k
      if (Previous && 
Previous->is(tok::less)186
) {
1643
18
        nextToken();
1644
18
        break;
1645
18
      }
1646
1647
      // parseEnum falls through and does not yet add an unwrapped line as an
1648
      // enum definition can start a structural element.
1649
2.46k
      if (!parseEnum())
1650
46
        break;
1651
      // This only applies for C++.
1652
2.42k
      if (!Style.isCpp()) {
1653
100
        addUnwrappedLine();
1654
100
        return;
1655
100
      }
1656
2.32k
      break;
1657
2.32k
    case tok::kw_typedef:
1658
958
      nextToken();
1659
958
      if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1660
958
                             Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS,
1661
958
                             Keywords.kw_CF_CLOSED_ENUM,
1662
958
                             Keywords.kw_NS_CLOSED_ENUM))
1663
74
        parseEnum();
1664
958
      break;
1665
4.14k
    case tok::kw_struct:
1666
4.38k
    case tok::kw_union:
1667
9.63k
    case tok::kw_class:
1668
9.63k
      if (parseStructLike())
1669
411
        return;
1670
9.22k
      break;
1671
9.22k
    case tok::period:
1672
3.18k
      nextToken();
1673
      // In Java, classes have an implicit static member "class".
1674
3.18k
      if (Style.Language == FormatStyle::LK_Java && 
FormatTok184
&&
1675
3.18k
          
FormatTok->is(tok::kw_class)184
)
1676
4
        nextToken();
1677
3.18k
      if (Style.isJavaScript() && 
FormatTok473
&&
1678
3.18k
          
FormatTok->Tok.getIdentifierInfo()473
)
1679
        // JavaScript only has pseudo keywords, all keywords are allowed to
1680
        // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1681
453
        nextToken();
1682
3.18k
      break;
1683
80.8k
    case tok::semi:
1684
80.8k
      nextToken();
1685
80.8k
      addUnwrappedLine();
1686
80.8k
      return;
1687
954
    case tok::r_brace:
1688
954
      addUnwrappedLine();
1689
954
      return;
1690
42.0k
    case tok::l_paren: {
1691
42.0k
      parseParens();
1692
      // Break the unwrapped line if a K&R C function definition has a parameter
1693
      // declaration.
1694
42.0k
      if (!IsTopLevel || 
!Style.isCpp()31.9k
||
!Previous30.5k
||
FormatTok->is(tok::eof)29.9k
)
1695
13.5k
        break;
1696
28.4k
      if (isC78ParameterDecl(FormatTok, Tokens->peekNextToken(), Previous)) {
1697
27
        addUnwrappedLine();
1698
27
        return;
1699
27
      }
1700
28.4k
      break;
1701
28.4k
    }
1702
28.4k
    case tok::kw_operator:
1703
3.24k
      nextToken();
1704
3.24k
      if (FormatTok->isBinaryOperator())
1705
1.42k
        nextToken();
1706
3.24k
      break;
1707
195
    case tok::caret:
1708
195
      nextToken();
1709
195
      if (FormatTok->Tok.isAnyIdentifier() ||
1710
195
          
FormatTok->isSimpleTypeSpecifier()132
)
1711
82
        nextToken();
1712
195
      if (FormatTok->is(tok::l_paren))
1713
59
        parseParens();
1714
195
      if (FormatTok->is(tok::l_brace))
1715
97
        parseChildBlock();
1716
195
      break;
1717
16.5k
    case tok::l_brace:
1718
16.5k
      if (NextLBracesType != TT_Unknown)
1719
18
        FormatTok->setFinalizedType(NextLBracesType);
1720
16.5k
      if (!tryToParsePropertyAccessor() && 
!tryToParseBracedList()16.4k
) {
1721
        // A block outside of parentheses must be the last part of a
1722
        // structural element.
1723
        // FIXME: Figure out cases where this is not true, and add projections
1724
        // for them (the one we know is missing are lambdas).
1725
14.3k
        if (Style.Language == FormatStyle::LK_Java &&
1726
14.3k
            
Line->Tokens.front().Tok->is(Keywords.kw_synchronized)113
) {
1727
          // If necessary, we could set the type to something different than
1728
          // TT_FunctionLBrace.
1729
6
          if (Style.BraceWrapping.AfterControlStatement ==
1730
6
              FormatStyle::BWACS_Always)
1731
2
            addUnwrappedLine();
1732
14.3k
        } else if (Style.BraceWrapping.AfterFunction) {
1733
1.52k
          addUnwrappedLine();
1734
1.52k
        }
1735
14.3k
        if (!Line->InPPDirective)
1736
14.2k
          FormatTok->setFinalizedType(TT_FunctionLBrace);
1737
14.3k
        parseBlock();
1738
14.3k
        addUnwrappedLine();
1739
14.3k
        return;
1740
14.3k
      }
1741
      // Otherwise this was a braced init list, and the structural
1742
      // element continues.
1743
2.19k
      break;
1744
2.19k
    case tok::kw_try:
1745
64
      if (Style.isJavaScript() && 
Line->MustBeDeclaration4
) {
1746
        // field/method declaration.
1747
4
        nextToken();
1748
4
        break;
1749
4
      }
1750
      // We arrive here when parsing function-try blocks.
1751
60
      if (Style.BraceWrapping.AfterFunction)
1752
11
        addUnwrappedLine();
1753
60
      parseTryCatch();
1754
60
      return;
1755
131k
    case tok::identifier: {
1756
131k
      if (Style.isCSharp() && 
FormatTok->is(Keywords.kw_where)1.09k
&&
1757
131k
          
Line->MustBeDeclaration2
) {
1758
2
        addUnwrappedLine();
1759
2
        parseCSharpGenericTypeConstraint();
1760
2
        break;
1761
2
      }
1762
131k
      if (FormatTok->is(TT_MacroBlockEnd)) {
1763
12
        addUnwrappedLine();
1764
12
        return;
1765
12
      }
1766
1767
      // Function declarations (as opposed to function expressions) are parsed
1768
      // on their own unwrapped line by continuing this loop. Function
1769
      // expressions (functions that are not on their own line) must not create
1770
      // a new unwrapped line, so they are special cased below.
1771
131k
      size_t TokenCount = Line->Tokens.size();
1772
131k
      if (Style.isJavaScript() && 
FormatTok->is(Keywords.kw_function)6.01k
&&
1773
131k
          
(290
TokenCount > 1290
||
(212
TokenCount == 1212
&& !Line->Tokens.front().Tok->is(
1774
98
                                                     Keywords.kw_async)))) {
1775
98
        tryToParseJSFunction();
1776
98
        break;
1777
98
      }
1778
131k
      if ((Style.isJavaScript() || 
Style.Language == FormatStyle::LK_Java125k
) &&
1779
131k
          
FormatTok->is(Keywords.kw_interface)6.88k
) {
1780
56
        if (Style.isJavaScript()) {
1781
          // In JavaScript/TypeScript, "interface" can be used as a standalone
1782
          // identifier, e.g. in `var interface = 1;`. If "interface" is
1783
          // followed by another identifier, it is very like to be an actual
1784
          // interface declaration.
1785
52
          unsigned StoredPosition = Tokens->getPosition();
1786
52
          FormatToken *Next = Tokens->getNextToken();
1787
52
          FormatTok = Tokens->setPosition(StoredPosition);
1788
52
          if (!mustBeJSIdent(Keywords, Next)) {
1789
16
            nextToken();
1790
16
            break;
1791
16
          }
1792
52
        }
1793
40
        parseRecord();
1794
40
        addUnwrappedLine();
1795
40
        return;
1796
56
      }
1797
1798
131k
      if (FormatTok->is(Keywords.kw_interface)) {
1799
41
        if (parseStructLike())
1800
40
          return;
1801
1
        break;
1802
41
      }
1803
1804
131k
      if (Style.isCpp() && 
FormatTok->is(TT_StatementMacro)122k
) {
1805
9
        parseStatementMacro();
1806
9
        return;
1807
9
      }
1808
1809
      // See if the following token should start a new unwrapped line.
1810
131k
      StringRef Text = FormatTok->TokenText;
1811
1812
131k
      FormatToken *PreviousToken = FormatTok;
1813
131k
      nextToken();
1814
1815
      // JS doesn't have macros, and within classes colons indicate fields, not
1816
      // labels.
1817
131k
      if (Style.isJavaScript())
1818
5.86k
        break;
1819
1820
125k
      TokenCount = Line->Tokens.size();
1821
125k
      if (TokenCount == 1 ||
1822
125k
          
(93.9k
TokenCount == 293.9k
&&
Line->Tokens.front().Tok->is(tok::comment)37.1k
)) {
1823
31.3k
        if (FormatTok->is(tok::colon) && 
!Line->MustBeDeclaration282
) {
1824
252
          Line->Tokens.begin()->Tok->MustBreakBefore = true;
1825
252
          parseLabel(!Style.IndentGotoLabels);
1826
252
          if (HasLabel)
1827
171
            *HasLabel = true;
1828
252
          return;
1829
252
        }
1830
        // Recognize function-like macro usages without trailing semicolon as
1831
        // well as free-standing macros like Q_OBJECT.
1832
31.1k
        bool FunctionLike = FormatTok->is(tok::l_paren);
1833
31.1k
        if (FunctionLike)
1834
13.9k
          parseParens();
1835
1836
31.1k
        bool FollowedByNewline =
1837
31.1k
            CommentsBeforeNextToken.empty()
1838
31.1k
                ? 
FormatTok->NewlinesBefore > 031.1k
1839
31.1k
                : 
CommentsBeforeNextToken.front()->NewlinesBefore > 012
;
1840
1841
31.1k
        if (FollowedByNewline && 
(557
Text.size() >= 5557
||
FunctionLike181
) &&
1842
31.1k
            
tokenCanStartNewLine(*FormatTok)530
&&
Text == Text.upper()254
) {
1843
114
          PreviousToken->setFinalizedType(TT_FunctionLikeOrFreestandingMacro);
1844
114
          addUnwrappedLine();
1845
114
          return;
1846
114
        }
1847
31.1k
      }
1848
124k
      break;
1849
125k
    }
1850
124k
    case tok::equal:
1851
17.9k
      if ((Style.isJavaScript() || 
Style.isCSharp()16.2k
) &&
1852
17.9k
          
FormatTok->is(TT_FatArrow)1.83k
) {
1853
102
        tryToParseChildBlock();
1854
102
        break;
1855
102
      }
1856
1857
17.8k
      nextToken();
1858
17.8k
      if (FormatTok->is(tok::l_brace)) {
1859
        // Block kind should probably be set to BK_BracedInit for any language.
1860
        // C# needs this change to ensure that array initialisers and object
1861
        // initialisers are indented the same way.
1862
1.68k
        if (Style.isCSharp())
1863
4
          FormatTok->setBlockKind(BK_BracedInit);
1864
1.68k
        nextToken();
1865
1.68k
        parseBracedList();
1866
16.1k
      } else if (Style.Language == FormatStyle::LK_Proto &&
1867
16.1k
                 
FormatTok->is(tok::less)95
) {
1868
22
        nextToken();
1869
22
        parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
1870
22
                        /*ClosingBraceKind=*/tok::greater);
1871
22
      }
1872
17.8k
      break;
1873
5.24k
    case tok::l_square:
1874
5.24k
      parseSquare();
1875
5.24k
      break;
1876
509
    case tok::kw_new:
1877
509
      parseNew();
1878
509
      break;
1879
31
    case tok::kw_case:
1880
31
      if (Style.isJavaScript() && 
Line->MustBeDeclaration4
) {
1881
        // 'case: string' field declaration.
1882
4
        nextToken();
1883
4
        break;
1884
4
      }
1885
27
      parseCaseLabel();
1886
27
      break;
1887
163k
    default:
1888
163k
      nextToken();
1889
163k
      break;
1890
482k
    }
1891
482k
  } while (
!eof()382k
);
1892
111k
}
1893
1894
16.5k
bool UnwrappedLineParser::tryToParsePropertyAccessor() {
1895
16.5k
  assert(FormatTok->is(tok::l_brace));
1896
16.5k
  if (!Style.isCSharp())
1897
16.2k
    return false;
1898
  // See if it's a property accessor.
1899
296
  if (FormatTok->Previous->isNot(tok::identifier))
1900
204
    return false;
1901
1902
  // See if we are inside a property accessor.
1903
  //
1904
  // Record the current tokenPosition so that we can advance and
1905
  // reset the current token. `Next` is not set yet so we need
1906
  // another way to advance along the token stream.
1907
92
  unsigned int StoredPosition = Tokens->getPosition();
1908
92
  FormatToken *Tok = Tokens->getNextToken();
1909
1910
  // A trivial property accessor is of the form:
1911
  // { [ACCESS_SPECIFIER] [get]; [ACCESS_SPECIFIER] [set|init] }
1912
  // Track these as they do not require line breaks to be introduced.
1913
92
  bool HasSpecialAccessor = false;
1914
92
  bool IsTrivialPropertyAccessor = true;
1915
334
  while (!eof()) {
1916
334
    if (Tok->isOneOf(tok::semi, tok::kw_public, tok::kw_private,
1917
334
                     tok::kw_protected, Keywords.kw_internal, Keywords.kw_get,
1918
334
                     Keywords.kw_init, Keywords.kw_set)) {
1919
242
      if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set))
1920
128
        HasSpecialAccessor = true;
1921
242
      Tok = Tokens->getNextToken();
1922
242
      continue;
1923
242
    }
1924
92
    if (Tok->isNot(tok::r_brace))
1925
20
      IsTrivialPropertyAccessor = false;
1926
92
    break;
1927
334
  }
1928
1929
92
  if (!HasSpecialAccessor) {
1930
10
    Tokens->setPosition(StoredPosition);
1931
10
    return false;
1932
10
  }
1933
1934
  // Try to parse the property accessor:
1935
  // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
1936
82
  Tokens->setPosition(StoredPosition);
1937
82
  if (!IsTrivialPropertyAccessor && 
Style.BraceWrapping.AfterFunction14
)
1938
2
    addUnwrappedLine();
1939
82
  nextToken();
1940
362
  do {
1941
362
    switch (FormatTok->Tok.getKind()) {
1942
82
    case tok::r_brace:
1943
82
      nextToken();
1944
82
      if (FormatTok->is(tok::equal)) {
1945
96
        while (!eof() && 
FormatTok->isNot(tok::semi)90
)
1946
78
          nextToken();
1947
18
        nextToken();
1948
18
      }
1949
82
      addUnwrappedLine();
1950
82
      return true;
1951
8
    case tok::l_brace:
1952
8
      ++Line->Level;
1953
8
      parseBlock(/*MustBeDeclaration=*/true);
1954
8
      addUnwrappedLine();
1955
8
      --Line->Level;
1956
8
      break;
1957
20
    case tok::equal:
1958
20
      if (FormatTok->is(TT_FatArrow)) {
1959
20
        ++Line->Level;
1960
60
        do {
1961
60
          nextToken();
1962
60
        } while (!eof() && FormatTok->isNot(tok::semi));
1963
20
        nextToken();
1964
20
        addUnwrappedLine();
1965
20
        --Line->Level;
1966
20
        break;
1967
20
      }
1968
0
      nextToken();
1969
0
      break;
1970
252
    default:
1971
252
      if (FormatTok->isOneOf(Keywords.kw_get, Keywords.kw_init,
1972
252
                             Keywords.kw_set) &&
1973
252
          
!IsTrivialPropertyAccessor142
) {
1974
        // Non-trivial get/set needs to be on its own line.
1975
28
        addUnwrappedLine();
1976
28
      }
1977
252
      nextToken();
1978
362
    }
1979
362
  } while (
!eof()280
);
1980
1981
  // Unreachable for well-formed code (paired '{' and '}').
1982
0
  return true;
1983
82
}
1984
1985
9.29k
bool UnwrappedLineParser::tryToParseLambda() {
1986
9.29k
  assert(FormatTok->is(tok::l_square));
1987
9.29k
  if (!Style.isCpp()) {
1988
658
    nextToken();
1989
658
    return false;
1990
658
  }
1991
8.63k
  FormatToken &LSquare = *FormatTok;
1992
8.63k
  if (!tryToParseLambdaIntroducer())
1993
3.86k
    return false;
1994
1995
4.77k
  bool SeenArrow = false;
1996
4.77k
  bool InTemplateParameterList = false;
1997
1998
11.9k
  while (FormatTok->isNot(tok::l_brace)) {
1999
9.12k
    if (FormatTok->isSimpleTypeSpecifier()) {
2000
162
      nextToken();
2001
162
      continue;
2002
162
    }
2003
8.96k
    switch (FormatTok->Tok.getKind()) {
2004
0
    case tok::l_brace:
2005
0
      break;
2006
2.45k
    case tok::l_paren:
2007
2.45k
      parseParens();
2008
2.45k
      break;
2009
72
    case tok::l_square:
2010
72
      parseSquare();
2011
72
      break;
2012
54
    case tok::kw_class:
2013
72
    case tok::kw_template:
2014
99
    case tok::kw_typename:
2015
99
      assert(FormatTok->Previous);
2016
99
      if (FormatTok->Previous->is(tok::less))
2017
72
        InTemplateParameterList = true;
2018
99
      nextToken();
2019
99
      break;
2020
42
    case tok::amp:
2021
129
    case tok::star:
2022
156
    case tok::kw_const:
2023
198
    case tok::comma:
2024
816
    case tok::less:
2025
1.43k
    case tok::greater:
2026
2.33k
    case tok::identifier:
2027
3.00k
    case tok::numeric_constant:
2028
3.09k
    case tok::coloncolon:
2029
3.12k
    case tok::kw_mutable:
2030
3.13k
    case tok::kw_noexcept:
2031
3.13k
      nextToken();
2032
3.13k
      break;
2033
    // Specialization of a template with an integer parameter can contain
2034
    // arithmetic, logical, comparison and ternary operators.
2035
    //
2036
    // FIXME: This also accepts sequences of operators that are not in the scope
2037
    // of a template argument list.
2038
    //
2039
    // In a C++ lambda a template type can only occur after an arrow. We use
2040
    // this as an heuristic to distinguish between Objective-C expressions
2041
    // followed by an `a->b` expression, such as:
2042
    // ([obj func:arg] + a->b)
2043
    // Otherwise the code below would parse as a lambda.
2044
    //
2045
    // FIXME: This heuristic is incorrect for C++20 generic lambdas with
2046
    // explicit template lists: []<bool b = true && false>(U &&u){}
2047
42
    case tok::plus:
2048
72
    case tok::minus:
2049
90
    case tok::exclaim:
2050
108
    case tok::tilde:
2051
138
    case tok::slash:
2052
168
    case tok::percent:
2053
198
    case tok::lessless:
2054
228
    case tok::pipe:
2055
258
    case tok::pipepipe:
2056
312
    case tok::ampamp:
2057
318
    case tok::caret:
2058
372
    case tok::equalequal:
2059
402
    case tok::exclaimequal:
2060
432
    case tok::greaterequal:
2061
462
    case tok::lessequal:
2062
495
    case tok::question:
2063
557
    case tok::colon:
2064
611
    case tok::ellipsis:
2065
647
    case tok::kw_true:
2066
701
    case tok::kw_false:
2067
701
      if (SeenArrow || 
InTemplateParameterList305
) {
2068
432
        nextToken();
2069
432
        break;
2070
432
      }
2071
269
      return true;
2072
852
    case tok::arrow:
2073
      // This might or might not actually be a lambda arrow (this could be an
2074
      // ObjC method invocation followed by a dereferencing arrow). We might
2075
      // reset this back to TT_Unknown in TokenAnnotator.
2076
852
      FormatTok->setFinalizedType(TT_LambdaArrow);
2077
852
      SeenArrow = true;
2078
852
      nextToken();
2079
852
      break;
2080
1.64k
    default:
2081
1.64k
      return true;
2082
8.96k
    }
2083
8.96k
  }
2084
2.85k
  FormatTok->setFinalizedType(TT_LambdaLBrace);
2085
2.85k
  LSquare.setFinalizedType(TT_LambdaLSquare);
2086
2.85k
  parseChildBlock();
2087
2.85k
  return true;
2088
4.77k
}
2089
2090
8.63k
bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
2091
8.63k
  const FormatToken *Previous = FormatTok->Previous;
2092
8.63k
  const FormatToken *LeftSquare = FormatTok;
2093
8.63k
  nextToken();
2094
8.63k
  if (Previous &&
2095
8.63k
      
(7.03k
Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
2096
7.03k
                         tok::kw_delete, tok::l_square) ||
2097
7.03k
       
LeftSquare->isCppStructuredBinding(Style)4.69k
||
Previous->closesScope()4.43k
||
2098
7.03k
       
Previous->isSimpleTypeSpecifier()3.83k
)) {
2099
3.26k
    return false;
2100
3.26k
  }
2101
5.36k
  if (FormatTok->is(tok::l_square))
2102
559
    return false;
2103
4.80k
  if (FormatTok->is(tok::r_square)) {
2104
2.32k
    const FormatToken *Next = Tokens->peekNextToken();
2105
2.32k
    if (Next->is(tok::greater))
2106
36
      return false;
2107
2.32k
  }
2108
4.77k
  parseSquare(/*LambdaIntroducer=*/true);
2109
4.77k
  return true;
2110
4.80k
}
2111
2112
298
void UnwrappedLineParser::tryToParseJSFunction() {
2113
298
  assert(FormatTok->is(Keywords.kw_function) ||
2114
298
         FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
2115
298
  if (FormatTok->is(Keywords.kw_async))
2116
0
    nextToken();
2117
  // Consume "function".
2118
298
  nextToken();
2119
2120
  // Consume * (generator function). Treat it like C++'s overloaded operators.
2121
298
  if (FormatTok->is(tok::star)) {
2122
4
    FormatTok->setFinalizedType(TT_OverloadedOperator);
2123
4
    nextToken();
2124
4
  }
2125
2126
  // Consume function name.
2127
298
  if (FormatTok->is(tok::identifier))
2128
52
    nextToken();
2129
2130
298
  if (FormatTok->isNot(tok::l_paren))
2131
0
    return;
2132
2133
  // Parse formal parameter list.
2134
298
  parseParens();
2135
2136
298
  if (FormatTok->is(tok::colon)) {
2137
    // Parse a type definition.
2138
16
    nextToken();
2139
2140
    // Eat the type declaration. For braced inline object types, balance braces,
2141
    // otherwise just parse until finding an l_brace for the function body.
2142
16
    if (FormatTok->is(tok::l_brace))
2143
8
      tryToParseBracedList();
2144
8
    else
2145
16
      
while (8
!FormatTok->isOneOf(tok::l_brace, tok::semi) &&
!eof()8
)
2146
8
        nextToken();
2147
16
  }
2148
2149
298
  if (FormatTok->is(tok::semi))
2150
16
    return;
2151
2152
282
  parseChildBlock();
2153
282
}
2154
2155
20.1k
bool UnwrappedLineParser::tryToParseBracedList() {
2156
20.1k
  if (FormatTok->is(BK_Unknown))
2157
17.9k
    calculateBraceTypes();
2158
20.1k
  assert(FormatTok->isNot(BK_Unknown));
2159
20.1k
  if (FormatTok->is(BK_Block))
2160
16.8k
    return false;
2161
3.29k
  nextToken();
2162
3.29k
  parseBracedList();
2163
3.29k
  return true;
2164
20.1k
}
2165
2166
164
bool UnwrappedLineParser::tryToParseChildBlock() {
2167
164
  assert(Style.isJavaScript() || Style.isCSharp());
2168
0
  assert(FormatTok->is(TT_FatArrow));
2169
  // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType TT_FatArrow.
2170
  // They always start an expression or a child block if followed by a curly
2171
  // brace.
2172
0
  nextToken();
2173
164
  if (FormatTok->isNot(tok::l_brace))
2174
66
    return false;
2175
98
  parseChildBlock();
2176
98
  return true;
2177
164
}
2178
2179
bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
2180
                                          bool IsEnum,
2181
12.9k
                                          tok::TokenKind ClosingBraceKind) {
2182
12.9k
  bool HasError = false;
2183
2184
  // FIXME: Once we have an expression parser in the UnwrappedLineParser,
2185
  // replace this by using parseAssignmentExpression() inside.
2186
65.1k
  do {
2187
65.1k
    if (Style.isCSharp() && 
FormatTok->is(TT_FatArrow)482
&&
2188
65.1k
        
tryToParseChildBlock()10
)
2189
4
      continue;
2190
65.1k
    if (Style.isJavaScript()) {
2191
4.00k
      if (FormatTok->is(Keywords.kw_function) ||
2192
4.00k
          
FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)3.94k
) {
2193
60
        tryToParseJSFunction();
2194
60
        continue;
2195
60
      }
2196
3.94k
      if (FormatTok->is(tok::l_brace)) {
2197
        // Could be a method inside of a braced list `{a() { return 1; }}`.
2198
36
        if (tryToParseBracedList())
2199
28
          continue;
2200
8
        parseChildBlock();
2201
8
      }
2202
3.94k
    }
2203
65.0k
    if (FormatTok->Tok.getKind() == ClosingBraceKind) {
2204
12.2k
      if (IsEnum && 
!Style.AllowShortEnumsOnASingleLine2.42k
)
2205
107
        addUnwrappedLine();
2206
12.2k
      nextToken();
2207
12.2k
      return !HasError;
2208
12.2k
    }
2209
52.7k
    switch (FormatTok->Tok.getKind()) {
2210
680
    case tok::l_square:
2211
680
      if (Style.isCSharp())
2212
8
        parseSquare();
2213
672
      else
2214
672
        tryToParseLambda();
2215
680
      break;
2216
733
    case tok::l_paren:
2217
733
      parseParens();
2218
      // JavaScript can just have free standing methods and getters/setters in
2219
      // object literals. Detect them by a "{" following ")".
2220
733
      if (Style.isJavaScript()) {
2221
40
        if (FormatTok->is(tok::l_brace))
2222
24
          parseChildBlock();
2223
40
        break;
2224
40
      }
2225
693
      break;
2226
2.93k
    case tok::l_brace:
2227
      // Assume there are no blocks inside a braced init list apart
2228
      // from the ones we explicitly parse out (like lambdas).
2229
2.93k
      FormatTok->setBlockKind(BK_BracedInit);
2230
2.93k
      nextToken();
2231
2.93k
      parseBracedList();
2232
2.93k
      break;
2233
492
    case tok::less:
2234
492
      if (Style.Language == FormatStyle::LK_Proto ||
2235
492
          
ClosingBraceKind == tok::greater436
) {
2236
220
        nextToken();
2237
220
        parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
2238
220
                        /*ClosingBraceKind=*/tok::greater);
2239
272
      } else {
2240
272
        nextToken();
2241
272
      }
2242
492
      break;
2243
96
    case tok::semi:
2244
      // JavaScript (or more precisely TypeScript) can have semicolons in braced
2245
      // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
2246
      // used for error recovery if we have otherwise determined that this is
2247
      // a braced list.
2248
96
      if (Style.isJavaScript()) {
2249
36
        nextToken();
2250
36
        break;
2251
36
      }
2252
60
      HasError = true;
2253
60
      if (!ContinueOnSemicolons)
2254
33
        return !HasError;
2255
27
      nextToken();
2256
27
      break;
2257
14.2k
    case tok::comma:
2258
14.2k
      nextToken();
2259
14.2k
      if (IsEnum && 
!Style.AllowShortEnumsOnASingleLine2.02k
)
2260
183
        addUnwrappedLine();
2261
14.2k
      break;
2262
33.6k
    default:
2263
33.6k
      nextToken();
2264
33.6k
      break;
2265
52.7k
    }
2266
52.8k
  } while (!eof());
2267
628
  return false;
2268
12.9k
}
2269
2270
/// \brief Parses a pair of parentheses (and everything between them).
2271
/// \param AmpAmpTokenType If different than TT_Unknown sets this type for all
2272
/// double ampersands. This only counts for the current parens scope.
2273
81.0k
void UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
2274
81.0k
  assert(FormatTok->is(tok::l_paren) && "'(' expected.");
2275
0
  nextToken();
2276
230k
  do {
2277
230k
    switch (FormatTok->Tok.getKind()) {
2278
7.53k
    case tok::l_paren:
2279
7.53k
      parseParens();
2280
7.53k
      if (Style.Language == FormatStyle::LK_Java && 
FormatTok->is(tok::l_brace)22
)
2281
10
        parseChildBlock();
2282
7.53k
      break;
2283
80.9k
    case tok::r_paren:
2284
80.9k
      nextToken();
2285
80.9k
      return;
2286
3
    case tok::r_brace:
2287
      // A "}" inside parenthesis is an error if there wasn't a matching "{".
2288
3
      return;
2289
2.21k
    case tok::l_square:
2290
2.21k
      tryToParseLambda();
2291
2.21k
      break;
2292
1.43k
    case tok::l_brace:
2293
1.43k
      if (!tryToParseBracedList())
2294
393
        parseChildBlock();
2295
1.43k
      break;
2296
42
    case tok::at:
2297
42
      nextToken();
2298
42
      if (FormatTok->is(tok::l_brace)) {
2299
6
        nextToken();
2300
6
        parseBracedList();
2301
6
      }
2302
42
      break;
2303
2.16k
    case tok::equal:
2304
2.16k
      if (Style.isCSharp() && 
FormatTok->is(TT_FatArrow)84
)
2305
52
        tryToParseChildBlock();
2306
2.11k
      else
2307
2.11k
        nextToken();
2308
2.16k
      break;
2309
112
    case tok::kw_class:
2310
112
      if (Style.isJavaScript())
2311
4
        parseRecord(/*ParseAsExpr=*/true);
2312
108
      else
2313
108
        nextToken();
2314
112
      break;
2315
68.3k
    case tok::identifier:
2316
68.3k
      if (Style.isJavaScript() &&
2317
68.3k
          
(1.70k
FormatTok->is(Keywords.kw_function)1.70k
||
2318
1.70k
           
FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)1.56k
))
2319
140
        tryToParseJSFunction();
2320
68.1k
      else
2321
68.1k
        nextToken();
2322
68.3k
      break;
2323
98
    case tok::kw_requires: {
2324
98
      auto RequiresToken = FormatTok;
2325
98
      nextToken();
2326
98
      parseRequiresExpression(RequiresToken);
2327
98
      break;
2328
0
    }
2329
1.23k
    case tok::ampamp:
2330
1.23k
      if (AmpAmpTokenType != TT_Unknown)
2331
59
        FormatTok->setFinalizedType(AmpAmpTokenType);
2332
1.23k
      LLVM_FALLTHROUGH;
2333
67.5k
    default:
2334
67.5k
      nextToken();
2335
67.5k
      break;
2336
230k
    }
2337
230k
  } while (
!eof()149k
);
2338
81.0k
}
2339
2340
11.1k
void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
2341
11.1k
  if (!LambdaIntroducer) {
2342
6.34k
    assert(FormatTok->is(tok::l_square) && "'[' expected.");
2343
6.34k
    if (tryToParseLambda())
2344
2.92k
      return;
2345
6.34k
  }
2346
24.9k
  
do 8.19k
{
2347
24.9k
    switch (FormatTok->Tok.getKind()) {
2348
592
    case tok::l_paren:
2349
592
      parseParens();
2350
592
      break;
2351
8.17k
    case tok::r_square:
2352
8.17k
      nextToken();
2353
8.17k
      return;
2354
0
    case tok::r_brace:
2355
      // A "}" inside parenthesis is an error if there wasn't a matching "{".
2356
0
      return;
2357
730
    case tok::l_square:
2358
730
      parseSquare();
2359
730
      break;
2360
384
    case tok::l_brace: {
2361
384
      if (!tryToParseBracedList())
2362
330
        parseChildBlock();
2363
384
      break;
2364
0
    }
2365
143
    case tok::at:
2366
143
      nextToken();
2367
143
      if (FormatTok->is(tok::l_brace)) {
2368
39
        nextToken();
2369
39
        parseBracedList();
2370
39
      }
2371
143
      break;
2372
14.9k
    default:
2373
14.9k
      nextToken();
2374
14.9k
      break;
2375
24.9k
    }
2376
24.9k
  } while (
!eof()16.7k
);
2377
8.19k
}
2378
2379
10.7k
void UnwrappedLineParser::keepAncestorBraces() {
2380
10.7k
  if (!Style.RemoveBracesLLVM)
2381
9.94k
    return;
2382
2383
804
  const int MaxNestingLevels = 2;
2384
804
  const int Size = NestedTooDeep.size();
2385
804
  if (Size >= MaxNestingLevels)
2386
26
    NestedTooDeep[Size - MaxNestingLevels] = true;
2387
804
  NestedTooDeep.push_back(false);
2388
804
}
2389
2390
130
static FormatToken *getLastNonComment(const UnwrappedLine &Line) {
2391
130
  for (const auto &Token : llvm::reverse(Line.Tokens))
2392
134
    if (Token.Tok->isNot(tok::comment))
2393
130
      return Token.Tok;
2394
2395
0
  return nullptr;
2396
130
}
2397
2398
5.19k
void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
2399
5.19k
  FormatToken *Tok = nullptr;
2400
2401
5.19k
  if (Style.InsertBraces && 
!Line->InPPDirective102
&&
!Line->Tokens.empty()90
&&
2402
5.19k
      
PreprocessorDirectives.empty()90
) {
2403
66
    Tok = getLastNonComment(*Line);
2404
66
    assert(Tok);
2405
66
    if (Tok->BraceCount < 0) {
2406
2
      assert(Tok->BraceCount == -1);
2407
0
      Tok = nullptr;
2408
64
    } else {
2409
64
      Tok->BraceCount = -1;
2410
64
    }
2411
66
  }
2412
2413
0
  addUnwrappedLine();
2414
5.19k
  ++Line->Level;
2415
5.19k
  parseStructuralElement();
2416
2417
5.19k
  if (Tok) {
2418
64
    assert(!Line->InPPDirective);
2419
0
    Tok = nullptr;
2420
68
    for (const auto &L : llvm::reverse(*CurrentLines)) {
2421
68
      if (!L.InPPDirective && 
getLastNonComment(L)64
) {
2422
64
        Tok = L.Tokens.back().Tok;
2423
64
        break;
2424
64
      }
2425
68
    }
2426
64
    assert(Tok);
2427
0
    ++Tok->BraceCount;
2428
64
  }
2429
2430
5.19k
  if (CheckEOF && 
FormatTok->is(tok::eof)786
)
2431
703
    addUnwrappedLine();
2432
2433
5.19k
  --Line->Level;
2434
5.19k
}
2435
2436
612
static void markOptionalBraces(FormatToken *LeftBrace) {
2437
612
  if (!LeftBrace)
2438
506
    return;
2439
2440
106
  assert(LeftBrace->is(tok::l_brace));
2441
2442
0
  FormatToken *RightBrace = LeftBrace->MatchingParen;
2443
106
  if (!RightBrace) {
2444
12
    assert(!LeftBrace->Optional);
2445
0
    return;
2446
12
  }
2447
2448
94
  assert(RightBrace->is(tok::r_brace));
2449
0
  assert(RightBrace->MatchingParen == LeftBrace);
2450
0
  assert(LeftBrace->Optional == RightBrace->Optional);
2451
2452
0
  LeftBrace->Optional = true;
2453
94
  RightBrace->Optional = true;
2454
94
}
2455
2456
9.85k
void UnwrappedLineParser::handleAttributes() {
2457
  // Handle AttributeMacro, e.g. `if (x) UNLIKELY`.
2458
9.85k
  if (FormatTok->is(TT_AttributeMacro))
2459
54
    nextToken();
2460
9.85k
  handleCppAttributes();
2461
9.85k
}
2462
2463
11.5k
bool UnwrappedLineParser::handleCppAttributes() {
2464
  // Handle [[likely]] / [[unlikely]] attributes.
2465
11.5k
  if (FormatTok->is(tok::l_square) && 
tryToParseSimpleAttribute()1.81k
) {
2466
244
    parseSquare();
2467
244
    return true;
2468
244
  }
2469
11.2k
  return false;
2470
11.5k
}
2471
2472
FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
2473
6.87k
                                                  bool KeepBraces) {
2474
6.87k
  assert(FormatTok->is(tok::kw_if) && "'if' expected");
2475
0
  nextToken();
2476
6.87k
  if (FormatTok->is(tok::exclaim))
2477
78
    nextToken();
2478
2479
6.87k
  bool KeepIfBraces = true;
2480
6.87k
  if (FormatTok->is(tok::kw_consteval)) {
2481
144
    nextToken();
2482
6.72k
  } else {
2483
6.72k
    if (Style.RemoveBracesLLVM)
2484
672
      KeepIfBraces = KeepBraces;
2485
6.72k
    if (FormatTok->isOneOf(tok::kw_constexpr, tok::identifier))
2486
666
      nextToken();
2487
6.72k
    if (FormatTok->is(tok::l_paren))
2488
6.66k
      parseParens();
2489
6.72k
  }
2490
6.87k
  handleAttributes();
2491
2492
6.87k
  bool NeedsUnwrappedLine = false;
2493
6.87k
  keepAncestorBraces();
2494
2495
6.87k
  FormatToken *IfLeftBrace = nullptr;
2496
6.87k
  IfStmtKind IfBlockKind = IfStmtKind::NotIf;
2497
2498
6.87k
  if (FormatTok->is(tok::l_brace)) {
2499
3.70k
    FormatTok->setFinalizedType(TT_ControlStatementLBrace);
2500
3.70k
    IfLeftBrace = FormatTok;
2501
3.70k
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2502
3.70k
    IfBlockKind = parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2503
3.70k
                             /*MunchSemi=*/true, KeepIfBraces);
2504
3.70k
    if (Style.BraceWrapping.BeforeElse)
2505
217
      addUnwrappedLine();
2506
3.48k
    else
2507
3.48k
      NeedsUnwrappedLine = true;
2508
3.70k
  } else {
2509
3.16k
    parseUnbracedBody();
2510
3.16k
  }
2511
2512
6.87k
  if (Style.RemoveBracesLLVM) {
2513
708
    assert(!NestedTooDeep.empty());
2514
708
    KeepIfBraces = KeepIfBraces ||
2515
708
                   
(612
IfLeftBrace612
&&
!IfLeftBrace->MatchingParen368
) ||
2516
708
                   
NestedTooDeep.back()432
||
IfBlockKind == IfStmtKind::IfOnly406
||
2517
708
                   
IfBlockKind == IfStmtKind::IfElseIf382
;
2518
708
  }
2519
2520
0
  bool KeepElseBraces = KeepIfBraces;
2521
6.87k
  FormatToken *ElseLeftBrace = nullptr;
2522
6.87k
  IfStmtKind Kind = IfStmtKind::IfOnly;
2523
2524
6.87k
  if (FormatTok->is(tok::kw_else)) {
2525
2.98k
    if (Style.RemoveBracesLLVM) {
2526
348
      NestedTooDeep.back() = false;
2527
348
      Kind = IfStmtKind::IfElse;
2528
348
    }
2529
2.98k
    nextToken();
2530
2.98k
    handleAttributes();
2531
2.98k
    if (FormatTok->is(tok::l_brace)) {
2532
1.08k
      FormatTok->setFinalizedType(TT_ElseLBrace);
2533
1.08k
      ElseLeftBrace = FormatTok;
2534
1.08k
      CompoundStatementIndenter Indenter(this, Style, Line->Level);
2535
1.08k
      if (parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2536
1.08k
                     /*MunchSemi=*/true, KeepElseBraces) == IfStmtKind::IfOnly)
2537
2
        Kind = IfStmtKind::IfElseIf;
2538
1.08k
      addUnwrappedLine();
2539
1.90k
    } else if (FormatTok->is(tok::kw_if)) {
2540
1.11k
      const FormatToken *Previous = Tokens->getPreviousToken();
2541
1.11k
      assert(Previous);
2542
0
      const bool IsPrecededByComment = Previous->is(tok::comment);
2543
1.11k
      if (IsPrecededByComment) {
2544
9
        addUnwrappedLine();
2545
9
        ++Line->Level;
2546
9
      }
2547
1.11k
      bool TooDeep = true;
2548
1.11k
      if (Style.RemoveBracesLLVM) {
2549
152
        Kind = IfStmtKind::IfElseIf;
2550
152
        TooDeep = NestedTooDeep.pop_back_val();
2551
152
      }
2552
1.11k
      ElseLeftBrace = parseIfThenElse(/*IfKind=*/nullptr, KeepIfBraces);
2553
1.11k
      if (Style.RemoveBracesLLVM)
2554
152
        NestedTooDeep.push_back(TooDeep);
2555
1.11k
      if (IsPrecededByComment)
2556
9
        --Line->Level;
2557
1.11k
    } else {
2558
786
      parseUnbracedBody(/*CheckEOF=*/true);
2559
786
    }
2560
3.88k
  } else {
2561
3.88k
    if (Style.RemoveBracesLLVM)
2562
360
      KeepIfBraces = KeepIfBraces || 
IfBlockKind == IfStmtKind::IfElse144
;
2563
3.88k
    if (NeedsUnwrappedLine)
2564
2.10k
      addUnwrappedLine();
2565
3.88k
  }
2566
2567
6.87k
  if (!Style.RemoveBracesLLVM)
2568
6.16k
    return nullptr;
2569
2570
708
  assert(!NestedTooDeep.empty());
2571
708
  KeepElseBraces = KeepElseBraces ||
2572
708
                   
(372
ElseLeftBrace372
&&
!ElseLeftBrace->MatchingParen98
) ||
2573
708
                   
NestedTooDeep.back()300
;
2574
2575
708
  NestedTooDeep.pop_back();
2576
2577
708
  if (!KeepIfBraces && 
!KeepElseBraces360
) {
2578
288
    markOptionalBraces(IfLeftBrace);
2579
288
    markOptionalBraces(ElseLeftBrace);
2580
420
  } else if (IfLeftBrace) {
2581
420
    FormatToken *IfRightBrace = IfLeftBrace->MatchingParen;
2582
420
    if (IfRightBrace) {
2583
144
      assert(IfRightBrace->MatchingParen == IfLeftBrace);
2584
0
      assert(!IfLeftBrace->Optional);
2585
0
      assert(!IfRightBrace->Optional);
2586
0
      IfLeftBrace->MatchingParen = nullptr;
2587
144
      IfRightBrace->MatchingParen = nullptr;
2588
144
    }
2589
420
  }
2590
2591
708
  if (IfKind)
2592
80
    *IfKind = Kind;
2593
2594
708
  return IfLeftBrace;
2595
6.87k
}
2596
2597
355
void UnwrappedLineParser::parseTryCatch() {
2598
355
  assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
2599
0
  nextToken();
2600
355
  bool NeedsUnwrappedLine = false;
2601
355
  if (FormatTok->is(tok::colon)) {
2602
    // We are in a function try block, what comes is an initializer list.
2603
44
    nextToken();
2604
2605
    // In case identifiers were removed by clang-tidy, what might follow is
2606
    // multiple commas in sequence - before the first identifier.
2607
50
    while (FormatTok->is(tok::comma))
2608
6
      nextToken();
2609
2610
130
    while (FormatTok->is(tok::identifier)) {
2611
86
      nextToken();
2612
86
      if (FormatTok->is(tok::l_paren))
2613
41
        parseParens();
2614
86
      if (FormatTok->Previous && FormatTok->Previous->is(tok::identifier) &&
2615
86
          
FormatTok->is(tok::l_brace)45
) {
2616
90
        do {
2617
90
          nextToken();
2618
90
        } while (!FormatTok->is(tok::r_brace));
2619
45
        nextToken();
2620
45
      }
2621
2622
      // In case identifiers were removed by clang-tidy, what might follow is
2623
      // multiple commas in sequence - after the first identifier.
2624
135
      while (FormatTok->is(tok::comma))
2625
49
        nextToken();
2626
86
    }
2627
44
  }
2628
  // Parse try with resource.
2629
355
  if (Style.Language == FormatStyle::LK_Java && 
FormatTok->is(tok::l_paren)12
)
2630
4
    parseParens();
2631
2632
355
  keepAncestorBraces();
2633
2634
355
  if (FormatTok->is(tok::l_brace)) {
2635
349
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2636
349
    parseBlock();
2637
349
    if (Style.BraceWrapping.BeforeCatch)
2638
118
      addUnwrappedLine();
2639
231
    else
2640
231
      NeedsUnwrappedLine = true;
2641
349
  } else 
if (6
!FormatTok->is(tok::kw_catch)6
) {
2642
    // The C++ standard requires a compound-statement after a try.
2643
    // If there's none, we try to assume there's a structuralElement
2644
    // and try to continue.
2645
6
    addUnwrappedLine();
2646
6
    ++Line->Level;
2647
6
    parseStructuralElement();
2648
6
    --Line->Level;
2649
6
  }
2650
719
  while (true) {
2651
719
    if (FormatTok->is(tok::at))
2652
28
      nextToken();
2653
719
    if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
2654
719
                             tok::kw___finally) ||
2655
719
          
(382
(382
Style.Language == FormatStyle::LK_Java382
||
Style.isJavaScript()366
) &&
2656
382
           
FormatTok->is(Keywords.kw_finally)26
) ||
2657
719
          
(374
FormatTok->isObjCAtKeyword(tok::objc_catch)374
||
2658
374
           FormatTok->isObjCAtKeyword(tok::objc_finally))))
2659
352
      break;
2660
367
    nextToken();
2661
704
    while (FormatTok->isNot(tok::l_brace)) {
2662
340
      if (FormatTok->is(tok::l_paren)) {
2663
319
        parseParens();
2664
319
        continue;
2665
319
      }
2666
21
      if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) {
2667
3
        if (Style.RemoveBracesLLVM)
2668
0
          NestedTooDeep.pop_back();
2669
3
        return;
2670
3
      }
2671
18
      nextToken();
2672
18
    }
2673
364
    NeedsUnwrappedLine = false;
2674
364
    Line->MustBeDeclaration = false;
2675
364
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2676
364
    parseBlock();
2677
364
    if (Style.BraceWrapping.BeforeCatch)
2678
118
      addUnwrappedLine();
2679
246
    else
2680
246
      NeedsUnwrappedLine = true;
2681
364
  }
2682
2683
352
  if (Style.RemoveBracesLLVM)
2684
0
    NestedTooDeep.pop_back();
2685
2686
352
  if (NeedsUnwrappedLine)
2687
228
    addUnwrappedLine();
2688
352
}
2689
2690
4.23k
void UnwrappedLineParser::parseNamespace() {
2691
4.23k
  assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
2692
4.23k
         "'namespace' expected");
2693
2694
0
  const FormatToken &InitialToken = *FormatTok;
2695
4.23k
  nextToken();
2696
4.23k
  if (InitialToken.is(TT_NamespaceMacro)) {
2697
160
    parseParens();
2698
4.07k
  } else {
2699
8.19k
    while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
2700
8.19k
                              tok::l_square, tok::period, tok::l_paren) ||
2701
8.19k
           
(4.07k
Style.isCSharp()4.07k
&&
FormatTok->is(tok::kw_union)28
))
2702
4.11k
      if (FormatTok->is(tok::l_square))
2703
16
        parseSquare();
2704
4.10k
      else if (FormatTok->is(tok::l_paren))
2705
41
        parseParens();
2706
4.06k
      else
2707
4.06k
        nextToken();
2708
4.07k
  }
2709
4.23k
  if (FormatTok->is(tok::l_brace)) {
2710
4.22k
    if (ShouldBreakBeforeBrace(Style, InitialToken))
2711
220
      addUnwrappedLine();
2712
2713
4.22k
    unsigned AddLevels =
2714
4.22k
        Style.NamespaceIndentation == FormatStyle::NI_All ||
2715
4.22k
                
(3.99k
Style.NamespaceIndentation == FormatStyle::NI_Inner3.99k
&&
2716
3.99k
                 
DeclarationScopeStack.size() > 1139
)
2717
4.22k
            ? 
1u296
2718
4.22k
            : 
0u3.92k
;
2719
4.22k
    bool ManageWhitesmithsBraces =
2720
4.22k
        AddLevels == 0u &&
2721
4.22k
        
Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths3.92k
;
2722
2723
    // If we're in Whitesmiths mode, indent the brace if we're not indenting
2724
    // the whole block.
2725
4.22k
    if (ManageWhitesmithsBraces)
2726
36
      ++Line->Level;
2727
2728
4.22k
    parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/true,
2729
4.22k
               /*KeepBraces=*/true, ManageWhitesmithsBraces);
2730
2731
    // Munch the semicolon after a namespace. This is more common than one would
2732
    // think. Putting the semicolon into its own line is very ugly.
2733
4.22k
    if (FormatTok->is(tok::semi))
2734
1
      nextToken();
2735
2736
4.22k
    addUnwrappedLine(AddLevels > 0 ? 
LineLevel::Remove296
:
LineLevel::Keep3.92k
);
2737
2738
4.22k
    if (ManageWhitesmithsBraces)
2739
36
      --Line->Level;
2740
4.22k
  }
2741
  // FIXME: Add error handling.
2742
4.23k
}
2743
2744
509
void UnwrappedLineParser::parseNew() {
2745
509
  assert(FormatTok->is(tok::kw_new) && "'new' expected");
2746
0
  nextToken();
2747
2748
509
  if (Style.isCSharp()) {
2749
128
    do {
2750
128
      if (FormatTok->is(tok::l_brace))
2751
18
        parseBracedList();
2752
2753
128
      if (FormatTok->isOneOf(tok::semi, tok::comma))
2754
20
        return;
2755
2756
108
      nextToken();
2757
108
    } while (!eof());
2758
32
  }
2759
2760
489
  if (Style.Language != FormatStyle::LK_Java)
2761
479
    return;
2762
2763
  // In Java, we can parse everything up to the parens, which aren't optional.
2764
32
  
do 10
{
2765
    // There should not be a ;, { or } before the new's open paren.
2766
32
    if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
2767
6
      return;
2768
2769
    // Consume the parens.
2770
26
    if (FormatTok->is(tok::l_paren)) {
2771
4
      parseParens();
2772
2773
      // If there is a class body of an anonymous class, consume that as child.
2774
4
      if (FormatTok->is(tok::l_brace))
2775
4
        parseChildBlock();
2776
4
      return;
2777
4
    }
2778
22
    nextToken();
2779
22
  } while (!eof());
2780
10
}
2781
2782
2.83k
void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) {
2783
2.83k
  keepAncestorBraces();
2784
2785
2.83k
  if (FormatTok->is(tok::l_brace)) {
2786
1.60k
    if (!KeepBraces)
2787
36
      FormatTok->setFinalizedType(TT_ControlStatementLBrace);
2788
1.60k
    FormatToken *LeftBrace = FormatTok;
2789
1.60k
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2790
1.60k
    parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2791
1.60k
               /*MunchSemi=*/true, KeepBraces);
2792
1.60k
    if (!KeepBraces) {
2793
36
      assert(!NestedTooDeep.empty());
2794
36
      if (!NestedTooDeep.back())
2795
36
        markOptionalBraces(LeftBrace);
2796
36
    }
2797
1.60k
    if (WrapRightBrace)
2798
1.40k
      addUnwrappedLine();
2799
1.60k
  } else {
2800
1.23k
    parseUnbracedBody();
2801
1.23k
  }
2802
2803
2.83k
  if (!KeepBraces)
2804
96
    NestedTooDeep.pop_back();
2805
2.83k
}
2806
2807
2.56k
void UnwrappedLineParser::parseForOrWhileLoop() {
2808
2.56k
  assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
2809
2.56k
         "'for', 'while' or foreach macro expected");
2810
2.56k
  const bool KeepBraces = !Style.RemoveBracesLLVM ||
2811
2.56k
                          
!FormatTok->isOneOf(tok::kw_for, tok::kw_while)96
;
2812
2813
2.56k
  nextToken();
2814
  // JS' for await ( ...
2815
2.56k
  if (Style.isJavaScript() && 
FormatTok->is(Keywords.kw_await)48
)
2816
8
    nextToken();
2817
2.56k
  if (Style.isCpp() && 
FormatTok->is(tok::kw_co_await)2.49k
)
2818
27
    nextToken();
2819
2.56k
  if (FormatTok->is(tok::l_paren))
2820
2.54k
    parseParens();
2821
2822
2.56k
  parseLoopBody(KeepBraces, /*WrapRightBrace=*/true);
2823
2.56k
}
2824
2825
274
void UnwrappedLineParser::parseDoWhile() {
2826
274
  assert(FormatTok->is(tok::kw_do) && "'do' expected");
2827
0
  nextToken();
2828
2829
274
  parseLoopBody(/*KeepBraces=*/true, Style.BraceWrapping.BeforeWhile);
2830
2831
  // FIXME: Add error handling.
2832
274
  if (!FormatTok->is(tok::kw_while)) {
2833
37
    addUnwrappedLine();
2834
37
    return;
2835
37
  }
2836
2837
  // If in Whitesmiths mode, the line with the while() needs to be indented
2838
  // to the same level as the block.
2839
237
  if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)
2840
9
    ++Line->Level;
2841
2842
237
  nextToken();
2843
237
  parseStructuralElement();
2844
237
}
2845
2846
1.73k
void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) {
2847
1.73k
  nextToken();
2848
1.73k
  unsigned OldLineLevel = Line->Level;
2849
1.73k
  if (Line->Level > 1 || 
(1.21k
!Line->InPPDirective1.21k
&&
Line->Level > 01.21k
))
2850
1.70k
    --Line->Level;
2851
1.73k
  if (LeftAlignLabel)
2852
27
    Line->Level = 0;
2853
2854
1.73k
  if (!Style.IndentCaseBlocks && 
CommentsBeforeNextToken.empty()1.72k
&&
2855
1.73k
      
FormatTok->is(tok::l_brace)1.68k
) {
2856
2857
438
    CompoundStatementIndenter Indenter(this, Line->Level,
2858
438
                                       Style.BraceWrapping.AfterCaseLabel,
2859
438
                                       Style.BraceWrapping.IndentBraces);
2860
438
    parseBlock();
2861
438
    if (FormatTok->is(tok::kw_break)) {
2862
99
      if (Style.BraceWrapping.AfterControlStatement ==
2863
99
          FormatStyle::BWACS_Always) {
2864
90
        addUnwrappedLine();
2865
90
        if (!Style.IndentCaseBlocks &&
2866
90
            Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)
2867
72
          ++Line->Level;
2868
90
      }
2869
99
      parseStructuralElement();
2870
99
    }
2871
438
    addUnwrappedLine();
2872
1.30k
  } else {
2873
1.30k
    if (FormatTok->is(tok::semi))
2874
36
      nextToken();
2875
1.30k
    addUnwrappedLine();
2876
1.30k
  }
2877
1.73k
  Line->Level = OldLineLevel;
2878
1.73k
  if (FormatTok->isNot(tok::l_brace)) {
2879
1.71k
    parseStructuralElement();
2880
1.71k
    addUnwrappedLine();
2881
1.71k
  }
2882
1.73k
}
2883
2884
1.10k
void UnwrappedLineParser::parseCaseLabel() {
2885
1.10k
  assert(FormatTok->is(tok::kw_case) && "'case' expected");
2886
2887
  // FIXME: fix handling of complex expressions here.
2888
2.38k
  do {
2889
2.38k
    nextToken();
2890
2.38k
  } while (!eof() && 
!FormatTok->is(tok::colon)2.37k
);
2891
1.10k
  parseLabel();
2892
1.10k
}
2893
2894
686
void UnwrappedLineParser::parseSwitch() {
2895
686
  assert(FormatTok->is(tok::kw_switch) && "'switch' expected");
2896
0
  nextToken();
2897
686
  if (FormatTok->is(tok::l_paren))
2898
677
    parseParens();
2899
2900
686
  keepAncestorBraces();
2901
2902
686
  if (FormatTok->is(tok::l_brace)) {
2903
659
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2904
659
    parseBlock();
2905
659
    addUnwrappedLine();
2906
659
  } else {
2907
27
    addUnwrappedLine();
2908
27
    ++Line->Level;
2909
27
    parseStructuralElement();
2910
27
    --Line->Level;
2911
27
  }
2912
2913
686
  if (Style.RemoveBracesLLVM)
2914
0
    NestedTooDeep.pop_back();
2915
686
}
2916
2917
// Operators that can follow a C variable.
2918
399
static bool isCOperatorFollowingVar(tok::TokenKind kind) {
2919
399
  switch (kind) {
2920
0
  case tok::ampamp:
2921
9
  case tok::ampequal:
2922
18
  case tok::arrow:
2923
27
  case tok::caret:
2924
36
  case tok::caretequal:
2925
36
  case tok::comma:
2926
36
  case tok::ellipsis:
2927
54
  case tok::equal:
2928
54
  case tok::equalequal:
2929
54
  case tok::exclaim:
2930
63
  case tok::exclaimequal:
2931
72
  case tok::greater:
2932
72
  case tok::greaterequal:
2933
72
  case tok::greatergreater:
2934
81
  case tok::greatergreaterequal:
2935
90
  case tok::l_paren:
2936
99
  case tok::l_square:
2937
99
  case tok::less:
2938
99
  case tok::lessequal:
2939
108
  case tok::lessless:
2940
117
  case tok::lesslessequal:
2941
117
  case tok::minus:
2942
126
  case tok::minusequal:
2943
135
  case tok::minusminus:
2944
135
  case tok::percent:
2945
144
  case tok::percentequal:
2946
153
  case tok::period:
2947
162
  case tok::pipe:
2948
171
  case tok::pipeequal:
2949
171
  case tok::pipepipe:
2950
171
  case tok::plus:
2951
180
  case tok::plusequal:
2952
189
  case tok::plusplus:
2953
189
  case tok::question:
2954
189
  case tok::r_brace:
2955
189
  case tok::r_paren:
2956
189
  case tok::r_square:
2957
189
  case tok::semi:
2958
189
  case tok::slash:
2959
198
  case tok::slashequal:
2960
198
  case tok::star:
2961
207
  case tok::starequal:
2962
207
    return true;
2963
192
  default:
2964
192
    return false;
2965
399
  }
2966
399
}
2967
2968
2.39k
void UnwrappedLineParser::parseAccessSpecifier() {
2969
2.39k
  FormatToken *AccessSpecifierCandidate = FormatTok;
2970
2.39k
  nextToken();
2971
  // Understand Qt's slots.
2972
2.39k
  if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
2973
54
    nextToken();
2974
  // Otherwise, we don't know what it is, and we'd better keep the next token.
2975
2.39k
  if (FormatTok->is(tok::colon)) {
2976
1.98k
    nextToken();
2977
1.98k
    addUnwrappedLine();
2978
1.98k
  } else 
if (408
!FormatTok->is(tok::coloncolon)408
&&
2979
408
             
!isCOperatorFollowingVar(FormatTok->Tok.getKind())399
) {
2980
    // Not a variable name nor namespace name.
2981
192
    addUnwrappedLine();
2982
216
  } else if (AccessSpecifierCandidate) {
2983
    // Consider the access specifier to be a C identifier.
2984
216
    AccessSpecifierCandidate->Tok.setKind(tok::identifier);
2985
216
  }
2986
2.39k
}
2987
2988
/// \brief Parses a concept definition.
2989
/// \pre The current token has to be the concept keyword.
2990
///
2991
/// Returns if either the concept has been completely parsed, or if it detects
2992
/// that the concept definition is incorrect.
2993
621
void UnwrappedLineParser::parseConcept() {
2994
621
  assert(FormatTok->is(tok::kw_concept) && "'concept' expected");
2995
0
  nextToken();
2996
621
  if (!FormatTok->is(tok::identifier))
2997
4
    return;
2998
617
  nextToken();
2999
617
  if (!FormatTok->is(tok::equal))
3000
0
    return;
3001
617
  nextToken();
3002
617
  parseConstraintExpression();
3003
617
  if (FormatTok->is(tok::semi))
3004
599
    nextToken();
3005
617
  addUnwrappedLine();
3006
617
}
3007
3008
/// \brief Parses a requires, decides if it is a clause or an expression.
3009
/// \pre The current token has to be the requires keyword.
3010
/// \returns true if it parsed a clause.
3011
1.00k
bool clang::format::UnwrappedLineParser::parseRequires() {
3012
1.00k
  assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3013
0
  auto RequiresToken = FormatTok;
3014
3015
  // We try to guess if it is a requires clause, or a requires expression. For
3016
  // that we first consume the keyword and check the next token.
3017
1.00k
  nextToken();
3018
3019
1.00k
  switch (FormatTok->Tok.getKind()) {
3020
0
  case tok::l_brace:
3021
    // This can only be an expression, never a clause.
3022
0
    parseRequiresExpression(RequiresToken);
3023
0
    return false;
3024
187
  case tok::l_paren:
3025
    // Clauses and expression can start with a paren, it's unclear what we have.
3026
187
    break;
3027
820
  default:
3028
    // All other tokens can only be a clause.
3029
820
    parseRequiresClause(RequiresToken);
3030
820
    return true;
3031
1.00k
  }
3032
3033
  // Looking forward we would have to decide if there are function declaration
3034
  // like arguments to the requires expression:
3035
  // requires (T t) {
3036
  // Or there is a constraint expression for the requires clause:
3037
  // requires (C<T> && ...
3038
3039
  // But first let's look behind.
3040
187
  auto *PreviousNonComment = RequiresToken->getPreviousNonComment();
3041
3042
187
  if (!PreviousNonComment ||
3043
187
      
PreviousNonComment->is(TT_RequiresExpressionLBrace)151
) {
3044
    // If there is no token, or an expression left brace, we are a requires
3045
    // clause within a requires expression.
3046
36
    parseRequiresClause(RequiresToken);
3047
36
    return true;
3048
36
  }
3049
3050
151
  switch (PreviousNonComment->Tok.getKind()) {
3051
64
  case tok::greater:
3052
109
  case tok::r_paren:
3053
109
  case tok::kw_noexcept:
3054
109
  case tok::kw_const:
3055
    // This is a requires clause.
3056
109
    parseRequiresClause(RequiresToken);
3057
109
    return true;
3058
0
  case tok::amp:
3059
4
  case tok::ampamp: {
3060
    // This can be either:
3061
    // if (... && requires (T t) ...)
3062
    // Or
3063
    // void member(...) && requires (C<T> ...
3064
    // We check the one token before that for a const:
3065
    // void member(...) const && requires (C<T> ...
3066
4
    auto PrevPrev = PreviousNonComment->getPreviousNonComment();
3067
4
    if (PrevPrev && PrevPrev->is(tok::kw_const)) {
3068
0
      parseRequiresClause(RequiresToken);
3069
0
      return true;
3070
0
    }
3071
4
    break;
3072
4
  }
3073
38
  default:
3074
    // It's an expression.
3075
38
    parseRequiresExpression(RequiresToken);
3076
38
    return false;
3077
151
  }
3078
3079
  // Now we look forward and try to check if the paren content is a parameter
3080
  // list. The parameters can be cv-qualified and contain references or
3081
  // pointers.
3082
  // So we want basically to check for TYPE NAME, but TYPE can contain all kinds
3083
  // of stuff: typename, const, *, &, &&, ::, identifiers.
3084
3085
4
  int NextTokenOffset = 1;
3086
4
  auto NextToken = Tokens->peekNextToken(NextTokenOffset);
3087
22
  auto PeekNext = [&NextTokenOffset, &NextToken, this] {
3088
22
    ++NextTokenOffset;
3089
22
    NextToken = Tokens->peekNextToken(NextTokenOffset);
3090
22
  };
3091
3092
4
  bool FoundType = false;
3093
4
  bool LastWasColonColon = false;
3094
4
  int OpenAngles = 0;
3095
3096
26
  for (; NextTokenOffset < 50; 
PeekNext()22
) {
3097
26
    switch (NextToken->Tok.getKind()) {
3098
0
    case tok::kw_volatile:
3099
0
    case tok::kw_const:
3100
0
    case tok::comma:
3101
0
      parseRequiresExpression(RequiresToken);
3102
0
      return false;
3103
4
    case tok::r_paren:
3104
4
    case tok::pipepipe:
3105
4
      parseRequiresClause(RequiresToken);
3106
4
      return true;
3107
0
    case tok::eof:
3108
      // Break out of the loop.
3109
0
      NextTokenOffset = 50;
3110
0
      break;
3111
4
    case tok::coloncolon:
3112
4
      LastWasColonColon = true;
3113
4
      break;
3114
11
    case tok::identifier:
3115
11
      if (FoundType && 
!LastWasColonColon7
&&
OpenAngles == 03
) {
3116
0
        parseRequiresExpression(RequiresToken);
3117
0
        return false;
3118
0
      }
3119
11
      FoundType = true;
3120
11
      LastWasColonColon = false;
3121
11
      break;
3122
3
    case tok::less:
3123
3
      ++OpenAngles;
3124
3
      break;
3125
3
    case tok::greater:
3126
3
      --OpenAngles;
3127
3
      break;
3128
1
    default:
3129
1
      if (NextToken->isSimpleTypeSpecifier()) {
3130
0
        parseRequiresExpression(RequiresToken);
3131
0
        return false;
3132
0
      }
3133
1
      break;
3134
26
    }
3135
26
  }
3136
3137
  // This seems to be a complicated expression, just assume it's a clause.
3138
0
  parseRequiresClause(RequiresToken);
3139
0
  return true;
3140
4
}
3141
3142
/// \brief Parses a requires clause.
3143
/// \param RequiresToken The requires keyword token, which starts this clause.
3144
/// \pre We need to be on the next token after the requires keyword.
3145
/// \sa parseRequiresExpression
3146
///
3147
/// Returns if it either has finished parsing the clause, or it detects, that
3148
/// the clause is incorrect.
3149
969
void UnwrappedLineParser::parseRequiresClause(FormatToken *RequiresToken) {
3150
969
  assert(FormatTok->getPreviousNonComment() == RequiresToken);
3151
0
  assert(RequiresToken->is(tok::kw_requires) && "'requires' expected");
3152
3153
  // If there is no previous token, we are within a requires expression,
3154
  // otherwise we will always have the template or function declaration in front
3155
  // of it.
3156
0
  bool InRequiresExpression =
3157
969
      !RequiresToken->Previous ||
3158
969
      
RequiresToken->Previous->is(TT_RequiresExpressionLBrace)824
;
3159
3160
969
  RequiresToken->setFinalizedType(InRequiresExpression
3161
969
                                      ? 
TT_RequiresClauseInARequiresExpression219
3162
969
                                      : 
TT_RequiresClause750
);
3163
3164
969
  parseConstraintExpression();
3165
3166
969
  if (!InRequiresExpression)
3167
750
    FormatTok->Previous->ClosesRequiresClause = true;
3168
969
}
3169
3170
/// \brief Parses a requires expression.
3171
/// \param RequiresToken The requires keyword token, which starts this clause.
3172
/// \pre We need to be on the next token after the requires keyword.
3173
/// \sa parseRequiresClause
3174
///
3175
/// Returns if it either has finished parsing the expression, or it detects,
3176
/// that the expression is incorrect.
3177
628
void UnwrappedLineParser::parseRequiresExpression(FormatToken *RequiresToken) {
3178
628
  assert(FormatTok->getPreviousNonComment() == RequiresToken);
3179
0
  assert(RequiresToken->is(tok::kw_requires) && "'requires' expected");
3180
3181
0
  RequiresToken->setFinalizedType(TT_RequiresExpression);
3182
3183
628
  if (FormatTok->is(tok::l_paren)) {
3184
581
    FormatTok->setFinalizedType(TT_RequiresExpressionLParen);
3185
581
    parseParens();
3186
581
  }
3187
3188
628
  if (FormatTok->is(tok::l_brace)) {
3189
601
    FormatTok->setFinalizedType(TT_RequiresExpressionLBrace);
3190
601
    parseChildBlock(/*CanContainBracedList=*/false,
3191
601
                    /*NextLBracesType=*/TT_CompoundRequirementLBrace);
3192
601
  }
3193
628
}
3194
3195
/// \brief Parses a constraint expression.
3196
///
3197
/// This is either the definition of a concept, or the body of a requires
3198
/// clause. It returns, when the parsing is complete, or the expression is
3199
/// incorrect.
3200
1.58k
void UnwrappedLineParser::parseConstraintExpression() {
3201
  // The special handling for lambdas is needed since tryToParseLambda() eats a
3202
  // token and if a requires expression is the last part of a requires clause
3203
  // and followed by an attribute like [[nodiscard]] the ClosesRequiresClause is
3204
  // not set on the correct token. Thus we need to be aware if we even expect a
3205
  // lambda to be possible.
3206
  // template <typename T> requires requires { ... } [[nodiscard]] ...;
3207
1.58k
  bool LambdaNextTimeAllowed = true;
3208
5.85k
  do {
3209
5.85k
    bool LambdaThisTimeAllowed = std::exchange(LambdaNextTimeAllowed, false);
3210
3211
5.85k
    switch (FormatTok->Tok.getKind()) {
3212
492
    case tok::kw_requires: {
3213
492
      auto RequiresToken = FormatTok;
3214
492
      nextToken();
3215
492
      parseRequiresExpression(RequiresToken);
3216
492
      break;
3217
0
    }
3218
3219
521
    case tok::l_paren:
3220
521
      parseParens(/*AmpAmpTokenType=*/TT_BinaryOperator);
3221
521
      break;
3222
3223
47
    case tok::l_square:
3224
47
      if (!LambdaThisTimeAllowed || 
!tryToParseLambda()36
)
3225
11
        return;
3226
36
      break;
3227
3228
36
    case tok::kw_const:
3229
861
    case tok::semi:
3230
915
    case tok::kw_class:
3231
1.03k
    case tok::kw_struct:
3232
1.03k
    case tok::kw_union:
3233
1.03k
      return;
3234
3235
204
    case tok::l_brace:
3236
      // Potential function body.
3237
204
      return;
3238
3239
724
    case tok::ampamp:
3240
805
    case tok::pipepipe:
3241
805
      FormatTok->setFinalizedType(TT_BinaryOperator);
3242
805
      nextToken();
3243
805
      LambdaNextTimeAllowed = true;
3244
805
      break;
3245
3246
0
    case tok::comma:
3247
0
    case tok::comment:
3248
0
      LambdaNextTimeAllowed = LambdaThisTimeAllowed;
3249
0
      nextToken();
3250
0
      break;
3251
3252
189
    case tok::kw_sizeof:
3253
216
    case tok::greater:
3254
243
    case tok::greaterequal:
3255
243
    case tok::greatergreater:
3256
252
    case tok::less:
3257
423
    case tok::lessequal:
3258
423
    case tok::lessless:
3259
432
    case tok::equalequal:
3260
459
    case tok::exclaim:
3261
459
    case tok::exclaimequal:
3262
468
    case tok::plus:
3263
468
    case tok::minus:
3264
468
    case tok::star:
3265
468
    case tok::slash:
3266
513
    case tok::kw_decltype:
3267
513
      LambdaNextTimeAllowed = true;
3268
      // Just eat them.
3269
513
      nextToken();
3270
513
      break;
3271
3272
306
    case tok::numeric_constant:
3273
415
    case tok::coloncolon:
3274
488
    case tok::kw_true:
3275
506
    case tok::kw_false:
3276
      // Just eat them.
3277
506
      nextToken();
3278
506
      break;
3279
3280
9
    case tok::kw_static_cast:
3281
9
    case tok::kw_const_cast:
3282
9
    case tok::kw_reinterpret_cast:
3283
9
    case tok::kw_dynamic_cast:
3284
9
      nextToken();
3285
9
      if (!FormatTok->is(tok::less))
3286
0
        return;
3287
3288
9
      nextToken();
3289
9
      parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
3290
9
                      /*ClosingBraceKind=*/tok::greater);
3291
9
      break;
3292
3293
9
    case tok::kw_bool:
3294
      // bool is only allowed if it is directly followed by a paren for a cast:
3295
      // concept C = bool(...);
3296
      // and bool is the only type, all other types as cast must be inside a
3297
      // cast to bool an thus are handled by the other cases.
3298
9
      nextToken();
3299
9
      if (FormatTok->isNot(tok::l_paren))
3300
0
        return;
3301
9
      parseParens();
3302
9
      break;
3303
3304
1.71k
    default:
3305
1.71k
      if (!FormatTok->Tok.getIdentifierInfo()) {
3306
        // Identifiers are part of the default case, we check for more then
3307
        // tok::identifier to handle builtin type traits.
3308
0
        return;
3309
0
      }
3310
3311
      // We need to differentiate identifiers for a template deduction guide,
3312
      // variables, or function return types (the constraint expression has
3313
      // ended before that), and basically all other cases. But it's easier to
3314
      // check the other way around.
3315
1.71k
      assert(FormatTok->Previous);
3316
0
      switch (FormatTok->Previous->Tok.getKind()) {
3317
109
      case tok::coloncolon:  // Nested identifier.
3318
444
      case tok::ampamp:      // Start of a function or variable for the
3319
453
      case tok::pipepipe:    // constraint expression.
3320
1.22k
      case tok::kw_requires: // Initial identifier of a requires clause.
3321
1.38k
      case tok::equal:       // Initial identifier of a concept declaration.
3322
1.38k
        break;
3323
326
      default:
3324
326
        return;
3325
1.71k
      }
3326
3327
      // Read identifier with optional template declaration.
3328
1.38k
      nextToken();
3329
1.38k
      if (FormatTok->is(tok::less)) {
3330
1.20k
        nextToken();
3331
1.20k
        parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
3332
1.20k
                        /*ClosingBraceKind=*/tok::greater);
3333
1.20k
      }
3334
1.38k
      break;
3335
5.85k
    }
3336
5.85k
  } while (
!eof()4.27k
);
3337
1.58k
}
3338
3339
2.54k
bool UnwrappedLineParser::parseEnum() {
3340
2.54k
  const FormatToken &InitialToken = *FormatTok;
3341
3342
  // Won't be 'enum' for NS_ENUMs.
3343
2.54k
  if (FormatTok->is(tok::kw_enum))
3344
2.46k
    nextToken();
3345
3346
  // In TypeScript, "enum" can also be used as property name, e.g. in interface
3347
  // declarations. An "enum" keyword followed by a colon would be a syntax
3348
  // error and thus assume it is just an identifier.
3349
2.54k
  if (Style.isJavaScript() && 
FormatTok->isOneOf(tok::colon, tok::question)48
)
3350
8
    return false;
3351
3352
  // In protobuf, "enum" can be used as a field name.
3353
2.53k
  if (Style.Language == FormatStyle::LK_Proto && 
FormatTok->is(tok::equal)8
)
3354
2
    return false;
3355
3356
  // Eat up enum class ...
3357
2.53k
  if (FormatTok->isOneOf(tok::kw_class, tok::kw_struct))
3358
388
    nextToken();
3359
3360
4.94k
  while (FormatTok->Tok.getIdentifierInfo() ||
3361
4.94k
         FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
3362
2.60k
                            tok::greater, tok::comma, tok::question)) {
3363
2.45k
    nextToken();
3364
    // We can have macros or attributes in between 'enum' and the enum name.
3365
2.45k
    if (FormatTok->is(tok::l_paren))
3366
128
      parseParens();
3367
2.45k
    if (FormatTok->is(tok::identifier)) {
3368
198
      nextToken();
3369
      // If there are two identifiers in a row, this is likely an elaborate
3370
      // return type. In Java, this can be "implements", etc.
3371
198
      if (Style.isCpp() && 
FormatTok->is(tok::identifier)194
)
3372
36
        return false;
3373
198
    }
3374
2.45k
  }
3375
3376
  // Just a declaration or something is wrong.
3377
2.49k
  if (FormatTok->isNot(tok::l_brace))
3378
46
    return true;
3379
2.44k
  FormatTok->setFinalizedType(TT_EnumLBrace);
3380
2.44k
  FormatTok->setBlockKind(BK_Block);
3381
3382
2.44k
  if (Style.Language == FormatStyle::LK_Java) {
3383
    // Java enums are different.
3384
22
    parseJavaEnumBody();
3385
22
    return true;
3386
22
  }
3387
2.42k
  if (Style.Language == FormatStyle::LK_Proto) {
3388
6
    parseBlock(/*MustBeDeclaration=*/true);
3389
6
    return true;
3390
6
  }
3391
3392
2.42k
  if (!Style.AllowShortEnumsOnASingleLine &&
3393
2.42k
      
ShouldBreakBeforeBrace(Style, InitialToken)107
)
3394
34
    addUnwrappedLine();
3395
  // Parse enum body.
3396
2.42k
  nextToken();
3397
2.42k
  if (!Style.AllowShortEnumsOnASingleLine) {
3398
107
    addUnwrappedLine();
3399
107
    Line->Level += 1;
3400
107
  }
3401
2.42k
  bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true,
3402
2.42k
                                   /*IsEnum=*/true);
3403
2.42k
  if (!Style.AllowShortEnumsOnASingleLine)
3404
107
    Line->Level -= 1;
3405
2.42k
  if (HasError) {
3406
18
    if (FormatTok->is(tok::semi))
3407
9
      nextToken();
3408
18
    addUnwrappedLine();
3409
18
  }
3410
2.42k
  return true;
3411
3412
  // There is no addUnwrappedLine() here so that we fall through to parsing a
3413
  // structural element afterwards. Thus, in "enum A {} n, m;",
3414
  // "} n, m;" will end up in one unwrapped line.
3415
2.42k
}
3416
3417
9.67k
bool UnwrappedLineParser::parseStructLike() {
3418
  // parseRecord falls through and does not yet add an unwrapped line as a
3419
  // record declaration or definition can start a structural element.
3420
9.67k
  parseRecord();
3421
  // This does not apply to Java, JavaScript and C#.
3422
9.67k
  if (Style.Language == FormatStyle::LK_Java || 
Style.isJavaScript()9.64k
||
3423
9.67k
      
Style.isCSharp()9.37k
) {
3424
451
    if (FormatTok->is(tok::semi))
3425
12
      nextToken();
3426
451
    addUnwrappedLine();
3427
451
    return true;
3428
451
  }
3429
9.22k
  return false;
3430
9.67k
}
3431
3432
namespace {
3433
// A class used to set and restore the Token position when peeking
3434
// ahead in the token source.
3435
class ScopedTokenPosition {
3436
  unsigned StoredPosition;
3437
  FormatTokenSource *Tokens;
3438
3439
public:
3440
1.81k
  ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) {
3441
1.81k
    assert(Tokens && "Tokens expected to not be null");
3442
0
    StoredPosition = Tokens->getPosition();
3443
1.81k
  }
3444
3445
1.81k
  ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); }
3446
};
3447
} // namespace
3448
3449
// Look to see if we have [[ by looking ahead, if
3450
// its not then rewind to the original position.
3451
1.81k
bool UnwrappedLineParser::tryToParseSimpleAttribute() {
3452
1.81k
  ScopedTokenPosition AutoPosition(Tokens);
3453
1.81k
  FormatToken *Tok = Tokens->getNextToken();
3454
  // We already read the first [ check for the second.
3455
1.81k
  if (!Tok->is(tok::l_square))
3456
1.39k
    return false;
3457
  // Double check that the attribute is just something
3458
  // fairly simple.
3459
1.78k
  
while (425
Tok->isNot(tok::eof)) {
3460
1.78k
    if (Tok->is(tok::r_square))
3461
425
      break;
3462
1.35k
    Tok = Tokens->getNextToken();
3463
1.35k
  }
3464
425
  if (Tok->is(tok::eof))
3465
0
    return false;
3466
425
  Tok = Tokens->getNextToken();
3467
425
  if (!Tok->is(tok::r_square))
3468
111
    return false;
3469
314
  Tok = Tokens->getNextToken();
3470
314
  if (Tok->is(tok::semi))
3471
70
    return false;
3472
244
  return true;
3473
314
}
3474
3475
22
void UnwrappedLineParser::parseJavaEnumBody() {
3476
22
  assert(FormatTok->is(tok::l_brace));
3477
0
  const FormatToken *OpeningBrace = FormatTok;
3478
3479
  // Determine whether the enum is simple, i.e. does not have a semicolon or
3480
  // constants with class bodies. Simple enums can be formatted like braced
3481
  // lists, contracted to a single line, etc.
3482
22
  unsigned StoredPosition = Tokens->getPosition();
3483
22
  bool IsSimple = true;
3484
22
  FormatToken *Tok = Tokens->getNextToken();
3485
136
  while (!Tok->is(tok::eof)) {
3486
136
    if (Tok->is(tok::r_brace))
3487
8
      break;
3488
128
    if (Tok->isOneOf(tok::l_brace, tok::semi)) {
3489
14
      IsSimple = false;
3490
14
      break;
3491
14
    }
3492
    // FIXME: This will also mark enums with braces in the arguments to enum
3493
    // constants as "not simple". This is probably fine in practice, though.
3494
114
    Tok = Tokens->getNextToken();
3495
114
  }
3496
22
  FormatTok = Tokens->setPosition(StoredPosition);
3497
3498
22
  if (IsSimple) {
3499
8
    nextToken();
3500
8
    parseBracedList();
3501
8
    addUnwrappedLine();
3502
8
    return;
3503
8
  }
3504
3505
  // Parse the body of a more complex enum.
3506
  // First add a line for everything up to the "{".
3507
14
  nextToken();
3508
14
  addUnwrappedLine();
3509
14
  ++Line->Level;
3510
3511
  // Parse the enum constants.
3512
76
  while (FormatTok) {
3513
76
    if (FormatTok->is(tok::l_brace)) {
3514
      // Parse the constant's class body.
3515
8
      parseBlock(/*MustBeDeclaration=*/true, /*AddLevels=*/1u,
3516
8
                 /*MunchSemi=*/false);
3517
68
    } else if (FormatTok->is(tok::l_paren)) {
3518
12
      parseParens();
3519
56
    } else if (FormatTok->is(tok::comma)) {
3520
14
      nextToken();
3521
14
      addUnwrappedLine();
3522
42
    } else if (FormatTok->is(tok::semi)) {
3523
14
      nextToken();
3524
14
      addUnwrappedLine();
3525
14
      break;
3526
28
    } else if (FormatTok->is(tok::r_brace)) {
3527
0
      addUnwrappedLine();
3528
0
      break;
3529
28
    } else {
3530
28
      nextToken();
3531
28
    }
3532
76
  }
3533
3534
  // Parse the class body after the enum's ";" if any.
3535
14
  parseLevel(OpeningBrace, /*CanContainBracedList=*/true);
3536
14
  nextToken();
3537
14
  --Line->Level;
3538
14
  addUnwrappedLine();
3539
14
}
3540
3541
9.72k
void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
3542
9.72k
  const FormatToken &InitialToken = *FormatTok;
3543
9.72k
  nextToken();
3544
3545
  // The actual identifier can be a nested name specifier, and in macros
3546
  // it is often token-pasted.
3547
  // An [[attribute]] can be before the identifier.
3548
20.6k
  while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
3549
20.6k
                            tok::kw___attribute, tok::kw___declspec,
3550
20.6k
                            tok::kw_alignas, tok::l_square, tok::r_square) ||
3551
20.6k
         
(9.73k
(9.73k
Style.Language == FormatStyle::LK_Java9.73k
||
Style.isJavaScript()9.69k
) &&
3552
10.9k
          
FormatTok->isOneOf(tok::period, tok::comma)365
)) {
3553
10.9k
    if (Style.isJavaScript() &&
3554
10.9k
        
FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)376
) {
3555
      // JavaScript/TypeScript supports inline object types in
3556
      // extends/implements positions:
3557
      //     class Foo implements {bar: number} { }
3558
52
      nextToken();
3559
52
      if (FormatTok->is(tok::l_brace)) {
3560
12
        tryToParseBracedList();
3561
12
        continue;
3562
12
      }
3563
52
    }
3564
10.8k
    bool IsNonMacroIdentifier =
3565
10.8k
        FormatTok->is(tok::identifier) &&
3566
10.8k
        
FormatTok->TokenText != FormatTok->TokenText.upper()10.1k
;
3567
10.8k
    nextToken();
3568
    // We can have macros or attributes in between 'class' and the class name.
3569
10.8k
    if (!IsNonMacroIdentifier) {
3570
5.50k
      if (FormatTok->is(tok::l_paren)) {
3571
72
        parseParens();
3572
5.43k
      } else if (FormatTok->is(TT_AttributeSquare)) {
3573
0
        parseSquare();
3574
        // Consume the closing TT_AttributeSquare.
3575
0
        if (FormatTok->Next && FormatTok->is(TT_AttributeSquare))
3576
0
          nextToken();
3577
0
      }
3578
5.50k
    }
3579
10.8k
  }
3580
3581
  // Note that parsing away template declarations here leads to incorrectly
3582
  // accepting function declarations as record declarations.
3583
  // In general, we cannot solve this problem. Consider:
3584
  // class A<int> B() {}
3585
  // which can be a function definition or a class definition when B() is a
3586
  // macro. If we find enough real-world cases where this is a problem, we
3587
  // can parse for the 'template' keyword in the beginning of the statement,
3588
  // and thus rule out the record production in case there is no template
3589
  // (this would still leave us with an ambiguity between template function
3590
  // and class declarations).
3591
9.72k
  if (FormatTok->isOneOf(tok::colon, tok::less)) {
3592
6.72k
    do {
3593
6.72k
      if (FormatTok->is(tok::l_brace)) {
3594
1.00k
        calculateBraceTypes(/*ExpectClassBody=*/true);
3595
1.00k
        if (!tryToParseBracedList())
3596
974
          break;
3597
1.00k
      }
3598
5.74k
      if (FormatTok->is(tok::l_square)) {
3599
54
        FormatToken *Previous = FormatTok->Previous;
3600
54
        if (!Previous ||
3601
54
            !(Previous->is(tok::r_paren) || 
Previous->isTypeOrIdentifier()36
)) {
3602
          // Don't try parsing a lambda if we had a closing parenthesis before,
3603
          // it was probably a pointer to an array: int (*)[].
3604
27
          if (!tryToParseLambda())
3605
0
            break;
3606
27
        } else {
3607
27
          parseSquare();
3608
27
          continue;
3609
27
        }
3610
54
      }
3611
5.72k
      if (FormatTok->is(tok::semi))
3612
18
        return;
3613
5.70k
      if (Style.isCSharp() && 
FormatTok->is(Keywords.kw_where)56
) {
3614
8
        addUnwrappedLine();
3615
8
        nextToken();
3616
8
        parseCSharpGenericTypeConstraint();
3617
8
        break;
3618
8
      }
3619
5.69k
      nextToken();
3620
5.72k
    } while (!eof());
3621
1.02k
  }
3622
3623
9.70k
  auto GetBraceType = [](const FormatToken &RecordTok) {
3624
7.84k
    switch (RecordTok.Tok.getKind()) {
3625
3.85k
    case tok::kw_class:
3626
3.85k
      return TT_ClassLBrace;
3627
3.70k
    case tok::kw_struct:
3628
3.70k
      return TT_StructLBrace;
3629
213
    case tok::kw_union:
3630
213
      return TT_UnionLBrace;
3631
80
    default:
3632
      // Useful for e.g. interface.
3633
80
      return TT_RecordLBrace;
3634
7.84k
    }
3635
7.84k
  };
3636
9.70k
  if (FormatTok->is(tok::l_brace)) {
3637
7.84k
    FormatTok->setFinalizedType(GetBraceType(InitialToken));
3638
7.84k
    if (ParseAsExpr) {
3639
4
      parseChildBlock();
3640
7.84k
    } else {
3641
7.84k
      if (ShouldBreakBeforeBrace(Style, InitialToken))
3642
550
        addUnwrappedLine();
3643
3644
7.84k
      unsigned AddLevels = Style.IndentAccessModifiers ? 
2u54
:
1u7.79k
;
3645
7.84k
      parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false);
3646
7.84k
    }
3647
7.84k
  }
3648
  // There is no addUnwrappedLine() here so that we fall through to parsing a
3649
  // structural element afterwards. Thus, in "class A {} n, m;",
3650
  // "} n, m;" will end up in one unwrapped line.
3651
9.70k
}
3652
3653
366
void UnwrappedLineParser::parseObjCMethod() {
3654
366
  assert(FormatTok->isOneOf(tok::l_paren, tok::identifier) &&
3655
366
         "'(' or identifier expected.");
3656
2.27k
  do {
3657
2.27k
    if (FormatTok->is(tok::semi)) {
3658
234
      nextToken();
3659
234
      addUnwrappedLine();
3660
234
      return;
3661
2.04k
    } else if (FormatTok->is(tok::l_brace)) {
3662
132
      if (Style.BraceWrapping.AfterFunction)
3663
6
        addUnwrappedLine();
3664
132
      parseBlock();
3665
132
      addUnwrappedLine();
3666
132
      return;
3667
1.90k
    } else {
3668
1.90k
      nextToken();
3669
1.90k
    }
3670
2.27k
  } while (
!eof()1.90k
);
3671
366
}
3672
3673
90
void UnwrappedLineParser::parseObjCProtocolList() {
3674
90
  assert(FormatTok->is(tok::less) && "'<' expected.");
3675
384
  do {
3676
384
    nextToken();
3677
    // Early exit in case someone forgot a close angle.
3678
384
    if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
3679
384
        FormatTok->isObjCAtKeyword(tok::objc_end))
3680
0
      return;
3681
384
  } while (!eof() && FormatTok->isNot(tok::greater));
3682
90
  nextToken(); // Skip '>'.
3683
90
}
3684
3685
494
void UnwrappedLineParser::parseObjCUntilAtEnd() {
3686
1.34k
  do {
3687
1.34k
    if (FormatTok->isObjCAtKeyword(tok::objc_end)) {
3688
429
      nextToken();
3689
429
      addUnwrappedLine();
3690
429
      break;
3691
429
    }
3692
918
    if (FormatTok->is(tok::l_brace)) {
3693
6
      parseBlock();
3694
      // In ObjC interfaces, nothing should be following the "}".
3695
6
      addUnwrappedLine();
3696
912
    } else if (FormatTok->is(tok::r_brace)) {
3697
      // Ignore stray "}". parseStructuralElement doesn't consume them.
3698
6
      nextToken();
3699
6
      addUnwrappedLine();
3700
906
    } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
3701
366
      nextToken();
3702
366
      parseObjCMethod();
3703
540
    } else {
3704
540
      parseStructuralElement();
3705
540
    }
3706
918
  } while (!eof());
3707
494
}
3708
3709
437
void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
3710
437
  assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
3711
437
         FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
3712
0
  nextToken();
3713
437
  nextToken(); // interface name
3714
3715
  // @interface can be followed by a lightweight generic
3716
  // specialization list, then either a base class or a category.
3717
437
  if (FormatTok->is(tok::less))
3718
20
    parseObjCLightweightGenerics();
3719
437
  if (FormatTok->is(tok::colon)) {
3720
117
    nextToken();
3721
117
    nextToken(); // base class name
3722
    // The base class can also have lightweight generics applied to it.
3723
117
    if (FormatTok->is(tok::less))
3724
70
      parseObjCLightweightGenerics();
3725
320
  } else if (FormatTok->is(tok::l_paren))
3726
    // Skip category, if present.
3727
121
    parseParens();
3728
3729
437
  if (FormatTok->is(tok::less))
3730
78
    parseObjCProtocolList();
3731
3732
437
  if (FormatTok->is(tok::l_brace)) {
3733
161
    if (Style.BraceWrapping.AfterObjCDeclaration)
3734
27
      addUnwrappedLine();
3735
161
    parseBlock(/*MustBeDeclaration=*/true);
3736
161
  }
3737
3738
  // With instance variables, this puts '}' on its own line.  Without instance
3739
  // variables, this ends the @interface line.
3740
437
  addUnwrappedLine();
3741
3742
437
  parseObjCUntilAtEnd();
3743
437
}
3744
3745
90
void UnwrappedLineParser::parseObjCLightweightGenerics() {
3746
90
  assert(FormatTok->is(tok::less));
3747
  // Unlike protocol lists, generic parameterizations support
3748
  // nested angles:
3749
  //
3750
  // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
3751
  //     NSObject <NSCopying, NSSecureCoding>
3752
  //
3753
  // so we need to count how many open angles we have left.
3754
0
  unsigned NumOpenAngles = 1;
3755
322
  do {
3756
322
    nextToken();
3757
    // Early exit in case someone forgot a close angle.
3758
322
    if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
3759
322
        FormatTok->isObjCAtKeyword(tok::objc_end))
3760
0
      break;
3761
322
    if (FormatTok->is(tok::less))
3762
12
      ++NumOpenAngles;
3763
310
    else if (FormatTok->is(tok::greater)) {
3764
102
      assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
3765
0
      --NumOpenAngles;
3766
102
    }
3767
322
  } while (!eof() && NumOpenAngles != 0);
3768
0
  nextToken(); // Skip '>'.
3769
90
}
3770
3771
// Returns true for the declaration/definition form of @protocol,
3772
// false for the expression form.
3773
75
bool UnwrappedLineParser::parseObjCProtocol() {
3774
75
  assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
3775
0
  nextToken();
3776
3777
75
  if (FormatTok->is(tok::l_paren))
3778
    // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
3779
6
    return false;
3780
3781
  // The definition/declaration form,
3782
  // @protocol Foo
3783
  // - (int)someMethod;
3784
  // @end
3785
3786
69
  nextToken(); // protocol name
3787
3788
69
  if (FormatTok->is(tok::less))
3789
12
    parseObjCProtocolList();
3790
3791
  // Check for protocol declaration.
3792
69
  if (FormatTok->is(tok::semi)) {
3793
12
    nextToken();
3794
12
    addUnwrappedLine();
3795
12
    return true;
3796
12
  }
3797
3798
57
  addUnwrappedLine();
3799
57
  parseObjCUntilAtEnd();
3800
57
  return true;
3801
69
}
3802
3803
554
void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
3804
554
  bool IsImport = FormatTok->is(Keywords.kw_import);
3805
554
  assert(IsImport || FormatTok->is(tok::kw_export));
3806
0
  nextToken();
3807
3808
  // Consume the "default" in "export default class/function".
3809
554
  if (FormatTok->is(tok::kw_default))
3810
42
    nextToken();
3811
3812
  // Consume "async function", "function" and "default function", so that these
3813
  // get parsed as free-standing JS functions, i.e. do not require a trailing
3814
  // semicolon.
3815
554
  if (FormatTok->is(Keywords.kw_async))
3816
4
    nextToken();
3817
554
  if (FormatTok->is(Keywords.kw_function)) {
3818
58
    nextToken();
3819
58
    return;
3820
58
  }
3821
3822
  // For imports, `export *`, `export {...}`, consume the rest of the line up
3823
  // to the terminating `;`. For everything else, just return and continue
3824
  // parsing the structural element, i.e. the declaration or expression for
3825
  // `export default`.
3826
496
  if (!IsImport && 
!FormatTok->isOneOf(tok::l_brace, tok::star)174
&&
3827
496
      
!FormatTok->isStringLiteral()125
)
3828
125
    return;
3829
3830
1.50k
  
while (371
!eof()) {
3831
1.50k
    if (FormatTok->is(tok::semi))
3832
363
      return;
3833
1.13k
    if (Line->Tokens.empty()) {
3834
      // Common issue: Automatic Semicolon Insertion wrapped the line, so the
3835
      // import statement should terminate.
3836
4
      return;
3837
4
    }
3838
1.13k
    if (FormatTok->is(tok::l_brace)) {
3839
330
      FormatTok->setBlockKind(BK_Block);
3840
330
      nextToken();
3841
330
      parseBracedList();
3842
805
    } else {
3843
805
      nextToken();
3844
805
    }
3845
1.13k
  }
3846
371
}
3847
3848
99
void UnwrappedLineParser::parseStatementMacro() {
3849
99
  nextToken();
3850
99
  if (FormatTok->is(tok::l_paren))
3851
90
    parseParens();
3852
99
  if (FormatTok->is(tok::semi))
3853
9
    nextToken();
3854
99
  addUnwrappedLine();
3855
99
}
3856
3857
LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
3858
0
                                                 StringRef Prefix = "") {
3859
0
  llvm::dbgs() << Prefix << "Line(" << Line.Level
3860
0
               << ", FSC=" << Line.FirstStartColumn << ")"
3861
0
               << (Line.InPPDirective ? " MACRO" : "") << ": ";
3862
0
  for (const auto &Node : Line.Tokens) {
3863
0
    llvm::dbgs() << Node.Tok->Tok.getName() << "["
3864
0
                 << "T=" << static_cast<unsigned>(Node.Tok->getType())
3865
0
                 << ", OC=" << Node.Tok->OriginalColumn << "] ";
3866
0
  }
3867
0
  for (const auto &Node : Line.Tokens)
3868
0
    for (const auto &ChildNode : Node.Children)
3869
0
      printDebugInfo(ChildNode, "\nChild: ");
3870
3871
0
  llvm::dbgs() << "\n";
3872
0
}
3873
3874
445k
void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) {
3875
445k
  if (Line->Tokens.empty())
3876
195k
    return;
3877
250k
  LLVM_DEBUG({
3878
250k
    if (CurrentLines == &Lines)
3879
250k
      printDebugInfo(*Line);
3880
250k
  });
3881
3882
  // If this line closes a block when in Whitesmiths mode, remember that
3883
  // information so that the level can be decreased after the line is added.
3884
  // This has to happen after the addition of the line since the line itself
3885
  // needs to be indented.
3886
250k
  bool ClosesWhitesmithsBlock =
3887
250k
      Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
3888
250k
      
Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths36.0k
;
3889
3890
250k
  CurrentLines->push_back(std::move(*Line));
3891
250k
  Line->Tokens.clear();
3892
250k
  Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
3893
250k
  Line->FirstStartColumn = 0;
3894
3895
250k
  if (ClosesWhitesmithsBlock && 
AdjustLevel == LineLevel::Remove702
)
3896
666
    --Line->Level;
3897
250k
  if (CurrentLines == &Lines && 
!PreprocessorDirectives.empty()238k
) {
3898
2.21k
    CurrentLines->append(
3899
2.21k
        std::make_move_iterator(PreprocessorDirectives.begin()),
3900
2.21k
        std::make_move_iterator(PreprocessorDirectives.end()));
3901
2.21k
    PreprocessorDirectives.clear();
3902
2.21k
  }
3903
  // Disconnect the current token from the last token on the previous line.
3904
250k
  FormatTok->Previous = nullptr;
3905
250k
}
3906
3907
1.84M
bool UnwrappedLineParser::eof() const { return FormatTok->is(tok::eof); }
3908
3909
1.05M
bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
3910
1.05M
  return (Line->InPPDirective || 
FormatTok.HasUnescapedNewline1.00M
) &&
3911
1.05M
         
FormatTok.NewlinesBefore > 0149k
;
3912
1.05M
}
3913
3914
// Checks if \p FormatTok is a line comment that continues the line comment
3915
// section on \p Line.
3916
static bool
3917
continuesLineCommentSection(const FormatToken &FormatTok,
3918
                            const UnwrappedLine &Line,
3919
23.5k
                            const llvm::Regex &CommentPragmasRegex) {
3920
23.5k
  if (Line.Tokens.empty())
3921
7.71k
    return false;
3922
3923
15.7k
  StringRef IndentContent = FormatTok.TokenText;
3924
15.7k
  if (FormatTok.TokenText.startswith("//") ||
3925
15.7k
      
FormatTok.TokenText.startswith("/*")2.46k
)
3926
15.7k
    IndentContent = FormatTok.TokenText.substr(2);
3927
15.7k
  if (CommentPragmasRegex.match(IndentContent))
3928
7
    return false;
3929
3930
  // If Line starts with a line comment, then FormatTok continues the comment
3931
  // section if its original column is greater or equal to the original start
3932
  // column of the line.
3933
  //
3934
  // Define the min column token of a line as follows: if a line ends in '{' or
3935
  // contains a '{' followed by a line comment, then the min column token is
3936
  // that '{'. Otherwise, the min column token of the line is the first token of
3937
  // the line.
3938
  //
3939
  // If Line starts with a token other than a line comment, then FormatTok
3940
  // continues the comment section if its original column is greater than the
3941
  // original start column of the min column token of the line.
3942
  //
3943
  // For example, the second line comment continues the first in these cases:
3944
  //
3945
  // // first line
3946
  // // second line
3947
  //
3948
  // and:
3949
  //
3950
  // // first line
3951
  //  // second line
3952
  //
3953
  // and:
3954
  //
3955
  // int i; // first line
3956
  //  // second line
3957
  //
3958
  // and:
3959
  //
3960
  // do { // first line
3961
  //      // second line
3962
  //   int i;
3963
  // } while (true);
3964
  //
3965
  // and:
3966
  //
3967
  // enum {
3968
  //   a, // first line
3969
  //    // second line
3970
  //   b
3971
  // };
3972
  //
3973
  // The second line comment doesn't continue the first in these cases:
3974
  //
3975
  //   // first line
3976
  //  // second line
3977
  //
3978
  // and:
3979
  //
3980
  // int i; // first line
3981
  // // second line
3982
  //
3983
  // and:
3984
  //
3985
  // do { // first line
3986
  //   // second line
3987
  //   int i;
3988
  // } while (true);
3989
  //
3990
  // and:
3991
  //
3992
  // enum {
3993
  //   a, // first line
3994
  //   // second line
3995
  // };
3996
15.7k
  const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
3997
3998
  // Scan for '{//'. If found, use the column of '{' as a min column for line
3999
  // comment section continuation.
4000
15.7k
  const FormatToken *PreviousToken = nullptr;
4001
75.4k
  for (const UnwrappedLineNode &Node : Line.Tokens) {
4002
75.4k
    if (PreviousToken && 
PreviousToken->is(tok::l_brace)59.7k
&&
4003
75.4k
        
isLineComment(*Node.Tok)2.13k
) {
4004
227
      MinColumnToken = PreviousToken;
4005
227
      break;
4006
227
    }
4007
75.2k
    PreviousToken = Node.Tok;
4008
4009
    // Grab the last newline preceding a token in this unwrapped line.
4010
75.2k
    if (Node.Tok->NewlinesBefore > 0)
4011
17.1k
      MinColumnToken = Node.Tok;
4012
75.2k
  }
4013
15.7k
  if (PreviousToken && PreviousToken->is(tok::l_brace))
4014
2.92k
    MinColumnToken = PreviousToken;
4015
4016
15.7k
  return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
4017
15.7k
                              MinColumnToken);
4018
15.7k
}
4019
4020
1.10M
void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
4021
1.10M
  bool JustComments = Line->Tokens.empty();
4022
1.10M
  for (FormatToken *Tok : CommentsBeforeNextToken) {
4023
    // Line comments that belong to the same line comment section are put on the
4024
    // same line since later we might want to reflow content between them.
4025
    // Additional fine-grained breaking of line comment sections is controlled
4026
    // by the class BreakableLineCommentSection in case it is desirable to keep
4027
    // several line comment sections in the same unwrapped line.
4028
    //
4029
    // FIXME: Consider putting separate line comment sections as children to the
4030
    // unwrapped line instead.
4031
8.09k
    Tok->ContinuesLineCommentSection =
4032
8.09k
        continuesLineCommentSection(*Tok, *Line, CommentPragmasRegex);
4033
8.09k
    if (isOnNewLine(*Tok) && 
JustComments6.51k
&&
!Tok->ContinuesLineCommentSection6.00k
)
4034
3.50k
      addUnwrappedLine();
4035
8.09k
    pushToken(Tok);
4036
8.09k
  }
4037
1.10M
  if (NewlineBeforeNext && 
JustComments155k
)
4038
131k
    addUnwrappedLine();
4039
1.10M
  CommentsBeforeNextToken.clear();
4040
1.10M
}
4041
4042
1.02M
void UnwrappedLineParser::nextToken(int LevelDifference) {
4043
1.02M
  if (eof())
4044
4.86k
    return;
4045
1.01M
  flushComments(isOnNewLine(*FormatTok));
4046
1.01M
  pushToken(FormatTok);
4047
1.01M
  FormatToken *Previous = FormatTok;
4048
1.01M
  if (!Style.isJavaScript())
4049
983k
    readToken(LevelDifference);
4050
34.1k
  else
4051
34.1k
    readTokenWithJavaScriptASI();
4052
1.01M
  FormatTok->Previous = Previous;
4053
1.01M
}
4054
4055
void UnwrappedLineParser::distributeComments(
4056
    const SmallVectorImpl<FormatToken *> &Comments,
4057
1.09M
    const FormatToken *NextTok) {
4058
  // Whether or not a line comment token continues a line is controlled by
4059
  // the method continuesLineCommentSection, with the following caveat:
4060
  //
4061
  // Define a trail of Comments to be a nonempty proper postfix of Comments such
4062
  // that each comment line from the trail is aligned with the next token, if
4063
  // the next token exists. If a trail exists, the beginning of the maximal
4064
  // trail is marked as a start of a new comment section.
4065
  //
4066
  // For example in this code:
4067
  //
4068
  // int a; // line about a
4069
  //   // line 1 about b
4070
  //   // line 2 about b
4071
  //   int b;
4072
  //
4073
  // the two lines about b form a maximal trail, so there are two sections, the
4074
  // first one consisting of the single comment "// line about a" and the
4075
  // second one consisting of the next two comments.
4076
1.09M
  if (Comments.empty())
4077
1.08M
    return;
4078
12.5k
  bool ShouldPushCommentsInCurrentLine = true;
4079
12.5k
  bool HasTrailAlignedWithNextToken = false;
4080
12.5k
  unsigned StartOfTrailAlignedWithNextToken = 0;
4081
12.5k
  if (NextTok) {
4082
    // We are skipping the first element intentionally.
4083
16.1k
    for (unsigned i = Comments.size() - 1; i > 0; 
--i3.56k
) {
4084
3.56k
      if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
4085
2.74k
        HasTrailAlignedWithNextToken = true;
4086
2.74k
        StartOfTrailAlignedWithNextToken = i;
4087
2.74k
      }
4088
3.56k
    }
4089
12.5k
  }
4090
28.6k
  for (unsigned i = 0, e = Comments.size(); i < e; 
++i16.1k
) {
4091
16.1k
    FormatToken *FormatTok = Comments[i];
4092
16.1k
    if (HasTrailAlignedWithNextToken && 
i == StartOfTrailAlignedWithNextToken3.56k
) {
4093
712
      FormatTok->ContinuesLineCommentSection = false;
4094
15.4k
    } else {
4095
15.4k
      FormatTok->ContinuesLineCommentSection =
4096
15.4k
          continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
4097
15.4k
    }
4098
16.1k
    if (!FormatTok->ContinuesLineCommentSection &&
4099
16.1k
        
(15.9k
isOnNewLine(*FormatTok)15.9k
||
FormatTok->IsFirst9.42k
))
4100
8.04k
      ShouldPushCommentsInCurrentLine = false;
4101
16.1k
    if (ShouldPushCommentsInCurrentLine)
4102
8.03k
      pushToken(FormatTok);
4103
8.09k
    else
4104
8.09k
      CommentsBeforeNextToken.push_back(FormatTok);
4105
16.1k
  }
4106
12.5k
}
4107
4108
1.08M
void UnwrappedLineParser::readToken(int LevelDifference) {
4109
1.08M
  SmallVector<FormatToken *, 1> Comments;
4110
1.08M
  bool PreviousWasComment = false;
4111
1.08M
  bool FirstNonCommentOnLine = false;
4112
1.10M
  do {
4113
1.10M
    FormatTok = Tokens->getNextToken();
4114
1.10M
    assert(FormatTok);
4115
1.10M
    while (FormatTok->getType() == TT_ConflictStart ||
4116
1.10M
           
FormatTok->getType() == TT_ConflictEnd1.10M
||
4117
1.10M
           
FormatTok->getType() == TT_ConflictAlternative1.10M
) {
4118
126
      if (FormatTok->getType() == TT_ConflictStart)
4119
27
        conditionalCompilationStart(/*Unreachable=*/false);
4120
99
      else if (FormatTok->getType() == TT_ConflictAlternative)
4121
72
        conditionalCompilationAlternative();
4122
27
      else if (FormatTok->getType() == TT_ConflictEnd)
4123
27
        conditionalCompilationEnd();
4124
126
      FormatTok = Tokens->getNextToken();
4125
126
      FormatTok->MustBreakBefore = true;
4126
126
    }
4127
4128
1.10M
    auto IsFirstNonCommentOnLine = [](bool FirstNonCommentOnLine,
4129
1.10M
                                      const FormatToken &Tok,
4130
1.11M
                                      bool PreviousWasComment) {
4131
1.11M
      auto IsFirstOnLine = [](const FormatToken &Tok) {
4132
1.11M
        return Tok.HasUnescapedNewline || 
Tok.IsFirst1.02M
;
4133
1.11M
      };
4134
4135
      // Consider preprocessor directives preceded by block comments as first
4136
      // on line.
4137
1.11M
      if (PreviousWasComment)
4138
16.6k
        return FirstNonCommentOnLine || 
IsFirstOnLine(Tok)7.87k
;
4139
1.10M
      return IsFirstOnLine(Tok);
4140
1.11M
    };
4141
4142
1.10M
    FirstNonCommentOnLine = IsFirstNonCommentOnLine(
4143
1.10M
        FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
4144
1.10M
    PreviousWasComment = FormatTok->is(tok::comment);
4145
4146
1.11M
    while (!Line->InPPDirective && 
FormatTok->is(tok::hash)1.06M
&&
4147
1.11M
           
FirstNonCommentOnLine13.4k
) {
4148
13.4k
      distributeComments(Comments, FormatTok);
4149
13.4k
      Comments.clear();
4150
      // If there is an unfinished unwrapped line, we flush the preprocessor
4151
      // directives only after that unwrapped line was finished later.
4152
13.4k
      bool SwitchToPreprocessorLines = !Line->Tokens.empty();
4153
13.4k
      ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
4154
13.4k
      assert((LevelDifference >= 0 ||
4155
13.4k
              static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
4156
13.4k
             "LevelDifference makes Line->Level negative");
4157
0
      Line->Level += LevelDifference;
4158
      // Comments stored before the preprocessor directive need to be output
4159
      // before the preprocessor directive, at the same level as the
4160
      // preprocessor directive, as we consider them to apply to the directive.
4161
13.4k
      if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&
4162
13.4k
          
PPBranchLevel > 0312
)
4163
96
        Line->Level += PPBranchLevel;
4164
13.4k
      flushComments(isOnNewLine(*FormatTok));
4165
13.4k
      parsePPDirective();
4166
13.4k
      PreviousWasComment = FormatTok->is(tok::comment);
4167
13.4k
      FirstNonCommentOnLine = IsFirstNonCommentOnLine(
4168
13.4k
          FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
4169
13.4k
    }
4170
4171
1.10M
    if (!PPStack.empty() && 
(PPStack.back().Kind == PP_Unreachable)29.0k
&&
4172
1.10M
        
!Line->InPPDirective13.4k
)
4173
7.49k
      continue;
4174
4175
1.09M
    if (!FormatTok->is(tok::comment)) {
4176
1.08M
      distributeComments(Comments, FormatTok);
4177
1.08M
      Comments.clear();
4178
1.08M
      return;
4179
1.08M
    }
4180
4181
16.1k
    Comments.push_back(FormatTok);
4182
23.6k
  } while (!eof());
4183
4184
0
  distributeComments(Comments, nullptr);
4185
0
  Comments.clear();
4186
0
}
4187
4188
1.09M
void UnwrappedLineParser::pushToken(FormatToken *Tok) {
4189
1.09M
  Line->Tokens.push_back(UnwrappedLineNode(Tok));
4190
1.09M
  if (MustBreakBeforeNextToken) {
4191
4.82k
    Line->Tokens.back().Tok->MustBreakBefore = true;
4192
4.82k
    MustBreakBeforeNextToken = false;
4193
4.82k
  }
4194
1.09M
}
4195
4196
} // end namespace format
4197
} // end namespace clang