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 class ScopedMacroState : public FormatTokenSource {
60 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
61 FormatToken *&ResetToken)
62 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
63 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
67 Line.InPPDirective = true;
70 ~ScopedMacroState() override {
71 TokenSource = PreviousTokenSource;
73 Line.InPPDirective = false;
74 Line.Level = PreviousLineLevel;
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;
117 } // end anonymous namespace
119 class ScopedLineState {
121 ScopedLineState(UnwrappedLineParser &Parser,
122 bool SwitchToPreprocessorLines = false)
123 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
124 if (SwitchToPreprocessorLines)
125 Parser.CurrentLines = &Parser.PreprocessorDirectives;
126 else if (!Parser.Line->Tokens.empty())
127 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
128 PreBlockLine = std::move(Parser.Line);
129 Parser.Line = llvm::make_unique<UnwrappedLine>();
130 Parser.Line->Level = PreBlockLine->Level;
131 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
135 if (!Parser.Line->Tokens.empty()) {
136 Parser.addUnwrappedLine();
138 assert(Parser.Line->Tokens.empty());
139 Parser.Line = std::move(PreBlockLine);
140 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
141 Parser.MustBreakBeforeNextToken = true;
142 Parser.CurrentLines = OriginalLines;
146 UnwrappedLineParser &Parser;
148 std::unique_ptr<UnwrappedLine> PreBlockLine;
149 SmallVectorImpl<UnwrappedLine> *OriginalLines;
152 class CompoundStatementIndenter {
154 CompoundStatementIndenter(UnwrappedLineParser *Parser,
155 const FormatStyle &Style, unsigned &LineLevel)
156 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
157 if (Style.BraceWrapping.AfterControlStatement)
158 Parser->addUnwrappedLine();
159 if (Style.BraceWrapping.IndentBraces)
162 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
166 unsigned OldLineLevel;
171 class IndexedTokenSource : public FormatTokenSource {
173 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
174 : Tokens(Tokens), Position(-1) {}
176 FormatToken *getNextToken() override {
178 return Tokens[Position];
181 unsigned getPosition() override {
182 assert(Position >= 0);
186 FormatToken *setPosition(unsigned P) override {
188 return Tokens[Position];
191 void reset() { Position = -1; }
194 ArrayRef<FormatToken *> Tokens;
198 } // end anonymous namespace
200 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
201 const AdditionalKeywords &Keywords,
202 ArrayRef<FormatToken *> Tokens,
203 UnwrappedLineConsumer &Callback)
204 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
205 CurrentLines(&Lines), Style(Style), Keywords(Keywords), Tokens(nullptr),
206 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
208 void UnwrappedLineParser::reset() {
210 Line.reset(new UnwrappedLine);
211 CommentsBeforeNextToken.clear();
213 MustBreakBeforeNextToken = false;
214 PreprocessorDirectives.clear();
215 CurrentLines = &Lines;
216 DeclarationScopeStack.clear();
220 void UnwrappedLineParser::parse() {
221 IndexedTokenSource TokenSource(AllTokens);
223 DEBUG(llvm::dbgs() << "----\n");
225 Tokens = &TokenSource;
230 // Create line with eof token.
231 pushToken(FormatTok);
234 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
237 Callback.consumeUnwrappedLine(*I);
239 Callback.finishRun();
241 while (!PPLevelBranchIndex.empty() &&
242 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
243 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
244 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
246 if (!PPLevelBranchIndex.empty()) {
247 ++PPLevelBranchIndex.back();
248 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
249 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
251 } while (!PPLevelBranchIndex.empty());
254 void UnwrappedLineParser::parseFile() {
255 // The top-level context in a file always has declarations, except for pre-
256 // processor directives and JavaScript files.
257 bool MustBeDeclaration =
258 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
259 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
261 parseLevel(/*HasOpeningBrace=*/false);
262 // Make sure to format the remaining tokens.
267 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
268 bool SwitchLabelEncountered = false;
270 tok::TokenKind kind = FormatTok->Tok.getKind();
271 if (FormatTok->Type == TT_MacroBlockBegin) {
273 } else if (FormatTok->Type == TT_MacroBlockEnd) {
283 // FIXME: Add parameter whether this can happen - if this happens, we must
284 // be in a non-declaration context.
285 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
287 parseBlock(/*MustBeDeclaration=*/false);
296 case tok::kw_default:
298 if (!SwitchLabelEncountered &&
299 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
301 SwitchLabelEncountered = true;
302 parseStructuralElement();
305 parseStructuralElement();
311 void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
312 // We'll parse forward through the tokens until we hit
313 // a closing brace or eof - note that getNextToken() will
314 // parse macros, so this will magically work inside macro
316 unsigned StoredPosition = Tokens->getPosition();
317 FormatToken *Tok = FormatTok;
318 const FormatToken *PrevTok = getPreviousToken();
319 // Keep a stack of positions of lbrace tokens. We will
320 // update information about whether an lbrace starts a
321 // braced init list or a different block during the loop.
322 SmallVector<FormatToken *, 8> LBraceStack;
323 assert(Tok->Tok.is(tok::l_brace));
325 // Get next non-comment token.
326 FormatToken *NextTok;
327 unsigned ReadTokens = 0;
329 NextTok = Tokens->getNextToken();
331 } while (NextTok->is(tok::comment));
333 switch (Tok->Tok.getKind()) {
335 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok &&
336 PrevTok->is(tok::colon))
337 // In TypeScript's TypeMemberLists, there can be semicolons between the
338 // individual members.
339 Tok->BlockKind = BK_BracedInit;
341 Tok->BlockKind = BK_Unknown;
342 LBraceStack.push_back(Tok);
345 if (LBraceStack.empty())
347 if (LBraceStack.back()->BlockKind == BK_Unknown) {
348 bool ProbablyBracedList = false;
349 if (Style.Language == FormatStyle::LK_Proto) {
350 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
352 // Using OriginalColumn to distinguish between ObjC methods and
353 // binary operators is a bit hacky.
354 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
355 NextTok->OriginalColumn == 0;
357 // If there is a comma, semicolon or right paren after the closing
358 // brace, we assume this is a braced initializer list. Note that
359 // regardless how we mark inner braces here, we will overwrite the
360 // BlockKind later if we parse a braced list (where all blocks
361 // inside are by default braced lists), or when we explicitly detect
362 // blocks (for example while parsing lambdas).
364 // We exclude + and - as they can be ObjC visibility modifiers.
366 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
367 tok::r_paren, tok::r_square, tok::l_brace,
368 tok::l_square, tok::l_paren, tok::ellipsis) ||
369 (NextTok->is(tok::semi) &&
370 (!ExpectClassBody || LBraceStack.size() != 1)) ||
371 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
373 if (ProbablyBracedList) {
374 Tok->BlockKind = BK_BracedInit;
375 LBraceStack.back()->BlockKind = BK_BracedInit;
377 Tok->BlockKind = BK_Block;
378 LBraceStack.back()->BlockKind = BK_Block;
381 LBraceStack.pop_back();
391 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
392 LBraceStack.back()->BlockKind = BK_Block;
399 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
401 // Assume other blocks for all unclosed opening braces.
402 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
403 if (LBraceStack[i]->BlockKind == BK_Unknown)
404 LBraceStack[i]->BlockKind = BK_Block;
407 FormatTok = Tokens->setPosition(StoredPosition);
410 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
412 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
413 "'{' or macro block token expected");
414 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
415 FormatTok->BlockKind = BK_Block;
417 unsigned InitialLevel = Line->Level;
420 if (MacroBlock && FormatTok->is(tok::l_paren))
425 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
429 parseLevel(/*HasOpeningBrace=*/true);
431 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
432 : !FormatTok->is(tok::r_brace)) {
433 Line->Level = InitialLevel;
434 FormatTok->BlockKind = BK_Block;
438 nextToken(); // Munch the closing brace.
440 if (MacroBlock && FormatTok->is(tok::l_paren))
443 if (MunchSemi && FormatTok->Tok.is(tok::semi))
445 Line->Level = InitialLevel;
448 static bool isGoogScope(const UnwrappedLine &Line) {
449 // FIXME: Closure-library specific stuff should not be hard-coded but be
451 if (Line.Tokens.size() < 4)
453 auto I = Line.Tokens.begin();
454 if (I->Tok->TokenText != "goog")
457 if (I->Tok->isNot(tok::period))
460 if (I->Tok->TokenText != "scope")
463 return I->Tok->is(tok::l_paren);
466 static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
467 const FormatToken &InitialToken) {
468 if (InitialToken.is(tok::kw_namespace))
469 return Style.BraceWrapping.AfterNamespace;
470 if (InitialToken.is(tok::kw_class))
471 return Style.BraceWrapping.AfterClass;
472 if (InitialToken.is(tok::kw_union))
473 return Style.BraceWrapping.AfterUnion;
474 if (InitialToken.is(tok::kw_struct))
475 return Style.BraceWrapping.AfterStruct;
479 void UnwrappedLineParser::parseChildBlock() {
480 FormatTok->BlockKind = BK_Block;
484 Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
485 ScopedLineState LineState(*this);
486 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
487 /*MustBeDeclaration=*/false);
488 Line->Level += GoogScope ? 0 : 1;
489 parseLevel(/*HasOpeningBrace=*/true);
490 flushComments(isOnNewLine(*FormatTok));
491 Line->Level -= GoogScope ? 0 : 1;
496 void UnwrappedLineParser::parsePPDirective() {
497 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
498 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
501 if (!FormatTok->Tok.getIdentifierInfo()) {
506 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
511 parsePPIf(/*IfDef=*/false);
515 parsePPIf(/*IfDef=*/true);
532 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
533 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
534 PPStack.push_back(PP_Unreachable);
536 PPStack.push_back(PP_Conditional);
539 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
541 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
542 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
543 PPLevelBranchIndex.push_back(0);
544 PPLevelBranchCount.push_back(0);
546 PPChainBranchIndex.push(0);
547 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
548 conditionalCompilationCondition(Unreachable || Skip);
551 void UnwrappedLineParser::conditionalCompilationAlternative() {
552 if (!PPStack.empty())
554 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
555 if (!PPChainBranchIndex.empty())
556 ++PPChainBranchIndex.top();
557 conditionalCompilationCondition(
558 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
559 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
562 void UnwrappedLineParser::conditionalCompilationEnd() {
563 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
564 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
565 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
566 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
569 // Guard against #endif's without #if.
570 if (PPBranchLevel > 0)
572 if (!PPChainBranchIndex.empty())
573 PPChainBranchIndex.pop();
574 if (!PPStack.empty())
578 void UnwrappedLineParser::parsePPIf(bool IfDef) {
580 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
581 FormatTok->Tok.getLiteralData() != nullptr &&
582 StringRef(FormatTok->Tok.getLiteralData(),
583 FormatTok->Tok.getLength()) == "0") ||
584 FormatTok->Tok.is(tok::kw_false);
585 conditionalCompilationStart(!IfDef && IsLiteralFalse);
589 void UnwrappedLineParser::parsePPElse() {
590 conditionalCompilationAlternative();
594 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
596 void UnwrappedLineParser::parsePPEndIf() {
597 conditionalCompilationEnd();
601 void UnwrappedLineParser::parsePPDefine() {
604 if (FormatTok->Tok.getKind() != tok::identifier) {
609 if (FormatTok->Tok.getKind() == tok::l_paren &&
610 FormatTok->WhitespaceRange.getBegin() ==
611 FormatTok->WhitespaceRange.getEnd()) {
617 // Errors during a preprocessor directive can only affect the layout of the
618 // preprocessor directive, and thus we ignore them. An alternative approach
619 // would be to use the same approach we use on the file level (no
620 // re-indentation if there was a structural error) within the macro
625 void UnwrappedLineParser::parsePPUnknown() {
632 // Here we blacklist certain tokens that are not usually the first token in an
633 // unwrapped line. This is used in attempt to distinguish macro calls without
634 // trailing semicolons from other constructs split to several lines.
635 static bool tokenCanStartNewLine(const clang::Token &Tok) {
636 // Semicolon can be a null-statement, l_square can be a start of a macro or
637 // a C++11 attribute, but this doesn't seem to be common.
638 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
639 Tok.isNot(tok::l_square) &&
640 // Tokens that can only be used as binary operators and a part of
641 // overloaded operator names.
642 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
643 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
644 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
645 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
646 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
647 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
648 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
649 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
650 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
651 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
652 Tok.isNot(tok::lesslessequal) &&
653 // Colon is used in labels, base class lists, initializer lists,
654 // range-based for loops, ternary operator, but should never be the
655 // first token in an unwrapped line.
656 Tok.isNot(tok::colon) &&
657 // 'noexcept' is a trailing annotation.
658 Tok.isNot(tok::kw_noexcept);
661 void UnwrappedLineParser::parseStructuralElement() {
662 assert(!FormatTok->is(tok::l_brace));
663 if (Style.Language == FormatStyle::LK_TableGen &&
664 FormatTok->is(tok::pp_include)) {
666 if (FormatTok->is(tok::string_literal))
671 switch (FormatTok->Tok.getKind()) {
674 if (FormatTok->Tok.is(tok::l_brace)) {
678 switch (FormatTok->Tok.getObjCKeywordID()) {
679 case tok::objc_public:
680 case tok::objc_protected:
681 case tok::objc_package:
682 case tok::objc_private:
683 return parseAccessSpecifier();
684 case tok::objc_interface:
685 case tok::objc_implementation:
686 return parseObjCInterfaceOrImplementation();
687 case tok::objc_protocol:
688 return parseObjCProtocol();
690 return; // Handled by the caller.
691 case tok::objc_optional:
692 case tok::objc_required:
696 case tok::objc_autoreleasepool:
698 if (FormatTok->Tok.is(tok::l_brace)) {
699 if (Style.BraceWrapping.AfterObjCDeclaration)
701 parseBlock(/*MustBeDeclaration=*/false);
706 // This branch isn't strictly necessary (the kw_try case below would
707 // do this too after the tok::at is parsed above). But be explicit.
716 if (FormatTok->is(tok::l_brace)) {
717 FormatTok->Type = TT_InlineASMBrace;
719 while (FormatTok && FormatTok->isNot(tok::eof)) {
720 if (FormatTok->is(tok::r_brace)) {
721 FormatTok->Type = TT_InlineASMBrace;
726 FormatTok->Finalized = true;
731 case tok::kw_namespace:
736 if (FormatTok->Tok.is(tok::kw_namespace)) {
742 case tok::kw_protected:
743 case tok::kw_private:
744 if (Style.Language == FormatStyle::LK_Java ||
745 Style.Language == FormatStyle::LK_JavaScript)
748 parseAccessSpecifier();
755 parseForOrWhileLoop();
763 case tok::kw_default:
776 if (FormatTok->Tok.is(tok::string_literal)) {
778 if (FormatTok->Tok.is(tok::l_brace)) {
779 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
786 if (Style.Language == FormatStyle::LK_JavaScript) {
787 parseJavaScriptEs6ImportExport();
791 case tok::identifier:
792 if (FormatTok->is(TT_ForEachMacro)) {
793 parseForOrWhileLoop();
796 if (FormatTok->is(TT_MacroBlockBegin)) {
797 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
798 /*MunchSemi=*/false);
801 if (Style.Language == FormatStyle::LK_JavaScript &&
802 FormatTok->is(Keywords.kw_import)) {
803 parseJavaScriptEs6ImportExport();
806 if (FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
807 Keywords.kw_slots, Keywords.kw_qslots)) {
809 if (FormatTok->is(tok::colon)) {
815 // In all other cases, parse the declaration.
821 switch (FormatTok->Tok.getKind()) {
824 if (FormatTok->Tok.is(tok::l_brace))
828 // parseEnum falls through and does not yet add an unwrapped line as an
829 // enum definition can start a structural element.
832 // This only applies for C++.
833 if (Style.Language != FormatStyle::LK_Cpp) {
838 case tok::kw_typedef:
840 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
841 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
847 // parseRecord falls through and does not yet add an unwrapped line as a
848 // record declaration or definition can start a structural element.
850 // This does not apply for Java and JavaScript.
851 if (Style.Language == FormatStyle::LK_Java ||
852 Style.Language == FormatStyle::LK_JavaScript) {
853 if (FormatTok->is(tok::semi))
861 // In Java, classes have an implicit static member "class".
862 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
863 FormatTok->is(tok::kw_class))
865 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
866 FormatTok->Tok.getIdentifierInfo())
867 // JavaScript only has pseudo keywords, all keywords are allowed to
868 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
881 case tok::kw_operator:
883 if (FormatTok->isBinaryOperator())
888 if (FormatTok->Tok.isAnyIdentifier() ||
889 FormatTok->isSimpleTypeSpecifier())
891 if (FormatTok->is(tok::l_paren))
893 if (FormatTok->is(tok::l_brace))
897 if (!tryToParseBracedList()) {
898 // A block outside of parentheses must be the last part of a
899 // structural element.
900 // FIXME: Figure out cases where this is not true, and add projections
901 // for them (the one we know is missing are lambdas).
902 if (Style.BraceWrapping.AfterFunction)
904 FormatTok->Type = TT_FunctionLBrace;
905 parseBlock(/*MustBeDeclaration=*/false);
909 // Otherwise this was a braced init list, and the structural
910 // element continues.
913 // We arrive here when parsing function-try blocks.
916 case tok::identifier: {
917 if (FormatTok->is(TT_MacroBlockEnd)) {
922 // Parse function literal unless 'function' is the first token in a line
923 // in which case this should be treated as a free-standing function.
924 if (Style.Language == FormatStyle::LK_JavaScript &&
925 FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) {
926 tryToParseJSFunction();
929 if ((Style.Language == FormatStyle::LK_JavaScript ||
930 Style.Language == FormatStyle::LK_Java) &&
931 FormatTok->is(Keywords.kw_interface)) {
937 StringRef Text = FormatTok->TokenText;
939 if (Line->Tokens.size() == 1 &&
940 // JS doesn't have macros, and within classes colons indicate fields,
942 Style.Language != FormatStyle::LK_JavaScript) {
943 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
947 // Recognize function-like macro usages without trailing semicolon as
948 // well as free-standing macros like Q_OBJECT.
949 bool FunctionLike = FormatTok->is(tok::l_paren);
953 bool FollowedByNewline =
954 CommentsBeforeNextToken.empty()
955 ? FormatTok->NewlinesBefore > 0
956 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
958 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
959 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
967 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
968 // TT_JsFatArrow. The always start an expression or a child block if
969 // followed by a curly.
970 if (FormatTok->is(TT_JsFatArrow)) {
972 if (FormatTok->is(tok::l_brace))
978 if (FormatTok->Tok.is(tok::l_brace)) {
995 bool UnwrappedLineParser::tryToParseLambda() {
996 if (Style.Language != FormatStyle::LK_Cpp) {
1000 const FormatToken* Previous = getPreviousToken();
1002 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1004 Previous->closesScope() || Previous->isSimpleTypeSpecifier())) {
1008 assert(FormatTok->is(tok::l_square));
1009 FormatToken &LSquare = *FormatTok;
1010 if (!tryToParseLambdaIntroducer())
1013 while (FormatTok->isNot(tok::l_brace)) {
1014 if (FormatTok->isSimpleTypeSpecifier()) {
1018 switch (FormatTok->Tok.getKind()) {
1030 case tok::identifier:
1031 case tok::numeric_constant:
1032 case tok::coloncolon:
1033 case tok::kw_mutable:
1037 FormatTok->Type = TT_LambdaArrow;
1044 LSquare.Type = TT_LambdaLSquare;
1049 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1051 if (FormatTok->is(tok::equal)) {
1053 if (FormatTok->is(tok::r_square)) {
1057 if (FormatTok->isNot(tok::comma))
1060 } else if (FormatTok->is(tok::amp)) {
1062 if (FormatTok->is(tok::r_square)) {
1066 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
1069 if (FormatTok->is(tok::comma))
1071 } else if (FormatTok->is(tok::r_square)) {
1076 if (FormatTok->is(tok::amp))
1078 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
1081 if (FormatTok->is(tok::ellipsis))
1083 if (FormatTok->is(tok::comma)) {
1085 } else if (FormatTok->is(tok::r_square)) {
1095 void UnwrappedLineParser::tryToParseJSFunction() {
1098 // Consume function name.
1099 if (FormatTok->is(tok::identifier))
1102 if (FormatTok->isNot(tok::l_paren))
1105 // Parse formal parameter list.
1108 if (FormatTok->is(tok::colon)) {
1109 // Parse a type definition.
1112 // Eat the type declaration. For braced inline object types, balance braces,
1113 // otherwise just parse until finding an l_brace for the function body.
1114 if (FormatTok->is(tok::l_brace))
1115 tryToParseBracedList();
1117 while (FormatTok->isNot(tok::l_brace) && !eof())
1124 bool UnwrappedLineParser::tryToParseBracedList() {
1125 if (FormatTok->BlockKind == BK_Unknown)
1126 calculateBraceTypes();
1127 assert(FormatTok->BlockKind != BK_Unknown);
1128 if (FormatTok->BlockKind == BK_Block)
1134 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1135 bool HasError = false;
1138 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1139 // replace this by using parseAssigmentExpression() inside.
1141 if (Style.Language == FormatStyle::LK_JavaScript) {
1142 if (FormatTok->is(Keywords.kw_function)) {
1143 tryToParseJSFunction();
1146 if (FormatTok->is(TT_JsFatArrow)) {
1148 // Fat arrows can be followed by simple expressions or by child blocks
1150 if (FormatTok->is(tok::l_brace)) {
1156 switch (FormatTok->Tok.getKind()) {
1159 if (FormatTok->is(tok::l_brace)) {
1167 // Assume there are no blocks inside a braced init list apart
1168 // from the ones we explicitly parse out (like lambdas).
1169 FormatTok->BlockKind = BK_BracedInit;
1174 // JavaScript can just have free standing methods and getters/setters in
1175 // object literals. Detect them by a "{" following ")".
1176 if (Style.Language == FormatStyle::LK_JavaScript) {
1177 if (FormatTok->is(tok::l_brace))
1186 // JavaScript (or more precisely TypeScript) can have semicolons in braced
1187 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1188 // used for error recovery if we have otherwise determined that this is
1190 if (Style.Language == FormatStyle::LK_JavaScript) {
1195 if (!ContinueOnSemicolons)
1210 void UnwrappedLineParser::parseParens() {
1211 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
1214 switch (FormatTok->Tok.getKind()) {
1217 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1224 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1230 if (!tryToParseBracedList())
1235 if (FormatTok->Tok.is(tok::l_brace))
1238 case tok::identifier:
1239 if (Style.Language == FormatStyle::LK_JavaScript &&
1240 FormatTok->is(Keywords.kw_function))
1241 tryToParseJSFunction();
1252 void UnwrappedLineParser::parseSquare() {
1253 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1254 if (tryToParseLambda())
1257 switch (FormatTok->Tok.getKind()) {
1265 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1270 case tok::l_brace: {
1271 if (!tryToParseBracedList())
1277 if (FormatTok->Tok.is(tok::l_brace))
1287 void UnwrappedLineParser::parseIfThenElse() {
1288 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
1290 if (FormatTok->Tok.is(tok::l_paren))
1292 bool NeedsUnwrappedLine = false;
1293 if (FormatTok->Tok.is(tok::l_brace)) {
1294 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1295 parseBlock(/*MustBeDeclaration=*/false);
1296 if (Style.BraceWrapping.BeforeElse)
1299 NeedsUnwrappedLine = true;
1303 parseStructuralElement();
1306 if (FormatTok->Tok.is(tok::kw_else)) {
1308 if (FormatTok->Tok.is(tok::l_brace)) {
1309 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1310 parseBlock(/*MustBeDeclaration=*/false);
1312 } else if (FormatTok->Tok.is(tok::kw_if)) {
1317 parseStructuralElement();
1320 } else if (NeedsUnwrappedLine) {
1325 void UnwrappedLineParser::parseTryCatch() {
1326 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
1328 bool NeedsUnwrappedLine = false;
1329 if (FormatTok->is(tok::colon)) {
1330 // We are in a function try block, what comes is an initializer list.
1332 while (FormatTok->is(tok::identifier)) {
1334 if (FormatTok->is(tok::l_paren))
1336 if (FormatTok->is(tok::comma))
1340 // Parse try with resource.
1341 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1344 if (FormatTok->is(tok::l_brace)) {
1345 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1346 parseBlock(/*MustBeDeclaration=*/false);
1347 if (Style.BraceWrapping.BeforeCatch) {
1350 NeedsUnwrappedLine = true;
1352 } else if (!FormatTok->is(tok::kw_catch)) {
1353 // The C++ standard requires a compound-statement after a try.
1354 // If there's none, we try to assume there's a structuralElement
1355 // and try to continue.
1358 parseStructuralElement();
1362 if (FormatTok->is(tok::at))
1364 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1365 tok::kw___finally) ||
1366 ((Style.Language == FormatStyle::LK_Java ||
1367 Style.Language == FormatStyle::LK_JavaScript) &&
1368 FormatTok->is(Keywords.kw_finally)) ||
1369 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1370 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1373 while (FormatTok->isNot(tok::l_brace)) {
1374 if (FormatTok->is(tok::l_paren)) {
1378 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
1382 NeedsUnwrappedLine = false;
1383 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1384 parseBlock(/*MustBeDeclaration=*/false);
1385 if (Style.BraceWrapping.BeforeCatch)
1388 NeedsUnwrappedLine = true;
1390 if (NeedsUnwrappedLine)
1394 void UnwrappedLineParser::parseNamespace() {
1395 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
1397 const FormatToken &InitialToken = *FormatTok;
1399 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
1401 if (FormatTok->Tok.is(tok::l_brace)) {
1402 if (ShouldBreakBeforeBrace(Style, InitialToken))
1405 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1406 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1407 DeclarationScopeStack.size() > 1);
1408 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
1409 // Munch the semicolon after a namespace. This is more common than one would
1410 // think. Puttin the semicolon into its own line is very ugly.
1411 if (FormatTok->Tok.is(tok::semi))
1415 // FIXME: Add error handling.
1418 void UnwrappedLineParser::parseNew() {
1419 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1421 if (Style.Language != FormatStyle::LK_Java)
1424 // In Java, we can parse everything up to the parens, which aren't optional.
1426 // There should not be a ;, { or } before the new's open paren.
1427 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1430 // Consume the parens.
1431 if (FormatTok->is(tok::l_paren)) {
1434 // If there is a class body of an anonymous class, consume that as child.
1435 if (FormatTok->is(tok::l_brace))
1443 void UnwrappedLineParser::parseForOrWhileLoop() {
1444 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
1445 "'for', 'while' or foreach macro expected");
1447 if (FormatTok->Tok.is(tok::l_paren))
1449 if (FormatTok->Tok.is(tok::l_brace)) {
1450 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1451 parseBlock(/*MustBeDeclaration=*/false);
1456 parseStructuralElement();
1461 void UnwrappedLineParser::parseDoWhile() {
1462 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
1464 if (FormatTok->Tok.is(tok::l_brace)) {
1465 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1466 parseBlock(/*MustBeDeclaration=*/false);
1467 if (Style.BraceWrapping.IndentBraces)
1472 parseStructuralElement();
1476 // FIXME: Add error handling.
1477 if (!FormatTok->Tok.is(tok::kw_while)) {
1483 parseStructuralElement();
1486 void UnwrappedLineParser::parseLabel() {
1488 unsigned OldLineLevel = Line->Level;
1489 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1491 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
1492 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1493 parseBlock(/*MustBeDeclaration=*/false);
1494 if (FormatTok->Tok.is(tok::kw_break)) {
1495 if (Style.BraceWrapping.AfterControlStatement)
1497 parseStructuralElement();
1501 if (FormatTok->is(tok::semi))
1505 Line->Level = OldLineLevel;
1508 void UnwrappedLineParser::parseCaseLabel() {
1509 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
1510 // FIXME: fix handling of complex expressions here.
1513 } while (!eof() && !FormatTok->Tok.is(tok::colon));
1517 void UnwrappedLineParser::parseSwitch() {
1518 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
1520 if (FormatTok->Tok.is(tok::l_paren))
1522 if (FormatTok->Tok.is(tok::l_brace)) {
1523 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1524 parseBlock(/*MustBeDeclaration=*/false);
1529 parseStructuralElement();
1534 void UnwrappedLineParser::parseAccessSpecifier() {
1536 // Understand Qt's slots.
1537 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
1539 // Otherwise, we don't know what it is, and we'd better keep the next token.
1540 if (FormatTok->Tok.is(tok::colon))
1545 bool UnwrappedLineParser::parseEnum() {
1546 // Won't be 'enum' for NS_ENUMs.
1547 if (FormatTok->Tok.is(tok::kw_enum))
1550 // In TypeScript, "enum" can also be used as property name, e.g. in interface
1551 // declarations. An "enum" keyword followed by a colon would be a syntax
1552 // error and thus assume it is just an identifier.
1553 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok->is(tok::colon))
1556 // Eat up enum class ...
1557 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1560 while (FormatTok->Tok.getIdentifierInfo() ||
1561 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1562 tok::greater, tok::comma, tok::question)) {
1564 // We can have macros or attributes in between 'enum' and the enum name.
1565 if (FormatTok->is(tok::l_paren))
1567 if (FormatTok->is(tok::identifier)) {
1569 // If there are two identifiers in a row, this is likely an elaborate
1570 // return type. In Java, this can be "implements", etc.
1571 if (Style.Language == FormatStyle::LK_Cpp &&
1572 FormatTok->is(tok::identifier))
1577 // Just a declaration or something is wrong.
1578 if (FormatTok->isNot(tok::l_brace))
1580 FormatTok->BlockKind = BK_Block;
1582 if (Style.Language == FormatStyle::LK_Java) {
1583 // Java enums are different.
1584 parseJavaEnumBody();
1587 if (Style.Language == FormatStyle::LK_Proto) {
1588 parseBlock(/*MustBeDeclaration=*/true);
1593 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1595 if (FormatTok->is(tok::semi))
1601 // There is no addUnwrappedLine() here so that we fall through to parsing a
1602 // structural element afterwards. Thus, in "enum A {} n, m;",
1603 // "} n, m;" will end up in one unwrapped line.
1606 void UnwrappedLineParser::parseJavaEnumBody() {
1607 // Determine whether the enum is simple, i.e. does not have a semicolon or
1608 // constants with class bodies. Simple enums can be formatted like braced
1609 // lists, contracted to a single line, etc.
1610 unsigned StoredPosition = Tokens->getPosition();
1611 bool IsSimple = true;
1612 FormatToken *Tok = Tokens->getNextToken();
1614 if (Tok->is(tok::r_brace))
1616 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1620 // FIXME: This will also mark enums with braces in the arguments to enum
1621 // constants as "not simple". This is probably fine in practice, though.
1622 Tok = Tokens->getNextToken();
1624 FormatTok = Tokens->setPosition(StoredPosition);
1632 // Parse the body of a more complex enum.
1633 // First add a line for everything up to the "{".
1638 // Parse the enum constants.
1640 if (FormatTok->is(tok::l_brace)) {
1641 // Parse the constant's class body.
1642 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1643 /*MunchSemi=*/false);
1644 } else if (FormatTok->is(tok::l_paren)) {
1646 } else if (FormatTok->is(tok::comma)) {
1649 } else if (FormatTok->is(tok::semi)) {
1653 } else if (FormatTok->is(tok::r_brace)) {
1661 // Parse the class body after the enum's ";" if any.
1662 parseLevel(/*HasOpeningBrace=*/true);
1668 void UnwrappedLineParser::parseRecord() {
1669 const FormatToken &InitialToken = *FormatTok;
1672 // The actual identifier can be a nested name specifier, and in macros
1673 // it is often token-pasted.
1674 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
1675 tok::kw___attribute, tok::kw___declspec,
1677 ((Style.Language == FormatStyle::LK_Java ||
1678 Style.Language == FormatStyle::LK_JavaScript) &&
1679 FormatTok->isOneOf(tok::period, tok::comma))) {
1680 bool IsNonMacroIdentifier =
1681 FormatTok->is(tok::identifier) &&
1682 FormatTok->TokenText != FormatTok->TokenText.upper();
1684 // We can have macros or attributes in between 'class' and the class name.
1685 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
1689 // Note that parsing away template declarations here leads to incorrectly
1690 // accepting function declarations as record declarations.
1691 // In general, we cannot solve this problem. Consider:
1692 // class A<int> B() {}
1693 // which can be a function definition or a class definition when B() is a
1694 // macro. If we find enough real-world cases where this is a problem, we
1695 // can parse for the 'template' keyword in the beginning of the statement,
1696 // and thus rule out the record production in case there is no template
1697 // (this would still leave us with an ambiguity between template function
1698 // and class declarations).
1699 if (FormatTok->isOneOf(tok::colon, tok::less)) {
1701 if (FormatTok->is(tok::l_brace)) {
1702 calculateBraceTypes(/*ExpectClassBody=*/true);
1703 if (!tryToParseBracedList())
1706 if (FormatTok->Tok.is(tok::semi))
1711 if (FormatTok->Tok.is(tok::l_brace)) {
1712 if (ShouldBreakBeforeBrace(Style, InitialToken))
1715 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1716 /*MunchSemi=*/false);
1718 // There is no addUnwrappedLine() here so that we fall through to parsing a
1719 // structural element afterwards. Thus, in "class A {} n, m;",
1720 // "} n, m;" will end up in one unwrapped line.
1723 void UnwrappedLineParser::parseObjCProtocolList() {
1724 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
1727 while (!eof() && FormatTok->Tok.isNot(tok::greater));
1728 nextToken(); // Skip '>'.
1731 void UnwrappedLineParser::parseObjCUntilAtEnd() {
1733 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
1738 if (FormatTok->is(tok::l_brace)) {
1739 parseBlock(/*MustBeDeclaration=*/false);
1740 // In ObjC interfaces, nothing should be following the "}".
1742 } else if (FormatTok->is(tok::r_brace)) {
1743 // Ignore stray "}". parseStructuralElement doesn't consume them.
1747 parseStructuralElement();
1752 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
1754 nextToken(); // interface name
1756 // @interface can be followed by either a base class, or a category.
1757 if (FormatTok->Tok.is(tok::colon)) {
1759 nextToken(); // base class name
1760 } else if (FormatTok->Tok.is(tok::l_paren))
1761 // Skip category, if present.
1764 if (FormatTok->Tok.is(tok::less))
1765 parseObjCProtocolList();
1767 if (FormatTok->Tok.is(tok::l_brace)) {
1768 if (Style.BraceWrapping.AfterObjCDeclaration)
1770 parseBlock(/*MustBeDeclaration=*/true);
1773 // With instance variables, this puts '}' on its own line. Without instance
1774 // variables, this ends the @interface line.
1777 parseObjCUntilAtEnd();
1780 void UnwrappedLineParser::parseObjCProtocol() {
1782 nextToken(); // protocol name
1784 if (FormatTok->Tok.is(tok::less))
1785 parseObjCProtocolList();
1787 // Check for protocol declaration.
1788 if (FormatTok->Tok.is(tok::semi)) {
1790 return addUnwrappedLine();
1794 parseObjCUntilAtEnd();
1797 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1798 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
1801 // Consume the "default" in "export default class/function".
1802 if (FormatTok->is(tok::kw_default))
1805 // Consume "function" and "default function", so that these get parsed as
1806 // free-standing JS functions, i.e. do not require a trailing semicolon.
1807 if (FormatTok->is(Keywords.kw_function)) {
1812 // Consume the "abstract" in "export abstract class".
1813 if (FormatTok->is(Keywords.kw_abstract))
1816 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, tok::kw_enum,
1817 Keywords.kw_interface, Keywords.kw_let,
1819 return; // Fall through to parsing the corresponding structure.
1821 while (!eof() && FormatTok->isNot(tok::semi)) {
1822 if (FormatTok->is(tok::l_brace)) {
1823 FormatTok->BlockKind = BK_Block;
1831 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1832 StringRef Prefix = "") {
1833 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1834 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1835 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1836 E = Line.Tokens.end();
1838 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
1840 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1841 E = Line.Tokens.end();
1843 const UnwrappedLineNode &Node = *I;
1844 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1845 I = Node.Children.begin(),
1846 E = Node.Children.end();
1848 printDebugInfo(*I, "\nChild: ");
1851 llvm::dbgs() << "\n";
1854 void UnwrappedLineParser::addUnwrappedLine() {
1855 if (Line->Tokens.empty())
1858 if (CurrentLines == &Lines)
1859 printDebugInfo(*Line);
1861 CurrentLines->push_back(std::move(*Line));
1862 Line->Tokens.clear();
1863 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
1864 CurrentLines->append(
1865 std::make_move_iterator(PreprocessorDirectives.begin()),
1866 std::make_move_iterator(PreprocessorDirectives.end()));
1867 PreprocessorDirectives.clear();
1871 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
1873 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
1874 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1875 FormatTok.NewlinesBefore > 0;
1878 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1879 bool JustComments = Line->Tokens.empty();
1880 for (SmallVectorImpl<FormatToken *>::const_iterator
1881 I = CommentsBeforeNextToken.begin(),
1882 E = CommentsBeforeNextToken.end();
1884 if (isOnNewLine(**I) && JustComments)
1888 if (NewlineBeforeNext && JustComments)
1890 CommentsBeforeNextToken.clear();
1893 void UnwrappedLineParser::nextToken() {
1896 flushComments(isOnNewLine(*FormatTok));
1897 pushToken(FormatTok);
1901 const FormatToken *UnwrappedLineParser::getPreviousToken() {
1902 // FIXME: This is a dirty way to access the previous token. Find a better
1904 if (!Line || Line->Tokens.empty())
1906 return Line->Tokens.back().Tok;
1909 void UnwrappedLineParser::readToken() {
1910 bool CommentsInCurrentLine = true;
1912 FormatTok = Tokens->getNextToken();
1914 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1915 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
1916 // If there is an unfinished unwrapped line, we flush the preprocessor
1917 // directives only after that unwrapped line was finished later.
1918 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
1919 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
1920 // Comments stored before the preprocessor directive need to be output
1921 // before the preprocessor directive, at the same level as the
1922 // preprocessor directive, as we consider them to apply to the directive.
1923 flushComments(isOnNewLine(*FormatTok));
1926 while (FormatTok->Type == TT_ConflictStart ||
1927 FormatTok->Type == TT_ConflictEnd ||
1928 FormatTok->Type == TT_ConflictAlternative) {
1929 if (FormatTok->Type == TT_ConflictStart) {
1930 conditionalCompilationStart(/*Unreachable=*/false);
1931 } else if (FormatTok->Type == TT_ConflictAlternative) {
1932 conditionalCompilationAlternative();
1933 } else if (FormatTok->Type == TT_ConflictEnd) {
1934 conditionalCompilationEnd();
1936 FormatTok = Tokens->getNextToken();
1937 FormatTok->MustBreakBefore = true;
1940 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1941 !Line->InPPDirective) {
1945 if (!FormatTok->Tok.is(tok::comment))
1947 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
1948 CommentsInCurrentLine = false;
1950 if (CommentsInCurrentLine) {
1951 pushToken(FormatTok);
1953 CommentsBeforeNextToken.push_back(FormatTok);
1958 void UnwrappedLineParser::pushToken(FormatToken *Tok) {
1959 Line->Tokens.push_back(UnwrappedLineNode(Tok));
1960 if (MustBreakBeforeNextToken) {
1961 Line->Tokens.back().Tok->MustBreakBefore = true;
1962 MustBreakBeforeNextToken = false;
1966 } // end namespace format
1967 } // end namespace clang