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