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.BreakBeforeBraces == FormatStyle::BS_Allman) {
158 Parser->addUnwrappedLine();
159 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
160 Parser->addUnwrappedLine();
164 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
168 unsigned OldLineLevel;
173 class IndexedTokenSource : public FormatTokenSource {
175 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
176 : Tokens(Tokens), Position(-1) {}
178 FormatToken *getNextToken() override {
180 return Tokens[Position];
183 unsigned getPosition() override {
184 assert(Position >= 0);
188 FormatToken *setPosition(unsigned P) override {
190 return Tokens[Position];
193 void reset() { Position = -1; }
196 ArrayRef<FormatToken *> Tokens;
200 } // end anonymous namespace
202 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
203 const AdditionalKeywords &Keywords,
204 ArrayRef<FormatToken *> Tokens,
205 UnwrappedLineConsumer &Callback)
206 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
207 CurrentLines(&Lines), Style(Style), Keywords(Keywords), Tokens(nullptr),
208 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
210 void UnwrappedLineParser::reset() {
212 Line.reset(new UnwrappedLine);
213 CommentsBeforeNextToken.clear();
215 MustBreakBeforeNextToken = false;
216 PreprocessorDirectives.clear();
217 CurrentLines = &Lines;
218 DeclarationScopeStack.clear();
222 void UnwrappedLineParser::parse() {
223 IndexedTokenSource TokenSource(AllTokens);
225 DEBUG(llvm::dbgs() << "----\n");
227 Tokens = &TokenSource;
232 // Create line with eof token.
233 pushToken(FormatTok);
236 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
239 Callback.consumeUnwrappedLine(*I);
241 Callback.finishRun();
243 while (!PPLevelBranchIndex.empty() &&
244 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
245 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
246 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
248 if (!PPLevelBranchIndex.empty()) {
249 ++PPLevelBranchIndex.back();
250 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
251 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
253 } while (!PPLevelBranchIndex.empty());
256 void UnwrappedLineParser::parseFile() {
257 // The top-level context in a file always has declarations, except for pre-
258 // processor directives and JavaScript files.
259 bool MustBeDeclaration =
260 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
261 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
263 parseLevel(/*HasOpeningBrace=*/false);
264 // Make sure to format the remaining tokens.
269 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
270 bool SwitchLabelEncountered = false;
272 switch (FormatTok->Tok.getKind()) {
278 // FIXME: Add parameter whether this can happen - if this happens, we must
279 // be in a non-declaration context.
280 parseBlock(/*MustBeDeclaration=*/false);
289 case tok::kw_default:
291 if (!SwitchLabelEncountered &&
292 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
294 SwitchLabelEncountered = true;
295 parseStructuralElement();
298 parseStructuralElement();
304 void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
305 // We'll parse forward through the tokens until we hit
306 // a closing brace or eof - note that getNextToken() will
307 // parse macros, so this will magically work inside macro
309 unsigned StoredPosition = Tokens->getPosition();
310 FormatToken *Tok = FormatTok;
311 // Keep a stack of positions of lbrace tokens. We will
312 // update information about whether an lbrace starts a
313 // braced init list or a different block during the loop.
314 SmallVector<FormatToken *, 8> LBraceStack;
315 assert(Tok->Tok.is(tok::l_brace));
317 // Get next none-comment token.
318 FormatToken *NextTok;
319 unsigned ReadTokens = 0;
321 NextTok = Tokens->getNextToken();
323 } while (NextTok->is(tok::comment));
325 switch (Tok->Tok.getKind()) {
327 Tok->BlockKind = BK_Unknown;
328 LBraceStack.push_back(Tok);
331 if (!LBraceStack.empty()) {
332 if (LBraceStack.back()->BlockKind == BK_Unknown) {
333 bool ProbablyBracedList = false;
334 if (Style.Language == FormatStyle::LK_Proto) {
335 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
337 // Using OriginalColumn to distinguish between ObjC methods and
338 // binary operators is a bit hacky.
339 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
340 NextTok->OriginalColumn == 0;
342 // If there is a comma, semicolon or right paren after the closing
343 // brace, we assume this is a braced initializer list. Note that
344 // regardless how we mark inner braces here, we will overwrite the
345 // BlockKind later if we parse a braced list (where all blocks
346 // inside are by default braced lists), or when we explicitly detect
347 // blocks (for example while parsing lambdas).
349 // We exclude + and - as they can be ObjC visibility modifiers.
351 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
352 tok::r_paren, tok::r_square, tok::l_brace,
353 tok::l_paren, tok::ellipsis) ||
354 (NextTok->is(tok::semi) &&
355 (!ExpectClassBody || LBraceStack.size() != 1)) ||
356 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
358 if (ProbablyBracedList) {
359 Tok->BlockKind = BK_BracedInit;
360 LBraceStack.back()->BlockKind = BK_BracedInit;
362 Tok->BlockKind = BK_Block;
363 LBraceStack.back()->BlockKind = BK_Block;
366 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;
413 nextToken(); // Munch the closing brace.
414 if (MunchSemi && FormatTok->Tok.is(tok::semi))
416 Line->Level = InitialLevel;
419 static bool isGoogScope(const UnwrappedLine &Line) {
420 // FIXME: Closure-library specific stuff should not be hard-coded but be
422 if (Line.Tokens.size() < 4)
424 auto I = Line.Tokens.begin();
425 if (I->Tok->TokenText != "goog")
428 if (I->Tok->isNot(tok::period))
431 if (I->Tok->TokenText != "scope")
434 return I->Tok->is(tok::l_paren);
437 static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
438 const FormatToken &InitialToken) {
439 switch (Style.BreakBeforeBraces) {
440 case FormatStyle::BS_Linux:
441 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
442 case FormatStyle::BS_Allman:
443 case FormatStyle::BS_GNU:
450 void UnwrappedLineParser::parseChildBlock() {
451 FormatTok->BlockKind = BK_Block;
455 Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
456 ScopedLineState LineState(*this);
457 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
458 /*MustBeDeclaration=*/false);
459 Line->Level += GoogScope ? 0 : 1;
460 parseLevel(/*HasOpeningBrace=*/true);
461 flushComments(isOnNewLine(*FormatTok));
462 Line->Level -= GoogScope ? 0 : 1;
467 void UnwrappedLineParser::parsePPDirective() {
468 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
469 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
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 FormatTok->Tok.getLiteralData() != nullptr &&
553 StringRef(FormatTok->Tok.getLiteralData(),
554 FormatTok->Tok.getLength()) == "0") ||
555 FormatTok->Tok.is(tok::kw_false);
556 conditionalCompilationStart(!IfDef && IsLiteralFalse);
560 void UnwrappedLineParser::parsePPElse() {
561 conditionalCompilationAlternative();
565 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
567 void UnwrappedLineParser::parsePPEndIf() {
568 conditionalCompilationEnd();
572 void UnwrappedLineParser::parsePPDefine() {
575 if (FormatTok->Tok.getKind() != tok::identifier) {
580 if (FormatTok->Tok.getKind() == tok::l_paren &&
581 FormatTok->WhitespaceRange.getBegin() ==
582 FormatTok->WhitespaceRange.getEnd()) {
588 // Errors during a preprocessor directive can only affect the layout of the
589 // preprocessor directive, and thus we ignore them. An alternative approach
590 // would be to use the same approach we use on the file level (no
591 // re-indentation if there was a structural error) within the macro
596 void UnwrappedLineParser::parsePPUnknown() {
603 // Here we blacklist certain tokens that are not usually the first token in an
604 // unwrapped line. This is used in attempt to distinguish macro calls without
605 // trailing semicolons from other constructs split to several lines.
606 static bool tokenCanStartNewLine(const clang::Token &Tok) {
607 // Semicolon can be a null-statement, l_square can be a start of a macro or
608 // a C++11 attribute, but this doesn't seem to be common.
609 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
610 Tok.isNot(tok::l_square) &&
611 // Tokens that can only be used as binary operators and a part of
612 // overloaded operator names.
613 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
614 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
615 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
616 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
617 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
618 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
619 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
620 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
621 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
622 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
623 Tok.isNot(tok::lesslessequal) &&
624 // Colon is used in labels, base class lists, initializer lists,
625 // range-based for loops, ternary operator, but should never be the
626 // first token in an unwrapped line.
627 Tok.isNot(tok::colon) &&
628 // 'noexcept' is a trailing annotation.
629 Tok.isNot(tok::kw_noexcept);
632 void UnwrappedLineParser::parseStructuralElement() {
633 assert(!FormatTok->Tok.is(tok::l_brace));
634 switch (FormatTok->Tok.getKind()) {
637 if (FormatTok->Tok.is(tok::l_brace)) {
641 switch (FormatTok->Tok.getObjCKeywordID()) {
642 case tok::objc_public:
643 case tok::objc_protected:
644 case tok::objc_package:
645 case tok::objc_private:
646 return parseAccessSpecifier();
647 case tok::objc_interface:
648 case tok::objc_implementation:
649 return parseObjCInterfaceOrImplementation();
650 case tok::objc_protocol:
651 return parseObjCProtocol();
653 return; // Handled by the caller.
654 case tok::objc_optional:
655 case tok::objc_required:
659 case tok::objc_autoreleasepool:
661 if (FormatTok->Tok.is(tok::l_brace)) {
662 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
663 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
665 parseBlock(/*MustBeDeclaration=*/false);
670 // This branch isn't strictly necessary (the kw_try case below would
671 // do this too after the tok::at is parsed above). But be explicit.
680 if (FormatTok->is(tok::l_brace)) {
681 FormatTok->Type = TT_InlineASMBrace;
683 while (FormatTok && FormatTok->isNot(tok::eof)) {
684 if (FormatTok->is(tok::r_brace)) {
685 FormatTok->Type = TT_InlineASMBrace;
690 FormatTok->Finalized = true;
695 case tok::kw_namespace:
700 if (FormatTok->Tok.is(tok::kw_namespace)) {
706 case tok::kw_protected:
707 case tok::kw_private:
708 if (Style.Language == FormatStyle::LK_Java ||
709 Style.Language == FormatStyle::LK_JavaScript)
712 parseAccessSpecifier();
719 parseForOrWhileLoop();
727 case tok::kw_default:
740 if (FormatTok->Tok.is(tok::string_literal)) {
742 if (FormatTok->Tok.is(tok::l_brace)) {
743 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
750 if (Style.Language == FormatStyle::LK_JavaScript) {
751 parseJavaScriptEs6ImportExport();
755 case tok::identifier:
756 if (FormatTok->is(TT_ForEachMacro)) {
757 parseForOrWhileLoop();
760 if (Style.Language == FormatStyle::LK_JavaScript &&
761 FormatTok->is(Keywords.kw_import)) {
762 parseJavaScriptEs6ImportExport();
765 if (FormatTok->is(Keywords.kw_signals)) {
767 if (FormatTok->is(tok::colon)) {
773 // In all other cases, parse the declaration.
779 switch (FormatTok->Tok.getKind()) {
782 if (FormatTok->Tok.is(tok::l_brace))
786 // parseEnum falls through and does not yet add an unwrapped line as an
787 // enum definition can start a structural element.
789 // This does not apply for Java and JavaScript.
790 if (Style.Language == FormatStyle::LK_Java ||
791 Style.Language == FormatStyle::LK_JavaScript) {
796 case tok::kw_typedef:
798 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
799 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
805 // parseRecord falls through and does not yet add an unwrapped line as a
806 // record declaration or definition can start a structural element.
808 // This does not apply for Java and JavaScript.
809 if (Style.Language == FormatStyle::LK_Java ||
810 Style.Language == FormatStyle::LK_JavaScript) {
817 // In Java, classes have an implicit static member "class".
818 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
819 FormatTok->is(tok::kw_class))
834 if (FormatTok->Tok.isAnyIdentifier() ||
835 FormatTok->isSimpleTypeSpecifier())
837 if (FormatTok->is(tok::l_paren))
839 if (FormatTok->is(tok::l_brace))
843 if (!tryToParseBracedList()) {
844 // A block outside of parentheses must be the last part of a
845 // structural element.
846 // FIXME: Figure out cases where this is not true, and add projections
847 // for them (the one we know is missing are lambdas).
848 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
850 FormatTok->Type = TT_FunctionLBrace;
851 parseBlock(/*MustBeDeclaration=*/false);
855 // Otherwise this was a braced init list, and the structural
856 // element continues.
859 // We arrive here when parsing function-try blocks.
862 case tok::identifier: {
863 // Parse function literal unless 'function' is the first token in a line
864 // in which case this should be treated as a free-standing function.
865 if (Style.Language == FormatStyle::LK_JavaScript &&
866 FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) {
867 tryToParseJSFunction();
870 if ((Style.Language == FormatStyle::LK_JavaScript ||
871 Style.Language == FormatStyle::LK_Java) &&
872 FormatTok->is(Keywords.kw_interface)) {
878 StringRef Text = FormatTok->TokenText;
880 if (Line->Tokens.size() == 1 &&
881 // JS doesn't have macros, and within classes colons indicate fields,
883 Style.Language != FormatStyle::LK_JavaScript) {
884 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
888 // Recognize function-like macro usages without trailing semicolon as
889 // well as free-standing macros like Q_OBJECT.
890 bool FunctionLike = FormatTok->is(tok::l_paren);
894 bool FollowedByNewline =
895 CommentsBeforeNextToken.empty()
896 ? FormatTok->NewlinesBefore > 0
897 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
899 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
900 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
908 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
909 // TT_JsFatArrow. The always start an expression or a child block if
910 // followed by a curly.
911 if (FormatTok->is(TT_JsFatArrow)) {
913 if (FormatTok->is(tok::l_brace))
919 if (FormatTok->Tok.is(tok::l_brace)) {
936 bool UnwrappedLineParser::tryToParseLambda() {
937 if (Style.Language != FormatStyle::LK_Cpp) {
941 // FIXME: This is a dirty way to access the previous token. Find a better
943 if (!Line->Tokens.empty() &&
944 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
945 tok::kw_new, tok::kw_delete) ||
946 Line->Tokens.back().Tok->closesScope() ||
947 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
951 assert(FormatTok->is(tok::l_square));
952 FormatToken &LSquare = *FormatTok;
953 if (!tryToParseLambdaIntroducer())
956 while (FormatTok->isNot(tok::l_brace)) {
957 if (FormatTok->isSimpleTypeSpecifier()) {
961 switch (FormatTok->Tok.getKind()) {
973 case tok::identifier:
974 case tok::coloncolon:
975 case tok::kw_mutable:
979 FormatTok->Type = TT_LambdaArrow;
986 LSquare.Type = TT_LambdaLSquare;
991 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
993 if (FormatTok->is(tok::equal)) {
995 if (FormatTok->is(tok::r_square)) {
999 if (FormatTok->isNot(tok::comma))
1002 } else if (FormatTok->is(tok::amp)) {
1004 if (FormatTok->is(tok::r_square)) {
1008 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
1011 if (FormatTok->is(tok::comma))
1013 } else if (FormatTok->is(tok::r_square)) {
1018 if (FormatTok->is(tok::amp))
1020 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
1023 if (FormatTok->is(tok::ellipsis))
1025 if (FormatTok->is(tok::comma)) {
1027 } else if (FormatTok->is(tok::r_square)) {
1037 void UnwrappedLineParser::tryToParseJSFunction() {
1040 // Consume function name.
1041 if (FormatTok->is(tok::identifier))
1044 if (FormatTok->isNot(tok::l_paren))
1047 // Parse formal parameter list.
1050 if (FormatTok->is(tok::colon)) {
1051 // Parse a type definition.
1054 // Eat the type declaration. For braced inline object types, balance braces,
1055 // otherwise just parse until finding an l_brace for the function body.
1056 if (FormatTok->is(tok::l_brace))
1057 tryToParseBracedList();
1059 while (FormatTok->isNot(tok::l_brace) && !eof())
1066 bool UnwrappedLineParser::tryToParseBracedList() {
1067 if (FormatTok->BlockKind == BK_Unknown)
1068 calculateBraceTypes();
1069 assert(FormatTok->BlockKind != BK_Unknown);
1070 if (FormatTok->BlockKind == BK_Block)
1076 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1077 bool HasError = false;
1080 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1081 // replace this by using parseAssigmentExpression() inside.
1083 if (Style.Language == FormatStyle::LK_JavaScript) {
1084 if (FormatTok->is(Keywords.kw_function)) {
1085 tryToParseJSFunction();
1088 if (FormatTok->is(TT_JsFatArrow)) {
1090 // Fat arrows can be followed by simple expressions or by child blocks
1092 if (FormatTok->is(tok::l_brace)) {
1098 switch (FormatTok->Tok.getKind()) {
1101 if (FormatTok->is(tok::l_brace)) {
1109 // Assume there are no blocks inside a braced init list apart
1110 // from the ones we explicitly parse out (like lambdas).
1111 FormatTok->BlockKind = BK_BracedInit;
1116 // JavaScript can just have free standing methods and getters/setters in
1117 // object literals. Detect them by a "{" following ")".
1118 if (Style.Language == FormatStyle::LK_JavaScript) {
1119 if (FormatTok->is(tok::l_brace))
1129 if (!ContinueOnSemicolons)
1144 void UnwrappedLineParser::parseParens() {
1145 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
1148 switch (FormatTok->Tok.getKind()) {
1151 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1158 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1164 if (!tryToParseBracedList())
1169 if (FormatTok->Tok.is(tok::l_brace))
1172 case tok::identifier:
1173 if (Style.Language == FormatStyle::LK_JavaScript &&
1174 FormatTok->is(Keywords.kw_function))
1175 tryToParseJSFunction();
1186 void UnwrappedLineParser::parseSquare() {
1187 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1188 if (tryToParseLambda())
1191 switch (FormatTok->Tok.getKind()) {
1199 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1204 case tok::l_brace: {
1205 if (!tryToParseBracedList())
1211 if (FormatTok->Tok.is(tok::l_brace))
1221 void UnwrappedLineParser::parseIfThenElse() {
1222 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
1224 if (FormatTok->Tok.is(tok::l_paren))
1226 bool NeedsUnwrappedLine = false;
1227 if (FormatTok->Tok.is(tok::l_brace)) {
1228 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1229 parseBlock(/*MustBeDeclaration=*/false);
1230 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1231 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
1234 NeedsUnwrappedLine = true;
1239 parseStructuralElement();
1242 if (FormatTok->Tok.is(tok::kw_else)) {
1243 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1246 if (FormatTok->Tok.is(tok::l_brace)) {
1247 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1248 parseBlock(/*MustBeDeclaration=*/false);
1250 } else if (FormatTok->Tok.is(tok::kw_if)) {
1255 parseStructuralElement();
1258 } else if (NeedsUnwrappedLine) {
1263 void UnwrappedLineParser::parseTryCatch() {
1264 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
1266 bool NeedsUnwrappedLine = false;
1267 if (FormatTok->is(tok::colon)) {
1268 // We are in a function try block, what comes is an initializer list.
1270 while (FormatTok->is(tok::identifier)) {
1272 if (FormatTok->is(tok::l_paren))
1274 if (FormatTok->is(tok::comma))
1278 // Parse try with resource.
1279 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1282 if (FormatTok->is(tok::l_brace)) {
1283 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1284 parseBlock(/*MustBeDeclaration=*/false);
1285 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1286 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1287 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1290 NeedsUnwrappedLine = true;
1292 } else if (!FormatTok->is(tok::kw_catch)) {
1293 // The C++ standard requires a compound-statement after a try.
1294 // If there's none, we try to assume there's a structuralElement
1295 // and try to continue.
1298 parseStructuralElement();
1302 if (FormatTok->is(tok::at))
1304 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1305 tok::kw___finally) ||
1306 ((Style.Language == FormatStyle::LK_Java ||
1307 Style.Language == FormatStyle::LK_JavaScript) &&
1308 FormatTok->is(Keywords.kw_finally)) ||
1309 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1310 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1313 while (FormatTok->isNot(tok::l_brace)) {
1314 if (FormatTok->is(tok::l_paren)) {
1318 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
1322 NeedsUnwrappedLine = false;
1323 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1324 parseBlock(/*MustBeDeclaration=*/false);
1325 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1326 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1327 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1330 NeedsUnwrappedLine = true;
1333 if (NeedsUnwrappedLine) {
1338 void UnwrappedLineParser::parseNamespace() {
1339 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
1341 const FormatToken &InitialToken = *FormatTok;
1343 if (FormatTok->Tok.is(tok::identifier))
1345 if (FormatTok->Tok.is(tok::l_brace)) {
1346 if (ShouldBreakBeforeBrace(Style, InitialToken))
1349 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1350 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1351 DeclarationScopeStack.size() > 1);
1352 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
1353 // Munch the semicolon after a namespace. This is more common than one would
1354 // think. Puttin the semicolon into its own line is very ugly.
1355 if (FormatTok->Tok.is(tok::semi))
1359 // FIXME: Add error handling.
1362 void UnwrappedLineParser::parseNew() {
1363 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1365 if (Style.Language != FormatStyle::LK_Java)
1368 // In Java, we can parse everything up to the parens, which aren't optional.
1370 // There should not be a ;, { or } before the new's open paren.
1371 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1374 // Consume the parens.
1375 if (FormatTok->is(tok::l_paren)) {
1378 // If there is a class body of an anonymous class, consume that as child.
1379 if (FormatTok->is(tok::l_brace))
1387 void UnwrappedLineParser::parseForOrWhileLoop() {
1388 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
1389 "'for', 'while' or foreach macro expected");
1391 if (FormatTok->Tok.is(tok::l_paren))
1393 if (FormatTok->Tok.is(tok::l_brace)) {
1394 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1395 parseBlock(/*MustBeDeclaration=*/false);
1400 parseStructuralElement();
1405 void UnwrappedLineParser::parseDoWhile() {
1406 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
1408 if (FormatTok->Tok.is(tok::l_brace)) {
1409 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1410 parseBlock(/*MustBeDeclaration=*/false);
1411 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1416 parseStructuralElement();
1420 // FIXME: Add error handling.
1421 if (!FormatTok->Tok.is(tok::kw_while)) {
1427 parseStructuralElement();
1430 void UnwrappedLineParser::parseLabel() {
1432 unsigned OldLineLevel = Line->Level;
1433 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1435 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
1436 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1437 parseBlock(/*MustBeDeclaration=*/false);
1438 if (FormatTok->Tok.is(tok::kw_break)) {
1439 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1440 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1441 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
1444 parseStructuralElement();
1448 if (FormatTok->is(tok::semi))
1452 Line->Level = OldLineLevel;
1455 void UnwrappedLineParser::parseCaseLabel() {
1456 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
1457 // FIXME: fix handling of complex expressions here.
1460 } while (!eof() && !FormatTok->Tok.is(tok::colon));
1464 void UnwrappedLineParser::parseSwitch() {
1465 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
1467 if (FormatTok->Tok.is(tok::l_paren))
1469 if (FormatTok->Tok.is(tok::l_brace)) {
1470 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1471 parseBlock(/*MustBeDeclaration=*/false);
1476 parseStructuralElement();
1481 void UnwrappedLineParser::parseAccessSpecifier() {
1483 // Understand Qt's slots.
1484 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
1486 // Otherwise, we don't know what it is, and we'd better keep the next token.
1487 if (FormatTok->Tok.is(tok::colon))
1492 void UnwrappedLineParser::parseEnum() {
1493 // Won't be 'enum' for NS_ENUMs.
1494 if (FormatTok->Tok.is(tok::kw_enum))
1497 // Eat up enum class ...
1498 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1501 while (FormatTok->Tok.getIdentifierInfo() ||
1502 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1503 tok::greater, tok::comma, tok::question)) {
1505 // We can have macros or attributes in between 'enum' and the enum name.
1506 if (FormatTok->is(tok::l_paren))
1508 if (FormatTok->is(tok::identifier)) {
1510 // If there are two identifiers in a row, this is likely an elaborate
1511 // return type. In Java, this can be "implements", etc.
1512 if (Style.Language == FormatStyle::LK_Cpp &&
1513 FormatTok->is(tok::identifier))
1518 // Just a declaration or something is wrong.
1519 if (FormatTok->isNot(tok::l_brace))
1521 FormatTok->BlockKind = BK_Block;
1523 if (Style.Language == FormatStyle::LK_Java) {
1524 // Java enums are different.
1525 parseJavaEnumBody();
1530 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1532 if (FormatTok->is(tok::semi))
1537 // There is no addUnwrappedLine() here so that we fall through to parsing a
1538 // structural element afterwards. Thus, in "enum A {} n, m;",
1539 // "} n, m;" will end up in one unwrapped line.
1542 void UnwrappedLineParser::parseJavaEnumBody() {
1543 // Determine whether the enum is simple, i.e. does not have a semicolon or
1544 // constants with class bodies. Simple enums can be formatted like braced
1545 // lists, contracted to a single line, etc.
1546 unsigned StoredPosition = Tokens->getPosition();
1547 bool IsSimple = true;
1548 FormatToken *Tok = Tokens->getNextToken();
1550 if (Tok->is(tok::r_brace))
1552 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1556 // FIXME: This will also mark enums with braces in the arguments to enum
1557 // constants as "not simple". This is probably fine in practice, though.
1558 Tok = Tokens->getNextToken();
1560 FormatTok = Tokens->setPosition(StoredPosition);
1568 // Parse the body of a more complex enum.
1569 // First add a line for everything up to the "{".
1574 // Parse the enum constants.
1576 if (FormatTok->is(tok::l_brace)) {
1577 // Parse the constant's class body.
1578 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1579 /*MunchSemi=*/false);
1580 } else if (FormatTok->is(tok::l_paren)) {
1582 } else if (FormatTok->is(tok::comma)) {
1585 } else if (FormatTok->is(tok::semi)) {
1589 } else if (FormatTok->is(tok::r_brace)) {
1597 // Parse the class body after the enum's ";" if any.
1598 parseLevel(/*HasOpeningBrace=*/true);
1604 void UnwrappedLineParser::parseRecord() {
1605 const FormatToken &InitialToken = *FormatTok;
1608 // The actual identifier can be a nested name specifier, and in macros
1609 // it is often token-pasted.
1610 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
1611 tok::kw___attribute, tok::kw___declspec,
1613 ((Style.Language == FormatStyle::LK_Java ||
1614 Style.Language == FormatStyle::LK_JavaScript) &&
1615 FormatTok->isOneOf(tok::period, tok::comma))) {
1616 bool IsNonMacroIdentifier =
1617 FormatTok->is(tok::identifier) &&
1618 FormatTok->TokenText != FormatTok->TokenText.upper();
1620 // We can have macros or attributes in between 'class' and the class name.
1621 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
1625 // Note that parsing away template declarations here leads to incorrectly
1626 // accepting function declarations as record declarations.
1627 // In general, we cannot solve this problem. Consider:
1628 // class A<int> B() {}
1629 // which can be a function definition or a class definition when B() is a
1630 // macro. If we find enough real-world cases where this is a problem, we
1631 // can parse for the 'template' keyword in the beginning of the statement,
1632 // and thus rule out the record production in case there is no template
1633 // (this would still leave us with an ambiguity between template function
1634 // and class declarations).
1635 if (FormatTok->isOneOf(tok::colon, tok::less)) {
1637 if (FormatTok->is(tok::l_brace)) {
1638 calculateBraceTypes(/*ExpectClassBody=*/true);
1639 if (!tryToParseBracedList())
1642 if (FormatTok->Tok.is(tok::semi))
1647 if (FormatTok->Tok.is(tok::l_brace)) {
1648 if (ShouldBreakBeforeBrace(Style, InitialToken))
1651 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1652 /*MunchSemi=*/false);
1654 // There is no addUnwrappedLine() here so that we fall through to parsing a
1655 // structural element afterwards. Thus, in "class A {} n, m;",
1656 // "} n, m;" will end up in one unwrapped line.
1659 void UnwrappedLineParser::parseObjCProtocolList() {
1660 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
1663 while (!eof() && FormatTok->Tok.isNot(tok::greater));
1664 nextToken(); // Skip '>'.
1667 void UnwrappedLineParser::parseObjCUntilAtEnd() {
1669 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
1674 if (FormatTok->is(tok::l_brace)) {
1675 parseBlock(/*MustBeDeclaration=*/false);
1676 // In ObjC interfaces, nothing should be following the "}".
1678 } else if (FormatTok->is(tok::r_brace)) {
1679 // Ignore stray "}". parseStructuralElement doesn't consume them.
1683 parseStructuralElement();
1688 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
1690 nextToken(); // interface name
1692 // @interface can be followed by either a base class, or a category.
1693 if (FormatTok->Tok.is(tok::colon)) {
1695 nextToken(); // base class name
1696 } else if (FormatTok->Tok.is(tok::l_paren))
1697 // Skip category, if present.
1700 if (FormatTok->Tok.is(tok::less))
1701 parseObjCProtocolList();
1703 if (FormatTok->Tok.is(tok::l_brace)) {
1704 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1705 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1707 parseBlock(/*MustBeDeclaration=*/true);
1710 // With instance variables, this puts '}' on its own line. Without instance
1711 // variables, this ends the @interface line.
1714 parseObjCUntilAtEnd();
1717 void UnwrappedLineParser::parseObjCProtocol() {
1719 nextToken(); // protocol name
1721 if (FormatTok->Tok.is(tok::less))
1722 parseObjCProtocolList();
1724 // Check for protocol declaration.
1725 if (FormatTok->Tok.is(tok::semi)) {
1727 return addUnwrappedLine();
1731 parseObjCUntilAtEnd();
1734 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1735 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
1738 // Consume the "default" in "export default class/function".
1739 if (FormatTok->is(tok::kw_default))
1742 // Consume "function" and "default function", so that these get parsed as
1743 // free-standing JS functions, i.e. do not require a trailing semicolon.
1744 if (FormatTok->is(Keywords.kw_function)) {
1749 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, tok::kw_enum,
1751 return; // Fall through to parsing the corresponding structure.
1753 if (FormatTok->is(tok::l_brace)) {
1754 FormatTok->BlockKind = BK_Block;
1758 while (!eof() && FormatTok->isNot(tok::semi) &&
1759 FormatTok->isNot(tok::l_brace)) {
1764 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1765 StringRef Prefix = "") {
1766 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1767 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1768 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1769 E = Line.Tokens.end();
1771 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
1773 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1774 E = Line.Tokens.end();
1776 const UnwrappedLineNode &Node = *I;
1777 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1778 I = Node.Children.begin(),
1779 E = Node.Children.end();
1781 printDebugInfo(*I, "\nChild: ");
1784 llvm::dbgs() << "\n";
1787 void UnwrappedLineParser::addUnwrappedLine() {
1788 if (Line->Tokens.empty())
1791 if (CurrentLines == &Lines)
1792 printDebugInfo(*Line);
1794 CurrentLines->push_back(std::move(*Line));
1795 Line->Tokens.clear();
1796 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
1797 CurrentLines->append(
1798 std::make_move_iterator(PreprocessorDirectives.begin()),
1799 std::make_move_iterator(PreprocessorDirectives.end()));
1800 PreprocessorDirectives.clear();
1804 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
1806 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
1807 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1808 FormatTok.NewlinesBefore > 0;
1811 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1812 bool JustComments = Line->Tokens.empty();
1813 for (SmallVectorImpl<FormatToken *>::const_iterator
1814 I = CommentsBeforeNextToken.begin(),
1815 E = CommentsBeforeNextToken.end();
1817 if (isOnNewLine(**I) && JustComments)
1821 if (NewlineBeforeNext && JustComments)
1823 CommentsBeforeNextToken.clear();
1826 void UnwrappedLineParser::nextToken() {
1829 flushComments(isOnNewLine(*FormatTok));
1830 pushToken(FormatTok);
1834 void UnwrappedLineParser::readToken() {
1835 bool CommentsInCurrentLine = true;
1837 FormatTok = Tokens->getNextToken();
1839 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1840 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
1841 // If there is an unfinished unwrapped line, we flush the preprocessor
1842 // directives only after that unwrapped line was finished later.
1843 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
1844 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
1845 // Comments stored before the preprocessor directive need to be output
1846 // before the preprocessor directive, at the same level as the
1847 // preprocessor directive, as we consider them to apply to the directive.
1848 flushComments(isOnNewLine(*FormatTok));
1851 while (FormatTok->Type == TT_ConflictStart ||
1852 FormatTok->Type == TT_ConflictEnd ||
1853 FormatTok->Type == TT_ConflictAlternative) {
1854 if (FormatTok->Type == TT_ConflictStart) {
1855 conditionalCompilationStart(/*Unreachable=*/false);
1856 } else if (FormatTok->Type == TT_ConflictAlternative) {
1857 conditionalCompilationAlternative();
1858 } else if (FormatTok->Type == TT_ConflictEnd) {
1859 conditionalCompilationEnd();
1861 FormatTok = Tokens->getNextToken();
1862 FormatTok->MustBreakBefore = true;
1865 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1866 !Line->InPPDirective) {
1870 if (!FormatTok->Tok.is(tok::comment))
1872 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
1873 CommentsInCurrentLine = false;
1875 if (CommentsInCurrentLine) {
1876 pushToken(FormatTok);
1878 CommentsBeforeNextToken.push_back(FormatTok);
1883 void UnwrappedLineParser::pushToken(FormatToken *Tok) {
1884 Line->Tokens.push_back(UnwrappedLineNode(Tok));
1885 if (MustBreakBeforeNextToken) {
1886 Line->Tokens.back().Tok->MustBreakBefore = true;
1887 MustBreakBeforeNextToken = false;
1891 } // end namespace format
1892 } // end namespace clang