1 //===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file contains the implementation of the UnwrappedLineParser,
12 /// which turns a stream of tokens into UnwrappedLines.
14 //===----------------------------------------------------------------------===//
16 #include "UnwrappedLineParser.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
21 #define DEBUG_TYPE "format-parser"
26 class FormatTokenSource {
28 virtual ~FormatTokenSource() {}
29 virtual FormatToken *getNextToken() = 0;
31 virtual unsigned getPosition() = 0;
32 virtual FormatToken *setPosition(unsigned Position) = 0;
37 class ScopedDeclarationState {
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);
45 ~ScopedDeclarationState() {
48 Line.MustBeDeclaration = Stack.back();
50 Line.MustBeDeclaration = true;
55 std::vector<bool> &Stack;
58 static bool isLineComment(const FormatToken &FormatTok) {
59 return FormatTok.is(tok::comment) &&
60 FormatTok.TokenText.startswith("//");
63 // Checks if \p FormatTok is a line comment that continues the line comment
64 // \p Previous. The original column of \p MinColumnToken is used to determine
65 // whether \p FormatTok is indented enough to the right to continue \p Previous.
66 static bool continuesLineComment(const FormatToken &FormatTok,
67 const FormatToken *Previous,
68 const FormatToken *MinColumnToken) {
69 if (!Previous || !MinColumnToken)
71 unsigned MinContinueColumn =
72 MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
73 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
74 isLineComment(*Previous) &&
75 FormatTok.OriginalColumn >= MinContinueColumn;
78 class ScopedMacroState : public FormatTokenSource {
80 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
81 FormatToken *&ResetToken)
82 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
83 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
84 Token(nullptr), PreviousToken(nullptr) {
87 Line.InPPDirective = true;
90 ~ScopedMacroState() override {
91 TokenSource = PreviousTokenSource;
93 Line.InPPDirective = false;
94 Line.Level = PreviousLineLevel;
97 FormatToken *getNextToken() override {
98 // The \c UnwrappedLineParser guards against this by never calling
99 // \c getNextToken() after it has encountered the first eof token.
101 PreviousToken = Token;
102 Token = PreviousTokenSource->getNextToken();
108 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
110 FormatToken *setPosition(unsigned Position) override {
111 PreviousToken = nullptr;
112 Token = PreviousTokenSource->setPosition(Position);
118 return Token && Token->HasUnescapedNewline &&
119 !continuesLineComment(*Token, PreviousToken,
120 /*MinColumnToken=*/PreviousToken);
123 FormatToken *getFakeEOF() {
124 static bool EOFInitialized = false;
125 static FormatToken FormatTok;
126 if (!EOFInitialized) {
127 FormatTok.Tok.startToken();
128 FormatTok.Tok.setKind(tok::eof);
129 EOFInitialized = true;
135 FormatTokenSource *&TokenSource;
136 FormatToken *&ResetToken;
137 unsigned PreviousLineLevel;
138 FormatTokenSource *PreviousTokenSource;
141 FormatToken *PreviousToken;
144 } // end anonymous namespace
146 class ScopedLineState {
148 ScopedLineState(UnwrappedLineParser &Parser,
149 bool SwitchToPreprocessorLines = false)
150 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
151 if (SwitchToPreprocessorLines)
152 Parser.CurrentLines = &Parser.PreprocessorDirectives;
153 else if (!Parser.Line->Tokens.empty())
154 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
155 PreBlockLine = std::move(Parser.Line);
156 Parser.Line = llvm::make_unique<UnwrappedLine>();
157 Parser.Line->Level = PreBlockLine->Level;
158 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
162 if (!Parser.Line->Tokens.empty()) {
163 Parser.addUnwrappedLine();
165 assert(Parser.Line->Tokens.empty());
166 Parser.Line = std::move(PreBlockLine);
167 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
168 Parser.MustBreakBeforeNextToken = true;
169 Parser.CurrentLines = OriginalLines;
173 UnwrappedLineParser &Parser;
175 std::unique_ptr<UnwrappedLine> PreBlockLine;
176 SmallVectorImpl<UnwrappedLine> *OriginalLines;
179 class CompoundStatementIndenter {
181 CompoundStatementIndenter(UnwrappedLineParser *Parser,
182 const FormatStyle &Style, unsigned &LineLevel)
183 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
184 if (Style.BraceWrapping.AfterControlStatement)
185 Parser->addUnwrappedLine();
186 if (Style.BraceWrapping.IndentBraces)
189 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
193 unsigned OldLineLevel;
198 class IndexedTokenSource : public FormatTokenSource {
200 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
201 : Tokens(Tokens), Position(-1) {}
203 FormatToken *getNextToken() override {
205 return Tokens[Position];
208 unsigned getPosition() override {
209 assert(Position >= 0);
213 FormatToken *setPosition(unsigned P) override {
215 return Tokens[Position];
218 void reset() { Position = -1; }
221 ArrayRef<FormatToken *> Tokens;
225 } // end anonymous namespace
227 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
228 const AdditionalKeywords &Keywords,
229 ArrayRef<FormatToken *> Tokens,
230 UnwrappedLineConsumer &Callback)
231 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
232 CurrentLines(&Lines), Style(Style), Keywords(Keywords),
233 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
234 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
236 void UnwrappedLineParser::reset() {
238 Line.reset(new UnwrappedLine);
239 CommentsBeforeNextToken.clear();
241 MustBreakBeforeNextToken = false;
242 PreprocessorDirectives.clear();
243 CurrentLines = &Lines;
244 DeclarationScopeStack.clear();
248 void UnwrappedLineParser::parse() {
249 IndexedTokenSource TokenSource(AllTokens);
251 DEBUG(llvm::dbgs() << "----\n");
253 Tokens = &TokenSource;
258 // Create line with eof token.
259 pushToken(FormatTok);
262 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
265 Callback.consumeUnwrappedLine(*I);
267 Callback.finishRun();
269 while (!PPLevelBranchIndex.empty() &&
270 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
271 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
272 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
274 if (!PPLevelBranchIndex.empty()) {
275 ++PPLevelBranchIndex.back();
276 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
277 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
279 } while (!PPLevelBranchIndex.empty());
282 void UnwrappedLineParser::parseFile() {
283 // The top-level context in a file always has declarations, except for pre-
284 // processor directives and JavaScript files.
285 bool MustBeDeclaration =
286 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
287 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
289 if (Style.Language == FormatStyle::LK_TextProto)
292 parseLevel(/*HasOpeningBrace=*/false);
293 // Make sure to format the remaining tokens.
298 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
299 bool SwitchLabelEncountered = false;
301 tok::TokenKind kind = FormatTok->Tok.getKind();
302 if (FormatTok->Type == TT_MacroBlockBegin) {
304 } else if (FormatTok->Type == TT_MacroBlockEnd) {
314 // FIXME: Add parameter whether this can happen - if this happens, we must
315 // be in a non-declaration context.
316 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
318 parseBlock(/*MustBeDeclaration=*/false);
327 case tok::kw_default:
329 if (!SwitchLabelEncountered &&
330 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
332 SwitchLabelEncountered = true;
333 parseStructuralElement();
336 parseStructuralElement();
342 void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
343 // We'll parse forward through the tokens until we hit
344 // a closing brace or eof - note that getNextToken() will
345 // parse macros, so this will magically work inside macro
347 unsigned StoredPosition = Tokens->getPosition();
348 FormatToken *Tok = FormatTok;
349 const FormatToken *PrevTok = getPreviousToken();
350 // Keep a stack of positions of lbrace tokens. We will
351 // update information about whether an lbrace starts a
352 // braced init list or a different block during the loop.
353 SmallVector<FormatToken *, 8> LBraceStack;
354 assert(Tok->Tok.is(tok::l_brace));
356 // Get next non-comment token.
357 FormatToken *NextTok;
358 unsigned ReadTokens = 0;
360 NextTok = Tokens->getNextToken();
362 } while (NextTok->is(tok::comment));
364 switch (Tok->Tok.getKind()) {
366 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) {
367 if (PrevTok->is(tok::colon))
368 // A colon indicates this code is in a type, or a braced list
369 // following a label in an object literal ({a: {b: 1}}). The code
370 // below could be confused by semicolons between the individual
371 // members in a type member list, which would normally trigger
372 // BK_Block. In both cases, this must be parsed as an inline braced
374 Tok->BlockKind = BK_BracedInit;
375 else if (PrevTok->is(tok::r_paren))
376 // `) { }` can only occur in function or method declarations in JS.
377 Tok->BlockKind = BK_Block;
379 Tok->BlockKind = BK_Unknown;
381 LBraceStack.push_back(Tok);
384 if (LBraceStack.empty())
386 if (LBraceStack.back()->BlockKind == BK_Unknown) {
387 bool ProbablyBracedList = false;
388 if (Style.Language == FormatStyle::LK_Proto) {
389 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
391 // Using OriginalColumn to distinguish between ObjC methods and
392 // binary operators is a bit hacky.
393 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
394 NextTok->OriginalColumn == 0;
396 // If there is a comma, semicolon or right paren after the closing
397 // brace, we assume this is a braced initializer list. Note that
398 // regardless how we mark inner braces here, we will overwrite the
399 // BlockKind later if we parse a braced list (where all blocks
400 // inside are by default braced lists), or when we explicitly detect
401 // blocks (for example while parsing lambdas).
402 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
403 // braced list in JS.
405 (Style.Language == FormatStyle::LK_JavaScript &&
406 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
408 (Style.isCpp() && NextTok->is(tok::l_paren)) ||
409 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
410 tok::r_paren, tok::r_square, tok::l_brace,
411 tok::l_square, tok::ellipsis) ||
412 (NextTok->is(tok::identifier) &&
413 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
414 (NextTok->is(tok::semi) &&
415 (!ExpectClassBody || LBraceStack.size() != 1)) ||
416 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
418 if (ProbablyBracedList) {
419 Tok->BlockKind = BK_BracedInit;
420 LBraceStack.back()->BlockKind = BK_BracedInit;
422 Tok->BlockKind = BK_Block;
423 LBraceStack.back()->BlockKind = BK_Block;
426 LBraceStack.pop_back();
436 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
437 LBraceStack.back()->BlockKind = BK_Block;
444 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
446 // Assume other blocks for all unclosed opening braces.
447 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
448 if (LBraceStack[i]->BlockKind == BK_Unknown)
449 LBraceStack[i]->BlockKind = BK_Block;
452 FormatTok = Tokens->setPosition(StoredPosition);
455 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
457 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
458 "'{' or macro block token expected");
459 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
460 FormatTok->BlockKind = BK_Block;
462 unsigned InitialLevel = Line->Level;
465 if (MacroBlock && FormatTok->is(tok::l_paren))
469 size_t OpeningLineIndex = CurrentLines->empty()
470 ? (UnwrappedLine::kInvalidIndex)
471 : (CurrentLines->size() - 1);
473 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
477 parseLevel(/*HasOpeningBrace=*/true);
482 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
483 : !FormatTok->is(tok::r_brace)) {
484 Line->Level = InitialLevel;
485 FormatTok->BlockKind = BK_Block;
489 nextToken(); // Munch the closing brace.
491 if (MacroBlock && FormatTok->is(tok::l_paren))
494 if (MunchSemi && FormatTok->Tok.is(tok::semi))
496 Line->Level = InitialLevel;
497 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
498 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
499 // Update the opening line to add the forward reference as well
500 (*CurrentLines)[OpeningLineIndex].MatchingOpeningBlockLineIndex =
501 CurrentLines->size() - 1;
505 static bool isGoogScope(const UnwrappedLine &Line) {
506 // FIXME: Closure-library specific stuff should not be hard-coded but be
508 if (Line.Tokens.size() < 4)
510 auto I = Line.Tokens.begin();
511 if (I->Tok->TokenText != "goog")
514 if (I->Tok->isNot(tok::period))
517 if (I->Tok->TokenText != "scope")
520 return I->Tok->is(tok::l_paren);
523 static bool isIIFE(const UnwrappedLine &Line,
524 const AdditionalKeywords &Keywords) {
525 // Look for the start of an immediately invoked anonymous function.
526 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
527 // This is commonly done in JavaScript to create a new, anonymous scope.
528 // Example: (function() { ... })()
529 if (Line.Tokens.size() < 3)
531 auto I = Line.Tokens.begin();
532 if (I->Tok->isNot(tok::l_paren))
535 if (I->Tok->isNot(Keywords.kw_function))
538 return I->Tok->is(tok::l_paren);
541 static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
542 const FormatToken &InitialToken) {
543 if (InitialToken.is(tok::kw_namespace))
544 return Style.BraceWrapping.AfterNamespace;
545 if (InitialToken.is(tok::kw_class))
546 return Style.BraceWrapping.AfterClass;
547 if (InitialToken.is(tok::kw_union))
548 return Style.BraceWrapping.AfterUnion;
549 if (InitialToken.is(tok::kw_struct))
550 return Style.BraceWrapping.AfterStruct;
554 void UnwrappedLineParser::parseChildBlock() {
555 FormatTok->BlockKind = BK_Block;
559 (Style.Language == FormatStyle::LK_JavaScript &&
560 (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
561 ScopedLineState LineState(*this);
562 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
563 /*MustBeDeclaration=*/false);
564 Line->Level += SkipIndent ? 0 : 1;
565 parseLevel(/*HasOpeningBrace=*/true);
566 flushComments(isOnNewLine(*FormatTok));
567 Line->Level -= SkipIndent ? 0 : 1;
572 void UnwrappedLineParser::parsePPDirective() {
573 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
574 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
577 if (!FormatTok->Tok.getIdentifierInfo()) {
582 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
587 parsePPIf(/*IfDef=*/false);
591 parsePPIf(/*IfDef=*/true);
608 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
609 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
610 PPStack.push_back(PP_Unreachable);
612 PPStack.push_back(PP_Conditional);
615 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
617 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
618 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
619 PPLevelBranchIndex.push_back(0);
620 PPLevelBranchCount.push_back(0);
622 PPChainBranchIndex.push(0);
623 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
624 conditionalCompilationCondition(Unreachable || Skip);
627 void UnwrappedLineParser::conditionalCompilationAlternative() {
628 if (!PPStack.empty())
630 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
631 if (!PPChainBranchIndex.empty())
632 ++PPChainBranchIndex.top();
633 conditionalCompilationCondition(
634 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
635 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
638 void UnwrappedLineParser::conditionalCompilationEnd() {
639 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
640 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
641 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
642 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
645 // Guard against #endif's without #if.
646 if (PPBranchLevel > 0)
648 if (!PPChainBranchIndex.empty())
649 PPChainBranchIndex.pop();
650 if (!PPStack.empty())
654 void UnwrappedLineParser::parsePPIf(bool IfDef) {
655 bool IfNDef = FormatTok->is(tok::pp_ifndef);
657 bool Unreachable = false;
658 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
660 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
662 conditionalCompilationStart(Unreachable);
666 void UnwrappedLineParser::parsePPElse() {
667 conditionalCompilationAlternative();
671 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
673 void UnwrappedLineParser::parsePPEndIf() {
674 conditionalCompilationEnd();
678 void UnwrappedLineParser::parsePPDefine() {
681 if (FormatTok->Tok.getKind() != tok::identifier) {
686 if (FormatTok->Tok.getKind() == tok::l_paren &&
687 FormatTok->WhitespaceRange.getBegin() ==
688 FormatTok->WhitespaceRange.getEnd()) {
694 // Errors during a preprocessor directive can only affect the layout of the
695 // preprocessor directive, and thus we ignore them. An alternative approach
696 // would be to use the same approach we use on the file level (no
697 // re-indentation if there was a structural error) within the macro
702 void UnwrappedLineParser::parsePPUnknown() {
709 // Here we blacklist certain tokens that are not usually the first token in an
710 // unwrapped line. This is used in attempt to distinguish macro calls without
711 // trailing semicolons from other constructs split to several lines.
712 static bool tokenCanStartNewLine(const clang::Token &Tok) {
713 // Semicolon can be a null-statement, l_square can be a start of a macro or
714 // a C++11 attribute, but this doesn't seem to be common.
715 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
716 Tok.isNot(tok::l_square) &&
717 // Tokens that can only be used as binary operators and a part of
718 // overloaded operator names.
719 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
720 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
721 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
722 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
723 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
724 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
725 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
726 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
727 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
728 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
729 Tok.isNot(tok::lesslessequal) &&
730 // Colon is used in labels, base class lists, initializer lists,
731 // range-based for loops, ternary operator, but should never be the
732 // first token in an unwrapped line.
733 Tok.isNot(tok::colon) &&
734 // 'noexcept' is a trailing annotation.
735 Tok.isNot(tok::kw_noexcept);
738 static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
739 const FormatToken *FormatTok) {
740 // FIXME: This returns true for C/C++ keywords like 'struct'.
741 return FormatTok->is(tok::identifier) &&
742 (FormatTok->Tok.getIdentifierInfo() == nullptr ||
744 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
745 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
746 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
747 Keywords.kw_let, Keywords.kw_var, tok::kw_const,
748 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
749 Keywords.kw_instanceof, Keywords.kw_interface,
750 Keywords.kw_throws, Keywords.kw_from));
753 static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
754 const FormatToken *FormatTok) {
755 return FormatTok->Tok.isLiteral() ||
756 FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
757 mustBeJSIdent(Keywords, FormatTok);
760 // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
761 // when encountered after a value (see mustBeJSIdentOrValue).
762 static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
763 const FormatToken *FormatTok) {
764 return FormatTok->isOneOf(
765 tok::kw_return, Keywords.kw_yield,
767 tok::kw_if, tok::kw_else,
769 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
771 tok::kw_switch, tok::kw_case,
773 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
775 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
776 Keywords.kw_async, Keywords.kw_function,
778 Keywords.kw_import, tok::kw_export);
781 // readTokenWithJavaScriptASI reads the next token and terminates the current
782 // line if JavaScript Automatic Semicolon Insertion must
783 // happen between the current token and the next token.
785 // This method is conservative - it cannot cover all edge cases of JavaScript,
786 // but only aims to correctly handle certain well known cases. It *must not*
787 // return true in speculative cases.
788 void UnwrappedLineParser::readTokenWithJavaScriptASI() {
789 FormatToken *Previous = FormatTok;
791 FormatToken *Next = FormatTok;
794 CommentsBeforeNextToken.empty()
795 ? Next->NewlinesBefore == 0
796 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
800 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
801 bool PreviousStartsTemplateExpr =
802 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${");
803 if (PreviousMustBeValue && Line && Line->Tokens.size() > 1) {
804 // If the token before the previous one is an '@', the previous token is an
805 // annotation and can precede another identifier/value.
806 const FormatToken *PrePrevious = std::prev(Line->Tokens.end(), 2)->Tok;
807 if (PrePrevious->is(tok::at))
810 if (Next->is(tok::exclaim) && PreviousMustBeValue)
811 return addUnwrappedLine();
812 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
813 bool NextEndsTemplateExpr =
814 Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
815 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
816 (PreviousMustBeValue ||
817 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
819 return addUnwrappedLine();
820 if (PreviousMustBeValue && isJSDeclOrStmt(Keywords, Next))
821 return addUnwrappedLine();
824 void UnwrappedLineParser::parseStructuralElement() {
825 assert(!FormatTok->is(tok::l_brace));
826 if (Style.Language == FormatStyle::LK_TableGen &&
827 FormatTok->is(tok::pp_include)) {
829 if (FormatTok->is(tok::string_literal))
834 switch (FormatTok->Tok.getKind()) {
837 if (FormatTok->Tok.is(tok::l_brace)) {
842 switch (FormatTok->Tok.getObjCKeywordID()) {
843 case tok::objc_public:
844 case tok::objc_protected:
845 case tok::objc_package:
846 case tok::objc_private:
847 return parseAccessSpecifier();
848 case tok::objc_interface:
849 case tok::objc_implementation:
850 return parseObjCInterfaceOrImplementation();
851 case tok::objc_protocol:
852 return parseObjCProtocol();
854 return; // Handled by the caller.
855 case tok::objc_optional:
856 case tok::objc_required:
860 case tok::objc_autoreleasepool:
862 if (FormatTok->Tok.is(tok::l_brace)) {
863 if (Style.BraceWrapping.AfterObjCDeclaration)
865 parseBlock(/*MustBeDeclaration=*/false);
870 // This branch isn't strictly necessary (the kw_try case below would
871 // do this too after the tok::at is parsed above). But be explicit.
880 if (FormatTok->is(tok::l_brace)) {
881 FormatTok->Type = TT_InlineASMBrace;
883 while (FormatTok && FormatTok->isNot(tok::eof)) {
884 if (FormatTok->is(tok::r_brace)) {
885 FormatTok->Type = TT_InlineASMBrace;
890 FormatTok->Finalized = true;
895 case tok::kw_namespace:
900 if (FormatTok->Tok.is(tok::kw_namespace)) {
906 case tok::kw_protected:
907 case tok::kw_private:
908 if (Style.Language == FormatStyle::LK_Java ||
909 Style.Language == FormatStyle::LK_JavaScript)
912 parseAccessSpecifier();
919 parseForOrWhileLoop();
927 case tok::kw_default:
940 if (FormatTok->Tok.is(tok::string_literal)) {
942 if (FormatTok->Tok.is(tok::l_brace)) {
943 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
950 if (Style.Language == FormatStyle::LK_JavaScript) {
951 parseJavaScriptEs6ImportExport();
955 case tok::identifier:
956 if (FormatTok->is(TT_ForEachMacro)) {
957 parseForOrWhileLoop();
960 if (FormatTok->is(TT_MacroBlockBegin)) {
961 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
962 /*MunchSemi=*/false);
965 if (FormatTok->is(Keywords.kw_import)) {
966 if (Style.Language == FormatStyle::LK_JavaScript) {
967 parseJavaScriptEs6ImportExport();
970 if (Style.Language == FormatStyle::LK_Proto) {
972 if (FormatTok->is(tok::kw_public))
974 if (!FormatTok->is(tok::string_literal))
977 if (FormatTok->is(tok::semi))
984 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
985 Keywords.kw_slots, Keywords.kw_qslots)) {
987 if (FormatTok->is(tok::colon)) {
993 // In all other cases, parse the declaration.
999 const FormatToken *Previous = getPreviousToken();
1000 switch (FormatTok->Tok.getKind()) {
1003 if (FormatTok->Tok.is(tok::l_brace)) {
1009 // Ignore if this is part of "template <enum ...".
1010 if (Previous && Previous->is(tok::less)) {
1015 // parseEnum falls through and does not yet add an unwrapped line as an
1016 // enum definition can start a structural element.
1019 // This only applies for C++.
1020 if (!Style.isCpp()) {
1025 case tok::kw_typedef:
1027 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1028 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
1031 case tok::kw_struct:
1034 // parseRecord falls through and does not yet add an unwrapped line as a
1035 // record declaration or definition can start a structural element.
1037 // This does not apply for Java and JavaScript.
1038 if (Style.Language == FormatStyle::LK_Java ||
1039 Style.Language == FormatStyle::LK_JavaScript) {
1040 if (FormatTok->is(tok::semi))
1048 // In Java, classes have an implicit static member "class".
1049 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
1050 FormatTok->is(tok::kw_class))
1052 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
1053 FormatTok->Tok.getIdentifierInfo())
1054 // JavaScript only has pseudo keywords, all keywords are allowed to
1055 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1068 case tok::kw_operator:
1070 if (FormatTok->isBinaryOperator())
1075 if (FormatTok->Tok.isAnyIdentifier() ||
1076 FormatTok->isSimpleTypeSpecifier())
1078 if (FormatTok->is(tok::l_paren))
1080 if (FormatTok->is(tok::l_brace))
1084 if (!tryToParseBracedList()) {
1085 // A block outside of parentheses must be the last part of a
1086 // structural element.
1087 // FIXME: Figure out cases where this is not true, and add projections
1088 // for them (the one we know is missing are lambdas).
1089 if (Style.BraceWrapping.AfterFunction)
1091 FormatTok->Type = TT_FunctionLBrace;
1092 parseBlock(/*MustBeDeclaration=*/false);
1096 // Otherwise this was a braced init list, and the structural
1097 // element continues.
1100 // We arrive here when parsing function-try blocks.
1103 case tok::identifier: {
1104 if (FormatTok->is(TT_MacroBlockEnd)) {
1109 // Function declarations (as opposed to function expressions) are parsed
1110 // on their own unwrapped line by continuing this loop. Function
1111 // expressions (functions that are not on their own line) must not create
1112 // a new unwrapped line, so they are special cased below.
1113 size_t TokenCount = Line->Tokens.size();
1114 if (Style.Language == FormatStyle::LK_JavaScript &&
1115 FormatTok->is(Keywords.kw_function) &&
1116 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1117 Keywords.kw_async)))) {
1118 tryToParseJSFunction();
1121 if ((Style.Language == FormatStyle::LK_JavaScript ||
1122 Style.Language == FormatStyle::LK_Java) &&
1123 FormatTok->is(Keywords.kw_interface)) {
1124 if (Style.Language == FormatStyle::LK_JavaScript) {
1125 // In JavaScript/TypeScript, "interface" can be used as a standalone
1126 // identifier, e.g. in `var interface = 1;`. If "interface" is
1127 // followed by another identifier, it is very like to be an actual
1128 // interface declaration.
1129 unsigned StoredPosition = Tokens->getPosition();
1130 FormatToken *Next = Tokens->getNextToken();
1131 FormatTok = Tokens->setPosition(StoredPosition);
1132 if (Next && !mustBeJSIdent(Keywords, Next)) {
1142 // See if the following token should start a new unwrapped line.
1143 StringRef Text = FormatTok->TokenText;
1145 if (Line->Tokens.size() == 1 &&
1146 // JS doesn't have macros, and within classes colons indicate fields,
1148 Style.Language != FormatStyle::LK_JavaScript) {
1149 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
1150 Line->Tokens.begin()->Tok->MustBreakBefore = true;
1154 // Recognize function-like macro usages without trailing semicolon as
1155 // well as free-standing macros like Q_OBJECT.
1156 bool FunctionLike = FormatTok->is(tok::l_paren);
1160 bool FollowedByNewline =
1161 CommentsBeforeNextToken.empty()
1162 ? FormatTok->NewlinesBefore > 0
1163 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1165 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
1166 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
1174 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1175 // TT_JsFatArrow. The always start an expression or a child block if
1176 // followed by a curly.
1177 if (FormatTok->is(TT_JsFatArrow)) {
1179 if (FormatTok->is(tok::l_brace))
1185 if (FormatTok->Tok.is(tok::l_brace)) {
1188 } else if (Style.Language == FormatStyle::LK_Proto &&
1189 FormatTok->Tok.is(tok::less)) {
1191 parseBracedList(/*ContinueOnSemicolons=*/false,
1192 /*ClosingBraceKind=*/tok::greater);
1208 bool UnwrappedLineParser::tryToParseLambda() {
1209 if (!Style.isCpp()) {
1213 const FormatToken* Previous = getPreviousToken();
1215 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1217 Previous->closesScope() || Previous->isSimpleTypeSpecifier())) {
1221 assert(FormatTok->is(tok::l_square));
1222 FormatToken &LSquare = *FormatTok;
1223 if (!tryToParseLambdaIntroducer())
1226 while (FormatTok->isNot(tok::l_brace)) {
1227 if (FormatTok->isSimpleTypeSpecifier()) {
1231 switch (FormatTok->Tok.getKind()) {
1243 case tok::identifier:
1244 case tok::numeric_constant:
1245 case tok::coloncolon:
1246 case tok::kw_mutable:
1250 FormatTok->Type = TT_LambdaArrow;
1257 LSquare.Type = TT_LambdaLSquare;
1262 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1264 if (FormatTok->is(tok::equal)) {
1266 if (FormatTok->is(tok::r_square)) {
1270 if (FormatTok->isNot(tok::comma))
1273 } else if (FormatTok->is(tok::amp)) {
1275 if (FormatTok->is(tok::r_square)) {
1279 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
1282 if (FormatTok->is(tok::comma))
1284 } else if (FormatTok->is(tok::r_square)) {
1289 if (FormatTok->is(tok::amp))
1291 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
1294 if (FormatTok->is(tok::ellipsis))
1296 if (FormatTok->is(tok::comma)) {
1298 } else if (FormatTok->is(tok::r_square)) {
1308 void UnwrappedLineParser::tryToParseJSFunction() {
1309 assert(FormatTok->is(Keywords.kw_function) ||
1310 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
1311 if (FormatTok->is(Keywords.kw_async))
1313 // Consume "function".
1316 // Consume * (generator function). Treat it like C++'s overloaded operators.
1317 if (FormatTok->is(tok::star)) {
1318 FormatTok->Type = TT_OverloadedOperator;
1322 // Consume function name.
1323 if (FormatTok->is(tok::identifier))
1326 if (FormatTok->isNot(tok::l_paren))
1329 // Parse formal parameter list.
1332 if (FormatTok->is(tok::colon)) {
1333 // Parse a type definition.
1336 // Eat the type declaration. For braced inline object types, balance braces,
1337 // otherwise just parse until finding an l_brace for the function body.
1338 if (FormatTok->is(tok::l_brace))
1339 tryToParseBracedList();
1341 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
1345 if (FormatTok->is(tok::semi))
1351 bool UnwrappedLineParser::tryToParseBracedList() {
1352 if (FormatTok->BlockKind == BK_Unknown)
1353 calculateBraceTypes();
1354 assert(FormatTok->BlockKind != BK_Unknown);
1355 if (FormatTok->BlockKind == BK_Block)
1362 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1363 tok::TokenKind ClosingBraceKind) {
1364 bool HasError = false;
1366 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1367 // replace this by using parseAssigmentExpression() inside.
1369 if (Style.Language == FormatStyle::LK_JavaScript) {
1370 if (FormatTok->is(Keywords.kw_function) ||
1371 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
1372 tryToParseJSFunction();
1375 if (FormatTok->is(TT_JsFatArrow)) {
1377 // Fat arrows can be followed by simple expressions or by child blocks
1379 if (FormatTok->is(tok::l_brace)) {
1384 if (FormatTok->is(tok::l_brace)) {
1385 // Could be a method inside of a braced list `{a() { return 1; }}`.
1386 if (tryToParseBracedList())
1391 if (FormatTok->Tok.getKind() == ClosingBraceKind) {
1395 switch (FormatTok->Tok.getKind()) {
1398 if (FormatTok->is(tok::l_brace)) {
1407 // JavaScript can just have free standing methods and getters/setters in
1408 // object literals. Detect them by a "{" following ")".
1409 if (Style.Language == FormatStyle::LK_JavaScript) {
1410 if (FormatTok->is(tok::l_brace))
1416 // Assume there are no blocks inside a braced init list apart
1417 // from the ones we explicitly parse out (like lambdas).
1418 FormatTok->BlockKind = BK_BracedInit;
1423 // JavaScript (or more precisely TypeScript) can have semicolons in braced
1424 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1425 // used for error recovery if we have otherwise determined that this is
1427 if (Style.Language == FormatStyle::LK_JavaScript) {
1432 if (!ContinueOnSemicolons)
1447 void UnwrappedLineParser::parseParens() {
1448 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
1451 switch (FormatTok->Tok.getKind()) {
1454 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1461 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1467 if (!tryToParseBracedList())
1472 if (FormatTok->Tok.is(tok::l_brace)) {
1478 if (Style.Language == FormatStyle::LK_JavaScript)
1479 parseRecord(/*ParseAsExpr=*/true);
1483 case tok::identifier:
1484 if (Style.Language == FormatStyle::LK_JavaScript &&
1485 (FormatTok->is(Keywords.kw_function) ||
1486 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
1487 tryToParseJSFunction();
1498 void UnwrappedLineParser::parseSquare() {
1499 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1500 if (tryToParseLambda())
1503 switch (FormatTok->Tok.getKind()) {
1511 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1516 case tok::l_brace: {
1517 if (!tryToParseBracedList())
1523 if (FormatTok->Tok.is(tok::l_brace)) {
1535 void UnwrappedLineParser::parseIfThenElse() {
1536 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
1538 if (FormatTok->Tok.is(tok::kw_constexpr))
1540 if (FormatTok->Tok.is(tok::l_paren))
1542 bool NeedsUnwrappedLine = false;
1543 if (FormatTok->Tok.is(tok::l_brace)) {
1544 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1545 parseBlock(/*MustBeDeclaration=*/false);
1546 if (Style.BraceWrapping.BeforeElse)
1549 NeedsUnwrappedLine = true;
1553 parseStructuralElement();
1556 if (FormatTok->Tok.is(tok::kw_else)) {
1558 if (FormatTok->Tok.is(tok::l_brace)) {
1559 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1560 parseBlock(/*MustBeDeclaration=*/false);
1562 } else if (FormatTok->Tok.is(tok::kw_if)) {
1567 parseStructuralElement();
1568 if (FormatTok->is(tok::eof))
1572 } else if (NeedsUnwrappedLine) {
1577 void UnwrappedLineParser::parseTryCatch() {
1578 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
1580 bool NeedsUnwrappedLine = false;
1581 if (FormatTok->is(tok::colon)) {
1582 // We are in a function try block, what comes is an initializer list.
1584 while (FormatTok->is(tok::identifier)) {
1586 if (FormatTok->is(tok::l_paren))
1588 if (FormatTok->is(tok::comma))
1592 // Parse try with resource.
1593 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1596 if (FormatTok->is(tok::l_brace)) {
1597 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1598 parseBlock(/*MustBeDeclaration=*/false);
1599 if (Style.BraceWrapping.BeforeCatch) {
1602 NeedsUnwrappedLine = true;
1604 } else if (!FormatTok->is(tok::kw_catch)) {
1605 // The C++ standard requires a compound-statement after a try.
1606 // If there's none, we try to assume there's a structuralElement
1607 // and try to continue.
1610 parseStructuralElement();
1614 if (FormatTok->is(tok::at))
1616 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1617 tok::kw___finally) ||
1618 ((Style.Language == FormatStyle::LK_Java ||
1619 Style.Language == FormatStyle::LK_JavaScript) &&
1620 FormatTok->is(Keywords.kw_finally)) ||
1621 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1622 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1625 while (FormatTok->isNot(tok::l_brace)) {
1626 if (FormatTok->is(tok::l_paren)) {
1630 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
1634 NeedsUnwrappedLine = false;
1635 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1636 parseBlock(/*MustBeDeclaration=*/false);
1637 if (Style.BraceWrapping.BeforeCatch)
1640 NeedsUnwrappedLine = true;
1642 if (NeedsUnwrappedLine)
1646 void UnwrappedLineParser::parseNamespace() {
1647 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
1649 const FormatToken &InitialToken = *FormatTok;
1651 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
1653 if (FormatTok->Tok.is(tok::l_brace)) {
1654 if (ShouldBreakBeforeBrace(Style, InitialToken))
1657 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1658 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1659 DeclarationScopeStack.size() > 1);
1660 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
1661 // Munch the semicolon after a namespace. This is more common than one would
1662 // think. Puttin the semicolon into its own line is very ugly.
1663 if (FormatTok->Tok.is(tok::semi))
1667 // FIXME: Add error handling.
1670 void UnwrappedLineParser::parseNew() {
1671 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1673 if (Style.Language != FormatStyle::LK_Java)
1676 // In Java, we can parse everything up to the parens, which aren't optional.
1678 // There should not be a ;, { or } before the new's open paren.
1679 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1682 // Consume the parens.
1683 if (FormatTok->is(tok::l_paren)) {
1686 // If there is a class body of an anonymous class, consume that as child.
1687 if (FormatTok->is(tok::l_brace))
1695 void UnwrappedLineParser::parseForOrWhileLoop() {
1696 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
1697 "'for', 'while' or foreach macro expected");
1699 // JS' for await ( ...
1700 if (Style.Language == FormatStyle::LK_JavaScript &&
1701 FormatTok->is(Keywords.kw_await))
1703 if (FormatTok->Tok.is(tok::l_paren))
1705 if (FormatTok->Tok.is(tok::l_brace)) {
1706 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1707 parseBlock(/*MustBeDeclaration=*/false);
1712 parseStructuralElement();
1717 void UnwrappedLineParser::parseDoWhile() {
1718 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
1720 if (FormatTok->Tok.is(tok::l_brace)) {
1721 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1722 parseBlock(/*MustBeDeclaration=*/false);
1723 if (Style.BraceWrapping.IndentBraces)
1728 parseStructuralElement();
1732 // FIXME: Add error handling.
1733 if (!FormatTok->Tok.is(tok::kw_while)) {
1739 parseStructuralElement();
1742 void UnwrappedLineParser::parseLabel() {
1744 unsigned OldLineLevel = Line->Level;
1745 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1747 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
1748 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1749 parseBlock(/*MustBeDeclaration=*/false);
1750 if (FormatTok->Tok.is(tok::kw_break)) {
1751 if (Style.BraceWrapping.AfterControlStatement)
1753 parseStructuralElement();
1757 if (FormatTok->is(tok::semi))
1761 Line->Level = OldLineLevel;
1762 if (FormatTok->isNot(tok::l_brace)) {
1763 parseStructuralElement();
1768 void UnwrappedLineParser::parseCaseLabel() {
1769 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
1770 // FIXME: fix handling of complex expressions here.
1773 } while (!eof() && !FormatTok->Tok.is(tok::colon));
1777 void UnwrappedLineParser::parseSwitch() {
1778 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
1780 if (FormatTok->Tok.is(tok::l_paren))
1782 if (FormatTok->Tok.is(tok::l_brace)) {
1783 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1784 parseBlock(/*MustBeDeclaration=*/false);
1789 parseStructuralElement();
1794 void UnwrappedLineParser::parseAccessSpecifier() {
1796 // Understand Qt's slots.
1797 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
1799 // Otherwise, we don't know what it is, and we'd better keep the next token.
1800 if (FormatTok->Tok.is(tok::colon))
1805 bool UnwrappedLineParser::parseEnum() {
1806 // Won't be 'enum' for NS_ENUMs.
1807 if (FormatTok->Tok.is(tok::kw_enum))
1810 // In TypeScript, "enum" can also be used as property name, e.g. in interface
1811 // declarations. An "enum" keyword followed by a colon would be a syntax
1812 // error and thus assume it is just an identifier.
1813 if (Style.Language == FormatStyle::LK_JavaScript &&
1814 FormatTok->isOneOf(tok::colon, tok::question))
1817 // Eat up enum class ...
1818 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1821 while (FormatTok->Tok.getIdentifierInfo() ||
1822 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1823 tok::greater, tok::comma, tok::question)) {
1825 // We can have macros or attributes in between 'enum' and the enum name.
1826 if (FormatTok->is(tok::l_paren))
1828 if (FormatTok->is(tok::identifier)) {
1830 // If there are two identifiers in a row, this is likely an elaborate
1831 // return type. In Java, this can be "implements", etc.
1832 if (Style.isCpp() && FormatTok->is(tok::identifier))
1837 // Just a declaration or something is wrong.
1838 if (FormatTok->isNot(tok::l_brace))
1840 FormatTok->BlockKind = BK_Block;
1842 if (Style.Language == FormatStyle::LK_Java) {
1843 // Java enums are different.
1844 parseJavaEnumBody();
1847 if (Style.Language == FormatStyle::LK_Proto) {
1848 parseBlock(/*MustBeDeclaration=*/true);
1854 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1856 if (FormatTok->is(tok::semi))
1862 // There is no addUnwrappedLine() here so that we fall through to parsing a
1863 // structural element afterwards. Thus, in "enum A {} n, m;",
1864 // "} n, m;" will end up in one unwrapped line.
1867 void UnwrappedLineParser::parseJavaEnumBody() {
1868 // Determine whether the enum is simple, i.e. does not have a semicolon or
1869 // constants with class bodies. Simple enums can be formatted like braced
1870 // lists, contracted to a single line, etc.
1871 unsigned StoredPosition = Tokens->getPosition();
1872 bool IsSimple = true;
1873 FormatToken *Tok = Tokens->getNextToken();
1875 if (Tok->is(tok::r_brace))
1877 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1881 // FIXME: This will also mark enums with braces in the arguments to enum
1882 // constants as "not simple". This is probably fine in practice, though.
1883 Tok = Tokens->getNextToken();
1885 FormatTok = Tokens->setPosition(StoredPosition);
1894 // Parse the body of a more complex enum.
1895 // First add a line for everything up to the "{".
1900 // Parse the enum constants.
1902 if (FormatTok->is(tok::l_brace)) {
1903 // Parse the constant's class body.
1904 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1905 /*MunchSemi=*/false);
1906 } else if (FormatTok->is(tok::l_paren)) {
1908 } else if (FormatTok->is(tok::comma)) {
1911 } else if (FormatTok->is(tok::semi)) {
1915 } else if (FormatTok->is(tok::r_brace)) {
1923 // Parse the class body after the enum's ";" if any.
1924 parseLevel(/*HasOpeningBrace=*/true);
1930 void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
1931 const FormatToken &InitialToken = *FormatTok;
1934 // The actual identifier can be a nested name specifier, and in macros
1935 // it is often token-pasted.
1936 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
1937 tok::kw___attribute, tok::kw___declspec,
1939 ((Style.Language == FormatStyle::LK_Java ||
1940 Style.Language == FormatStyle::LK_JavaScript) &&
1941 FormatTok->isOneOf(tok::period, tok::comma))) {
1942 bool IsNonMacroIdentifier =
1943 FormatTok->is(tok::identifier) &&
1944 FormatTok->TokenText != FormatTok->TokenText.upper();
1946 // We can have macros or attributes in between 'class' and the class name.
1947 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
1951 // Note that parsing away template declarations here leads to incorrectly
1952 // accepting function declarations as record declarations.
1953 // In general, we cannot solve this problem. Consider:
1954 // class A<int> B() {}
1955 // which can be a function definition or a class definition when B() is a
1956 // macro. If we find enough real-world cases where this is a problem, we
1957 // can parse for the 'template' keyword in the beginning of the statement,
1958 // and thus rule out the record production in case there is no template
1959 // (this would still leave us with an ambiguity between template function
1960 // and class declarations).
1961 if (FormatTok->isOneOf(tok::colon, tok::less)) {
1963 if (FormatTok->is(tok::l_brace)) {
1964 calculateBraceTypes(/*ExpectClassBody=*/true);
1965 if (!tryToParseBracedList())
1968 if (FormatTok->Tok.is(tok::semi))
1973 if (FormatTok->Tok.is(tok::l_brace)) {
1977 if (ShouldBreakBeforeBrace(Style, InitialToken))
1980 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1981 /*MunchSemi=*/false);
1984 // There is no addUnwrappedLine() here so that we fall through to parsing a
1985 // structural element afterwards. Thus, in "class A {} n, m;",
1986 // "} n, m;" will end up in one unwrapped line.
1989 void UnwrappedLineParser::parseObjCProtocolList() {
1990 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
1993 while (!eof() && FormatTok->Tok.isNot(tok::greater));
1994 nextToken(); // Skip '>'.
1997 void UnwrappedLineParser::parseObjCUntilAtEnd() {
1999 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
2004 if (FormatTok->is(tok::l_brace)) {
2005 parseBlock(/*MustBeDeclaration=*/false);
2006 // In ObjC interfaces, nothing should be following the "}".
2008 } else if (FormatTok->is(tok::r_brace)) {
2009 // Ignore stray "}". parseStructuralElement doesn't consume them.
2013 parseStructuralElement();
2018 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
2020 nextToken(); // interface name
2022 // @interface can be followed by either a base class, or a category.
2023 if (FormatTok->Tok.is(tok::colon)) {
2025 nextToken(); // base class name
2026 } else if (FormatTok->Tok.is(tok::l_paren))
2027 // Skip category, if present.
2030 if (FormatTok->Tok.is(tok::less))
2031 parseObjCProtocolList();
2033 if (FormatTok->Tok.is(tok::l_brace)) {
2034 if (Style.BraceWrapping.AfterObjCDeclaration)
2036 parseBlock(/*MustBeDeclaration=*/true);
2039 // With instance variables, this puts '}' on its own line. Without instance
2040 // variables, this ends the @interface line.
2043 parseObjCUntilAtEnd();
2046 void UnwrappedLineParser::parseObjCProtocol() {
2048 nextToken(); // protocol name
2050 if (FormatTok->Tok.is(tok::less))
2051 parseObjCProtocolList();
2053 // Check for protocol declaration.
2054 if (FormatTok->Tok.is(tok::semi)) {
2056 return addUnwrappedLine();
2060 parseObjCUntilAtEnd();
2063 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
2064 bool IsImport = FormatTok->is(Keywords.kw_import);
2065 assert(IsImport || FormatTok->is(tok::kw_export));
2068 // Consume the "default" in "export default class/function".
2069 if (FormatTok->is(tok::kw_default))
2072 // Consume "async function", "function" and "default function", so that these
2073 // get parsed as free-standing JS functions, i.e. do not require a trailing
2075 if (FormatTok->is(Keywords.kw_async))
2077 if (FormatTok->is(Keywords.kw_function)) {
2082 // For imports, `export *`, `export {...}`, consume the rest of the line up
2083 // to the terminating `;`. For everything else, just return and continue
2084 // parsing the structural element, i.e. the declaration or expression for
2085 // `export default`.
2086 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
2087 !FormatTok->isStringLiteral())
2091 if (FormatTok->is(tok::semi))
2093 if (Line->Tokens.size() == 0) {
2094 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
2095 // import statement should terminate.
2098 if (FormatTok->is(tok::l_brace)) {
2099 FormatTok->BlockKind = BK_Block;
2108 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
2109 StringRef Prefix = "") {
2110 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
2111 << (Line.InPPDirective ? " MACRO" : "") << ": ";
2112 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2113 E = Line.Tokens.end();
2115 llvm::dbgs() << I->Tok->Tok.getName() << "["
2116 << "T=" << I->Tok->Type
2117 << ", OC=" << I->Tok->OriginalColumn << "] ";
2119 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2120 E = Line.Tokens.end();
2122 const UnwrappedLineNode &Node = *I;
2123 for (SmallVectorImpl<UnwrappedLine>::const_iterator
2124 I = Node.Children.begin(),
2125 E = Node.Children.end();
2127 printDebugInfo(*I, "\nChild: ");
2130 llvm::dbgs() << "\n";
2133 void UnwrappedLineParser::addUnwrappedLine() {
2134 if (Line->Tokens.empty())
2137 if (CurrentLines == &Lines)
2138 printDebugInfo(*Line);
2140 CurrentLines->push_back(std::move(*Line));
2141 Line->Tokens.clear();
2142 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
2143 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
2144 CurrentLines->append(
2145 std::make_move_iterator(PreprocessorDirectives.begin()),
2146 std::make_move_iterator(PreprocessorDirectives.end()));
2147 PreprocessorDirectives.clear();
2151 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
2153 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
2154 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
2155 FormatTok.NewlinesBefore > 0;
2158 // Checks if \p FormatTok is a line comment that continues the line comment
2159 // section on \p Line.
2160 static bool continuesLineCommentSection(const FormatToken &FormatTok,
2161 const UnwrappedLine &Line,
2162 llvm::Regex &CommentPragmasRegex) {
2163 if (Line.Tokens.empty())
2166 StringRef IndentContent = FormatTok.TokenText;
2167 if (FormatTok.TokenText.startswith("//") ||
2168 FormatTok.TokenText.startswith("/*"))
2169 IndentContent = FormatTok.TokenText.substr(2);
2170 if (CommentPragmasRegex.match(IndentContent))
2173 // If Line starts with a line comment, then FormatTok continues the comment
2174 // section if its original column is greater or equal to the original start
2175 // column of the line.
2177 // Define the min column token of a line as follows: if a line ends in '{' or
2178 // contains a '{' followed by a line comment, then the min column token is
2179 // that '{'. Otherwise, the min column token of the line is the first token of
2182 // If Line starts with a token other than a line comment, then FormatTok
2183 // continues the comment section if its original column is greater than the
2184 // original start column of the min column token of the line.
2186 // For example, the second line comment continues the first in these cases:
2198 // int i; // first line
2203 // do { // first line
2216 // The second line comment doesn't continue the first in these cases:
2223 // int i; // first line
2228 // do { // first line
2239 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
2241 // Scan for '{//'. If found, use the column of '{' as a min column for line
2242 // comment section continuation.
2243 const FormatToken *PreviousToken = nullptr;
2244 for (const UnwrappedLineNode &Node : Line.Tokens) {
2245 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
2246 isLineComment(*Node.Tok)) {
2247 MinColumnToken = PreviousToken;
2250 PreviousToken = Node.Tok;
2252 // Grab the last newline preceding a token in this unwrapped line.
2253 if (Node.Tok->NewlinesBefore > 0) {
2254 MinColumnToken = Node.Tok;
2257 if (PreviousToken && PreviousToken->is(tok::l_brace)) {
2258 MinColumnToken = PreviousToken;
2261 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
2265 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
2266 bool JustComments = Line->Tokens.empty();
2267 for (SmallVectorImpl<FormatToken *>::const_iterator
2268 I = CommentsBeforeNextToken.begin(),
2269 E = CommentsBeforeNextToken.end();
2271 // Line comments that belong to the same line comment section are put on the
2272 // same line since later we might want to reflow content between them.
2273 // Additional fine-grained breaking of line comment sections is controlled
2274 // by the class BreakableLineCommentSection in case it is desirable to keep
2275 // several line comment sections in the same unwrapped line.
2277 // FIXME: Consider putting separate line comment sections as children to the
2278 // unwrapped line instead.
2279 (*I)->ContinuesLineCommentSection =
2280 continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
2281 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
2285 if (NewlineBeforeNext && JustComments)
2287 CommentsBeforeNextToken.clear();
2290 void UnwrappedLineParser::nextToken() {
2293 flushComments(isOnNewLine(*FormatTok));
2294 pushToken(FormatTok);
2295 if (Style.Language != FormatStyle::LK_JavaScript)
2298 readTokenWithJavaScriptASI();
2301 const FormatToken *UnwrappedLineParser::getPreviousToken() {
2302 // FIXME: This is a dirty way to access the previous token. Find a better
2304 if (!Line || Line->Tokens.empty())
2306 return Line->Tokens.back().Tok;
2309 void UnwrappedLineParser::distributeComments(
2310 const SmallVectorImpl<FormatToken *> &Comments,
2311 const FormatToken *NextTok) {
2312 // Whether or not a line comment token continues a line is controlled by
2313 // the method continuesLineCommentSection, with the following caveat:
2315 // Define a trail of Comments to be a nonempty proper postfix of Comments such
2316 // that each comment line from the trail is aligned with the next token, if
2317 // the next token exists. If a trail exists, the beginning of the maximal
2318 // trail is marked as a start of a new comment section.
2320 // For example in this code:
2322 // int a; // line about a
2323 // // line 1 about b
2324 // // line 2 about b
2327 // the two lines about b form a maximal trail, so there are two sections, the
2328 // first one consisting of the single comment "// line about a" and the
2329 // second one consisting of the next two comments.
2330 if (Comments.empty())
2332 bool ShouldPushCommentsInCurrentLine = true;
2333 bool HasTrailAlignedWithNextToken = false;
2334 unsigned StartOfTrailAlignedWithNextToken = 0;
2336 // We are skipping the first element intentionally.
2337 for (unsigned i = Comments.size() - 1; i > 0; --i) {
2338 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
2339 HasTrailAlignedWithNextToken = true;
2340 StartOfTrailAlignedWithNextToken = i;
2344 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
2345 FormatToken *FormatTok = Comments[i];
2346 if (HasTrailAlignedWithNextToken &&
2347 i == StartOfTrailAlignedWithNextToken) {
2348 FormatTok->ContinuesLineCommentSection = false;
2350 FormatTok->ContinuesLineCommentSection =
2351 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
2353 if (!FormatTok->ContinuesLineCommentSection &&
2354 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
2355 ShouldPushCommentsInCurrentLine = false;
2357 if (ShouldPushCommentsInCurrentLine) {
2358 pushToken(FormatTok);
2360 CommentsBeforeNextToken.push_back(FormatTok);
2365 void UnwrappedLineParser::readToken() {
2366 SmallVector<FormatToken *, 1> Comments;
2368 FormatTok = Tokens->getNextToken();
2370 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
2371 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
2372 distributeComments(Comments, FormatTok);
2374 // If there is an unfinished unwrapped line, we flush the preprocessor
2375 // directives only after that unwrapped line was finished later.
2376 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
2377 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
2378 // Comments stored before the preprocessor directive need to be output
2379 // before the preprocessor directive, at the same level as the
2380 // preprocessor directive, as we consider them to apply to the directive.
2381 flushComments(isOnNewLine(*FormatTok));
2384 while (FormatTok->Type == TT_ConflictStart ||
2385 FormatTok->Type == TT_ConflictEnd ||
2386 FormatTok->Type == TT_ConflictAlternative) {
2387 if (FormatTok->Type == TT_ConflictStart) {
2388 conditionalCompilationStart(/*Unreachable=*/false);
2389 } else if (FormatTok->Type == TT_ConflictAlternative) {
2390 conditionalCompilationAlternative();
2391 } else if (FormatTok->Type == TT_ConflictEnd) {
2392 conditionalCompilationEnd();
2394 FormatTok = Tokens->getNextToken();
2395 FormatTok->MustBreakBefore = true;
2398 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
2399 !Line->InPPDirective) {
2403 if (!FormatTok->Tok.is(tok::comment)) {
2404 distributeComments(Comments, FormatTok);
2409 Comments.push_back(FormatTok);
2412 distributeComments(Comments, nullptr);
2416 void UnwrappedLineParser::pushToken(FormatToken *Tok) {
2417 Line->Tokens.push_back(UnwrappedLineNode(Tok));
2418 if (MustBreakBeforeNextToken) {
2419 Line->Tokens.back().Tok->MustBreakBefore = true;
2420 MustBreakBeforeNextToken = false;
2424 } // end namespace format
2425 } // end namespace clang