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