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