1 //===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file contains the implementation of the UnwrappedLineParser,
12 /// which turns a stream of tokens into UnwrappedLines.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "UnwrappedLineParser.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 #define DEBUG_TYPE "format-parser"
22 
23 namespace clang {
24 namespace format {
25 
26 class FormatTokenSource {
27 public:
28   virtual ~FormatTokenSource() {}
29   virtual FormatToken *getNextToken() = 0;
30 
31   virtual unsigned getPosition() = 0;
32   virtual FormatToken *setPosition(unsigned Position) = 0;
33 };
34 
35 namespace {
36 
37 class ScopedDeclarationState {
38 public:
39   ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
40                          bool MustBeDeclaration)
41       : Line(Line), Stack(Stack) {
42     Line.MustBeDeclaration = MustBeDeclaration;
43     Stack.push_back(MustBeDeclaration);
44   }
45   ~ScopedDeclarationState() {
46     Stack.pop_back();
47     if (!Stack.empty())
48       Line.MustBeDeclaration = Stack.back();
49     else
50       Line.MustBeDeclaration = true;
51   }
52 
53 private:
54   UnwrappedLine &Line;
55   std::vector<bool> &Stack;
56 };
57 
58 class ScopedMacroState : public FormatTokenSource {
59 public:
60   ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
61                    FormatToken *&ResetToken)
62       : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
63         PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
64         Token(nullptr) {
65     TokenSource = this;
66     Line.Level = 0;
67     Line.InPPDirective = true;
68   }
69 
70   ~ScopedMacroState() override {
71     TokenSource = PreviousTokenSource;
72     ResetToken = Token;
73     Line.InPPDirective = false;
74     Line.Level = PreviousLineLevel;
75   }
76 
77   FormatToken *getNextToken() override {
78     // The \c UnwrappedLineParser guards against this by never calling
79     // \c getNextToken() after it has encountered the first eof token.
80     assert(!eof());
81     Token = PreviousTokenSource->getNextToken();
82     if (eof())
83       return getFakeEOF();
84     return Token;
85   }
86 
87   unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
88 
89   FormatToken *setPosition(unsigned Position) override {
90     Token = PreviousTokenSource->setPosition(Position);
91     return Token;
92   }
93 
94 private:
95   bool eof() { return Token && Token->HasUnescapedNewline; }
96 
97   FormatToken *getFakeEOF() {
98     static bool EOFInitialized = false;
99     static FormatToken FormatTok;
100     if (!EOFInitialized) {
101       FormatTok.Tok.startToken();
102       FormatTok.Tok.setKind(tok::eof);
103       EOFInitialized = true;
104     }
105     return &FormatTok;
106   }
107 
108   UnwrappedLine &Line;
109   FormatTokenSource *&TokenSource;
110   FormatToken *&ResetToken;
111   unsigned PreviousLineLevel;
112   FormatTokenSource *PreviousTokenSource;
113 
114   FormatToken *Token;
115 };
116 
117 } // end anonymous namespace
118 
119 class ScopedLineState {
120 public:
121   ScopedLineState(UnwrappedLineParser &Parser,
122                   bool SwitchToPreprocessorLines = false)
123       : Parser(Parser), OriginalLines(Parser.CurrentLines) {
124     if (SwitchToPreprocessorLines)
125       Parser.CurrentLines = &Parser.PreprocessorDirectives;
126     else if (!Parser.Line->Tokens.empty())
127       Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
128     PreBlockLine = std::move(Parser.Line);
129     Parser.Line = llvm::make_unique<UnwrappedLine>();
130     Parser.Line->Level = PreBlockLine->Level;
131     Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
132   }
133 
134   ~ScopedLineState() {
135     if (!Parser.Line->Tokens.empty()) {
136       Parser.addUnwrappedLine();
137     }
138     assert(Parser.Line->Tokens.empty());
139     Parser.Line = std::move(PreBlockLine);
140     if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
141       Parser.MustBreakBeforeNextToken = true;
142     Parser.CurrentLines = OriginalLines;
143   }
144 
145 private:
146   UnwrappedLineParser &Parser;
147 
148   std::unique_ptr<UnwrappedLine> PreBlockLine;
149   SmallVectorImpl<UnwrappedLine> *OriginalLines;
150 };
151 
152 class CompoundStatementIndenter {
153 public:
154   CompoundStatementIndenter(UnwrappedLineParser *Parser,
155                             const FormatStyle &Style, unsigned &LineLevel)
156       : LineLevel(LineLevel), OldLineLevel(LineLevel) {
157     if (Style.BraceWrapping.AfterControlStatement)
158       Parser->addUnwrappedLine();
159     if (Style.BraceWrapping.IndentBraces)
160       ++LineLevel;
161   }
162   ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
163 
164 private:
165   unsigned &LineLevel;
166   unsigned OldLineLevel;
167 };
168 
169 namespace {
170 
171 class IndexedTokenSource : public FormatTokenSource {
172 public:
173   IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
174       : Tokens(Tokens), Position(-1) {}
175 
176   FormatToken *getNextToken() override {
177     ++Position;
178     return Tokens[Position];
179   }
180 
181   unsigned getPosition() override {
182     assert(Position >= 0);
183     return Position;
184   }
185 
186   FormatToken *setPosition(unsigned P) override {
187     Position = P;
188     return Tokens[Position];
189   }
190 
191   void reset() { Position = -1; }
192 
193 private:
194   ArrayRef<FormatToken *> Tokens;
195   int Position;
196 };
197 
198 } // end anonymous namespace
199 
200 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
201                                          const AdditionalKeywords &Keywords,
202                                          ArrayRef<FormatToken *> Tokens,
203                                          UnwrappedLineConsumer &Callback)
204     : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
205       CurrentLines(&Lines), Style(Style), Keywords(Keywords), Tokens(nullptr),
206       Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
207 
208 void UnwrappedLineParser::reset() {
209   PPBranchLevel = -1;
210   Line.reset(new UnwrappedLine);
211   CommentsBeforeNextToken.clear();
212   FormatTok = nullptr;
213   MustBreakBeforeNextToken = false;
214   PreprocessorDirectives.clear();
215   CurrentLines = &Lines;
216   DeclarationScopeStack.clear();
217   PPStack.clear();
218 }
219 
220 void UnwrappedLineParser::parse() {
221   IndexedTokenSource TokenSource(AllTokens);
222   do {
223     DEBUG(llvm::dbgs() << "----\n");
224     reset();
225     Tokens = &TokenSource;
226     TokenSource.reset();
227 
228     readToken();
229     parseFile();
230     // Create line with eof token.
231     pushToken(FormatTok);
232     addUnwrappedLine();
233 
234     for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
235                                                   E = Lines.end();
236          I != E; ++I) {
237       Callback.consumeUnwrappedLine(*I);
238     }
239     Callback.finishRun();
240     Lines.clear();
241     while (!PPLevelBranchIndex.empty() &&
242            PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
243       PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
244       PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
245     }
246     if (!PPLevelBranchIndex.empty()) {
247       ++PPLevelBranchIndex.back();
248       assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
249       assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
250     }
251   } while (!PPLevelBranchIndex.empty());
252 }
253 
254 void UnwrappedLineParser::parseFile() {
255   // The top-level context in a file always has declarations, except for pre-
256   // processor directives and JavaScript files.
257   bool MustBeDeclaration =
258       !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
259   ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
260                                           MustBeDeclaration);
261   parseLevel(/*HasOpeningBrace=*/false);
262   // Make sure to format the remaining tokens.
263   flushComments(true);
264   addUnwrappedLine();
265 }
266 
267 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
268   bool SwitchLabelEncountered = false;
269   do {
270     tok::TokenKind kind = FormatTok->Tok.getKind();
271     if (FormatTok->Type == TT_MacroBlockBegin) {
272       kind = tok::l_brace;
273     } else if (FormatTok->Type == TT_MacroBlockEnd) {
274       kind = tok::r_brace;
275     }
276 
277     switch (kind) {
278     case tok::comment:
279       nextToken();
280       addUnwrappedLine();
281       break;
282     case tok::l_brace:
283       // FIXME: Add parameter whether this can happen - if this happens, we must
284       // be in a non-declaration context.
285       if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
286         continue;
287       parseBlock(/*MustBeDeclaration=*/false);
288       addUnwrappedLine();
289       break;
290     case tok::r_brace:
291       if (HasOpeningBrace)
292         return;
293       nextToken();
294       addUnwrappedLine();
295       break;
296     case tok::kw_default:
297     case tok::kw_case:
298       if (!SwitchLabelEncountered &&
299           (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
300         ++Line->Level;
301       SwitchLabelEncountered = true;
302       parseStructuralElement();
303       break;
304     default:
305       parseStructuralElement();
306       break;
307     }
308   } while (!eof());
309 }
310 
311 void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
312   // We'll parse forward through the tokens until we hit
313   // a closing brace or eof - note that getNextToken() will
314   // parse macros, so this will magically work inside macro
315   // definitions, too.
316   unsigned StoredPosition = Tokens->getPosition();
317   FormatToken *Tok = FormatTok;
318   const FormatToken *PrevTok = getPreviousToken();
319   // Keep a stack of positions of lbrace tokens. We will
320   // update information about whether an lbrace starts a
321   // braced init list or a different block during the loop.
322   SmallVector<FormatToken *, 8> LBraceStack;
323   assert(Tok->Tok.is(tok::l_brace));
324   do {
325     // Get next non-comment token.
326     FormatToken *NextTok;
327     unsigned ReadTokens = 0;
328     do {
329       NextTok = Tokens->getNextToken();
330       ++ReadTokens;
331     } while (NextTok->is(tok::comment));
332 
333     switch (Tok->Tok.getKind()) {
334     case tok::l_brace:
335       if (Style.Language == FormatStyle::LK_JavaScript && PrevTok &&
336           PrevTok->is(tok::colon))
337         // In TypeScript's TypeMemberLists, there can be semicolons between the
338         // individual members.
339         Tok->BlockKind = BK_BracedInit;
340       else
341         Tok->BlockKind = BK_Unknown;
342       LBraceStack.push_back(Tok);
343       break;
344     case tok::r_brace:
345       if (LBraceStack.empty())
346         break;
347       if (LBraceStack.back()->BlockKind == BK_Unknown) {
348         bool ProbablyBracedList = false;
349         if (Style.Language == FormatStyle::LK_Proto) {
350           ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
351         } else {
352           // Using OriginalColumn to distinguish between ObjC methods and
353           // binary operators is a bit hacky.
354           bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
355                                   NextTok->OriginalColumn == 0;
356 
357           // If there is a comma, semicolon or right paren after the closing
358           // brace, we assume this is a braced initializer list.  Note that
359           // regardless how we mark inner braces here, we will overwrite the
360           // BlockKind later if we parse a braced list (where all blocks
361           // inside are by default braced lists), or when we explicitly detect
362           // blocks (for example while parsing lambdas).
363           //
364           // We exclude + and - as they can be ObjC visibility modifiers.
365           ProbablyBracedList =
366               (Style.Language == FormatStyle::LK_JavaScript &&
367                NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
368                                 Keywords.kw_as)) ||
369               NextTok->isOneOf(tok::comma, tok::period, tok::colon,
370                                tok::r_paren, tok::r_square, tok::l_brace,
371                                tok::l_square, tok::l_paren, tok::ellipsis) ||
372               (NextTok->is(tok::semi) &&
373                (!ExpectClassBody || LBraceStack.size() != 1)) ||
374               (NextTok->isBinaryOperator() && !NextIsObjCMethod);
375         }
376         if (ProbablyBracedList) {
377           Tok->BlockKind = BK_BracedInit;
378           LBraceStack.back()->BlockKind = BK_BracedInit;
379         } else {
380           Tok->BlockKind = BK_Block;
381           LBraceStack.back()->BlockKind = BK_Block;
382         }
383       }
384       LBraceStack.pop_back();
385       break;
386     case tok::at:
387     case tok::semi:
388     case tok::kw_if:
389     case tok::kw_while:
390     case tok::kw_for:
391     case tok::kw_switch:
392     case tok::kw_try:
393     case tok::kw___try:
394       if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
395         LBraceStack.back()->BlockKind = BK_Block;
396       break;
397     default:
398       break;
399     }
400     PrevTok = Tok;
401     Tok = NextTok;
402   } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
403 
404   // Assume other blocks for all unclosed opening braces.
405   for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
406     if (LBraceStack[i]->BlockKind == BK_Unknown)
407       LBraceStack[i]->BlockKind = BK_Block;
408   }
409 
410   FormatTok = Tokens->setPosition(StoredPosition);
411 }
412 
413 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
414                                      bool MunchSemi) {
415   assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
416          "'{' or macro block token expected");
417   const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
418   FormatTok->BlockKind = BK_Block;
419 
420   unsigned InitialLevel = Line->Level;
421   nextToken();
422 
423   if (MacroBlock && FormatTok->is(tok::l_paren))
424     parseParens();
425 
426   addUnwrappedLine();
427 
428   ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
429                                           MustBeDeclaration);
430   if (AddLevel)
431     ++Line->Level;
432   parseLevel(/*HasOpeningBrace=*/true);
433 
434   if (eof())
435     return;
436 
437   if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
438                  : !FormatTok->is(tok::r_brace)) {
439     Line->Level = InitialLevel;
440     FormatTok->BlockKind = BK_Block;
441     return;
442   }
443 
444   nextToken(); // Munch the closing brace.
445 
446   if (MacroBlock && FormatTok->is(tok::l_paren))
447     parseParens();
448 
449   if (MunchSemi && FormatTok->Tok.is(tok::semi))
450     nextToken();
451   Line->Level = InitialLevel;
452 }
453 
454 static bool isGoogScope(const UnwrappedLine &Line) {
455   // FIXME: Closure-library specific stuff should not be hard-coded but be
456   // configurable.
457   if (Line.Tokens.size() < 4)
458     return false;
459   auto I = Line.Tokens.begin();
460   if (I->Tok->TokenText != "goog")
461     return false;
462   ++I;
463   if (I->Tok->isNot(tok::period))
464     return false;
465   ++I;
466   if (I->Tok->TokenText != "scope")
467     return false;
468   ++I;
469   return I->Tok->is(tok::l_paren);
470 }
471 
472 static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
473                                    const FormatToken &InitialToken) {
474   if (InitialToken.is(tok::kw_namespace))
475     return Style.BraceWrapping.AfterNamespace;
476   if (InitialToken.is(tok::kw_class))
477     return Style.BraceWrapping.AfterClass;
478   if (InitialToken.is(tok::kw_union))
479     return Style.BraceWrapping.AfterUnion;
480   if (InitialToken.is(tok::kw_struct))
481     return Style.BraceWrapping.AfterStruct;
482   return false;
483 }
484 
485 void UnwrappedLineParser::parseChildBlock() {
486   FormatTok->BlockKind = BK_Block;
487   nextToken();
488   {
489     bool GoogScope =
490         Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
491     ScopedLineState LineState(*this);
492     ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
493                                             /*MustBeDeclaration=*/false);
494     Line->Level += GoogScope ? 0 : 1;
495     parseLevel(/*HasOpeningBrace=*/true);
496     flushComments(isOnNewLine(*FormatTok));
497     Line->Level -= GoogScope ? 0 : 1;
498   }
499   nextToken();
500 }
501 
502 void UnwrappedLineParser::parsePPDirective() {
503   assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
504   ScopedMacroState MacroState(*Line, Tokens, FormatTok);
505   nextToken();
506 
507   if (!FormatTok->Tok.getIdentifierInfo()) {
508     parsePPUnknown();
509     return;
510   }
511 
512   switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
513   case tok::pp_define:
514     parsePPDefine();
515     return;
516   case tok::pp_if:
517     parsePPIf(/*IfDef=*/false);
518     break;
519   case tok::pp_ifdef:
520   case tok::pp_ifndef:
521     parsePPIf(/*IfDef=*/true);
522     break;
523   case tok::pp_else:
524     parsePPElse();
525     break;
526   case tok::pp_elif:
527     parsePPElIf();
528     break;
529   case tok::pp_endif:
530     parsePPEndIf();
531     break;
532   default:
533     parsePPUnknown();
534     break;
535   }
536 }
537 
538 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
539   if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
540     PPStack.push_back(PP_Unreachable);
541   else
542     PPStack.push_back(PP_Conditional);
543 }
544 
545 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
546   ++PPBranchLevel;
547   assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
548   if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
549     PPLevelBranchIndex.push_back(0);
550     PPLevelBranchCount.push_back(0);
551   }
552   PPChainBranchIndex.push(0);
553   bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
554   conditionalCompilationCondition(Unreachable || Skip);
555 }
556 
557 void UnwrappedLineParser::conditionalCompilationAlternative() {
558   if (!PPStack.empty())
559     PPStack.pop_back();
560   assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
561   if (!PPChainBranchIndex.empty())
562     ++PPChainBranchIndex.top();
563   conditionalCompilationCondition(
564       PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
565       PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
566 }
567 
568 void UnwrappedLineParser::conditionalCompilationEnd() {
569   assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
570   if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
571     if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
572       PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
573     }
574   }
575   // Guard against #endif's without #if.
576   if (PPBranchLevel > 0)
577     --PPBranchLevel;
578   if (!PPChainBranchIndex.empty())
579     PPChainBranchIndex.pop();
580   if (!PPStack.empty())
581     PPStack.pop_back();
582 }
583 
584 void UnwrappedLineParser::parsePPIf(bool IfDef) {
585   nextToken();
586   bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
587                          FormatTok->Tok.getLiteralData() != nullptr &&
588                          StringRef(FormatTok->Tok.getLiteralData(),
589                                    FormatTok->Tok.getLength()) == "0") ||
590                         FormatTok->Tok.is(tok::kw_false);
591   conditionalCompilationStart(!IfDef && IsLiteralFalse);
592   parsePPUnknown();
593 }
594 
595 void UnwrappedLineParser::parsePPElse() {
596   conditionalCompilationAlternative();
597   parsePPUnknown();
598 }
599 
600 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
601 
602 void UnwrappedLineParser::parsePPEndIf() {
603   conditionalCompilationEnd();
604   parsePPUnknown();
605 }
606 
607 void UnwrappedLineParser::parsePPDefine() {
608   nextToken();
609 
610   if (FormatTok->Tok.getKind() != tok::identifier) {
611     parsePPUnknown();
612     return;
613   }
614   nextToken();
615   if (FormatTok->Tok.getKind() == tok::l_paren &&
616       FormatTok->WhitespaceRange.getBegin() ==
617           FormatTok->WhitespaceRange.getEnd()) {
618     parseParens();
619   }
620   addUnwrappedLine();
621   Line->Level = 1;
622 
623   // Errors during a preprocessor directive can only affect the layout of the
624   // preprocessor directive, and thus we ignore them. An alternative approach
625   // would be to use the same approach we use on the file level (no
626   // re-indentation if there was a structural error) within the macro
627   // definition.
628   parseFile();
629 }
630 
631 void UnwrappedLineParser::parsePPUnknown() {
632   do {
633     nextToken();
634   } while (!eof());
635   addUnwrappedLine();
636 }
637 
638 // Here we blacklist certain tokens that are not usually the first token in an
639 // unwrapped line. This is used in attempt to distinguish macro calls without
640 // trailing semicolons from other constructs split to several lines.
641 static bool tokenCanStartNewLine(const clang::Token &Tok) {
642   // Semicolon can be a null-statement, l_square can be a start of a macro or
643   // a C++11 attribute, but this doesn't seem to be common.
644   return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
645          Tok.isNot(tok::l_square) &&
646          // Tokens that can only be used as binary operators and a part of
647          // overloaded operator names.
648          Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
649          Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
650          Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
651          Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
652          Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
653          Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
654          Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
655          Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
656          Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
657          Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
658          Tok.isNot(tok::lesslessequal) &&
659          // Colon is used in labels, base class lists, initializer lists,
660          // range-based for loops, ternary operator, but should never be the
661          // first token in an unwrapped line.
662          Tok.isNot(tok::colon) &&
663          // 'noexcept' is a trailing annotation.
664          Tok.isNot(tok::kw_noexcept);
665 }
666 
667 static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
668                           const FormatToken *FormatTok) {
669   // FIXME: This returns true for C/C++ keywords like 'struct'.
670   return FormatTok->is(tok::identifier) &&
671          (FormatTok->Tok.getIdentifierInfo() == nullptr ||
672           !FormatTok->isOneOf(Keywords.kw_in, Keywords.kw_of, Keywords.kw_as,
673                               Keywords.kw_async, Keywords.kw_await,
674                               Keywords.kw_yield, Keywords.kw_finally,
675                               Keywords.kw_function, Keywords.kw_import,
676                               Keywords.kw_is, Keywords.kw_let, Keywords.kw_var,
677                               Keywords.kw_abstract, Keywords.kw_extends,
678                               Keywords.kw_implements, Keywords.kw_instanceof,
679                               Keywords.kw_interface, Keywords.kw_throws));
680 }
681 
682 static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
683                                  const FormatToken *FormatTok) {
684   return FormatTok->Tok.isLiteral() ||
685          FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
686          mustBeJSIdent(Keywords, FormatTok);
687 }
688 
689 // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
690 // when encountered after a value (see mustBeJSIdentOrValue).
691 static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
692                            const FormatToken *FormatTok) {
693   return FormatTok->isOneOf(
694       tok::kw_return, Keywords.kw_yield,
695       // conditionals
696       tok::kw_if, tok::kw_else,
697       // loops
698       tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
699       // switch/case
700       tok::kw_switch, tok::kw_case,
701       // exceptions
702       tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
703       // declaration
704       tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
705       Keywords.kw_async, Keywords.kw_function,
706       // import/export
707       Keywords.kw_import, tok::kw_export);
708 }
709 
710 // readTokenWithJavaScriptASI reads the next token and terminates the current
711 // line if JavaScript Automatic Semicolon Insertion must
712 // happen between the current token and the next token.
713 //
714 // This method is conservative - it cannot cover all edge cases of JavaScript,
715 // but only aims to correctly handle certain well known cases. It *must not*
716 // return true in speculative cases.
717 void UnwrappedLineParser::readTokenWithJavaScriptASI() {
718   FormatToken *Previous = FormatTok;
719   readToken();
720   FormatToken *Next = FormatTok;
721 
722   bool IsOnSameLine =
723       CommentsBeforeNextToken.empty()
724           ? Next->NewlinesBefore == 0
725           : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
726   if (IsOnSameLine)
727     return;
728 
729   bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
730   if (PreviousMustBeValue && Line && Line->Tokens.size() > 1) {
731     // If the token before the previous one is an '@', the previous token is an
732     // annotation and can precede another identifier/value.
733     const FormatToken *PrePrevious = std::prev(Line->Tokens.end(), 2)->Tok;
734     if (PrePrevious->is(tok::at))
735       return;
736   }
737   if (Next->is(tok::exclaim) && PreviousMustBeValue)
738     addUnwrappedLine();
739   bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
740   if (NextMustBeValue && (PreviousMustBeValue ||
741                           Previous->isOneOf(tok::r_square, tok::r_paren,
742                                             tok::plusplus, tok::minusminus)))
743     addUnwrappedLine();
744   if (PreviousMustBeValue && isJSDeclOrStmt(Keywords, Next))
745     addUnwrappedLine();
746 }
747 
748 void UnwrappedLineParser::parseStructuralElement() {
749   assert(!FormatTok->is(tok::l_brace));
750   if (Style.Language == FormatStyle::LK_TableGen &&
751       FormatTok->is(tok::pp_include)) {
752     nextToken();
753     if (FormatTok->is(tok::string_literal))
754       nextToken();
755     addUnwrappedLine();
756     return;
757   }
758   switch (FormatTok->Tok.getKind()) {
759   case tok::at:
760     nextToken();
761     if (FormatTok->Tok.is(tok::l_brace)) {
762       parseBracedList();
763       break;
764     }
765     switch (FormatTok->Tok.getObjCKeywordID()) {
766     case tok::objc_public:
767     case tok::objc_protected:
768     case tok::objc_package:
769     case tok::objc_private:
770       return parseAccessSpecifier();
771     case tok::objc_interface:
772     case tok::objc_implementation:
773       return parseObjCInterfaceOrImplementation();
774     case tok::objc_protocol:
775       return parseObjCProtocol();
776     case tok::objc_end:
777       return; // Handled by the caller.
778     case tok::objc_optional:
779     case tok::objc_required:
780       nextToken();
781       addUnwrappedLine();
782       return;
783     case tok::objc_autoreleasepool:
784       nextToken();
785       if (FormatTok->Tok.is(tok::l_brace)) {
786         if (Style.BraceWrapping.AfterObjCDeclaration)
787           addUnwrappedLine();
788         parseBlock(/*MustBeDeclaration=*/false);
789       }
790       addUnwrappedLine();
791       return;
792     case tok::objc_try:
793       // This branch isn't strictly necessary (the kw_try case below would
794       // do this too after the tok::at is parsed above).  But be explicit.
795       parseTryCatch();
796       return;
797     default:
798       break;
799     }
800     break;
801   case tok::kw_asm:
802     nextToken();
803     if (FormatTok->is(tok::l_brace)) {
804       FormatTok->Type = TT_InlineASMBrace;
805       nextToken();
806       while (FormatTok && FormatTok->isNot(tok::eof)) {
807         if (FormatTok->is(tok::r_brace)) {
808           FormatTok->Type = TT_InlineASMBrace;
809           nextToken();
810           addUnwrappedLine();
811           break;
812         }
813         FormatTok->Finalized = true;
814         nextToken();
815       }
816     }
817     break;
818   case tok::kw_namespace:
819     parseNamespace();
820     return;
821   case tok::kw_inline:
822     nextToken();
823     if (FormatTok->Tok.is(tok::kw_namespace)) {
824       parseNamespace();
825       return;
826     }
827     break;
828   case tok::kw_public:
829   case tok::kw_protected:
830   case tok::kw_private:
831     if (Style.Language == FormatStyle::LK_Java ||
832         Style.Language == FormatStyle::LK_JavaScript)
833       nextToken();
834     else
835       parseAccessSpecifier();
836     return;
837   case tok::kw_if:
838     parseIfThenElse();
839     return;
840   case tok::kw_for:
841   case tok::kw_while:
842     parseForOrWhileLoop();
843     return;
844   case tok::kw_do:
845     parseDoWhile();
846     return;
847   case tok::kw_switch:
848     parseSwitch();
849     return;
850   case tok::kw_default:
851     nextToken();
852     parseLabel();
853     return;
854   case tok::kw_case:
855     parseCaseLabel();
856     return;
857   case tok::kw_try:
858   case tok::kw___try:
859     parseTryCatch();
860     return;
861   case tok::kw_extern:
862     nextToken();
863     if (FormatTok->Tok.is(tok::string_literal)) {
864       nextToken();
865       if (FormatTok->Tok.is(tok::l_brace)) {
866         parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
867         addUnwrappedLine();
868         return;
869       }
870     }
871     break;
872   case tok::kw_export:
873     if (Style.Language == FormatStyle::LK_JavaScript) {
874       parseJavaScriptEs6ImportExport();
875       return;
876     }
877     break;
878   case tok::identifier:
879     if (FormatTok->is(TT_ForEachMacro)) {
880       parseForOrWhileLoop();
881       return;
882     }
883     if (FormatTok->is(TT_MacroBlockBegin)) {
884       parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
885                  /*MunchSemi=*/false);
886       return;
887     }
888     if (FormatTok->is(Keywords.kw_import)) {
889       if (Style.Language == FormatStyle::LK_JavaScript) {
890         parseJavaScriptEs6ImportExport();
891         return;
892       }
893       if (Style.Language == FormatStyle::LK_Proto) {
894         nextToken();
895         if (FormatTok->is(tok::kw_public))
896           nextToken();
897         if (!FormatTok->is(tok::string_literal))
898           return;
899         nextToken();
900         if (FormatTok->is(tok::semi))
901           nextToken();
902         addUnwrappedLine();
903         return;
904       }
905     }
906     if (FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
907                            Keywords.kw_slots, Keywords.kw_qslots)) {
908       nextToken();
909       if (FormatTok->is(tok::colon)) {
910         nextToken();
911         addUnwrappedLine();
912         return;
913       }
914     }
915     // In all other cases, parse the declaration.
916     break;
917   default:
918     break;
919   }
920   do {
921     const FormatToken *Previous = getPreviousToken();
922     switch (FormatTok->Tok.getKind()) {
923     case tok::at:
924       nextToken();
925       if (FormatTok->Tok.is(tok::l_brace))
926         parseBracedList();
927       break;
928     case tok::kw_enum:
929       // Ignore if this is part of "template <enum ...".
930       if (Previous && Previous->is(tok::less)) {
931         nextToken();
932         break;
933       }
934 
935       // parseEnum falls through and does not yet add an unwrapped line as an
936       // enum definition can start a structural element.
937       if (!parseEnum())
938         break;
939       // This only applies for C++.
940       if (Style.Language != FormatStyle::LK_Cpp) {
941         addUnwrappedLine();
942         return;
943       }
944       break;
945     case tok::kw_typedef:
946       nextToken();
947       if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
948                              Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
949         parseEnum();
950       break;
951     case tok::kw_struct:
952     case tok::kw_union:
953     case tok::kw_class:
954       // parseRecord falls through and does not yet add an unwrapped line as a
955       // record declaration or definition can start a structural element.
956       parseRecord();
957       // This does not apply for Java and JavaScript.
958       if (Style.Language == FormatStyle::LK_Java ||
959           Style.Language == FormatStyle::LK_JavaScript) {
960         if (FormatTok->is(tok::semi))
961           nextToken();
962         addUnwrappedLine();
963         return;
964       }
965       break;
966     case tok::period:
967       nextToken();
968       // In Java, classes have an implicit static member "class".
969       if (Style.Language == FormatStyle::LK_Java && FormatTok &&
970           FormatTok->is(tok::kw_class))
971         nextToken();
972       if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
973           FormatTok->Tok.getIdentifierInfo())
974         // JavaScript only has pseudo keywords, all keywords are allowed to
975         // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
976         nextToken();
977       break;
978     case tok::semi:
979       nextToken();
980       addUnwrappedLine();
981       return;
982     case tok::r_brace:
983       addUnwrappedLine();
984       return;
985     case tok::l_paren:
986       parseParens();
987       break;
988     case tok::kw_operator:
989       nextToken();
990       if (FormatTok->isBinaryOperator())
991         nextToken();
992       break;
993     case tok::caret:
994       nextToken();
995       if (FormatTok->Tok.isAnyIdentifier() ||
996           FormatTok->isSimpleTypeSpecifier())
997         nextToken();
998       if (FormatTok->is(tok::l_paren))
999         parseParens();
1000       if (FormatTok->is(tok::l_brace))
1001         parseChildBlock();
1002       break;
1003     case tok::l_brace:
1004       if (!tryToParseBracedList()) {
1005         // A block outside of parentheses must be the last part of a
1006         // structural element.
1007         // FIXME: Figure out cases where this is not true, and add projections
1008         // for them (the one we know is missing are lambdas).
1009         if (Style.BraceWrapping.AfterFunction)
1010           addUnwrappedLine();
1011         FormatTok->Type = TT_FunctionLBrace;
1012         parseBlock(/*MustBeDeclaration=*/false);
1013         addUnwrappedLine();
1014         return;
1015       }
1016       // Otherwise this was a braced init list, and the structural
1017       // element continues.
1018       break;
1019     case tok::kw_try:
1020       // We arrive here when parsing function-try blocks.
1021       parseTryCatch();
1022       return;
1023     case tok::identifier: {
1024       if (FormatTok->is(TT_MacroBlockEnd)) {
1025         addUnwrappedLine();
1026         return;
1027       }
1028 
1029       // Parse function literal unless 'function' is the first token in a line
1030       // in which case this should be treated as a free-standing function.
1031       if (Style.Language == FormatStyle::LK_JavaScript &&
1032           (FormatTok->is(Keywords.kw_function) ||
1033            FormatTok->startsSequence(Keywords.kw_async,
1034                                      Keywords.kw_function)) &&
1035           Line->Tokens.size() > 0) {
1036         tryToParseJSFunction();
1037         break;
1038       }
1039       if ((Style.Language == FormatStyle::LK_JavaScript ||
1040            Style.Language == FormatStyle::LK_Java) &&
1041           FormatTok->is(Keywords.kw_interface)) {
1042         if (Style.Language == FormatStyle::LK_JavaScript) {
1043           // In JavaScript/TypeScript, "interface" can be used as a standalone
1044           // identifier, e.g. in `var interface = 1;`. If "interface" is
1045           // followed by another identifier, it is very like to be an actual
1046           // interface declaration.
1047           unsigned StoredPosition = Tokens->getPosition();
1048           FormatToken *Next = Tokens->getNextToken();
1049           FormatTok = Tokens->setPosition(StoredPosition);
1050           if (Next && !mustBeJSIdent(Keywords, Next)) {
1051             nextToken();
1052             break;
1053           }
1054         }
1055         parseRecord();
1056         addUnwrappedLine();
1057         return;
1058       }
1059 
1060       // See if the following token should start a new unwrapped line.
1061       StringRef Text = FormatTok->TokenText;
1062       nextToken();
1063       if (Line->Tokens.size() == 1 &&
1064           // JS doesn't have macros, and within classes colons indicate fields,
1065           // not labels.
1066           Style.Language != FormatStyle::LK_JavaScript) {
1067         if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
1068           Line->Tokens.begin()->Tok->MustBreakBefore = true;
1069           parseLabel();
1070           return;
1071         }
1072         // Recognize function-like macro usages without trailing semicolon as
1073         // well as free-standing macros like Q_OBJECT.
1074         bool FunctionLike = FormatTok->is(tok::l_paren);
1075         if (FunctionLike)
1076           parseParens();
1077 
1078         bool FollowedByNewline =
1079             CommentsBeforeNextToken.empty()
1080                 ? FormatTok->NewlinesBefore > 0
1081                 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1082 
1083         if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
1084             tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
1085           addUnwrappedLine();
1086           return;
1087         }
1088       }
1089       break;
1090     }
1091     case tok::equal:
1092       // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1093       // TT_JsFatArrow. The always start an expression or a child block if
1094       // followed by a curly.
1095       if (FormatTok->is(TT_JsFatArrow)) {
1096         nextToken();
1097         if (FormatTok->is(tok::l_brace))
1098           parseChildBlock();
1099         break;
1100       }
1101 
1102       nextToken();
1103       if (FormatTok->Tok.is(tok::l_brace)) {
1104         parseBracedList();
1105       }
1106       break;
1107     case tok::l_square:
1108       parseSquare();
1109       break;
1110     case tok::kw_new:
1111       parseNew();
1112       break;
1113     default:
1114       nextToken();
1115       break;
1116     }
1117   } while (!eof());
1118 }
1119 
1120 bool UnwrappedLineParser::tryToParseLambda() {
1121   if (Style.Language != FormatStyle::LK_Cpp) {
1122     nextToken();
1123     return false;
1124   }
1125   const FormatToken* Previous = getPreviousToken();
1126   if (Previous &&
1127       (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1128                          tok::kw_delete) ||
1129        Previous->closesScope() || Previous->isSimpleTypeSpecifier())) {
1130     nextToken();
1131     return false;
1132   }
1133   assert(FormatTok->is(tok::l_square));
1134   FormatToken &LSquare = *FormatTok;
1135   if (!tryToParseLambdaIntroducer())
1136     return false;
1137 
1138   while (FormatTok->isNot(tok::l_brace)) {
1139     if (FormatTok->isSimpleTypeSpecifier()) {
1140       nextToken();
1141       continue;
1142     }
1143     switch (FormatTok->Tok.getKind()) {
1144     case tok::l_brace:
1145       break;
1146     case tok::l_paren:
1147       parseParens();
1148       break;
1149     case tok::amp:
1150     case tok::star:
1151     case tok::kw_const:
1152     case tok::comma:
1153     case tok::less:
1154     case tok::greater:
1155     case tok::identifier:
1156     case tok::numeric_constant:
1157     case tok::coloncolon:
1158     case tok::kw_mutable:
1159       nextToken();
1160       break;
1161     case tok::arrow:
1162       FormatTok->Type = TT_LambdaArrow;
1163       nextToken();
1164       break;
1165     default:
1166       return true;
1167     }
1168   }
1169   LSquare.Type = TT_LambdaLSquare;
1170   parseChildBlock();
1171   return true;
1172 }
1173 
1174 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1175   nextToken();
1176   if (FormatTok->is(tok::equal)) {
1177     nextToken();
1178     if (FormatTok->is(tok::r_square)) {
1179       nextToken();
1180       return true;
1181     }
1182     if (FormatTok->isNot(tok::comma))
1183       return false;
1184     nextToken();
1185   } else if (FormatTok->is(tok::amp)) {
1186     nextToken();
1187     if (FormatTok->is(tok::r_square)) {
1188       nextToken();
1189       return true;
1190     }
1191     if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
1192       return false;
1193     }
1194     if (FormatTok->is(tok::comma))
1195       nextToken();
1196   } else if (FormatTok->is(tok::r_square)) {
1197     nextToken();
1198     return true;
1199   }
1200   do {
1201     if (FormatTok->is(tok::amp))
1202       nextToken();
1203     if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
1204       return false;
1205     nextToken();
1206     if (FormatTok->is(tok::ellipsis))
1207       nextToken();
1208     if (FormatTok->is(tok::comma)) {
1209       nextToken();
1210     } else if (FormatTok->is(tok::r_square)) {
1211       nextToken();
1212       return true;
1213     } else {
1214       return false;
1215     }
1216   } while (!eof());
1217   return false;
1218 }
1219 
1220 void UnwrappedLineParser::tryToParseJSFunction() {
1221   assert(FormatTok->is(Keywords.kw_function) ||
1222          FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
1223   if (FormatTok->is(Keywords.kw_async))
1224     nextToken();
1225   // Consume "function".
1226   nextToken();
1227 
1228   // Consume * (generator function).
1229   if (FormatTok->is(tok::star))
1230     nextToken();
1231 
1232   // Consume function name.
1233   if (FormatTok->is(tok::identifier))
1234     nextToken();
1235 
1236   if (FormatTok->isNot(tok::l_paren))
1237     return;
1238 
1239   // Parse formal parameter list.
1240   parseParens();
1241 
1242   if (FormatTok->is(tok::colon)) {
1243     // Parse a type definition.
1244     nextToken();
1245 
1246     // Eat the type declaration. For braced inline object types, balance braces,
1247     // otherwise just parse until finding an l_brace for the function body.
1248     if (FormatTok->is(tok::l_brace))
1249       tryToParseBracedList();
1250     else
1251       while (FormatTok->isNot(tok::l_brace) && !eof())
1252         nextToken();
1253   }
1254 
1255   parseChildBlock();
1256 }
1257 
1258 bool UnwrappedLineParser::tryToParseBracedList() {
1259   if (FormatTok->BlockKind == BK_Unknown)
1260     calculateBraceTypes();
1261   assert(FormatTok->BlockKind != BK_Unknown);
1262   if (FormatTok->BlockKind == BK_Block)
1263     return false;
1264   parseBracedList();
1265   return true;
1266 }
1267 
1268 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1269   bool HasError = false;
1270   nextToken();
1271 
1272   // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1273   // replace this by using parseAssigmentExpression() inside.
1274   do {
1275     if (Style.Language == FormatStyle::LK_JavaScript) {
1276       if (FormatTok->is(Keywords.kw_function) ||
1277           FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
1278         tryToParseJSFunction();
1279         continue;
1280       }
1281       if (FormatTok->is(TT_JsFatArrow)) {
1282         nextToken();
1283         // Fat arrows can be followed by simple expressions or by child blocks
1284         // in curly braces.
1285         if (FormatTok->is(tok::l_brace)) {
1286           parseChildBlock();
1287           continue;
1288         }
1289       }
1290     }
1291     switch (FormatTok->Tok.getKind()) {
1292     case tok::caret:
1293       nextToken();
1294       if (FormatTok->is(tok::l_brace)) {
1295         parseChildBlock();
1296       }
1297       break;
1298     case tok::l_square:
1299       tryToParseLambda();
1300       break;
1301     case tok::l_brace:
1302       // Assume there are no blocks inside a braced init list apart
1303       // from the ones we explicitly parse out (like lambdas).
1304       FormatTok->BlockKind = BK_BracedInit;
1305       parseBracedList();
1306       break;
1307     case tok::l_paren:
1308       parseParens();
1309       // JavaScript can just have free standing methods and getters/setters in
1310       // object literals. Detect them by a "{" following ")".
1311       if (Style.Language == FormatStyle::LK_JavaScript) {
1312         if (FormatTok->is(tok::l_brace))
1313           parseChildBlock();
1314         break;
1315       }
1316       break;
1317     case tok::r_brace:
1318       nextToken();
1319       return !HasError;
1320     case tok::semi:
1321       // JavaScript (or more precisely TypeScript) can have semicolons in braced
1322       // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1323       // used for error recovery if we have otherwise determined that this is
1324       // a braced list.
1325       if (Style.Language == FormatStyle::LK_JavaScript) {
1326         nextToken();
1327         break;
1328       }
1329       HasError = true;
1330       if (!ContinueOnSemicolons)
1331         return !HasError;
1332       nextToken();
1333       break;
1334     case tok::comma:
1335       nextToken();
1336       break;
1337     default:
1338       nextToken();
1339       break;
1340     }
1341   } while (!eof());
1342   return false;
1343 }
1344 
1345 void UnwrappedLineParser::parseParens() {
1346   assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
1347   nextToken();
1348   do {
1349     switch (FormatTok->Tok.getKind()) {
1350     case tok::l_paren:
1351       parseParens();
1352       if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1353         parseChildBlock();
1354       break;
1355     case tok::r_paren:
1356       nextToken();
1357       return;
1358     case tok::r_brace:
1359       // A "}" inside parenthesis is an error if there wasn't a matching "{".
1360       return;
1361     case tok::l_square:
1362       tryToParseLambda();
1363       break;
1364     case tok::l_brace:
1365       if (!tryToParseBracedList())
1366         parseChildBlock();
1367       break;
1368     case tok::at:
1369       nextToken();
1370       if (FormatTok->Tok.is(tok::l_brace))
1371         parseBracedList();
1372       break;
1373     case tok::identifier:
1374       if (Style.Language == FormatStyle::LK_JavaScript &&
1375           (FormatTok->is(Keywords.kw_function) ||
1376            FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
1377         tryToParseJSFunction();
1378       else
1379         nextToken();
1380       break;
1381     default:
1382       nextToken();
1383       break;
1384     }
1385   } while (!eof());
1386 }
1387 
1388 void UnwrappedLineParser::parseSquare() {
1389   assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1390   if (tryToParseLambda())
1391     return;
1392   do {
1393     switch (FormatTok->Tok.getKind()) {
1394     case tok::l_paren:
1395       parseParens();
1396       break;
1397     case tok::r_square:
1398       nextToken();
1399       return;
1400     case tok::r_brace:
1401       // A "}" inside parenthesis is an error if there wasn't a matching "{".
1402       return;
1403     case tok::l_square:
1404       parseSquare();
1405       break;
1406     case tok::l_brace: {
1407       if (!tryToParseBracedList())
1408         parseChildBlock();
1409       break;
1410     }
1411     case tok::at:
1412       nextToken();
1413       if (FormatTok->Tok.is(tok::l_brace))
1414         parseBracedList();
1415       break;
1416     default:
1417       nextToken();
1418       break;
1419     }
1420   } while (!eof());
1421 }
1422 
1423 void UnwrappedLineParser::parseIfThenElse() {
1424   assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
1425   nextToken();
1426   if (FormatTok->Tok.is(tok::l_paren))
1427     parseParens();
1428   bool NeedsUnwrappedLine = false;
1429   if (FormatTok->Tok.is(tok::l_brace)) {
1430     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1431     parseBlock(/*MustBeDeclaration=*/false);
1432     if (Style.BraceWrapping.BeforeElse)
1433       addUnwrappedLine();
1434     else
1435       NeedsUnwrappedLine = true;
1436   } else {
1437     addUnwrappedLine();
1438     ++Line->Level;
1439     parseStructuralElement();
1440     --Line->Level;
1441   }
1442   if (FormatTok->Tok.is(tok::kw_else)) {
1443     nextToken();
1444     if (FormatTok->Tok.is(tok::l_brace)) {
1445       CompoundStatementIndenter Indenter(this, Style, Line->Level);
1446       parseBlock(/*MustBeDeclaration=*/false);
1447       addUnwrappedLine();
1448     } else if (FormatTok->Tok.is(tok::kw_if)) {
1449       parseIfThenElse();
1450     } else {
1451       addUnwrappedLine();
1452       ++Line->Level;
1453       parseStructuralElement();
1454       if (FormatTok->is(tok::eof))
1455         addUnwrappedLine();
1456       --Line->Level;
1457     }
1458   } else if (NeedsUnwrappedLine) {
1459     addUnwrappedLine();
1460   }
1461 }
1462 
1463 void UnwrappedLineParser::parseTryCatch() {
1464   assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
1465   nextToken();
1466   bool NeedsUnwrappedLine = false;
1467   if (FormatTok->is(tok::colon)) {
1468     // We are in a function try block, what comes is an initializer list.
1469     nextToken();
1470     while (FormatTok->is(tok::identifier)) {
1471       nextToken();
1472       if (FormatTok->is(tok::l_paren))
1473         parseParens();
1474       if (FormatTok->is(tok::comma))
1475         nextToken();
1476     }
1477   }
1478   // Parse try with resource.
1479   if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1480     parseParens();
1481   }
1482   if (FormatTok->is(tok::l_brace)) {
1483     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1484     parseBlock(/*MustBeDeclaration=*/false);
1485     if (Style.BraceWrapping.BeforeCatch) {
1486       addUnwrappedLine();
1487     } else {
1488       NeedsUnwrappedLine = true;
1489     }
1490   } else if (!FormatTok->is(tok::kw_catch)) {
1491     // The C++ standard requires a compound-statement after a try.
1492     // If there's none, we try to assume there's a structuralElement
1493     // and try to continue.
1494     addUnwrappedLine();
1495     ++Line->Level;
1496     parseStructuralElement();
1497     --Line->Level;
1498   }
1499   while (1) {
1500     if (FormatTok->is(tok::at))
1501       nextToken();
1502     if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1503                              tok::kw___finally) ||
1504           ((Style.Language == FormatStyle::LK_Java ||
1505             Style.Language == FormatStyle::LK_JavaScript) &&
1506            FormatTok->is(Keywords.kw_finally)) ||
1507           (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1508            FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1509       break;
1510     nextToken();
1511     while (FormatTok->isNot(tok::l_brace)) {
1512       if (FormatTok->is(tok::l_paren)) {
1513         parseParens();
1514         continue;
1515       }
1516       if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
1517         return;
1518       nextToken();
1519     }
1520     NeedsUnwrappedLine = false;
1521     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1522     parseBlock(/*MustBeDeclaration=*/false);
1523     if (Style.BraceWrapping.BeforeCatch)
1524       addUnwrappedLine();
1525     else
1526       NeedsUnwrappedLine = true;
1527   }
1528   if (NeedsUnwrappedLine)
1529     addUnwrappedLine();
1530 }
1531 
1532 void UnwrappedLineParser::parseNamespace() {
1533   assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
1534 
1535   const FormatToken &InitialToken = *FormatTok;
1536   nextToken();
1537   while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
1538     nextToken();
1539   if (FormatTok->Tok.is(tok::l_brace)) {
1540     if (ShouldBreakBeforeBrace(Style, InitialToken))
1541       addUnwrappedLine();
1542 
1543     bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1544                     (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1545                      DeclarationScopeStack.size() > 1);
1546     parseBlock(/*MustBeDeclaration=*/true, AddLevel);
1547     // Munch the semicolon after a namespace. This is more common than one would
1548     // think. Puttin the semicolon into its own line is very ugly.
1549     if (FormatTok->Tok.is(tok::semi))
1550       nextToken();
1551     addUnwrappedLine();
1552   }
1553   // FIXME: Add error handling.
1554 }
1555 
1556 void UnwrappedLineParser::parseNew() {
1557   assert(FormatTok->is(tok::kw_new) && "'new' expected");
1558   nextToken();
1559   if (Style.Language != FormatStyle::LK_Java)
1560     return;
1561 
1562   // In Java, we can parse everything up to the parens, which aren't optional.
1563   do {
1564     // There should not be a ;, { or } before the new's open paren.
1565     if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1566       return;
1567 
1568     // Consume the parens.
1569     if (FormatTok->is(tok::l_paren)) {
1570       parseParens();
1571 
1572       // If there is a class body of an anonymous class, consume that as child.
1573       if (FormatTok->is(tok::l_brace))
1574         parseChildBlock();
1575       return;
1576     }
1577     nextToken();
1578   } while (!eof());
1579 }
1580 
1581 void UnwrappedLineParser::parseForOrWhileLoop() {
1582   assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
1583          "'for', 'while' or foreach macro expected");
1584   nextToken();
1585   if (FormatTok->Tok.is(tok::l_paren))
1586     parseParens();
1587   if (FormatTok->Tok.is(tok::l_brace)) {
1588     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1589     parseBlock(/*MustBeDeclaration=*/false);
1590     addUnwrappedLine();
1591   } else {
1592     addUnwrappedLine();
1593     ++Line->Level;
1594     parseStructuralElement();
1595     --Line->Level;
1596   }
1597 }
1598 
1599 void UnwrappedLineParser::parseDoWhile() {
1600   assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
1601   nextToken();
1602   if (FormatTok->Tok.is(tok::l_brace)) {
1603     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1604     parseBlock(/*MustBeDeclaration=*/false);
1605     if (Style.BraceWrapping.IndentBraces)
1606       addUnwrappedLine();
1607   } else {
1608     addUnwrappedLine();
1609     ++Line->Level;
1610     parseStructuralElement();
1611     --Line->Level;
1612   }
1613 
1614   // FIXME: Add error handling.
1615   if (!FormatTok->Tok.is(tok::kw_while)) {
1616     addUnwrappedLine();
1617     return;
1618   }
1619 
1620   nextToken();
1621   parseStructuralElement();
1622 }
1623 
1624 void UnwrappedLineParser::parseLabel() {
1625   nextToken();
1626   unsigned OldLineLevel = Line->Level;
1627   if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1628     --Line->Level;
1629   if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
1630     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1631     parseBlock(/*MustBeDeclaration=*/false);
1632     if (FormatTok->Tok.is(tok::kw_break)) {
1633       if (Style.BraceWrapping.AfterControlStatement)
1634         addUnwrappedLine();
1635       parseStructuralElement();
1636     }
1637     addUnwrappedLine();
1638   } else {
1639     if (FormatTok->is(tok::semi))
1640       nextToken();
1641     addUnwrappedLine();
1642   }
1643   Line->Level = OldLineLevel;
1644   if (FormatTok->isNot(tok::l_brace)) {
1645     parseStructuralElement();
1646     addUnwrappedLine();
1647   }
1648 }
1649 
1650 void UnwrappedLineParser::parseCaseLabel() {
1651   assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
1652   // FIXME: fix handling of complex expressions here.
1653   do {
1654     nextToken();
1655   } while (!eof() && !FormatTok->Tok.is(tok::colon));
1656   parseLabel();
1657 }
1658 
1659 void UnwrappedLineParser::parseSwitch() {
1660   assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
1661   nextToken();
1662   if (FormatTok->Tok.is(tok::l_paren))
1663     parseParens();
1664   if (FormatTok->Tok.is(tok::l_brace)) {
1665     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1666     parseBlock(/*MustBeDeclaration=*/false);
1667     addUnwrappedLine();
1668   } else {
1669     addUnwrappedLine();
1670     ++Line->Level;
1671     parseStructuralElement();
1672     --Line->Level;
1673   }
1674 }
1675 
1676 void UnwrappedLineParser::parseAccessSpecifier() {
1677   nextToken();
1678   // Understand Qt's slots.
1679   if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
1680     nextToken();
1681   // Otherwise, we don't know what it is, and we'd better keep the next token.
1682   if (FormatTok->Tok.is(tok::colon))
1683     nextToken();
1684   addUnwrappedLine();
1685 }
1686 
1687 bool UnwrappedLineParser::parseEnum() {
1688   // Won't be 'enum' for NS_ENUMs.
1689   if (FormatTok->Tok.is(tok::kw_enum))
1690     nextToken();
1691 
1692   // In TypeScript, "enum" can also be used as property name, e.g. in interface
1693   // declarations. An "enum" keyword followed by a colon would be a syntax
1694   // error and thus assume it is just an identifier.
1695   if (Style.Language == FormatStyle::LK_JavaScript &&
1696       FormatTok->isOneOf(tok::colon, tok::question))
1697     return false;
1698 
1699   // Eat up enum class ...
1700   if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1701     nextToken();
1702 
1703   while (FormatTok->Tok.getIdentifierInfo() ||
1704          FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1705                             tok::greater, tok::comma, tok::question)) {
1706     nextToken();
1707     // We can have macros or attributes in between 'enum' and the enum name.
1708     if (FormatTok->is(tok::l_paren))
1709       parseParens();
1710     if (FormatTok->is(tok::identifier)) {
1711       nextToken();
1712       // If there are two identifiers in a row, this is likely an elaborate
1713       // return type. In Java, this can be "implements", etc.
1714       if (Style.Language == FormatStyle::LK_Cpp &&
1715           FormatTok->is(tok::identifier))
1716         return false;
1717     }
1718   }
1719 
1720   // Just a declaration or something is wrong.
1721   if (FormatTok->isNot(tok::l_brace))
1722     return true;
1723   FormatTok->BlockKind = BK_Block;
1724 
1725   if (Style.Language == FormatStyle::LK_Java) {
1726     // Java enums are different.
1727     parseJavaEnumBody();
1728     return true;
1729   }
1730   if (Style.Language == FormatStyle::LK_Proto) {
1731     parseBlock(/*MustBeDeclaration=*/true);
1732     return true;
1733   }
1734 
1735   // Parse enum body.
1736   bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1737   if (HasError) {
1738     if (FormatTok->is(tok::semi))
1739       nextToken();
1740     addUnwrappedLine();
1741   }
1742   return true;
1743 
1744   // There is no addUnwrappedLine() here so that we fall through to parsing a
1745   // structural element afterwards. Thus, in "enum A {} n, m;",
1746   // "} n, m;" will end up in one unwrapped line.
1747 }
1748 
1749 void UnwrappedLineParser::parseJavaEnumBody() {
1750   // Determine whether the enum is simple, i.e. does not have a semicolon or
1751   // constants with class bodies. Simple enums can be formatted like braced
1752   // lists, contracted to a single line, etc.
1753   unsigned StoredPosition = Tokens->getPosition();
1754   bool IsSimple = true;
1755   FormatToken *Tok = Tokens->getNextToken();
1756   while (Tok) {
1757     if (Tok->is(tok::r_brace))
1758       break;
1759     if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1760       IsSimple = false;
1761       break;
1762     }
1763     // FIXME: This will also mark enums with braces in the arguments to enum
1764     // constants as "not simple". This is probably fine in practice, though.
1765     Tok = Tokens->getNextToken();
1766   }
1767   FormatTok = Tokens->setPosition(StoredPosition);
1768 
1769   if (IsSimple) {
1770     parseBracedList();
1771     addUnwrappedLine();
1772     return;
1773   }
1774 
1775   // Parse the body of a more complex enum.
1776   // First add a line for everything up to the "{".
1777   nextToken();
1778   addUnwrappedLine();
1779   ++Line->Level;
1780 
1781   // Parse the enum constants.
1782   while (FormatTok) {
1783     if (FormatTok->is(tok::l_brace)) {
1784       // Parse the constant's class body.
1785       parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1786                  /*MunchSemi=*/false);
1787     } else if (FormatTok->is(tok::l_paren)) {
1788       parseParens();
1789     } else if (FormatTok->is(tok::comma)) {
1790       nextToken();
1791       addUnwrappedLine();
1792     } else if (FormatTok->is(tok::semi)) {
1793       nextToken();
1794       addUnwrappedLine();
1795       break;
1796     } else if (FormatTok->is(tok::r_brace)) {
1797       addUnwrappedLine();
1798       break;
1799     } else {
1800       nextToken();
1801     }
1802   }
1803 
1804   // Parse the class body after the enum's ";" if any.
1805   parseLevel(/*HasOpeningBrace=*/true);
1806   nextToken();
1807   --Line->Level;
1808   addUnwrappedLine();
1809 }
1810 
1811 void UnwrappedLineParser::parseRecord() {
1812   const FormatToken &InitialToken = *FormatTok;
1813   nextToken();
1814 
1815   // The actual identifier can be a nested name specifier, and in macros
1816   // it is often token-pasted.
1817   while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
1818                             tok::kw___attribute, tok::kw___declspec,
1819                             tok::kw_alignas) ||
1820          ((Style.Language == FormatStyle::LK_Java ||
1821            Style.Language == FormatStyle::LK_JavaScript) &&
1822           FormatTok->isOneOf(tok::period, tok::comma))) {
1823     bool IsNonMacroIdentifier =
1824         FormatTok->is(tok::identifier) &&
1825         FormatTok->TokenText != FormatTok->TokenText.upper();
1826     nextToken();
1827     // We can have macros or attributes in between 'class' and the class name.
1828     if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
1829       parseParens();
1830   }
1831 
1832   // Note that parsing away template declarations here leads to incorrectly
1833   // accepting function declarations as record declarations.
1834   // In general, we cannot solve this problem. Consider:
1835   // class A<int> B() {}
1836   // which can be a function definition or a class definition when B() is a
1837   // macro. If we find enough real-world cases where this is a problem, we
1838   // can parse for the 'template' keyword in the beginning of the statement,
1839   // and thus rule out the record production in case there is no template
1840   // (this would still leave us with an ambiguity between template function
1841   // and class declarations).
1842   if (FormatTok->isOneOf(tok::colon, tok::less)) {
1843     while (!eof()) {
1844       if (FormatTok->is(tok::l_brace)) {
1845         calculateBraceTypes(/*ExpectClassBody=*/true);
1846         if (!tryToParseBracedList())
1847           break;
1848       }
1849       if (FormatTok->Tok.is(tok::semi))
1850         return;
1851       nextToken();
1852     }
1853   }
1854   if (FormatTok->Tok.is(tok::l_brace)) {
1855     if (ShouldBreakBeforeBrace(Style, InitialToken))
1856       addUnwrappedLine();
1857 
1858     parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1859                /*MunchSemi=*/false);
1860   }
1861   // There is no addUnwrappedLine() here so that we fall through to parsing a
1862   // structural element afterwards. Thus, in "class A {} n, m;",
1863   // "} n, m;" will end up in one unwrapped line.
1864 }
1865 
1866 void UnwrappedLineParser::parseObjCProtocolList() {
1867   assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
1868   do
1869     nextToken();
1870   while (!eof() && FormatTok->Tok.isNot(tok::greater));
1871   nextToken(); // Skip '>'.
1872 }
1873 
1874 void UnwrappedLineParser::parseObjCUntilAtEnd() {
1875   do {
1876     if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
1877       nextToken();
1878       addUnwrappedLine();
1879       break;
1880     }
1881     if (FormatTok->is(tok::l_brace)) {
1882       parseBlock(/*MustBeDeclaration=*/false);
1883       // In ObjC interfaces, nothing should be following the "}".
1884       addUnwrappedLine();
1885     } else if (FormatTok->is(tok::r_brace)) {
1886       // Ignore stray "}". parseStructuralElement doesn't consume them.
1887       nextToken();
1888       addUnwrappedLine();
1889     } else {
1890       parseStructuralElement();
1891     }
1892   } while (!eof());
1893 }
1894 
1895 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
1896   nextToken();
1897   nextToken(); // interface name
1898 
1899   // @interface can be followed by either a base class, or a category.
1900   if (FormatTok->Tok.is(tok::colon)) {
1901     nextToken();
1902     nextToken(); // base class name
1903   } else if (FormatTok->Tok.is(tok::l_paren))
1904     // Skip category, if present.
1905     parseParens();
1906 
1907   if (FormatTok->Tok.is(tok::less))
1908     parseObjCProtocolList();
1909 
1910   if (FormatTok->Tok.is(tok::l_brace)) {
1911     if (Style.BraceWrapping.AfterObjCDeclaration)
1912       addUnwrappedLine();
1913     parseBlock(/*MustBeDeclaration=*/true);
1914   }
1915 
1916   // With instance variables, this puts '}' on its own line.  Without instance
1917   // variables, this ends the @interface line.
1918   addUnwrappedLine();
1919 
1920   parseObjCUntilAtEnd();
1921 }
1922 
1923 void UnwrappedLineParser::parseObjCProtocol() {
1924   nextToken();
1925   nextToken(); // protocol name
1926 
1927   if (FormatTok->Tok.is(tok::less))
1928     parseObjCProtocolList();
1929 
1930   // Check for protocol declaration.
1931   if (FormatTok->Tok.is(tok::semi)) {
1932     nextToken();
1933     return addUnwrappedLine();
1934   }
1935 
1936   addUnwrappedLine();
1937   parseObjCUntilAtEnd();
1938 }
1939 
1940 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1941   bool IsImport = FormatTok->is(Keywords.kw_import);
1942   assert(IsImport || FormatTok->is(tok::kw_export));
1943   nextToken();
1944 
1945   // Consume the "default" in "export default class/function".
1946   if (FormatTok->is(tok::kw_default))
1947     nextToken();
1948 
1949   // Consume "async function", "function" and "default function", so that these
1950   // get parsed as free-standing JS functions, i.e. do not require a trailing
1951   // semicolon.
1952   if (FormatTok->is(Keywords.kw_async))
1953     nextToken();
1954   if (FormatTok->is(Keywords.kw_function)) {
1955     nextToken();
1956     return;
1957   }
1958 
1959   // For imports, `export *`, `export {...}`, consume the rest of the line up
1960   // to the terminating `;`. For everything else, just return and continue
1961   // parsing the structural element, i.e. the declaration or expression for
1962   // `export default`.
1963   if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
1964       !FormatTok->isStringLiteral())
1965     return;
1966 
1967   while (!eof() && FormatTok->isNot(tok::semi)) {
1968     if (FormatTok->is(tok::l_brace)) {
1969       FormatTok->BlockKind = BK_Block;
1970       parseBracedList();
1971     } else {
1972       nextToken();
1973     }
1974   }
1975 }
1976 
1977 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1978                                                  StringRef Prefix = "") {
1979   llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1980                << (Line.InPPDirective ? " MACRO" : "") << ": ";
1981   for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1982                                                     E = Line.Tokens.end();
1983        I != E; ++I) {
1984     llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
1985   }
1986   for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1987                                                     E = Line.Tokens.end();
1988        I != E; ++I) {
1989     const UnwrappedLineNode &Node = *I;
1990     for (SmallVectorImpl<UnwrappedLine>::const_iterator
1991              I = Node.Children.begin(),
1992              E = Node.Children.end();
1993          I != E; ++I) {
1994       printDebugInfo(*I, "\nChild: ");
1995     }
1996   }
1997   llvm::dbgs() << "\n";
1998 }
1999 
2000 void UnwrappedLineParser::addUnwrappedLine() {
2001   if (Line->Tokens.empty())
2002     return;
2003   DEBUG({
2004     if (CurrentLines == &Lines)
2005       printDebugInfo(*Line);
2006   });
2007   CurrentLines->push_back(std::move(*Line));
2008   Line->Tokens.clear();
2009   if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
2010     CurrentLines->append(
2011         std::make_move_iterator(PreprocessorDirectives.begin()),
2012         std::make_move_iterator(PreprocessorDirectives.end()));
2013     PreprocessorDirectives.clear();
2014   }
2015 }
2016 
2017 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
2018 
2019 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
2020   return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
2021          FormatTok.NewlinesBefore > 0;
2022 }
2023 
2024 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
2025   bool JustComments = Line->Tokens.empty();
2026   for (SmallVectorImpl<FormatToken *>::const_iterator
2027            I = CommentsBeforeNextToken.begin(),
2028            E = CommentsBeforeNextToken.end();
2029        I != E; ++I) {
2030     if (isOnNewLine(**I) && JustComments)
2031       addUnwrappedLine();
2032     pushToken(*I);
2033   }
2034   if (NewlineBeforeNext && JustComments)
2035     addUnwrappedLine();
2036   CommentsBeforeNextToken.clear();
2037 }
2038 
2039 void UnwrappedLineParser::nextToken() {
2040   if (eof())
2041     return;
2042   flushComments(isOnNewLine(*FormatTok));
2043   pushToken(FormatTok);
2044   if (Style.Language != FormatStyle::LK_JavaScript)
2045     readToken();
2046   else
2047     readTokenWithJavaScriptASI();
2048 }
2049 
2050 const FormatToken *UnwrappedLineParser::getPreviousToken() {
2051   // FIXME: This is a dirty way to access the previous token. Find a better
2052   // solution.
2053   if (!Line || Line->Tokens.empty())
2054     return nullptr;
2055   return Line->Tokens.back().Tok;
2056 }
2057 
2058 void UnwrappedLineParser::readToken() {
2059   bool CommentsInCurrentLine = true;
2060   do {
2061     FormatTok = Tokens->getNextToken();
2062     assert(FormatTok);
2063     while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
2064            (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
2065       // If there is an unfinished unwrapped line, we flush the preprocessor
2066       // directives only after that unwrapped line was finished later.
2067       bool SwitchToPreprocessorLines = !Line->Tokens.empty();
2068       ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
2069       // Comments stored before the preprocessor directive need to be output
2070       // before the preprocessor directive, at the same level as the
2071       // preprocessor directive, as we consider them to apply to the directive.
2072       flushComments(isOnNewLine(*FormatTok));
2073       parsePPDirective();
2074     }
2075     while (FormatTok->Type == TT_ConflictStart ||
2076            FormatTok->Type == TT_ConflictEnd ||
2077            FormatTok->Type == TT_ConflictAlternative) {
2078       if (FormatTok->Type == TT_ConflictStart) {
2079         conditionalCompilationStart(/*Unreachable=*/false);
2080       } else if (FormatTok->Type == TT_ConflictAlternative) {
2081         conditionalCompilationAlternative();
2082       } else if (FormatTok->Type == TT_ConflictEnd) {
2083         conditionalCompilationEnd();
2084       }
2085       FormatTok = Tokens->getNextToken();
2086       FormatTok->MustBreakBefore = true;
2087     }
2088 
2089     if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
2090         !Line->InPPDirective) {
2091       continue;
2092     }
2093 
2094     if (!FormatTok->Tok.is(tok::comment))
2095       return;
2096     if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
2097       CommentsInCurrentLine = false;
2098     }
2099     if (CommentsInCurrentLine) {
2100       pushToken(FormatTok);
2101     } else {
2102       CommentsBeforeNextToken.push_back(FormatTok);
2103     }
2104   } while (!eof());
2105 }
2106 
2107 void UnwrappedLineParser::pushToken(FormatToken *Tok) {
2108   Line->Tokens.push_back(UnwrappedLineNode(Tok));
2109   if (MustBreakBeforeNextToken) {
2110     Line->Tokens.back().Tok->MustBreakBefore = true;
2111     MustBreakBeforeNextToken = false;
2112   }
2113 }
2114 
2115 } // end namespace format
2116 } // end namespace clang
2117