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   FormatToken *Tok = FormatTok;
315   // Keep a stack of positions of lbrace tokens. We will
316   // update information about whether an lbrace starts a
317   // braced init list or a different block during the loop.
318   SmallVector<FormatToken *, 8> LBraceStack;
319   assert(Tok->Tok.is(tok::l_brace));
320   do {
321     // Get next none-comment token.
322     FormatToken *NextTok;
323     unsigned ReadTokens = 0;
324     do {
325       NextTok = Tokens->getNextToken();
326       ++ReadTokens;
327     } while (NextTok->is(tok::comment));
328 
329     switch (Tok->Tok.getKind()) {
330     case tok::l_brace:
331       LBraceStack.push_back(Tok);
332       break;
333     case tok::r_brace:
334       if (!LBraceStack.empty()) {
335         if (LBraceStack.back()->BlockKind == BK_Unknown) {
336           bool ProbablyBracedList = false;
337           if (Style.Language == FormatStyle::LK_Proto) {
338             ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
339           } else {
340             // Using OriginalColumn to distinguish between ObjC methods and
341             // binary operators is a bit hacky.
342             bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
343                                     NextTok->OriginalColumn == 0;
344 
345             // If there is a comma, semicolon or right paren after the closing
346             // brace, we assume this is a braced initializer list.  Note that
347             // regardless how we mark inner braces here, we will overwrite the
348             // BlockKind later if we parse a braced list (where all blocks
349             // inside are by default braced lists), or when we explicitly detect
350             // blocks (for example while parsing lambdas).
351             //
352             // We exclude + and - as they can be ObjC visibility modifiers.
353             ProbablyBracedList =
354                 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
355                                  tok::r_paren, tok::r_square, tok::l_brace,
356                                  tok::l_paren, tok::ellipsis) ||
357                 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
358           }
359           if (ProbablyBracedList) {
360             Tok->BlockKind = BK_BracedInit;
361             LBraceStack.back()->BlockKind = BK_BracedInit;
362           } else {
363             Tok->BlockKind = BK_Block;
364             LBraceStack.back()->BlockKind = BK_Block;
365           }
366         }
367         LBraceStack.pop_back();
368       }
369       break;
370     case tok::at:
371     case tok::semi:
372     case tok::kw_if:
373     case tok::kw_while:
374     case tok::kw_for:
375     case tok::kw_switch:
376     case tok::kw_try:
377       if (!LBraceStack.empty())
378         LBraceStack.back()->BlockKind = BK_Block;
379       break;
380     default:
381       break;
382     }
383     Tok = NextTok;
384   } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
385   // Assume other blocks for all unclosed opening braces.
386   for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
387     if (LBraceStack[i]->BlockKind == BK_Unknown)
388       LBraceStack[i]->BlockKind = BK_Block;
389   }
390 
391   FormatTok = Tokens->setPosition(StoredPosition);
392 }
393 
394 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
395                                      bool MunchSemi) {
396   assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
397   unsigned InitialLevel = Line->Level;
398   nextToken();
399 
400   addUnwrappedLine();
401 
402   ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
403                                           MustBeDeclaration);
404   if (AddLevel)
405     ++Line->Level;
406   parseLevel(/*HasOpeningBrace=*/true);
407 
408   if (!FormatTok->Tok.is(tok::r_brace)) {
409     Line->Level = InitialLevel;
410     StructuralError = true;
411     return;
412   }
413 
414   nextToken(); // Munch the closing brace.
415   if (MunchSemi && FormatTok->Tok.is(tok::semi))
416     nextToken();
417   Line->Level = InitialLevel;
418 }
419 
420 static bool IsGoogScope(const UnwrappedLine &Line) {
421   // FIXME: Closure-library specific stuff should not be hard-coded but be
422   // configurable.
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->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
751                              Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
752         parseEnum();
753       break;
754     case tok::kw_struct:
755     case tok::kw_union:
756     case tok::kw_class:
757       parseRecord();
758       // A record declaration or definition is always the start of a structural
759       // element.
760       break;
761     case tok::period:
762       nextToken();
763       // In Java, classes have an implicit static member "class".
764       if (Style.Language == FormatStyle::LK_Java && FormatTok &&
765           FormatTok->is(tok::kw_class))
766         nextToken();
767       break;
768     case tok::semi:
769       nextToken();
770       addUnwrappedLine();
771       return;
772     case tok::r_brace:
773       addUnwrappedLine();
774       return;
775     case tok::l_paren:
776       parseParens();
777       break;
778     case tok::caret:
779       nextToken();
780       if (FormatTok->Tok.isAnyIdentifier() ||
781           FormatTok->isSimpleTypeSpecifier())
782         nextToken();
783       if (FormatTok->is(tok::l_paren))
784         parseParens();
785       if (FormatTok->is(tok::l_brace))
786         parseChildBlock();
787       break;
788     case tok::l_brace:
789       if (!tryToParseBracedList()) {
790         // A block outside of parentheses must be the last part of a
791         // structural element.
792         // FIXME: Figure out cases where this is not true, and add projections
793         // for them (the one we know is missing are lambdas).
794         if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
795           addUnwrappedLine();
796         FormatTok->Type = TT_FunctionLBrace;
797         parseBlock(/*MustBeDeclaration=*/false);
798         addUnwrappedLine();
799         return;
800       }
801       // Otherwise this was a braced init list, and the structural
802       // element continues.
803       break;
804     case tok::kw_try:
805       // We arrive here when parsing function-try blocks.
806       parseTryCatch();
807       return;
808     case tok::identifier: {
809       StringRef Text = FormatTok->TokenText;
810       // Parse function literal unless 'function' is the first token in a line
811       // in which case this should be treated as a free-standing function.
812       if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
813           Line->Tokens.size() > 0) {
814         tryToParseJSFunction();
815         break;
816       }
817       nextToken();
818       if (Line->Tokens.size() == 1) {
819         if (FormatTok->Tok.is(tok::colon)) {
820           parseLabel();
821           return;
822         }
823         // Recognize function-like macro usages without trailing semicolon as
824         // well as free-standing macrose like Q_OBJECT.
825         bool FunctionLike = FormatTok->is(tok::l_paren);
826         if (FunctionLike)
827           parseParens();
828         if (FormatTok->NewlinesBefore > 0 &&
829             (Text.size() >= 5 || FunctionLike) &&
830             tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
831           addUnwrappedLine();
832           return;
833         }
834       }
835       break;
836     }
837     case tok::equal:
838       nextToken();
839       if (FormatTok->Tok.is(tok::l_brace)) {
840         parseBracedList();
841       }
842       break;
843     case tok::l_square:
844       parseSquare();
845       break;
846     default:
847       nextToken();
848       break;
849     }
850   } while (!eof());
851 }
852 
853 bool UnwrappedLineParser::tryToParseLambda() {
854   // FIXME: This is a dirty way to access the previous token. Find a better
855   // solution.
856   if (!Line->Tokens.empty() &&
857       (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
858                                         tok::kw_new, tok::kw_delete) ||
859        Line->Tokens.back().Tok->closesScope() ||
860        Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
861     nextToken();
862     return false;
863   }
864   assert(FormatTok->is(tok::l_square));
865   FormatToken &LSquare = *FormatTok;
866   if (!tryToParseLambdaIntroducer())
867     return false;
868 
869   while (FormatTok->isNot(tok::l_brace)) {
870     if (FormatTok->isSimpleTypeSpecifier()) {
871       nextToken();
872       continue;
873     }
874     switch (FormatTok->Tok.getKind()) {
875     case tok::l_brace:
876       break;
877     case tok::l_paren:
878       parseParens();
879       break;
880     case tok::amp:
881     case tok::star:
882     case tok::kw_const:
883     case tok::comma:
884     case tok::less:
885     case tok::greater:
886     case tok::identifier:
887     case tok::coloncolon:
888     case tok::kw_mutable:
889       nextToken();
890       break;
891     case tok::arrow:
892       FormatTok->Type = TT_TrailingReturnArrow;
893       nextToken();
894       break;
895     default:
896       return true;
897     }
898   }
899   LSquare.Type = TT_LambdaLSquare;
900   parseChildBlock();
901   return true;
902 }
903 
904 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
905   nextToken();
906   if (FormatTok->is(tok::equal)) {
907     nextToken();
908     if (FormatTok->is(tok::r_square)) {
909       nextToken();
910       return true;
911     }
912     if (FormatTok->isNot(tok::comma))
913       return false;
914     nextToken();
915   } else if (FormatTok->is(tok::amp)) {
916     nextToken();
917     if (FormatTok->is(tok::r_square)) {
918       nextToken();
919       return true;
920     }
921     if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
922       return false;
923     }
924     if (FormatTok->is(tok::comma))
925       nextToken();
926   } else if (FormatTok->is(tok::r_square)) {
927     nextToken();
928     return true;
929   }
930   do {
931     if (FormatTok->is(tok::amp))
932       nextToken();
933     if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
934       return false;
935     nextToken();
936     if (FormatTok->is(tok::ellipsis))
937       nextToken();
938     if (FormatTok->is(tok::comma)) {
939       nextToken();
940     } else if (FormatTok->is(tok::r_square)) {
941       nextToken();
942       return true;
943     } else {
944       return false;
945     }
946   } while (!eof());
947   return false;
948 }
949 
950 void UnwrappedLineParser::tryToParseJSFunction() {
951   nextToken();
952 
953   // Consume function name.
954   if (FormatTok->is(tok::identifier))
955       nextToken();
956 
957   if (FormatTok->isNot(tok::l_paren))
958     return;
959   nextToken();
960   while (FormatTok->isNot(tok::l_brace)) {
961     // Err on the side of caution in order to avoid consuming the full file in
962     // case of incomplete code.
963     if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
964                             tok::comment))
965       return;
966     nextToken();
967   }
968   parseChildBlock();
969 }
970 
971 bool UnwrappedLineParser::tryToParseBracedList() {
972   if (FormatTok->BlockKind == BK_Unknown)
973     calculateBraceTypes();
974   assert(FormatTok->BlockKind != BK_Unknown);
975   if (FormatTok->BlockKind == BK_Block)
976     return false;
977   parseBracedList();
978   return true;
979 }
980 
981 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
982   bool HasError = false;
983   nextToken();
984 
985   // FIXME: Once we have an expression parser in the UnwrappedLineParser,
986   // replace this by using parseAssigmentExpression() inside.
987   do {
988     if (Style.Language == FormatStyle::LK_JavaScript &&
989         FormatTok->is(Keywords.kw_function)) {
990       tryToParseJSFunction();
991       continue;
992     }
993     switch (FormatTok->Tok.getKind()) {
994     case tok::caret:
995       nextToken();
996       if (FormatTok->is(tok::l_brace)) {
997         parseChildBlock();
998       }
999       break;
1000     case tok::l_square:
1001       tryToParseLambda();
1002       break;
1003     case tok::l_brace:
1004       // Assume there are no blocks inside a braced init list apart
1005       // from the ones we explicitly parse out (like lambdas).
1006       FormatTok->BlockKind = BK_BracedInit;
1007       parseBracedList();
1008       break;
1009     case tok::r_brace:
1010       nextToken();
1011       return !HasError;
1012     case tok::semi:
1013       HasError = true;
1014       if (!ContinueOnSemicolons)
1015         return !HasError;
1016       nextToken();
1017       break;
1018     case tok::comma:
1019       nextToken();
1020       break;
1021     default:
1022       nextToken();
1023       break;
1024     }
1025   } while (!eof());
1026   return false;
1027 }
1028 
1029 void UnwrappedLineParser::parseParens() {
1030   assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
1031   nextToken();
1032   do {
1033     switch (FormatTok->Tok.getKind()) {
1034     case tok::l_paren:
1035       parseParens();
1036       break;
1037     case tok::r_paren:
1038       nextToken();
1039       return;
1040     case tok::r_brace:
1041       // A "}" inside parenthesis is an error if there wasn't a matching "{".
1042       return;
1043     case tok::l_square:
1044       tryToParseLambda();
1045       break;
1046     case tok::l_brace: {
1047       if (!tryToParseBracedList()) {
1048         parseChildBlock();
1049       }
1050       break;
1051     }
1052     case tok::at:
1053       nextToken();
1054       if (FormatTok->Tok.is(tok::l_brace))
1055         parseBracedList();
1056       break;
1057     case tok::identifier:
1058       if (Style.Language == FormatStyle::LK_JavaScript &&
1059           FormatTok->is(Keywords.kw_function))
1060         tryToParseJSFunction();
1061       else
1062         nextToken();
1063       break;
1064     default:
1065       nextToken();
1066       break;
1067     }
1068   } while (!eof());
1069 }
1070 
1071 void UnwrappedLineParser::parseSquare() {
1072   assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1073   if (tryToParseLambda())
1074     return;
1075   do {
1076     switch (FormatTok->Tok.getKind()) {
1077     case tok::l_paren:
1078       parseParens();
1079       break;
1080     case tok::r_square:
1081       nextToken();
1082       return;
1083     case tok::r_brace:
1084       // A "}" inside parenthesis is an error if there wasn't a matching "{".
1085       return;
1086     case tok::l_square:
1087       parseSquare();
1088       break;
1089     case tok::l_brace: {
1090       if (!tryToParseBracedList()) {
1091         parseChildBlock();
1092       }
1093       break;
1094     }
1095     case tok::at:
1096       nextToken();
1097       if (FormatTok->Tok.is(tok::l_brace))
1098         parseBracedList();
1099       break;
1100     default:
1101       nextToken();
1102       break;
1103     }
1104   } while (!eof());
1105 }
1106 
1107 void UnwrappedLineParser::parseIfThenElse() {
1108   assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
1109   nextToken();
1110   if (FormatTok->Tok.is(tok::l_paren))
1111     parseParens();
1112   bool NeedsUnwrappedLine = false;
1113   if (FormatTok->Tok.is(tok::l_brace)) {
1114     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1115     parseBlock(/*MustBeDeclaration=*/false);
1116     if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1117         Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
1118       addUnwrappedLine();
1119     } else {
1120       NeedsUnwrappedLine = true;
1121     }
1122   } else {
1123     addUnwrappedLine();
1124     ++Line->Level;
1125     parseStructuralElement();
1126     --Line->Level;
1127   }
1128   if (FormatTok->Tok.is(tok::kw_else)) {
1129     if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1130       addUnwrappedLine();
1131     nextToken();
1132     if (FormatTok->Tok.is(tok::l_brace)) {
1133       CompoundStatementIndenter Indenter(this, Style, Line->Level);
1134       parseBlock(/*MustBeDeclaration=*/false);
1135       addUnwrappedLine();
1136     } else if (FormatTok->Tok.is(tok::kw_if)) {
1137       parseIfThenElse();
1138     } else {
1139       addUnwrappedLine();
1140       ++Line->Level;
1141       parseStructuralElement();
1142       --Line->Level;
1143     }
1144   } else if (NeedsUnwrappedLine) {
1145     addUnwrappedLine();
1146   }
1147 }
1148 
1149 void UnwrappedLineParser::parseTryCatch() {
1150   assert(FormatTok->is(tok::kw_try) && "'try' expected");
1151   nextToken();
1152   bool NeedsUnwrappedLine = false;
1153   if (FormatTok->is(tok::colon)) {
1154     // We are in a function try block, what comes is an initializer list.
1155     nextToken();
1156     while (FormatTok->is(tok::identifier)) {
1157       nextToken();
1158       if (FormatTok->is(tok::l_paren))
1159         parseParens();
1160       else
1161         StructuralError = true;
1162       if (FormatTok->is(tok::comma))
1163         nextToken();
1164     }
1165   }
1166   if (FormatTok->is(tok::l_brace)) {
1167     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1168     parseBlock(/*MustBeDeclaration=*/false);
1169     if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1170         Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1171         Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1172       addUnwrappedLine();
1173     } else {
1174       NeedsUnwrappedLine = true;
1175     }
1176   } else if (!FormatTok->is(tok::kw_catch)) {
1177     // The C++ standard requires a compound-statement after a try.
1178     // If there's none, we try to assume there's a structuralElement
1179     // and try to continue.
1180     StructuralError = true;
1181     addUnwrappedLine();
1182     ++Line->Level;
1183     parseStructuralElement();
1184     --Line->Level;
1185   }
1186   while (FormatTok->is(tok::kw_catch) ||
1187          ((Style.Language == FormatStyle::LK_Java ||
1188            Style.Language == FormatStyle::LK_JavaScript) &&
1189           FormatTok->is(Keywords.kw_finally))) {
1190     nextToken();
1191     while (FormatTok->isNot(tok::l_brace)) {
1192       if (FormatTok->is(tok::l_paren)) {
1193         parseParens();
1194         continue;
1195       }
1196       if (FormatTok->isOneOf(tok::semi, tok::r_brace))
1197         return;
1198       nextToken();
1199     }
1200     NeedsUnwrappedLine = false;
1201     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1202     parseBlock(/*MustBeDeclaration=*/false);
1203     if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1204         Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1205         Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1206       addUnwrappedLine();
1207     } else {
1208       NeedsUnwrappedLine = true;
1209     }
1210   }
1211   if (NeedsUnwrappedLine) {
1212     addUnwrappedLine();
1213   }
1214 }
1215 
1216 void UnwrappedLineParser::parseNamespace() {
1217   assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
1218 
1219   const FormatToken &InitialToken = *FormatTok;
1220   nextToken();
1221   if (FormatTok->Tok.is(tok::identifier))
1222     nextToken();
1223   if (FormatTok->Tok.is(tok::l_brace)) {
1224     if (ShouldBreakBeforeBrace(Style, InitialToken))
1225       addUnwrappedLine();
1226 
1227     bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1228                     (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1229                      DeclarationScopeStack.size() > 1);
1230     parseBlock(/*MustBeDeclaration=*/true, AddLevel);
1231     // Munch the semicolon after a namespace. This is more common than one would
1232     // think. Puttin the semicolon into its own line is very ugly.
1233     if (FormatTok->Tok.is(tok::semi))
1234       nextToken();
1235     addUnwrappedLine();
1236   }
1237   // FIXME: Add error handling.
1238 }
1239 
1240 void UnwrappedLineParser::parseForOrWhileLoop() {
1241   assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1242           FormatTok->IsForEachMacro) &&
1243          "'for', 'while' or foreach macro expected");
1244   nextToken();
1245   if (FormatTok->Tok.is(tok::l_paren))
1246     parseParens();
1247   if (FormatTok->Tok.is(tok::l_brace)) {
1248     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1249     parseBlock(/*MustBeDeclaration=*/false);
1250     addUnwrappedLine();
1251   } else {
1252     addUnwrappedLine();
1253     ++Line->Level;
1254     parseStructuralElement();
1255     --Line->Level;
1256   }
1257 }
1258 
1259 void UnwrappedLineParser::parseDoWhile() {
1260   assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
1261   nextToken();
1262   if (FormatTok->Tok.is(tok::l_brace)) {
1263     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1264     parseBlock(/*MustBeDeclaration=*/false);
1265     if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1266       addUnwrappedLine();
1267   } else {
1268     addUnwrappedLine();
1269     ++Line->Level;
1270     parseStructuralElement();
1271     --Line->Level;
1272   }
1273 
1274   // FIXME: Add error handling.
1275   if (!FormatTok->Tok.is(tok::kw_while)) {
1276     addUnwrappedLine();
1277     return;
1278   }
1279 
1280   nextToken();
1281   parseStructuralElement();
1282 }
1283 
1284 void UnwrappedLineParser::parseLabel() {
1285   nextToken();
1286   unsigned OldLineLevel = Line->Level;
1287   if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1288     --Line->Level;
1289   if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
1290     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1291     parseBlock(/*MustBeDeclaration=*/false);
1292     if (FormatTok->Tok.is(tok::kw_break)) {
1293       // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1294       if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1295           Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
1296         addUnwrappedLine();
1297       }
1298       parseStructuralElement();
1299     }
1300     addUnwrappedLine();
1301   } else {
1302     addUnwrappedLine();
1303   }
1304   Line->Level = OldLineLevel;
1305 }
1306 
1307 void UnwrappedLineParser::parseCaseLabel() {
1308   assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
1309   // FIXME: fix handling of complex expressions here.
1310   do {
1311     nextToken();
1312   } while (!eof() && !FormatTok->Tok.is(tok::colon));
1313   parseLabel();
1314 }
1315 
1316 void UnwrappedLineParser::parseSwitch() {
1317   assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
1318   nextToken();
1319   if (FormatTok->Tok.is(tok::l_paren))
1320     parseParens();
1321   if (FormatTok->Tok.is(tok::l_brace)) {
1322     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1323     parseBlock(/*MustBeDeclaration=*/false);
1324     addUnwrappedLine();
1325   } else {
1326     addUnwrappedLine();
1327     ++Line->Level;
1328     parseStructuralElement();
1329     --Line->Level;
1330   }
1331 }
1332 
1333 void UnwrappedLineParser::parseAccessSpecifier() {
1334   nextToken();
1335   // Understand Qt's slots.
1336   if (FormatTok->is(tok::identifier) &&
1337       (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
1338     nextToken();
1339   // Otherwise, we don't know what it is, and we'd better keep the next token.
1340   if (FormatTok->Tok.is(tok::colon))
1341     nextToken();
1342   addUnwrappedLine();
1343 }
1344 
1345 void UnwrappedLineParser::parseEnum() {
1346   // Won't be 'enum' for NS_ENUMs.
1347   if (FormatTok->Tok.is(tok::kw_enum))
1348     nextToken();
1349 
1350   // Eat up enum class ...
1351   if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1352     nextToken();
1353   while (FormatTok->Tok.getIdentifierInfo() ||
1354          FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1355                             tok::greater, tok::comma, tok::question)) {
1356     nextToken();
1357     // We can have macros or attributes in between 'enum' and the enum name.
1358     if (FormatTok->is(tok::l_paren))
1359       parseParens();
1360     if (FormatTok->is(tok::identifier))
1361       nextToken();
1362   }
1363 
1364   // Just a declaration or something is wrong.
1365   if (FormatTok->isNot(tok::l_brace))
1366     return;
1367   FormatTok->BlockKind = BK_Block;
1368 
1369   if (Style.Language == FormatStyle::LK_Java) {
1370     // Java enums are different.
1371     parseJavaEnumBody();
1372     return;
1373   }
1374 
1375   // Parse enum body.
1376   bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1377   if (HasError) {
1378     if (FormatTok->is(tok::semi))
1379       nextToken();
1380     addUnwrappedLine();
1381   }
1382 
1383   // We fall through to parsing a structural element afterwards, so that in
1384   // enum A {} n, m;
1385   // "} n, m;" will end up in one unwrapped line.
1386 }
1387 
1388 void UnwrappedLineParser::parseJavaEnumBody() {
1389   // Determine whether the enum is simple, i.e. does not have a semicolon or
1390   // constants with class bodies. Simple enums can be formatted like braced
1391   // lists, contracted to a single line, etc.
1392   unsigned StoredPosition = Tokens->getPosition();
1393   bool IsSimple = true;
1394   FormatToken *Tok = Tokens->getNextToken();
1395   while (Tok) {
1396     if (Tok->is(tok::r_brace))
1397       break;
1398     if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1399       IsSimple = false;
1400       break;
1401     }
1402     // FIXME: This will also mark enums with braces in the arguments to enum
1403     // constants as "not simple". This is probably fine in practice, though.
1404     Tok = Tokens->getNextToken();
1405   }
1406   FormatTok = Tokens->setPosition(StoredPosition);
1407 
1408   if (IsSimple) {
1409     parseBracedList();
1410     addUnwrappedLine();
1411     return;
1412   }
1413 
1414   // Parse the body of a more complex enum.
1415   // First add a line for everything up to the "{".
1416   nextToken();
1417   addUnwrappedLine();
1418   ++Line->Level;
1419 
1420   // Parse the enum constants.
1421   while (FormatTok) {
1422     if (FormatTok->is(tok::l_brace)) {
1423       // Parse the constant's class body.
1424       parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1425                  /*MunchSemi=*/false);
1426     } else if (FormatTok->is(tok::l_paren)) {
1427       parseParens();
1428     } else if (FormatTok->is(tok::comma)) {
1429       nextToken();
1430       addUnwrappedLine();
1431     } else if (FormatTok->is(tok::semi)) {
1432       nextToken();
1433       addUnwrappedLine();
1434       break;
1435     } else if (FormatTok->is(tok::r_brace)) {
1436       addUnwrappedLine();
1437       break;
1438     } else {
1439       nextToken();
1440     }
1441   }
1442 
1443   // Parse the class body after the enum's ";" if any.
1444   parseLevel(/*HasOpeningBrace=*/true);
1445   nextToken();
1446   --Line->Level;
1447   addUnwrappedLine();
1448 }
1449 
1450 void UnwrappedLineParser::parseRecord() {
1451   const FormatToken &InitialToken = *FormatTok;
1452   nextToken();
1453   if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1454                          tok::kw___declspec, tok::kw_alignas)) {
1455     nextToken();
1456     // We can have macros or attributes in between 'class' and the class name.
1457     if (FormatTok->Tok.is(tok::l_paren)) {
1458       parseParens();
1459     }
1460     // The actual identifier can be a nested name specifier, and in macros
1461     // it is often token-pasted.
1462     while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1463            FormatTok->is(tok::hashhash) ||
1464            (Style.Language == FormatStyle::LK_Java &&
1465             FormatTok->isOneOf(tok::period, tok::comma)))
1466       nextToken();
1467 
1468     // Note that parsing away template declarations here leads to incorrectly
1469     // accepting function declarations as record declarations.
1470     // In general, we cannot solve this problem. Consider:
1471     // class A<int> B() {}
1472     // which can be a function definition or a class definition when B() is a
1473     // macro. If we find enough real-world cases where this is a problem, we
1474     // can parse for the 'template' keyword in the beginning of the statement,
1475     // and thus rule out the record production in case there is no template
1476     // (this would still leave us with an ambiguity between template function
1477     // and class declarations).
1478     if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1479       while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1480         if (FormatTok->Tok.is(tok::semi))
1481           return;
1482         nextToken();
1483       }
1484     }
1485   }
1486   if (FormatTok->Tok.is(tok::l_brace)) {
1487     if (ShouldBreakBeforeBrace(Style, InitialToken))
1488       addUnwrappedLine();
1489 
1490     parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1491                /*MunchSemi=*/false);
1492   }
1493   // We fall through to parsing a structural element afterwards, so
1494   // class A {} n, m;
1495   // will end up in one unwrapped line.
1496   // This does not apply for Java.
1497   if (Style.Language == FormatStyle::LK_Java)
1498     addUnwrappedLine();
1499 }
1500 
1501 void UnwrappedLineParser::parseObjCProtocolList() {
1502   assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
1503   do
1504     nextToken();
1505   while (!eof() && FormatTok->Tok.isNot(tok::greater));
1506   nextToken(); // Skip '>'.
1507 }
1508 
1509 void UnwrappedLineParser::parseObjCUntilAtEnd() {
1510   do {
1511     if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
1512       nextToken();
1513       addUnwrappedLine();
1514       break;
1515     }
1516     if (FormatTok->is(tok::l_brace)) {
1517       parseBlock(/*MustBeDeclaration=*/false);
1518       // In ObjC interfaces, nothing should be following the "}".
1519       addUnwrappedLine();
1520     } else if (FormatTok->is(tok::r_brace)) {
1521       // Ignore stray "}". parseStructuralElement doesn't consume them.
1522       nextToken();
1523       addUnwrappedLine();
1524     } else {
1525       parseStructuralElement();
1526     }
1527   } while (!eof());
1528 }
1529 
1530 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
1531   nextToken();
1532   nextToken(); // interface name
1533 
1534   // @interface can be followed by either a base class, or a category.
1535   if (FormatTok->Tok.is(tok::colon)) {
1536     nextToken();
1537     nextToken(); // base class name
1538   } else if (FormatTok->Tok.is(tok::l_paren))
1539     // Skip category, if present.
1540     parseParens();
1541 
1542   if (FormatTok->Tok.is(tok::less))
1543     parseObjCProtocolList();
1544 
1545   if (FormatTok->Tok.is(tok::l_brace)) {
1546     if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1547         Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1548       addUnwrappedLine();
1549     parseBlock(/*MustBeDeclaration=*/true);
1550   }
1551 
1552   // With instance variables, this puts '}' on its own line.  Without instance
1553   // variables, this ends the @interface line.
1554   addUnwrappedLine();
1555 
1556   parseObjCUntilAtEnd();
1557 }
1558 
1559 void UnwrappedLineParser::parseObjCProtocol() {
1560   nextToken();
1561   nextToken(); // protocol name
1562 
1563   if (FormatTok->Tok.is(tok::less))
1564     parseObjCProtocolList();
1565 
1566   // Check for protocol declaration.
1567   if (FormatTok->Tok.is(tok::semi)) {
1568     nextToken();
1569     return addUnwrappedLine();
1570   }
1571 
1572   addUnwrappedLine();
1573   parseObjCUntilAtEnd();
1574 }
1575 
1576 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1577                                                  StringRef Prefix = "") {
1578   llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1579                << (Line.InPPDirective ? " MACRO" : "") << ": ";
1580   for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1581                                                     E = Line.Tokens.end();
1582        I != E; ++I) {
1583     llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
1584   }
1585   for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1586                                                     E = Line.Tokens.end();
1587        I != E; ++I) {
1588     const UnwrappedLineNode &Node = *I;
1589     for (SmallVectorImpl<UnwrappedLine>::const_iterator
1590              I = Node.Children.begin(),
1591              E = Node.Children.end();
1592          I != E; ++I) {
1593       printDebugInfo(*I, "\nChild: ");
1594     }
1595   }
1596   llvm::dbgs() << "\n";
1597 }
1598 
1599 void UnwrappedLineParser::addUnwrappedLine() {
1600   if (Line->Tokens.empty())
1601     return;
1602   DEBUG({
1603     if (CurrentLines == &Lines)
1604       printDebugInfo(*Line);
1605   });
1606   CurrentLines->push_back(*Line);
1607   Line->Tokens.clear();
1608   if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
1609     for (SmallVectorImpl<UnwrappedLine>::iterator
1610              I = PreprocessorDirectives.begin(),
1611              E = PreprocessorDirectives.end();
1612          I != E; ++I) {
1613       CurrentLines->push_back(*I);
1614     }
1615     PreprocessorDirectives.clear();
1616   }
1617 }
1618 
1619 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
1620 
1621 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
1622   return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1623          FormatTok.NewlinesBefore > 0;
1624 }
1625 
1626 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1627   bool JustComments = Line->Tokens.empty();
1628   for (SmallVectorImpl<FormatToken *>::const_iterator
1629            I = CommentsBeforeNextToken.begin(),
1630            E = CommentsBeforeNextToken.end();
1631        I != E; ++I) {
1632     if (isOnNewLine(**I) && JustComments) {
1633       addUnwrappedLine();
1634     }
1635     pushToken(*I);
1636   }
1637   if (NewlineBeforeNext && JustComments) {
1638     addUnwrappedLine();
1639   }
1640   CommentsBeforeNextToken.clear();
1641 }
1642 
1643 void UnwrappedLineParser::nextToken() {
1644   if (eof())
1645     return;
1646   flushComments(isOnNewLine(*FormatTok));
1647   pushToken(FormatTok);
1648   readToken();
1649 }
1650 
1651 void UnwrappedLineParser::readToken() {
1652   bool CommentsInCurrentLine = true;
1653   do {
1654     FormatTok = Tokens->getNextToken();
1655     assert(FormatTok);
1656     while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1657            (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
1658       // If there is an unfinished unwrapped line, we flush the preprocessor
1659       // directives only after that unwrapped line was finished later.
1660       bool SwitchToPreprocessorLines =
1661           !Line->Tokens.empty() && CurrentLines == &Lines;
1662       ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
1663       // Comments stored before the preprocessor directive need to be output
1664       // before the preprocessor directive, at the same level as the
1665       // preprocessor directive, as we consider them to apply to the directive.
1666       flushComments(isOnNewLine(*FormatTok));
1667       parsePPDirective();
1668     }
1669     while (FormatTok->Type == TT_ConflictStart ||
1670            FormatTok->Type == TT_ConflictEnd ||
1671            FormatTok->Type == TT_ConflictAlternative) {
1672       if (FormatTok->Type == TT_ConflictStart) {
1673         conditionalCompilationStart(/*Unreachable=*/false);
1674       } else if (FormatTok->Type == TT_ConflictAlternative) {
1675         conditionalCompilationAlternative();
1676       } else if (FormatTok->Type == TT_ConflictEnd) {
1677         conditionalCompilationEnd();
1678       }
1679       FormatTok = Tokens->getNextToken();
1680       FormatTok->MustBreakBefore = true;
1681     }
1682 
1683     if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1684         !Line->InPPDirective) {
1685       continue;
1686     }
1687 
1688     if (!FormatTok->Tok.is(tok::comment))
1689       return;
1690     if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
1691       CommentsInCurrentLine = false;
1692     }
1693     if (CommentsInCurrentLine) {
1694       pushToken(FormatTok);
1695     } else {
1696       CommentsBeforeNextToken.push_back(FormatTok);
1697     }
1698   } while (!eof());
1699 }
1700 
1701 void UnwrappedLineParser::pushToken(FormatToken *Tok) {
1702   Line->Tokens.push_back(UnwrappedLineNode(Tok));
1703   if (MustBreakBeforeNextToken) {
1704     Line->Tokens.back().Tok->MustBreakBefore = true;
1705     MustBreakBeforeNextToken = false;
1706   }
1707 }
1708 
1709 } // end namespace format
1710 } // end namespace clang
1711