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