]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Format/UnwrappedLineParser.cpp
MFV r302260: expat 2.2.0
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Format / UnwrappedLineParser.cpp
1 //===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file contains the implementation of the UnwrappedLineParser,
12 /// which turns a stream of tokens into UnwrappedLines.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #include "UnwrappedLineParser.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20
21 #define DEBUG_TYPE "format-parser"
22
23 namespace clang {
24 namespace format {
25
26 class FormatTokenSource {
27 public:
28   virtual ~FormatTokenSource() {}
29   virtual FormatToken *getNextToken() = 0;
30
31   virtual unsigned getPosition() = 0;
32   virtual FormatToken *setPosition(unsigned Position) = 0;
33 };
34
35 namespace {
36
37 class ScopedDeclarationState {
38 public:
39   ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
40                          bool MustBeDeclaration)
41       : Line(Line), Stack(Stack) {
42     Line.MustBeDeclaration = MustBeDeclaration;
43     Stack.push_back(MustBeDeclaration);
44   }
45   ~ScopedDeclarationState() {
46     Stack.pop_back();
47     if (!Stack.empty())
48       Line.MustBeDeclaration = Stack.back();
49     else
50       Line.MustBeDeclaration = true;
51   }
52
53 private:
54   UnwrappedLine &Line;
55   std::vector<bool> &Stack;
56 };
57
58 class ScopedMacroState : public FormatTokenSource {
59 public:
60   ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
61                    FormatToken *&ResetToken)
62       : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
63         PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
64         Token(nullptr) {
65     TokenSource = this;
66     Line.Level = 0;
67     Line.InPPDirective = true;
68   }
69
70   ~ScopedMacroState() override {
71     TokenSource = PreviousTokenSource;
72     ResetToken = Token;
73     Line.InPPDirective = false;
74     Line.Level = PreviousLineLevel;
75   }
76
77   FormatToken *getNextToken() override {
78     // The \c UnwrappedLineParser guards against this by never calling
79     // \c getNextToken() after it has encountered the first eof token.
80     assert(!eof());
81     Token = PreviousTokenSource->getNextToken();
82     if (eof())
83       return getFakeEOF();
84     return Token;
85   }
86
87   unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
88
89   FormatToken *setPosition(unsigned Position) override {
90     Token = PreviousTokenSource->setPosition(Position);
91     return Token;
92   }
93
94 private:
95   bool eof() { return Token && Token->HasUnescapedNewline; }
96
97   FormatToken *getFakeEOF() {
98     static bool EOFInitialized = false;
99     static FormatToken FormatTok;
100     if (!EOFInitialized) {
101       FormatTok.Tok.startToken();
102       FormatTok.Tok.setKind(tok::eof);
103       EOFInitialized = true;
104     }
105     return &FormatTok;
106   }
107
108   UnwrappedLine &Line;
109   FormatTokenSource *&TokenSource;
110   FormatToken *&ResetToken;
111   unsigned PreviousLineLevel;
112   FormatTokenSource *PreviousTokenSource;
113
114   FormatToken *Token;
115 };
116
117 } // end anonymous namespace
118
119 class ScopedLineState {
120 public:
121   ScopedLineState(UnwrappedLineParser &Parser,
122                   bool SwitchToPreprocessorLines = false)
123       : Parser(Parser), OriginalLines(Parser.CurrentLines) {
124     if (SwitchToPreprocessorLines)
125       Parser.CurrentLines = &Parser.PreprocessorDirectives;
126     else if (!Parser.Line->Tokens.empty())
127       Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
128     PreBlockLine = std::move(Parser.Line);
129     Parser.Line = llvm::make_unique<UnwrappedLine>();
130     Parser.Line->Level = PreBlockLine->Level;
131     Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
132   }
133
134   ~ScopedLineState() {
135     if (!Parser.Line->Tokens.empty()) {
136       Parser.addUnwrappedLine();
137     }
138     assert(Parser.Line->Tokens.empty());
139     Parser.Line = std::move(PreBlockLine);
140     if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
141       Parser.MustBreakBeforeNextToken = true;
142     Parser.CurrentLines = OriginalLines;
143   }
144
145 private:
146   UnwrappedLineParser &Parser;
147
148   std::unique_ptr<UnwrappedLine> PreBlockLine;
149   SmallVectorImpl<UnwrappedLine> *OriginalLines;
150 };
151
152 class CompoundStatementIndenter {
153 public:
154   CompoundStatementIndenter(UnwrappedLineParser *Parser,
155                             const FormatStyle &Style, unsigned &LineLevel)
156       : LineLevel(LineLevel), OldLineLevel(LineLevel) {
157     if (Style.BraceWrapping.AfterControlStatement)
158       Parser->addUnwrappedLine();
159     if (Style.BraceWrapping.IndentBraces)
160       ++LineLevel;
161   }
162   ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
163
164 private:
165   unsigned &LineLevel;
166   unsigned OldLineLevel;
167 };
168
169 namespace {
170
171 class IndexedTokenSource : public FormatTokenSource {
172 public:
173   IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
174       : Tokens(Tokens), Position(-1) {}
175
176   FormatToken *getNextToken() override {
177     ++Position;
178     return Tokens[Position];
179   }
180
181   unsigned getPosition() override {
182     assert(Position >= 0);
183     return Position;
184   }
185
186   FormatToken *setPosition(unsigned P) override {
187     Position = P;
188     return Tokens[Position];
189   }
190
191   void reset() { Position = -1; }
192
193 private:
194   ArrayRef<FormatToken *> Tokens;
195   int Position;
196 };
197
198 } // end anonymous namespace
199
200 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
201                                          const AdditionalKeywords &Keywords,
202                                          ArrayRef<FormatToken *> Tokens,
203                                          UnwrappedLineConsumer &Callback)
204     : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
205       CurrentLines(&Lines), Style(Style), Keywords(Keywords), Tokens(nullptr),
206       Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
207
208 void UnwrappedLineParser::reset() {
209   PPBranchLevel = -1;
210   Line.reset(new UnwrappedLine);
211   CommentsBeforeNextToken.clear();
212   FormatTok = nullptr;
213   MustBreakBeforeNextToken = false;
214   PreprocessorDirectives.clear();
215   CurrentLines = &Lines;
216   DeclarationScopeStack.clear();
217   PPStack.clear();
218 }
219
220 void UnwrappedLineParser::parse() {
221   IndexedTokenSource TokenSource(AllTokens);
222   do {
223     DEBUG(llvm::dbgs() << "----\n");
224     reset();
225     Tokens = &TokenSource;
226     TokenSource.reset();
227
228     readToken();
229     parseFile();
230     // Create line with eof token.
231     pushToken(FormatTok);
232     addUnwrappedLine();
233
234     for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
235                                                   E = Lines.end();
236          I != E; ++I) {
237       Callback.consumeUnwrappedLine(*I);
238     }
239     Callback.finishRun();
240     Lines.clear();
241     while (!PPLevelBranchIndex.empty() &&
242            PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
243       PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
244       PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
245     }
246     if (!PPLevelBranchIndex.empty()) {
247       ++PPLevelBranchIndex.back();
248       assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
249       assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
250     }
251   } while (!PPLevelBranchIndex.empty());
252 }
253
254 void UnwrappedLineParser::parseFile() {
255   // The top-level context in a file always has declarations, except for pre-
256   // processor directives and JavaScript files.
257   bool MustBeDeclaration =
258       !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
259   ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
260                                           MustBeDeclaration);
261   parseLevel(/*HasOpeningBrace=*/false);
262   // Make sure to format the remaining tokens.
263   flushComments(true);
264   addUnwrappedLine();
265 }
266
267 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
268   bool SwitchLabelEncountered = false;
269   do {
270     tok::TokenKind kind = FormatTok->Tok.getKind();
271     if (FormatTok->Type == TT_MacroBlockBegin) {
272       kind = tok::l_brace;
273     } else if (FormatTok->Type == TT_MacroBlockEnd) {
274       kind = tok::r_brace;
275     }
276
277     switch (kind) {
278     case tok::comment:
279       nextToken();
280       addUnwrappedLine();
281       break;
282     case tok::l_brace:
283       // FIXME: Add parameter whether this can happen - if this happens, we must
284       // be in a non-declaration context.
285       if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
286         continue;
287       parseBlock(/*MustBeDeclaration=*/false);
288       addUnwrappedLine();
289       break;
290     case tok::r_brace:
291       if (HasOpeningBrace)
292         return;
293       nextToken();
294       addUnwrappedLine();
295       break;
296     case tok::kw_default:
297     case tok::kw_case:
298       if (!SwitchLabelEncountered &&
299           (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
300         ++Line->Level;
301       SwitchLabelEncountered = true;
302       parseStructuralElement();
303       break;
304     default:
305       parseStructuralElement();
306       break;
307     }
308   } while (!eof());
309 }
310
311 void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
312   // We'll parse forward through the tokens until we hit
313   // a closing brace or eof - note that getNextToken() will
314   // parse macros, so this will magically work inside macro
315   // definitions, too.
316   unsigned StoredPosition = Tokens->getPosition();
317   FormatToken *Tok = FormatTok;
318   const FormatToken *PrevTok = getPreviousToken();
319   // Keep a stack of positions of lbrace tokens. We will
320   // update information about whether an lbrace starts a
321   // braced init list or a different block during the loop.
322   SmallVector<FormatToken *, 8> LBraceStack;
323   assert(Tok->Tok.is(tok::l_brace));
324   do {
325     // Get next non-comment token.
326     FormatToken *NextTok;
327     unsigned ReadTokens = 0;
328     do {
329       NextTok = Tokens->getNextToken();
330       ++ReadTokens;
331     } while (NextTok->is(tok::comment));
332
333     switch (Tok->Tok.getKind()) {
334     case tok::l_brace:
335       if (Style.Language == FormatStyle::LK_JavaScript && PrevTok &&
336           PrevTok->is(tok::colon))
337         // In TypeScript's TypeMemberLists, there can be semicolons between the
338         // individual members.
339         Tok->BlockKind = BK_BracedInit;
340       else
341         Tok->BlockKind = BK_Unknown;
342       LBraceStack.push_back(Tok);
343       break;
344     case tok::r_brace:
345       if (LBraceStack.empty())
346         break;
347       if (LBraceStack.back()->BlockKind == BK_Unknown) {
348         bool ProbablyBracedList = false;
349         if (Style.Language == FormatStyle::LK_Proto) {
350           ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
351         } else {
352           // Using OriginalColumn to distinguish between ObjC methods and
353           // binary operators is a bit hacky.
354           bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
355                                   NextTok->OriginalColumn == 0;
356
357           // If there is a comma, semicolon or right paren after the closing
358           // brace, we assume this is a braced initializer list.  Note that
359           // regardless how we mark inner braces here, we will overwrite the
360           // BlockKind later if we parse a braced list (where all blocks
361           // inside are by default braced lists), or when we explicitly detect
362           // blocks (for example while parsing lambdas).
363           //
364           // We exclude + and - as they can be ObjC visibility modifiers.
365           ProbablyBracedList =
366               NextTok->isOneOf(tok::comma, tok::period, tok::colon,
367                                tok::r_paren, tok::r_square, tok::l_brace,
368                                tok::l_square, tok::l_paren, tok::ellipsis) ||
369               (NextTok->is(tok::semi) &&
370                (!ExpectClassBody || LBraceStack.size() != 1)) ||
371               (NextTok->isBinaryOperator() && !NextIsObjCMethod);
372         }
373         if (ProbablyBracedList) {
374           Tok->BlockKind = BK_BracedInit;
375           LBraceStack.back()->BlockKind = BK_BracedInit;
376         } else {
377           Tok->BlockKind = BK_Block;
378           LBraceStack.back()->BlockKind = BK_Block;
379         }
380       }
381       LBraceStack.pop_back();
382       break;
383     case tok::at:
384     case tok::semi:
385     case tok::kw_if:
386     case tok::kw_while:
387     case tok::kw_for:
388     case tok::kw_switch:
389     case tok::kw_try:
390     case tok::kw___try:
391       if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
392         LBraceStack.back()->BlockKind = BK_Block;
393       break;
394     default:
395       break;
396     }
397     PrevTok = Tok;
398     Tok = NextTok;
399   } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
400
401   // Assume other blocks for all unclosed opening braces.
402   for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
403     if (LBraceStack[i]->BlockKind == BK_Unknown)
404       LBraceStack[i]->BlockKind = BK_Block;
405   }
406
407   FormatTok = Tokens->setPosition(StoredPosition);
408 }
409
410 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
411                                      bool MunchSemi) {
412   assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
413          "'{' or macro block token expected");
414   const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
415   FormatTok->BlockKind = BK_Block;
416
417   unsigned InitialLevel = Line->Level;
418   nextToken();
419
420   if (MacroBlock && FormatTok->is(tok::l_paren))
421     parseParens();
422
423   addUnwrappedLine();
424
425   ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
426                                           MustBeDeclaration);
427   if (AddLevel)
428     ++Line->Level;
429   parseLevel(/*HasOpeningBrace=*/true);
430
431   if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
432                  : !FormatTok->is(tok::r_brace)) {
433     Line->Level = InitialLevel;
434     FormatTok->BlockKind = BK_Block;
435     return;
436   }
437
438   nextToken(); // Munch the closing brace.
439
440   if (MacroBlock && FormatTok->is(tok::l_paren))
441     parseParens();
442
443   if (MunchSemi && FormatTok->Tok.is(tok::semi))
444     nextToken();
445   Line->Level = InitialLevel;
446 }
447
448 static bool isGoogScope(const UnwrappedLine &Line) {
449   // FIXME: Closure-library specific stuff should not be hard-coded but be
450   // configurable.
451   if (Line.Tokens.size() < 4)
452     return false;
453   auto I = Line.Tokens.begin();
454   if (I->Tok->TokenText != "goog")
455     return false;
456   ++I;
457   if (I->Tok->isNot(tok::period))
458     return false;
459   ++I;
460   if (I->Tok->TokenText != "scope")
461     return false;
462   ++I;
463   return I->Tok->is(tok::l_paren);
464 }
465
466 static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
467                                    const FormatToken &InitialToken) {
468   if (InitialToken.is(tok::kw_namespace))
469     return Style.BraceWrapping.AfterNamespace;
470   if (InitialToken.is(tok::kw_class))
471     return Style.BraceWrapping.AfterClass;
472   if (InitialToken.is(tok::kw_union))
473     return Style.BraceWrapping.AfterUnion;
474   if (InitialToken.is(tok::kw_struct))
475     return Style.BraceWrapping.AfterStruct;
476   return false;
477 }
478
479 void UnwrappedLineParser::parseChildBlock() {
480   FormatTok->BlockKind = BK_Block;
481   nextToken();
482   {
483     bool GoogScope =
484         Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
485     ScopedLineState LineState(*this);
486     ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
487                                             /*MustBeDeclaration=*/false);
488     Line->Level += GoogScope ? 0 : 1;
489     parseLevel(/*HasOpeningBrace=*/true);
490     flushComments(isOnNewLine(*FormatTok));
491     Line->Level -= GoogScope ? 0 : 1;
492   }
493   nextToken();
494 }
495
496 void UnwrappedLineParser::parsePPDirective() {
497   assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
498   ScopedMacroState MacroState(*Line, Tokens, FormatTok);
499   nextToken();
500
501   if (!FormatTok->Tok.getIdentifierInfo()) {
502     parsePPUnknown();
503     return;
504   }
505
506   switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
507   case tok::pp_define:
508     parsePPDefine();
509     return;
510   case tok::pp_if:
511     parsePPIf(/*IfDef=*/false);
512     break;
513   case tok::pp_ifdef:
514   case tok::pp_ifndef:
515     parsePPIf(/*IfDef=*/true);
516     break;
517   case tok::pp_else:
518     parsePPElse();
519     break;
520   case tok::pp_elif:
521     parsePPElIf();
522     break;
523   case tok::pp_endif:
524     parsePPEndIf();
525     break;
526   default:
527     parsePPUnknown();
528     break;
529   }
530 }
531
532 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
533   if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
534     PPStack.push_back(PP_Unreachable);
535   else
536     PPStack.push_back(PP_Conditional);
537 }
538
539 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
540   ++PPBranchLevel;
541   assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
542   if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
543     PPLevelBranchIndex.push_back(0);
544     PPLevelBranchCount.push_back(0);
545   }
546   PPChainBranchIndex.push(0);
547   bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
548   conditionalCompilationCondition(Unreachable || Skip);
549 }
550
551 void UnwrappedLineParser::conditionalCompilationAlternative() {
552   if (!PPStack.empty())
553     PPStack.pop_back();
554   assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
555   if (!PPChainBranchIndex.empty())
556     ++PPChainBranchIndex.top();
557   conditionalCompilationCondition(
558       PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
559       PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
560 }
561
562 void UnwrappedLineParser::conditionalCompilationEnd() {
563   assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
564   if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
565     if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
566       PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
567     }
568   }
569   // Guard against #endif's without #if.
570   if (PPBranchLevel > 0)
571     --PPBranchLevel;
572   if (!PPChainBranchIndex.empty())
573     PPChainBranchIndex.pop();
574   if (!PPStack.empty())
575     PPStack.pop_back();
576 }
577
578 void UnwrappedLineParser::parsePPIf(bool IfDef) {
579   nextToken();
580   bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
581                          FormatTok->Tok.getLiteralData() != nullptr &&
582                          StringRef(FormatTok->Tok.getLiteralData(),
583                                    FormatTok->Tok.getLength()) == "0") ||
584                         FormatTok->Tok.is(tok::kw_false);
585   conditionalCompilationStart(!IfDef && IsLiteralFalse);
586   parsePPUnknown();
587 }
588
589 void UnwrappedLineParser::parsePPElse() {
590   conditionalCompilationAlternative();
591   parsePPUnknown();
592 }
593
594 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
595
596 void UnwrappedLineParser::parsePPEndIf() {
597   conditionalCompilationEnd();
598   parsePPUnknown();
599 }
600
601 void UnwrappedLineParser::parsePPDefine() {
602   nextToken();
603
604   if (FormatTok->Tok.getKind() != tok::identifier) {
605     parsePPUnknown();
606     return;
607   }
608   nextToken();
609   if (FormatTok->Tok.getKind() == tok::l_paren &&
610       FormatTok->WhitespaceRange.getBegin() ==
611           FormatTok->WhitespaceRange.getEnd()) {
612     parseParens();
613   }
614   addUnwrappedLine();
615   Line->Level = 1;
616
617   // Errors during a preprocessor directive can only affect the layout of the
618   // preprocessor directive, and thus we ignore them. An alternative approach
619   // would be to use the same approach we use on the file level (no
620   // re-indentation if there was a structural error) within the macro
621   // definition.
622   parseFile();
623 }
624
625 void UnwrappedLineParser::parsePPUnknown() {
626   do {
627     nextToken();
628   } while (!eof());
629   addUnwrappedLine();
630 }
631
632 // Here we blacklist certain tokens that are not usually the first token in an
633 // unwrapped line. This is used in attempt to distinguish macro calls without
634 // trailing semicolons from other constructs split to several lines.
635 static bool tokenCanStartNewLine(const clang::Token &Tok) {
636   // Semicolon can be a null-statement, l_square can be a start of a macro or
637   // a C++11 attribute, but this doesn't seem to be common.
638   return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
639          Tok.isNot(tok::l_square) &&
640          // Tokens that can only be used as binary operators and a part of
641          // overloaded operator names.
642          Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
643          Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
644          Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
645          Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
646          Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
647          Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
648          Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
649          Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
650          Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
651          Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
652          Tok.isNot(tok::lesslessequal) &&
653          // Colon is used in labels, base class lists, initializer lists,
654          // range-based for loops, ternary operator, but should never be the
655          // first token in an unwrapped line.
656          Tok.isNot(tok::colon) &&
657          // 'noexcept' is a trailing annotation.
658          Tok.isNot(tok::kw_noexcept);
659 }
660
661 void UnwrappedLineParser::parseStructuralElement() {
662   assert(!FormatTok->is(tok::l_brace));
663   if (Style.Language == FormatStyle::LK_TableGen &&
664       FormatTok->is(tok::pp_include)) {
665     nextToken();
666     if (FormatTok->is(tok::string_literal))
667       nextToken();
668     addUnwrappedLine();
669     return;
670   }
671   switch (FormatTok->Tok.getKind()) {
672   case tok::at:
673     nextToken();
674     if (FormatTok->Tok.is(tok::l_brace)) {
675       parseBracedList();
676       break;
677     }
678     switch (FormatTok->Tok.getObjCKeywordID()) {
679     case tok::objc_public:
680     case tok::objc_protected:
681     case tok::objc_package:
682     case tok::objc_private:
683       return parseAccessSpecifier();
684     case tok::objc_interface:
685     case tok::objc_implementation:
686       return parseObjCInterfaceOrImplementation();
687     case tok::objc_protocol:
688       return parseObjCProtocol();
689     case tok::objc_end:
690       return; // Handled by the caller.
691     case tok::objc_optional:
692     case tok::objc_required:
693       nextToken();
694       addUnwrappedLine();
695       return;
696     case tok::objc_autoreleasepool:
697       nextToken();
698       if (FormatTok->Tok.is(tok::l_brace)) {
699         if (Style.BraceWrapping.AfterObjCDeclaration)
700           addUnwrappedLine();
701         parseBlock(/*MustBeDeclaration=*/false);
702       }
703       addUnwrappedLine();
704       return;
705     case tok::objc_try:
706       // This branch isn't strictly necessary (the kw_try case below would
707       // do this too after the tok::at is parsed above).  But be explicit.
708       parseTryCatch();
709       return;
710     default:
711       break;
712     }
713     break;
714   case tok::kw_asm:
715     nextToken();
716     if (FormatTok->is(tok::l_brace)) {
717       FormatTok->Type = TT_InlineASMBrace;
718       nextToken();
719       while (FormatTok && FormatTok->isNot(tok::eof)) {
720         if (FormatTok->is(tok::r_brace)) {
721           FormatTok->Type = TT_InlineASMBrace;
722           nextToken();
723           addUnwrappedLine();
724           break;
725         }
726         FormatTok->Finalized = true;
727         nextToken();
728       }
729     }
730     break;
731   case tok::kw_namespace:
732     parseNamespace();
733     return;
734   case tok::kw_inline:
735     nextToken();
736     if (FormatTok->Tok.is(tok::kw_namespace)) {
737       parseNamespace();
738       return;
739     }
740     break;
741   case tok::kw_public:
742   case tok::kw_protected:
743   case tok::kw_private:
744     if (Style.Language == FormatStyle::LK_Java ||
745         Style.Language == FormatStyle::LK_JavaScript)
746       nextToken();
747     else
748       parseAccessSpecifier();
749     return;
750   case tok::kw_if:
751     parseIfThenElse();
752     return;
753   case tok::kw_for:
754   case tok::kw_while:
755     parseForOrWhileLoop();
756     return;
757   case tok::kw_do:
758     parseDoWhile();
759     return;
760   case tok::kw_switch:
761     parseSwitch();
762     return;
763   case tok::kw_default:
764     nextToken();
765     parseLabel();
766     return;
767   case tok::kw_case:
768     parseCaseLabel();
769     return;
770   case tok::kw_try:
771   case tok::kw___try:
772     parseTryCatch();
773     return;
774   case tok::kw_extern:
775     nextToken();
776     if (FormatTok->Tok.is(tok::string_literal)) {
777       nextToken();
778       if (FormatTok->Tok.is(tok::l_brace)) {
779         parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
780         addUnwrappedLine();
781         return;
782       }
783     }
784     break;
785   case tok::kw_export:
786     if (Style.Language == FormatStyle::LK_JavaScript) {
787       parseJavaScriptEs6ImportExport();
788       return;
789     }
790     break;
791   case tok::identifier:
792     if (FormatTok->is(TT_ForEachMacro)) {
793       parseForOrWhileLoop();
794       return;
795     }
796     if (FormatTok->is(TT_MacroBlockBegin)) {
797       parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
798                  /*MunchSemi=*/false);
799       return;
800     }
801     if (Style.Language == FormatStyle::LK_JavaScript &&
802         FormatTok->is(Keywords.kw_import)) {
803       parseJavaScriptEs6ImportExport();
804       return;
805     }
806     if (FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
807                            Keywords.kw_slots, Keywords.kw_qslots)) {
808       nextToken();
809       if (FormatTok->is(tok::colon)) {
810         nextToken();
811         addUnwrappedLine();
812       }
813       return;
814     }
815     // In all other cases, parse the declaration.
816     break;
817   default:
818     break;
819   }
820   do {
821     switch (FormatTok->Tok.getKind()) {
822     case tok::at:
823       nextToken();
824       if (FormatTok->Tok.is(tok::l_brace))
825         parseBracedList();
826       break;
827     case tok::kw_enum:
828       // parseEnum falls through and does not yet add an unwrapped line as an
829       // enum definition can start a structural element.
830       if (!parseEnum())
831         break;
832       // This only applies for C++.
833       if (Style.Language != FormatStyle::LK_Cpp) {
834         addUnwrappedLine();
835         return;
836       }
837       break;
838     case tok::kw_typedef:
839       nextToken();
840       if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
841                              Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
842         parseEnum();
843       break;
844     case tok::kw_struct:
845     case tok::kw_union:
846     case tok::kw_class:
847       // parseRecord falls through and does not yet add an unwrapped line as a
848       // record declaration or definition can start a structural element.
849       parseRecord();
850       // This does not apply for Java and JavaScript.
851       if (Style.Language == FormatStyle::LK_Java ||
852           Style.Language == FormatStyle::LK_JavaScript) {
853         if (FormatTok->is(tok::semi))
854           nextToken();
855         addUnwrappedLine();
856         return;
857       }
858       break;
859     case tok::period:
860       nextToken();
861       // In Java, classes have an implicit static member "class".
862       if (Style.Language == FormatStyle::LK_Java && FormatTok &&
863           FormatTok->is(tok::kw_class))
864         nextToken();
865       if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
866           FormatTok->Tok.getIdentifierInfo())
867         // JavaScript only has pseudo keywords, all keywords are allowed to
868         // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
869         nextToken();
870       break;
871     case tok::semi:
872       nextToken();
873       addUnwrappedLine();
874       return;
875     case tok::r_brace:
876       addUnwrappedLine();
877       return;
878     case tok::l_paren:
879       parseParens();
880       break;
881     case tok::kw_operator:
882       nextToken();
883       if (FormatTok->isBinaryOperator())
884         nextToken();
885       break;
886     case tok::caret:
887       nextToken();
888       if (FormatTok->Tok.isAnyIdentifier() ||
889           FormatTok->isSimpleTypeSpecifier())
890         nextToken();
891       if (FormatTok->is(tok::l_paren))
892         parseParens();
893       if (FormatTok->is(tok::l_brace))
894         parseChildBlock();
895       break;
896     case tok::l_brace:
897       if (!tryToParseBracedList()) {
898         // A block outside of parentheses must be the last part of a
899         // structural element.
900         // FIXME: Figure out cases where this is not true, and add projections
901         // for them (the one we know is missing are lambdas).
902         if (Style.BraceWrapping.AfterFunction)
903           addUnwrappedLine();
904         FormatTok->Type = TT_FunctionLBrace;
905         parseBlock(/*MustBeDeclaration=*/false);
906         addUnwrappedLine();
907         return;
908       }
909       // Otherwise this was a braced init list, and the structural
910       // element continues.
911       break;
912     case tok::kw_try:
913       // We arrive here when parsing function-try blocks.
914       parseTryCatch();
915       return;
916     case tok::identifier: {
917       if (FormatTok->is(TT_MacroBlockEnd)) {
918         addUnwrappedLine();
919         return;
920       }
921
922       // Parse function literal unless 'function' is the first token in a line
923       // in which case this should be treated as a free-standing function.
924       if (Style.Language == FormatStyle::LK_JavaScript &&
925           FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) {
926         tryToParseJSFunction();
927         break;
928       }
929       if ((Style.Language == FormatStyle::LK_JavaScript ||
930            Style.Language == FormatStyle::LK_Java) &&
931           FormatTok->is(Keywords.kw_interface)) {
932         parseRecord();
933         addUnwrappedLine();
934         return;
935       }
936
937       StringRef Text = FormatTok->TokenText;
938       nextToken();
939       if (Line->Tokens.size() == 1 &&
940           // JS doesn't have macros, and within classes colons indicate fields,
941           // not labels.
942           Style.Language != FormatStyle::LK_JavaScript) {
943         if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
944           parseLabel();
945           return;
946         }
947         // Recognize function-like macro usages without trailing semicolon as
948         // well as free-standing macros like Q_OBJECT.
949         bool FunctionLike = FormatTok->is(tok::l_paren);
950         if (FunctionLike)
951           parseParens();
952
953         bool FollowedByNewline =
954             CommentsBeforeNextToken.empty()
955                 ? FormatTok->NewlinesBefore > 0
956                 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
957
958         if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
959             tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
960           addUnwrappedLine();
961           return;
962         }
963       }
964       break;
965     }
966     case tok::equal:
967       // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
968       // TT_JsFatArrow. The always start an expression or a child block if
969       // followed by a curly.
970       if (FormatTok->is(TT_JsFatArrow)) {
971         nextToken();
972         if (FormatTok->is(tok::l_brace))
973           parseChildBlock();
974         break;
975       }
976
977       nextToken();
978       if (FormatTok->Tok.is(tok::l_brace)) {
979         parseBracedList();
980       }
981       break;
982     case tok::l_square:
983       parseSquare();
984       break;
985     case tok::kw_new:
986       parseNew();
987       break;
988     default:
989       nextToken();
990       break;
991     }
992   } while (!eof());
993 }
994
995 bool UnwrappedLineParser::tryToParseLambda() {
996   if (Style.Language != FormatStyle::LK_Cpp) {
997     nextToken();
998     return false;
999   }
1000   const FormatToken* Previous = getPreviousToken();
1001   if (Previous &&
1002       (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1003                          tok::kw_delete) ||
1004        Previous->closesScope() || Previous->isSimpleTypeSpecifier())) {
1005     nextToken();
1006     return false;
1007   }
1008   assert(FormatTok->is(tok::l_square));
1009   FormatToken &LSquare = *FormatTok;
1010   if (!tryToParseLambdaIntroducer())
1011     return false;
1012
1013   while (FormatTok->isNot(tok::l_brace)) {
1014     if (FormatTok->isSimpleTypeSpecifier()) {
1015       nextToken();
1016       continue;
1017     }
1018     switch (FormatTok->Tok.getKind()) {
1019     case tok::l_brace:
1020       break;
1021     case tok::l_paren:
1022       parseParens();
1023       break;
1024     case tok::amp:
1025     case tok::star:
1026     case tok::kw_const:
1027     case tok::comma:
1028     case tok::less:
1029     case tok::greater:
1030     case tok::identifier:
1031     case tok::numeric_constant:
1032     case tok::coloncolon:
1033     case tok::kw_mutable:
1034       nextToken();
1035       break;
1036     case tok::arrow:
1037       FormatTok->Type = TT_LambdaArrow;
1038       nextToken();
1039       break;
1040     default:
1041       return true;
1042     }
1043   }
1044   LSquare.Type = TT_LambdaLSquare;
1045   parseChildBlock();
1046   return true;
1047 }
1048
1049 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1050   nextToken();
1051   if (FormatTok->is(tok::equal)) {
1052     nextToken();
1053     if (FormatTok->is(tok::r_square)) {
1054       nextToken();
1055       return true;
1056     }
1057     if (FormatTok->isNot(tok::comma))
1058       return false;
1059     nextToken();
1060   } else if (FormatTok->is(tok::amp)) {
1061     nextToken();
1062     if (FormatTok->is(tok::r_square)) {
1063       nextToken();
1064       return true;
1065     }
1066     if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
1067       return false;
1068     }
1069     if (FormatTok->is(tok::comma))
1070       nextToken();
1071   } else if (FormatTok->is(tok::r_square)) {
1072     nextToken();
1073     return true;
1074   }
1075   do {
1076     if (FormatTok->is(tok::amp))
1077       nextToken();
1078     if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
1079       return false;
1080     nextToken();
1081     if (FormatTok->is(tok::ellipsis))
1082       nextToken();
1083     if (FormatTok->is(tok::comma)) {
1084       nextToken();
1085     } else if (FormatTok->is(tok::r_square)) {
1086       nextToken();
1087       return true;
1088     } else {
1089       return false;
1090     }
1091   } while (!eof());
1092   return false;
1093 }
1094
1095 void UnwrappedLineParser::tryToParseJSFunction() {
1096   nextToken();
1097
1098   // Consume function name.
1099   if (FormatTok->is(tok::identifier))
1100     nextToken();
1101
1102   if (FormatTok->isNot(tok::l_paren))
1103     return;
1104
1105   // Parse formal parameter list.
1106   parseParens();
1107
1108   if (FormatTok->is(tok::colon)) {
1109     // Parse a type definition.
1110     nextToken();
1111
1112     // Eat the type declaration. For braced inline object types, balance braces,
1113     // otherwise just parse until finding an l_brace for the function body.
1114     if (FormatTok->is(tok::l_brace))
1115       tryToParseBracedList();
1116     else
1117       while (FormatTok->isNot(tok::l_brace) && !eof())
1118         nextToken();
1119   }
1120
1121   parseChildBlock();
1122 }
1123
1124 bool UnwrappedLineParser::tryToParseBracedList() {
1125   if (FormatTok->BlockKind == BK_Unknown)
1126     calculateBraceTypes();
1127   assert(FormatTok->BlockKind != BK_Unknown);
1128   if (FormatTok->BlockKind == BK_Block)
1129     return false;
1130   parseBracedList();
1131   return true;
1132 }
1133
1134 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1135   bool HasError = false;
1136   nextToken();
1137
1138   // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1139   // replace this by using parseAssigmentExpression() inside.
1140   do {
1141     if (Style.Language == FormatStyle::LK_JavaScript) {
1142       if (FormatTok->is(Keywords.kw_function)) {
1143         tryToParseJSFunction();
1144         continue;
1145       }
1146       if (FormatTok->is(TT_JsFatArrow)) {
1147         nextToken();
1148         // Fat arrows can be followed by simple expressions or by child blocks
1149         // in curly braces.
1150         if (FormatTok->is(tok::l_brace)) {
1151           parseChildBlock();
1152           continue;
1153         }
1154       }
1155     }
1156     switch (FormatTok->Tok.getKind()) {
1157     case tok::caret:
1158       nextToken();
1159       if (FormatTok->is(tok::l_brace)) {
1160         parseChildBlock();
1161       }
1162       break;
1163     case tok::l_square:
1164       tryToParseLambda();
1165       break;
1166     case tok::l_brace:
1167       // Assume there are no blocks inside a braced init list apart
1168       // from the ones we explicitly parse out (like lambdas).
1169       FormatTok->BlockKind = BK_BracedInit;
1170       parseBracedList();
1171       break;
1172     case tok::l_paren:
1173       parseParens();
1174       // JavaScript can just have free standing methods and getters/setters in
1175       // object literals. Detect them by a "{" following ")".
1176       if (Style.Language == FormatStyle::LK_JavaScript) {
1177         if (FormatTok->is(tok::l_brace))
1178           parseChildBlock();
1179         break;
1180       }
1181       break;
1182     case tok::r_brace:
1183       nextToken();
1184       return !HasError;
1185     case tok::semi:
1186       // JavaScript (or more precisely TypeScript) can have semicolons in braced
1187       // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1188       // used for error recovery if we have otherwise determined that this is
1189       // a braced list.
1190       if (Style.Language == FormatStyle::LK_JavaScript) {
1191         nextToken();
1192         break;
1193       }
1194       HasError = true;
1195       if (!ContinueOnSemicolons)
1196         return !HasError;
1197       nextToken();
1198       break;
1199     case tok::comma:
1200       nextToken();
1201       break;
1202     default:
1203       nextToken();
1204       break;
1205     }
1206   } while (!eof());
1207   return false;
1208 }
1209
1210 void UnwrappedLineParser::parseParens() {
1211   assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
1212   nextToken();
1213   do {
1214     switch (FormatTok->Tok.getKind()) {
1215     case tok::l_paren:
1216       parseParens();
1217       if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1218         parseChildBlock();
1219       break;
1220     case tok::r_paren:
1221       nextToken();
1222       return;
1223     case tok::r_brace:
1224       // A "}" inside parenthesis is an error if there wasn't a matching "{".
1225       return;
1226     case tok::l_square:
1227       tryToParseLambda();
1228       break;
1229     case tok::l_brace:
1230       if (!tryToParseBracedList())
1231         parseChildBlock();
1232       break;
1233     case tok::at:
1234       nextToken();
1235       if (FormatTok->Tok.is(tok::l_brace))
1236         parseBracedList();
1237       break;
1238     case tok::identifier:
1239       if (Style.Language == FormatStyle::LK_JavaScript &&
1240           FormatTok->is(Keywords.kw_function))
1241         tryToParseJSFunction();
1242       else
1243         nextToken();
1244       break;
1245     default:
1246       nextToken();
1247       break;
1248     }
1249   } while (!eof());
1250 }
1251
1252 void UnwrappedLineParser::parseSquare() {
1253   assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1254   if (tryToParseLambda())
1255     return;
1256   do {
1257     switch (FormatTok->Tok.getKind()) {
1258     case tok::l_paren:
1259       parseParens();
1260       break;
1261     case tok::r_square:
1262       nextToken();
1263       return;
1264     case tok::r_brace:
1265       // A "}" inside parenthesis is an error if there wasn't a matching "{".
1266       return;
1267     case tok::l_square:
1268       parseSquare();
1269       break;
1270     case tok::l_brace: {
1271       if (!tryToParseBracedList())
1272         parseChildBlock();
1273       break;
1274     }
1275     case tok::at:
1276       nextToken();
1277       if (FormatTok->Tok.is(tok::l_brace))
1278         parseBracedList();
1279       break;
1280     default:
1281       nextToken();
1282       break;
1283     }
1284   } while (!eof());
1285 }
1286
1287 void UnwrappedLineParser::parseIfThenElse() {
1288   assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
1289   nextToken();
1290   if (FormatTok->Tok.is(tok::l_paren))
1291     parseParens();
1292   bool NeedsUnwrappedLine = false;
1293   if (FormatTok->Tok.is(tok::l_brace)) {
1294     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1295     parseBlock(/*MustBeDeclaration=*/false);
1296     if (Style.BraceWrapping.BeforeElse)
1297       addUnwrappedLine();
1298     else
1299       NeedsUnwrappedLine = true;
1300   } else {
1301     addUnwrappedLine();
1302     ++Line->Level;
1303     parseStructuralElement();
1304     --Line->Level;
1305   }
1306   if (FormatTok->Tok.is(tok::kw_else)) {
1307     nextToken();
1308     if (FormatTok->Tok.is(tok::l_brace)) {
1309       CompoundStatementIndenter Indenter(this, Style, Line->Level);
1310       parseBlock(/*MustBeDeclaration=*/false);
1311       addUnwrappedLine();
1312     } else if (FormatTok->Tok.is(tok::kw_if)) {
1313       parseIfThenElse();
1314     } else {
1315       addUnwrappedLine();
1316       ++Line->Level;
1317       parseStructuralElement();
1318       --Line->Level;
1319     }
1320   } else if (NeedsUnwrappedLine) {
1321     addUnwrappedLine();
1322   }
1323 }
1324
1325 void UnwrappedLineParser::parseTryCatch() {
1326   assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
1327   nextToken();
1328   bool NeedsUnwrappedLine = false;
1329   if (FormatTok->is(tok::colon)) {
1330     // We are in a function try block, what comes is an initializer list.
1331     nextToken();
1332     while (FormatTok->is(tok::identifier)) {
1333       nextToken();
1334       if (FormatTok->is(tok::l_paren))
1335         parseParens();
1336       if (FormatTok->is(tok::comma))
1337         nextToken();
1338     }
1339   }
1340   // Parse try with resource.
1341   if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1342     parseParens();
1343   }
1344   if (FormatTok->is(tok::l_brace)) {
1345     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1346     parseBlock(/*MustBeDeclaration=*/false);
1347     if (Style.BraceWrapping.BeforeCatch) {
1348       addUnwrappedLine();
1349     } else {
1350       NeedsUnwrappedLine = true;
1351     }
1352   } else if (!FormatTok->is(tok::kw_catch)) {
1353     // The C++ standard requires a compound-statement after a try.
1354     // If there's none, we try to assume there's a structuralElement
1355     // and try to continue.
1356     addUnwrappedLine();
1357     ++Line->Level;
1358     parseStructuralElement();
1359     --Line->Level;
1360   }
1361   while (1) {
1362     if (FormatTok->is(tok::at))
1363       nextToken();
1364     if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1365                              tok::kw___finally) ||
1366           ((Style.Language == FormatStyle::LK_Java ||
1367             Style.Language == FormatStyle::LK_JavaScript) &&
1368            FormatTok->is(Keywords.kw_finally)) ||
1369           (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1370            FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1371       break;
1372     nextToken();
1373     while (FormatTok->isNot(tok::l_brace)) {
1374       if (FormatTok->is(tok::l_paren)) {
1375         parseParens();
1376         continue;
1377       }
1378       if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
1379         return;
1380       nextToken();
1381     }
1382     NeedsUnwrappedLine = false;
1383     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1384     parseBlock(/*MustBeDeclaration=*/false);
1385     if (Style.BraceWrapping.BeforeCatch)
1386       addUnwrappedLine();
1387     else
1388       NeedsUnwrappedLine = true;
1389   }
1390   if (NeedsUnwrappedLine)
1391     addUnwrappedLine();
1392 }
1393
1394 void UnwrappedLineParser::parseNamespace() {
1395   assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
1396
1397   const FormatToken &InitialToken = *FormatTok;
1398   nextToken();
1399   while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
1400     nextToken();
1401   if (FormatTok->Tok.is(tok::l_brace)) {
1402     if (ShouldBreakBeforeBrace(Style, InitialToken))
1403       addUnwrappedLine();
1404
1405     bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1406                     (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1407                      DeclarationScopeStack.size() > 1);
1408     parseBlock(/*MustBeDeclaration=*/true, AddLevel);
1409     // Munch the semicolon after a namespace. This is more common than one would
1410     // think. Puttin the semicolon into its own line is very ugly.
1411     if (FormatTok->Tok.is(tok::semi))
1412       nextToken();
1413     addUnwrappedLine();
1414   }
1415   // FIXME: Add error handling.
1416 }
1417
1418 void UnwrappedLineParser::parseNew() {
1419   assert(FormatTok->is(tok::kw_new) && "'new' expected");
1420   nextToken();
1421   if (Style.Language != FormatStyle::LK_Java)
1422     return;
1423
1424   // In Java, we can parse everything up to the parens, which aren't optional.
1425   do {
1426     // There should not be a ;, { or } before the new's open paren.
1427     if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1428       return;
1429
1430     // Consume the parens.
1431     if (FormatTok->is(tok::l_paren)) {
1432       parseParens();
1433
1434       // If there is a class body of an anonymous class, consume that as child.
1435       if (FormatTok->is(tok::l_brace))
1436         parseChildBlock();
1437       return;
1438     }
1439     nextToken();
1440   } while (!eof());
1441 }
1442
1443 void UnwrappedLineParser::parseForOrWhileLoop() {
1444   assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
1445          "'for', 'while' or foreach macro expected");
1446   nextToken();
1447   if (FormatTok->Tok.is(tok::l_paren))
1448     parseParens();
1449   if (FormatTok->Tok.is(tok::l_brace)) {
1450     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1451     parseBlock(/*MustBeDeclaration=*/false);
1452     addUnwrappedLine();
1453   } else {
1454     addUnwrappedLine();
1455     ++Line->Level;
1456     parseStructuralElement();
1457     --Line->Level;
1458   }
1459 }
1460
1461 void UnwrappedLineParser::parseDoWhile() {
1462   assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
1463   nextToken();
1464   if (FormatTok->Tok.is(tok::l_brace)) {
1465     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1466     parseBlock(/*MustBeDeclaration=*/false);
1467     if (Style.BraceWrapping.IndentBraces)
1468       addUnwrappedLine();
1469   } else {
1470     addUnwrappedLine();
1471     ++Line->Level;
1472     parseStructuralElement();
1473     --Line->Level;
1474   }
1475
1476   // FIXME: Add error handling.
1477   if (!FormatTok->Tok.is(tok::kw_while)) {
1478     addUnwrappedLine();
1479     return;
1480   }
1481
1482   nextToken();
1483   parseStructuralElement();
1484 }
1485
1486 void UnwrappedLineParser::parseLabel() {
1487   nextToken();
1488   unsigned OldLineLevel = Line->Level;
1489   if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1490     --Line->Level;
1491   if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
1492     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1493     parseBlock(/*MustBeDeclaration=*/false);
1494     if (FormatTok->Tok.is(tok::kw_break)) {
1495       if (Style.BraceWrapping.AfterControlStatement)
1496         addUnwrappedLine();
1497       parseStructuralElement();
1498     }
1499     addUnwrappedLine();
1500   } else {
1501     if (FormatTok->is(tok::semi))
1502       nextToken();
1503     addUnwrappedLine();
1504   }
1505   Line->Level = OldLineLevel;
1506 }
1507
1508 void UnwrappedLineParser::parseCaseLabel() {
1509   assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
1510   // FIXME: fix handling of complex expressions here.
1511   do {
1512     nextToken();
1513   } while (!eof() && !FormatTok->Tok.is(tok::colon));
1514   parseLabel();
1515 }
1516
1517 void UnwrappedLineParser::parseSwitch() {
1518   assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
1519   nextToken();
1520   if (FormatTok->Tok.is(tok::l_paren))
1521     parseParens();
1522   if (FormatTok->Tok.is(tok::l_brace)) {
1523     CompoundStatementIndenter Indenter(this, Style, Line->Level);
1524     parseBlock(/*MustBeDeclaration=*/false);
1525     addUnwrappedLine();
1526   } else {
1527     addUnwrappedLine();
1528     ++Line->Level;
1529     parseStructuralElement();
1530     --Line->Level;
1531   }
1532 }
1533
1534 void UnwrappedLineParser::parseAccessSpecifier() {
1535   nextToken();
1536   // Understand Qt's slots.
1537   if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
1538     nextToken();
1539   // Otherwise, we don't know what it is, and we'd better keep the next token.
1540   if (FormatTok->Tok.is(tok::colon))
1541     nextToken();
1542   addUnwrappedLine();
1543 }
1544
1545 bool UnwrappedLineParser::parseEnum() {
1546   // Won't be 'enum' for NS_ENUMs.
1547   if (FormatTok->Tok.is(tok::kw_enum))
1548     nextToken();
1549
1550   // In TypeScript, "enum" can also be used as property name, e.g. in interface
1551   // declarations. An "enum" keyword followed by a colon would be a syntax
1552   // error and thus assume it is just an identifier.
1553   if (Style.Language == FormatStyle::LK_JavaScript && FormatTok->is(tok::colon))
1554     return false;
1555
1556   // Eat up enum class ...
1557   if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1558     nextToken();
1559
1560   while (FormatTok->Tok.getIdentifierInfo() ||
1561          FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1562                             tok::greater, tok::comma, tok::question)) {
1563     nextToken();
1564     // We can have macros or attributes in between 'enum' and the enum name.
1565     if (FormatTok->is(tok::l_paren))
1566       parseParens();
1567     if (FormatTok->is(tok::identifier)) {
1568       nextToken();
1569       // If there are two identifiers in a row, this is likely an elaborate
1570       // return type. In Java, this can be "implements", etc.
1571       if (Style.Language == FormatStyle::LK_Cpp &&
1572           FormatTok->is(tok::identifier))
1573         return false;
1574     }
1575   }
1576
1577   // Just a declaration or something is wrong.
1578   if (FormatTok->isNot(tok::l_brace))
1579     return true;
1580   FormatTok->BlockKind = BK_Block;
1581
1582   if (Style.Language == FormatStyle::LK_Java) {
1583     // Java enums are different.
1584     parseJavaEnumBody();
1585     return true;
1586   }
1587   if (Style.Language == FormatStyle::LK_Proto) {
1588     parseBlock(/*MustBeDeclaration=*/true);
1589     return true;
1590   }
1591
1592   // Parse enum body.
1593   bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1594   if (HasError) {
1595     if (FormatTok->is(tok::semi))
1596       nextToken();
1597     addUnwrappedLine();
1598   }
1599   return true;
1600
1601   // There is no addUnwrappedLine() here so that we fall through to parsing a
1602   // structural element afterwards. Thus, in "enum A {} n, m;",
1603   // "} n, m;" will end up in one unwrapped line.
1604 }
1605
1606 void UnwrappedLineParser::parseJavaEnumBody() {
1607   // Determine whether the enum is simple, i.e. does not have a semicolon or
1608   // constants with class bodies. Simple enums can be formatted like braced
1609   // lists, contracted to a single line, etc.
1610   unsigned StoredPosition = Tokens->getPosition();
1611   bool IsSimple = true;
1612   FormatToken *Tok = Tokens->getNextToken();
1613   while (Tok) {
1614     if (Tok->is(tok::r_brace))
1615       break;
1616     if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1617       IsSimple = false;
1618       break;
1619     }
1620     // FIXME: This will also mark enums with braces in the arguments to enum
1621     // constants as "not simple". This is probably fine in practice, though.
1622     Tok = Tokens->getNextToken();
1623   }
1624   FormatTok = Tokens->setPosition(StoredPosition);
1625
1626   if (IsSimple) {
1627     parseBracedList();
1628     addUnwrappedLine();
1629     return;
1630   }
1631
1632   // Parse the body of a more complex enum.
1633   // First add a line for everything up to the "{".
1634   nextToken();
1635   addUnwrappedLine();
1636   ++Line->Level;
1637
1638   // Parse the enum constants.
1639   while (FormatTok) {
1640     if (FormatTok->is(tok::l_brace)) {
1641       // Parse the constant's class body.
1642       parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1643                  /*MunchSemi=*/false);
1644     } else if (FormatTok->is(tok::l_paren)) {
1645       parseParens();
1646     } else if (FormatTok->is(tok::comma)) {
1647       nextToken();
1648       addUnwrappedLine();
1649     } else if (FormatTok->is(tok::semi)) {
1650       nextToken();
1651       addUnwrappedLine();
1652       break;
1653     } else if (FormatTok->is(tok::r_brace)) {
1654       addUnwrappedLine();
1655       break;
1656     } else {
1657       nextToken();
1658     }
1659   }
1660
1661   // Parse the class body after the enum's ";" if any.
1662   parseLevel(/*HasOpeningBrace=*/true);
1663   nextToken();
1664   --Line->Level;
1665   addUnwrappedLine();
1666 }
1667
1668 void UnwrappedLineParser::parseRecord() {
1669   const FormatToken &InitialToken = *FormatTok;
1670   nextToken();
1671
1672   // The actual identifier can be a nested name specifier, and in macros
1673   // it is often token-pasted.
1674   while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
1675                             tok::kw___attribute, tok::kw___declspec,
1676                             tok::kw_alignas) ||
1677          ((Style.Language == FormatStyle::LK_Java ||
1678            Style.Language == FormatStyle::LK_JavaScript) &&
1679           FormatTok->isOneOf(tok::period, tok::comma))) {
1680     bool IsNonMacroIdentifier =
1681         FormatTok->is(tok::identifier) &&
1682         FormatTok->TokenText != FormatTok->TokenText.upper();
1683     nextToken();
1684     // We can have macros or attributes in between 'class' and the class name.
1685     if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
1686       parseParens();
1687   }
1688
1689   // Note that parsing away template declarations here leads to incorrectly
1690   // accepting function declarations as record declarations.
1691   // In general, we cannot solve this problem. Consider:
1692   // class A<int> B() {}
1693   // which can be a function definition or a class definition when B() is a
1694   // macro. If we find enough real-world cases where this is a problem, we
1695   // can parse for the 'template' keyword in the beginning of the statement,
1696   // and thus rule out the record production in case there is no template
1697   // (this would still leave us with an ambiguity between template function
1698   // and class declarations).
1699   if (FormatTok->isOneOf(tok::colon, tok::less)) {
1700     while (!eof()) {
1701       if (FormatTok->is(tok::l_brace)) {
1702         calculateBraceTypes(/*ExpectClassBody=*/true);
1703         if (!tryToParseBracedList())
1704           break;
1705       }
1706       if (FormatTok->Tok.is(tok::semi))
1707         return;
1708       nextToken();
1709     }
1710   }
1711   if (FormatTok->Tok.is(tok::l_brace)) {
1712     if (ShouldBreakBeforeBrace(Style, InitialToken))
1713       addUnwrappedLine();
1714
1715     parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1716                /*MunchSemi=*/false);
1717   }
1718   // There is no addUnwrappedLine() here so that we fall through to parsing a
1719   // structural element afterwards. Thus, in "class A {} n, m;",
1720   // "} n, m;" will end up in one unwrapped line.
1721 }
1722
1723 void UnwrappedLineParser::parseObjCProtocolList() {
1724   assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
1725   do
1726     nextToken();
1727   while (!eof() && FormatTok->Tok.isNot(tok::greater));
1728   nextToken(); // Skip '>'.
1729 }
1730
1731 void UnwrappedLineParser::parseObjCUntilAtEnd() {
1732   do {
1733     if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
1734       nextToken();
1735       addUnwrappedLine();
1736       break;
1737     }
1738     if (FormatTok->is(tok::l_brace)) {
1739       parseBlock(/*MustBeDeclaration=*/false);
1740       // In ObjC interfaces, nothing should be following the "}".
1741       addUnwrappedLine();
1742     } else if (FormatTok->is(tok::r_brace)) {
1743       // Ignore stray "}". parseStructuralElement doesn't consume them.
1744       nextToken();
1745       addUnwrappedLine();
1746     } else {
1747       parseStructuralElement();
1748     }
1749   } while (!eof());
1750 }
1751
1752 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
1753   nextToken();
1754   nextToken(); // interface name
1755
1756   // @interface can be followed by either a base class, or a category.
1757   if (FormatTok->Tok.is(tok::colon)) {
1758     nextToken();
1759     nextToken(); // base class name
1760   } else if (FormatTok->Tok.is(tok::l_paren))
1761     // Skip category, if present.
1762     parseParens();
1763
1764   if (FormatTok->Tok.is(tok::less))
1765     parseObjCProtocolList();
1766
1767   if (FormatTok->Tok.is(tok::l_brace)) {
1768     if (Style.BraceWrapping.AfterObjCDeclaration)
1769       addUnwrappedLine();
1770     parseBlock(/*MustBeDeclaration=*/true);
1771   }
1772
1773   // With instance variables, this puts '}' on its own line.  Without instance
1774   // variables, this ends the @interface line.
1775   addUnwrappedLine();
1776
1777   parseObjCUntilAtEnd();
1778 }
1779
1780 void UnwrappedLineParser::parseObjCProtocol() {
1781   nextToken();
1782   nextToken(); // protocol name
1783
1784   if (FormatTok->Tok.is(tok::less))
1785     parseObjCProtocolList();
1786
1787   // Check for protocol declaration.
1788   if (FormatTok->Tok.is(tok::semi)) {
1789     nextToken();
1790     return addUnwrappedLine();
1791   }
1792
1793   addUnwrappedLine();
1794   parseObjCUntilAtEnd();
1795 }
1796
1797 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1798   assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
1799   nextToken();
1800
1801   // Consume the "default" in "export default class/function".
1802   if (FormatTok->is(tok::kw_default))
1803     nextToken();
1804
1805   // Consume "function" and "default function", so that these get parsed as
1806   // free-standing JS functions, i.e. do not require a trailing semicolon.
1807   if (FormatTok->is(Keywords.kw_function)) {
1808     nextToken();
1809     return;
1810   }
1811
1812   // Consume the "abstract" in "export abstract class".
1813   if (FormatTok->is(Keywords.kw_abstract))
1814     nextToken();
1815
1816   if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, tok::kw_enum,
1817                          Keywords.kw_interface, Keywords.kw_let,
1818                          Keywords.kw_var))
1819     return; // Fall through to parsing the corresponding structure.
1820
1821   while (!eof() && FormatTok->isNot(tok::semi)) {
1822     if (FormatTok->is(tok::l_brace)) {
1823       FormatTok->BlockKind = BK_Block;
1824       parseBracedList();
1825     } else {
1826       nextToken();
1827     }
1828   }
1829 }
1830
1831 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1832                                                  StringRef Prefix = "") {
1833   llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1834                << (Line.InPPDirective ? " MACRO" : "") << ": ";
1835   for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1836                                                     E = Line.Tokens.end();
1837        I != E; ++I) {
1838     llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
1839   }
1840   for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1841                                                     E = Line.Tokens.end();
1842        I != E; ++I) {
1843     const UnwrappedLineNode &Node = *I;
1844     for (SmallVectorImpl<UnwrappedLine>::const_iterator
1845              I = Node.Children.begin(),
1846              E = Node.Children.end();
1847          I != E; ++I) {
1848       printDebugInfo(*I, "\nChild: ");
1849     }
1850   }
1851   llvm::dbgs() << "\n";
1852 }
1853
1854 void UnwrappedLineParser::addUnwrappedLine() {
1855   if (Line->Tokens.empty())
1856     return;
1857   DEBUG({
1858     if (CurrentLines == &Lines)
1859       printDebugInfo(*Line);
1860   });
1861   CurrentLines->push_back(std::move(*Line));
1862   Line->Tokens.clear();
1863   if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
1864     CurrentLines->append(
1865         std::make_move_iterator(PreprocessorDirectives.begin()),
1866         std::make_move_iterator(PreprocessorDirectives.end()));
1867     PreprocessorDirectives.clear();
1868   }
1869 }
1870
1871 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
1872
1873 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
1874   return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1875          FormatTok.NewlinesBefore > 0;
1876 }
1877
1878 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1879   bool JustComments = Line->Tokens.empty();
1880   for (SmallVectorImpl<FormatToken *>::const_iterator
1881            I = CommentsBeforeNextToken.begin(),
1882            E = CommentsBeforeNextToken.end();
1883        I != E; ++I) {
1884     if (isOnNewLine(**I) && JustComments)
1885       addUnwrappedLine();
1886     pushToken(*I);
1887   }
1888   if (NewlineBeforeNext && JustComments)
1889     addUnwrappedLine();
1890   CommentsBeforeNextToken.clear();
1891 }
1892
1893 void UnwrappedLineParser::nextToken() {
1894   if (eof())
1895     return;
1896   flushComments(isOnNewLine(*FormatTok));
1897   pushToken(FormatTok);
1898   readToken();
1899 }
1900
1901 const FormatToken *UnwrappedLineParser::getPreviousToken() {
1902   // FIXME: This is a dirty way to access the previous token. Find a better
1903   // solution.
1904   if (!Line || Line->Tokens.empty())
1905     return nullptr;
1906   return Line->Tokens.back().Tok;
1907 }
1908
1909 void UnwrappedLineParser::readToken() {
1910   bool CommentsInCurrentLine = true;
1911   do {
1912     FormatTok = Tokens->getNextToken();
1913     assert(FormatTok);
1914     while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1915            (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
1916       // If there is an unfinished unwrapped line, we flush the preprocessor
1917       // directives only after that unwrapped line was finished later.
1918       bool SwitchToPreprocessorLines = !Line->Tokens.empty();
1919       ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
1920       // Comments stored before the preprocessor directive need to be output
1921       // before the preprocessor directive, at the same level as the
1922       // preprocessor directive, as we consider them to apply to the directive.
1923       flushComments(isOnNewLine(*FormatTok));
1924       parsePPDirective();
1925     }
1926     while (FormatTok->Type == TT_ConflictStart ||
1927            FormatTok->Type == TT_ConflictEnd ||
1928            FormatTok->Type == TT_ConflictAlternative) {
1929       if (FormatTok->Type == TT_ConflictStart) {
1930         conditionalCompilationStart(/*Unreachable=*/false);
1931       } else if (FormatTok->Type == TT_ConflictAlternative) {
1932         conditionalCompilationAlternative();
1933       } else if (FormatTok->Type == TT_ConflictEnd) {
1934         conditionalCompilationEnd();
1935       }
1936       FormatTok = Tokens->getNextToken();
1937       FormatTok->MustBreakBefore = true;
1938     }
1939
1940     if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1941         !Line->InPPDirective) {
1942       continue;
1943     }
1944
1945     if (!FormatTok->Tok.is(tok::comment))
1946       return;
1947     if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
1948       CommentsInCurrentLine = false;
1949     }
1950     if (CommentsInCurrentLine) {
1951       pushToken(FormatTok);
1952     } else {
1953       CommentsBeforeNextToken.push_back(FormatTok);
1954     }
1955   } while (!eof());
1956 }
1957
1958 void UnwrappedLineParser::pushToken(FormatToken *Tok) {
1959   Line->Tokens.push_back(UnwrappedLineNode(Tok));
1960   if (MustBreakBeforeNextToken) {
1961     Line->Tokens.back().Tok->MustBreakBefore = true;
1962     MustBreakBeforeNextToken = false;
1963   }
1964 }
1965
1966 } // end namespace format
1967 } // end namespace clang