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