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