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 #define DEBUG_TYPE "format-parser"
17 
18 #include "UnwrappedLineParser.h"
19 #include "llvm/Support/Debug.h"
20 
21 namespace clang {
22 namespace format {
23 
24 class FormatTokenSource {
25 public:
26   virtual ~FormatTokenSource() {}
27   virtual FormatToken *getNextToken() = 0;
28 
29   virtual unsigned getPosition() = 0;
30   virtual FormatToken *setPosition(unsigned Position) = 0;
31 };
32 
33 namespace {
34 
35 class ScopedDeclarationState {
36 public:
37   ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
38                          bool MustBeDeclaration)
39       : Line(Line), Stack(Stack) {
40     Line.MustBeDeclaration = MustBeDeclaration;
41     Stack.push_back(MustBeDeclaration);
42   }
43   ~ScopedDeclarationState() {
44     Stack.pop_back();
45     if (!Stack.empty())
46       Line.MustBeDeclaration = Stack.back();
47     else
48       Line.MustBeDeclaration = true;
49   }
50 
51 private:
52   UnwrappedLine &Line;
53   std::vector<bool> &Stack;
54 };
55 
56 class ScopedMacroState : public FormatTokenSource {
57 public:
58   ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
59                    FormatToken *&ResetToken, bool &StructuralError)
60       : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
61         PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
62         StructuralError(StructuralError),
63         PreviousStructuralError(StructuralError), Token(NULL) {
64     TokenSource = this;
65     Line.Level = 0;
66     Line.InPPDirective = true;
67   }
68 
69   ~ScopedMacroState() {
70     TokenSource = PreviousTokenSource;
71     ResetToken = Token;
72     Line.InPPDirective = false;
73     Line.Level = PreviousLineLevel;
74     StructuralError = PreviousStructuralError;
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   bool &StructuralError;
114   bool PreviousStructuralError;
115 
116   FormatToken *Token;
117 };
118 
119 } // end anonymous namespace
120 
121 class ScopedLineState {
122 public:
123   ScopedLineState(UnwrappedLineParser &Parser,
124                   bool SwitchToPreprocessorLines = false)
125       : Parser(Parser) {
126     OriginalLines = Parser.CurrentLines;
127     if (SwitchToPreprocessorLines)
128       Parser.CurrentLines = &Parser.PreprocessorDirectives;
129     else if (!Parser.Line->Tokens.empty())
130       Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
131     PreBlockLine = Parser.Line.release();
132     Parser.Line.reset(new UnwrappedLine());
133     Parser.Line->Level = PreBlockLine->Level;
134     Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
135   }
136 
137   ~ScopedLineState() {
138     if (!Parser.Line->Tokens.empty()) {
139       Parser.addUnwrappedLine();
140     }
141     assert(Parser.Line->Tokens.empty());
142     Parser.Line.reset(PreBlockLine);
143     if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
144       Parser.MustBreakBeforeNextToken = true;
145     Parser.CurrentLines = OriginalLines;
146   }
147 
148 private:
149   UnwrappedLineParser &Parser;
150 
151   UnwrappedLine *PreBlockLine;
152   SmallVectorImpl<UnwrappedLine> *OriginalLines;
153 };
154 
155 class CompoundStatementIndenter {
156 public:
157   CompoundStatementIndenter(UnwrappedLineParser *Parser,
158                             const FormatStyle &Style, unsigned &LineLevel)
159       : LineLevel(LineLevel), OldLineLevel(LineLevel) {
160     if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
161       Parser->addUnwrappedLine();
162     } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
163       Parser->addUnwrappedLine();
164       ++LineLevel;
165     }
166   }
167   ~CompoundStatementIndenter() {
168     LineLevel = OldLineLevel;
169   }
170 
171 private:
172   unsigned &LineLevel;
173   unsigned OldLineLevel;
174 };
175 
176 namespace {
177 
178 class IndexedTokenSource : public FormatTokenSource {
179 public:
180   IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
181       : Tokens(Tokens), Position(-1) {}
182 
183   FormatToken *getNextToken() override {
184     ++Position;
185     return Tokens[Position];
186   }
187 
188   unsigned getPosition() override {
189     assert(Position >= 0);
190     return Position;
191   }
192 
193   FormatToken *setPosition(unsigned P) override {
194     Position = P;
195     return Tokens[Position];
196   }
197 
198   void reset() { Position = -1; }
199 
200 private:
201   ArrayRef<FormatToken *> Tokens;
202   int Position;
203 };
204 
205 } // end anonymous namespace
206 
207 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
208                                          ArrayRef<FormatToken *> Tokens,
209                                          UnwrappedLineConsumer &Callback)
210     : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
211       CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL),
212       Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
213 
214 void UnwrappedLineParser::reset() {
215   PPBranchLevel = -1;
216   Line.reset(new UnwrappedLine);
217   CommentsBeforeNextToken.clear();
218   FormatTok = NULL;
219   MustBreakBeforeNextToken = false;
220   PreprocessorDirectives.clear();
221   CurrentLines = &Lines;
222   DeclarationScopeStack.clear();
223   StructuralError = false;
224   PPStack.clear();
225 }
226 
227 bool UnwrappedLineParser::parse() {
228   IndexedTokenSource TokenSource(AllTokens);
229   do {
230     DEBUG(llvm::dbgs() << "----\n");
231     reset();
232     Tokens = &TokenSource;
233     TokenSource.reset();
234 
235     readToken();
236     parseFile();
237     // Create line with eof token.
238     pushToken(FormatTok);
239     addUnwrappedLine();
240 
241     for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
242                                                   E = Lines.end();
243          I != E; ++I) {
244       Callback.consumeUnwrappedLine(*I);
245     }
246     Callback.finishRun();
247     Lines.clear();
248     while (!PPLevelBranchIndex.empty() &&
249            PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
250       PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
251       PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
252     }
253     if (!PPLevelBranchIndex.empty()) {
254       ++PPLevelBranchIndex.back();
255       assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
256       assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
257     }
258   } while (!PPLevelBranchIndex.empty());
259 
260   return StructuralError;
261 }
262 
263 void UnwrappedLineParser::parseFile() {
264   ScopedDeclarationState DeclarationState(
265       *Line, DeclarationScopeStack,
266       /*MustBeDeclaration=*/ !Line->InPPDirective);
267   parseLevel(/*HasOpeningBrace=*/false);
268   // Make sure to format the remaining tokens.
269   flushComments(true);
270   addUnwrappedLine();
271 }
272 
273 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
274   bool SwitchLabelEncountered = false;
275   do {
276     switch (FormatTok->Tok.getKind()) {
277     case tok::comment:
278       nextToken();
279       addUnwrappedLine();
280       break;
281     case tok::l_brace:
282       // FIXME: Add parameter whether this can happen - if this happens, we must
283       // be in a non-declaration context.
284       parseBlock(/*MustBeDeclaration=*/false);
285       addUnwrappedLine();
286       break;
287     case tok::r_brace:
288       if (HasOpeningBrace)
289         return;
290       StructuralError = true;
291       nextToken();
292       addUnwrappedLine();
293       break;
294     case tok::kw_default:
295     case tok::kw_case:
296       if (!SwitchLabelEncountered &&
297           (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
298         ++Line->Level;
299       SwitchLabelEncountered = true;
300       parseStructuralElement();
301       break;
302     default:
303       parseStructuralElement();
304       break;
305     }
306   } while (!eof());
307 }
308 
309 void UnwrappedLineParser::calculateBraceTypes() {
310   // We'll parse forward through the tokens until we hit
311   // a closing brace or eof - note that getNextToken() will
312   // parse macros, so this will magically work inside macro
313   // definitions, too.
314   unsigned StoredPosition = Tokens->getPosition();
315   unsigned Position = StoredPosition;
316   FormatToken *Tok = FormatTok;
317   // Keep a stack of positions of lbrace tokens. We will
318   // update information about whether an lbrace starts a
319   // braced init list or a different block during the loop.
320   SmallVector<FormatToken *, 8> LBraceStack;
321   assert(Tok->Tok.is(tok::l_brace));
322   do {
323     // Get next none-comment token.
324     FormatToken *NextTok;
325     unsigned ReadTokens = 0;
326     do {
327       NextTok = Tokens->getNextToken();
328       ++ReadTokens;
329     } while (NextTok->is(tok::comment));
330 
331     switch (Tok->Tok.getKind()) {
332     case tok::l_brace:
333       LBraceStack.push_back(Tok);
334       break;
335     case tok::r_brace:
336       if (!LBraceStack.empty()) {
337         if (LBraceStack.back()->BlockKind == BK_Unknown) {
338           // If there is a comma, semicolon or right paren after the closing
339           // brace, we assume this is a braced initializer list.  Note that
340           // regardless how we mark inner braces here, we will overwrite the
341           // BlockKind later if we parse a braced list (where all blocks inside
342           // are by default braced lists), or when we explicitly detect blocks
343           // (for example while parsing lambdas).
344           //
345           // We exclude + and - as they can be ObjC visibility modifiers.
346           if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren, tok::period,
347                                tok::r_square, tok::l_brace, tok::colon) ||
348               (NextTok->isBinaryOperator() &&
349                !NextTok->isOneOf(tok::plus, tok::minus))) {
350             Tok->BlockKind = BK_BracedInit;
351             LBraceStack.back()->BlockKind = BK_BracedInit;
352           } else {
353             Tok->BlockKind = BK_Block;
354             LBraceStack.back()->BlockKind = BK_Block;
355           }
356         }
357         LBraceStack.pop_back();
358       }
359       break;
360     case tok::at:
361     case tok::semi:
362     case tok::kw_if:
363     case tok::kw_while:
364     case tok::kw_for:
365     case tok::kw_switch:
366     case tok::kw_try:
367       if (!LBraceStack.empty())
368         LBraceStack.back()->BlockKind = BK_Block;
369       break;
370     default:
371       break;
372     }
373     Tok = NextTok;
374     Position += ReadTokens;
375   } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
376   // Assume other blocks for all unclosed opening braces.
377   for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
378     if (LBraceStack[i]->BlockKind == BK_Unknown)
379       LBraceStack[i]->BlockKind = BK_Block;
380   }
381 
382   FormatTok = Tokens->setPosition(StoredPosition);
383 }
384 
385 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
386                                      bool MunchSemi) {
387   assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
388   unsigned InitialLevel = Line->Level;
389   nextToken();
390 
391   addUnwrappedLine();
392 
393   ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
394                                           MustBeDeclaration);
395   if (AddLevel)
396     ++Line->Level;
397   parseLevel(/*HasOpeningBrace=*/true);
398 
399   if (!FormatTok->Tok.is(tok::r_brace)) {
400     Line->Level = InitialLevel;
401     StructuralError = true;
402     return;
403   }
404 
405   nextToken(); // Munch the closing brace.
406   if (MunchSemi && FormatTok->Tok.is(tok::semi))
407     nextToken();
408   Line->Level = InitialLevel;
409 }
410 
411 void UnwrappedLineParser::parseChildBlock() {
412   FormatTok->BlockKind = BK_Block;
413   nextToken();
414   {
415     ScopedLineState LineState(*this);
416     ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
417                                             /*MustBeDeclaration=*/false);
418     Line->Level += 1;
419     parseLevel(/*HasOpeningBrace=*/true);
420     Line->Level -= 1;
421   }
422   nextToken();
423 }
424 
425 void UnwrappedLineParser::parsePPDirective() {
426   assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
427   ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
428   nextToken();
429 
430   if (FormatTok->Tok.getIdentifierInfo() == NULL) {
431     parsePPUnknown();
432     return;
433   }
434 
435   switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
436   case tok::pp_define:
437     parsePPDefine();
438     return;
439   case tok::pp_if:
440     parsePPIf(/*IfDef=*/false);
441     break;
442   case tok::pp_ifdef:
443   case tok::pp_ifndef:
444     parsePPIf(/*IfDef=*/true);
445     break;
446   case tok::pp_else:
447     parsePPElse();
448     break;
449   case tok::pp_elif:
450     parsePPElIf();
451     break;
452   case tok::pp_endif:
453     parsePPEndIf();
454     break;
455   default:
456     parsePPUnknown();
457     break;
458   }
459 }
460 
461 void UnwrappedLineParser::pushPPConditional() {
462   if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
463     PPStack.push_back(PP_Unreachable);
464   else
465     PPStack.push_back(PP_Conditional);
466 }
467 
468 void UnwrappedLineParser::parsePPIf(bool IfDef) {
469   ++PPBranchLevel;
470   assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
471   if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
472     PPLevelBranchIndex.push_back(0);
473     PPLevelBranchCount.push_back(0);
474   }
475   PPChainBranchIndex.push(0);
476   nextToken();
477   bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
478                          StringRef(FormatTok->Tok.getLiteralData(),
479                                    FormatTok->Tok.getLength()) == "0") ||
480                         FormatTok->Tok.is(tok::kw_false);
481   if ((!IfDef && IsLiteralFalse) || PPLevelBranchIndex[PPBranchLevel] > 0) {
482     PPStack.push_back(PP_Unreachable);
483   } else {
484     pushPPConditional();
485   }
486   parsePPUnknown();
487 }
488 
489 void UnwrappedLineParser::parsePPElse() {
490   if (!PPStack.empty())
491     PPStack.pop_back();
492   assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
493   if (!PPChainBranchIndex.empty())
494     ++PPChainBranchIndex.top();
495   if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
496       PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()) {
497     PPStack.push_back(PP_Unreachable);
498   } else {
499     pushPPConditional();
500   }
501   parsePPUnknown();
502 }
503 
504 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
505 
506 void UnwrappedLineParser::parsePPEndIf() {
507   assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
508   if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
509     if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
510       PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
511     }
512   }
513   // Guard against #endif's without #if.
514   if (PPBranchLevel > 0)
515     --PPBranchLevel;
516   if (!PPChainBranchIndex.empty())
517     PPChainBranchIndex.pop();
518   if (!PPStack.empty())
519     PPStack.pop_back();
520   parsePPUnknown();
521 }
522 
523 void UnwrappedLineParser::parsePPDefine() {
524   nextToken();
525 
526   if (FormatTok->Tok.getKind() != tok::identifier) {
527     parsePPUnknown();
528     return;
529   }
530   nextToken();
531   if (FormatTok->Tok.getKind() == tok::l_paren &&
532       FormatTok->WhitespaceRange.getBegin() ==
533           FormatTok->WhitespaceRange.getEnd()) {
534     parseParens();
535   }
536   addUnwrappedLine();
537   Line->Level = 1;
538 
539   // Errors during a preprocessor directive can only affect the layout of the
540   // preprocessor directive, and thus we ignore them. An alternative approach
541   // would be to use the same approach we use on the file level (no
542   // re-indentation if there was a structural error) within the macro
543   // definition.
544   parseFile();
545 }
546 
547 void UnwrappedLineParser::parsePPUnknown() {
548   do {
549     nextToken();
550   } while (!eof());
551   addUnwrappedLine();
552 }
553 
554 // Here we blacklist certain tokens that are not usually the first token in an
555 // unwrapped line. This is used in attempt to distinguish macro calls without
556 // trailing semicolons from other constructs split to several lines.
557 bool tokenCanStartNewLine(clang::Token Tok) {
558   // Semicolon can be a null-statement, l_square can be a start of a macro or
559   // a C++11 attribute, but this doesn't seem to be common.
560   return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
561          Tok.isNot(tok::l_square) &&
562          // Tokens that can only be used as binary operators and a part of
563          // overloaded operator names.
564          Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
565          Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
566          Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
567          Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
568          Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
569          Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
570          Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
571          Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
572          Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
573          Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
574          Tok.isNot(tok::lesslessequal) &&
575          // Colon is used in labels, base class lists, initializer lists,
576          // range-based for loops, ternary operator, but should never be the
577          // first token in an unwrapped line.
578          Tok.isNot(tok::colon);
579 }
580 
581 void UnwrappedLineParser::parseStructuralElement() {
582   assert(!FormatTok->Tok.is(tok::l_brace));
583   switch (FormatTok->Tok.getKind()) {
584   case tok::at:
585     nextToken();
586     if (FormatTok->Tok.is(tok::l_brace)) {
587       parseBracedList();
588       break;
589     }
590     switch (FormatTok->Tok.getObjCKeywordID()) {
591     case tok::objc_public:
592     case tok::objc_protected:
593     case tok::objc_package:
594     case tok::objc_private:
595       return parseAccessSpecifier();
596     case tok::objc_interface:
597     case tok::objc_implementation:
598       return parseObjCInterfaceOrImplementation();
599     case tok::objc_protocol:
600       return parseObjCProtocol();
601     case tok::objc_end:
602       return; // Handled by the caller.
603     case tok::objc_optional:
604     case tok::objc_required:
605       nextToken();
606       addUnwrappedLine();
607       return;
608     default:
609       break;
610     }
611     break;
612   case tok::kw_namespace:
613     parseNamespace();
614     return;
615   case tok::kw_inline:
616     nextToken();
617     if (FormatTok->Tok.is(tok::kw_namespace)) {
618       parseNamespace();
619       return;
620     }
621     break;
622   case tok::kw_public:
623   case tok::kw_protected:
624   case tok::kw_private:
625     parseAccessSpecifier();
626     return;
627   case tok::kw_if:
628     parseIfThenElse();
629     return;
630   case tok::kw_for:
631   case tok::kw_while:
632     parseForOrWhileLoop();
633     return;
634   case tok::kw_do:
635     parseDoWhile();
636     return;
637   case tok::kw_switch:
638     parseSwitch();
639     return;
640   case tok::kw_default:
641     nextToken();
642     parseLabel();
643     return;
644   case tok::kw_case:
645     parseCaseLabel();
646     return;
647   case tok::kw_extern:
648     nextToken();
649     if (FormatTok->Tok.is(tok::string_literal)) {
650       nextToken();
651       if (FormatTok->Tok.is(tok::l_brace)) {
652         parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
653         addUnwrappedLine();
654         return;
655       }
656     }
657     // In all other cases, parse the declaration.
658     break;
659   default:
660     break;
661   }
662   do {
663     switch (FormatTok->Tok.getKind()) {
664     case tok::at:
665       nextToken();
666       if (FormatTok->Tok.is(tok::l_brace))
667         parseBracedList();
668       break;
669     case tok::kw_enum:
670       parseEnum();
671       break;
672     case tok::kw_typedef:
673       nextToken();
674       // FIXME: Use the IdentifierTable instead.
675       if (FormatTok->TokenText == "NS_ENUM")
676         parseEnum();
677       break;
678     case tok::kw_struct:
679     case tok::kw_union:
680     case tok::kw_class:
681       parseRecord();
682       // A record declaration or definition is always the start of a structural
683       // element.
684       break;
685     case tok::semi:
686       nextToken();
687       addUnwrappedLine();
688       return;
689     case tok::r_brace:
690       addUnwrappedLine();
691       return;
692     case tok::l_paren:
693       parseParens();
694       break;
695     case tok::caret:
696       nextToken();
697       if (FormatTok->is(tok::l_brace)) {
698         parseChildBlock();
699       }
700       break;
701     case tok::l_brace:
702       if (!tryToParseBracedList()) {
703         // A block outside of parentheses must be the last part of a
704         // structural element.
705         // FIXME: Figure out cases where this is not true, and add projections
706         // for them (the one we know is missing are lambdas).
707         if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
708           addUnwrappedLine();
709         FormatTok->Type = TT_FunctionLBrace;
710         parseBlock(/*MustBeDeclaration=*/false);
711         addUnwrappedLine();
712         return;
713       }
714       // Otherwise this was a braced init list, and the structural
715       // element continues.
716       break;
717     case tok::identifier: {
718       StringRef Text = FormatTok->TokenText;
719       nextToken();
720       if (Line->Tokens.size() == 1) {
721         if (FormatTok->Tok.is(tok::colon)) {
722           parseLabel();
723           return;
724         }
725         // Recognize function-like macro usages without trailing semicolon.
726         if (FormatTok->Tok.is(tok::l_paren)) {
727           parseParens();
728           if (FormatTok->NewlinesBefore > 0 &&
729               tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
730             addUnwrappedLine();
731             return;
732           }
733         } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
734                    Text == Text.upper()) {
735           // Recognize free-standing macros like Q_OBJECT.
736           addUnwrappedLine();
737           return;
738         }
739       }
740       break;
741     }
742     case tok::equal:
743       nextToken();
744       if (FormatTok->Tok.is(tok::l_brace)) {
745         parseBracedList();
746       }
747       break;
748     case tok::l_square:
749       parseSquare();
750       break;
751     default:
752       nextToken();
753       break;
754     }
755   } while (!eof());
756 }
757 
758 bool UnwrappedLineParser::tryToParseLambda() {
759   // FIXME: This is a dirty way to access the previous token. Find a better
760   // solution.
761   if (!Line->Tokens.empty() &&
762       (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator) ||
763        Line->Tokens.back().Tok->closesScope() ||
764        Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
765     nextToken();
766     return false;
767   }
768   assert(FormatTok->is(tok::l_square));
769   FormatToken &LSquare = *FormatTok;
770   if (!tryToParseLambdaIntroducer())
771     return false;
772 
773   while (FormatTok->isNot(tok::l_brace)) {
774     if (FormatTok->isSimpleTypeSpecifier()) {
775       nextToken();
776       continue;
777     }
778     switch (FormatTok->Tok.getKind()) {
779     case tok::l_brace:
780       break;
781     case tok::l_paren:
782       parseParens();
783       break;
784     case tok::less:
785     case tok::greater:
786     case tok::identifier:
787     case tok::coloncolon:
788     case tok::kw_mutable:
789       nextToken();
790       break;
791     case tok::arrow:
792       FormatTok->Type = TT_TrailingReturnArrow;
793       nextToken();
794       break;
795     default:
796       return true;
797     }
798   }
799   LSquare.Type = TT_LambdaLSquare;
800   parseChildBlock();
801   return true;
802 }
803 
804 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
805   nextToken();
806   if (FormatTok->is(tok::equal)) {
807     nextToken();
808     if (FormatTok->is(tok::r_square)) {
809       nextToken();
810       return true;
811     }
812     if (FormatTok->isNot(tok::comma))
813       return false;
814     nextToken();
815   } else if (FormatTok->is(tok::amp)) {
816     nextToken();
817     if (FormatTok->is(tok::r_square)) {
818       nextToken();
819       return true;
820     }
821     if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
822       return false;
823     }
824     if (FormatTok->is(tok::comma))
825       nextToken();
826   } else if (FormatTok->is(tok::r_square)) {
827     nextToken();
828     return true;
829   }
830   do {
831     if (FormatTok->is(tok::amp))
832       nextToken();
833     if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
834       return false;
835     nextToken();
836     if (FormatTok->is(tok::comma)) {
837       nextToken();
838     } else if (FormatTok->is(tok::r_square)) {
839       nextToken();
840       return true;
841     } else {
842       return false;
843     }
844   } while (!eof());
845   return false;
846 }
847 
848 bool UnwrappedLineParser::tryToParseBracedList() {
849   if (FormatTok->BlockKind == BK_Unknown)
850     calculateBraceTypes();
851   assert(FormatTok->BlockKind != BK_Unknown);
852   if (FormatTok->BlockKind == BK_Block)
853     return false;
854   parseBracedList();
855   return true;
856 }
857 
858 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
859   bool HasError = false;
860   nextToken();
861 
862   // FIXME: Once we have an expression parser in the UnwrappedLineParser,
863   // replace this by using parseAssigmentExpression() inside.
864   do {
865     // FIXME: When we start to support lambdas, we'll want to parse them away
866     // here, otherwise our bail-out scenarios below break. The better solution
867     // might be to just implement a more or less complete expression parser.
868     switch (FormatTok->Tok.getKind()) {
869     case tok::caret:
870       nextToken();
871       if (FormatTok->is(tok::l_brace)) {
872         parseChildBlock();
873       }
874       break;
875     case tok::l_square:
876       tryToParseLambda();
877       break;
878     case tok::l_brace:
879       // Assume there are no blocks inside a braced init list apart
880       // from the ones we explicitly parse out (like lambdas).
881       FormatTok->BlockKind = BK_BracedInit;
882       parseBracedList();
883       break;
884     case tok::r_brace:
885       nextToken();
886       return !HasError;
887     case tok::semi:
888       HasError = true;
889       if (!ContinueOnSemicolons)
890         return !HasError;
891       nextToken();
892       break;
893     case tok::comma:
894       nextToken();
895       break;
896     default:
897       nextToken();
898       break;
899     }
900   } while (!eof());
901   return false;
902 }
903 
904 void UnwrappedLineParser::parseParens() {
905   assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
906   nextToken();
907   do {
908     switch (FormatTok->Tok.getKind()) {
909     case tok::l_paren:
910       parseParens();
911       break;
912     case tok::r_paren:
913       nextToken();
914       return;
915     case tok::r_brace:
916       // A "}" inside parenthesis is an error if there wasn't a matching "{".
917       return;
918     case tok::l_square:
919       tryToParseLambda();
920       break;
921     case tok::l_brace: {
922       if (!tryToParseBracedList()) {
923         parseChildBlock();
924       }
925       break;
926     }
927     case tok::at:
928       nextToken();
929       if (FormatTok->Tok.is(tok::l_brace))
930         parseBracedList();
931       break;
932     default:
933       nextToken();
934       break;
935     }
936   } while (!eof());
937 }
938 
939 void UnwrappedLineParser::parseSquare() {
940   assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
941   if (tryToParseLambda())
942     return;
943   do {
944     switch (FormatTok->Tok.getKind()) {
945     case tok::l_paren:
946       parseParens();
947       break;
948     case tok::r_square:
949       nextToken();
950       return;
951     case tok::r_brace:
952       // A "}" inside parenthesis is an error if there wasn't a matching "{".
953       return;
954     case tok::l_square:
955       parseSquare();
956       break;
957     case tok::l_brace: {
958       if (!tryToParseBracedList()) {
959         parseChildBlock();
960       }
961       break;
962     }
963     case tok::at:
964       nextToken();
965       if (FormatTok->Tok.is(tok::l_brace))
966         parseBracedList();
967       break;
968     default:
969       nextToken();
970       break;
971     }
972   } while (!eof());
973 }
974 
975 void UnwrappedLineParser::parseIfThenElse() {
976   assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
977   nextToken();
978   if (FormatTok->Tok.is(tok::l_paren))
979     parseParens();
980   bool NeedsUnwrappedLine = false;
981   if (FormatTok->Tok.is(tok::l_brace)) {
982     CompoundStatementIndenter Indenter(this, Style, Line->Level);
983     parseBlock(/*MustBeDeclaration=*/false);
984     if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
985         Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
986       addUnwrappedLine();
987     } else {
988       NeedsUnwrappedLine = true;
989     }
990   } else {
991     addUnwrappedLine();
992     ++Line->Level;
993     parseStructuralElement();
994     --Line->Level;
995   }
996   if (FormatTok->Tok.is(tok::kw_else)) {
997     nextToken();
998     if (FormatTok->Tok.is(tok::l_brace)) {
999       CompoundStatementIndenter Indenter(this, Style, Line->Level);
1000       parseBlock(/*MustBeDeclaration=*/false);
1001       addUnwrappedLine();
1002     } else if (FormatTok->Tok.is(tok::kw_if)) {
1003       parseIfThenElse();
1004     } else {
1005       addUnwrappedLine();
1006       ++Line->Level;
1007       parseStructuralElement();
1008       --Line->Level;
1009     }
1010   } else if (NeedsUnwrappedLine) {
1011     addUnwrappedLine();
1012   }
1013 }
1014 
1015 void UnwrappedLineParser::parseNamespace() {
1016   assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
1017   nextToken();
1018   if (FormatTok->Tok.is(tok::identifier))
1019     nextToken();
1020   if (FormatTok->Tok.is(tok::l_brace)) {
1021     if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
1022         Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1023         Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1024       addUnwrappedLine();
1025 
1026     bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1027                     (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1028                      DeclarationScopeStack.size() > 1);
1029     parseBlock(/*MustBeDeclaration=*/true, AddLevel);
1030     // Munch the semicolon after a namespace. This is more common than one would
1031     // think. Puttin the semicolon into its own line is very ugly.
1032     if (FormatTok->Tok.is(tok::semi))
1033       nextToken();
1034     addUnwrappedLine();
1035   }
1036   // FIXME: Add error handling.
1037 }
1038 
1039 void UnwrappedLineParser::parseForOrWhileLoop() {
1040   assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
1041          "'for' or 'while' expected");
1042   nextToken();
1043   if (FormatTok->Tok.is(tok::l_paren))
1044     parseParens();
1045   if (FormatTok->Tok.is(tok::l_brace)) {
1046     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1047     parseBlock(/*MustBeDeclaration=*/false);
1048     addUnwrappedLine();
1049   } else {
1050     addUnwrappedLine();
1051     ++Line->Level;
1052     parseStructuralElement();
1053     --Line->Level;
1054   }
1055 }
1056 
1057 void UnwrappedLineParser::parseDoWhile() {
1058   assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
1059   nextToken();
1060   if (FormatTok->Tok.is(tok::l_brace)) {
1061     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1062     parseBlock(/*MustBeDeclaration=*/false);
1063     if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1064       addUnwrappedLine();
1065   } else {
1066     addUnwrappedLine();
1067     ++Line->Level;
1068     parseStructuralElement();
1069     --Line->Level;
1070   }
1071 
1072   // FIXME: Add error handling.
1073   if (!FormatTok->Tok.is(tok::kw_while)) {
1074     addUnwrappedLine();
1075     return;
1076   }
1077 
1078   nextToken();
1079   parseStructuralElement();
1080 }
1081 
1082 void UnwrappedLineParser::parseLabel() {
1083   nextToken();
1084   unsigned OldLineLevel = Line->Level;
1085   if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1086     --Line->Level;
1087   if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
1088     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1089     parseBlock(/*MustBeDeclaration=*/false);
1090     if (FormatTok->Tok.is(tok::kw_break)) {
1091       // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1092       if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1093           Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
1094         addUnwrappedLine();
1095       }
1096       parseStructuralElement();
1097     }
1098     addUnwrappedLine();
1099   } else {
1100     addUnwrappedLine();
1101   }
1102   Line->Level = OldLineLevel;
1103 }
1104 
1105 void UnwrappedLineParser::parseCaseLabel() {
1106   assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
1107   // FIXME: fix handling of complex expressions here.
1108   do {
1109     nextToken();
1110   } while (!eof() && !FormatTok->Tok.is(tok::colon));
1111   parseLabel();
1112 }
1113 
1114 void UnwrappedLineParser::parseSwitch() {
1115   assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
1116   nextToken();
1117   if (FormatTok->Tok.is(tok::l_paren))
1118     parseParens();
1119   if (FormatTok->Tok.is(tok::l_brace)) {
1120     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1121     parseBlock(/*MustBeDeclaration=*/false);
1122     addUnwrappedLine();
1123   } else {
1124     addUnwrappedLine();
1125     ++Line->Level;
1126     parseStructuralElement();
1127     --Line->Level;
1128   }
1129 }
1130 
1131 void UnwrappedLineParser::parseAccessSpecifier() {
1132   nextToken();
1133   // Understand Qt's slots.
1134   if (FormatTok->is(tok::identifier) &&
1135       (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
1136     nextToken();
1137   // Otherwise, we don't know what it is, and we'd better keep the next token.
1138   if (FormatTok->Tok.is(tok::colon))
1139     nextToken();
1140   addUnwrappedLine();
1141 }
1142 
1143 void UnwrappedLineParser::parseEnum() {
1144   if (FormatTok->Tok.is(tok::kw_enum)) {
1145     // Won't be 'enum' for NS_ENUMs.
1146     nextToken();
1147   }
1148   // Eat up enum class ...
1149   if (FormatTok->Tok.is(tok::kw_class) ||
1150       FormatTok->Tok.is(tok::kw_struct))
1151       nextToken();
1152   while (FormatTok->Tok.getIdentifierInfo() ||
1153          FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
1154     nextToken();
1155     // We can have macros or attributes in between 'enum' and the enum name.
1156     if (FormatTok->Tok.is(tok::l_paren)) {
1157       parseParens();
1158     }
1159     if (FormatTok->Tok.is(tok::identifier))
1160       nextToken();
1161   }
1162   if (FormatTok->Tok.is(tok::l_brace)) {
1163     FormatTok->BlockKind = BK_Block;
1164     bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1165     if (HasError) {
1166       if (FormatTok->is(tok::semi))
1167         nextToken();
1168       addUnwrappedLine();
1169     }
1170   }
1171   // We fall through to parsing a structural element afterwards, so that in
1172   // enum A {} n, m;
1173   // "} n, m;" will end up in one unwrapped line.
1174 }
1175 
1176 void UnwrappedLineParser::parseRecord() {
1177   nextToken();
1178   if (FormatTok->Tok.is(tok::identifier) ||
1179       FormatTok->Tok.is(tok::kw___attribute) ||
1180       FormatTok->Tok.is(tok::kw___declspec) ||
1181       FormatTok->Tok.is(tok::kw_alignas)) {
1182     nextToken();
1183     // We can have macros or attributes in between 'class' and the class name.
1184     if (FormatTok->Tok.is(tok::l_paren)) {
1185       parseParens();
1186     }
1187     // The actual identifier can be a nested name specifier, and in macros
1188     // it is often token-pasted.
1189     while (FormatTok->Tok.is(tok::identifier) ||
1190            FormatTok->Tok.is(tok::coloncolon) ||
1191            FormatTok->Tok.is(tok::hashhash))
1192       nextToken();
1193 
1194     // Note that parsing away template declarations here leads to incorrectly
1195     // accepting function declarations as record declarations.
1196     // In general, we cannot solve this problem. Consider:
1197     // class A<int> B() {}
1198     // which can be a function definition or a class definition when B() is a
1199     // macro. If we find enough real-world cases where this is a problem, we
1200     // can parse for the 'template' keyword in the beginning of the statement,
1201     // and thus rule out the record production in case there is no template
1202     // (this would still leave us with an ambiguity between template function
1203     // and class declarations).
1204     if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1205       while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1206         if (FormatTok->Tok.is(tok::semi))
1207           return;
1208         nextToken();
1209       }
1210     }
1211   }
1212   if (FormatTok->Tok.is(tok::l_brace)) {
1213     if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
1214         Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1215         Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1216       addUnwrappedLine();
1217 
1218     parseBlock(/*MustBeDeclaration=*/true, /*Addlevel=*/true,
1219                /*MunchSemi=*/false);
1220   }
1221   // We fall through to parsing a structural element afterwards, so
1222   // class A {} n, m;
1223   // will end up in one unwrapped line.
1224 }
1225 
1226 void UnwrappedLineParser::parseObjCProtocolList() {
1227   assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
1228   do
1229     nextToken();
1230   while (!eof() && FormatTok->Tok.isNot(tok::greater));
1231   nextToken(); // Skip '>'.
1232 }
1233 
1234 void UnwrappedLineParser::parseObjCUntilAtEnd() {
1235   do {
1236     if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
1237       nextToken();
1238       addUnwrappedLine();
1239       break;
1240     }
1241     if (FormatTok->is(tok::l_brace)) {
1242       parseBlock(/*MustBeDeclaration=*/false);
1243       // In ObjC interfaces, nothing should be following the "}".
1244       addUnwrappedLine();
1245     } else if (FormatTok->is(tok::r_brace)) {
1246       // Ignore stray "}". parseStructuralElement doesn't consume them.
1247       nextToken();
1248       addUnwrappedLine();
1249     } else {
1250       parseStructuralElement();
1251     }
1252   } while (!eof());
1253 }
1254 
1255 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
1256   nextToken();
1257   nextToken(); // interface name
1258 
1259   // @interface can be followed by either a base class, or a category.
1260   if (FormatTok->Tok.is(tok::colon)) {
1261     nextToken();
1262     nextToken(); // base class name
1263   } else if (FormatTok->Tok.is(tok::l_paren))
1264     // Skip category, if present.
1265     parseParens();
1266 
1267   if (FormatTok->Tok.is(tok::less))
1268     parseObjCProtocolList();
1269 
1270   // If instance variables are present, keep the '{' on the first line too.
1271   if (FormatTok->Tok.is(tok::l_brace))
1272     parseBlock(/*MustBeDeclaration=*/true);
1273 
1274   // With instance variables, this puts '}' on its own line.  Without instance
1275   // variables, this ends the @interface line.
1276   addUnwrappedLine();
1277 
1278   parseObjCUntilAtEnd();
1279 }
1280 
1281 void UnwrappedLineParser::parseObjCProtocol() {
1282   nextToken();
1283   nextToken(); // protocol name
1284 
1285   if (FormatTok->Tok.is(tok::less))
1286     parseObjCProtocolList();
1287 
1288   // Check for protocol declaration.
1289   if (FormatTok->Tok.is(tok::semi)) {
1290     nextToken();
1291     return addUnwrappedLine();
1292   }
1293 
1294   addUnwrappedLine();
1295   parseObjCUntilAtEnd();
1296 }
1297 
1298 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1299                                                  StringRef Prefix = "") {
1300   llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1301                << (Line.InPPDirective ? " MACRO" : "") << ": ";
1302   for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1303                                                     E = Line.Tokens.end();
1304        I != E; ++I) {
1305     llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
1306   }
1307   for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1308                                                     E = Line.Tokens.end();
1309        I != E; ++I) {
1310     const UnwrappedLineNode &Node = *I;
1311     for (SmallVectorImpl<UnwrappedLine>::const_iterator
1312              I = Node.Children.begin(),
1313              E = Node.Children.end();
1314          I != E; ++I) {
1315       printDebugInfo(*I, "\nChild: ");
1316     }
1317   }
1318   llvm::dbgs() << "\n";
1319 }
1320 
1321 void UnwrappedLineParser::addUnwrappedLine() {
1322   if (Line->Tokens.empty())
1323     return;
1324   DEBUG({
1325     if (CurrentLines == &Lines)
1326       printDebugInfo(*Line);
1327   });
1328   CurrentLines->push_back(*Line);
1329   Line->Tokens.clear();
1330   if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
1331     for (SmallVectorImpl<UnwrappedLine>::iterator
1332              I = PreprocessorDirectives.begin(),
1333              E = PreprocessorDirectives.end();
1334          I != E; ++I) {
1335       CurrentLines->push_back(*I);
1336     }
1337     PreprocessorDirectives.clear();
1338   }
1339 }
1340 
1341 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
1342 
1343 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1344   bool JustComments = Line->Tokens.empty();
1345   for (SmallVectorImpl<FormatToken *>::const_iterator
1346            I = CommentsBeforeNextToken.begin(),
1347            E = CommentsBeforeNextToken.end();
1348        I != E; ++I) {
1349     if ((*I)->NewlinesBefore && JustComments) {
1350       addUnwrappedLine();
1351     }
1352     pushToken(*I);
1353   }
1354   if (NewlineBeforeNext && JustComments) {
1355     addUnwrappedLine();
1356   }
1357   CommentsBeforeNextToken.clear();
1358 }
1359 
1360 void UnwrappedLineParser::nextToken() {
1361   if (eof())
1362     return;
1363   flushComments(FormatTok->NewlinesBefore > 0);
1364   pushToken(FormatTok);
1365   readToken();
1366 }
1367 
1368 void UnwrappedLineParser::readToken() {
1369   bool CommentsInCurrentLine = true;
1370   do {
1371     FormatTok = Tokens->getNextToken();
1372     assert(FormatTok);
1373     while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1374            (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
1375       // If there is an unfinished unwrapped line, we flush the preprocessor
1376       // directives only after that unwrapped line was finished later.
1377       bool SwitchToPreprocessorLines =
1378           !Line->Tokens.empty() && CurrentLines == &Lines;
1379       ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
1380       // Comments stored before the preprocessor directive need to be output
1381       // before the preprocessor directive, at the same level as the
1382       // preprocessor directive, as we consider them to apply to the directive.
1383       flushComments(FormatTok->NewlinesBefore > 0);
1384       parsePPDirective();
1385     }
1386 
1387     if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1388         !Line->InPPDirective) {
1389       continue;
1390     }
1391 
1392     if (!FormatTok->Tok.is(tok::comment))
1393       return;
1394     if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
1395       CommentsInCurrentLine = false;
1396     }
1397     if (CommentsInCurrentLine) {
1398       pushToken(FormatTok);
1399     } else {
1400       CommentsBeforeNextToken.push_back(FormatTok);
1401     }
1402   } while (!eof());
1403 }
1404 
1405 void UnwrappedLineParser::pushToken(FormatToken *Tok) {
1406   Line->Tokens.push_back(UnwrappedLineNode(Tok));
1407   if (MustBreakBeforeNextToken) {
1408     Line->Tokens.back().Tok->MustBreakBefore = true;
1409     MustBreakBeforeNextToken = false;
1410   }
1411 }
1412 
1413 } // end namespace format
1414 } // end namespace clang
1415