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