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