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