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/Support/Debug.h"
19 #define DEBUG_TYPE "format-parser"
24 class FormatTokenSource {
26 virtual ~FormatTokenSource() {}
27 virtual FormatToken *getNextToken() = 0;
29 virtual unsigned getPosition() = 0;
30 virtual FormatToken *setPosition(unsigned Position) = 0;
35 class ScopedDeclarationState {
37 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
38 bool MustBeDeclaration)
39 : Line(Line), Stack(Stack) {
40 Line.MustBeDeclaration = MustBeDeclaration;
41 Stack.push_back(MustBeDeclaration);
43 ~ScopedDeclarationState() {
46 Line.MustBeDeclaration = Stack.back();
48 Line.MustBeDeclaration = true;
53 std::vector<bool> &Stack;
56 class ScopedMacroState : public FormatTokenSource {
58 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
59 FormatToken *&ResetToken, bool &StructuralError)
60 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
61 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
62 StructuralError(StructuralError),
63 PreviousStructuralError(StructuralError), Token(nullptr) {
66 Line.InPPDirective = true;
70 TokenSource = PreviousTokenSource;
72 Line.InPPDirective = false;
73 Line.Level = PreviousLineLevel;
74 StructuralError = PreviousStructuralError;
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.
81 Token = PreviousTokenSource->getNextToken();
87 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
89 FormatToken *setPosition(unsigned Position) override {
90 Token = PreviousTokenSource->setPosition(Position);
95 bool eof() { return Token && Token->HasUnescapedNewline; }
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;
109 FormatTokenSource *&TokenSource;
110 FormatToken *&ResetToken;
111 unsigned PreviousLineLevel;
112 FormatTokenSource *PreviousTokenSource;
113 bool &StructuralError;
114 bool PreviousStructuralError;
119 } // end anonymous namespace
121 class ScopedLineState {
123 ScopedLineState(UnwrappedLineParser &Parser,
124 bool SwitchToPreprocessorLines = false)
125 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
126 if (SwitchToPreprocessorLines)
127 Parser.CurrentLines = &Parser.PreprocessorDirectives;
128 else if (!Parser.Line->Tokens.empty())
129 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
130 PreBlockLine = std::move(Parser.Line);
131 Parser.Line = llvm::make_unique<UnwrappedLine>();
132 Parser.Line->Level = PreBlockLine->Level;
133 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
137 if (!Parser.Line->Tokens.empty()) {
138 Parser.addUnwrappedLine();
140 assert(Parser.Line->Tokens.empty());
141 Parser.Line = std::move(PreBlockLine);
142 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
143 Parser.MustBreakBeforeNextToken = true;
144 Parser.CurrentLines = OriginalLines;
148 UnwrappedLineParser &Parser;
150 std::unique_ptr<UnwrappedLine> PreBlockLine;
151 SmallVectorImpl<UnwrappedLine> *OriginalLines;
154 class CompoundStatementIndenter {
156 CompoundStatementIndenter(UnwrappedLineParser *Parser,
157 const FormatStyle &Style, unsigned &LineLevel)
158 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
159 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
160 Parser->addUnwrappedLine();
161 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
162 Parser->addUnwrappedLine();
166 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
170 unsigned OldLineLevel;
175 class IndexedTokenSource : public FormatTokenSource {
177 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
178 : Tokens(Tokens), Position(-1) {}
180 FormatToken *getNextToken() override {
182 return Tokens[Position];
185 unsigned getPosition() override {
186 assert(Position >= 0);
190 FormatToken *setPosition(unsigned P) override {
192 return Tokens[Position];
195 void reset() { Position = -1; }
198 ArrayRef<FormatToken *> Tokens;
202 } // end anonymous namespace
204 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
205 const AdditionalKeywords &Keywords,
206 ArrayRef<FormatToken *> Tokens,
207 UnwrappedLineConsumer &Callback)
208 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
209 CurrentLines(&Lines), StructuralError(false), Style(Style),
210 Keywords(Keywords), Tokens(nullptr), Callback(Callback),
211 AllTokens(Tokens), PPBranchLevel(-1) {}
213 void UnwrappedLineParser::reset() {
215 Line.reset(new UnwrappedLine);
216 CommentsBeforeNextToken.clear();
218 MustBreakBeforeNextToken = false;
219 PreprocessorDirectives.clear();
220 CurrentLines = &Lines;
221 DeclarationScopeStack.clear();
222 StructuralError = false;
226 bool UnwrappedLineParser::parse() {
227 IndexedTokenSource TokenSource(AllTokens);
229 DEBUG(llvm::dbgs() << "----\n");
231 Tokens = &TokenSource;
236 // Create line with eof token.
237 pushToken(FormatTok);
240 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
243 Callback.consumeUnwrappedLine(*I);
245 Callback.finishRun();
247 while (!PPLevelBranchIndex.empty() &&
248 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
249 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
250 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
252 if (!PPLevelBranchIndex.empty()) {
253 ++PPLevelBranchIndex.back();
254 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
255 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
257 } while (!PPLevelBranchIndex.empty());
259 return StructuralError;
262 void UnwrappedLineParser::parseFile() {
263 ScopedDeclarationState DeclarationState(
264 *Line, DeclarationScopeStack,
265 /*MustBeDeclaration=*/ !Line->InPPDirective);
266 parseLevel(/*HasOpeningBrace=*/false);
267 // Make sure to format the remaining tokens.
272 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
273 bool SwitchLabelEncountered = false;
275 switch (FormatTok->Tok.getKind()) {
281 // FIXME: Add parameter whether this can happen - if this happens, we must
282 // be in a non-declaration context.
283 parseBlock(/*MustBeDeclaration=*/false);
289 StructuralError = true;
293 case tok::kw_default:
295 if (!SwitchLabelEncountered &&
296 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
298 SwitchLabelEncountered = true;
299 parseStructuralElement();
302 parseStructuralElement();
308 void UnwrappedLineParser::calculateBraceTypes() {
309 // We'll parse forward through the tokens until we hit
310 // a closing brace or eof - note that getNextToken() will
311 // parse macros, so this will magically work inside macro
313 unsigned StoredPosition = Tokens->getPosition();
314 FormatToken *Tok = FormatTok;
315 // Keep a stack of positions of lbrace tokens. We will
316 // update information about whether an lbrace starts a
317 // braced init list or a different block during the loop.
318 SmallVector<FormatToken *, 8> LBraceStack;
319 assert(Tok->Tok.is(tok::l_brace));
321 // Get next none-comment token.
322 FormatToken *NextTok;
323 unsigned ReadTokens = 0;
325 NextTok = Tokens->getNextToken();
327 } while (NextTok->is(tok::comment));
329 switch (Tok->Tok.getKind()) {
331 LBraceStack.push_back(Tok);
334 if (!LBraceStack.empty()) {
335 if (LBraceStack.back()->BlockKind == BK_Unknown) {
336 bool ProbablyBracedList = false;
337 if (Style.Language == FormatStyle::LK_Proto) {
338 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
340 // Using OriginalColumn to distinguish between ObjC methods and
341 // binary operators is a bit hacky.
342 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
343 NextTok->OriginalColumn == 0;
345 // If there is a comma, semicolon or right paren after the closing
346 // brace, we assume this is a braced initializer list. Note that
347 // regardless how we mark inner braces here, we will overwrite the
348 // BlockKind later if we parse a braced list (where all blocks
349 // inside are by default braced lists), or when we explicitly detect
350 // blocks (for example while parsing lambdas).
352 // We exclude + and - as they can be ObjC visibility modifiers.
354 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
355 tok::r_paren, tok::r_square, tok::l_brace,
356 tok::l_paren, tok::ellipsis) ||
357 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
359 if (ProbablyBracedList) {
360 Tok->BlockKind = BK_BracedInit;
361 LBraceStack.back()->BlockKind = BK_BracedInit;
363 Tok->BlockKind = BK_Block;
364 LBraceStack.back()->BlockKind = BK_Block;
367 LBraceStack.pop_back();
377 if (!LBraceStack.empty())
378 LBraceStack.back()->BlockKind = BK_Block;
384 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
385 // Assume other blocks for all unclosed opening braces.
386 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
387 if (LBraceStack[i]->BlockKind == BK_Unknown)
388 LBraceStack[i]->BlockKind = BK_Block;
391 FormatTok = Tokens->setPosition(StoredPosition);
394 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
396 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
397 unsigned InitialLevel = Line->Level;
402 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
406 parseLevel(/*HasOpeningBrace=*/true);
408 if (!FormatTok->Tok.is(tok::r_brace)) {
409 Line->Level = InitialLevel;
410 StructuralError = true;
414 nextToken(); // Munch the closing brace.
415 if (MunchSemi && FormatTok->Tok.is(tok::semi))
417 Line->Level = InitialLevel;
420 static bool IsGoogScope(const UnwrappedLine &Line) {
421 // FIXME: Closure-library specific stuff should not be hard-coded but be
423 if (Line.Tokens.size() < 4)
425 auto I = Line.Tokens.begin();
426 if (I->Tok->TokenText != "goog")
429 if (I->Tok->isNot(tok::period))
432 if (I->Tok->TokenText != "scope")
435 return I->Tok->is(tok::l_paren);
438 static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
439 const FormatToken &InitialToken) {
440 switch (Style.BreakBeforeBraces) {
441 case FormatStyle::BS_Linux:
442 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
443 case FormatStyle::BS_Allman:
444 case FormatStyle::BS_GNU:
451 void UnwrappedLineParser::parseChildBlock() {
452 FormatTok->BlockKind = BK_Block;
456 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
457 ScopedLineState LineState(*this);
458 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
459 /*MustBeDeclaration=*/false);
460 Line->Level += GoogScope ? 0 : 1;
461 parseLevel(/*HasOpeningBrace=*/true);
462 Line->Level -= GoogScope ? 0 : 1;
467 void UnwrappedLineParser::parsePPDirective() {
468 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
469 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
472 if (!FormatTok->Tok.getIdentifierInfo()) {
477 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
482 parsePPIf(/*IfDef=*/false);
486 parsePPIf(/*IfDef=*/true);
503 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
504 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
505 PPStack.push_back(PP_Unreachable);
507 PPStack.push_back(PP_Conditional);
510 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
512 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
513 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
514 PPLevelBranchIndex.push_back(0);
515 PPLevelBranchCount.push_back(0);
517 PPChainBranchIndex.push(0);
518 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
519 conditionalCompilationCondition(Unreachable || Skip);
522 void UnwrappedLineParser::conditionalCompilationAlternative() {
523 if (!PPStack.empty())
525 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
526 if (!PPChainBranchIndex.empty())
527 ++PPChainBranchIndex.top();
528 conditionalCompilationCondition(
529 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
530 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
533 void UnwrappedLineParser::conditionalCompilationEnd() {
534 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
535 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
536 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
537 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
540 // Guard against #endif's without #if.
541 if (PPBranchLevel > 0)
543 if (!PPChainBranchIndex.empty())
544 PPChainBranchIndex.pop();
545 if (!PPStack.empty())
549 void UnwrappedLineParser::parsePPIf(bool IfDef) {
551 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
552 StringRef(FormatTok->Tok.getLiteralData(),
553 FormatTok->Tok.getLength()) == "0") ||
554 FormatTok->Tok.is(tok::kw_false);
555 conditionalCompilationStart(!IfDef && IsLiteralFalse);
559 void UnwrappedLineParser::parsePPElse() {
560 conditionalCompilationAlternative();
564 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
566 void UnwrappedLineParser::parsePPEndIf() {
567 conditionalCompilationEnd();
571 void UnwrappedLineParser::parsePPDefine() {
574 if (FormatTok->Tok.getKind() != tok::identifier) {
579 if (FormatTok->Tok.getKind() == tok::l_paren &&
580 FormatTok->WhitespaceRange.getBegin() ==
581 FormatTok->WhitespaceRange.getEnd()) {
587 // Errors during a preprocessor directive can only affect the layout of the
588 // preprocessor directive, and thus we ignore them. An alternative approach
589 // would be to use the same approach we use on the file level (no
590 // re-indentation if there was a structural error) within the macro
595 void UnwrappedLineParser::parsePPUnknown() {
602 // Here we blacklist certain tokens that are not usually the first token in an
603 // unwrapped line. This is used in attempt to distinguish macro calls without
604 // trailing semicolons from other constructs split to several lines.
605 bool tokenCanStartNewLine(clang::Token Tok) {
606 // Semicolon can be a null-statement, l_square can be a start of a macro or
607 // a C++11 attribute, but this doesn't seem to be common.
608 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
609 Tok.isNot(tok::l_square) &&
610 // Tokens that can only be used as binary operators and a part of
611 // overloaded operator names.
612 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
613 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
614 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
615 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
616 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
617 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
618 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
619 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
620 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
621 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
622 Tok.isNot(tok::lesslessequal) &&
623 // Colon is used in labels, base class lists, initializer lists,
624 // range-based for loops, ternary operator, but should never be the
625 // first token in an unwrapped line.
626 Tok.isNot(tok::colon) &&
627 // 'noexcept' is a trailing annotation.
628 Tok.isNot(tok::kw_noexcept);
631 void UnwrappedLineParser::parseStructuralElement() {
632 assert(!FormatTok->Tok.is(tok::l_brace));
633 switch (FormatTok->Tok.getKind()) {
636 if (FormatTok->Tok.is(tok::l_brace)) {
640 switch (FormatTok->Tok.getObjCKeywordID()) {
641 case tok::objc_public:
642 case tok::objc_protected:
643 case tok::objc_package:
644 case tok::objc_private:
645 return parseAccessSpecifier();
646 case tok::objc_interface:
647 case tok::objc_implementation:
648 return parseObjCInterfaceOrImplementation();
649 case tok::objc_protocol:
650 return parseObjCProtocol();
652 return; // Handled by the caller.
653 case tok::objc_optional:
654 case tok::objc_required:
664 if (FormatTok->is(tok::l_brace)) {
666 while (FormatTok && FormatTok->isNot(tok::eof)) {
667 if (FormatTok->is(tok::r_brace)) {
671 FormatTok->Finalized = true;
676 case tok::kw_namespace:
681 if (FormatTok->Tok.is(tok::kw_namespace)) {
687 case tok::kw_protected:
688 case tok::kw_private:
689 if (Style.Language == FormatStyle::LK_Java)
692 parseAccessSpecifier();
699 parseForOrWhileLoop();
707 case tok::kw_default:
719 if (FormatTok->Tok.is(tok::string_literal)) {
721 if (FormatTok->Tok.is(tok::l_brace)) {
722 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
728 case tok::identifier:
729 if (FormatTok->IsForEachMacro) {
730 parseForOrWhileLoop();
733 // In all other cases, parse the declaration.
739 switch (FormatTok->Tok.getKind()) {
742 if (FormatTok->Tok.is(tok::l_brace))
748 case tok::kw_typedef:
750 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
751 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
758 // A record declaration or definition is always the start of a structural
763 // In Java, classes have an implicit static member "class".
764 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
765 FormatTok->is(tok::kw_class))
780 if (FormatTok->Tok.isAnyIdentifier() ||
781 FormatTok->isSimpleTypeSpecifier())
783 if (FormatTok->is(tok::l_paren))
785 if (FormatTok->is(tok::l_brace))
789 if (!tryToParseBracedList()) {
790 // A block outside of parentheses must be the last part of a
791 // structural element.
792 // FIXME: Figure out cases where this is not true, and add projections
793 // for them (the one we know is missing are lambdas).
794 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
796 FormatTok->Type = TT_FunctionLBrace;
797 parseBlock(/*MustBeDeclaration=*/false);
801 // Otherwise this was a braced init list, and the structural
802 // element continues.
805 // We arrive here when parsing function-try blocks.
808 case tok::identifier: {
809 StringRef Text = FormatTok->TokenText;
810 // Parse function literal unless 'function' is the first token in a line
811 // in which case this should be treated as a free-standing function.
812 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
813 Line->Tokens.size() > 0) {
814 tryToParseJSFunction();
818 if (Line->Tokens.size() == 1) {
819 if (FormatTok->Tok.is(tok::colon)) {
823 // Recognize function-like macro usages without trailing semicolon as
824 // well as free-standing macrose like Q_OBJECT.
825 bool FunctionLike = FormatTok->is(tok::l_paren);
828 if (FormatTok->NewlinesBefore > 0 &&
829 (Text.size() >= 5 || FunctionLike) &&
830 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
839 if (FormatTok->Tok.is(tok::l_brace)) {
853 bool UnwrappedLineParser::tryToParseLambda() {
854 // FIXME: This is a dirty way to access the previous token. Find a better
856 if (!Line->Tokens.empty() &&
857 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
858 tok::kw_new, tok::kw_delete) ||
859 Line->Tokens.back().Tok->closesScope() ||
860 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
864 assert(FormatTok->is(tok::l_square));
865 FormatToken &LSquare = *FormatTok;
866 if (!tryToParseLambdaIntroducer())
869 while (FormatTok->isNot(tok::l_brace)) {
870 if (FormatTok->isSimpleTypeSpecifier()) {
874 switch (FormatTok->Tok.getKind()) {
886 case tok::identifier:
887 case tok::coloncolon:
888 case tok::kw_mutable:
892 FormatTok->Type = TT_TrailingReturnArrow;
899 LSquare.Type = TT_LambdaLSquare;
904 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
906 if (FormatTok->is(tok::equal)) {
908 if (FormatTok->is(tok::r_square)) {
912 if (FormatTok->isNot(tok::comma))
915 } else if (FormatTok->is(tok::amp)) {
917 if (FormatTok->is(tok::r_square)) {
921 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
924 if (FormatTok->is(tok::comma))
926 } else if (FormatTok->is(tok::r_square)) {
931 if (FormatTok->is(tok::amp))
933 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
936 if (FormatTok->is(tok::ellipsis))
938 if (FormatTok->is(tok::comma)) {
940 } else if (FormatTok->is(tok::r_square)) {
950 void UnwrappedLineParser::tryToParseJSFunction() {
953 // Consume function name.
954 if (FormatTok->is(tok::identifier))
957 if (FormatTok->isNot(tok::l_paren))
960 while (FormatTok->isNot(tok::l_brace)) {
961 // Err on the side of caution in order to avoid consuming the full file in
962 // case of incomplete code.
963 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
971 bool UnwrappedLineParser::tryToParseBracedList() {
972 if (FormatTok->BlockKind == BK_Unknown)
973 calculateBraceTypes();
974 assert(FormatTok->BlockKind != BK_Unknown);
975 if (FormatTok->BlockKind == BK_Block)
981 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
982 bool HasError = false;
985 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
986 // replace this by using parseAssigmentExpression() inside.
988 if (Style.Language == FormatStyle::LK_JavaScript &&
989 FormatTok->is(Keywords.kw_function)) {
990 tryToParseJSFunction();
993 switch (FormatTok->Tok.getKind()) {
996 if (FormatTok->is(tok::l_brace)) {
1004 // Assume there are no blocks inside a braced init list apart
1005 // from the ones we explicitly parse out (like lambdas).
1006 FormatTok->BlockKind = BK_BracedInit;
1014 if (!ContinueOnSemicolons)
1029 void UnwrappedLineParser::parseParens() {
1030 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
1033 switch (FormatTok->Tok.getKind()) {
1036 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1043 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1049 if (!tryToParseBracedList()) {
1055 if (FormatTok->Tok.is(tok::l_brace))
1058 case tok::identifier:
1059 if (Style.Language == FormatStyle::LK_JavaScript &&
1060 FormatTok->is(Keywords.kw_function))
1061 tryToParseJSFunction();
1072 void UnwrappedLineParser::parseSquare() {
1073 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1074 if (tryToParseLambda())
1077 switch (FormatTok->Tok.getKind()) {
1085 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1090 case tok::l_brace: {
1091 if (!tryToParseBracedList()) {
1098 if (FormatTok->Tok.is(tok::l_brace))
1108 void UnwrappedLineParser::parseIfThenElse() {
1109 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
1111 if (FormatTok->Tok.is(tok::l_paren))
1113 bool NeedsUnwrappedLine = false;
1114 if (FormatTok->Tok.is(tok::l_brace)) {
1115 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1116 parseBlock(/*MustBeDeclaration=*/false);
1117 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1118 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
1121 NeedsUnwrappedLine = true;
1126 parseStructuralElement();
1129 if (FormatTok->Tok.is(tok::kw_else)) {
1130 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1133 if (FormatTok->Tok.is(tok::l_brace)) {
1134 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1135 parseBlock(/*MustBeDeclaration=*/false);
1137 } else if (FormatTok->Tok.is(tok::kw_if)) {
1142 parseStructuralElement();
1145 } else if (NeedsUnwrappedLine) {
1150 void UnwrappedLineParser::parseTryCatch() {
1151 assert(FormatTok->is(tok::kw_try) && "'try' expected");
1153 bool NeedsUnwrappedLine = false;
1154 if (FormatTok->is(tok::colon)) {
1155 // We are in a function try block, what comes is an initializer list.
1157 while (FormatTok->is(tok::identifier)) {
1159 if (FormatTok->is(tok::l_paren))
1162 StructuralError = true;
1163 if (FormatTok->is(tok::comma))
1167 // Parse try with resource.
1168 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1171 if (FormatTok->is(tok::l_brace)) {
1172 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1173 parseBlock(/*MustBeDeclaration=*/false);
1174 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1175 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1176 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1179 NeedsUnwrappedLine = true;
1181 } else if (!FormatTok->is(tok::kw_catch)) {
1182 // The C++ standard requires a compound-statement after a try.
1183 // If there's none, we try to assume there's a structuralElement
1184 // and try to continue.
1185 StructuralError = true;
1188 parseStructuralElement();
1191 while (FormatTok->is(tok::kw_catch) ||
1192 ((Style.Language == FormatStyle::LK_Java ||
1193 Style.Language == FormatStyle::LK_JavaScript) &&
1194 FormatTok->is(Keywords.kw_finally))) {
1196 while (FormatTok->isNot(tok::l_brace)) {
1197 if (FormatTok->is(tok::l_paren)) {
1201 if (FormatTok->isOneOf(tok::semi, tok::r_brace))
1205 NeedsUnwrappedLine = false;
1206 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1207 parseBlock(/*MustBeDeclaration=*/false);
1208 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1209 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1210 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1213 NeedsUnwrappedLine = true;
1216 if (NeedsUnwrappedLine) {
1221 void UnwrappedLineParser::parseNamespace() {
1222 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
1224 const FormatToken &InitialToken = *FormatTok;
1226 if (FormatTok->Tok.is(tok::identifier))
1228 if (FormatTok->Tok.is(tok::l_brace)) {
1229 if (ShouldBreakBeforeBrace(Style, InitialToken))
1232 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1233 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1234 DeclarationScopeStack.size() > 1);
1235 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
1236 // Munch the semicolon after a namespace. This is more common than one would
1237 // think. Puttin the semicolon into its own line is very ugly.
1238 if (FormatTok->Tok.is(tok::semi))
1242 // FIXME: Add error handling.
1245 void UnwrappedLineParser::parseForOrWhileLoop() {
1246 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1247 FormatTok->IsForEachMacro) &&
1248 "'for', 'while' or foreach macro expected");
1250 if (FormatTok->Tok.is(tok::l_paren))
1252 if (FormatTok->Tok.is(tok::l_brace)) {
1253 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1254 parseBlock(/*MustBeDeclaration=*/false);
1259 parseStructuralElement();
1264 void UnwrappedLineParser::parseDoWhile() {
1265 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
1267 if (FormatTok->Tok.is(tok::l_brace)) {
1268 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1269 parseBlock(/*MustBeDeclaration=*/false);
1270 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1275 parseStructuralElement();
1279 // FIXME: Add error handling.
1280 if (!FormatTok->Tok.is(tok::kw_while)) {
1286 parseStructuralElement();
1289 void UnwrappedLineParser::parseLabel() {
1291 unsigned OldLineLevel = Line->Level;
1292 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1294 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
1295 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1296 parseBlock(/*MustBeDeclaration=*/false);
1297 if (FormatTok->Tok.is(tok::kw_break)) {
1298 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1299 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1300 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
1303 parseStructuralElement();
1309 Line->Level = OldLineLevel;
1312 void UnwrappedLineParser::parseCaseLabel() {
1313 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
1314 // FIXME: fix handling of complex expressions here.
1317 } while (!eof() && !FormatTok->Tok.is(tok::colon));
1321 void UnwrappedLineParser::parseSwitch() {
1322 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
1324 if (FormatTok->Tok.is(tok::l_paren))
1326 if (FormatTok->Tok.is(tok::l_brace)) {
1327 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1328 parseBlock(/*MustBeDeclaration=*/false);
1333 parseStructuralElement();
1338 void UnwrappedLineParser::parseAccessSpecifier() {
1340 // Understand Qt's slots.
1341 if (FormatTok->is(tok::identifier) &&
1342 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
1344 // Otherwise, we don't know what it is, and we'd better keep the next token.
1345 if (FormatTok->Tok.is(tok::colon))
1350 void UnwrappedLineParser::parseEnum() {
1351 // Won't be 'enum' for NS_ENUMs.
1352 if (FormatTok->Tok.is(tok::kw_enum))
1355 // Eat up enum class ...
1356 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1358 while (FormatTok->Tok.getIdentifierInfo() ||
1359 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1360 tok::greater, tok::comma, tok::question)) {
1362 // We can have macros or attributes in between 'enum' and the enum name.
1363 if (FormatTok->is(tok::l_paren))
1365 if (FormatTok->is(tok::identifier))
1369 // Just a declaration or something is wrong.
1370 if (FormatTok->isNot(tok::l_brace))
1372 FormatTok->BlockKind = BK_Block;
1374 if (Style.Language == FormatStyle::LK_Java) {
1375 // Java enums are different.
1376 parseJavaEnumBody();
1381 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1383 if (FormatTok->is(tok::semi))
1388 // We fall through to parsing a structural element afterwards, so that in
1390 // "} n, m;" will end up in one unwrapped line.
1393 void UnwrappedLineParser::parseJavaEnumBody() {
1394 // Determine whether the enum is simple, i.e. does not have a semicolon or
1395 // constants with class bodies. Simple enums can be formatted like braced
1396 // lists, contracted to a single line, etc.
1397 unsigned StoredPosition = Tokens->getPosition();
1398 bool IsSimple = true;
1399 FormatToken *Tok = Tokens->getNextToken();
1401 if (Tok->is(tok::r_brace))
1403 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1407 // FIXME: This will also mark enums with braces in the arguments to enum
1408 // constants as "not simple". This is probably fine in practice, though.
1409 Tok = Tokens->getNextToken();
1411 FormatTok = Tokens->setPosition(StoredPosition);
1419 // Parse the body of a more complex enum.
1420 // First add a line for everything up to the "{".
1425 // Parse the enum constants.
1427 if (FormatTok->is(tok::l_brace)) {
1428 // Parse the constant's class body.
1429 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1430 /*MunchSemi=*/false);
1431 } else if (FormatTok->is(tok::l_paren)) {
1433 } else if (FormatTok->is(tok::comma)) {
1436 } else if (FormatTok->is(tok::semi)) {
1440 } else if (FormatTok->is(tok::r_brace)) {
1448 // Parse the class body after the enum's ";" if any.
1449 parseLevel(/*HasOpeningBrace=*/true);
1455 void UnwrappedLineParser::parseRecord() {
1456 const FormatToken &InitialToken = *FormatTok;
1458 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1459 tok::kw___declspec, tok::kw_alignas)) {
1461 // We can have macros or attributes in between 'class' and the class name.
1462 if (FormatTok->Tok.is(tok::l_paren)) {
1465 // The actual identifier can be a nested name specifier, and in macros
1466 // it is often token-pasted.
1467 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1468 FormatTok->is(tok::hashhash) ||
1469 (Style.Language == FormatStyle::LK_Java &&
1470 FormatTok->isOneOf(tok::period, tok::comma)))
1473 // Note that parsing away template declarations here leads to incorrectly
1474 // accepting function declarations as record declarations.
1475 // In general, we cannot solve this problem. Consider:
1476 // class A<int> B() {}
1477 // which can be a function definition or a class definition when B() is a
1478 // macro. If we find enough real-world cases where this is a problem, we
1479 // can parse for the 'template' keyword in the beginning of the statement,
1480 // and thus rule out the record production in case there is no template
1481 // (this would still leave us with an ambiguity between template function
1482 // and class declarations).
1483 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1484 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1485 if (FormatTok->Tok.is(tok::semi))
1491 if (FormatTok->Tok.is(tok::l_brace)) {
1492 if (ShouldBreakBeforeBrace(Style, InitialToken))
1495 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1496 /*MunchSemi=*/false);
1498 // We fall through to parsing a structural element afterwards, so
1500 // will end up in one unwrapped line.
1501 // This does not apply for Java.
1502 if (Style.Language == FormatStyle::LK_Java)
1506 void UnwrappedLineParser::parseObjCProtocolList() {
1507 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
1510 while (!eof() && FormatTok->Tok.isNot(tok::greater));
1511 nextToken(); // Skip '>'.
1514 void UnwrappedLineParser::parseObjCUntilAtEnd() {
1516 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
1521 if (FormatTok->is(tok::l_brace)) {
1522 parseBlock(/*MustBeDeclaration=*/false);
1523 // In ObjC interfaces, nothing should be following the "}".
1525 } else if (FormatTok->is(tok::r_brace)) {
1526 // Ignore stray "}". parseStructuralElement doesn't consume them.
1530 parseStructuralElement();
1535 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
1537 nextToken(); // interface name
1539 // @interface can be followed by either a base class, or a category.
1540 if (FormatTok->Tok.is(tok::colon)) {
1542 nextToken(); // base class name
1543 } else if (FormatTok->Tok.is(tok::l_paren))
1544 // Skip category, if present.
1547 if (FormatTok->Tok.is(tok::less))
1548 parseObjCProtocolList();
1550 if (FormatTok->Tok.is(tok::l_brace)) {
1551 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1552 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1554 parseBlock(/*MustBeDeclaration=*/true);
1557 // With instance variables, this puts '}' on its own line. Without instance
1558 // variables, this ends the @interface line.
1561 parseObjCUntilAtEnd();
1564 void UnwrappedLineParser::parseObjCProtocol() {
1566 nextToken(); // protocol name
1568 if (FormatTok->Tok.is(tok::less))
1569 parseObjCProtocolList();
1571 // Check for protocol declaration.
1572 if (FormatTok->Tok.is(tok::semi)) {
1574 return addUnwrappedLine();
1578 parseObjCUntilAtEnd();
1581 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1582 StringRef Prefix = "") {
1583 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1584 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1585 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1586 E = Line.Tokens.end();
1588 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
1590 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1591 E = Line.Tokens.end();
1593 const UnwrappedLineNode &Node = *I;
1594 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1595 I = Node.Children.begin(),
1596 E = Node.Children.end();
1598 printDebugInfo(*I, "\nChild: ");
1601 llvm::dbgs() << "\n";
1604 void UnwrappedLineParser::addUnwrappedLine() {
1605 if (Line->Tokens.empty())
1608 if (CurrentLines == &Lines)
1609 printDebugInfo(*Line);
1611 CurrentLines->push_back(*Line);
1612 Line->Tokens.clear();
1613 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
1614 for (SmallVectorImpl<UnwrappedLine>::iterator
1615 I = PreprocessorDirectives.begin(),
1616 E = PreprocessorDirectives.end();
1618 CurrentLines->push_back(*I);
1620 PreprocessorDirectives.clear();
1624 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
1626 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
1627 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1628 FormatTok.NewlinesBefore > 0;
1631 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1632 bool JustComments = Line->Tokens.empty();
1633 for (SmallVectorImpl<FormatToken *>::const_iterator
1634 I = CommentsBeforeNextToken.begin(),
1635 E = CommentsBeforeNextToken.end();
1637 if (isOnNewLine(**I) && JustComments) {
1642 if (NewlineBeforeNext && JustComments) {
1645 CommentsBeforeNextToken.clear();
1648 void UnwrappedLineParser::nextToken() {
1651 flushComments(isOnNewLine(*FormatTok));
1652 pushToken(FormatTok);
1656 void UnwrappedLineParser::readToken() {
1657 bool CommentsInCurrentLine = true;
1659 FormatTok = Tokens->getNextToken();
1661 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1662 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
1663 // If there is an unfinished unwrapped line, we flush the preprocessor
1664 // directives only after that unwrapped line was finished later.
1665 bool SwitchToPreprocessorLines =
1666 !Line->Tokens.empty() && CurrentLines == &Lines;
1667 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
1668 // Comments stored before the preprocessor directive need to be output
1669 // before the preprocessor directive, at the same level as the
1670 // preprocessor directive, as we consider them to apply to the directive.
1671 flushComments(isOnNewLine(*FormatTok));
1674 while (FormatTok->Type == TT_ConflictStart ||
1675 FormatTok->Type == TT_ConflictEnd ||
1676 FormatTok->Type == TT_ConflictAlternative) {
1677 if (FormatTok->Type == TT_ConflictStart) {
1678 conditionalCompilationStart(/*Unreachable=*/false);
1679 } else if (FormatTok->Type == TT_ConflictAlternative) {
1680 conditionalCompilationAlternative();
1681 } else if (FormatTok->Type == TT_ConflictEnd) {
1682 conditionalCompilationEnd();
1684 FormatTok = Tokens->getNextToken();
1685 FormatTok->MustBreakBefore = true;
1688 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1689 !Line->InPPDirective) {
1693 if (!FormatTok->Tok.is(tok::comment))
1695 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
1696 CommentsInCurrentLine = false;
1698 if (CommentsInCurrentLine) {
1699 pushToken(FormatTok);
1701 CommentsBeforeNextToken.push_back(FormatTok);
1706 void UnwrappedLineParser::pushToken(FormatToken *Tok) {
1707 Line->Tokens.push_back(UnwrappedLineNode(Tok));
1708 if (MustBreakBeforeNextToken) {
1709 Line->Tokens.back().Tok->MustBreakBefore = true;
1710 MustBreakBeforeNextToken = false;
1714 } // end namespace format
1715 } // end namespace clang