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