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