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