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