Coverage Report

Created: 2024-01-17 10:31

/src/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 "FormatTokenLexer.h"
18
#include "FormatTokenSource.h"
19
#include "Macros.h"
20
#include "TokenAnnotator.h"
21
#include "clang/Basic/TokenKinds.h"
22
#include "llvm/ADT/STLExtras.h"
23
#include "llvm/ADT/StringRef.h"
24
#include "llvm/Support/Debug.h"
25
#include "llvm/Support/raw_os_ostream.h"
26
#include "llvm/Support/raw_ostream.h"
27
28
#include <algorithm>
29
#include <utility>
30
31
#define DEBUG_TYPE "format-parser"
32
33
namespace clang {
34
namespace format {
35
36
namespace {
37
38
void printLine(llvm::raw_ostream &OS, const UnwrappedLine &Line,
39
0
               StringRef Prefix = "", bool PrintText = false) {
40
0
  OS << Prefix << "Line(" << Line.Level << ", FSC=" << Line.FirstStartColumn
41
0
     << ")" << (Line.InPPDirective ? " MACRO" : "") << ": ";
42
0
  bool NewLine = false;
43
0
  for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
44
0
                                                    E = Line.Tokens.end();
45
0
       I != E; ++I) {
46
0
    if (NewLine) {
47
0
      OS << Prefix;
48
0
      NewLine = false;
49
0
    }
50
0
    OS << I->Tok->Tok.getName() << "[" << "T=" << (unsigned)I->Tok->getType()
51
0
       << ", OC=" << I->Tok->OriginalColumn << ", \"" << I->Tok->TokenText
52
0
       << "\"] ";
53
0
    for (SmallVectorImpl<UnwrappedLine>::const_iterator
54
0
             CI = I->Children.begin(),
55
0
             CE = I->Children.end();
56
0
         CI != CE; ++CI) {
57
0
      OS << "\n";
58
0
      printLine(OS, *CI, (Prefix + "  ").str());
59
0
      NewLine = true;
60
0
    }
61
0
  }
62
0
  if (!NewLine)
63
0
    OS << "\n";
64
0
}
65
66
0
LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line) {
67
0
  printLine(llvm::dbgs(), Line);
68
0
}
69
70
class ScopedDeclarationState {
71
public:
72
  ScopedDeclarationState(UnwrappedLine &Line, llvm::BitVector &Stack,
73
                         bool MustBeDeclaration)
74
271k
      : Line(Line), Stack(Stack) {
75
271k
    Line.MustBeDeclaration = MustBeDeclaration;
76
271k
    Stack.push_back(MustBeDeclaration);
77
271k
  }
78
271k
  ~ScopedDeclarationState() {
79
271k
    Stack.pop_back();
80
271k
    if (!Stack.empty())
81
270k
      Line.MustBeDeclaration = Stack.back();
82
1.05k
    else
83
1.05k
      Line.MustBeDeclaration = true;
84
271k
  }
85
86
private:
87
  UnwrappedLine &Line;
88
  llvm::BitVector &Stack;
89
};
90
91
} // end anonymous namespace
92
93
class ScopedLineState {
94
public:
95
  ScopedLineState(UnwrappedLineParser &Parser,
96
                  bool SwitchToPreprocessorLines = false)
97
275k
      : Parser(Parser), OriginalLines(Parser.CurrentLines) {
98
275k
    if (SwitchToPreprocessorLines)
99
253k
      Parser.CurrentLines = &Parser.PreprocessorDirectives;
100
21.7k
    else if (!Parser.Line->Tokens.empty())
101
21.7k
      Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
102
275k
    PreBlockLine = std::move(Parser.Line);
103
275k
    Parser.Line = std::make_unique<UnwrappedLine>();
104
275k
    Parser.Line->Level = PreBlockLine->Level;
105
275k
    Parser.Line->PPLevel = PreBlockLine->PPLevel;
106
275k
    Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
107
275k
    Parser.Line->InMacroBody = PreBlockLine->InMacroBody;
108
275k
  }
109
110
275k
  ~ScopedLineState() {
111
275k
    if (!Parser.Line->Tokens.empty())
112
14.6k
      Parser.addUnwrappedLine();
113
275k
    assert(Parser.Line->Tokens.empty());
114
0
    Parser.Line = std::move(PreBlockLine);
115
275k
    if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
116
253k
      Parser.MustBreakBeforeNextToken = true;
117
275k
    Parser.CurrentLines = OriginalLines;
118
275k
  }
119
120
private:
121
  UnwrappedLineParser &Parser;
122
123
  std::unique_ptr<UnwrappedLine> PreBlockLine;
124
  SmallVectorImpl<UnwrappedLine> *OriginalLines;
125
};
126
127
class CompoundStatementIndenter {
128
public:
129
  CompoundStatementIndenter(UnwrappedLineParser *Parser,
130
                            const FormatStyle &Style, unsigned &LineLevel)
131
      : CompoundStatementIndenter(Parser, LineLevel,
132
                                  Style.BraceWrapping.AfterControlStatement,
133
3.31k
                                  Style.BraceWrapping.IndentBraces) {}
134
  CompoundStatementIndenter(UnwrappedLineParser *Parser, unsigned &LineLevel,
135
                            bool WrapBrace, bool IndentBrace)
136
29.0k
      : LineLevel(LineLevel), OldLineLevel(LineLevel) {
137
29.0k
    if (WrapBrace)
138
0
      Parser->addUnwrappedLine();
139
29.0k
    if (IndentBrace)
140
0
      ++LineLevel;
141
29.0k
  }
142
29.0k
  ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
143
144
private:
145
  unsigned &LineLevel;
146
  unsigned OldLineLevel;
147
};
148
149
UnwrappedLineParser::UnwrappedLineParser(
150
    SourceManager &SourceMgr, const FormatStyle &Style,
151
    const AdditionalKeywords &Keywords, unsigned FirstStartColumn,
152
    ArrayRef<FormatToken *> Tokens, UnwrappedLineConsumer &Callback,
153
    llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
154
    IdentifierTable &IdentTable)
155
    : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
156
      CurrentLines(&Lines), Style(Style), Keywords(Keywords),
157
      CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
158
      Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
159
      IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
160
                       ? IG_Rejected
161
                       : IG_Inited),
162
      IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn),
163
1.01k
      Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) {}
164
165
1.05k
void UnwrappedLineParser::reset() {
166
1.05k
  PPBranchLevel = -1;
167
1.05k
  IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None
168
1.05k
                     ? IG_Rejected
169
1.05k
                     : IG_Inited;
170
1.05k
  IncludeGuardToken = nullptr;
171
1.05k
  Line.reset(new UnwrappedLine);
172
1.05k
  CommentsBeforeNextToken.clear();
173
1.05k
  FormatTok = nullptr;
174
1.05k
  MustBreakBeforeNextToken = false;
175
1.05k
  IsDecltypeAutoFunction = false;
176
1.05k
  PreprocessorDirectives.clear();
177
1.05k
  CurrentLines = &Lines;
178
1.05k
  DeclarationScopeStack.clear();
179
1.05k
  NestedTooDeep.clear();
180
1.05k
  NestedLambdas.clear();
181
1.05k
  PPStack.clear();
182
1.05k
  Line->FirstStartColumn = FirstStartColumn;
183
184
1.05k
  if (!Unexpanded.empty())
185
0
    for (FormatToken *Token : AllTokens)
186
0
      Token->MacroCtx.reset();
187
1.05k
  CurrentExpandedLines.clear();
188
1.05k
  ExpandedLines.clear();
189
1.05k
  Unexpanded.clear();
190
1.05k
  InExpansion = false;
191
1.05k
  Reconstruct.reset();
192
1.05k
}
193
194
1.01k
void UnwrappedLineParser::parse() {
195
1.01k
  IndexedTokenSource TokenSource(AllTokens);
196
1.01k
  Line->FirstStartColumn = FirstStartColumn;
197
1.05k
  do {
198
1.05k
    LLVM_DEBUG(llvm::dbgs() << "----\n");
199
1.05k
    reset();
200
1.05k
    Tokens = &TokenSource;
201
1.05k
    TokenSource.reset();
202
203
1.05k
    readToken();
204
1.05k
    parseFile();
205
206
    // If we found an include guard then all preprocessor directives (other than
207
    // the guard) are over-indented by one.
208
1.05k
    if (IncludeGuard == IG_Found) {
209
0
      for (auto &Line : Lines)
210
0
        if (Line.InPPDirective && Line.Level > 0)
211
0
          --Line.Level;
212
0
    }
213
214
    // Create line with eof token.
215
1.05k
    assert(eof());
216
0
    pushToken(FormatTok);
217
1.05k
    addUnwrappedLine();
218
219
    // In a first run, format everything with the lines containing macro calls
220
    // replaced by the expansion.
221
1.05k
    if (!ExpandedLines.empty()) {
222
0
      LLVM_DEBUG(llvm::dbgs() << "Expanded lines:\n");
223
0
      for (const auto &Line : Lines) {
224
0
        if (!Line.Tokens.empty()) {
225
0
          auto it = ExpandedLines.find(Line.Tokens.begin()->Tok);
226
0
          if (it != ExpandedLines.end()) {
227
0
            for (const auto &Expanded : it->second) {
228
0
              LLVM_DEBUG(printDebugInfo(Expanded));
229
0
              Callback.consumeUnwrappedLine(Expanded);
230
0
            }
231
0
            continue;
232
0
          }
233
0
        }
234
0
        LLVM_DEBUG(printDebugInfo(Line));
235
0
        Callback.consumeUnwrappedLine(Line);
236
0
      }
237
0
      Callback.finishRun();
238
0
    }
239
240
1.05k
    LLVM_DEBUG(llvm::dbgs() << "Unwrapped lines:\n");
241
1.32M
    for (const UnwrappedLine &Line : Lines) {
242
1.32M
      LLVM_DEBUG(printDebugInfo(Line));
243
1.32M
      Callback.consumeUnwrappedLine(Line);
244
1.32M
    }
245
1.05k
    Callback.finishRun();
246
1.05k
    Lines.clear();
247
262k
    while (!PPLevelBranchIndex.empty() &&
248
262k
           PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
249
261k
      PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
250
261k
      PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
251
261k
    }
252
1.05k
    if (!PPLevelBranchIndex.empty()) {
253
42
      ++PPLevelBranchIndex.back();
254
42
      assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
255
0
      assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
256
42
    }
257
1.05k
  } while (!PPLevelBranchIndex.empty());
258
1.01k
}
259
260
3.08k
void UnwrappedLineParser::parseFile() {
261
  // The top-level context in a file always has declarations, except for pre-
262
  // processor directives and JavaScript files.
263
3.08k
  bool MustBeDeclaration = !Line->InPPDirective && !Style.isJavaScript();
264
3.08k
  ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
265
3.08k
                                          MustBeDeclaration);
266
3.08k
  if (Style.Language == FormatStyle::LK_TextProto)
267
0
    parseBracedList();
268
3.08k
  else
269
3.08k
    parseLevel();
270
  // Make sure to format the remaining tokens.
271
  //
272
  // LK_TextProto is special since its top-level is parsed as the body of a
273
  // braced list, which does not necessarily have natural line separators such
274
  // as a semicolon. Comments after the last entry that have been determined to
275
  // not belong to that line, as in:
276
  //   key: value
277
  //   // endfile comment
278
  // do not have a chance to be put on a line of their own until this point.
279
  // Here we add this newline before end-of-file comments.
280
3.08k
  if (Style.Language == FormatStyle::LK_TextProto &&
281
3.08k
      !CommentsBeforeNextToken.empty()) {
282
0
    addUnwrappedLine();
283
0
  }
284
3.08k
  flushComments(true);
285
3.08k
  addUnwrappedLine();
286
3.08k
}
287
288
0
void UnwrappedLineParser::parseCSharpGenericTypeConstraint() {
289
0
  do {
290
0
    switch (FormatTok->Tok.getKind()) {
291
0
    case tok::l_brace:
292
0
      return;
293
0
    default:
294
0
      if (FormatTok->is(Keywords.kw_where)) {
295
0
        addUnwrappedLine();
296
0
        nextToken();
297
0
        parseCSharpGenericTypeConstraint();
298
0
        break;
299
0
      }
300
0
      nextToken();
301
0
      break;
302
0
    }
303
0
  } while (!eof());
304
0
}
305
306
0
void UnwrappedLineParser::parseCSharpAttribute() {
307
0
  int UnpairedSquareBrackets = 1;
308
0
  do {
309
0
    switch (FormatTok->Tok.getKind()) {
310
0
    case tok::r_square:
311
0
      nextToken();
312
0
      --UnpairedSquareBrackets;
313
0
      if (UnpairedSquareBrackets == 0) {
314
0
        addUnwrappedLine();
315
0
        return;
316
0
      }
317
0
      break;
318
0
    case tok::l_square:
319
0
      ++UnpairedSquareBrackets;
320
0
      nextToken();
321
0
      break;
322
0
    default:
323
0
      nextToken();
324
0
      break;
325
0
    }
326
0
  } while (!eof());
327
0
}
328
329
0
bool UnwrappedLineParser::precededByCommentOrPPDirective() const {
330
0
  if (!Lines.empty() && Lines.back().InPPDirective)
331
0
    return true;
332
333
0
  const FormatToken *Previous = Tokens->getPreviousToken();
334
0
  return Previous && Previous->is(tok::comment) &&
335
0
         (Previous->IsMultiline || Previous->NewlinesBefore > 0);
336
0
}
337
338
/// \brief Parses a level, that is ???.
339
/// \param OpeningBrace Opening brace (\p nullptr if absent) of that level.
340
/// \param IfKind The \p if statement kind in the level.
341
/// \param IfLeftBrace The left brace of the \p if block in the level.
342
/// \returns true if a simple block of if/else/for/while, or false otherwise.
343
/// (A simple block has a single statement.)
344
bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
345
                                     IfStmtKind *IfKind,
346
271k
                                     FormatToken **IfLeftBrace) {
347
271k
  const bool InRequiresExpression =
348
271k
      OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
349
271k
  const bool IsPrecededByCommentOrPPDirective =
350
271k
      !Style.RemoveBracesLLVM || precededByCommentOrPPDirective();
351
271k
  FormatToken *IfLBrace = nullptr;
352
271k
  bool HasDoWhile = false;
353
271k
  bool HasLabel = false;
354
271k
  unsigned StatementCount = 0;
355
271k
  bool SwitchLabelEncountered = false;
356
357
1.31M
  do {
358
1.31M
    if (FormatTok->isAttribute()) {
359
0
      nextToken();
360
0
      continue;
361
0
    }
362
1.31M
    tok::TokenKind kind = FormatTok->Tok.getKind();
363
1.31M
    if (FormatTok->getType() == TT_MacroBlockBegin)
364
0
      kind = tok::l_brace;
365
1.31M
    else if (FormatTok->getType() == TT_MacroBlockEnd)
366
0
      kind = tok::r_brace;
367
368
1.31M
    auto ParseDefault = [this, OpeningBrace, IfKind, &IfLBrace, &HasDoWhile,
369
1.31M
                         &HasLabel, &StatementCount] {
370
722k
      parseStructuralElement(OpeningBrace, IfKind, &IfLBrace,
371
722k
                             HasDoWhile ? nullptr : &HasDoWhile,
372
722k
                             HasLabel ? nullptr : &HasLabel);
373
722k
      ++StatementCount;
374
722k
      assert(StatementCount > 0 && "StatementCount overflow!");
375
722k
    };
376
377
1.31M
    switch (kind) {
378
0
    case tok::comment:
379
0
      nextToken();
380
0
      addUnwrappedLine();
381
0
      break;
382
177k
    case tok::l_brace:
383
177k
      if (InRequiresExpression) {
384
0
        FormatTok->setFinalizedType(TT_RequiresExpressionLBrace);
385
177k
      } else if (FormatTok->Previous &&
386
177k
                 FormatTok->Previous->ClosesRequiresClause) {
387
        // We need the 'default' case here to correctly parse a function
388
        // l_brace.
389
0
        ParseDefault();
390
0
        continue;
391
0
      }
392
177k
      if (!InRequiresExpression && FormatTok->isNot(TT_MacroBlockBegin) &&
393
177k
          tryToParseBracedList()) {
394
1.42k
        continue;
395
1.42k
      }
396
176k
      parseBlock();
397
176k
      ++StatementCount;
398
176k
      assert(StatementCount > 0 && "StatementCount overflow!");
399
0
      addUnwrappedLine();
400
176k
      break;
401
417k
    case tok::r_brace:
402
417k
      if (OpeningBrace) {
403
216k
        if (!Style.RemoveBracesLLVM || Line->InPPDirective ||
404
216k
            !OpeningBrace->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace)) {
405
216k
          return false;
406
216k
        }
407
0
        if (FormatTok->isNot(tok::r_brace) || StatementCount != 1 || HasLabel ||
408
0
            HasDoWhile || IsPrecededByCommentOrPPDirective ||
409
0
            precededByCommentOrPPDirective()) {
410
0
          return false;
411
0
        }
412
0
        const FormatToken *Next = Tokens->peekNextToken();
413
0
        if (Next->is(tok::comment) && Next->NewlinesBefore == 0)
414
0
          return false;
415
0
        if (IfLeftBrace)
416
0
          *IfLeftBrace = IfLBrace;
417
0
        return true;
418
0
      }
419
201k
      nextToken();
420
201k
      addUnwrappedLine();
421
201k
      break;
422
0
    case tok::kw_default: {
423
0
      unsigned StoredPosition = Tokens->getPosition();
424
0
      FormatToken *Next;
425
0
      do {
426
0
        Next = Tokens->getNextToken();
427
0
        assert(Next);
428
0
      } while (Next->is(tok::comment));
429
0
      FormatTok = Tokens->setPosition(StoredPosition);
430
0
      if (Next->isNot(tok::colon)) {
431
        // default not followed by ':' is not a case label; treat it like
432
        // an identifier.
433
0
        parseStructuralElement();
434
0
        break;
435
0
      }
436
      // Else, if it is 'default:', fall through to the case handling.
437
0
      [[fallthrough]];
438
0
    }
439
0
    case tok::kw_case:
440
0
      if (Style.Language == FormatStyle::LK_Proto || Style.isVerilog() ||
441
0
          (Style.isJavaScript() && Line->MustBeDeclaration)) {
442
        // Proto: there are no switch/case statements
443
        // Verilog: Case labels don't have this word. We handle case
444
        // labels including default in TokenAnnotator.
445
        // JavaScript: A 'case: string' style field declaration.
446
0
        ParseDefault();
447
0
        break;
448
0
      }
449
0
      if (!SwitchLabelEncountered &&
450
0
          (Style.IndentCaseLabels ||
451
0
           (Line->InPPDirective && Line->Level == 1))) {
452
0
        ++Line->Level;
453
0
      }
454
0
      SwitchLabelEncountered = true;
455
0
      parseStructuralElement();
456
0
      break;
457
1.04k
    case tok::l_square:
458
1.04k
      if (Style.isCSharp()) {
459
0
        nextToken();
460
0
        parseCSharpAttribute();
461
0
        break;
462
0
      }
463
1.04k
      if (handleCppAttributes())
464
0
        break;
465
1.04k
      [[fallthrough]];
466
722k
    default:
467
722k
      ParseDefault();
468
722k
      break;
469
1.31M
    }
470
1.31M
  } while (!eof());
471
472
55.3k
  return false;
473
271k
}
474
475
254k
void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
476
  // We'll parse forward through the tokens until we hit
477
  // a closing brace or eof - note that getNextToken() will
478
  // parse macros, so this will magically work inside macro
479
  // definitions, too.
480
254k
  unsigned StoredPosition = Tokens->getPosition();
481
254k
  FormatToken *Tok = FormatTok;
482
254k
  const FormatToken *PrevTok = Tok->Previous;
483
  // Keep a stack of positions of lbrace tokens. We will
484
  // update information about whether an lbrace starts a
485
  // braced init list or a different block during the loop.
486
254k
  struct StackEntry {
487
254k
    FormatToken *Tok;
488
254k
    const FormatToken *PrevTok;
489
254k
  };
490
254k
  SmallVector<StackEntry, 8> LBraceStack;
491
254k
  assert(Tok->is(tok::l_brace));
492
54.9M
  do {
493
    // Get next non-comment, non-preprocessor token.
494
54.9M
    FormatToken *NextTok;
495
55.0M
    do {
496
55.0M
      NextTok = Tokens->getNextToken();
497
55.0M
    } while (NextTok->is(tok::comment));
498
55.1M
    while (NextTok->is(tok::hash) && !Line->InMacroBody) {
499
194k
      NextTok = Tokens->getNextToken();
500
44.0M
      do {
501
44.0M
        NextTok = Tokens->getNextToken();
502
44.0M
      } while (NextTok->is(tok::comment) ||
503
44.0M
               (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof)));
504
194k
    }
505
506
54.9M
    switch (Tok->Tok.getKind()) {
507
5.58M
    case tok::l_brace:
508
5.58M
      if (Style.isJavaScript() && PrevTok) {
509
0
        if (PrevTok->isOneOf(tok::colon, tok::less)) {
510
          // A ':' indicates this code is in a type, or a braced list
511
          // following a label in an object literal ({a: {b: 1}}).
512
          // A '<' could be an object used in a comparison, but that is nonsense
513
          // code (can never return true), so more likely it is a generic type
514
          // argument (`X<{a: string; b: number}>`).
515
          // The code below could be confused by semicolons between the
516
          // individual members in a type member list, which would normally
517
          // trigger BK_Block. In both cases, this must be parsed as an inline
518
          // braced init.
519
0
          Tok->setBlockKind(BK_BracedInit);
520
0
        } else if (PrevTok->is(tok::r_paren)) {
521
          // `) { }` can only occur in function or method declarations in JS.
522
0
          Tok->setBlockKind(BK_Block);
523
0
        }
524
5.58M
      } else {
525
5.58M
        Tok->setBlockKind(BK_Unknown);
526
5.58M
      }
527
5.58M
      LBraceStack.push_back({Tok, PrevTok});
528
5.58M
      break;
529
377k
    case tok::r_brace:
530
377k
      if (LBraceStack.empty())
531
0
        break;
532
377k
      if (LBraceStack.back().Tok->is(BK_Unknown)) {
533
367k
        bool ProbablyBracedList = false;
534
367k
        if (Style.Language == FormatStyle::LK_Proto) {
535
0
          ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
536
367k
        } else {
537
          // Skip NextTok over preprocessor lines, otherwise we may not
538
          // properly diagnose the block as a braced intializer
539
          // if the comma separator appears after the pp directive.
540
367k
          while (NextTok->is(tok::hash)) {
541
0
            ScopedMacroState MacroState(*Line, Tokens, NextTok);
542
0
            do {
543
0
              NextTok = Tokens->getNextToken();
544
0
            } while (NextTok->isNot(tok::eof));
545
0
          }
546
547
          // Using OriginalColumn to distinguish between ObjC methods and
548
          // binary operators is a bit hacky.
549
367k
          bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
550
367k
                                  NextTok->OriginalColumn == 0;
551
552
          // Try to detect a braced list. Note that regardless how we mark inner
553
          // braces here, we will overwrite the BlockKind later if we parse a
554
          // braced list (where all blocks inside are by default braced lists),
555
          // or when we explicitly detect blocks (for example while parsing
556
          // lambdas).
557
558
          // If we already marked the opening brace as braced list, the closing
559
          // must also be part of it.
560
367k
          ProbablyBracedList = LBraceStack.back().Tok->is(TT_BracedListLBrace);
561
562
367k
          ProbablyBracedList = ProbablyBracedList ||
563
367k
                               (Style.isJavaScript() &&
564
367k
                                NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
565
0
                                                 Keywords.kw_as));
566
367k
          ProbablyBracedList = ProbablyBracedList ||
567
367k
                               (Style.isCpp() && NextTok->is(tok::l_paren));
568
569
          // If there is a comma, semicolon or right paren after the closing
570
          // brace, we assume this is a braced initializer list.
571
          // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
572
          // braced list in JS.
573
367k
          ProbablyBracedList =
574
367k
              ProbablyBracedList ||
575
367k
              NextTok->isOneOf(tok::comma, tok::period, tok::colon,
576
366k
                               tok::r_paren, tok::r_square, tok::ellipsis);
577
578
          // Distinguish between braced list in a constructor initializer list
579
          // followed by constructor body, or just adjacent blocks.
580
367k
          ProbablyBracedList =
581
367k
              ProbablyBracedList ||
582
367k
              (NextTok->is(tok::l_brace) && LBraceStack.back().PrevTok &&
583
287k
               LBraceStack.back().PrevTok->isOneOf(tok::identifier,
584
1.33k
                                                   tok::greater));
585
586
367k
          ProbablyBracedList =
587
367k
              ProbablyBracedList ||
588
367k
              (NextTok->is(tok::identifier) &&
589
287k
               !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace));
590
591
367k
          ProbablyBracedList = ProbablyBracedList ||
592
367k
                               (NextTok->is(tok::semi) &&
593
232k
                                (!ExpectClassBody || LBraceStack.size() != 1));
594
595
367k
          ProbablyBracedList =
596
367k
              ProbablyBracedList ||
597
367k
              (NextTok->isBinaryOperator() && !NextIsObjCMethod);
598
599
367k
          if (!Style.isCSharp() && NextTok->is(tok::l_square)) {
600
            // We can have an array subscript after a braced init
601
            // list, but C++11 attributes are expected after blocks.
602
126
            NextTok = Tokens->getNextToken();
603
126
            ProbablyBracedList = NextTok->isNot(tok::l_square);
604
126
          }
605
367k
        }
606
367k
        if (ProbablyBracedList) {
607
158k
          Tok->setBlockKind(BK_BracedInit);
608
158k
          LBraceStack.back().Tok->setBlockKind(BK_BracedInit);
609
208k
        } else {
610
208k
          Tok->setBlockKind(BK_Block);
611
208k
          LBraceStack.back().Tok->setBlockKind(BK_Block);
612
208k
        }
613
367k
      }
614
377k
      LBraceStack.pop_back();
615
377k
      break;
616
3.57M
    case tok::identifier:
617
3.57M
      if (Tok->isNot(TT_StatementMacro))
618
3.57M
        break;
619
3.57M
      [[fallthrough]];
620
385k
    case tok::at:
621
862k
    case tok::semi:
622
867k
    case tok::kw_if:
623
868k
    case tok::kw_while:
624
868k
    case tok::kw_for:
625
868k
    case tok::kw_switch:
626
868k
    case tok::kw_try:
627
868k
    case tok::kw___try:
628
868k
      if (!LBraceStack.empty() && LBraceStack.back().Tok->is(BK_Unknown))
629
283k
        LBraceStack.back().Tok->setBlockKind(BK_Block);
630
868k
      break;
631
44.5M
    default:
632
44.5M
      break;
633
54.9M
    }
634
54.9M
    PrevTok = Tok;
635
54.9M
    Tok = NextTok;
636
54.9M
  } while (Tok->isNot(tok::eof) && !LBraceStack.empty());
637
638
  // Assume other blocks for all unclosed opening braces.
639
254k
  for (const auto &Entry : LBraceStack)
640
5.20M
    if (Entry.Tok->is(BK_Unknown))
641
4.93M
      Entry.Tok->setBlockKind(BK_Block);
642
643
254k
  FormatTok = Tokens->setPosition(StoredPosition);
644
254k
}
645
646
// Sets the token type of the directly previous right brace.
647
3.66k
void UnwrappedLineParser::setPreviousRBraceType(TokenType Type) {
648
3.66k
  if (auto Prev = FormatTok->getPreviousNonComment();
649
3.66k
      Prev && Prev->is(tok::r_brace)) {
650
3.54k
    Prev->setFinalizedType(Type);
651
3.54k
  }
652
3.66k
}
653
654
template <class T>
655
2.45G
static inline void hash_combine(std::size_t &seed, const T &v) {
656
2.45G
  std::hash<T> hasher;
657
2.45G
  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
658
2.45G
}
659
660
751k
size_t UnwrappedLineParser::computePPHash() const {
661
751k
  size_t h = 0;
662
1.22G
  for (const auto &i : PPStack) {
663
1.22G
    hash_combine(h, size_t(i.Kind));
664
1.22G
    hash_combine(h, i.Line);
665
1.22G
  }
666
751k
  return h;
667
751k
}
668
669
// Checks whether \p ParsedLine might fit on a single line. If \p OpeningBrace
670
// is not null, subtracts its length (plus the preceding space) when computing
671
// the length of \p ParsedLine. We must clone the tokens of \p ParsedLine before
672
// running the token annotator on it so that we can restore them afterward.
673
bool UnwrappedLineParser::mightFitOnOneLine(
674
0
    UnwrappedLine &ParsedLine, const FormatToken *OpeningBrace) const {
675
0
  const auto ColumnLimit = Style.ColumnLimit;
676
0
  if (ColumnLimit == 0)
677
0
    return true;
678
679
0
  auto &Tokens = ParsedLine.Tokens;
680
0
  assert(!Tokens.empty());
681
682
0
  const auto *LastToken = Tokens.back().Tok;
683
0
  assert(LastToken);
684
685
0
  SmallVector<UnwrappedLineNode> SavedTokens(Tokens.size());
686
687
0
  int Index = 0;
688
0
  for (const auto &Token : Tokens) {
689
0
    assert(Token.Tok);
690
0
    auto &SavedToken = SavedTokens[Index++];
691
0
    SavedToken.Tok = new FormatToken;
692
0
    SavedToken.Tok->copyFrom(*Token.Tok);
693
0
    SavedToken.Children = std::move(Token.Children);
694
0
  }
695
696
0
  AnnotatedLine Line(ParsedLine);
697
0
  assert(Line.Last == LastToken);
698
699
0
  TokenAnnotator Annotator(Style, Keywords);
700
0
  Annotator.annotate(Line);
701
0
  Annotator.calculateFormattingInformation(Line);
702
703
0
  auto Length = LastToken->TotalLength;
704
0
  if (OpeningBrace) {
705
0
    assert(OpeningBrace != Tokens.front().Tok);
706
0
    if (auto Prev = OpeningBrace->Previous;
707
0
        Prev && Prev->TotalLength + ColumnLimit == OpeningBrace->TotalLength) {
708
0
      Length -= ColumnLimit;
709
0
    }
710
0
    Length -= OpeningBrace->TokenText.size() + 1;
711
0
  }
712
713
0
  if (const auto *FirstToken = Line.First; FirstToken->is(tok::r_brace)) {
714
0
    assert(!OpeningBrace || OpeningBrace->is(TT_ControlStatementLBrace));
715
0
    Length -= FirstToken->TokenText.size() + 1;
716
0
  }
717
718
0
  Index = 0;
719
0
  for (auto &Token : Tokens) {
720
0
    const auto &SavedToken = SavedTokens[Index++];
721
0
    Token.Tok->copyFrom(*SavedToken.Tok);
722
0
    Token.Children = std::move(SavedToken.Children);
723
0
    delete SavedToken.Tok;
724
0
  }
725
726
  // If these change PPLevel needs to be used for get correct indentation.
727
0
  assert(!Line.InMacroBody);
728
0
  assert(!Line.InPPDirective);
729
0
  return Line.Level * Style.IndentWidth + Length <= ColumnLimit;
730
0
}
731
732
FormatToken *UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
733
                                             unsigned AddLevels, bool MunchSemi,
734
                                             bool KeepBraces,
735
                                             IfStmtKind *IfKind,
736
540k
                                             bool UnindentWhitesmithsBraces) {
737
751k
  auto HandleVerilogBlockLabel = [this]() {
738
    // ":" name
739
751k
    if (Style.isVerilog() && FormatTok->is(tok::colon)) {
740
0
      nextToken();
741
0
      if (Keywords.isVerilogIdentifier(*FormatTok))
742
0
        nextToken();
743
0
    }
744
751k
  };
745
746
  // Whether this is a Verilog-specific block that has a special header like a
747
  // module.
748
540k
  const bool VerilogHierarchy =
749
540k
      Style.isVerilog() && Keywords.isVerilogHierarchy(*FormatTok);
750
540k
  assert((FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) ||
751
540k
          (Style.isVerilog() &&
752
540k
           (Keywords.isVerilogBegin(*FormatTok) || VerilogHierarchy))) &&
753
540k
         "'{' or macro block token expected");
754
0
  FormatToken *Tok = FormatTok;
755
540k
  const bool FollowedByComment = Tokens->peekNextToken()->is(tok::comment);
756
540k
  auto Index = CurrentLines->size();
757
540k
  const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
758
540k
  FormatTok->setBlockKind(BK_Block);
759
760
  // For Whitesmiths mode, jump to the next level prior to skipping over the
761
  // braces.
762
540k
  if (!VerilogHierarchy && AddLevels > 0 &&
763
540k
      Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
764
0
    ++Line->Level;
765
0
  }
766
767
540k
  size_t PPStartHash = computePPHash();
768
769
540k
  const unsigned InitialLevel = Line->Level;
770
540k
  if (VerilogHierarchy) {
771
0
    AddLevels += parseVerilogHierarchyHeader();
772
540k
  } else {
773
540k
    nextToken(/*LevelDifference=*/AddLevels);
774
540k
    HandleVerilogBlockLabel();
775
540k
  }
776
777
  // Bail out if there are too many levels. Otherwise, the stack might overflow.
778
540k
  if (Line->Level > 300)
779
293k
    return nullptr;
780
781
246k
  if (MacroBlock && FormatTok->is(tok::l_paren))
782
0
    parseParens();
783
784
246k
  size_t NbPreprocessorDirectives =
785
246k
      !parsingPPDirective() ? PreprocessorDirectives.size() : 0;
786
246k
  addUnwrappedLine();
787
246k
  size_t OpeningLineIndex =
788
246k
      CurrentLines->empty()
789
246k
          ? (UnwrappedLine::kInvalidIndex)
790
246k
          : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
791
792
  // Whitesmiths is weird here. The brace needs to be indented for the namespace
793
  // block, but the block itself may not be indented depending on the style
794
  // settings. This allows the format to back up one level in those cases.
795
246k
  if (UnindentWhitesmithsBraces)
796
0
    --Line->Level;
797
798
246k
  ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
799
246k
                                          MustBeDeclaration);
800
246k
  if (AddLevels > 0u && Style.BreakBeforeBraces != FormatStyle::BS_Whitesmiths)
801
246k
    Line->Level += AddLevels;
802
803
246k
  FormatToken *IfLBrace = nullptr;
804
246k
  const bool SimpleBlock = parseLevel(Tok, IfKind, &IfLBrace);
805
806
246k
  if (eof())
807
35.5k
    return IfLBrace;
808
809
211k
  if (MacroBlock ? FormatTok->isNot(TT_MacroBlockEnd)
810
211k
                 : FormatTok->isNot(tok::r_brace)) {
811
0
    Line->Level = InitialLevel;
812
0
    FormatTok->setBlockKind(BK_Block);
813
0
    return IfLBrace;
814
0
  }
815
816
211k
  if (FormatTok->is(tok::r_brace) && Tok->is(TT_NamespaceLBrace))
817
0
    FormatTok->setFinalizedType(TT_NamespaceRBrace);
818
819
211k
  const bool IsFunctionRBrace =
820
211k
      FormatTok->is(tok::r_brace) && Tok->is(TT_FunctionLBrace);
821
822
211k
  auto RemoveBraces = [=]() mutable {
823
211k
    if (!SimpleBlock)
824
211k
      return false;
825
0
    assert(Tok->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace));
826
0
    assert(FormatTok->is(tok::r_brace));
827
0
    const bool WrappedOpeningBrace = !Tok->Previous;
828
0
    if (WrappedOpeningBrace && FollowedByComment)
829
0
      return false;
830
0
    const bool HasRequiredIfBraces = IfLBrace && !IfLBrace->Optional;
831
0
    if (KeepBraces && !HasRequiredIfBraces)
832
0
      return false;
833
0
    if (Tok->isNot(TT_ElseLBrace) || !HasRequiredIfBraces) {
834
0
      const FormatToken *Previous = Tokens->getPreviousToken();
835
0
      assert(Previous);
836
0
      if (Previous->is(tok::r_brace) && !Previous->Optional)
837
0
        return false;
838
0
    }
839
0
    assert(!CurrentLines->empty());
840
0
    auto &LastLine = CurrentLines->back();
841
0
    if (LastLine.Level == InitialLevel + 1 && !mightFitOnOneLine(LastLine))
842
0
      return false;
843
0
    if (Tok->is(TT_ElseLBrace))
844
0
      return true;
845
0
    if (WrappedOpeningBrace) {
846
0
      assert(Index > 0);
847
0
      --Index; // The line above the wrapped l_brace.
848
0
      Tok = nullptr;
849
0
    }
850
0
    return mightFitOnOneLine((*CurrentLines)[Index], Tok);
851
0
  };
852
211k
  if (RemoveBraces()) {
853
0
    Tok->MatchingParen = FormatTok;
854
0
    FormatTok->MatchingParen = Tok;
855
0
  }
856
857
211k
  size_t PPEndHash = computePPHash();
858
859
  // Munch the closing brace.
860
211k
  nextToken(/*LevelDifference=*/-AddLevels);
861
862
  // When this is a function block and there is an unnecessary semicolon
863
  // afterwards then mark it as optional (so the RemoveSemi pass can get rid of
864
  // it later).
865
211k
  if (Style.RemoveSemicolon && IsFunctionRBrace) {
866
0
    while (FormatTok->is(tok::semi)) {
867
0
      FormatTok->Optional = true;
868
0
      nextToken();
869
0
    }
870
0
  }
871
872
211k
  HandleVerilogBlockLabel();
873
874
211k
  if (MacroBlock && FormatTok->is(tok::l_paren))
875
0
    parseParens();
876
877
211k
  Line->Level = InitialLevel;
878
879
211k
  if (FormatTok->is(tok::kw_noexcept)) {
880
    // A noexcept in a requires expression.
881
0
    nextToken();
882
0
  }
883
884
211k
  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
0
    nextToken();
888
0
    parseStructuralElement();
889
0
  }
890
891
211k
  if (MunchSemi && FormatTok->is(tok::semi))
892
15
    nextToken();
893
894
211k
  if (PPStartHash == PPEndHash) {
895
211k
    Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
896
211k
    if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
897
      // Update the opening line to add the forward reference as well
898
211k
      (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
899
211k
          CurrentLines->size() - 1;
900
211k
    }
901
211k
  }
902
903
211k
  return IfLBrace;
904
211k
}
905
906
0
static bool isGoogScope(const UnwrappedLine &Line) {
907
  // FIXME: Closure-library specific stuff should not be hard-coded but be
908
  // configurable.
909
0
  if (Line.Tokens.size() < 4)
910
0
    return false;
911
0
  auto I = Line.Tokens.begin();
912
0
  if (I->Tok->TokenText != "goog")
913
0
    return false;
914
0
  ++I;
915
0
  if (I->Tok->isNot(tok::period))
916
0
    return false;
917
0
  ++I;
918
0
  if (I->Tok->TokenText != "scope")
919
0
    return false;
920
0
  ++I;
921
0
  return I->Tok->is(tok::l_paren);
922
0
}
923
924
static bool isIIFE(const UnwrappedLine &Line,
925
0
                   const AdditionalKeywords &Keywords) {
926
  // Look for the start of an immediately invoked anonymous function.
927
  // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
928
  // This is commonly done in JavaScript to create a new, anonymous scope.
929
  // Example: (function() { ... })()
930
0
  if (Line.Tokens.size() < 3)
931
0
    return false;
932
0
  auto I = Line.Tokens.begin();
933
0
  if (I->Tok->isNot(tok::l_paren))
934
0
    return false;
935
0
  ++I;
936
0
  if (I->Tok->isNot(Keywords.kw_function))
937
0
    return false;
938
0
  ++I;
939
0
  return I->Tok->is(tok::l_paren);
940
0
}
941
942
static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
943
156
                                   const FormatToken &InitialToken) {
944
156
  tok::TokenKind Kind = InitialToken.Tok.getKind();
945
156
  if (InitialToken.is(TT_NamespaceMacro))
946
0
    Kind = tok::kw_namespace;
947
948
156
  switch (Kind) {
949
0
  case tok::kw_namespace:
950
0
    return Style.BraceWrapping.AfterNamespace;
951
57
  case tok::kw_class:
952
57
    return Style.BraceWrapping.AfterClass;
953
0
  case tok::kw_union:
954
0
    return Style.BraceWrapping.AfterUnion;
955
99
  case tok::kw_struct:
956
99
    return Style.BraceWrapping.AfterStruct;
957
0
  case tok::kw_enum:
958
0
    return Style.BraceWrapping.AfterEnum;
959
0
  default:
960
0
    return false;
961
156
  }
962
156
}
963
964
21.7k
void UnwrappedLineParser::parseChildBlock() {
965
21.7k
  assert(FormatTok->is(tok::l_brace));
966
0
  FormatTok->setBlockKind(BK_Block);
967
21.7k
  const FormatToken *OpeningBrace = FormatTok;
968
21.7k
  nextToken();
969
21.7k
  {
970
21.7k
    bool SkipIndent = (Style.isJavaScript() &&
971
21.7k
                       (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
972
21.7k
    ScopedLineState LineState(*this);
973
21.7k
    ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
974
21.7k
                                            /*MustBeDeclaration=*/false);
975
21.7k
    Line->Level += SkipIndent ? 0 : 1;
976
21.7k
    parseLevel(OpeningBrace);
977
21.7k
    flushComments(isOnNewLine(*FormatTok));
978
21.7k
    Line->Level -= SkipIndent ? 0 : 1;
979
21.7k
  }
980
21.7k
  nextToken();
981
21.7k
}
982
983
253k
void UnwrappedLineParser::parsePPDirective() {
984
253k
  assert(FormatTok->is(tok::hash) && "'#' expected");
985
0
  ScopedMacroState MacroState(*Line, Tokens, FormatTok);
986
987
253k
  nextToken();
988
989
253k
  if (!FormatTok->Tok.getIdentifierInfo()) {
990
228k
    parsePPUnknown();
991
228k
    return;
992
228k
  }
993
994
25.4k
  switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
995
2.03k
  case tok::pp_define:
996
2.03k
    parsePPDefine();
997
2.03k
    return;
998
150
  case tok::pp_if:
999
150
    parsePPIf(/*IfDef=*/false);
1000
150
    break;
1001
201
  case tok::pp_ifdef:
1002
555
  case tok::pp_ifndef:
1003
555
    parsePPIf(/*IfDef=*/true);
1004
555
    break;
1005
168
  case tok::pp_else:
1006
168
  case tok::pp_elifdef:
1007
168
  case tok::pp_elifndef:
1008
243
  case tok::pp_elif:
1009
243
    parsePPElse();
1010
243
    break;
1011
591
  case tok::pp_endif:
1012
591
    parsePPEndIf();
1013
591
    break;
1014
117
  case tok::pp_pragma:
1015
117
    parsePPPragma();
1016
117
    break;
1017
21.8k
  default:
1018
21.8k
    parsePPUnknown();
1019
21.8k
    break;
1020
25.4k
  }
1021
25.4k
}
1022
1023
262k
void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
1024
262k
  size_t Line = CurrentLines->size();
1025
262k
  if (CurrentLines == &PreprocessorDirectives)
1026
972
    Line += Lines.size();
1027
1028
262k
  if (Unreachable ||
1029
262k
      (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable)) {
1030
825
    PPStack.push_back({PP_Unreachable, Line});
1031
261k
  } else {
1032
261k
    PPStack.push_back({PP_Conditional, Line});
1033
261k
  }
1034
262k
}
1035
1036
261k
void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
1037
261k
  ++PPBranchLevel;
1038
261k
  assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
1039
261k
  if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
1040
261k
    PPLevelBranchIndex.push_back(0);
1041
261k
    PPLevelBranchCount.push_back(0);
1042
261k
  }
1043
261k
  PPChainBranchIndex.push(Unreachable ? -1 : 0);
1044
261k
  bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
1045
261k
  conditionalCompilationCondition(Unreachable || Skip);
1046
261k
}
1047
1048
369
void UnwrappedLineParser::conditionalCompilationAlternative() {
1049
369
  if (!PPStack.empty())
1050
360
    PPStack.pop_back();
1051
369
  assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
1052
369
  if (!PPChainBranchIndex.empty())
1053
366
    ++PPChainBranchIndex.top();
1054
369
  conditionalCompilationCondition(
1055
369
      PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
1056
369
      PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
1057
369
}
1058
1059
762
void UnwrappedLineParser::conditionalCompilationEnd() {
1060
762
  assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
1061
762
  if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
1062
609
    if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel])
1063
135
      PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
1064
609
  }
1065
  // Guard against #endif's without #if.
1066
762
  if (PPBranchLevel > -1)
1067
609
    --PPBranchLevel;
1068
762
  if (!PPChainBranchIndex.empty())
1069
621
    PPChainBranchIndex.pop();
1070
762
  if (!PPStack.empty())
1071
609
    PPStack.pop_back();
1072
762
}
1073
1074
705
void UnwrappedLineParser::parsePPIf(bool IfDef) {
1075
705
  bool IfNDef = FormatTok->is(tok::pp_ifndef);
1076
705
  nextToken();
1077
705
  bool Unreachable = false;
1078
705
  if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
1079
15
    Unreachable = true;
1080
705
  if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
1081
0
    Unreachable = true;
1082
705
  conditionalCompilationStart(Unreachable);
1083
705
  FormatToken *IfCondition = FormatTok;
1084
  // If there's a #ifndef on the first line, and the only lines before it are
1085
  // comments, it could be an include guard.
1086
705
  bool MaybeIncludeGuard = IfNDef;
1087
705
  if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
1088
0
    for (auto &Line : Lines) {
1089
0
      if (Line.Tokens.front().Tok->isNot(tok::comment)) {
1090
0
        MaybeIncludeGuard = false;
1091
0
        IncludeGuard = IG_Rejected;
1092
0
        break;
1093
0
      }
1094
0
    }
1095
0
  }
1096
705
  --PPBranchLevel;
1097
705
  parsePPUnknown();
1098
705
  ++PPBranchLevel;
1099
705
  if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
1100
0
    IncludeGuard = IG_IfNdefed;
1101
0
    IncludeGuardToken = IfCondition;
1102
0
  }
1103
705
}
1104
1105
243
void UnwrappedLineParser::parsePPElse() {
1106
  // If a potential include guard has an #else, it's not an include guard.
1107
243
  if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
1108
0
    IncludeGuard = IG_Rejected;
1109
  // Don't crash when there is an #else without an #if.
1110
243
  assert(PPBranchLevel >= -1);
1111
243
  if (PPBranchLevel == -1)
1112
24
    conditionalCompilationStart(/*Unreachable=*/true);
1113
243
  conditionalCompilationAlternative();
1114
243
  --PPBranchLevel;
1115
243
  parsePPUnknown();
1116
243
  ++PPBranchLevel;
1117
243
}
1118
1119
591
void UnwrappedLineParser::parsePPEndIf() {
1120
591
  conditionalCompilationEnd();
1121
591
  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
591
  if (IncludeGuard == IG_Defined && PPBranchLevel == -1 && Tokens->isEOF() &&
1125
591
      Style.IndentPPDirectives != FormatStyle::PPDIS_None) {
1126
0
    IncludeGuard = IG_Found;
1127
0
  }
1128
591
}
1129
1130
2.03k
void UnwrappedLineParser::parsePPDefine() {
1131
2.03k
  nextToken();
1132
1133
2.03k
  if (!FormatTok->Tok.getIdentifierInfo()) {
1134
6
    IncludeGuard = IG_Rejected;
1135
6
    IncludeGuardToken = nullptr;
1136
6
    parsePPUnknown();
1137
6
    return;
1138
6
  }
1139
1140
2.02k
  if (IncludeGuard == IG_IfNdefed &&
1141
2.02k
      IncludeGuardToken->TokenText == FormatTok->TokenText) {
1142
0
    IncludeGuard = IG_Defined;
1143
0
    IncludeGuardToken = nullptr;
1144
0
    for (auto &Line : Lines) {
1145
0
      if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
1146
0
        IncludeGuard = IG_Rejected;
1147
0
        break;
1148
0
      }
1149
0
    }
1150
0
  }
1151
1152
  // In the context of a define, even keywords should be treated as normal
1153
  // identifiers. Setting the kind to identifier is not enough, because we need
1154
  // to treat additional keywords like __except as well, which are already
1155
  // identifiers. Setting the identifier info to null interferes with include
1156
  // guard processing above, and changes preprocessing nesting.
1157
2.02k
  FormatTok->Tok.setKind(tok::identifier);
1158
2.02k
  FormatTok->Tok.setIdentifierInfo(Keywords.kw_internal_ident_after_define);
1159
2.02k
  nextToken();
1160
2.02k
  if (FormatTok->Tok.getKind() == tok::l_paren &&
1161
2.02k
      !FormatTok->hasWhitespaceBefore()) {
1162
192
    parseParens();
1163
192
  }
1164
2.02k
  if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
1165
0
    Line->Level += PPBranchLevel + 1;
1166
2.02k
  addUnwrappedLine();
1167
2.02k
  ++Line->Level;
1168
1169
2.02k
  Line->PPLevel = PPBranchLevel + (IncludeGuard == IG_Defined ? 0 : 1);
1170
2.02k
  assert((int)Line->PPLevel >= 0);
1171
0
  Line->InMacroBody = true;
1172
1173
2.02k
  if (FormatTok->is(tok::identifier) &&
1174
2.02k
      Tokens->peekNextToken()->is(tok::colon)) {
1175
0
    nextToken();
1176
0
    nextToken();
1177
0
  }
1178
1179
  // Errors during a preprocessor directive can only affect the layout of the
1180
  // preprocessor directive, and thus we ignore them. An alternative approach
1181
  // would be to use the same approach we use on the file level (no
1182
  // re-indentation if there was a structural error) within the macro
1183
  // definition.
1184
2.02k
  parseFile();
1185
2.02k
}
1186
1187
117
void UnwrappedLineParser::parsePPPragma() {
1188
117
  Line->InPragmaDirective = true;
1189
117
  parsePPUnknown();
1190
117
}
1191
1192
251k
void UnwrappedLineParser::parsePPUnknown() {
1193
820k
  do {
1194
820k
    nextToken();
1195
820k
  } while (!eof());
1196
251k
  if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
1197
0
    Line->Level += PPBranchLevel + 1;
1198
251k
  addUnwrappedLine();
1199
251k
}
1200
1201
// Here we exclude certain tokens that are not usually the first token in an
1202
// unwrapped line. This is used in attempt to distinguish macro calls without
1203
// trailing semicolons from other constructs split to several lines.
1204
4.63k
static bool tokenCanStartNewLine(const FormatToken &Tok) {
1205
  // Semicolon can be a null-statement, l_square can be a start of a macro or
1206
  // a C++11 attribute, but this doesn't seem to be common.
1207
4.63k
  assert(Tok.isNot(TT_AttributeSquare));
1208
0
  return !Tok.isOneOf(tok::semi, tok::l_brace,
1209
                      // Tokens that can only be used as binary operators and a
1210
                      // part of overloaded operator names.
1211
4.63k
                      tok::period, tok::periodstar, tok::arrow, tok::arrowstar,
1212
4.63k
                      tok::less, tok::greater, tok::slash, tok::percent,
1213
4.63k
                      tok::lessless, tok::greatergreater, tok::equal,
1214
4.63k
                      tok::plusequal, tok::minusequal, tok::starequal,
1215
4.63k
                      tok::slashequal, tok::percentequal, tok::ampequal,
1216
4.63k
                      tok::pipeequal, tok::caretequal, tok::greatergreaterequal,
1217
4.63k
                      tok::lesslessequal,
1218
                      // Colon is used in labels, base class lists, initializer
1219
                      // lists, range-based for loops, ternary operator, but
1220
                      // should never be the first token in an unwrapped line.
1221
4.63k
                      tok::colon,
1222
                      // 'noexcept' is a trailing annotation.
1223
4.63k
                      tok::kw_noexcept);
1224
4.63k
}
1225
1226
static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
1227
0
                          const FormatToken *FormatTok) {
1228
  // FIXME: This returns true for C/C++ keywords like 'struct'.
1229
0
  return FormatTok->is(tok::identifier) &&
1230
0
         (!FormatTok->Tok.getIdentifierInfo() ||
1231
0
          !FormatTok->isOneOf(
1232
0
              Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
1233
0
              Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
1234
0
              Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
1235
0
              Keywords.kw_let, Keywords.kw_var, tok::kw_const,
1236
0
              Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
1237
0
              Keywords.kw_instanceof, Keywords.kw_interface,
1238
0
              Keywords.kw_override, Keywords.kw_throws, Keywords.kw_from));
1239
0
}
1240
1241
static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
1242
0
                                 const FormatToken *FormatTok) {
1243
0
  return FormatTok->Tok.isLiteral() ||
1244
0
         FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
1245
0
         mustBeJSIdent(Keywords, FormatTok);
1246
0
}
1247
1248
// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
1249
// when encountered after a value (see mustBeJSIdentOrValue).
1250
static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
1251
0
                           const FormatToken *FormatTok) {
1252
0
  return FormatTok->isOneOf(
1253
0
      tok::kw_return, Keywords.kw_yield,
1254
      // conditionals
1255
0
      tok::kw_if, tok::kw_else,
1256
      // loops
1257
0
      tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
1258
      // switch/case
1259
0
      tok::kw_switch, tok::kw_case,
1260
      // exceptions
1261
0
      tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
1262
      // declaration
1263
0
      tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
1264
0
      Keywords.kw_async, Keywords.kw_function,
1265
      // import/export
1266
0
      Keywords.kw_import, tok::kw_export);
1267
0
}
1268
1269
// Checks whether a token is a type in K&R C (aka C78).
1270
26.3k
static bool isC78Type(const FormatToken &Tok) {
1271
26.3k
  return Tok.isOneOf(tok::kw_char, tok::kw_short, tok::kw_int, tok::kw_long,
1272
26.3k
                     tok::kw_unsigned, tok::kw_float, tok::kw_double,
1273
26.3k
                     tok::identifier);
1274
26.3k
}
1275
1276
// This function checks whether a token starts the first parameter declaration
1277
// in a K&R C (aka C78) function definition, e.g.:
1278
//   int f(a, b)
1279
//   short a, b;
1280
//   {
1281
//      return a + b;
1282
//   }
1283
static bool isC78ParameterDecl(const FormatToken *Tok, const FormatToken *Next,
1284
21.4k
                               const FormatToken *FuncName) {
1285
21.4k
  assert(Tok);
1286
0
  assert(Next);
1287
0
  assert(FuncName);
1288
1289
21.4k
  if (FuncName->isNot(tok::identifier))
1290
5.07k
    return false;
1291
1292
16.4k
  const FormatToken *Prev = FuncName->Previous;
1293
16.4k
  if (!Prev || (Prev->isNot(tok::star) && !isC78Type(*Prev)))
1294
6.44k
    return false;
1295
1296
9.96k
  if (!isC78Type(*Tok) &&
1297
9.96k
      !Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union)) {
1298
9.04k
    return false;
1299
9.04k
  }
1300
1301
921
  if (Next->isNot(tok::star) && !Next->Tok.getIdentifierInfo())
1302
213
    return false;
1303
1304
708
  Tok = Tok->Previous;
1305
708
  if (!Tok || Tok->isNot(tok::r_paren))
1306
0
    return false;
1307
1308
708
  Tok = Tok->Previous;
1309
708
  if (!Tok || Tok->isNot(tok::identifier))
1310
24
    return false;
1311
1312
684
  return Tok->Previous && Tok->Previous->isOneOf(tok::l_paren, tok::comma);
1313
708
}
1314
1315
0
bool UnwrappedLineParser::parseModuleImport() {
1316
0
  assert(FormatTok->is(Keywords.kw_import) && "'import' expected");
1317
1318
0
  if (auto Token = Tokens->peekNextToken(/*SkipComment=*/true);
1319
0
      !Token->Tok.getIdentifierInfo() &&
1320
0
      !Token->isOneOf(tok::colon, tok::less, tok::string_literal)) {
1321
0
    return false;
1322
0
  }
1323
1324
0
  nextToken();
1325
0
  while (!eof()) {
1326
0
    if (FormatTok->is(tok::colon)) {
1327
0
      FormatTok->setFinalizedType(TT_ModulePartitionColon);
1328
0
    }
1329
    // Handle import <foo/bar.h> as we would an include statement.
1330
0
    else if (FormatTok->is(tok::less)) {
1331
0
      nextToken();
1332
0
      while (!FormatTok->isOneOf(tok::semi, tok::greater, tok::eof)) {
1333
        // Mark tokens up to the trailing line comments as implicit string
1334
        // literals.
1335
0
        if (FormatTok->isNot(tok::comment) &&
1336
0
            !FormatTok->TokenText.starts_with("//")) {
1337
0
          FormatTok->setFinalizedType(TT_ImplicitStringLiteral);
1338
0
        }
1339
0
        nextToken();
1340
0
      }
1341
0
    }
1342
0
    if (FormatTok->is(tok::semi)) {
1343
0
      nextToken();
1344
0
      break;
1345
0
    }
1346
0
    nextToken();
1347
0
  }
1348
1349
0
  addUnwrappedLine();
1350
0
  return true;
1351
0
}
1352
1353
// readTokenWithJavaScriptASI reads the next token and terminates the current
1354
// line if JavaScript Automatic Semicolon Insertion must
1355
// happen between the current token and the next token.
1356
//
1357
// This method is conservative - it cannot cover all edge cases of JavaScript,
1358
// but only aims to correctly handle certain well known cases. It *must not*
1359
// return true in speculative cases.
1360
0
void UnwrappedLineParser::readTokenWithJavaScriptASI() {
1361
0
  FormatToken *Previous = FormatTok;
1362
0
  readToken();
1363
0
  FormatToken *Next = FormatTok;
1364
1365
0
  bool IsOnSameLine =
1366
0
      CommentsBeforeNextToken.empty()
1367
0
          ? Next->NewlinesBefore == 0
1368
0
          : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
1369
0
  if (IsOnSameLine)
1370
0
    return;
1371
1372
0
  bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
1373
0
  bool PreviousStartsTemplateExpr =
1374
0
      Previous->is(TT_TemplateString) && Previous->TokenText.ends_with("${");
1375
0
  if (PreviousMustBeValue || Previous->is(tok::r_paren)) {
1376
    // If the line contains an '@' sign, the previous token might be an
1377
    // annotation, which can precede another identifier/value.
1378
0
    bool HasAt = llvm::any_of(Line->Tokens, [](UnwrappedLineNode &LineNode) {
1379
0
      return LineNode.Tok->is(tok::at);
1380
0
    });
1381
0
    if (HasAt)
1382
0
      return;
1383
0
  }
1384
0
  if (Next->is(tok::exclaim) && PreviousMustBeValue)
1385
0
    return addUnwrappedLine();
1386
0
  bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
1387
0
  bool NextEndsTemplateExpr =
1388
0
      Next->is(TT_TemplateString) && Next->TokenText.starts_with("}");
1389
0
  if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
1390
0
      (PreviousMustBeValue ||
1391
0
       Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
1392
0
                         tok::minusminus))) {
1393
0
    return addUnwrappedLine();
1394
0
  }
1395
0
  if ((PreviousMustBeValue || Previous->is(tok::r_paren)) &&
1396
0
      isJSDeclOrStmt(Keywords, Next)) {
1397
0
    return addUnwrappedLine();
1398
0
  }
1399
0
}
1400
1401
void UnwrappedLineParser::parseStructuralElement(
1402
    const FormatToken *OpeningBrace, IfStmtKind *IfKind,
1403
768k
    FormatToken **IfLeftBrace, bool *HasDoWhile, bool *HasLabel) {
1404
768k
  if (Style.Language == FormatStyle::LK_TableGen &&
1405
768k
      FormatTok->is(tok::pp_include)) {
1406
0
    nextToken();
1407
0
    if (FormatTok->is(tok::string_literal))
1408
0
      nextToken();
1409
0
    addUnwrappedLine();
1410
0
    return;
1411
0
  }
1412
1413
768k
  if (Style.isCpp()) {
1414
768k
    while (FormatTok->is(tok::l_square) && handleCppAttributes()) {
1415
0
    }
1416
768k
  } else if (Style.isVerilog()) {
1417
0
    if (Keywords.isVerilogStructuredProcedure(*FormatTok)) {
1418
0
      parseForOrWhileLoop(/*HasParens=*/false);
1419
0
      return;
1420
0
    }
1421
0
    if (FormatTok->isOneOf(Keywords.kw_foreach, Keywords.kw_repeat)) {
1422
0
      parseForOrWhileLoop();
1423
0
      return;
1424
0
    }
1425
0
    if (FormatTok->isOneOf(tok::kw_restrict, Keywords.kw_assert,
1426
0
                           Keywords.kw_assume, Keywords.kw_cover)) {
1427
0
      parseIfThenElse(IfKind, /*KeepBraces=*/false, /*IsVerilogAssert=*/true);
1428
0
      return;
1429
0
    }
1430
1431
    // Skip things that can exist before keywords like 'if' and 'case'.
1432
0
    while (true) {
1433
0
      if (FormatTok->isOneOf(Keywords.kw_priority, Keywords.kw_unique,
1434
0
                             Keywords.kw_unique0)) {
1435
0
        nextToken();
1436
0
      } else if (FormatTok->is(tok::l_paren) &&
1437
0
                 Tokens->peekNextToken()->is(tok::star)) {
1438
0
        parseParens();
1439
0
      } else {
1440
0
        break;
1441
0
      }
1442
0
    }
1443
0
  }
1444
1445
  // Tokens that only make sense at the beginning of a line.
1446
768k
  switch (FormatTok->Tok.getKind()) {
1447
0
  case tok::kw_asm:
1448
0
    nextToken();
1449
0
    if (FormatTok->is(tok::l_brace)) {
1450
0
      FormatTok->setFinalizedType(TT_InlineASMBrace);
1451
0
      nextToken();
1452
0
      while (FormatTok && !eof()) {
1453
0
        if (FormatTok->is(tok::r_brace)) {
1454
0
          FormatTok->setFinalizedType(TT_InlineASMBrace);
1455
0
          nextToken();
1456
0
          addUnwrappedLine();
1457
0
          break;
1458
0
        }
1459
0
        FormatTok->Finalized = true;
1460
0
        nextToken();
1461
0
      }
1462
0
    }
1463
0
    break;
1464
0
  case tok::kw_namespace:
1465
0
    parseNamespace();
1466
0
    return;
1467
0
  case tok::kw_public:
1468
0
  case tok::kw_protected:
1469
0
  case tok::kw_private:
1470
0
    if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() ||
1471
0
        Style.isCSharp()) {
1472
0
      nextToken();
1473
0
    } else {
1474
0
      parseAccessSpecifier();
1475
0
    }
1476
0
    return;
1477
5.76k
  case tok::kw_if: {
1478
5.76k
    if (Style.isJavaScript() && Line->MustBeDeclaration) {
1479
      // field/method declaration.
1480
0
      break;
1481
0
    }
1482
5.76k
    FormatToken *Tok = parseIfThenElse(IfKind);
1483
5.76k
    if (IfLeftBrace)
1484
5.76k
      *IfLeftBrace = Tok;
1485
5.76k
    return;
1486
5.76k
  }
1487
294
  case tok::kw_for:
1488
381
  case tok::kw_while:
1489
381
    if (Style.isJavaScript() && Line->MustBeDeclaration) {
1490
      // field/method declaration.
1491
0
      break;
1492
0
    }
1493
381
    parseForOrWhileLoop();
1494
381
    return;
1495
66
  case tok::kw_do:
1496
66
    if (Style.isJavaScript() && Line->MustBeDeclaration) {
1497
      // field/method declaration.
1498
0
      break;
1499
0
    }
1500
66
    parseDoWhile();
1501
66
    if (HasDoWhile)
1502
66
      *HasDoWhile = true;
1503
66
    return;
1504
0
  case tok::kw_switch:
1505
0
    if (Style.isJavaScript() && Line->MustBeDeclaration) {
1506
      // 'switch: string' field declaration.
1507
0
      break;
1508
0
    }
1509
0
    parseSwitch();
1510
0
    return;
1511
0
  case tok::kw_default:
1512
    // In Verilog default along with other labels are handled in the next loop.
1513
0
    if (Style.isVerilog())
1514
0
      break;
1515
0
    if (Style.isJavaScript() && Line->MustBeDeclaration) {
1516
      // 'default: string' field declaration.
1517
0
      break;
1518
0
    }
1519
0
    nextToken();
1520
0
    if (FormatTok->is(tok::colon)) {
1521
0
      FormatTok->setFinalizedType(TT_CaseLabelColon);
1522
0
      parseLabel();
1523
0
      return;
1524
0
    }
1525
    // e.g. "default void f() {}" in a Java interface.
1526
0
    break;
1527
0
  case tok::kw_case:
1528
    // Proto: there are no switch/case statements.
1529
0
    if (Style.Language == FormatStyle::LK_Proto) {
1530
0
      nextToken();
1531
0
      return;
1532
0
    }
1533
0
    if (Style.isVerilog()) {
1534
0
      parseBlock();
1535
0
      addUnwrappedLine();
1536
0
      return;
1537
0
    }
1538
0
    if (Style.isJavaScript() && Line->MustBeDeclaration) {
1539
      // 'case: string' field declaration.
1540
0
      nextToken();
1541
0
      break;
1542
0
    }
1543
0
    parseCaseLabel();
1544
0
    return;
1545
0
  case tok::kw_try:
1546
0
  case tok::kw___try:
1547
0
    if (Style.isJavaScript() && Line->MustBeDeclaration) {
1548
      // field/method declaration.
1549
0
      break;
1550
0
    }
1551
0
    parseTryCatch();
1552
0
    return;
1553
3
  case tok::kw_extern:
1554
3
    nextToken();
1555
3
    if (Style.isVerilog()) {
1556
      // In Verilog and extern module declaration looks like a start of module.
1557
      // But there is no body and endmodule. So we handle it separately.
1558
0
      if (Keywords.isVerilogHierarchy(*FormatTok)) {
1559
0
        parseVerilogHierarchyHeader();
1560
0
        return;
1561
0
      }
1562
3
    } else if (FormatTok->is(tok::string_literal)) {
1563
3
      nextToken();
1564
3
      if (FormatTok->is(tok::l_brace)) {
1565
3
        if (Style.BraceWrapping.AfterExternBlock)
1566
0
          addUnwrappedLine();
1567
        // Either we indent or for backwards compatibility we follow the
1568
        // AfterExternBlock style.
1569
3
        unsigned AddLevels =
1570
3
            (Style.IndentExternBlock == FormatStyle::IEBS_Indent) ||
1571
3
                    (Style.BraceWrapping.AfterExternBlock &&
1572
3
                     Style.IndentExternBlock ==
1573
0
                         FormatStyle::IEBS_AfterExternBlock)
1574
3
                ? 1u
1575
3
                : 0u;
1576
3
        parseBlock(/*MustBeDeclaration=*/true, AddLevels);
1577
3
        addUnwrappedLine();
1578
3
        return;
1579
3
      }
1580
3
    }
1581
0
    break;
1582
0
  case tok::kw_export:
1583
0
    if (Style.isJavaScript()) {
1584
0
      parseJavaScriptEs6ImportExport();
1585
0
      return;
1586
0
    }
1587
0
    if (Style.isCpp()) {
1588
0
      nextToken();
1589
0
      if (FormatTok->is(tok::kw_namespace)) {
1590
0
        parseNamespace();
1591
0
        return;
1592
0
      }
1593
0
      if (FormatTok->is(Keywords.kw_import) && parseModuleImport())
1594
0
        return;
1595
0
    }
1596
0
    break;
1597
0
  case tok::kw_inline:
1598
0
    nextToken();
1599
0
    if (FormatTok->is(tok::kw_namespace)) {
1600
0
      parseNamespace();
1601
0
      return;
1602
0
    }
1603
0
    break;
1604
390k
  case tok::identifier:
1605
390k
    if (FormatTok->is(TT_ForEachMacro)) {
1606
0
      parseForOrWhileLoop();
1607
0
      return;
1608
0
    }
1609
390k
    if (FormatTok->is(TT_MacroBlockBegin)) {
1610
0
      parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
1611
0
                 /*MunchSemi=*/false);
1612
0
      return;
1613
0
    }
1614
390k
    if (FormatTok->is(Keywords.kw_import)) {
1615
0
      if (Style.isJavaScript()) {
1616
0
        parseJavaScriptEs6ImportExport();
1617
0
        return;
1618
0
      }
1619
0
      if (Style.Language == FormatStyle::LK_Proto) {
1620
0
        nextToken();
1621
0
        if (FormatTok->is(tok::kw_public))
1622
0
          nextToken();
1623
0
        if (FormatTok->isNot(tok::string_literal))
1624
0
          return;
1625
0
        nextToken();
1626
0
        if (FormatTok->is(tok::semi))
1627
0
          nextToken();
1628
0
        addUnwrappedLine();
1629
0
        return;
1630
0
      }
1631
0
      if (Style.isCpp() && parseModuleImport())
1632
0
        return;
1633
0
    }
1634
390k
    if (Style.isCpp() &&
1635
390k
        FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
1636
390k
                           Keywords.kw_slots, Keywords.kw_qslots)) {
1637
0
      nextToken();
1638
0
      if (FormatTok->is(tok::colon)) {
1639
0
        nextToken();
1640
0
        addUnwrappedLine();
1641
0
        return;
1642
0
      }
1643
0
    }
1644
390k
    if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) {
1645
0
      parseStatementMacro();
1646
0
      return;
1647
0
    }
1648
390k
    if (Style.isCpp() && FormatTok->is(TT_NamespaceMacro)) {
1649
0
      parseNamespace();
1650
0
      return;
1651
0
    }
1652
    // In Verilog labels can be any expression, so we don't do them here.
1653
    // JS doesn't have macros, and within classes colons indicate fields, not
1654
    // labels.
1655
390k
    if (!Style.isJavaScript() && !Style.isVerilog() &&
1656
390k
        Tokens->peekNextToken()->is(tok::colon) && !Line->MustBeDeclaration) {
1657
42.5k
      nextToken();
1658
42.5k
      Line->Tokens.begin()->Tok->MustBreakBefore = true;
1659
42.5k
      FormatTok->setFinalizedType(TT_GotoLabelColon);
1660
42.5k
      parseLabel(!Style.IndentGotoLabels);
1661
42.5k
      if (HasLabel)
1662
33.5k
        *HasLabel = true;
1663
42.5k
      return;
1664
42.5k
    }
1665
    // In all other cases, parse the declaration.
1666
347k
    break;
1667
372k
  default:
1668
372k
    break;
1669
768k
  }
1670
1671
719k
  const bool InRequiresExpression =
1672
719k
      OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
1673
18.6M
  do {
1674
18.6M
    const FormatToken *Previous = FormatTok->Previous;
1675
18.6M
    switch (FormatTok->Tok.getKind()) {
1676
14.6k
    case tok::at:
1677
14.6k
      nextToken();
1678
14.6k
      if (FormatTok->is(tok::l_brace)) {
1679
21
        nextToken();
1680
21
        parseBracedList();
1681
21
        break;
1682
14.6k
      } else if (Style.Language == FormatStyle::LK_Java &&
1683
14.6k
                 FormatTok->is(Keywords.kw_interface)) {
1684
0
        nextToken();
1685
0
        break;
1686
0
      }
1687
14.6k
      switch (FormatTok->Tok.getObjCKeywordID()) {
1688
0
      case tok::objc_public:
1689
0
      case tok::objc_protected:
1690
0
      case tok::objc_package:
1691
0
      case tok::objc_private:
1692
0
        return parseAccessSpecifier();
1693
0
      case tok::objc_interface:
1694
0
      case tok::objc_implementation:
1695
0
        return parseObjCInterfaceOrImplementation();
1696
0
      case tok::objc_protocol:
1697
0
        if (parseObjCProtocol())
1698
0
          return;
1699
0
        break;
1700
0
      case tok::objc_end:
1701
0
        return; // Handled by the caller.
1702
0
      case tok::objc_optional:
1703
0
      case tok::objc_required:
1704
0
        nextToken();
1705
0
        addUnwrappedLine();
1706
0
        return;
1707
0
      case tok::objc_autoreleasepool:
1708
0
        nextToken();
1709
0
        if (FormatTok->is(tok::l_brace)) {
1710
0
          if (Style.BraceWrapping.AfterControlStatement ==
1711
0
              FormatStyle::BWACS_Always) {
1712
0
            addUnwrappedLine();
1713
0
          }
1714
0
          parseBlock();
1715
0
        }
1716
0
        addUnwrappedLine();
1717
0
        return;
1718
0
      case tok::objc_synchronized:
1719
0
        nextToken();
1720
0
        if (FormatTok->is(tok::l_paren)) {
1721
          // Skip synchronization object
1722
0
          parseParens();
1723
0
        }
1724
0
        if (FormatTok->is(tok::l_brace)) {
1725
0
          if (Style.BraceWrapping.AfterControlStatement ==
1726
0
              FormatStyle::BWACS_Always) {
1727
0
            addUnwrappedLine();
1728
0
          }
1729
0
          parseBlock();
1730
0
        }
1731
0
        addUnwrappedLine();
1732
0
        return;
1733
0
      case tok::objc_try:
1734
        // This branch isn't strictly necessary (the kw_try case below would
1735
        // do this too after the tok::at is parsed above).  But be explicit.
1736
0
        parseTryCatch();
1737
0
        return;
1738
14.6k
      default:
1739
14.6k
        break;
1740
14.6k
      }
1741
14.6k
      break;
1742
14.6k
    case tok::kw_requires: {
1743
0
      if (Style.isCpp()) {
1744
0
        bool ParsedClause = parseRequires();
1745
0
        if (ParsedClause)
1746
0
          return;
1747
0
      } else {
1748
0
        nextToken();
1749
0
      }
1750
0
      break;
1751
0
    }
1752
192
    case tok::kw_enum:
1753
      // Ignore if this is part of "template <enum ...".
1754
192
      if (Previous && Previous->is(tok::less)) {
1755
0
        nextToken();
1756
0
        break;
1757
0
      }
1758
1759
      // parseEnum falls through and does not yet add an unwrapped line as an
1760
      // enum definition can start a structural element.
1761
192
      if (!parseEnum())
1762
0
        break;
1763
      // This only applies to C++ and Verilog.
1764
192
      if (!Style.isCpp() && !Style.isVerilog()) {
1765
0
        addUnwrappedLine();
1766
0
        return;
1767
0
      }
1768
192
      break;
1769
216
    case tok::kw_typedef:
1770
216
      nextToken();
1771
216
      if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1772
216
                             Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS,
1773
216
                             Keywords.kw_CF_CLOSED_ENUM,
1774
216
                             Keywords.kw_NS_CLOSED_ENUM)) {
1775
0
        parseEnum();
1776
0
      }
1777
216
      break;
1778
921
    case tok::kw_class:
1779
921
      if (Style.isVerilog()) {
1780
0
        parseBlock();
1781
0
        addUnwrappedLine();
1782
0
        return;
1783
0
      }
1784
921
      [[fallthrough]];
1785
1.02k
    case tok::kw_struct:
1786
1.02k
    case tok::kw_union:
1787
1.02k
      if (parseStructLike())
1788
0
        return;
1789
1.02k
      break;
1790
1.02k
    case tok::kw_decltype:
1791
0
      nextToken();
1792
0
      if (FormatTok->is(tok::l_paren)) {
1793
0
        parseParens();
1794
0
        assert(FormatTok->Previous);
1795
0
        if (FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
1796
0
                                              tok::l_paren)) {
1797
0
          Line->SeenDecltypeAuto = true;
1798
0
        }
1799
0
      }
1800
0
      break;
1801
243k
    case tok::period:
1802
243k
      nextToken();
1803
      // In Java, classes have an implicit static member "class".
1804
243k
      if (Style.Language == FormatStyle::LK_Java && FormatTok &&
1805
243k
          FormatTok->is(tok::kw_class)) {
1806
0
        nextToken();
1807
0
      }
1808
243k
      if (Style.isJavaScript() && FormatTok &&
1809
243k
          FormatTok->Tok.getIdentifierInfo()) {
1810
        // JavaScript only has pseudo keywords, all keywords are allowed to
1811
        // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1812
0
        nextToken();
1813
0
      }
1814
243k
      break;
1815
208k
    case tok::semi:
1816
208k
      nextToken();
1817
208k
      addUnwrappedLine();
1818
208k
      return;
1819
131k
    case tok::r_brace:
1820
131k
      addUnwrappedLine();
1821
131k
      return;
1822
48.4k
    case tok::l_paren: {
1823
48.4k
      parseParens();
1824
      // Break the unwrapped line if a K&R C function definition has a parameter
1825
      // declaration.
1826
48.4k
      if (OpeningBrace || !Style.isCpp() || !Previous || eof())
1827
26.9k
        break;
1828
21.4k
      if (isC78ParameterDecl(FormatTok,
1829
21.4k
                             Tokens->peekNextToken(/*SkipComment=*/true),
1830
21.4k
                             Previous)) {
1831
0
        addUnwrappedLine();
1832
0
        return;
1833
0
      }
1834
21.4k
      break;
1835
21.4k
    }
1836
21.4k
    case tok::kw_operator:
1837
0
      nextToken();
1838
0
      if (FormatTok->isBinaryOperator())
1839
0
        nextToken();
1840
0
      break;
1841
7.35k
    case tok::caret:
1842
7.35k
      nextToken();
1843
      // Block return type.
1844
7.35k
      if (FormatTok->Tok.isAnyIdentifier() ||
1845
7.35k
          FormatTok->isSimpleTypeSpecifier()) {
1846
936
        nextToken();
1847
        // Return types: pointers are ok too.
1848
936
        while (FormatTok->is(tok::star))
1849
0
          nextToken();
1850
936
      }
1851
      // Block argument list.
1852
7.35k
      if (FormatTok->is(tok::l_paren))
1853
3
        parseParens();
1854
      // Block body.
1855
7.35k
      if (FormatTok->is(tok::l_brace))
1856
0
        parseChildBlock();
1857
7.35k
      break;
1858
407k
    case tok::l_brace:
1859
407k
      if (InRequiresExpression)
1860
0
        FormatTok->setFinalizedType(TT_BracedListLBrace);
1861
407k
      if (!tryToParsePropertyAccessor() && !tryToParseBracedList()) {
1862
334k
        IsDecltypeAutoFunction = Line->SeenDecltypeAuto;
1863
        // A block outside of parentheses must be the last part of a
1864
        // structural element.
1865
        // FIXME: Figure out cases where this is not true, and add projections
1866
        // for them (the one we know is missing are lambdas).
1867
334k
        if (Style.Language == FormatStyle::LK_Java &&
1868
334k
            Line->Tokens.front().Tok->is(Keywords.kw_synchronized)) {
1869
          // If necessary, we could set the type to something different than
1870
          // TT_FunctionLBrace.
1871
0
          if (Style.BraceWrapping.AfterControlStatement ==
1872
0
              FormatStyle::BWACS_Always) {
1873
0
            addUnwrappedLine();
1874
0
          }
1875
334k
        } else if (Style.BraceWrapping.AfterFunction) {
1876
0
          addUnwrappedLine();
1877
0
        }
1878
334k
        FormatTok->setFinalizedType(TT_FunctionLBrace);
1879
334k
        parseBlock();
1880
334k
        IsDecltypeAutoFunction = false;
1881
334k
        addUnwrappedLine();
1882
334k
        return;
1883
334k
      }
1884
      // Otherwise this was a braced init list, and the structural
1885
      // element continues.
1886
72.6k
      break;
1887
72.6k
    case tok::kw_try:
1888
0
      if (Style.isJavaScript() && Line->MustBeDeclaration) {
1889
        // field/method declaration.
1890
0
        nextToken();
1891
0
        break;
1892
0
      }
1893
      // We arrive here when parsing function-try blocks.
1894
0
      if (Style.BraceWrapping.AfterFunction)
1895
0
        addUnwrappedLine();
1896
0
      parseTryCatch();
1897
0
      return;
1898
2.58M
    case tok::identifier: {
1899
2.58M
      if (Style.isCSharp() && FormatTok->is(Keywords.kw_where) &&
1900
2.58M
          Line->MustBeDeclaration) {
1901
0
        addUnwrappedLine();
1902
0
        parseCSharpGenericTypeConstraint();
1903
0
        break;
1904
0
      }
1905
2.58M
      if (FormatTok->is(TT_MacroBlockEnd)) {
1906
0
        addUnwrappedLine();
1907
0
        return;
1908
0
      }
1909
1910
      // Function declarations (as opposed to function expressions) are parsed
1911
      // on their own unwrapped line by continuing this loop. Function
1912
      // expressions (functions that are not on their own line) must not create
1913
      // a new unwrapped line, so they are special cased below.
1914
2.58M
      size_t TokenCount = Line->Tokens.size();
1915
2.58M
      if (Style.isJavaScript() && FormatTok->is(Keywords.kw_function) &&
1916
2.58M
          (TokenCount > 1 ||
1917
0
           (TokenCount == 1 &&
1918
0
            Line->Tokens.front().Tok->isNot(Keywords.kw_async)))) {
1919
0
        tryToParseJSFunction();
1920
0
        break;
1921
0
      }
1922
2.58M
      if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) &&
1923
2.58M
          FormatTok->is(Keywords.kw_interface)) {
1924
0
        if (Style.isJavaScript()) {
1925
          // In JavaScript/TypeScript, "interface" can be used as a standalone
1926
          // identifier, e.g. in `var interface = 1;`. If "interface" is
1927
          // followed by another identifier, it is very like to be an actual
1928
          // interface declaration.
1929
0
          unsigned StoredPosition = Tokens->getPosition();
1930
0
          FormatToken *Next = Tokens->getNextToken();
1931
0
          FormatTok = Tokens->setPosition(StoredPosition);
1932
0
          if (!mustBeJSIdent(Keywords, Next)) {
1933
0
            nextToken();
1934
0
            break;
1935
0
          }
1936
0
        }
1937
0
        parseRecord();
1938
0
        addUnwrappedLine();
1939
0
        return;
1940
0
      }
1941
1942
2.58M
      if (Style.isVerilog()) {
1943
0
        if (FormatTok->is(Keywords.kw_table)) {
1944
0
          parseVerilogTable();
1945
0
          return;
1946
0
        }
1947
0
        if (Keywords.isVerilogBegin(*FormatTok) ||
1948
0
            Keywords.isVerilogHierarchy(*FormatTok)) {
1949
0
          parseBlock();
1950
0
          addUnwrappedLine();
1951
0
          return;
1952
0
        }
1953
0
      }
1954
1955
2.58M
      if (!Style.isCpp() && FormatTok->is(Keywords.kw_interface)) {
1956
0
        if (parseStructLike())
1957
0
          return;
1958
0
        break;
1959
0
      }
1960
1961
2.58M
      if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) {
1962
0
        parseStatementMacro();
1963
0
        return;
1964
0
      }
1965
1966
      // See if the following token should start a new unwrapped line.
1967
2.58M
      StringRef Text = FormatTok->TokenText;
1968
1969
2.58M
      FormatToken *PreviousToken = FormatTok;
1970
2.58M
      nextToken();
1971
1972
      // JS doesn't have macros, and within classes colons indicate fields, not
1973
      // labels.
1974
2.58M
      if (Style.isJavaScript())
1975
0
        break;
1976
1977
2.58M
      auto OneTokenSoFar = [&]() {
1978
2.58M
        auto I = Line->Tokens.begin(), E = Line->Tokens.end();
1979
2.58M
        while (I != E && I->Tok->is(tok::comment))
1980
9
          ++I;
1981
2.58M
        if (Style.isVerilog())
1982
0
          while (I != E && I->Tok->is(tok::hash))
1983
0
            ++I;
1984
2.58M
        return I != E && (++I == E);
1985
2.58M
      };
1986
2.58M
      if (OneTokenSoFar()) {
1987
        // Recognize function-like macro usages without trailing semicolon as
1988
        // well as free-standing macros like Q_OBJECT.
1989
346k
        bool FunctionLike = FormatTok->is(tok::l_paren);
1990
346k
        if (FunctionLike)
1991
4.56k
          parseParens();
1992
1993
346k
        bool FollowedByNewline =
1994
346k
            CommentsBeforeNextToken.empty()
1995
346k
                ? FormatTok->NewlinesBefore > 0
1996
346k
                : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1997
1998
346k
        if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
1999
346k
            tokenCanStartNewLine(*FormatTok) && Text == Text.upper()) {
2000
2.61k
          if (PreviousToken->isNot(TT_UntouchableMacroFunc))
2001
2.61k
            PreviousToken->setFinalizedType(TT_FunctionLikeOrFreestandingMacro);
2002
2.61k
          addUnwrappedLine();
2003
2.61k
          return;
2004
2.61k
        }
2005
346k
      }
2006
2.58M
      break;
2007
2.58M
    }
2008
2.58M
    case tok::equal:
2009
126k
      if ((Style.isJavaScript() || Style.isCSharp()) &&
2010
126k
          FormatTok->is(TT_FatArrow)) {
2011
0
        tryToParseChildBlock();
2012
0
        break;
2013
0
      }
2014
2015
126k
      nextToken();
2016
126k
      if (FormatTok->is(tok::l_brace)) {
2017
        // Block kind should probably be set to BK_BracedInit for any language.
2018
        // C# needs this change to ensure that array initialisers and object
2019
        // initialisers are indented the same way.
2020
192
        if (Style.isCSharp())
2021
0
          FormatTok->setBlockKind(BK_BracedInit);
2022
192
        nextToken();
2023
192
        parseBracedList();
2024
125k
      } else if (Style.Language == FormatStyle::LK_Proto &&
2025
125k
                 FormatTok->is(tok::less)) {
2026
0
        nextToken();
2027
0
        parseBracedList(/*IsAngleBracket=*/true);
2028
0
      }
2029
126k
      break;
2030
25.1k
    case tok::l_square:
2031
25.1k
      parseSquare();
2032
25.1k
      break;
2033
12
    case tok::kw_new:
2034
12
      parseNew();
2035
12
      break;
2036
6
    case tok::kw_case:
2037
      // Proto: there are no switch/case statements.
2038
6
      if (Style.Language == FormatStyle::LK_Proto) {
2039
0
        nextToken();
2040
0
        return;
2041
0
      }
2042
      // In Verilog switch is called case.
2043
6
      if (Style.isVerilog()) {
2044
0
        parseBlock();
2045
0
        addUnwrappedLine();
2046
0
        return;
2047
0
      }
2048
6
      if (Style.isJavaScript() && Line->MustBeDeclaration) {
2049
        // 'case: string' field declaration.
2050
0
        nextToken();
2051
0
        break;
2052
0
      }
2053
6
      parseCaseLabel();
2054
6
      break;
2055
54
    case tok::kw_default:
2056
54
      nextToken();
2057
54
      if (Style.isVerilog()) {
2058
0
        if (FormatTok->is(tok::colon)) {
2059
          // The label will be handled in the next iteration.
2060
0
          break;
2061
0
        }
2062
0
        if (FormatTok->is(Keywords.kw_clocking)) {
2063
          // A default clocking block.
2064
0
          parseBlock();
2065
0
          addUnwrappedLine();
2066
0
          return;
2067
0
        }
2068
0
        parseVerilogCaseLabel();
2069
0
        return;
2070
0
      }
2071
54
      break;
2072
235k
    case tok::colon:
2073
235k
      nextToken();
2074
235k
      if (Style.isVerilog()) {
2075
0
        parseVerilogCaseLabel();
2076
0
        return;
2077
0
      }
2078
235k
      break;
2079
14.5M
    default:
2080
14.5M
      nextToken();
2081
14.5M
      break;
2082
18.6M
    }
2083
18.6M
  } while (!eof());
2084
719k
}
2085
2086
407k
bool UnwrappedLineParser::tryToParsePropertyAccessor() {
2087
407k
  assert(FormatTok->is(tok::l_brace));
2088
407k
  if (!Style.isCSharp())
2089
407k
    return false;
2090
  // See if it's a property accessor.
2091
0
  if (FormatTok->Previous->isNot(tok::identifier))
2092
0
    return false;
2093
2094
  // See if we are inside a property accessor.
2095
  //
2096
  // Record the current tokenPosition so that we can advance and
2097
  // reset the current token. `Next` is not set yet so we need
2098
  // another way to advance along the token stream.
2099
0
  unsigned int StoredPosition = Tokens->getPosition();
2100
0
  FormatToken *Tok = Tokens->getNextToken();
2101
2102
  // A trivial property accessor is of the form:
2103
  // { [ACCESS_SPECIFIER] [get]; [ACCESS_SPECIFIER] [set|init] }
2104
  // Track these as they do not require line breaks to be introduced.
2105
0
  bool HasSpecialAccessor = false;
2106
0
  bool IsTrivialPropertyAccessor = true;
2107
0
  while (!eof()) {
2108
0
    if (Tok->isOneOf(tok::semi, tok::kw_public, tok::kw_private,
2109
0
                     tok::kw_protected, Keywords.kw_internal, Keywords.kw_get,
2110
0
                     Keywords.kw_init, Keywords.kw_set)) {
2111
0
      if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set))
2112
0
        HasSpecialAccessor = true;
2113
0
      Tok = Tokens->getNextToken();
2114
0
      continue;
2115
0
    }
2116
0
    if (Tok->isNot(tok::r_brace))
2117
0
      IsTrivialPropertyAccessor = false;
2118
0
    break;
2119
0
  }
2120
2121
0
  if (!HasSpecialAccessor) {
2122
0
    Tokens->setPosition(StoredPosition);
2123
0
    return false;
2124
0
  }
2125
2126
  // Try to parse the property accessor:
2127
  // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
2128
0
  Tokens->setPosition(StoredPosition);
2129
0
  if (!IsTrivialPropertyAccessor && Style.BraceWrapping.AfterFunction)
2130
0
    addUnwrappedLine();
2131
0
  nextToken();
2132
0
  do {
2133
0
    switch (FormatTok->Tok.getKind()) {
2134
0
    case tok::r_brace:
2135
0
      nextToken();
2136
0
      if (FormatTok->is(tok::equal)) {
2137
0
        while (!eof() && FormatTok->isNot(tok::semi))
2138
0
          nextToken();
2139
0
        nextToken();
2140
0
      }
2141
0
      addUnwrappedLine();
2142
0
      return true;
2143
0
    case tok::l_brace:
2144
0
      ++Line->Level;
2145
0
      parseBlock(/*MustBeDeclaration=*/true);
2146
0
      addUnwrappedLine();
2147
0
      --Line->Level;
2148
0
      break;
2149
0
    case tok::equal:
2150
0
      if (FormatTok->is(TT_FatArrow)) {
2151
0
        ++Line->Level;
2152
0
        do {
2153
0
          nextToken();
2154
0
        } while (!eof() && FormatTok->isNot(tok::semi));
2155
0
        nextToken();
2156
0
        addUnwrappedLine();
2157
0
        --Line->Level;
2158
0
        break;
2159
0
      }
2160
0
      nextToken();
2161
0
      break;
2162
0
    default:
2163
0
      if (FormatTok->isOneOf(Keywords.kw_get, Keywords.kw_init,
2164
0
                             Keywords.kw_set) &&
2165
0
          !IsTrivialPropertyAccessor) {
2166
        // Non-trivial get/set needs to be on its own line.
2167
0
        addUnwrappedLine();
2168
0
      }
2169
0
      nextToken();
2170
0
    }
2171
0
  } while (!eof());
2172
2173
  // Unreachable for well-formed code (paired '{' and '}').
2174
0
  return true;
2175
0
}
2176
2177
571k
bool UnwrappedLineParser::tryToParseLambda() {
2178
571k
  assert(FormatTok->is(tok::l_square));
2179
571k
  if (!Style.isCpp()) {
2180
0
    nextToken();
2181
0
    return false;
2182
0
  }
2183
571k
  FormatToken &LSquare = *FormatTok;
2184
571k
  if (!tryToParseLambdaIntroducer())
2185
458k
    return false;
2186
2187
112k
  bool SeenArrow = false;
2188
112k
  bool InTemplateParameterList = false;
2189
2190
389k
  while (FormatTok->isNot(tok::l_brace)) {
2191
389k
    if (FormatTok->isSimpleTypeSpecifier()) {
2192
0
      nextToken();
2193
0
      continue;
2194
0
    }
2195
389k
    switch (FormatTok->Tok.getKind()) {
2196
0
    case tok::l_brace:
2197
0
      break;
2198
279
    case tok::l_paren:
2199
279
      parseParens(/*AmpAmpTokenType=*/TT_PointerOrReference);
2200
279
      break;
2201
294
    case tok::l_square:
2202
294
      parseSquare();
2203
294
      break;
2204
82.4k
    case tok::less:
2205
82.4k
      assert(FormatTok->Previous);
2206
82.4k
      if (FormatTok->Previous->is(tok::r_square))
2207
723
        InTemplateParameterList = true;
2208
82.4k
      nextToken();
2209
82.4k
      break;
2210
0
    case tok::kw_auto:
2211
0
    case tok::kw_class:
2212
0
    case tok::kw_template:
2213
0
    case tok::kw_typename:
2214
690
    case tok::amp:
2215
768
    case tok::star:
2216
768
    case tok::kw_const:
2217
768
    case tok::kw_constexpr:
2218
768
    case tok::kw_consteval:
2219
1.52k
    case tok::comma:
2220
92.1k
    case tok::greater:
2221
188k
    case tok::identifier:
2222
189k
    case tok::numeric_constant:
2223
189k
    case tok::coloncolon:
2224
189k
    case tok::kw_mutable:
2225
189k
    case tok::kw_noexcept:
2226
189k
    case tok::kw_static:
2227
189k
      nextToken();
2228
189k
      break;
2229
    // Specialization of a template with an integer parameter can contain
2230
    // arithmetic, logical, comparison and ternary operators.
2231
    //
2232
    // FIXME: This also accepts sequences of operators that are not in the scope
2233
    // of a template argument list.
2234
    //
2235
    // In a C++ lambda a template type can only occur after an arrow. We use
2236
    // this as an heuristic to distinguish between Objective-C expressions
2237
    // followed by an `a->b` expression, such as:
2238
    // ([obj func:arg] + a->b)
2239
    // Otherwise the code below would parse as a lambda.
2240
141
    case tok::plus:
2241
993
    case tok::minus:
2242
2.04k
    case tok::exclaim:
2243
2.05k
    case tok::tilde:
2244
2.52k
    case tok::slash:
2245
2.61k
    case tok::percent:
2246
2.62k
    case tok::lessless:
2247
3.66k
    case tok::pipe:
2248
3.66k
    case tok::pipepipe:
2249
3.66k
    case tok::ampamp:
2250
3.66k
    case tok::caret:
2251
3.66k
    case tok::equalequal:
2252
3.66k
    case tok::exclaimequal:
2253
3.70k
    case tok::greaterequal:
2254
3.70k
    case tok::lessequal:
2255
6.40k
    case tok::question:
2256
10.0k
    case tok::colon:
2257
10.0k
    case tok::ellipsis:
2258
10.0k
    case tok::kw_true:
2259
10.0k
    case tok::kw_false:
2260
10.0k
      if (SeenArrow || InTemplateParameterList) {
2261
3.20k
        nextToken();
2262
3.20k
        break;
2263
3.20k
      }
2264
6.88k
      return true;
2265
969
    case tok::arrow:
2266
      // This might or might not actually be a lambda arrow (this could be an
2267
      // ObjC method invocation followed by a dereferencing arrow). We might
2268
      // reset this back to TT_Unknown in TokenAnnotator.
2269
969
      FormatTok->setFinalizedType(TT_TrailingReturnArrow);
2270
969
      SeenArrow = true;
2271
969
      nextToken();
2272
969
      break;
2273
0
    case tok::kw_requires: {
2274
0
      auto *RequiresToken = FormatTok;
2275
0
      nextToken();
2276
0
      parseRequiresClause(RequiresToken);
2277
0
      break;
2278
10.0k
    }
2279
1.02k
    case tok::equal:
2280
1.02k
      if (!InTemplateParameterList)
2281
984
        return true;
2282
39
      nextToken();
2283
39
      break;
2284
104k
    default:
2285
104k
      return true;
2286
389k
    }
2287
389k
  }
2288
2289
30
  FormatTok->setFinalizedType(TT_LambdaLBrace);
2290
30
  LSquare.setFinalizedType(TT_LambdaLSquare);
2291
2292
30
  NestedLambdas.push_back(Line->SeenDecltypeAuto);
2293
30
  parseChildBlock();
2294
30
  assert(!NestedLambdas.empty());
2295
0
  NestedLambdas.pop_back();
2296
2297
30
  return true;
2298
112k
}
2299
2300
571k
bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
2301
571k
  const FormatToken *Previous = FormatTok->Previous;
2302
571k
  const FormatToken *LeftSquare = FormatTok;
2303
571k
  nextToken();
2304
571k
  if ((Previous && ((Previous->Tok.getIdentifierInfo() &&
2305
569k
                     !Previous->isOneOf(tok::kw_return, tok::kw_co_await,
2306
50.7k
                                        tok::kw_co_yield, tok::kw_co_return)) ||
2307
569k
                    Previous->closesScope())) ||
2308
571k
      LeftSquare->isCppStructuredBinding(Style)) {
2309
52.5k
    return false;
2310
52.5k
  }
2311
518k
  if (FormatTok->is(tok::l_square) || tok::isLiteral(FormatTok->Tok.getKind()))
2312
406k
    return false;
2313
112k
  if (FormatTok->is(tok::r_square)) {
2314
3.87k
    const FormatToken *Next = Tokens->peekNextToken(/*SkipComment=*/true);
2315
3.87k
    if (Next->is(tok::greater))
2316
9
      return false;
2317
3.87k
  }
2318
112k
  parseSquare(/*LambdaIntroducer=*/true);
2319
112k
  return true;
2320
112k
}
2321
2322
0
void UnwrappedLineParser::tryToParseJSFunction() {
2323
0
  assert(FormatTok->is(Keywords.kw_function));
2324
0
  if (FormatTok->is(Keywords.kw_async))
2325
0
    nextToken();
2326
  // Consume "function".
2327
0
  nextToken();
2328
2329
  // Consume * (generator function). Treat it like C++'s overloaded operators.
2330
0
  if (FormatTok->is(tok::star)) {
2331
0
    FormatTok->setFinalizedType(TT_OverloadedOperator);
2332
0
    nextToken();
2333
0
  }
2334
2335
  // Consume function name.
2336
0
  if (FormatTok->is(tok::identifier))
2337
0
    nextToken();
2338
2339
0
  if (FormatTok->isNot(tok::l_paren))
2340
0
    return;
2341
2342
  // Parse formal parameter list.
2343
0
  parseParens();
2344
2345
0
  if (FormatTok->is(tok::colon)) {
2346
    // Parse a type definition.
2347
0
    nextToken();
2348
2349
    // Eat the type declaration. For braced inline object types, balance braces,
2350
    // otherwise just parse until finding an l_brace for the function body.
2351
0
    if (FormatTok->is(tok::l_brace))
2352
0
      tryToParseBracedList();
2353
0
    else
2354
0
      while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
2355
0
        nextToken();
2356
0
  }
2357
2358
0
  if (FormatTok->is(tok::semi))
2359
0
    return;
2360
2361
0
  parseChildBlock();
2362
0
}
2363
2364
684k
bool UnwrappedLineParser::tryToParseBracedList() {
2365
684k
  if (FormatTok->is(BK_Unknown))
2366
254k
    calculateBraceTypes();
2367
684k
  assert(FormatTok->isNot(BK_Unknown));
2368
684k
  if (FormatTok->is(BK_Block))
2369
532k
    return false;
2370
152k
  nextToken();
2371
152k
  parseBracedList();
2372
152k
  return true;
2373
684k
}
2374
2375
0
bool UnwrappedLineParser::tryToParseChildBlock() {
2376
0
  assert(Style.isJavaScript() || Style.isCSharp());
2377
0
  assert(FormatTok->is(TT_FatArrow));
2378
  // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType TT_FatArrow.
2379
  // They always start an expression or a child block if followed by a curly
2380
  // brace.
2381
0
  nextToken();
2382
0
  if (FormatTok->isNot(tok::l_brace))
2383
0
    return false;
2384
0
  parseChildBlock();
2385
0
  return true;
2386
0
}
2387
2388
175k
bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
2389
175k
  bool HasError = false;
2390
2391
  // FIXME: Once we have an expression parser in the UnwrappedLineParser,
2392
  // replace this by using parseAssignmentExpression() inside.
2393
1.00M
  do {
2394
1.00M
    if (Style.isCSharp() && FormatTok->is(TT_FatArrow) &&
2395
1.00M
        tryToParseChildBlock()) {
2396
0
      continue;
2397
0
    }
2398
1.00M
    if (Style.isJavaScript()) {
2399
0
      if (FormatTok->is(Keywords.kw_function)) {
2400
0
        tryToParseJSFunction();
2401
0
        continue;
2402
0
      }
2403
0
      if (FormatTok->is(tok::l_brace)) {
2404
        // Could be a method inside of a braced list `{a() { return 1; }}`.
2405
0
        if (tryToParseBracedList())
2406
0
          continue;
2407
0
        parseChildBlock();
2408
0
      }
2409
0
    }
2410
1.00M
    if (FormatTok->is(IsAngleBracket ? tok::greater : tok::r_brace)) {
2411
160k
      if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
2412
0
        addUnwrappedLine();
2413
160k
      nextToken();
2414
160k
      return !HasError;
2415
160k
    }
2416
842k
    switch (FormatTok->Tok.getKind()) {
2417
3.30k
    case tok::l_square:
2418
3.30k
      if (Style.isCSharp())
2419
0
        parseSquare();
2420
3.30k
      else
2421
3.30k
        tryToParseLambda();
2422
3.30k
      break;
2423
1.74k
    case tok::l_paren:
2424
1.74k
      parseParens();
2425
      // JavaScript can just have free standing methods and getters/setters in
2426
      // object literals. Detect them by a "{" following ")".
2427
1.74k
      if (Style.isJavaScript()) {
2428
0
        if (FormatTok->is(tok::l_brace))
2429
0
          parseChildBlock();
2430
0
        break;
2431
0
      }
2432
1.74k
      break;
2433
18.8k
    case tok::l_brace:
2434
      // Assume there are no blocks inside a braced init list apart
2435
      // from the ones we explicitly parse out (like lambdas).
2436
18.8k
      FormatTok->setBlockKind(BK_BracedInit);
2437
18.8k
      nextToken();
2438
18.8k
      parseBracedList();
2439
18.8k
      break;
2440
13.1k
    case tok::less:
2441
13.1k
      nextToken();
2442
13.1k
      if (IsAngleBracket)
2443
0
        parseBracedList(/*IsAngleBracket=*/true);
2444
13.1k
      break;
2445
14.8k
    case tok::semi:
2446
      // JavaScript (or more precisely TypeScript) can have semicolons in braced
2447
      // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
2448
      // used for error recovery if we have otherwise determined that this is
2449
      // a braced list.
2450
14.8k
      if (Style.isJavaScript()) {
2451
0
        nextToken();
2452
0
        break;
2453
0
      }
2454
14.8k
      HasError = true;
2455
14.8k
      if (!IsEnum)
2456
14.6k
        return false;
2457
225
      nextToken();
2458
225
      break;
2459
83.3k
    case tok::comma:
2460
83.3k
      nextToken();
2461
83.3k
      if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
2462
0
        addUnwrappedLine();
2463
83.3k
      break;
2464
707k
    default:
2465
707k
      nextToken();
2466
707k
      break;
2467
842k
    }
2468
842k
  } while (!eof());
2469
117
  return false;
2470
175k
}
2471
2472
/// \brief Parses a pair of parentheses (and everything between them).
2473
/// \param AmpAmpTokenType If different than TT_Unknown sets this type for all
2474
/// double ampersands. This applies for all nested scopes as well.
2475
///
2476
/// Returns whether there is a `=` token between the parentheses.
2477
444k
bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
2478
444k
  assert(FormatTok->is(tok::l_paren) && "'(' expected.");
2479
0
  auto *LeftParen = FormatTok;
2480
444k
  bool SeenEqual = false;
2481
444k
  const bool MightBeStmtExpr = Tokens->peekNextToken()->is(tok::l_brace);
2482
444k
  nextToken();
2483
16.2M
  do {
2484
16.2M
    switch (FormatTok->Tok.getKind()) {
2485
357k
    case tok::l_paren:
2486
357k
      if (parseParens(AmpAmpTokenType))
2487
190k
        SeenEqual = true;
2488
357k
      if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
2489
0
        parseChildBlock();
2490
357k
      break;
2491
165k
    case tok::r_paren:
2492
165k
      if (!MightBeStmtExpr &&
2493
165k
          Style.RemoveParentheses > FormatStyle::RPS_Leave) {
2494
0
        const auto *Prev = LeftParen->Previous;
2495
0
        const auto *Next = Tokens->peekNextToken();
2496
0
        const bool DoubleParens =
2497
0
            Prev && Prev->is(tok::l_paren) && Next && Next->is(tok::r_paren);
2498
0
        const auto *PrevPrev = Prev ? Prev->getPreviousNonComment() : nullptr;
2499
0
        const bool Blacklisted =
2500
0
            PrevPrev &&
2501
0
            (PrevPrev->isOneOf(tok::kw___attribute, tok::kw_decltype) ||
2502
0
             (SeenEqual &&
2503
0
              (PrevPrev->isOneOf(tok::kw_if, tok::kw_while) ||
2504
0
               PrevPrev->endsSequence(tok::kw_constexpr, tok::kw_if))));
2505
0
        const bool ReturnParens =
2506
0
            Style.RemoveParentheses == FormatStyle::RPS_ReturnStatement &&
2507
0
            ((NestedLambdas.empty() && !IsDecltypeAutoFunction) ||
2508
0
             (!NestedLambdas.empty() && !NestedLambdas.back())) &&
2509
0
            Prev && Prev->isOneOf(tok::kw_return, tok::kw_co_return) && Next &&
2510
0
            Next->is(tok::semi);
2511
0
        if ((DoubleParens && !Blacklisted) || ReturnParens) {
2512
0
          LeftParen->Optional = true;
2513
0
          FormatTok->Optional = true;
2514
0
        }
2515
0
      }
2516
165k
      nextToken();
2517
165k
      return SeenEqual;
2518
125k
    case tok::r_brace:
2519
      // A "}" inside parenthesis is an error if there wasn't a matching "{".
2520
125k
      return SeenEqual;
2521
75.9k
    case tok::l_square:
2522
75.9k
      tryToParseLambda();
2523
75.9k
      break;
2524
15.5k
    case tok::l_brace:
2525
15.5k
      if (!tryToParseBracedList())
2526
14.1k
        parseChildBlock();
2527
15.5k
      break;
2528
29.8k
    case tok::at:
2529
29.8k
      nextToken();
2530
29.8k
      if (FormatTok->is(tok::l_brace)) {
2531
1.76k
        nextToken();
2532
1.76k
        parseBracedList();
2533
1.76k
      }
2534
29.8k
      break;
2535
99.1k
    case tok::equal:
2536
99.1k
      SeenEqual = true;
2537
99.1k
      if (Style.isCSharp() && FormatTok->is(TT_FatArrow))
2538
0
        tryToParseChildBlock();
2539
99.1k
      else
2540
99.1k
        nextToken();
2541
99.1k
      break;
2542
24
    case tok::kw_class:
2543
24
      if (Style.isJavaScript())
2544
0
        parseRecord(/*ParseAsExpr=*/true);
2545
24
      else
2546
24
        nextToken();
2547
24
      break;
2548
1.95M
    case tok::identifier:
2549
1.95M
      if (Style.isJavaScript() && (FormatTok->is(Keywords.kw_function)))
2550
0
        tryToParseJSFunction();
2551
1.95M
      else
2552
1.95M
        nextToken();
2553
1.95M
      break;
2554
0
    case tok::kw_requires: {
2555
0
      auto RequiresToken = FormatTok;
2556
0
      nextToken();
2557
0
      parseRequiresExpression(RequiresToken);
2558
0
      break;
2559
0
    }
2560
3.49k
    case tok::ampamp:
2561
3.49k
      if (AmpAmpTokenType != TT_Unknown)
2562
0
        FormatTok->setFinalizedType(AmpAmpTokenType);
2563
3.49k
      [[fallthrough]];
2564
13.4M
    default:
2565
13.4M
      nextToken();
2566
13.4M
      break;
2567
16.2M
    }
2568
16.2M
  } while (!eof());
2569
153k
  return SeenEqual;
2570
444k
}
2571
2572
604k
void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
2573
604k
  if (!LambdaIntroducer) {
2574
492k
    assert(FormatTok->is(tok::l_square) && "'[' expected.");
2575
492k
    if (tryToParseLambda())
2576
99.4k
      return;
2577
492k
  }
2578
14.7M
  do {
2579
14.7M
    switch (FormatTok->Tok.getKind()) {
2580
26.1k
    case tok::l_paren:
2581
26.1k
      parseParens();
2582
26.1k
      break;
2583
47.9k
    case tok::r_square:
2584
47.9k
      nextToken();
2585
47.9k
      return;
2586
26.5k
    case tok::r_brace:
2587
      // A "}" inside parenthesis is an error if there wasn't a matching "{".
2588
26.5k
      return;
2589
466k
    case tok::l_square:
2590
466k
      parseSquare();
2591
466k
      break;
2592
84.2k
    case tok::l_brace: {
2593
84.2k
      if (!tryToParseBracedList())
2594
7.58k
        parseChildBlock();
2595
84.2k
      break;
2596
0
    }
2597
31.5k
    case tok::at:
2598
31.5k
      nextToken();
2599
31.5k
      if (FormatTok->is(tok::l_brace)) {
2600
1.88k
        nextToken();
2601
1.88k
        parseBracedList();
2602
1.88k
      }
2603
31.5k
      break;
2604
14.0M
    default:
2605
14.0M
      nextToken();
2606
14.0M
      break;
2607
14.7M
    }
2608
14.7M
  } while (!eof());
2609
505k
}
2610
2611
6.39k
void UnwrappedLineParser::keepAncestorBraces() {
2612
6.39k
  if (!Style.RemoveBracesLLVM)
2613
6.39k
    return;
2614
2615
0
  const int MaxNestingLevels = 2;
2616
0
  const int Size = NestedTooDeep.size();
2617
0
  if (Size >= MaxNestingLevels)
2618
0
    NestedTooDeep[Size - MaxNestingLevels] = true;
2619
0
  NestedTooDeep.push_back(false);
2620
0
}
2621
2622
0
static FormatToken *getLastNonComment(const UnwrappedLine &Line) {
2623
0
  for (const auto &Token : llvm::reverse(Line.Tokens))
2624
0
    if (Token.Tok->isNot(tok::comment))
2625
0
      return Token.Tok;
2626
2627
0
  return nullptr;
2628
0
}
2629
2630
3.46k
void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
2631
3.46k
  FormatToken *Tok = nullptr;
2632
2633
3.46k
  if (Style.InsertBraces && !Line->InPPDirective && !Line->Tokens.empty() &&
2634
3.46k
      PreprocessorDirectives.empty() && FormatTok->isNot(tok::semi)) {
2635
0
    Tok = Style.BraceWrapping.AfterControlStatement == FormatStyle::BWACS_Never
2636
0
              ? getLastNonComment(*Line)
2637
0
              : Line->Tokens.back().Tok;
2638
0
    assert(Tok);
2639
0
    if (Tok->BraceCount < 0) {
2640
0
      assert(Tok->BraceCount == -1);
2641
0
      Tok = nullptr;
2642
0
    } else {
2643
0
      Tok->BraceCount = -1;
2644
0
    }
2645
0
  }
2646
2647
0
  addUnwrappedLine();
2648
3.46k
  ++Line->Level;
2649
3.46k
  parseStructuralElement();
2650
2651
3.46k
  if (Tok) {
2652
0
    assert(!Line->InPPDirective);
2653
0
    Tok = nullptr;
2654
0
    for (const auto &L : llvm::reverse(*CurrentLines)) {
2655
0
      if (!L.InPPDirective && getLastNonComment(L)) {
2656
0
        Tok = L.Tokens.back().Tok;
2657
0
        break;
2658
0
      }
2659
0
    }
2660
0
    assert(Tok);
2661
0
    ++Tok->BraceCount;
2662
0
  }
2663
2664
3.46k
  if (CheckEOF && eof())
2665
0
    addUnwrappedLine();
2666
2667
3.46k
  --Line->Level;
2668
3.46k
}
2669
2670
0
static void markOptionalBraces(FormatToken *LeftBrace) {
2671
0
  if (!LeftBrace)
2672
0
    return;
2673
2674
0
  assert(LeftBrace->is(tok::l_brace));
2675
2676
0
  FormatToken *RightBrace = LeftBrace->MatchingParen;
2677
0
  if (!RightBrace) {
2678
0
    assert(!LeftBrace->Optional);
2679
0
    return;
2680
0
  }
2681
2682
0
  assert(RightBrace->is(tok::r_brace));
2683
0
  assert(RightBrace->MatchingParen == LeftBrace);
2684
0
  assert(LeftBrace->Optional == RightBrace->Optional);
2685
2686
0
  LeftBrace->Optional = true;
2687
0
  RightBrace->Optional = true;
2688
0
}
2689
2690
6.90k
void UnwrappedLineParser::handleAttributes() {
2691
  // Handle AttributeMacro, e.g. `if (x) UNLIKELY`.
2692
6.90k
  if (FormatTok->isAttribute())
2693
0
    nextToken();
2694
6.90k
  else if (FormatTok->is(tok::l_square))
2695
0
    handleCppAttributes();
2696
6.90k
}
2697
2698
2.77k
bool UnwrappedLineParser::handleCppAttributes() {
2699
  // Handle [[likely]] / [[unlikely]] attributes.
2700
2.77k
  assert(FormatTok->is(tok::l_square));
2701
2.77k
  if (!tryToParseSimpleAttribute())
2702
2.77k
    return false;
2703
0
  parseSquare();
2704
0
  return true;
2705
2.77k
}
2706
2707
/// Returns whether \c Tok begins a block.
2708
6.96k
bool UnwrappedLineParser::isBlockBegin(const FormatToken &Tok) const {
2709
  // FIXME: rename the function or make
2710
  // Tok.isOneOf(tok::l_brace, TT_MacroBlockBegin) work.
2711
6.96k
  return Style.isVerilog() ? Keywords.isVerilogBegin(Tok)
2712
6.96k
                           : Tok.is(tok::l_brace);
2713
6.96k
}
2714
2715
FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
2716
                                                  bool KeepBraces,
2717
5.94k
                                                  bool IsVerilogAssert) {
2718
5.94k
  assert((FormatTok->is(tok::kw_if) ||
2719
5.94k
          (Style.isVerilog() &&
2720
5.94k
           FormatTok->isOneOf(tok::kw_restrict, Keywords.kw_assert,
2721
5.94k
                              Keywords.kw_assume, Keywords.kw_cover))) &&
2722
5.94k
         "'if' expected");
2723
0
  nextToken();
2724
2725
5.94k
  if (IsVerilogAssert) {
2726
    // Handle `assert #0` and `assert final`.
2727
0
    if (FormatTok->is(Keywords.kw_verilogHash)) {
2728
0
      nextToken();
2729
0
      if (FormatTok->is(tok::numeric_constant))
2730
0
        nextToken();
2731
0
    } else if (FormatTok->isOneOf(Keywords.kw_final, Keywords.kw_property,
2732
0
                                  Keywords.kw_sequence)) {
2733
0
      nextToken();
2734
0
    }
2735
0
  }
2736
2737
  // Handle `if !consteval`.
2738
5.94k
  if (FormatTok->is(tok::exclaim))
2739
0
    nextToken();
2740
2741
5.94k
  bool KeepIfBraces = true;
2742
5.94k
  if (FormatTok->is(tok::kw_consteval)) {
2743
0
    nextToken();
2744
5.94k
  } else {
2745
5.94k
    KeepIfBraces = !Style.RemoveBracesLLVM || KeepBraces;
2746
5.94k
    if (FormatTok->isOneOf(tok::kw_constexpr, tok::identifier))
2747
348
      nextToken();
2748
5.94k
    if (FormatTok->is(tok::l_paren)) {
2749
5.51k
      FormatTok->setFinalizedType(TT_ConditionLParen);
2750
5.51k
      parseParens();
2751
5.51k
    }
2752
5.94k
  }
2753
5.94k
  handleAttributes();
2754
  // The then action is optional in Verilog assert statements.
2755
5.94k
  if (IsVerilogAssert && FormatTok->is(tok::semi)) {
2756
0
    nextToken();
2757
0
    addUnwrappedLine();
2758
0
    return nullptr;
2759
0
  }
2760
2761
5.94k
  bool NeedsUnwrappedLine = false;
2762
5.94k
  keepAncestorBraces();
2763
2764
5.94k
  FormatToken *IfLeftBrace = nullptr;
2765
5.94k
  IfStmtKind IfBlockKind = IfStmtKind::NotIf;
2766
2767
5.94k
  if (isBlockBegin(*FormatTok)) {
2768
2.63k
    FormatTok->setFinalizedType(TT_ControlStatementLBrace);
2769
2.63k
    IfLeftBrace = FormatTok;
2770
2.63k
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2771
2.63k
    parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2772
2.63k
               /*MunchSemi=*/true, KeepIfBraces, &IfBlockKind);
2773
2.63k
    setPreviousRBraceType(TT_ControlStatementRBrace);
2774
2.63k
    if (Style.BraceWrapping.BeforeElse)
2775
0
      addUnwrappedLine();
2776
2.63k
    else
2777
2.63k
      NeedsUnwrappedLine = true;
2778
3.31k
  } else if (IsVerilogAssert && FormatTok->is(tok::kw_else)) {
2779
0
    addUnwrappedLine();
2780
3.31k
  } else {
2781
3.31k
    parseUnbracedBody();
2782
3.31k
  }
2783
2784
5.94k
  if (Style.RemoveBracesLLVM) {
2785
0
    assert(!NestedTooDeep.empty());
2786
0
    KeepIfBraces = KeepIfBraces ||
2787
0
                   (IfLeftBrace && !IfLeftBrace->MatchingParen) ||
2788
0
                   NestedTooDeep.back() || IfBlockKind == IfStmtKind::IfOnly ||
2789
0
                   IfBlockKind == IfStmtKind::IfElseIf;
2790
0
  }
2791
2792
0
  bool KeepElseBraces = KeepIfBraces;
2793
5.94k
  FormatToken *ElseLeftBrace = nullptr;
2794
5.94k
  IfStmtKind Kind = IfStmtKind::IfOnly;
2795
2796
5.94k
  if (FormatTok->is(tok::kw_else)) {
2797
573
    if (Style.RemoveBracesLLVM) {
2798
0
      NestedTooDeep.back() = false;
2799
0
      Kind = IfStmtKind::IfElse;
2800
0
    }
2801
573
    nextToken();
2802
573
    handleAttributes();
2803
573
    if (isBlockBegin(*FormatTok)) {
2804
366
      const bool FollowedByIf = Tokens->peekNextToken()->is(tok::kw_if);
2805
366
      FormatTok->setFinalizedType(TT_ElseLBrace);
2806
366
      ElseLeftBrace = FormatTok;
2807
366
      CompoundStatementIndenter Indenter(this, Style, Line->Level);
2808
366
      IfStmtKind ElseBlockKind = IfStmtKind::NotIf;
2809
366
      FormatToken *IfLBrace =
2810
366
          parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2811
366
                     /*MunchSemi=*/true, KeepElseBraces, &ElseBlockKind);
2812
366
      setPreviousRBraceType(TT_ElseRBrace);
2813
366
      if (FormatTok->is(tok::kw_else)) {
2814
0
        KeepElseBraces = KeepElseBraces ||
2815
0
                         ElseBlockKind == IfStmtKind::IfOnly ||
2816
0
                         ElseBlockKind == IfStmtKind::IfElseIf;
2817
366
      } else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) {
2818
0
        KeepElseBraces = true;
2819
0
        assert(ElseLeftBrace->MatchingParen);
2820
0
        markOptionalBraces(ElseLeftBrace);
2821
0
      }
2822
0
      addUnwrappedLine();
2823
366
    } else if (!IsVerilogAssert && FormatTok->is(tok::kw_if)) {
2824
183
      const FormatToken *Previous = Tokens->getPreviousToken();
2825
183
      assert(Previous);
2826
0
      const bool IsPrecededByComment = Previous->is(tok::comment);
2827
183
      if (IsPrecededByComment) {
2828
0
        addUnwrappedLine();
2829
0
        ++Line->Level;
2830
0
      }
2831
183
      bool TooDeep = true;
2832
183
      if (Style.RemoveBracesLLVM) {
2833
0
        Kind = IfStmtKind::IfElseIf;
2834
0
        TooDeep = NestedTooDeep.pop_back_val();
2835
0
      }
2836
183
      ElseLeftBrace = parseIfThenElse(/*IfKind=*/nullptr, KeepIfBraces);
2837
183
      if (Style.RemoveBracesLLVM)
2838
0
        NestedTooDeep.push_back(TooDeep);
2839
183
      if (IsPrecededByComment)
2840
0
        --Line->Level;
2841
183
    } else {
2842
24
      parseUnbracedBody(/*CheckEOF=*/true);
2843
24
    }
2844
5.37k
  } else {
2845
5.37k
    KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse;
2846
5.37k
    if (NeedsUnwrappedLine)
2847
2.08k
      addUnwrappedLine();
2848
5.37k
  }
2849
2850
5.94k
  if (!Style.RemoveBracesLLVM)
2851
5.94k
    return nullptr;
2852
2853
0
  assert(!NestedTooDeep.empty());
2854
0
  KeepElseBraces = KeepElseBraces ||
2855
0
                   (ElseLeftBrace && !ElseLeftBrace->MatchingParen) ||
2856
0
                   NestedTooDeep.back();
2857
2858
0
  NestedTooDeep.pop_back();
2859
2860
0
  if (!KeepIfBraces && !KeepElseBraces) {
2861
0
    markOptionalBraces(IfLeftBrace);
2862
0
    markOptionalBraces(ElseLeftBrace);
2863
0
  } else if (IfLeftBrace) {
2864
0
    FormatToken *IfRightBrace = IfLeftBrace->MatchingParen;
2865
0
    if (IfRightBrace) {
2866
0
      assert(IfRightBrace->MatchingParen == IfLeftBrace);
2867
0
      assert(!IfLeftBrace->Optional);
2868
0
      assert(!IfRightBrace->Optional);
2869
0
      IfLeftBrace->MatchingParen = nullptr;
2870
0
      IfRightBrace->MatchingParen = nullptr;
2871
0
    }
2872
0
  }
2873
2874
0
  if (IfKind)
2875
0
    *IfKind = Kind;
2876
2877
0
  return IfLeftBrace;
2878
5.94k
}
2879
2880
0
void UnwrappedLineParser::parseTryCatch() {
2881
0
  assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
2882
0
  nextToken();
2883
0
  bool NeedsUnwrappedLine = false;
2884
0
  if (FormatTok->is(tok::colon)) {
2885
    // We are in a function try block, what comes is an initializer list.
2886
0
    nextToken();
2887
2888
    // In case identifiers were removed by clang-tidy, what might follow is
2889
    // multiple commas in sequence - before the first identifier.
2890
0
    while (FormatTok->is(tok::comma))
2891
0
      nextToken();
2892
2893
0
    while (FormatTok->is(tok::identifier)) {
2894
0
      nextToken();
2895
0
      if (FormatTok->is(tok::l_paren))
2896
0
        parseParens();
2897
0
      if (FormatTok->Previous && FormatTok->Previous->is(tok::identifier) &&
2898
0
          FormatTok->is(tok::l_brace)) {
2899
0
        do {
2900
0
          nextToken();
2901
0
        } while (FormatTok->isNot(tok::r_brace));
2902
0
        nextToken();
2903
0
      }
2904
2905
      // In case identifiers were removed by clang-tidy, what might follow is
2906
      // multiple commas in sequence - after the first identifier.
2907
0
      while (FormatTok->is(tok::comma))
2908
0
        nextToken();
2909
0
    }
2910
0
  }
2911
  // Parse try with resource.
2912
0
  if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren))
2913
0
    parseParens();
2914
2915
0
  keepAncestorBraces();
2916
2917
0
  if (FormatTok->is(tok::l_brace)) {
2918
0
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2919
0
    parseBlock();
2920
0
    if (Style.BraceWrapping.BeforeCatch)
2921
0
      addUnwrappedLine();
2922
0
    else
2923
0
      NeedsUnwrappedLine = true;
2924
0
  } else if (FormatTok->isNot(tok::kw_catch)) {
2925
    // The C++ standard requires a compound-statement after a try.
2926
    // If there's none, we try to assume there's a structuralElement
2927
    // and try to continue.
2928
0
    addUnwrappedLine();
2929
0
    ++Line->Level;
2930
0
    parseStructuralElement();
2931
0
    --Line->Level;
2932
0
  }
2933
0
  while (true) {
2934
0
    if (FormatTok->is(tok::at))
2935
0
      nextToken();
2936
0
    if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
2937
0
                             tok::kw___finally) ||
2938
0
          ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
2939
0
           FormatTok->is(Keywords.kw_finally)) ||
2940
0
          (FormatTok->isObjCAtKeyword(tok::objc_catch) ||
2941
0
           FormatTok->isObjCAtKeyword(tok::objc_finally)))) {
2942
0
      break;
2943
0
    }
2944
0
    nextToken();
2945
0
    while (FormatTok->isNot(tok::l_brace)) {
2946
0
      if (FormatTok->is(tok::l_paren)) {
2947
0
        parseParens();
2948
0
        continue;
2949
0
      }
2950
0
      if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) {
2951
0
        if (Style.RemoveBracesLLVM)
2952
0
          NestedTooDeep.pop_back();
2953
0
        return;
2954
0
      }
2955
0
      nextToken();
2956
0
    }
2957
0
    NeedsUnwrappedLine = false;
2958
0
    Line->MustBeDeclaration = false;
2959
0
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
2960
0
    parseBlock();
2961
0
    if (Style.BraceWrapping.BeforeCatch)
2962
0
      addUnwrappedLine();
2963
0
    else
2964
0
      NeedsUnwrappedLine = true;
2965
0
  }
2966
2967
0
  if (Style.RemoveBracesLLVM)
2968
0
    NestedTooDeep.pop_back();
2969
2970
0
  if (NeedsUnwrappedLine)
2971
0
    addUnwrappedLine();
2972
0
}
2973
2974
0
void UnwrappedLineParser::parseNamespace() {
2975
0
  assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
2976
0
         "'namespace' expected");
2977
2978
0
  const FormatToken &InitialToken = *FormatTok;
2979
0
  nextToken();
2980
0
  if (InitialToken.is(TT_NamespaceMacro)) {
2981
0
    parseParens();
2982
0
  } else {
2983
0
    while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
2984
0
                              tok::l_square, tok::period, tok::l_paren) ||
2985
0
           (Style.isCSharp() && FormatTok->is(tok::kw_union))) {
2986
0
      if (FormatTok->is(tok::l_square))
2987
0
        parseSquare();
2988
0
      else if (FormatTok->is(tok::l_paren))
2989
0
        parseParens();
2990
0
      else
2991
0
        nextToken();
2992
0
    }
2993
0
  }
2994
0
  if (FormatTok->is(tok::l_brace)) {
2995
0
    FormatTok->setFinalizedType(TT_NamespaceLBrace);
2996
2997
0
    if (ShouldBreakBeforeBrace(Style, InitialToken))
2998
0
      addUnwrappedLine();
2999
3000
0
    unsigned AddLevels =
3001
0
        Style.NamespaceIndentation == FormatStyle::NI_All ||
3002
0
                (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
3003
0
                 DeclarationScopeStack.size() > 1)
3004
0
            ? 1u
3005
0
            : 0u;
3006
0
    bool ManageWhitesmithsBraces =
3007
0
        AddLevels == 0u &&
3008
0
        Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
3009
3010
    // If we're in Whitesmiths mode, indent the brace if we're not indenting
3011
    // the whole block.
3012
0
    if (ManageWhitesmithsBraces)
3013
0
      ++Line->Level;
3014
3015
    // Munch the semicolon after a namespace. This is more common than one would
3016
    // think. Putting the semicolon into its own line is very ugly.
3017
0
    parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/true,
3018
0
               /*KeepBraces=*/true, /*IfKind=*/nullptr,
3019
0
               ManageWhitesmithsBraces);
3020
3021
0
    addUnwrappedLine(AddLevels > 0 ? LineLevel::Remove : LineLevel::Keep);
3022
3023
0
    if (ManageWhitesmithsBraces)
3024
0
      --Line->Level;
3025
0
  }
3026
  // FIXME: Add error handling.
3027
0
}
3028
3029
12
void UnwrappedLineParser::parseNew() {
3030
12
  assert(FormatTok->is(tok::kw_new) && "'new' expected");
3031
0
  nextToken();
3032
3033
12
  if (Style.isCSharp()) {
3034
0
    do {
3035
      // Handle constructor invocation, e.g. `new(field: value)`.
3036
0
      if (FormatTok->is(tok::l_paren))
3037
0
        parseParens();
3038
3039
      // Handle array initialization syntax, e.g. `new[] {10, 20, 30}`.
3040
0
      if (FormatTok->is(tok::l_brace))
3041
0
        parseBracedList();
3042
3043
0
      if (FormatTok->isOneOf(tok::semi, tok::comma))
3044
0
        return;
3045
3046
0
      nextToken();
3047
0
    } while (!eof());
3048
0
  }
3049
3050
12
  if (Style.Language != FormatStyle::LK_Java)
3051
12
    return;
3052
3053
  // In Java, we can parse everything up to the parens, which aren't optional.
3054
0
  do {
3055
    // There should not be a ;, { or } before the new's open paren.
3056
0
    if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
3057
0
      return;
3058
3059
    // Consume the parens.
3060
0
    if (FormatTok->is(tok::l_paren)) {
3061
0
      parseParens();
3062
3063
      // If there is a class body of an anonymous class, consume that as child.
3064
0
      if (FormatTok->is(tok::l_brace))
3065
0
        parseChildBlock();
3066
0
      return;
3067
0
    }
3068
0
    nextToken();
3069
0
  } while (!eof());
3070
0
}
3071
3072
444
void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) {
3073
444
  keepAncestorBraces();
3074
3075
444
  if (isBlockBegin(*FormatTok)) {
3076
318
    FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3077
318
    FormatToken *LeftBrace = FormatTok;
3078
318
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
3079
318
    parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
3080
318
               /*MunchSemi=*/true, KeepBraces);
3081
318
    setPreviousRBraceType(TT_ControlStatementRBrace);
3082
318
    if (!KeepBraces) {
3083
0
      assert(!NestedTooDeep.empty());
3084
0
      if (!NestedTooDeep.back())
3085
0
        markOptionalBraces(LeftBrace);
3086
0
    }
3087
318
    if (WrapRightBrace)
3088
252
      addUnwrappedLine();
3089
318
  } else {
3090
126
    parseUnbracedBody();
3091
126
  }
3092
3093
444
  if (!KeepBraces)
3094
0
    NestedTooDeep.pop_back();
3095
444
}
3096
3097
381
void UnwrappedLineParser::parseForOrWhileLoop(bool HasParens) {
3098
381
  assert((FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) ||
3099
381
          (Style.isVerilog() &&
3100
381
           FormatTok->isOneOf(Keywords.kw_always, Keywords.kw_always_comb,
3101
381
                              Keywords.kw_always_ff, Keywords.kw_always_latch,
3102
381
                              Keywords.kw_final, Keywords.kw_initial,
3103
381
                              Keywords.kw_foreach, Keywords.kw_forever,
3104
381
                              Keywords.kw_repeat))) &&
3105
381
         "'for', 'while' or foreach macro expected");
3106
381
  const bool KeepBraces = !Style.RemoveBracesLLVM ||
3107
381
                          !FormatTok->isOneOf(tok::kw_for, tok::kw_while);
3108
3109
381
  nextToken();
3110
  // JS' for await ( ...
3111
381
  if (Style.isJavaScript() && FormatTok->is(Keywords.kw_await))
3112
0
    nextToken();
3113
381
  if (Style.isCpp() && FormatTok->is(tok::kw_co_await))
3114
0
    nextToken();
3115
381
  if (HasParens && FormatTok->is(tok::l_paren)) {
3116
    // The type is only set for Verilog basically because we were afraid to
3117
    // change the existing behavior for loops. See the discussion on D121756 for
3118
    // details.
3119
381
    if (Style.isVerilog())
3120
0
      FormatTok->setFinalizedType(TT_ConditionLParen);
3121
381
    parseParens();
3122
381
  }
3123
3124
381
  if (Style.isVerilog()) {
3125
    // Event control.
3126
0
    parseVerilogSensitivityList();
3127
381
  } else if (Style.AllowShortLoopsOnASingleLine && FormatTok->is(tok::semi) &&
3128
381
             Tokens->getPreviousToken()->is(tok::r_paren)) {
3129
3
    nextToken();
3130
3
    addUnwrappedLine();
3131
3
    return;
3132
3
  }
3133
3134
378
  handleAttributes();
3135
378
  parseLoopBody(KeepBraces, /*WrapRightBrace=*/true);
3136
378
}
3137
3138
66
void UnwrappedLineParser::parseDoWhile() {
3139
66
  assert(FormatTok->is(tok::kw_do) && "'do' expected");
3140
0
  nextToken();
3141
3142
66
  parseLoopBody(/*KeepBraces=*/true, Style.BraceWrapping.BeforeWhile);
3143
3144
  // FIXME: Add error handling.
3145
66
  if (FormatTok->isNot(tok::kw_while)) {
3146
24
    addUnwrappedLine();
3147
24
    return;
3148
24
  }
3149
3150
42
  FormatTok->setFinalizedType(TT_DoWhile);
3151
3152
  // If in Whitesmiths mode, the line with the while() needs to be indented
3153
  // to the same level as the block.
3154
42
  if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)
3155
0
    ++Line->Level;
3156
3157
42
  nextToken();
3158
42
  parseStructuralElement();
3159
42
}
3160
3161
42.6k
void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) {
3162
42.6k
  nextToken();
3163
42.6k
  unsigned OldLineLevel = Line->Level;
3164
42.6k
  if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
3165
42.6k
    --Line->Level;
3166
42.6k
  if (LeftAlignLabel)
3167
0
    Line->Level = 0;
3168
3169
42.6k
  if (!Style.IndentCaseBlocks && CommentsBeforeNextToken.empty() &&
3170
42.6k
      FormatTok->is(tok::l_brace)) {
3171
3172
25.7k
    CompoundStatementIndenter Indenter(this, Line->Level,
3173
25.7k
                                       Style.BraceWrapping.AfterCaseLabel,
3174
25.7k
                                       Style.BraceWrapping.IndentBraces);
3175
25.7k
    parseBlock();
3176
25.7k
    if (FormatTok->is(tok::kw_break)) {
3177
0
      if (Style.BraceWrapping.AfterControlStatement ==
3178
0
          FormatStyle::BWACS_Always) {
3179
0
        addUnwrappedLine();
3180
0
        if (!Style.IndentCaseBlocks &&
3181
0
            Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
3182
0
          ++Line->Level;
3183
0
        }
3184
0
      }
3185
0
      parseStructuralElement();
3186
0
    }
3187
25.7k
    addUnwrappedLine();
3188
25.7k
  } else {
3189
16.8k
    if (FormatTok->is(tok::semi))
3190
0
      nextToken();
3191
16.8k
    addUnwrappedLine();
3192
16.8k
  }
3193
42.6k
  Line->Level = OldLineLevel;
3194
42.6k
  if (FormatTok->isNot(tok::l_brace)) {
3195
42.6k
    parseStructuralElement();
3196
42.6k
    addUnwrappedLine();
3197
42.6k
  }
3198
42.6k
}
3199
3200
6
void UnwrappedLineParser::parseCaseLabel() {
3201
6
  assert(FormatTok->is(tok::kw_case) && "'case' expected");
3202
3203
  // FIXME: fix handling of complex expressions here.
3204
1.20k
  do {
3205
1.20k
    nextToken();
3206
1.20k
    if (FormatTok->is(tok::colon)) {
3207
6
      FormatTok->setFinalizedType(TT_CaseLabelColon);
3208
6
      break;
3209
6
    }
3210
1.20k
  } while (!eof());
3211
0
  parseLabel();
3212
6
}
3213
3214
0
void UnwrappedLineParser::parseSwitch() {
3215
0
  assert(FormatTok->is(tok::kw_switch) && "'switch' expected");
3216
0
  nextToken();
3217
0
  if (FormatTok->is(tok::l_paren))
3218
0
    parseParens();
3219
3220
0
  keepAncestorBraces();
3221
3222
0
  if (FormatTok->is(tok::l_brace)) {
3223
0
    CompoundStatementIndenter Indenter(this, Style, Line->Level);
3224
0
    FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3225
0
    parseBlock();
3226
0
    setPreviousRBraceType(TT_ControlStatementRBrace);
3227
0
    addUnwrappedLine();
3228
0
  } else {
3229
0
    addUnwrappedLine();
3230
0
    ++Line->Level;
3231
0
    parseStructuralElement();
3232
0
    --Line->Level;
3233
0
  }
3234
3235
0
  if (Style.RemoveBracesLLVM)
3236
0
    NestedTooDeep.pop_back();
3237
0
}
3238
3239
// Operators that can follow a C variable.
3240
0
static bool isCOperatorFollowingVar(tok::TokenKind kind) {
3241
0
  switch (kind) {
3242
0
  case tok::ampamp:
3243
0
  case tok::ampequal:
3244
0
  case tok::arrow:
3245
0
  case tok::caret:
3246
0
  case tok::caretequal:
3247
0
  case tok::comma:
3248
0
  case tok::ellipsis:
3249
0
  case tok::equal:
3250
0
  case tok::equalequal:
3251
0
  case tok::exclaim:
3252
0
  case tok::exclaimequal:
3253
0
  case tok::greater:
3254
0
  case tok::greaterequal:
3255
0
  case tok::greatergreater:
3256
0
  case tok::greatergreaterequal:
3257
0
  case tok::l_paren:
3258
0
  case tok::l_square:
3259
0
  case tok::less:
3260
0
  case tok::lessequal:
3261
0
  case tok::lessless:
3262
0
  case tok::lesslessequal:
3263
0
  case tok::minus:
3264
0
  case tok::minusequal:
3265
0
  case tok::minusminus:
3266
0
  case tok::percent:
3267
0
  case tok::percentequal:
3268
0
  case tok::period:
3269
0
  case tok::pipe:
3270
0
  case tok::pipeequal:
3271
0
  case tok::pipepipe:
3272
0
  case tok::plus:
3273
0
  case tok::plusequal:
3274
0
  case tok::plusplus:
3275
0
  case tok::question:
3276
0
  case tok::r_brace:
3277
0
  case tok::r_paren:
3278
0
  case tok::r_square:
3279
0
  case tok::semi:
3280
0
  case tok::slash:
3281
0
  case tok::slashequal:
3282
0
  case tok::star:
3283
0
  case tok::starequal:
3284
0
    return true;
3285
0
  default:
3286
0
    return false;
3287
0
  }
3288
0
}
3289
3290
0
void UnwrappedLineParser::parseAccessSpecifier() {
3291
0
  FormatToken *AccessSpecifierCandidate = FormatTok;
3292
0
  nextToken();
3293
  // Understand Qt's slots.
3294
0
  if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
3295
0
    nextToken();
3296
  // Otherwise, we don't know what it is, and we'd better keep the next token.
3297
0
  if (FormatTok->is(tok::colon)) {
3298
0
    nextToken();
3299
0
    addUnwrappedLine();
3300
0
  } else if (FormatTok->isNot(tok::coloncolon) &&
3301
0
             !isCOperatorFollowingVar(FormatTok->Tok.getKind())) {
3302
    // Not a variable name nor namespace name.
3303
0
    addUnwrappedLine();
3304
0
  } else if (AccessSpecifierCandidate) {
3305
    // Consider the access specifier to be a C identifier.
3306
0
    AccessSpecifierCandidate->Tok.setKind(tok::identifier);
3307
0
  }
3308
0
}
3309
3310
/// \brief Parses a requires, decides if it is a clause or an expression.
3311
/// \pre The current token has to be the requires keyword.
3312
/// \returns true if it parsed a clause.
3313
0
bool clang::format::UnwrappedLineParser::parseRequires() {
3314
0
  assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3315
0
  auto RequiresToken = FormatTok;
3316
3317
  // We try to guess if it is a requires clause, or a requires expression. For
3318
  // that we first consume the keyword and check the next token.
3319
0
  nextToken();
3320
3321
0
  switch (FormatTok->Tok.getKind()) {
3322
0
  case tok::l_brace:
3323
    // This can only be an expression, never a clause.
3324
0
    parseRequiresExpression(RequiresToken);
3325
0
    return false;
3326
0
  case tok::l_paren:
3327
    // Clauses and expression can start with a paren, it's unclear what we have.
3328
0
    break;
3329
0
  default:
3330
    // All other tokens can only be a clause.
3331
0
    parseRequiresClause(RequiresToken);
3332
0
    return true;
3333
0
  }
3334
3335
  // Looking forward we would have to decide if there are function declaration
3336
  // like arguments to the requires expression:
3337
  // requires (T t) {
3338
  // Or there is a constraint expression for the requires clause:
3339
  // requires (C<T> && ...
3340
3341
  // But first let's look behind.
3342
0
  auto *PreviousNonComment = RequiresToken->getPreviousNonComment();
3343
3344
0
  if (!PreviousNonComment ||
3345
0
      PreviousNonComment->is(TT_RequiresExpressionLBrace)) {
3346
    // If there is no token, or an expression left brace, we are a requires
3347
    // clause within a requires expression.
3348
0
    parseRequiresClause(RequiresToken);
3349
0
    return true;
3350
0
  }
3351
3352
0
  switch (PreviousNonComment->Tok.getKind()) {
3353
0
  case tok::greater:
3354
0
  case tok::r_paren:
3355
0
  case tok::kw_noexcept:
3356
0
  case tok::kw_const:
3357
    // This is a requires clause.
3358
0
    parseRequiresClause(RequiresToken);
3359
0
    return true;
3360
0
  case tok::amp:
3361
0
  case tok::ampamp: {
3362
    // This can be either:
3363
    // if (... && requires (T t) ...)
3364
    // Or
3365
    // void member(...) && requires (C<T> ...
3366
    // We check the one token before that for a const:
3367
    // void member(...) const && requires (C<T> ...
3368
0
    auto PrevPrev = PreviousNonComment->getPreviousNonComment();
3369
0
    if (PrevPrev && PrevPrev->is(tok::kw_const)) {
3370
0
      parseRequiresClause(RequiresToken);
3371
0
      return true;
3372
0
    }
3373
0
    break;
3374
0
  }
3375
0
  default:
3376
0
    if (PreviousNonComment->isTypeOrIdentifier()) {
3377
      // This is a requires clause.
3378
0
      parseRequiresClause(RequiresToken);
3379
0
      return true;
3380
0
    }
3381
    // It's an expression.
3382
0
    parseRequiresExpression(RequiresToken);
3383
0
    return false;
3384
0
  }
3385
3386
  // Now we look forward and try to check if the paren content is a parameter
3387
  // list. The parameters can be cv-qualified and contain references or
3388
  // pointers.
3389
  // So we want basically to check for TYPE NAME, but TYPE can contain all kinds
3390
  // of stuff: typename, const, *, &, &&, ::, identifiers.
3391
3392
0
  unsigned StoredPosition = Tokens->getPosition();
3393
0
  FormatToken *NextToken = Tokens->getNextToken();
3394
0
  int Lookahead = 0;
3395
0
  auto PeekNext = [&Lookahead, &NextToken, this] {
3396
0
    ++Lookahead;
3397
0
    NextToken = Tokens->getNextToken();
3398
0
  };
3399
3400
0
  bool FoundType = false;
3401
0
  bool LastWasColonColon = false;
3402
0
  int OpenAngles = 0;
3403
3404
0
  for (; Lookahead < 50; PeekNext()) {
3405
0
    switch (NextToken->Tok.getKind()) {
3406
0
    case tok::kw_volatile:
3407
0
    case tok::kw_const:
3408
0
    case tok::comma:
3409
0
      if (OpenAngles == 0) {
3410
0
        FormatTok = Tokens->setPosition(StoredPosition);
3411
0
        parseRequiresExpression(RequiresToken);
3412
0
        return false;
3413
0
      }
3414
0
      break;
3415
0
    case tok::r_paren:
3416
0
    case tok::pipepipe:
3417
0
      FormatTok = Tokens->setPosition(StoredPosition);
3418
0
      parseRequiresClause(RequiresToken);
3419
0
      return true;
3420
0
    case tok::eof:
3421
      // Break out of the loop.
3422
0
      Lookahead = 50;
3423
0
      break;
3424
0
    case tok::coloncolon:
3425
0
      LastWasColonColon = true;
3426
0
      break;
3427
0
    case tok::identifier:
3428
0
      if (FoundType && !LastWasColonColon && OpenAngles == 0) {
3429
0
        FormatTok = Tokens->setPosition(StoredPosition);
3430
0
        parseRequiresExpression(RequiresToken);
3431
0
        return false;
3432
0
      }
3433
0
      FoundType = true;
3434
0
      LastWasColonColon = false;
3435
0
      break;
3436
0
    case tok::less:
3437
0
      ++OpenAngles;
3438
0
      break;
3439
0
    case tok::greater:
3440
0
      --OpenAngles;
3441
0
      break;
3442
0
    default:
3443
0
      if (NextToken->isSimpleTypeSpecifier()) {
3444
0
        FormatTok = Tokens->setPosition(StoredPosition);
3445
0
        parseRequiresExpression(RequiresToken);
3446
0
        return false;
3447
0
      }
3448
0
      break;
3449
0
    }
3450
0
  }
3451
  // This seems to be a complicated expression, just assume it's a clause.
3452
0
  FormatTok = Tokens->setPosition(StoredPosition);
3453
0
  parseRequiresClause(RequiresToken);
3454
0
  return true;
3455
0
}
3456
3457
/// \brief Parses a requires clause.
3458
/// \param RequiresToken The requires keyword token, which starts this clause.
3459
/// \pre We need to be on the next token after the requires keyword.
3460
/// \sa parseRequiresExpression
3461
///
3462
/// Returns if it either has finished parsing the clause, or it detects, that
3463
/// the clause is incorrect.
3464
0
void UnwrappedLineParser::parseRequiresClause(FormatToken *RequiresToken) {
3465
0
  assert(FormatTok->getPreviousNonComment() == RequiresToken);
3466
0
  assert(RequiresToken->is(tok::kw_requires) && "'requires' expected");
3467
3468
  // If there is no previous token, we are within a requires expression,
3469
  // otherwise we will always have the template or function declaration in front
3470
  // of it.
3471
0
  bool InRequiresExpression =
3472
0
      !RequiresToken->Previous ||
3473
0
      RequiresToken->Previous->is(TT_RequiresExpressionLBrace);
3474
3475
0
  RequiresToken->setFinalizedType(InRequiresExpression
3476
0
                                      ? TT_RequiresClauseInARequiresExpression
3477
0
                                      : TT_RequiresClause);
3478
3479
  // NOTE: parseConstraintExpression is only ever called from this function.
3480
  // It could be inlined into here.
3481
0
  parseConstraintExpression();
3482
3483
0
  if (!InRequiresExpression)
3484
0
    FormatTok->Previous->ClosesRequiresClause = true;
3485
0
}
3486
3487
/// \brief Parses a requires expression.
3488
/// \param RequiresToken The requires keyword token, which starts this clause.
3489
/// \pre We need to be on the next token after the requires keyword.
3490
/// \sa parseRequiresClause
3491
///
3492
/// Returns if it either has finished parsing the expression, or it detects,
3493
/// that the expression is incorrect.
3494
0
void UnwrappedLineParser::parseRequiresExpression(FormatToken *RequiresToken) {
3495
0
  assert(FormatTok->getPreviousNonComment() == RequiresToken);
3496
0
  assert(RequiresToken->is(tok::kw_requires) && "'requires' expected");
3497
3498
0
  RequiresToken->setFinalizedType(TT_RequiresExpression);
3499
3500
0
  if (FormatTok->is(tok::l_paren)) {
3501
0
    FormatTok->setFinalizedType(TT_RequiresExpressionLParen);
3502
0
    parseParens();
3503
0
  }
3504
3505
0
  if (FormatTok->is(tok::l_brace)) {
3506
0
    FormatTok->setFinalizedType(TT_RequiresExpressionLBrace);
3507
0
    parseChildBlock();
3508
0
  }
3509
0
}
3510
3511
/// \brief Parses a constraint expression.
3512
///
3513
/// This is the body of a requires clause. It returns, when the parsing is
3514
/// complete, or the expression is incorrect.
3515
0
void UnwrappedLineParser::parseConstraintExpression() {
3516
  // The special handling for lambdas is needed since tryToParseLambda() eats a
3517
  // token and if a requires expression is the last part of a requires clause
3518
  // and followed by an attribute like [[nodiscard]] the ClosesRequiresClause is
3519
  // not set on the correct token. Thus we need to be aware if we even expect a
3520
  // lambda to be possible.
3521
  // template <typename T> requires requires { ... } [[nodiscard]] ...;
3522
0
  bool LambdaNextTimeAllowed = true;
3523
3524
  // Within lambda declarations, it is permitted to put a requires clause after
3525
  // its template parameter list, which would place the requires clause right
3526
  // before the parentheses of the parameters of the lambda declaration. Thus,
3527
  // we track if we expect to see grouping parentheses at all.
3528
  // Without this check, `requires foo<T> (T t)` in the below example would be
3529
  // seen as the whole requires clause, accidentally eating the parameters of
3530
  // the lambda.
3531
  // [&]<typename T> requires foo<T> (T t) { ... };
3532
0
  bool TopLevelParensAllowed = true;
3533
3534
0
  do {
3535
0
    bool LambdaThisTimeAllowed = std::exchange(LambdaNextTimeAllowed, false);
3536
3537
0
    switch (FormatTok->Tok.getKind()) {
3538
0
    case tok::kw_requires: {
3539
0
      auto RequiresToken = FormatTok;
3540
0
      nextToken();
3541
0
      parseRequiresExpression(RequiresToken);
3542
0
      break;
3543
0
    }
3544
3545
0
    case tok::l_paren:
3546
0
      if (!TopLevelParensAllowed)
3547
0
        return;
3548
0
      parseParens(/*AmpAmpTokenType=*/TT_BinaryOperator);
3549
0
      TopLevelParensAllowed = false;
3550
0
      break;
3551
3552
0
    case tok::l_square:
3553
0
      if (!LambdaThisTimeAllowed || !tryToParseLambda())
3554
0
        return;
3555
0
      break;
3556
3557
0
    case tok::kw_const:
3558
0
    case tok::semi:
3559
0
    case tok::kw_class:
3560
0
    case tok::kw_struct:
3561
0
    case tok::kw_union:
3562
0
      return;
3563
3564
0
    case tok::l_brace:
3565
      // Potential function body.
3566
0
      return;
3567
3568
0
    case tok::ampamp:
3569
0
    case tok::pipepipe:
3570
0
      FormatTok->setFinalizedType(TT_BinaryOperator);
3571
0
      nextToken();
3572
0
      LambdaNextTimeAllowed = true;
3573
0
      TopLevelParensAllowed = true;
3574
0
      break;
3575
3576
0
    case tok::comma:
3577
0
    case tok::comment:
3578
0
      LambdaNextTimeAllowed = LambdaThisTimeAllowed;
3579
0
      nextToken();
3580
0
      break;
3581
3582
0
    case tok::kw_sizeof:
3583
0
    case tok::greater:
3584
0
    case tok::greaterequal:
3585
0
    case tok::greatergreater:
3586
0
    case tok::less:
3587
0
    case tok::lessequal:
3588
0
    case tok::lessless:
3589
0
    case tok::equalequal:
3590
0
    case tok::exclaim:
3591
0
    case tok::exclaimequal:
3592
0
    case tok::plus:
3593
0
    case tok::minus:
3594
0
    case tok::star:
3595
0
    case tok::slash:
3596
0
      LambdaNextTimeAllowed = true;
3597
0
      TopLevelParensAllowed = true;
3598
      // Just eat them.
3599
0
      nextToken();
3600
0
      break;
3601
3602
0
    case tok::numeric_constant:
3603
0
    case tok::coloncolon:
3604
0
    case tok::kw_true:
3605
0
    case tok::kw_false:
3606
0
      TopLevelParensAllowed = false;
3607
      // Just eat them.
3608
0
      nextToken();
3609
0
      break;
3610
3611
0
    case tok::kw_static_cast:
3612
0
    case tok::kw_const_cast:
3613
0
    case tok::kw_reinterpret_cast:
3614
0
    case tok::kw_dynamic_cast:
3615
0
      nextToken();
3616
0
      if (FormatTok->isNot(tok::less))
3617
0
        return;
3618
3619
0
      nextToken();
3620
0
      parseBracedList(/*IsAngleBracket=*/true);
3621
0
      break;
3622
3623
0
    default:
3624
0
      if (!FormatTok->Tok.getIdentifierInfo()) {
3625
        // Identifiers are part of the default case, we check for more then
3626
        // tok::identifier to handle builtin type traits.
3627
0
        return;
3628
0
      }
3629
3630
      // We need to differentiate identifiers for a template deduction guide,
3631
      // variables, or function return types (the constraint expression has
3632
      // ended before that), and basically all other cases. But it's easier to
3633
      // check the other way around.
3634
0
      assert(FormatTok->Previous);
3635
0
      switch (FormatTok->Previous->Tok.getKind()) {
3636
0
      case tok::coloncolon:  // Nested identifier.
3637
0
      case tok::ampamp:      // Start of a function or variable for the
3638
0
      case tok::pipepipe:    // constraint expression. (binary)
3639
0
      case tok::exclaim:     // The same as above, but unary.
3640
0
      case tok::kw_requires: // Initial identifier of a requires clause.
3641
0
      case tok::equal:       // Initial identifier of a concept declaration.
3642
0
        break;
3643
0
      default:
3644
0
        return;
3645
0
      }
3646
3647
      // Read identifier with optional template declaration.
3648
0
      nextToken();
3649
0
      if (FormatTok->is(tok::less)) {
3650
0
        nextToken();
3651
0
        parseBracedList(/*IsAngleBracket=*/true);
3652
0
      }
3653
0
      TopLevelParensAllowed = false;
3654
0
      break;
3655
0
    }
3656
0
  } while (!eof());
3657
0
}
3658
3659
192
bool UnwrappedLineParser::parseEnum() {
3660
192
  const FormatToken &InitialToken = *FormatTok;
3661
3662
  // Won't be 'enum' for NS_ENUMs.
3663
192
  if (FormatTok->is(tok::kw_enum))
3664
192
    nextToken();
3665
3666
  // In TypeScript, "enum" can also be used as property name, e.g. in interface
3667
  // declarations. An "enum" keyword followed by a colon would be a syntax
3668
  // error and thus assume it is just an identifier.
3669
192
  if (Style.isJavaScript() && FormatTok->isOneOf(tok::colon, tok::question))
3670
0
    return false;
3671
3672
  // In protobuf, "enum" can be used as a field name.
3673
192
  if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal))
3674
0
    return false;
3675
3676
  // Eat up enum class ...
3677
192
  if (FormatTok->isOneOf(tok::kw_class, tok::kw_struct))
3678
0
    nextToken();
3679
3680
192
  while (FormatTok->Tok.getIdentifierInfo() ||
3681
192
         FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
3682
192
                            tok::greater, tok::comma, tok::question,
3683
192
                            tok::l_square, tok::r_square)) {
3684
0
    if (Style.isVerilog()) {
3685
0
      FormatTok->setFinalizedType(TT_VerilogDimensionedTypeName);
3686
0
      nextToken();
3687
      // In Verilog the base type can have dimensions.
3688
0
      while (FormatTok->is(tok::l_square))
3689
0
        parseSquare();
3690
0
    } else {
3691
0
      nextToken();
3692
0
    }
3693
    // We can have macros or attributes in between 'enum' and the enum name.
3694
0
    if (FormatTok->is(tok::l_paren))
3695
0
      parseParens();
3696
0
    assert(FormatTok->isNot(TT_AttributeSquare));
3697
0
    if (FormatTok->is(tok::identifier)) {
3698
0
      nextToken();
3699
      // If there are two identifiers in a row, this is likely an elaborate
3700
      // return type. In Java, this can be "implements", etc.
3701
0
      if (Style.isCpp() && FormatTok->is(tok::identifier))
3702
0
        return false;
3703
0
    }
3704
0
  }
3705
3706
  // Just a declaration or something is wrong.
3707
192
  if (FormatTok->isNot(tok::l_brace))
3708
0
    return true;
3709
192
  FormatTok->setFinalizedType(TT_EnumLBrace);
3710
192
  FormatTok->setBlockKind(BK_Block);
3711
3712
192
  if (Style.Language == FormatStyle::LK_Java) {
3713
    // Java enums are different.
3714
0
    parseJavaEnumBody();
3715
0
    return true;
3716
0
  }
3717
192
  if (Style.Language == FormatStyle::LK_Proto) {
3718
0
    parseBlock(/*MustBeDeclaration=*/true);
3719
0
    return true;
3720
0
  }
3721
3722
192
  if (!Style.AllowShortEnumsOnASingleLine &&
3723
192
      ShouldBreakBeforeBrace(Style, InitialToken)) {
3724
0
    addUnwrappedLine();
3725
0
  }
3726
  // Parse enum body.
3727
192
  nextToken();
3728
192
  if (!Style.AllowShortEnumsOnASingleLine) {
3729
0
    addUnwrappedLine();
3730
0
    Line->Level += 1;
3731
0
  }
3732
192
  bool HasError = !parseBracedList(/*IsAngleBracket=*/false, /*IsEnum=*/true);
3733
192
  if (!Style.AllowShortEnumsOnASingleLine)
3734
0
    Line->Level -= 1;
3735
192
  if (HasError) {
3736
27
    if (FormatTok->is(tok::semi))
3737
0
      nextToken();
3738
27
    addUnwrappedLine();
3739
27
  }
3740
192
  setPreviousRBraceType(TT_EnumRBrace);
3741
192
  return true;
3742
3743
  // There is no addUnwrappedLine() here so that we fall through to parsing a
3744
  // structural element afterwards. Thus, in "enum A {} n, m;",
3745
  // "} n, m;" will end up in one unwrapped line.
3746
192
}
3747
3748
1.02k
bool UnwrappedLineParser::parseStructLike() {
3749
  // parseRecord falls through and does not yet add an unwrapped line as a
3750
  // record declaration or definition can start a structural element.
3751
1.02k
  parseRecord();
3752
  // This does not apply to Java, JavaScript and C#.
3753
1.02k
  if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() ||
3754
1.02k
      Style.isCSharp()) {
3755
0
    if (FormatTok->is(tok::semi))
3756
0
      nextToken();
3757
0
    addUnwrappedLine();
3758
0
    return true;
3759
0
  }
3760
1.02k
  return false;
3761
1.02k
}
3762
3763
namespace {
3764
// A class used to set and restore the Token position when peeking
3765
// ahead in the token source.
3766
class ScopedTokenPosition {
3767
  unsigned StoredPosition;
3768
  FormatTokenSource *Tokens;
3769
3770
public:
3771
2.77k
  ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) {
3772
2.77k
    assert(Tokens && "Tokens expected to not be null");
3773
0
    StoredPosition = Tokens->getPosition();
3774
2.77k
  }
3775
3776
2.77k
  ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); }
3777
};
3778
} // namespace
3779
3780
// Look to see if we have [[ by looking ahead, if
3781
// its not then rewind to the original position.
3782
2.77k
bool UnwrappedLineParser::tryToParseSimpleAttribute() {
3783
2.77k
  ScopedTokenPosition AutoPosition(Tokens);
3784
2.77k
  FormatToken *Tok = Tokens->getNextToken();
3785
  // We already read the first [ check for the second.
3786
2.77k
  if (Tok->isNot(tok::l_square))
3787
2.75k
    return false;
3788
  // Double check that the attribute is just something
3789
  // fairly simple.
3790
270
  while (Tok->isNot(tok::eof)) {
3791
264
    if (Tok->is(tok::r_square))
3792
12
      break;
3793
252
    Tok = Tokens->getNextToken();
3794
252
  }
3795
18
  if (Tok->is(tok::eof))
3796
6
    return false;
3797
12
  Tok = Tokens->getNextToken();
3798
12
  if (Tok->isNot(tok::r_square))
3799
12
    return false;
3800
0
  Tok = Tokens->getNextToken();
3801
0
  if (Tok->is(tok::semi))
3802
0
    return false;
3803
0
  return true;
3804
0
}
3805
3806
0
void UnwrappedLineParser::parseJavaEnumBody() {
3807
0
  assert(FormatTok->is(tok::l_brace));
3808
0
  const FormatToken *OpeningBrace = FormatTok;
3809
3810
  // Determine whether the enum is simple, i.e. does not have a semicolon or
3811
  // constants with class bodies. Simple enums can be formatted like braced
3812
  // lists, contracted to a single line, etc.
3813
0
  unsigned StoredPosition = Tokens->getPosition();
3814
0
  bool IsSimple = true;
3815
0
  FormatToken *Tok = Tokens->getNextToken();
3816
0
  while (Tok->isNot(tok::eof)) {
3817
0
    if (Tok->is(tok::r_brace))
3818
0
      break;
3819
0
    if (Tok->isOneOf(tok::l_brace, tok::semi)) {
3820
0
      IsSimple = false;
3821
0
      break;
3822
0
    }
3823
    // FIXME: This will also mark enums with braces in the arguments to enum
3824
    // constants as "not simple". This is probably fine in practice, though.
3825
0
    Tok = Tokens->getNextToken();
3826
0
  }
3827
0
  FormatTok = Tokens->setPosition(StoredPosition);
3828
3829
0
  if (IsSimple) {
3830
0
    nextToken();
3831
0
    parseBracedList();
3832
0
    addUnwrappedLine();
3833
0
    return;
3834
0
  }
3835
3836
  // Parse the body of a more complex enum.
3837
  // First add a line for everything up to the "{".
3838
0
  nextToken();
3839
0
  addUnwrappedLine();
3840
0
  ++Line->Level;
3841
3842
  // Parse the enum constants.
3843
0
  while (!eof()) {
3844
0
    if (FormatTok->is(tok::l_brace)) {
3845
      // Parse the constant's class body.
3846
0
      parseBlock(/*MustBeDeclaration=*/true, /*AddLevels=*/1u,
3847
0
                 /*MunchSemi=*/false);
3848
0
    } else if (FormatTok->is(tok::l_paren)) {
3849
0
      parseParens();
3850
0
    } else if (FormatTok->is(tok::comma)) {
3851
0
      nextToken();
3852
0
      addUnwrappedLine();
3853
0
    } else if (FormatTok->is(tok::semi)) {
3854
0
      nextToken();
3855
0
      addUnwrappedLine();
3856
0
      break;
3857
0
    } else if (FormatTok->is(tok::r_brace)) {
3858
0
      addUnwrappedLine();
3859
0
      break;
3860
0
    } else {
3861
0
      nextToken();
3862
0
    }
3863
0
  }
3864
3865
  // Parse the class body after the enum's ";" if any.
3866
0
  parseLevel(OpeningBrace);
3867
0
  nextToken();
3868
0
  --Line->Level;
3869
0
  addUnwrappedLine();
3870
0
}
3871
3872
1.02k
void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
3873
1.02k
  const FormatToken &InitialToken = *FormatTok;
3874
1.02k
  nextToken();
3875
3876
  // The actual identifier can be a nested name specifier, and in macros
3877
  // it is often token-pasted.
3878
  // An [[attribute]] can be before the identifier.
3879
1.11k
  while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
3880
1.11k
                            tok::kw_alignas, tok::l_square) ||
3881
1.11k
         FormatTok->isAttribute() ||
3882
1.11k
         ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
3883
1.02k
          FormatTok->isOneOf(tok::period, tok::comma))) {
3884
81
    if (Style.isJavaScript() &&
3885
81
        FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
3886
      // JavaScript/TypeScript supports inline object types in
3887
      // extends/implements positions:
3888
      //     class Foo implements {bar: number} { }
3889
0
      nextToken();
3890
0
      if (FormatTok->is(tok::l_brace)) {
3891
0
        tryToParseBracedList();
3892
0
        continue;
3893
0
      }
3894
0
    }
3895
81
    if (FormatTok->is(tok::l_square) && handleCppAttributes())
3896
0
      continue;
3897
81
    bool IsNonMacroIdentifier =
3898
81
        FormatTok->is(tok::identifier) &&
3899
81
        FormatTok->TokenText != FormatTok->TokenText.upper();
3900
81
    nextToken();
3901
    // We can have macros in between 'class' and the class name.
3902
81
    if (!IsNonMacroIdentifier && FormatTok->is(tok::l_paren))
3903
0
      parseParens();
3904
81
  }
3905
3906
  // Note that parsing away template declarations here leads to incorrectly
3907
  // accepting function declarations as record declarations.
3908
  // In general, we cannot solve this problem. Consider:
3909
  // class A<int> B() {}
3910
  // which can be a function definition or a class definition when B() is a
3911
  // macro. If we find enough real-world cases where this is a problem, we
3912
  // can parse for the 'template' keyword in the beginning of the statement,
3913
  // and thus rule out the record production in case there is no template
3914
  // (this would still leave us with an ambiguity between template function
3915
  // and class declarations).
3916
1.02k
  if (FormatTok->isOneOf(tok::colon, tok::less)) {
3917
3.06k
    do {
3918
3.06k
      if (FormatTok->is(tok::l_brace)) {
3919
75
        calculateBraceTypes(/*ExpectClassBody=*/true);
3920
75
        if (!tryToParseBracedList())
3921
57
          break;
3922
75
      }
3923
3.01k
      if (FormatTok->is(tok::l_square)) {
3924
0
        FormatToken *Previous = FormatTok->Previous;
3925
0
        if (!Previous ||
3926
0
            !(Previous->is(tok::r_paren) || Previous->isTypeOrIdentifier())) {
3927
          // Don't try parsing a lambda if we had a closing parenthesis before,
3928
          // it was probably a pointer to an array: int (*)[].
3929
0
          if (!tryToParseLambda())
3930
0
            continue;
3931
0
        } else {
3932
0
          parseSquare();
3933
0
          continue;
3934
0
        }
3935
0
      }
3936
3.01k
      if (FormatTok->is(tok::semi))
3937
0
        return;
3938
3.01k
      if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)) {
3939
0
        addUnwrappedLine();
3940
0
        nextToken();
3941
0
        parseCSharpGenericTypeConstraint();
3942
0
        break;
3943
0
      }
3944
3.01k
      nextToken();
3945
3.01k
    } while (!eof());
3946
57
  }
3947
3948
1.02k
  auto GetBraceTypes =
3949
1.02k
      [](const FormatToken &RecordTok) -> std::pair<TokenType, TokenType> {
3950
156
    switch (RecordTok.Tok.getKind()) {
3951
57
    case tok::kw_class:
3952
57
      return {TT_ClassLBrace, TT_ClassRBrace};
3953
99
    case tok::kw_struct:
3954
99
      return {TT_StructLBrace, TT_StructRBrace};
3955
0
    case tok::kw_union:
3956
0
      return {TT_UnionLBrace, TT_UnionRBrace};
3957
0
    default:
3958
      // Useful for e.g. interface.
3959
0
      return {TT_RecordLBrace, TT_RecordRBrace};
3960
156
    }
3961
156
  };
3962
1.02k
  if (FormatTok->is(tok::l_brace)) {
3963
156
    auto [OpenBraceType, ClosingBraceType] = GetBraceTypes(InitialToken);
3964
156
    FormatTok->setFinalizedType(OpenBraceType);
3965
156
    if (ParseAsExpr) {
3966
0
      parseChildBlock();
3967
156
    } else {
3968
156
      if (ShouldBreakBeforeBrace(Style, InitialToken))
3969
0
        addUnwrappedLine();
3970
3971
156
      unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u;
3972
156
      parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false);
3973
156
    }
3974
156
    setPreviousRBraceType(ClosingBraceType);
3975
156
  }
3976
  // There is no addUnwrappedLine() here so that we fall through to parsing a
3977
  // structural element afterwards. Thus, in "class A {} n, m;",
3978
  // "} n, m;" will end up in one unwrapped line.
3979
1.02k
}
3980
3981
0
void UnwrappedLineParser::parseObjCMethod() {
3982
0
  assert(FormatTok->isOneOf(tok::l_paren, tok::identifier) &&
3983
0
         "'(' or identifier expected.");
3984
0
  do {
3985
0
    if (FormatTok->is(tok::semi)) {
3986
0
      nextToken();
3987
0
      addUnwrappedLine();
3988
0
      return;
3989
0
    } else if (FormatTok->is(tok::l_brace)) {
3990
0
      if (Style.BraceWrapping.AfterFunction)
3991
0
        addUnwrappedLine();
3992
0
      parseBlock();
3993
0
      addUnwrappedLine();
3994
0
      return;
3995
0
    } else {
3996
0
      nextToken();
3997
0
    }
3998
0
  } while (!eof());
3999
0
}
4000
4001
0
void UnwrappedLineParser::parseObjCProtocolList() {
4002
0
  assert(FormatTok->is(tok::less) && "'<' expected.");
4003
0
  do {
4004
0
    nextToken();
4005
    // Early exit in case someone forgot a close angle.
4006
0
    if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
4007
0
        FormatTok->isObjCAtKeyword(tok::objc_end)) {
4008
0
      return;
4009
0
    }
4010
0
  } while (!eof() && FormatTok->isNot(tok::greater));
4011
0
  nextToken(); // Skip '>'.
4012
0
}
4013
4014
0
void UnwrappedLineParser::parseObjCUntilAtEnd() {
4015
0
  do {
4016
0
    if (FormatTok->isObjCAtKeyword(tok::objc_end)) {
4017
0
      nextToken();
4018
0
      addUnwrappedLine();
4019
0
      break;
4020
0
    }
4021
0
    if (FormatTok->is(tok::l_brace)) {
4022
0
      parseBlock();
4023
      // In ObjC interfaces, nothing should be following the "}".
4024
0
      addUnwrappedLine();
4025
0
    } else if (FormatTok->is(tok::r_brace)) {
4026
      // Ignore stray "}". parseStructuralElement doesn't consume them.
4027
0
      nextToken();
4028
0
      addUnwrappedLine();
4029
0
    } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
4030
0
      nextToken();
4031
0
      parseObjCMethod();
4032
0
    } else {
4033
0
      parseStructuralElement();
4034
0
    }
4035
0
  } while (!eof());
4036
0
}
4037
4038
0
void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
4039
0
  assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
4040
0
         FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
4041
0
  nextToken();
4042
0
  nextToken(); // interface name
4043
4044
  // @interface can be followed by a lightweight generic
4045
  // specialization list, then either a base class or a category.
4046
0
  if (FormatTok->is(tok::less))
4047
0
    parseObjCLightweightGenerics();
4048
0
  if (FormatTok->is(tok::colon)) {
4049
0
    nextToken();
4050
0
    nextToken(); // base class name
4051
    // The base class can also have lightweight generics applied to it.
4052
0
    if (FormatTok->is(tok::less))
4053
0
      parseObjCLightweightGenerics();
4054
0
  } else if (FormatTok->is(tok::l_paren)) {
4055
    // Skip category, if present.
4056
0
    parseParens();
4057
0
  }
4058
4059
0
  if (FormatTok->is(tok::less))
4060
0
    parseObjCProtocolList();
4061
4062
0
  if (FormatTok->is(tok::l_brace)) {
4063
0
    if (Style.BraceWrapping.AfterObjCDeclaration)
4064
0
      addUnwrappedLine();
4065
0
    parseBlock(/*MustBeDeclaration=*/true);
4066
0
  }
4067
4068
  // With instance variables, this puts '}' on its own line.  Without instance
4069
  // variables, this ends the @interface line.
4070
0
  addUnwrappedLine();
4071
4072
0
  parseObjCUntilAtEnd();
4073
0
}
4074
4075
0
void UnwrappedLineParser::parseObjCLightweightGenerics() {
4076
0
  assert(FormatTok->is(tok::less));
4077
  // Unlike protocol lists, generic parameterizations support
4078
  // nested angles:
4079
  //
4080
  // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
4081
  //     NSObject <NSCopying, NSSecureCoding>
4082
  //
4083
  // so we need to count how many open angles we have left.
4084
0
  unsigned NumOpenAngles = 1;
4085
0
  do {
4086
0
    nextToken();
4087
    // Early exit in case someone forgot a close angle.
4088
0
    if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
4089
0
        FormatTok->isObjCAtKeyword(tok::objc_end)) {
4090
0
      break;
4091
0
    }
4092
0
    if (FormatTok->is(tok::less)) {
4093
0
      ++NumOpenAngles;
4094
0
    } else if (FormatTok->is(tok::greater)) {
4095
0
      assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
4096
0
      --NumOpenAngles;
4097
0
    }
4098
0
  } while (!eof() && NumOpenAngles != 0);
4099
0
  nextToken(); // Skip '>'.
4100
0
}
4101
4102
// Returns true for the declaration/definition form of @protocol,
4103
// false for the expression form.
4104
0
bool UnwrappedLineParser::parseObjCProtocol() {
4105
0
  assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
4106
0
  nextToken();
4107
4108
0
  if (FormatTok->is(tok::l_paren)) {
4109
    // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
4110
0
    return false;
4111
0
  }
4112
4113
  // The definition/declaration form,
4114
  // @protocol Foo
4115
  // - (int)someMethod;
4116
  // @end
4117
4118
0
  nextToken(); // protocol name
4119
4120
0
  if (FormatTok->is(tok::less))
4121
0
    parseObjCProtocolList();
4122
4123
  // Check for protocol declaration.
4124
0
  if (FormatTok->is(tok::semi)) {
4125
0
    nextToken();
4126
0
    addUnwrappedLine();
4127
0
    return true;
4128
0
  }
4129
4130
0
  addUnwrappedLine();
4131
0
  parseObjCUntilAtEnd();
4132
0
  return true;
4133
0
}
4134
4135
0
void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
4136
0
  bool IsImport = FormatTok->is(Keywords.kw_import);
4137
0
  assert(IsImport || FormatTok->is(tok::kw_export));
4138
0
  nextToken();
4139
4140
  // Consume the "default" in "export default class/function".
4141
0
  if (FormatTok->is(tok::kw_default))
4142
0
    nextToken();
4143
4144
  // Consume "async function", "function" and "default function", so that these
4145
  // get parsed as free-standing JS functions, i.e. do not require a trailing
4146
  // semicolon.
4147
0
  if (FormatTok->is(Keywords.kw_async))
4148
0
    nextToken();
4149
0
  if (FormatTok->is(Keywords.kw_function)) {
4150
0
    nextToken();
4151
0
    return;
4152
0
  }
4153
4154
  // For imports, `export *`, `export {...}`, consume the rest of the line up
4155
  // to the terminating `;`. For everything else, just return and continue
4156
  // parsing the structural element, i.e. the declaration or expression for
4157
  // `export default`.
4158
0
  if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
4159
0
      !FormatTok->isStringLiteral() &&
4160
0
      !(FormatTok->is(Keywords.kw_type) &&
4161
0
        Tokens->peekNextToken()->isOneOf(tok::l_brace, tok::star))) {
4162
0
    return;
4163
0
  }
4164
4165
0
  while (!eof()) {
4166
0
    if (FormatTok->is(tok::semi))
4167
0
      return;
4168
0
    if (Line->Tokens.empty()) {
4169
      // Common issue: Automatic Semicolon Insertion wrapped the line, so the
4170
      // import statement should terminate.
4171
0
      return;
4172
0
    }
4173
0
    if (FormatTok->is(tok::l_brace)) {
4174
0
      FormatTok->setBlockKind(BK_Block);
4175
0
      nextToken();
4176
0
      parseBracedList();
4177
0
    } else {
4178
0
      nextToken();
4179
0
    }
4180
0
  }
4181
0
}
4182
4183
0
void UnwrappedLineParser::parseStatementMacro() {
4184
0
  nextToken();
4185
0
  if (FormatTok->is(tok::l_paren))
4186
0
    parseParens();
4187
0
  if (FormatTok->is(tok::semi))
4188
0
    nextToken();
4189
0
  addUnwrappedLine();
4190
0
}
4191
4192
0
void UnwrappedLineParser::parseVerilogHierarchyIdentifier() {
4193
  // consume things like a::`b.c[d:e] or a::*
4194
0
  while (true) {
4195
0
    if (FormatTok->isOneOf(tok::star, tok::period, tok::periodstar,
4196
0
                           tok::coloncolon, tok::hash) ||
4197
0
        Keywords.isVerilogIdentifier(*FormatTok)) {
4198
0
      nextToken();
4199
0
    } else if (FormatTok->is(tok::l_square)) {
4200
0
      parseSquare();
4201
0
    } else {
4202
0
      break;
4203
0
    }
4204
0
  }
4205
0
}
4206
4207
0
void UnwrappedLineParser::parseVerilogSensitivityList() {
4208
0
  if (FormatTok->isNot(tok::at))
4209
0
    return;
4210
0
  nextToken();
4211
  // A block event expression has 2 at signs.
4212
0
  if (FormatTok->is(tok::at))
4213
0
    nextToken();
4214
0
  switch (FormatTok->Tok.getKind()) {
4215
0
  case tok::star:
4216
0
    nextToken();
4217
0
    break;
4218
0
  case tok::l_paren:
4219
0
    parseParens();
4220
0
    break;
4221
0
  default:
4222
0
    parseVerilogHierarchyIdentifier();
4223
0
    break;
4224
0
  }
4225
0
}
4226
4227
0
unsigned UnwrappedLineParser::parseVerilogHierarchyHeader() {
4228
0
  unsigned AddLevels = 0;
4229
4230
0
  if (FormatTok->is(Keywords.kw_clocking)) {
4231
0
    nextToken();
4232
0
    if (Keywords.isVerilogIdentifier(*FormatTok))
4233
0
      nextToken();
4234
0
    parseVerilogSensitivityList();
4235
0
    if (FormatTok->is(tok::semi))
4236
0
      nextToken();
4237
0
  } else if (FormatTok->isOneOf(tok::kw_case, Keywords.kw_casex,
4238
0
                                Keywords.kw_casez, Keywords.kw_randcase,
4239
0
                                Keywords.kw_randsequence)) {
4240
0
    if (Style.IndentCaseLabels)
4241
0
      AddLevels++;
4242
0
    nextToken();
4243
0
    if (FormatTok->is(tok::l_paren)) {
4244
0
      FormatTok->setFinalizedType(TT_ConditionLParen);
4245
0
      parseParens();
4246
0
    }
4247
0
    if (FormatTok->isOneOf(Keywords.kw_inside, Keywords.kw_matches))
4248
0
      nextToken();
4249
    // The case header has no semicolon.
4250
0
  } else {
4251
    // "module" etc.
4252
0
    nextToken();
4253
    // all the words like the name of the module and specifiers like
4254
    // "automatic" and the width of function return type
4255
0
    while (true) {
4256
0
      if (FormatTok->is(tok::l_square)) {
4257
0
        auto Prev = FormatTok->getPreviousNonComment();
4258
0
        if (Prev && Keywords.isVerilogIdentifier(*Prev))
4259
0
          Prev->setFinalizedType(TT_VerilogDimensionedTypeName);
4260
0
        parseSquare();
4261
0
      } else if (Keywords.isVerilogIdentifier(*FormatTok) ||
4262
0
                 FormatTok->isOneOf(Keywords.kw_automatic, tok::kw_static)) {
4263
0
        nextToken();
4264
0
      } else {
4265
0
        break;
4266
0
      }
4267
0
    }
4268
4269
0
    auto NewLine = [this]() {
4270
0
      addUnwrappedLine();
4271
0
      Line->IsContinuation = true;
4272
0
    };
4273
4274
    // package imports
4275
0
    while (FormatTok->is(Keywords.kw_import)) {
4276
0
      NewLine();
4277
0
      nextToken();
4278
0
      parseVerilogHierarchyIdentifier();
4279
0
      if (FormatTok->is(tok::semi))
4280
0
        nextToken();
4281
0
    }
4282
4283
    // parameters and ports
4284
0
    if (FormatTok->is(Keywords.kw_verilogHash)) {
4285
0
      NewLine();
4286
0
      nextToken();
4287
0
      if (FormatTok->is(tok::l_paren)) {
4288
0
        FormatTok->setFinalizedType(TT_VerilogMultiLineListLParen);
4289
0
        parseParens();
4290
0
      }
4291
0
    }
4292
0
    if (FormatTok->is(tok::l_paren)) {
4293
0
      NewLine();
4294
0
      FormatTok->setFinalizedType(TT_VerilogMultiLineListLParen);
4295
0
      parseParens();
4296
0
    }
4297
4298
    // extends and implements
4299
0
    if (FormatTok->is(Keywords.kw_extends)) {
4300
0
      NewLine();
4301
0
      nextToken();
4302
0
      parseVerilogHierarchyIdentifier();
4303
0
      if (FormatTok->is(tok::l_paren))
4304
0
        parseParens();
4305
0
    }
4306
0
    if (FormatTok->is(Keywords.kw_implements)) {
4307
0
      NewLine();
4308
0
      do {
4309
0
        nextToken();
4310
0
        parseVerilogHierarchyIdentifier();
4311
0
      } while (FormatTok->is(tok::comma));
4312
0
    }
4313
4314
    // Coverage event for cover groups.
4315
0
    if (FormatTok->is(tok::at)) {
4316
0
      NewLine();
4317
0
      parseVerilogSensitivityList();
4318
0
    }
4319
4320
0
    if (FormatTok->is(tok::semi))
4321
0
      nextToken(/*LevelDifference=*/1);
4322
0
    addUnwrappedLine();
4323
0
  }
4324
4325
0
  return AddLevels;
4326
0
}
4327
4328
0
void UnwrappedLineParser::parseVerilogTable() {
4329
0
  assert(FormatTok->is(Keywords.kw_table));
4330
0
  nextToken(/*LevelDifference=*/1);
4331
0
  addUnwrappedLine();
4332
4333
0
  auto InitialLevel = Line->Level++;
4334
0
  while (!eof() && !Keywords.isVerilogEnd(*FormatTok)) {
4335
0
    FormatToken *Tok = FormatTok;
4336
0
    nextToken();
4337
0
    if (Tok->is(tok::semi))
4338
0
      addUnwrappedLine();
4339
0
    else if (Tok->isOneOf(tok::star, tok::colon, tok::question, tok::minus))
4340
0
      Tok->setFinalizedType(TT_VerilogTableItem);
4341
0
  }
4342
0
  Line->Level = InitialLevel;
4343
0
  nextToken(/*LevelDifference=*/-1);
4344
0
  addUnwrappedLine();
4345
0
}
4346
4347
0
void UnwrappedLineParser::parseVerilogCaseLabel() {
4348
  // The label will get unindented in AnnotatingParser. If there are no leading
4349
  // spaces, indent the rest here so that things inside the block will be
4350
  // indented relative to things outside. We don't use parseLabel because we
4351
  // don't know whether this colon is a label or a ternary expression at this
4352
  // point.
4353
0
  auto OrigLevel = Line->Level;
4354
0
  auto FirstLine = CurrentLines->size();
4355
0
  if (Line->Level == 0 || (Line->InPPDirective && Line->Level <= 1))
4356
0
    ++Line->Level;
4357
0
  else if (!Style.IndentCaseBlocks && Keywords.isVerilogBegin(*FormatTok))
4358
0
    --Line->Level;
4359
0
  parseStructuralElement();
4360
  // Restore the indentation in both the new line and the line that has the
4361
  // label.
4362
0
  if (CurrentLines->size() > FirstLine)
4363
0
    (*CurrentLines)[FirstLine].Level = OrigLevel;
4364
0
  Line->Level = OrigLevel;
4365
0
}
4366
4367
1.33M
bool UnwrappedLineParser::containsExpansion(const UnwrappedLine &Line) const {
4368
51.3M
  for (const auto &N : Line.Tokens) {
4369
51.3M
    if (N.Tok->MacroCtx)
4370
0
      return true;
4371
51.3M
    for (const UnwrappedLine &Child : N.Children)
4372
270k
      if (containsExpansion(Child))
4373
0
        return true;
4374
51.3M
  }
4375
1.33M
  return false;
4376
1.33M
}
4377
4378
2.44M
void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) {
4379
2.44M
  if (Line->Tokens.empty())
4380
850k
    return;
4381
1.59M
  LLVM_DEBUG({
4382
1.59M
    if (!parsingPPDirective()) {
4383
1.59M
      llvm::dbgs() << "Adding unwrapped line:\n";
4384
1.59M
      printDebugInfo(*Line);
4385
1.59M
    }
4386
1.59M
  });
4387
4388
  // If this line closes a block when in Whitesmiths mode, remember that
4389
  // information so that the level can be decreased after the line is added.
4390
  // This has to happen after the addition of the line since the line itself
4391
  // needs to be indented.
4392
1.59M
  bool ClosesWhitesmithsBlock =
4393
1.59M
      Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
4394
1.59M
      Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
4395
4396
  // If the current line was expanded from a macro call, we use it to
4397
  // reconstruct an unwrapped line from the structure of the expanded unwrapped
4398
  // line and the unexpanded token stream.
4399
1.59M
  if (!parsingPPDirective() && !InExpansion && containsExpansion(*Line)) {
4400
0
    if (!Reconstruct)
4401
0
      Reconstruct.emplace(Line->Level, Unexpanded);
4402
0
    Reconstruct->addLine(*Line);
4403
4404
    // While the reconstructed unexpanded lines are stored in the normal
4405
    // flow of lines, the expanded lines are stored on the side to be analyzed
4406
    // in an extra step.
4407
0
    CurrentExpandedLines.push_back(std::move(*Line));
4408
4409
0
    if (Reconstruct->finished()) {
4410
0
      UnwrappedLine Reconstructed = std::move(*Reconstruct).takeResult();
4411
0
      assert(!Reconstructed.Tokens.empty() &&
4412
0
             "Reconstructed must at least contain the macro identifier.");
4413
0
      assert(!parsingPPDirective());
4414
0
      LLVM_DEBUG({
4415
0
        llvm::dbgs() << "Adding unexpanded line:\n";
4416
0
        printDebugInfo(Reconstructed);
4417
0
      });
4418
0
      ExpandedLines[Reconstructed.Tokens.begin()->Tok] = CurrentExpandedLines;
4419
0
      Lines.push_back(std::move(Reconstructed));
4420
0
      CurrentExpandedLines.clear();
4421
0
      Reconstruct.reset();
4422
0
    }
4423
1.59M
  } else {
4424
    // At the top level we only get here when no unexpansion is going on, or
4425
    // when conditional formatting led to unfinished macro reconstructions.
4426
1.59M
    assert(!Reconstruct || (CurrentLines != &Lines) || PPStack.size() > 0);
4427
0
    CurrentLines->push_back(std::move(*Line));
4428
1.59M
  }
4429
0
  Line->Tokens.clear();
4430
1.59M
  Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
4431
1.59M
  Line->FirstStartColumn = 0;
4432
1.59M
  Line->IsContinuation = false;
4433
1.59M
  Line->SeenDecltypeAuto = false;
4434
4435
1.59M
  if (ClosesWhitesmithsBlock && AdjustLevel == LineLevel::Remove)
4436
0
    --Line->Level;
4437
1.59M
  if (!parsingPPDirective() && !PreprocessorDirectives.empty()) {
4438
1.12k
    CurrentLines->append(
4439
1.12k
        std::make_move_iterator(PreprocessorDirectives.begin()),
4440
1.12k
        std::make_move_iterator(PreprocessorDirectives.end()));
4441
1.12k
    PreprocessorDirectives.clear();
4442
1.12k
  }
4443
  // Disconnect the current token from the last token on the previous line.
4444
1.59M
  FormatTok->Previous = nullptr;
4445
1.59M
}
4446
4447
106M
bool UnwrappedLineParser::eof() const { return FormatTok->is(tok::eof); }
4448
4449
52.5M
bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
4450
52.5M
  return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
4451
52.5M
         FormatTok.NewlinesBefore > 0;
4452
52.5M
}
4453
4454
// Checks if \p FormatTok is a line comment that continues the line comment
4455
// section on \p Line.
4456
static bool
4457
continuesLineCommentSection(const FormatToken &FormatTok,
4458
                            const UnwrappedLine &Line,
4459
55.5k
                            const llvm::Regex &CommentPragmasRegex) {
4460
55.5k
  if (Line.Tokens.empty())
4461
2.73k
    return false;
4462
4463
52.8k
  StringRef IndentContent = FormatTok.TokenText;
4464
52.8k
  if (FormatTok.TokenText.starts_with("//") ||
4465
52.8k
      FormatTok.TokenText.starts_with("/*")) {
4466
52.8k
    IndentContent = FormatTok.TokenText.substr(2);
4467
52.8k
  }
4468
52.8k
  if (CommentPragmasRegex.match(IndentContent))
4469
0
    return false;
4470
4471
  // If Line starts with a line comment, then FormatTok continues the comment
4472
  // section if its original column is greater or equal to the original start
4473
  // column of the line.
4474
  //
4475
  // Define the min column token of a line as follows: if a line ends in '{' or
4476
  // contains a '{' followed by a line comment, then the min column token is
4477
  // that '{'. Otherwise, the min column token of the line is the first token of
4478
  // the line.
4479
  //
4480
  // If Line starts with a token other than a line comment, then FormatTok
4481
  // continues the comment section if its original column is greater than the
4482
  // original start column of the min column token of the line.
4483
  //
4484
  // For example, the second line comment continues the first in these cases:
4485
  //
4486
  // // first line
4487
  // // second line
4488
  //
4489
  // and:
4490
  //
4491
  // // first line
4492
  //  // second line
4493
  //
4494
  // and:
4495
  //
4496
  // int i; // first line
4497
  //  // second line
4498
  //
4499
  // and:
4500
  //
4501
  // do { // first line
4502
  //      // second line
4503
  //   int i;
4504
  // } while (true);
4505
  //
4506
  // and:
4507
  //
4508
  // enum {
4509
  //   a, // first line
4510
  //    // second line
4511
  //   b
4512
  // };
4513
  //
4514
  // The second line comment doesn't continue the first in these cases:
4515
  //
4516
  //   // first line
4517
  //  // second line
4518
  //
4519
  // and:
4520
  //
4521
  // int i; // first line
4522
  // // second line
4523
  //
4524
  // and:
4525
  //
4526
  // do { // first line
4527
  //   // second line
4528
  //   int i;
4529
  // } while (true);
4530
  //
4531
  // and:
4532
  //
4533
  // enum {
4534
  //   a, // first line
4535
  //   // second line
4536
  // };
4537
52.8k
  const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
4538
4539
  // Scan for '{//'. If found, use the column of '{' as a min column for line
4540
  // comment section continuation.
4541
52.8k
  const FormatToken *PreviousToken = nullptr;
4542
74.0M
  for (const UnwrappedLineNode &Node : Line.Tokens) {
4543
74.0M
    if (PreviousToken && PreviousToken->is(tok::l_brace) &&
4544
74.0M
        isLineComment(*Node.Tok)) {
4545
0
      MinColumnToken = PreviousToken;
4546
0
      break;
4547
0
    }
4548
74.0M
    PreviousToken = Node.Tok;
4549
4550
    // Grab the last newline preceding a token in this unwrapped line.
4551
74.0M
    if (Node.Tok->NewlinesBefore > 0)
4552
4.70M
      MinColumnToken = Node.Tok;
4553
74.0M
  }
4554
52.8k
  if (PreviousToken && PreviousToken->is(tok::l_brace))
4555
882
    MinColumnToken = PreviousToken;
4556
4557
52.8k
  return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
4558
52.8k
                              MinColumnToken);
4559
52.8k
}
4560
4561
52.4M
void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
4562
52.4M
  bool JustComments = Line->Tokens.empty();
4563
52.4M
  for (FormatToken *Tok : CommentsBeforeNextToken) {
4564
    // Line comments that belong to the same line comment section are put on the
4565
    // same line since later we might want to reflow content between them.
4566
    // Additional fine-grained breaking of line comment sections is controlled
4567
    // by the class BreakableLineCommentSection in case it is desirable to keep
4568
    // several line comment sections in the same unwrapped line.
4569
    //
4570
    // FIXME: Consider putting separate line comment sections as children to the
4571
    // unwrapped line instead.
4572
12.1k
    Tok->ContinuesLineCommentSection =
4573
12.1k
        continuesLineCommentSection(*Tok, *Line, CommentPragmasRegex);
4574
12.1k
    if (isOnNewLine(*Tok) && JustComments && !Tok->ContinuesLineCommentSection)
4575
4.32k
      addUnwrappedLine();
4576
12.1k
    pushToken(Tok);
4577
12.1k
  }
4578
52.4M
  if (NewlineBeforeNext && JustComments)
4579
774k
    addUnwrappedLine();
4580
52.4M
  CommentsBeforeNextToken.clear();
4581
52.4M
}
4582
4583
52.4M
void UnwrappedLineParser::nextToken(int LevelDifference) {
4584
52.4M
  if (eof())
4585
258k
    return;
4586
52.1M
  flushComments(isOnNewLine(*FormatTok));
4587
52.1M
  pushToken(FormatTok);
4588
52.1M
  FormatToken *Previous = FormatTok;
4589
52.1M
  if (!Style.isJavaScript())
4590
52.1M
    readToken(LevelDifference);
4591
0
  else
4592
0
    readTokenWithJavaScriptASI();
4593
52.1M
  FormatTok->Previous = Previous;
4594
52.1M
  if (Style.isVerilog()) {
4595
    // Blocks in Verilog can have `begin` and `end` instead of braces.  For
4596
    // keywords like `begin`, we can't treat them the same as left braces
4597
    // because some contexts require one of them.  For example structs use
4598
    // braces and if blocks use keywords, and a left brace can occur in an if
4599
    // statement, but it is not a block.  For keywords like `end`, we simply
4600
    // treat them the same as right braces.
4601
0
    if (Keywords.isVerilogEnd(*FormatTok))
4602
0
      FormatTok->Tok.setKind(tok::r_brace);
4603
0
  }
4604
52.1M
}
4605
4606
void UnwrappedLineParser::distributeComments(
4607
    const SmallVectorImpl<FormatToken *> &Comments,
4608
52.4M
    const FormatToken *NextTok) {
4609
  // Whether or not a line comment token continues a line is controlled by
4610
  // the method continuesLineCommentSection, with the following caveat:
4611
  //
4612
  // Define a trail of Comments to be a nonempty proper postfix of Comments such
4613
  // that each comment line from the trail is aligned with the next token, if
4614
  // the next token exists. If a trail exists, the beginning of the maximal
4615
  // trail is marked as a start of a new comment section.
4616
  //
4617
  // For example in this code:
4618
  //
4619
  // int a; // line about a
4620
  //   // line 1 about b
4621
  //   // line 2 about b
4622
  //   int b;
4623
  //
4624
  // the two lines about b form a maximal trail, so there are two sections, the
4625
  // first one consisting of the single comment "// line about a" and the
4626
  // second one consisting of the next two comments.
4627
52.4M
  if (Comments.empty())
4628
52.4M
    return;
4629
38.8k
  bool ShouldPushCommentsInCurrentLine = true;
4630
38.8k
  bool HasTrailAlignedWithNextToken = false;
4631
38.8k
  unsigned StartOfTrailAlignedWithNextToken = 0;
4632
38.8k
  if (NextTok) {
4633
    // We are skipping the first element intentionally.
4634
45.7k
    for (unsigned i = Comments.size() - 1; i > 0; --i) {
4635
6.90k
      if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
4636
6.21k
        HasTrailAlignedWithNextToken = true;
4637
6.21k
        StartOfTrailAlignedWithNextToken = i;
4638
6.21k
      }
4639
6.90k
    }
4640
38.8k
  }
4641
84.6k
  for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
4642
45.7k
    FormatToken *FormatTok = Comments[i];
4643
45.7k
    if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
4644
2.32k
      FormatTok->ContinuesLineCommentSection = false;
4645
43.4k
    } else {
4646
43.4k
      FormatTok->ContinuesLineCommentSection =
4647
43.4k
          continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
4648
43.4k
    }
4649
45.7k
    if (!FormatTok->ContinuesLineCommentSection &&
4650
45.7k
        (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
4651
12.0k
      ShouldPushCommentsInCurrentLine = false;
4652
12.0k
    }
4653
45.7k
    if (ShouldPushCommentsInCurrentLine)
4654
33.6k
      pushToken(FormatTok);
4655
12.1k
    else
4656
12.1k
      CommentsBeforeNextToken.push_back(FormatTok);
4657
45.7k
  }
4658
38.8k
}
4659
4660
52.1M
void UnwrappedLineParser::readToken(int LevelDifference) {
4661
52.1M
  SmallVector<FormatToken *, 1> Comments;
4662
52.1M
  bool PreviousWasComment = false;
4663
52.1M
  bool FirstNonCommentOnLine = false;
4664
54.7M
  do {
4665
54.7M
    FormatTok = Tokens->getNextToken();
4666
54.7M
    assert(FormatTok);
4667
55.0M
    while (FormatTok->getType() == TT_ConflictStart ||
4668
55.0M
           FormatTok->getType() == TT_ConflictEnd ||
4669
55.0M
           FormatTok->getType() == TT_ConflictAlternative) {
4670
261k
      if (FormatTok->getType() == TT_ConflictStart)
4671
261k
        conditionalCompilationStart(/*Unreachable=*/false);
4672
297
      else if (FormatTok->getType() == TT_ConflictAlternative)
4673
126
        conditionalCompilationAlternative();
4674
171
      else if (FormatTok->getType() == TT_ConflictEnd)
4675
171
        conditionalCompilationEnd();
4676
261k
      FormatTok = Tokens->getNextToken();
4677
261k
      FormatTok->MustBreakBefore = true;
4678
261k
      FormatTok->MustBreakBeforeFinalized = true;
4679
261k
    }
4680
4681
54.7M
    auto IsFirstNonCommentOnLine = [](bool FirstNonCommentOnLine,
4682
54.7M
                                      const FormatToken &Tok,
4683
55.0M
                                      bool PreviousWasComment) {
4684
55.0M
      auto IsFirstOnLine = [](const FormatToken &Tok) {
4685
55.0M
        return Tok.HasUnescapedNewline || Tok.IsFirst;
4686
55.0M
      };
4687
4688
      // Consider preprocessor directives preceded by block comments as first
4689
      // on line.
4690
55.0M
      if (PreviousWasComment)
4691
50.8k
        return FirstNonCommentOnLine || IsFirstOnLine(Tok);
4692
54.9M
      return IsFirstOnLine(Tok);
4693
55.0M
    };
4694
4695
54.7M
    FirstNonCommentOnLine = IsFirstNonCommentOnLine(
4696
54.7M
        FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
4697
54.7M
    PreviousWasComment = FormatTok->is(tok::comment);
4698
4699
55.0M
    while (!Line->InPPDirective && FormatTok->is(tok::hash) &&
4700
55.0M
           (!Style.isVerilog() ||
4701
412k
            Keywords.isVerilogPPDirective(*Tokens->peekNextToken())) &&
4702
55.0M
           FirstNonCommentOnLine) {
4703
253k
      distributeComments(Comments, FormatTok);
4704
253k
      Comments.clear();
4705
      // If there is an unfinished unwrapped line, we flush the preprocessor
4706
      // directives only after that unwrapped line was finished later.
4707
253k
      bool SwitchToPreprocessorLines = !Line->Tokens.empty();
4708
253k
      ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
4709
253k
      assert((LevelDifference >= 0 ||
4710
253k
              static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
4711
253k
             "LevelDifference makes Line->Level negative");
4712
0
      Line->Level += LevelDifference;
4713
      // Comments stored before the preprocessor directive need to be output
4714
      // before the preprocessor directive, at the same level as the
4715
      // preprocessor directive, as we consider them to apply to the directive.
4716
253k
      if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&
4717
253k
          PPBranchLevel > 0) {
4718
0
        Line->Level += PPBranchLevel;
4719
0
      }
4720
253k
      flushComments(isOnNewLine(*FormatTok));
4721
253k
      parsePPDirective();
4722
253k
      PreviousWasComment = FormatTok->is(tok::comment);
4723
253k
      FirstNonCommentOnLine = IsFirstNonCommentOnLine(
4724
253k
          FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
4725
253k
    }
4726
4727
54.7M
    if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
4728
54.7M
        !Line->InPPDirective) {
4729
2.56M
      continue;
4730
2.56M
    }
4731
4732
52.2M
    if (FormatTok->is(tok::identifier) &&
4733
52.2M
        Macros.defined(FormatTok->TokenText) &&
4734
        // FIXME: Allow expanding macros in preprocessor directives.
4735
52.2M
        !Line->InPPDirective) {
4736
0
      FormatToken *ID = FormatTok;
4737
0
      unsigned Position = Tokens->getPosition();
4738
4739
      // To correctly parse the code, we need to replace the tokens of the macro
4740
      // call with its expansion.
4741
0
      auto PreCall = std::move(Line);
4742
0
      Line.reset(new UnwrappedLine);
4743
0
      bool OldInExpansion = InExpansion;
4744
0
      InExpansion = true;
4745
      // We parse the macro call into a new line.
4746
0
      auto Args = parseMacroCall();
4747
0
      InExpansion = OldInExpansion;
4748
0
      assert(Line->Tokens.front().Tok == ID);
4749
      // And remember the unexpanded macro call tokens.
4750
0
      auto UnexpandedLine = std::move(Line);
4751
      // Reset to the old line.
4752
0
      Line = std::move(PreCall);
4753
4754
0
      LLVM_DEBUG({
4755
0
        llvm::dbgs() << "Macro call: " << ID->TokenText << "(";
4756
0
        if (Args) {
4757
0
          llvm::dbgs() << "(";
4758
0
          for (const auto &Arg : Args.value())
4759
0
            for (const auto &T : Arg)
4760
0
              llvm::dbgs() << T->TokenText << " ";
4761
0
          llvm::dbgs() << ")";
4762
0
        }
4763
0
        llvm::dbgs() << "\n";
4764
0
      });
4765
0
      if (Macros.objectLike(ID->TokenText) && Args &&
4766
0
          !Macros.hasArity(ID->TokenText, Args->size())) {
4767
        // The macro is either
4768
        // - object-like, but we got argumnets, or
4769
        // - overloaded to be both object-like and function-like, but none of
4770
        //   the function-like arities match the number of arguments.
4771
        // Thus, expand as object-like macro.
4772
0
        LLVM_DEBUG(llvm::dbgs()
4773
0
                   << "Macro \"" << ID->TokenText
4774
0
                   << "\" not overloaded for arity " << Args->size()
4775
0
                   << "or not function-like, using object-like overload.");
4776
0
        Args.reset();
4777
0
        UnexpandedLine->Tokens.resize(1);
4778
0
        Tokens->setPosition(Position);
4779
0
        nextToken();
4780
0
        assert(!Args && Macros.objectLike(ID->TokenText));
4781
0
      }
4782
0
      if ((!Args && Macros.objectLike(ID->TokenText)) ||
4783
0
          (Args && Macros.hasArity(ID->TokenText, Args->size()))) {
4784
        // Next, we insert the expanded tokens in the token stream at the
4785
        // current position, and continue parsing.
4786
0
        Unexpanded[ID] = std::move(UnexpandedLine);
4787
0
        SmallVector<FormatToken *, 8> Expansion =
4788
0
            Macros.expand(ID, std::move(Args));
4789
0
        if (!Expansion.empty())
4790
0
          FormatTok = Tokens->insertTokens(Expansion);
4791
4792
0
        LLVM_DEBUG({
4793
0
          llvm::dbgs() << "Expanded: ";
4794
0
          for (const auto &T : Expansion)
4795
0
            llvm::dbgs() << T->TokenText << " ";
4796
0
          llvm::dbgs() << "\n";
4797
0
        });
4798
0
      } else {
4799
0
        LLVM_DEBUG({
4800
0
          llvm::dbgs() << "Did not expand macro \"" << ID->TokenText
4801
0
                       << "\", because it was used ";
4802
0
          if (Args)
4803
0
            llvm::dbgs() << "with " << Args->size();
4804
0
          else
4805
0
            llvm::dbgs() << "without";
4806
0
          llvm::dbgs() << " arguments, which doesn't match any definition.\n";
4807
0
        });
4808
0
        Tokens->setPosition(Position);
4809
0
        FormatTok = ID;
4810
0
      }
4811
0
    }
4812
4813
52.2M
    if (FormatTok->isNot(tok::comment)) {
4814
52.1M
      distributeComments(Comments, FormatTok);
4815
52.1M
      Comments.clear();
4816
52.1M
      return;
4817
52.1M
    }
4818
4819
45.7k
    Comments.push_back(FormatTok);
4820
2.60M
  } while (!eof());
4821
4822
39
  distributeComments(Comments, nullptr);
4823
39
  Comments.clear();
4824
39
}
4825
4826
namespace {
4827
template <typename Iterator>
4828
void pushTokens(Iterator Begin, Iterator End,
4829
0
                llvm::SmallVectorImpl<FormatToken *> &Into) {
4830
0
  for (auto I = Begin; I != End; ++I) {
4831
0
    Into.push_back(I->Tok);
4832
0
    for (const auto &Child : I->Children)
4833
0
      pushTokens(Child.Tokens.begin(), Child.Tokens.end(), Into);
4834
0
  }
4835
0
}
Unexecuted instantiation: UnwrappedLineParser.cpp:void clang::format::(anonymous namespace)::pushTokens<std::__1::__list_iterator<clang::format::UnwrappedLineNode, void*> >(std::__1::__list_iterator<clang::format::UnwrappedLineNode, void*>, std::__1::__list_iterator<clang::format::UnwrappedLineNode, void*>, llvm::SmallVectorImpl<clang::format::FormatToken*>&)
Unexecuted instantiation: UnwrappedLineParser.cpp:void clang::format::(anonymous namespace)::pushTokens<std::__1::__list_const_iterator<clang::format::UnwrappedLineNode, void*> >(std::__1::__list_const_iterator<clang::format::UnwrappedLineNode, void*>, std::__1::__list_const_iterator<clang::format::UnwrappedLineNode, void*>, llvm::SmallVectorImpl<clang::format::FormatToken*>&)
4836
} // namespace
4837
4838
std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>>
4839
0
UnwrappedLineParser::parseMacroCall() {
4840
0
  std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>> Args;
4841
0
  assert(Line->Tokens.empty());
4842
0
  nextToken();
4843
0
  if (FormatTok->isNot(tok::l_paren))
4844
0
    return Args;
4845
0
  unsigned Position = Tokens->getPosition();
4846
0
  FormatToken *Tok = FormatTok;
4847
0
  nextToken();
4848
0
  Args.emplace();
4849
0
  auto ArgStart = std::prev(Line->Tokens.end());
4850
4851
0
  int Parens = 0;
4852
0
  do {
4853
0
    switch (FormatTok->Tok.getKind()) {
4854
0
    case tok::l_paren:
4855
0
      ++Parens;
4856
0
      nextToken();
4857
0
      break;
4858
0
    case tok::r_paren: {
4859
0
      if (Parens > 0) {
4860
0
        --Parens;
4861
0
        nextToken();
4862
0
        break;
4863
0
      }
4864
0
      Args->push_back({});
4865
0
      pushTokens(std::next(ArgStart), Line->Tokens.end(), Args->back());
4866
0
      nextToken();
4867
0
      return Args;
4868
0
    }
4869
0
    case tok::comma: {
4870
0
      if (Parens > 0) {
4871
0
        nextToken();
4872
0
        break;
4873
0
      }
4874
0
      Args->push_back({});
4875
0
      pushTokens(std::next(ArgStart), Line->Tokens.end(), Args->back());
4876
0
      nextToken();
4877
0
      ArgStart = std::prev(Line->Tokens.end());
4878
0
      break;
4879
0
    }
4880
0
    default:
4881
0
      nextToken();
4882
0
      break;
4883
0
    }
4884
0
  } while (!eof());
4885
0
  Line->Tokens.resize(1);
4886
0
  Tokens->setPosition(Position);
4887
0
  FormatTok = Tok;
4888
0
  return {};
4889
0
}
4890
4891
52.2M
void UnwrappedLineParser::pushToken(FormatToken *Tok) {
4892
52.2M
  Line->Tokens.push_back(UnwrappedLineNode(Tok));
4893
52.2M
  if (MustBreakBeforeNextToken) {
4894
253k
    Line->Tokens.back().Tok->MustBreakBefore = true;
4895
253k
    Line->Tokens.back().Tok->MustBreakBeforeFinalized = true;
4896
253k
    MustBreakBeforeNextToken = false;
4897
253k
  }
4898
52.2M
}
4899
4900
} // end namespace format
4901
} // end namespace clang