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"
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;
58 class ScopedMacroState :
public FormatTokenSource {
60 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&
TokenSource,
67 Line.InPPDirective =
true;
70 ~ScopedMacroState()
override {
73 Line.InPPDirective =
false;
77 FormatToken *getNextToken()
override {
89 FormatToken *setPosition(
unsigned Position)
override {
97 FormatToken *getFakeEOF() {
98 static bool EOFInitialized =
false;
99 static FormatToken FormatTok;
100 if (!EOFInitialized) {
101 FormatTok.Tok.startToken();
103 EOFInitialized =
true;
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;
148 std::unique_ptr<UnwrappedLine> PreBlockLine;
156 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
158 Parser->addUnwrappedLine();
166 unsigned OldLineLevel;
171 class IndexedTokenSource :
public FormatTokenSource {
174 : Tokens(Tokens), Position(-1) {}
176 FormatToken *getNextToken()
override {
181 unsigned getPosition()
override {
182 assert(Position >= 0);
186 FormatToken *setPosition(
unsigned P)
override {
191 void reset() { Position = -1; }
205 CurrentLines(&Lines), Style(Style), Keywords(Keywords), Tokens(nullptr),
206 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
208 void UnwrappedLineParser::reset() {
211 CommentsBeforeNextToken.clear();
213 MustBreakBeforeNextToken =
false;
214 PreprocessorDirectives.clear();
215 CurrentLines = &Lines;
216 DeclarationScopeStack.clear();
223 DEBUG(llvm::dbgs() <<
"----\n");
231 pushToken(FormatTok);
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() {
257 bool MustBeDeclaration =
259 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
267 void UnwrappedLineParser::parseLevel(
bool HasOpeningBrace) {
268 bool SwitchLabelEncountered =
false;
271 if (FormatTok->Type == TT_MacroBlockBegin) {
273 }
else if (FormatTok->Type == TT_MacroBlockEnd) {
285 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
296 case tok::kw_default:
298 if (!SwitchLabelEncountered &&
301 SwitchLabelEncountered =
true;
302 parseStructuralElement();
305 parseStructuralElement();
311 void UnwrappedLineParser::calculateBraceTypes(
bool ExpectClassBody) {
317 FormatToken *Tok = FormatTok;
318 const FormatToken *PrevTok = getPreviousToken();
322 SmallVector<FormatToken *, 8> LBraceStack;
323 assert(Tok->Tok.is(tok::l_brace));
326 FormatToken *NextTok;
327 unsigned ReadTokens = 0;
331 }
while (NextTok->is(tok::comment));
333 switch (Tok->Tok.getKind()) {
336 PrevTok->is(tok::colon))
342 LBraceStack.push_back(Tok);
345 if (LBraceStack.empty())
347 if (LBraceStack.back()->BlockKind ==
BK_Unknown) {
348 bool ProbablyBracedList =
false;
350 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
354 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
355 NextTok->OriginalColumn == 0;
367 NextTok->isOneOf(Keywords.
kw_of, Keywords.
kw_in)) ||
368 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
369 tok::r_paren, tok::r_square, tok::l_brace,
370 tok::l_square, tok::l_paren, tok::ellipsis) ||
371 (NextTok->is(tok::semi) &&
372 (!ExpectClassBody || LBraceStack.size() != 1)) ||
373 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
375 if (ProbablyBracedList) {
380 LBraceStack.back()->BlockKind =
BK_Block;
383 LBraceStack.pop_back();
393 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind ==
BK_Unknown)
394 LBraceStack.back()->BlockKind =
BK_Block;
401 }
while (Tok->Tok.isNot(
tok::eof) && !LBraceStack.empty());
404 for (
unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
406 LBraceStack[i]->BlockKind =
BK_Block;
412 void UnwrappedLineParser::parseBlock(
bool MustBeDeclaration,
bool AddLevel,
414 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
415 "'{' or macro block token expected");
416 const bool MacroBlock = FormatTok->
is(TT_MacroBlockBegin);
419 unsigned InitialLevel = Line->Level;
422 if (MacroBlock && FormatTok->is(tok::l_paren))
427 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
436 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
437 : !FormatTok->is(tok::r_brace)) {
438 Line->Level = InitialLevel;
445 if (MacroBlock && FormatTok->is(tok::l_paren))
448 if (MunchSemi && FormatTok->Tok.is(tok::semi))
450 Line->Level = InitialLevel;
456 if (Line.
Tokens.size() < 4)
459 if (
I->Tok->TokenText !=
"goog")
462 if (
I->Tok->isNot(tok::period))
465 if (
I->Tok->TokenText !=
"scope")
468 return I->Tok->is(tok::l_paren);
473 if (InitialToken.
is(tok::kw_namespace))
475 if (InitialToken.
is(tok::kw_class))
477 if (InitialToken.
is(tok::kw_union))
479 if (InitialToken.
is(tok::kw_struct))
484 void UnwrappedLineParser::parseChildBlock() {
491 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
493 Line->Level += GoogScope ? 0 : 1;
495 flushComments(isOnNewLine(*FormatTok));
496 Line->Level -= GoogScope ? 0 : 1;
501 void UnwrappedLineParser::parsePPDirective() {
502 assert(FormatTok->Tok.is(tok::hash) &&
"'#' expected");
503 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
506 if (!FormatTok->Tok.getIdentifierInfo()) {
511 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
537 void UnwrappedLineParser::conditionalCompilationCondition(
bool Unreachable) {
538 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
539 PPStack.push_back(PP_Unreachable);
541 PPStack.push_back(PP_Conditional);
544 void UnwrappedLineParser::conditionalCompilationStart(
bool Unreachable) {
546 assert(PPBranchLevel >= 0 && PPBranchLevel <= (
int)PPLevelBranchIndex.size());
547 if (PPBranchLevel == (
int)PPLevelBranchIndex.size()) {
548 PPLevelBranchIndex.push_back(0);
549 PPLevelBranchCount.push_back(0);
551 PPChainBranchIndex.push(0);
552 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
553 conditionalCompilationCondition(Unreachable || Skip);
556 void UnwrappedLineParser::conditionalCompilationAlternative() {
557 if (!PPStack.empty())
559 assert(PPBranchLevel < (
int)PPLevelBranchIndex.size());
560 if (!PPChainBranchIndex.empty())
561 ++PPChainBranchIndex.top();
562 conditionalCompilationCondition(
563 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
564 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
567 void UnwrappedLineParser::conditionalCompilationEnd() {
568 assert(PPBranchLevel < (
int)PPLevelBranchIndex.size());
569 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
570 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
571 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
575 if (PPBranchLevel > 0)
577 if (!PPChainBranchIndex.empty())
578 PPChainBranchIndex.pop();
579 if (!PPStack.empty())
583 void UnwrappedLineParser::parsePPIf(
bool IfDef) {
585 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
586 FormatTok->Tok.getLiteralData() !=
nullptr &&
587 StringRef(FormatTok->Tok.getLiteralData(),
588 FormatTok->Tok.getLength()) ==
"0") ||
589 FormatTok->Tok.is(tok::kw_false);
590 conditionalCompilationStart(!IfDef && IsLiteralFalse);
594 void UnwrappedLineParser::parsePPElse() {
595 conditionalCompilationAlternative();
599 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
601 void UnwrappedLineParser::parsePPEndIf() {
602 conditionalCompilationEnd();
606 void UnwrappedLineParser::parsePPDefine() {
609 if (FormatTok->Tok.getKind() != tok::identifier) {
614 if (FormatTok->Tok.getKind() == tok::l_paren &&
615 FormatTok->WhitespaceRange.getBegin() ==
616 FormatTok->WhitespaceRange.getEnd()) {
630 void UnwrappedLineParser::parsePPUnknown() {
643 return Tok.
isNot(tok::semi) && Tok.
isNot(tok::l_brace) &&
644 Tok.
isNot(tok::l_square) &&
647 Tok.
isNot(tok::period) && Tok.
isNot(tok::periodstar) &&
648 Tok.
isNot(tok::arrow) && Tok.
isNot(tok::arrowstar) &&
649 Tok.
isNot(tok::less) && Tok.
isNot(tok::greater) &&
650 Tok.
isNot(tok::slash) && Tok.
isNot(tok::percent) &&
651 Tok.
isNot(tok::lessless) && Tok.
isNot(tok::greatergreater) &&
652 Tok.
isNot(tok::equal) && Tok.
isNot(tok::plusequal) &&
653 Tok.
isNot(tok::minusequal) && Tok.
isNot(tok::starequal) &&
654 Tok.
isNot(tok::slashequal) && Tok.
isNot(tok::percentequal) &&
655 Tok.
isNot(tok::ampequal) && Tok.
isNot(tok::pipeequal) &&
656 Tok.
isNot(tok::caretequal) && Tok.
isNot(tok::greatergreaterequal) &&
657 Tok.
isNot(tok::lesslessequal) &&
661 Tok.
isNot(tok::colon) &&
663 Tok.
isNot(tok::kw_noexcept);
669 return FormatTok->
is(tok::identifier) &&
693 tok::kw_if, tok::kw_else,
695 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
697 tok::kw_switch, tok::kw_case,
699 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.
kw_finally,
701 tok::kw_const, tok::kw_class, Keywords.
kw_var, Keywords.
kw_let,
714 void UnwrappedLineParser::readTokenWithJavaScriptASI() {
717 FormatToken *
Next = FormatTok;
720 CommentsBeforeNextToken.empty()
721 ? Next->NewlinesBefore == 0
722 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
727 if (PreviousMustBeValue && Line && Line->Tokens.size() > 1) {
730 const FormatToken *PrePrevious = std::prev(Line->Tokens.end(), 2)->Tok;
731 if (PrePrevious->is(tok::at))
734 if (Next->is(tok::exclaim) && PreviousMustBeValue)
737 if (NextMustBeValue && (PreviousMustBeValue ||
738 Previous->isOneOf(tok::r_square, tok::r_paren,
739 tok::plusplus, tok::minusminus)))
745 void UnwrappedLineParser::parseStructuralElement() {
746 assert(!FormatTok->is(tok::l_brace));
748 FormatTok->is(tok::pp_include)) {
750 if (FormatTok->is(tok::string_literal))
755 switch (FormatTok->Tok.getKind()) {
758 if (FormatTok->Tok.is(tok::l_brace)) {
762 switch (FormatTok->Tok.getObjCKeywordID()) {
763 case tok::objc_public:
764 case tok::objc_protected:
765 case tok::objc_package:
766 case tok::objc_private:
767 return parseAccessSpecifier();
768 case tok::objc_interface:
769 case tok::objc_implementation:
770 return parseObjCInterfaceOrImplementation();
771 case tok::objc_protocol:
772 return parseObjCProtocol();
775 case tok::objc_optional:
776 case tok::objc_required:
780 case tok::objc_autoreleasepool:
782 if (FormatTok->Tok.is(tok::l_brace)) {
800 if (FormatTok->is(tok::l_brace)) {
801 FormatTok->Type = TT_InlineASMBrace;
803 while (FormatTok && FormatTok->isNot(
tok::eof)) {
804 if (FormatTok->is(tok::r_brace)) {
805 FormatTok->Type = TT_InlineASMBrace;
810 FormatTok->Finalized =
true;
815 case tok::kw_namespace:
820 if (FormatTok->Tok.is(tok::kw_namespace)) {
826 case tok::kw_protected:
827 case tok::kw_private:
832 parseAccessSpecifier();
839 parseForOrWhileLoop();
847 case tok::kw_default:
860 if (FormatTok->Tok.is(tok::string_literal)) {
862 if (FormatTok->Tok.is(tok::l_brace)) {
863 parseBlock(
true,
false);
871 parseJavaScriptEs6ImportExport();
875 case tok::identifier:
876 if (FormatTok->is(TT_ForEachMacro)) {
877 parseForOrWhileLoop();
880 if (FormatTok->is(TT_MacroBlockBegin)) {
881 parseBlock(
false,
true,
887 parseJavaScriptEs6ImportExport();
892 if (FormatTok->is(tok::kw_public))
894 if (!FormatTok->is(tok::string_literal))
897 if (FormatTok->is(tok::semi))
906 if (FormatTok->is(tok::colon)) {
918 const FormatToken *Previous = getPreviousToken();
919 switch (FormatTok->Tok.getKind()) {
922 if (FormatTok->Tok.is(tok::l_brace))
927 if (Previous && Previous->is(tok::less)) {
942 case tok::kw_typedef:
957 if (FormatTok->is(tok::semi))
967 FormatTok->is(tok::kw_class))
970 FormatTok->Tok.getIdentifierInfo())
985 case tok::kw_operator:
987 if (FormatTok->isBinaryOperator())
992 if (FormatTok->Tok.isAnyIdentifier() ||
993 FormatTok->isSimpleTypeSpecifier())
995 if (FormatTok->is(tok::l_paren))
997 if (FormatTok->is(tok::l_brace))
1001 if (!tryToParseBracedList()) {
1008 FormatTok->Type = TT_FunctionLBrace;
1020 case tok::identifier: {
1021 if (FormatTok->is(TT_MacroBlockEnd)) {
1030 FormatTok->startsSequence(Keywords.
kw_async,
1032 Line->Tokens.size() > 0) {
1033 tryToParseJSFunction();
1058 StringRef
Text = FormatTok->TokenText;
1060 if (Line->Tokens.size() == 1 &&
1064 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
1065 Line->Tokens.begin()->Tok->MustBreakBefore =
true;
1071 bool FunctionLike = FormatTok->is(tok::l_paren);
1075 bool FollowedByNewline =
1076 CommentsBeforeNextToken.empty()
1077 ? FormatTok->NewlinesBefore > 0
1078 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1080 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
1092 if (FormatTok->is(TT_JsFatArrow)) {
1094 if (FormatTok->is(tok::l_brace))
1100 if (FormatTok->Tok.is(tok::l_brace)) {
1117 bool UnwrappedLineParser::tryToParseLambda() {
1122 const FormatToken* Previous = getPreviousToken();
1124 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1126 Previous->closesScope() || Previous->isSimpleTypeSpecifier())) {
1130 assert(FormatTok->is(tok::l_square));
1131 FormatToken &LSquare = *FormatTok;
1132 if (!tryToParseLambdaIntroducer())
1135 while (FormatTok->isNot(tok::l_brace)) {
1136 if (FormatTok->isSimpleTypeSpecifier()) {
1140 switch (FormatTok->Tok.getKind()) {
1152 case tok::identifier:
1153 case tok::numeric_constant:
1154 case tok::coloncolon:
1155 case tok::kw_mutable:
1159 FormatTok->Type = TT_LambdaArrow;
1166 LSquare.Type = TT_LambdaLSquare;
1171 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1173 if (FormatTok->is(tok::equal)) {
1175 if (FormatTok->is(tok::r_square)) {
1179 if (FormatTok->isNot(tok::comma))
1182 }
else if (FormatTok->is(tok::amp)) {
1184 if (FormatTok->is(tok::r_square)) {
1188 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
1191 if (FormatTok->is(tok::comma))
1193 }
else if (FormatTok->is(tok::r_square)) {
1198 if (FormatTok->is(tok::amp))
1200 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
1203 if (FormatTok->is(tok::ellipsis))
1205 if (FormatTok->is(tok::comma)) {
1207 }
else if (FormatTok->is(tok::r_square)) {
1217 void UnwrappedLineParser::tryToParseJSFunction() {
1220 if (FormatTok->is(Keywords.
kw_async))
1226 if (FormatTok->is(tok::star))
1230 if (FormatTok->is(tok::identifier))
1233 if (FormatTok->isNot(tok::l_paren))
1239 if (FormatTok->is(tok::colon)) {
1245 if (FormatTok->is(tok::l_brace))
1246 tryToParseBracedList();
1248 while (FormatTok->isNot(tok::l_brace) && !eof())
1255 bool UnwrappedLineParser::tryToParseBracedList() {
1257 calculateBraceTypes();
1259 if (FormatTok->BlockKind ==
BK_Block)
1265 bool UnwrappedLineParser::parseBracedList(
bool ContinueOnSemicolons) {
1266 bool HasError =
false;
1275 tryToParseJSFunction();
1278 if (FormatTok->is(TT_JsFatArrow)) {
1282 if (FormatTok->is(tok::l_brace)) {
1288 switch (FormatTok->Tok.getKind()) {
1291 if (FormatTok->is(tok::l_brace)) {
1309 if (FormatTok->is(tok::l_brace))
1327 if (!ContinueOnSemicolons)
1342 void UnwrappedLineParser::parseParens() {
1343 assert(FormatTok->Tok.is(tok::l_paren) &&
"'(' expected.");
1346 switch (FormatTok->Tok.getKind()) {
1362 if (!tryToParseBracedList())
1367 if (FormatTok->Tok.is(tok::l_brace))
1370 case tok::identifier:
1374 tryToParseJSFunction();
1385 void UnwrappedLineParser::parseSquare() {
1386 assert(FormatTok->Tok.is(tok::l_square) &&
"'[' expected.");
1387 if (tryToParseLambda())
1390 switch (FormatTok->Tok.getKind()) {
1403 case tok::l_brace: {
1404 if (!tryToParseBracedList())
1410 if (FormatTok->Tok.is(tok::l_brace))
1420 void UnwrappedLineParser::parseIfThenElse() {
1421 assert(FormatTok->Tok.is(tok::kw_if) &&
"'if' expected");
1423 if (FormatTok->Tok.is(tok::l_paren))
1425 bool NeedsUnwrappedLine =
false;
1426 if (FormatTok->Tok.is(tok::l_brace)) {
1432 NeedsUnwrappedLine =
true;
1436 parseStructuralElement();
1439 if (FormatTok->Tok.is(tok::kw_else)) {
1441 if (FormatTok->Tok.is(tok::l_brace)) {
1445 }
else if (FormatTok->Tok.is(tok::kw_if)) {
1450 parseStructuralElement();
1455 }
else if (NeedsUnwrappedLine) {
1460 void UnwrappedLineParser::parseTryCatch() {
1461 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) &&
"'try' expected");
1463 bool NeedsUnwrappedLine =
false;
1464 if (FormatTok->is(tok::colon)) {
1467 while (FormatTok->is(tok::identifier)) {
1469 if (FormatTok->is(tok::l_paren))
1471 if (FormatTok->is(tok::comma))
1479 if (FormatTok->is(tok::l_brace)) {
1485 NeedsUnwrappedLine =
true;
1487 }
else if (!FormatTok->is(tok::kw_catch)) {
1493 parseStructuralElement();
1497 if (FormatTok->is(tok::at))
1499 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.
kw___except,
1500 tok::kw___finally) ||
1504 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1505 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1508 while (FormatTok->isNot(tok::l_brace)) {
1509 if (FormatTok->is(tok::l_paren)) {
1513 if (FormatTok->isOneOf(tok::semi, tok::r_brace,
tok::eof))
1517 NeedsUnwrappedLine =
false;
1523 NeedsUnwrappedLine =
true;
1525 if (NeedsUnwrappedLine)
1529 void UnwrappedLineParser::parseNamespace() {
1530 assert(FormatTok->Tok.is(tok::kw_namespace) &&
"'namespace' expected");
1532 const FormatToken &InitialToken = *FormatTok;
1534 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
1536 if (FormatTok->Tok.is(tok::l_brace)) {
1542 DeclarationScopeStack.size() > 1);
1543 parseBlock(
true, AddLevel);
1546 if (FormatTok->Tok.is(tok::semi))
1553 void UnwrappedLineParser::parseNew() {
1554 assert(FormatTok->is(tok::kw_new) &&
"'new' expected");
1562 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1566 if (FormatTok->is(tok::l_paren)) {
1570 if (FormatTok->is(tok::l_brace))
1578 void UnwrappedLineParser::parseForOrWhileLoop() {
1579 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
1580 "'for', 'while' or foreach macro expected");
1582 if (FormatTok->Tok.is(tok::l_paren))
1584 if (FormatTok->Tok.is(tok::l_brace)) {
1591 parseStructuralElement();
1596 void UnwrappedLineParser::parseDoWhile() {
1597 assert(FormatTok->Tok.is(tok::kw_do) &&
"'do' expected");
1599 if (FormatTok->Tok.is(tok::l_brace)) {
1607 parseStructuralElement();
1612 if (!FormatTok->Tok.is(tok::kw_while)) {
1618 parseStructuralElement();
1621 void UnwrappedLineParser::parseLabel() {
1623 unsigned OldLineLevel = Line->Level;
1624 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1626 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
1629 if (FormatTok->Tok.is(tok::kw_break)) {
1632 parseStructuralElement();
1636 if (FormatTok->is(tok::semi))
1640 Line->Level = OldLineLevel;
1641 if (FormatTok->isNot(tok::l_brace)) {
1642 parseStructuralElement();
1647 void UnwrappedLineParser::parseCaseLabel() {
1648 assert(FormatTok->Tok.is(tok::kw_case) &&
"'case' expected");
1652 }
while (!eof() && !FormatTok->Tok.is(tok::colon));
1656 void UnwrappedLineParser::parseSwitch() {
1657 assert(FormatTok->Tok.is(tok::kw_switch) &&
"'switch' expected");
1659 if (FormatTok->Tok.is(tok::l_paren))
1661 if (FormatTok->Tok.is(tok::l_brace)) {
1668 parseStructuralElement();
1673 void UnwrappedLineParser::parseAccessSpecifier() {
1679 if (FormatTok->Tok.is(tok::colon))
1684 bool UnwrappedLineParser::parseEnum() {
1686 if (FormatTok->Tok.is(tok::kw_enum))
1693 FormatTok->isOneOf(tok::colon, tok::question))
1697 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1700 while (FormatTok->Tok.getIdentifierInfo() ||
1701 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1702 tok::greater, tok::comma, tok::question)) {
1705 if (FormatTok->is(tok::l_paren))
1707 if (FormatTok->is(tok::identifier)) {
1712 FormatTok->is(tok::identifier))
1718 if (FormatTok->isNot(tok::l_brace))
1724 parseJavaEnumBody();
1733 bool HasError = !parseBracedList(
true);
1735 if (FormatTok->is(tok::semi))
1746 void UnwrappedLineParser::parseJavaEnumBody() {
1751 bool IsSimple =
true;
1754 if (Tok->is(tok::r_brace))
1756 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1780 if (FormatTok->is(tok::l_brace)) {
1782 parseBlock(
true,
true,
1784 }
else if (FormatTok->is(tok::l_paren)) {
1786 }
else if (FormatTok->is(tok::comma)) {
1789 }
else if (FormatTok->is(tok::semi)) {
1793 }
else if (FormatTok->is(tok::r_brace)) {
1808 void UnwrappedLineParser::parseRecord() {
1809 const FormatToken &InitialToken = *FormatTok;
1814 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
1815 tok::kw___attribute, tok::kw___declspec,
1819 FormatTok->isOneOf(tok::period, tok::comma))) {
1820 bool IsNonMacroIdentifier =
1821 FormatTok->is(tok::identifier) &&
1822 FormatTok->TokenText != FormatTok->TokenText.upper();
1825 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
1839 if (FormatTok->isOneOf(tok::colon, tok::less)) {
1841 if (FormatTok->is(tok::l_brace)) {
1842 calculateBraceTypes(
true);
1843 if (!tryToParseBracedList())
1846 if (FormatTok->Tok.is(tok::semi))
1851 if (FormatTok->Tok.is(tok::l_brace)) {
1855 parseBlock(
true,
true,
1863 void UnwrappedLineParser::parseObjCProtocolList() {
1864 assert(FormatTok->Tok.is(tok::less) &&
"'<' expected.");
1867 while (!eof() && FormatTok->Tok.isNot(tok::greater));
1871 void UnwrappedLineParser::parseObjCUntilAtEnd() {
1873 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
1878 if (FormatTok->is(tok::l_brace)) {
1882 }
else if (FormatTok->is(tok::r_brace)) {
1887 parseStructuralElement();
1892 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
1897 if (FormatTok->Tok.is(tok::colon)) {
1900 }
else if (FormatTok->Tok.is(tok::l_paren))
1904 if (FormatTok->Tok.is(tok::less))
1905 parseObjCProtocolList();
1907 if (FormatTok->Tok.is(tok::l_brace)) {
1917 parseObjCUntilAtEnd();
1920 void UnwrappedLineParser::parseObjCProtocol() {
1924 if (FormatTok->Tok.is(tok::less))
1925 parseObjCProtocolList();
1928 if (FormatTok->Tok.is(tok::semi)) {
1930 return addUnwrappedLine();
1934 parseObjCUntilAtEnd();
1937 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1938 bool IsImport = FormatTok->is(Keywords.
kw_import);
1939 assert(IsImport || FormatTok->is(tok::kw_export));
1943 if (FormatTok->is(tok::kw_default))
1949 if (FormatTok->is(Keywords.
kw_async))
1960 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
1961 !FormatTok->isStringLiteral())
1964 while (!eof() && FormatTok->isNot(tok::semi)) {
1965 if (FormatTok->is(tok::l_brace)) {
1975 StringRef Prefix =
"") {
1976 llvm::dbgs() << Prefix <<
"Line(" << Line.
Level <<
")"
1978 for (std::list<UnwrappedLineNode>::const_iterator
I = Line.
Tokens.begin(),
1981 llvm::dbgs() <<
I->Tok->Tok.getName() <<
"[" <<
I->Tok->Type <<
"] ";
1983 for (std::list<UnwrappedLineNode>::const_iterator
I = Line.
Tokens.begin(),
1994 llvm::dbgs() <<
"\n";
1997 void UnwrappedLineParser::addUnwrappedLine() {
1998 if (Line->Tokens.empty())
2001 if (CurrentLines == &Lines)
2004 CurrentLines->push_back(std::move(*Line));
2005 Line->Tokens.clear();
2006 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
2007 CurrentLines->append(
2008 std::make_move_iterator(PreprocessorDirectives.begin()),
2009 std::make_move_iterator(PreprocessorDirectives.end()));
2010 PreprocessorDirectives.clear();
2014 bool UnwrappedLineParser::eof()
const {
return FormatTok->Tok.is(
tok::eof); }
2016 bool UnwrappedLineParser::isOnNewLine(
const FormatToken &FormatTok) {
2017 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
2018 FormatTok.NewlinesBefore > 0;
2021 void UnwrappedLineParser::flushComments(
bool NewlineBeforeNext) {
2022 bool JustComments = Line->Tokens.empty();
2023 for (SmallVectorImpl<FormatToken *>::const_iterator
2024 I = CommentsBeforeNextToken.begin(),
2025 E = CommentsBeforeNextToken.end();
2027 if (isOnNewLine(**
I) && JustComments)
2031 if (NewlineBeforeNext && JustComments)
2033 CommentsBeforeNextToken.clear();
2036 void UnwrappedLineParser::nextToken() {
2039 flushComments(isOnNewLine(*FormatTok));
2040 pushToken(FormatTok);
2044 readTokenWithJavaScriptASI();
2047 const FormatToken *UnwrappedLineParser::getPreviousToken() {
2050 if (!Line || Line->Tokens.empty())
2052 return Line->Tokens.back().Tok;
2055 void UnwrappedLineParser::readToken() {
2056 bool CommentsInCurrentLine =
true;
2060 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
2061 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
2064 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
2069 flushComments(isOnNewLine(*FormatTok));
2072 while (FormatTok->Type == TT_ConflictStart ||
2073 FormatTok->Type == TT_ConflictEnd ||
2074 FormatTok->Type == TT_ConflictAlternative) {
2075 if (FormatTok->Type == TT_ConflictStart) {
2076 conditionalCompilationStart(
false);
2077 }
else if (FormatTok->Type == TT_ConflictAlternative) {
2078 conditionalCompilationAlternative();
2079 }
else if (FormatTok->Type == TT_ConflictEnd) {
2080 conditionalCompilationEnd();
2086 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
2087 !Line->InPPDirective) {
2091 if (!FormatTok->Tok.is(tok::comment))
2093 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
2094 CommentsInCurrentLine =
false;
2096 if (CommentsInCurrentLine) {
2097 pushToken(FormatTok);
2099 CommentsBeforeNextToken.push_back(FormatTok);
2104 void UnwrappedLineParser::pushToken(FormatToken *Tok) {
2105 Line->Tokens.push_back(UnwrappedLineNode(Tok));
2106 if (MustBreakBeforeNextToken) {
2107 Line->Tokens.back().Tok->MustBreakBefore =
true;
2108 MustBreakBeforeNextToken =
false;
Parser - This implements a parser for the C family of languages.
FormatToken *& ResetToken
Token - This structure provides full information about a lexed token.
detail::InMemoryDirectory::const_iterator I
std::vector< bool > & Stack
FormatTokenSource *& TokenSource
MatchFinder::MatchCallback * Callback
bool isNot(tok::TokenKind K) const
ArrayRef< FormatToken * > Tokens
This file contains the declaration of the UnwrappedLineParser, which turns a stream of tokens into Un...
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
ast_type_traits::DynTypedNode Node
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T-> getSizeExpr()))
const AdditionalKeywords & Keywords
detail::InMemoryDirectory::const_iterator E
bool isLiteral() const
Return true if this is a "literal", like a numeric constant, string, etc.
unsigned kind
All of the diagnostics that can be emitted by the frontend.
unsigned PreviousLineLevel
FormatTokenSource * PreviousTokenSource
IdentifierInfo * getIdentifierInfo() const