17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/Support/Debug.h" 19 #include "llvm/Support/raw_ostream.h" 23 #define DEBUG_TYPE "format-parser" 39 class ScopedDeclarationState {
42 bool MustBeDeclaration)
43 :
Line(Line), Stack(Stack) {
45 Stack.push_back(MustBeDeclaration);
47 ~ScopedDeclarationState() {
57 std::vector<bool> &Stack;
60 static bool isLineComment(
const FormatToken &FormatTok) {
61 return FormatTok.
is(tok::comment) && !FormatTok.
TokenText.startswith(
"/*");
67 static bool continuesLineComment(
const FormatToken &FormatTok,
70 if (!Previous || !MinColumnToken)
72 unsigned MinContinueColumn =
73 MinColumnToken->
OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
74 return isLineComment(FormatTok) && FormatTok.
NewlinesBefore == 1 &&
75 isLineComment(*Previous) &&
83 :
Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
84 PreviousLineLevel(Line.
Level), PreviousTokenSource(TokenSource),
85 Token(
nullptr), PreviousToken(
nullptr) {
91 ~ScopedMacroState()
override {
92 TokenSource = PreviousTokenSource;
95 Line.
Level = PreviousLineLevel;
102 PreviousToken =
Token;
103 Token = PreviousTokenSource->getNextToken();
109 unsigned getPosition()
override {
return PreviousTokenSource->getPosition(); }
112 PreviousToken =
nullptr;
113 Token = PreviousTokenSource->setPosition(Position);
120 !continuesLineComment(*
Token, PreviousToken,
125 static bool EOFInitialized =
false;
127 if (!EOFInitialized) {
130 EOFInitialized =
true;
138 unsigned PreviousLineLevel;
150 bool SwitchToPreprocessorLines =
false)
151 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
152 if (SwitchToPreprocessorLines)
153 Parser.CurrentLines = &Parser.PreprocessorDirectives;
154 else if (!Parser.Line->Tokens.empty())
155 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
156 PreBlockLine = std::move(Parser.Line);
157 Parser.Line = llvm::make_unique<UnwrappedLine>();
158 Parser.Line->Level = PreBlockLine->Level;
159 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
163 if (!
Parser.Line->Tokens.empty()) {
164 Parser.addUnwrappedLine();
166 assert(
Parser.Line->Tokens.empty());
167 Parser.Line = std::move(PreBlockLine);
168 if (
Parser.CurrentLines == &
Parser.PreprocessorDirectives)
169 Parser.MustBreakBeforeNextToken =
true;
170 Parser.CurrentLines = OriginalLines;
176 std::unique_ptr<UnwrappedLine> PreBlockLine;
184 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
186 Parser->addUnwrappedLine();
194 unsigned OldLineLevel;
202 : Tokens(Tokens), Position(-1) {}
206 return Tokens[Position];
210 assert(Position >= 0);
216 return Tokens[Position];
219 void reset() { Position = -1; }
230 unsigned FirstStartColumn,
234 CurrentLines(&Lines), Style(Style), Keywords(Keywords),
235 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
236 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
237 IncludeGuard(Style.IndentPPDirectives ==
FormatStyle::PPDIS_None
240 IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
242 void UnwrappedLineParser::reset() {
247 IncludeGuardToken =
nullptr;
249 CommentsBeforeNextToken.clear();
251 MustBreakBeforeNextToken =
false;
252 PreprocessorDirectives.clear();
253 CurrentLines = &Lines;
254 DeclarationScopeStack.clear();
256 Line->FirstStartColumn = FirstStartColumn;
260 IndexedTokenSource TokenSource(AllTokens);
261 Line->FirstStartColumn = FirstStartColumn;
263 DEBUG(llvm::dbgs() <<
"----\n");
265 Tokens = &TokenSource;
273 if (IncludeGuard == IG_Found)
274 for (
auto &Line : Lines)
275 if (Line.InPPDirective && Line.Level > 0)
279 pushToken(FormatTok);
289 while (!PPLevelBranchIndex.empty() &&
290 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
291 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
292 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
294 if (!PPLevelBranchIndex.empty()) {
295 ++PPLevelBranchIndex.back();
296 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
297 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
299 }
while (!PPLevelBranchIndex.empty());
302 void UnwrappedLineParser::parseFile() {
305 bool MustBeDeclaration =
307 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
318 void UnwrappedLineParser::parseLevel(
bool HasOpeningBrace) {
319 bool SwitchLabelEncountered =
false;
322 if (FormatTok->
Type == TT_MacroBlockBegin) {
324 }
else if (FormatTok->
Type == TT_MacroBlockEnd) {
336 if (!FormatTok->
is(TT_MacroBlockBegin) && tryToParseBracedList())
347 case tok::kw_default:
350 Line->MustBeDeclaration) {
352 parseStructuralElement();
355 if (!SwitchLabelEncountered &&
358 SwitchLabelEncountered =
true;
359 parseStructuralElement();
362 parseStructuralElement();
368 void UnwrappedLineParser::calculateBraceTypes(
bool ExpectClassBody) {
380 assert(Tok->
Tok.
is(tok::l_brace));
384 unsigned ReadTokens = 0;
388 }
while (NextTok->
is(tok::comment));
393 if (PrevTok->
isOneOf(tok::colon, tok::less))
404 else if (PrevTok->
is(tok::r_paren))
410 LBraceStack.push_back(Tok);
413 if (LBraceStack.empty())
415 if (LBraceStack.back()->BlockKind ==
BK_Unknown) {
416 bool ProbablyBracedList =
false;
418 ProbablyBracedList = NextTok->
isOneOf(tok::comma, tok::r_square);
422 bool NextIsObjCMethod = NextTok->
isOneOf(tok::plus, tok::minus) &&
437 (Style.
isCpp() && NextTok->
is(tok::l_paren)) ||
438 NextTok->
isOneOf(tok::comma, tok::period, tok::colon,
439 tok::r_paren, tok::r_square, tok::l_brace,
440 tok::l_square, tok::ellipsis) ||
441 (NextTok->
is(tok::identifier) &&
442 !PrevTok->
isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
443 (NextTok->
is(tok::semi) &&
444 (!ExpectClassBody || LBraceStack.size() != 1)) ||
447 if (ProbablyBracedList) {
452 LBraceStack.back()->BlockKind =
BK_Block;
455 LBraceStack.pop_back();
465 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind ==
BK_Unknown)
466 LBraceStack.back()->BlockKind =
BK_Block;
476 for (
unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
478 LBraceStack[i]->BlockKind =
BK_Block;
487 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
490 size_t UnwrappedLineParser::computePPHash()
const {
492 for (
const auto &i : PPStack) {
499 void UnwrappedLineParser::parseBlock(
bool MustBeDeclaration,
bool AddLevel,
501 assert(FormatTok->
isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
502 "'{' or macro block token expected");
503 const bool MacroBlock = FormatTok->
is(TT_MacroBlockBegin);
506 size_t PPStartHash = computePPHash();
508 unsigned InitialLevel = Line->Level;
509 nextToken(AddLevel ? 1 : 0);
511 if (MacroBlock && FormatTok->
is(tok::l_paren))
514 size_t NbPreprocessorDirectives =
515 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
517 size_t OpeningLineIndex =
518 CurrentLines->empty()
520 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
522 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
531 if (MacroBlock ? !FormatTok->
is(TT_MacroBlockEnd)
532 : !FormatTok->
is(tok::r_brace)) {
533 Line->Level = InitialLevel;
538 size_t PPEndHash = computePPHash();
541 nextToken(AddLevel ? -1 : 0);
543 if (MacroBlock && FormatTok->
is(tok::l_paren))
546 if (MunchSemi && FormatTok->
Tok.
is(tok::semi))
548 Line->Level = InitialLevel;
550 if (PPStartHash == PPEndHash) {
551 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
554 (*CurrentLines)[OpeningLineIndex].MatchingOpeningBlockLineIndex =
555 CurrentLines->size() - 1;
563 if (Line.
Tokens.size() < 4)
565 auto I = Line.
Tokens.begin();
566 if (I->Tok->TokenText !=
"goog")
569 if (I->Tok->isNot(tok::period))
572 if (I->Tok->TokenText !=
"scope")
575 return I->Tok->is(tok::l_paren);
584 if (Line.
Tokens.size() < 3)
586 auto I = Line.
Tokens.begin();
587 if (I->Tok->isNot(tok::l_paren))
593 return I->Tok->is(tok::l_paren);
598 if (InitialToken.
is(tok::kw_namespace))
600 if (InitialToken.
is(tok::kw_class))
602 if (InitialToken.
is(tok::kw_union))
604 if (InitialToken.
is(tok::kw_struct))
609 void UnwrappedLineParser::parseChildBlock() {
616 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
618 Line->Level += SkipIndent ? 0 : 1;
620 flushComments(isOnNewLine(*FormatTok));
621 Line->Level -= SkipIndent ? 0 : 1;
626 void UnwrappedLineParser::parsePPDirective() {
627 assert(FormatTok->
Tok.
is(tok::hash) &&
"'#' expected");
628 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
662 void UnwrappedLineParser::conditionalCompilationCondition(
bool Unreachable) {
663 size_t Line = CurrentLines->size();
664 if (CurrentLines == &PreprocessorDirectives)
665 Line += Lines.size();
668 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
669 PPStack.push_back({PP_Unreachable, Line});
671 PPStack.push_back({PP_Conditional, Line});
674 void UnwrappedLineParser::conditionalCompilationStart(
bool Unreachable) {
676 assert(PPBranchLevel >= 0 && PPBranchLevel <= (
int)PPLevelBranchIndex.size());
677 if (PPBranchLevel == (
int)PPLevelBranchIndex.size()) {
678 PPLevelBranchIndex.push_back(0);
679 PPLevelBranchCount.push_back(0);
681 PPChainBranchIndex.push(0);
682 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
683 conditionalCompilationCondition(Unreachable || Skip);
686 void UnwrappedLineParser::conditionalCompilationAlternative() {
687 if (!PPStack.empty())
689 assert(PPBranchLevel < (
int)PPLevelBranchIndex.size());
690 if (!PPChainBranchIndex.empty())
691 ++PPChainBranchIndex.top();
692 conditionalCompilationCondition(
693 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
694 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
697 void UnwrappedLineParser::conditionalCompilationEnd() {
698 assert(PPBranchLevel < (
int)PPLevelBranchIndex.size());
699 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
700 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
701 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
705 if (PPBranchLevel > -1)
707 if (!PPChainBranchIndex.empty())
708 PPChainBranchIndex.pop();
709 if (!PPStack.empty())
713 void UnwrappedLineParser::parsePPIf(
bool IfDef) {
714 bool IfNDef = FormatTok->
is(tok::pp_ifndef);
716 bool Unreachable =
false;
717 if (!IfDef && (FormatTok->
is(tok::kw_false) || FormatTok->
TokenText ==
"0"))
719 if (IfDef && !IfNDef && FormatTok->
TokenText ==
"SWIG")
721 conditionalCompilationStart(Unreachable);
722 FormatToken *IfCondition = FormatTok;
725 bool MaybeIncludeGuard = IfNDef;
726 if (IncludeGuard == IG_Inited && MaybeIncludeGuard)
727 for (
auto &Line : Lines) {
728 if (!Line.Tokens.front().Tok->is(tok::comment)) {
729 MaybeIncludeGuard =
false;
730 IncludeGuard = IG_Rejected;
737 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
738 IncludeGuard = IG_IfNdefed;
739 IncludeGuardToken = IfCondition;
743 void UnwrappedLineParser::parsePPElse() {
745 if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
746 IncludeGuard = IG_Rejected;
747 conditionalCompilationAlternative();
748 if (PPBranchLevel > -1)
754 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
756 void UnwrappedLineParser::parsePPEndIf() {
757 conditionalCompilationEnd();
762 FormatToken *PeekNext = AllTokens[TokenPosition];
763 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
766 IncludeGuard = IG_Found;
769 void UnwrappedLineParser::parsePPDefine() {
772 if (FormatTok->
Tok.
getKind() != tok::identifier) {
773 IncludeGuard = IG_Rejected;
774 IncludeGuardToken =
nullptr;
779 if (IncludeGuard == IG_IfNdefed &&
781 IncludeGuard = IG_Defined;
782 IncludeGuardToken =
nullptr;
783 for (
auto &Line : Lines) {
784 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
785 IncludeGuard = IG_Rejected;
792 if (FormatTok->
Tok.
getKind() == tok::l_paren &&
798 Line->Level += PPBranchLevel + 1;
810 void UnwrappedLineParser::parsePPUnknown() {
815 Line->Level += PPBranchLevel + 1;
825 return Tok.
isNot(tok::semi) && Tok.
isNot(tok::l_brace) &&
826 Tok.
isNot(tok::l_square) &&
829 Tok.
isNot(tok::period) && Tok.
isNot(tok::periodstar) &&
830 Tok.
isNot(tok::arrow) && Tok.
isNot(tok::arrowstar) &&
831 Tok.
isNot(tok::less) && Tok.
isNot(tok::greater) &&
832 Tok.
isNot(tok::slash) && Tok.
isNot(tok::percent) &&
833 Tok.
isNot(tok::lessless) && Tok.
isNot(tok::greatergreater) &&
834 Tok.
isNot(tok::equal) && Tok.
isNot(tok::plusequal) &&
835 Tok.
isNot(tok::minusequal) && Tok.
isNot(tok::starequal) &&
836 Tok.
isNot(tok::slashequal) && Tok.
isNot(tok::percentequal) &&
837 Tok.
isNot(tok::ampequal) && Tok.
isNot(tok::pipeequal) &&
838 Tok.
isNot(tok::caretequal) && Tok.
isNot(tok::greatergreaterequal) &&
839 Tok.
isNot(tok::lesslessequal) &&
843 Tok.
isNot(tok::colon) &&
845 Tok.
isNot(tok::kw_noexcept);
851 return FormatTok->
is(tok::identifier) &&
866 FormatTok->
isOneOf(tok::kw_true, tok::kw_false) ||
877 tok::kw_if, tok::kw_else,
879 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
881 tok::kw_switch, tok::kw_case,
883 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.
kw_finally,
885 tok::kw_const, tok::kw_class, Keywords.
kw_var, Keywords.
kw_let,
898 void UnwrappedLineParser::readTokenWithJavaScriptASI() {
904 CommentsBeforeNextToken.empty()
906 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
911 bool PreviousStartsTemplateExpr =
912 Previous->
is(TT_TemplateString) && Previous->
TokenText.endswith(
"${");
913 if (PreviousMustBeValue || Previous->
is(tok::r_paren)) {
916 bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
918 return LineNode.Tok->is(tok::at);
919 }) != Line->Tokens.end();
923 if (Next->
is(tok::exclaim) && PreviousMustBeValue)
924 return addUnwrappedLine();
926 bool NextEndsTemplateExpr =
927 Next->
is(TT_TemplateString) && Next->
TokenText.startswith(
"}");
928 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
929 (PreviousMustBeValue ||
930 Previous->
isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
932 return addUnwrappedLine();
933 if ((PreviousMustBeValue || Previous->
is(tok::r_paren)) &&
935 return addUnwrappedLine();
938 void UnwrappedLineParser::parseStructuralElement() {
939 assert(!FormatTok->
is(tok::l_brace));
941 FormatTok->
is(tok::pp_include)) {
943 if (FormatTok->
is(tok::string_literal))
951 if (FormatTok->
Tok.
is(tok::l_brace)) {
957 case tok::objc_public:
958 case tok::objc_protected:
959 case tok::objc_package:
960 case tok::objc_private:
961 return parseAccessSpecifier();
962 case tok::objc_interface:
963 case tok::objc_implementation:
964 return parseObjCInterfaceOrImplementation();
965 case tok::objc_protocol:
966 return parseObjCProtocol();
969 case tok::objc_optional:
970 case tok::objc_required:
974 case tok::objc_autoreleasepool:
976 if (FormatTok->
Tok.
is(tok::l_brace)) {
994 if (FormatTok->
is(tok::l_brace)) {
995 FormatTok->
Type = TT_InlineASMBrace;
998 if (FormatTok->
is(tok::r_brace)) {
999 FormatTok->
Type = TT_InlineASMBrace;
1009 case tok::kw_namespace:
1012 case tok::kw_inline:
1014 if (FormatTok->
Tok.
is(tok::kw_namespace)) {
1019 case tok::kw_public:
1020 case tok::kw_protected:
1021 case tok::kw_private:
1026 parseAccessSpecifier();
1033 parseForOrWhileLoop();
1038 case tok::kw_switch:
1044 case tok::kw_default:
1061 case tok::kw_extern:
1063 if (FormatTok->
Tok.
is(tok::string_literal)) {
1065 if (FormatTok->
Tok.
is(tok::l_brace)) {
1070 parseBlock(
true,
false);
1077 case tok::kw_export:
1079 parseJavaScriptEs6ImportExport();
1083 case tok::identifier:
1084 if (FormatTok->
is(TT_ForEachMacro)) {
1085 parseForOrWhileLoop();
1088 if (FormatTok->
is(TT_MacroBlockBegin)) {
1089 parseBlock(
false,
true,
1095 parseJavaScriptEs6ImportExport();
1100 if (FormatTok->
is(tok::kw_public))
1102 if (!FormatTok->
is(tok::string_literal))
1105 if (FormatTok->
is(tok::semi))
1111 if (Style.
isCpp() &&
1115 if (FormatTok->
is(tok::colon)) {
1131 if (FormatTok->
Tok.
is(tok::l_brace)) {
1138 if (Previous && Previous->
is(tok::less)) {
1148 if (!Style.
isCpp()) {
1153 case tok::kw_typedef:
1159 case tok::kw_struct:
1168 if (FormatTok->
is(tok::semi))
1178 FormatTok->
is(tok::kw_class))
1196 case tok::kw_operator:
1206 if (FormatTok->
is(tok::l_paren))
1208 if (FormatTok->
is(tok::l_brace))
1212 if (!tryToParseBracedList()) {
1219 FormatTok->
Type = TT_FunctionLBrace;
1231 case tok::identifier: {
1232 if (FormatTok->
is(TT_MacroBlockEnd)) {
1241 size_t TokenCount = Line->Tokens.size();
1244 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1246 tryToParseJSFunction();
1273 if (Line->Tokens.size() == 1 &&
1277 if (FormatTok->
Tok.
is(tok::colon) && !Line->MustBeDeclaration) {
1278 Line->Tokens.begin()->Tok->MustBreakBefore =
true;
1284 bool FunctionLike = FormatTok->
is(tok::l_paren);
1288 bool FollowedByNewline =
1289 CommentsBeforeNextToken.empty()
1291 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1293 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
1305 if (FormatTok->
is(TT_JsFatArrow)) {
1307 if (FormatTok->
is(tok::l_brace))
1313 if (FormatTok->
Tok.
is(tok::l_brace)) {
1317 FormatTok->
Tok.
is(tok::less)) {
1319 parseBracedList(
false,
1336 bool UnwrappedLineParser::tryToParseLambda() {
1337 if (!Style.
isCpp()) {
1341 assert(FormatTok->
is(tok::l_square));
1343 if (!tryToParseLambdaIntroducer())
1346 while (FormatTok->
isNot(tok::l_brace)) {
1363 case tok::identifier:
1364 case tok::numeric_constant:
1365 case tok::coloncolon:
1366 case tok::kw_mutable:
1370 FormatTok->
Type = TT_LambdaArrow;
1377 LSquare.Type = TT_LambdaLSquare;
1382 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1385 (Previous->
isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1397 void UnwrappedLineParser::tryToParseJSFunction() {
1406 if (FormatTok->
is(tok::star)) {
1407 FormatTok->
Type = TT_OverloadedOperator;
1412 if (FormatTok->
is(tok::identifier))
1415 if (FormatTok->
isNot(tok::l_paren))
1421 if (FormatTok->
is(tok::colon)) {
1427 if (FormatTok->
is(tok::l_brace))
1428 tryToParseBracedList();
1430 while (!FormatTok->
isOneOf(tok::l_brace, tok::semi) && !eof())
1434 if (FormatTok->
is(tok::semi))
1440 bool UnwrappedLineParser::tryToParseBracedList() {
1442 calculateBraceTypes();
1451 bool UnwrappedLineParser::parseBracedList(
bool ContinueOnSemicolons,
1453 bool HasError =
false;
1461 tryToParseJSFunction();
1464 if (FormatTok->
is(TT_JsFatArrow)) {
1468 if (FormatTok->
is(tok::l_brace)) {
1473 if (FormatTok->
is(tok::l_brace)) {
1475 if (tryToParseBracedList())
1480 if (FormatTok->
Tok.
getKind() == ClosingBraceKind) {
1487 if (FormatTok->
is(tok::l_brace)) {
1499 if (FormatTok->
is(tok::l_brace))
1514 parseBracedList(
false,
1530 if (!ContinueOnSemicolons)
1545 void UnwrappedLineParser::parseParens() {
1546 assert(FormatTok->
Tok.
is(tok::l_paren) &&
"'(' expected.");
1565 if (!tryToParseBracedList())
1570 if (FormatTok->
Tok.
is(tok::l_brace)) {
1581 case tok::identifier:
1585 tryToParseJSFunction();
1597 if (!LambdaIntroducer) {
1598 assert(FormatTok->
Tok.
is(tok::l_square) &&
"'[' expected.");
1599 if (tryToParseLambda())
1616 case tok::l_brace: {
1617 if (!tryToParseBracedList())
1623 if (FormatTok->
Tok.
is(tok::l_brace)) {
1635 void UnwrappedLineParser::parseIfThenElse() {
1636 assert(FormatTok->
Tok.
is(tok::kw_if) &&
"'if' expected");
1638 if (FormatTok->
Tok.
is(tok::kw_constexpr))
1640 if (FormatTok->
Tok.
is(tok::l_paren))
1642 bool NeedsUnwrappedLine =
false;
1643 if (FormatTok->
Tok.
is(tok::l_brace)) {
1649 NeedsUnwrappedLine =
true;
1653 parseStructuralElement();
1656 if (FormatTok->
Tok.
is(tok::kw_else)) {
1658 if (FormatTok->
Tok.
is(tok::l_brace)) {
1662 }
else if (FormatTok->
Tok.
is(tok::kw_if)) {
1667 parseStructuralElement();
1672 }
else if (NeedsUnwrappedLine) {
1677 void UnwrappedLineParser::parseTryCatch() {
1678 assert(FormatTok->
isOneOf(tok::kw_try, tok::kw___try) &&
"'try' expected");
1680 bool NeedsUnwrappedLine =
false;
1681 if (FormatTok->
is(tok::colon)) {
1684 while (FormatTok->
is(tok::identifier)) {
1686 if (FormatTok->
is(tok::l_paren))
1688 if (FormatTok->
is(tok::comma))
1696 if (FormatTok->
is(tok::l_brace)) {
1702 NeedsUnwrappedLine =
true;
1704 }
else if (!FormatTok->
is(tok::kw_catch)) {
1710 parseStructuralElement();
1714 if (FormatTok->
is(tok::at))
1717 tok::kw___finally) ||
1725 while (FormatTok->
isNot(tok::l_brace)) {
1726 if (FormatTok->
is(tok::l_paren)) {
1734 NeedsUnwrappedLine =
false;
1740 NeedsUnwrappedLine =
true;
1742 if (NeedsUnwrappedLine)
1746 void UnwrappedLineParser::parseNamespace() {
1747 assert(FormatTok->
Tok.
is(tok::kw_namespace) &&
"'namespace' expected");
1751 while (FormatTok->
isOneOf(tok::identifier, tok::coloncolon))
1753 if (FormatTok->
Tok.
is(tok::l_brace)) {
1759 DeclarationScopeStack.size() > 1);
1760 parseBlock(
true, AddLevel);
1763 if (FormatTok->
Tok.
is(tok::semi))
1770 void UnwrappedLineParser::parseNew() {
1771 assert(FormatTok->
is(tok::kw_new) &&
"'new' expected");
1779 if (FormatTok->
isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1783 if (FormatTok->
is(tok::l_paren)) {
1787 if (FormatTok->
is(tok::l_brace))
1795 void UnwrappedLineParser::parseForOrWhileLoop() {
1796 assert(FormatTok->
isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
1797 "'for', 'while' or foreach macro expected");
1803 if (FormatTok->
Tok.
is(tok::l_paren))
1805 if (FormatTok->
Tok.
is(tok::l_brace)) {
1812 parseStructuralElement();
1817 void UnwrappedLineParser::parseDoWhile() {
1818 assert(FormatTok->
Tok.
is(tok::kw_do) &&
"'do' expected");
1820 if (FormatTok->
Tok.
is(tok::l_brace)) {
1828 parseStructuralElement();
1833 if (!FormatTok->
Tok.
is(tok::kw_while)) {
1839 parseStructuralElement();
1842 void UnwrappedLineParser::parseLabel() {
1844 unsigned OldLineLevel = Line->Level;
1845 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1847 if (CommentsBeforeNextToken.empty() && FormatTok->
Tok.
is(tok::l_brace)) {
1850 if (FormatTok->
Tok.
is(tok::kw_break)) {
1853 parseStructuralElement();
1857 if (FormatTok->
is(tok::semi))
1861 Line->Level = OldLineLevel;
1862 if (FormatTok->
isNot(tok::l_brace)) {
1863 parseStructuralElement();
1868 void UnwrappedLineParser::parseCaseLabel() {
1869 assert(FormatTok->
Tok.
is(tok::kw_case) &&
"'case' expected");
1873 }
while (!eof() && !FormatTok->
Tok.
is(tok::colon));
1877 void UnwrappedLineParser::parseSwitch() {
1878 assert(FormatTok->
Tok.
is(tok::kw_switch) &&
"'switch' expected");
1880 if (FormatTok->
Tok.
is(tok::l_paren))
1882 if (FormatTok->
Tok.
is(tok::l_brace)) {
1889 parseStructuralElement();
1894 void UnwrappedLineParser::parseAccessSpecifier() {
1900 if (FormatTok->
Tok.
is(tok::colon))
1905 bool UnwrappedLineParser::parseEnum() {
1907 if (FormatTok->
Tok.
is(tok::kw_enum))
1914 FormatTok->
isOneOf(tok::colon, tok::question))
1918 if (FormatTok->
Tok.
is(tok::kw_class) || FormatTok->
Tok.
is(tok::kw_struct))
1922 FormatTok->
isOneOf(tok::colon, tok::coloncolon, tok::less,
1923 tok::greater, tok::comma, tok::question)) {
1926 if (FormatTok->
is(tok::l_paren))
1928 if (FormatTok->
is(tok::identifier)) {
1932 if (Style.
isCpp() && FormatTok->
is(tok::identifier))
1938 if (FormatTok->
isNot(tok::l_brace))
1944 parseJavaEnumBody();
1954 bool HasError = !parseBracedList(
true);
1956 if (FormatTok->
is(tok::semi))
1967 void UnwrappedLineParser::parseJavaEnumBody() {
1972 bool IsSimple =
true;
1975 if (Tok->
is(tok::r_brace))
1977 if (Tok->
isOneOf(tok::l_brace, tok::semi)) {
2002 if (FormatTok->
is(tok::l_brace)) {
2004 parseBlock(
true,
true,
2006 }
else if (FormatTok->
is(tok::l_paren)) {
2008 }
else if (FormatTok->
is(tok::comma)) {
2011 }
else if (FormatTok->
is(tok::semi)) {
2015 }
else if (FormatTok->
is(tok::r_brace)) {
2030 void UnwrappedLineParser::parseRecord(
bool ParseAsExpr) {
2036 while (FormatTok->
isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2037 tok::kw___attribute, tok::kw___declspec,
2041 FormatTok->
isOneOf(tok::period, tok::comma))) {
2048 if (FormatTok->
is(tok::l_brace)) {
2049 tryToParseBracedList();
2053 bool IsNonMacroIdentifier =
2054 FormatTok->
is(tok::identifier) &&
2058 if (!IsNonMacroIdentifier && FormatTok->
Tok.
is(tok::l_paren))
2072 if (FormatTok->
isOneOf(tok::colon, tok::less)) {
2074 if (FormatTok->
is(tok::l_brace)) {
2075 calculateBraceTypes(
true);
2076 if (!tryToParseBracedList())
2079 if (FormatTok->
Tok.
is(tok::semi))
2084 if (FormatTok->
Tok.
is(tok::l_brace)) {
2091 parseBlock(
true,
true,
2100 void UnwrappedLineParser::parseObjCProtocolList() {
2101 assert(FormatTok->
Tok.
is(tok::less) &&
"'<' expected.");
2104 while (!eof() && FormatTok->
Tok.
isNot(tok::greater));
2108 void UnwrappedLineParser::parseObjCUntilAtEnd() {
2115 if (FormatTok->
is(tok::l_brace)) {
2119 }
else if (FormatTok->
is(tok::r_brace)) {
2124 parseStructuralElement();
2129 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
2134 if (FormatTok->
Tok.
is(tok::colon)) {
2137 }
else if (FormatTok->
Tok.
is(tok::l_paren))
2141 if (FormatTok->
Tok.
is(tok::less))
2142 parseObjCProtocolList();
2144 if (FormatTok->
Tok.
is(tok::l_brace)) {
2154 parseObjCUntilAtEnd();
2157 void UnwrappedLineParser::parseObjCProtocol() {
2161 if (FormatTok->
Tok.
is(tok::less))
2162 parseObjCProtocolList();
2165 if (FormatTok->
Tok.
is(tok::semi)) {
2167 return addUnwrappedLine();
2171 parseObjCUntilAtEnd();
2174 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
2175 bool IsImport = FormatTok->
is(Keywords.
kw_import);
2176 assert(IsImport || FormatTok->
is(tok::kw_export));
2180 if (FormatTok->
is(tok::kw_default))
2197 if (!IsImport && !FormatTok->
isOneOf(tok::l_brace, tok::star) &&
2202 if (FormatTok->
is(tok::semi))
2204 if (Line->Tokens.empty()) {
2209 if (FormatTok->
is(tok::l_brace)) {
2220 StringRef Prefix =
"") {
2221 llvm::dbgs() << Prefix <<
"Line(" << Line.
Level 2224 for (std::list<UnwrappedLineNode>::const_iterator I = Line.
Tokens.begin(),
2227 llvm::dbgs() << I->Tok->Tok.getName() <<
"[" 2228 <<
"T=" << I->Tok->Type <<
", OC=" << I->Tok->OriginalColumn
2231 for (std::list<UnwrappedLineNode>::const_iterator I = Line.
Tokens.begin(),
2242 llvm::dbgs() <<
"\n";
2245 void UnwrappedLineParser::addUnwrappedLine() {
2246 if (Line->Tokens.empty())
2249 if (CurrentLines == &Lines)
2252 CurrentLines->push_back(std::move(*Line));
2253 Line->Tokens.clear();
2255 Line->FirstStartColumn = 0;
2256 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
2257 CurrentLines->append(
2258 std::make_move_iterator(PreprocessorDirectives.begin()),
2259 std::make_move_iterator(PreprocessorDirectives.end()));
2260 PreprocessorDirectives.clear();
2266 bool UnwrappedLineParser::eof()
const {
return FormatTok->
Tok.
is(
tok::eof); }
2268 bool UnwrappedLineParser::isOnNewLine(
const FormatToken &FormatTok) {
2277 llvm::Regex &CommentPragmasRegex) {
2281 StringRef IndentContent = FormatTok.
TokenText;
2282 if (FormatTok.
TokenText.startswith(
"//") ||
2284 IndentContent = FormatTok.
TokenText.substr(2);
2285 if (CommentPragmasRegex.match(IndentContent))
2360 if (PreviousToken && PreviousToken->
is(tok::l_brace) &&
2361 isLineComment(*Node.
Tok)) {
2362 MinColumnToken = PreviousToken;
2365 PreviousToken = Node.
Tok;
2369 MinColumnToken = Node.
Tok;
2372 if (PreviousToken && PreviousToken->
is(tok::l_brace)) {
2373 MinColumnToken = PreviousToken;
2376 return continuesLineComment(FormatTok, Line.
Tokens.back().Tok,
2380 void UnwrappedLineParser::flushComments(
bool NewlineBeforeNext) {
2381 bool JustComments = Line->Tokens.empty();
2383 I = CommentsBeforeNextToken.begin(),
2384 E = CommentsBeforeNextToken.end();
2396 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
2400 if (NewlineBeforeNext && JustComments)
2402 CommentsBeforeNextToken.clear();
2405 void UnwrappedLineParser::nextToken(
int LevelDifference) {
2408 flushComments(isOnNewLine(*FormatTok));
2409 pushToken(FormatTok);
2412 readToken(LevelDifference);
2414 readTokenWithJavaScriptASI();
2418 void UnwrappedLineParser::distributeComments(
2439 if (Comments.empty())
2441 bool ShouldPushCommentsInCurrentLine =
true;
2442 bool HasTrailAlignedWithNextToken =
false;
2443 unsigned StartOfTrailAlignedWithNextToken = 0;
2446 for (
unsigned i = Comments.size() - 1; i > 0; --i) {
2448 HasTrailAlignedWithNextToken =
true;
2449 StartOfTrailAlignedWithNextToken = i;
2453 for (
unsigned i = 0, e = Comments.size(); i < e; ++i) {
2455 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
2462 (isOnNewLine(*FormatTok) || FormatTok->
IsFirst)) {
2463 ShouldPushCommentsInCurrentLine =
false;
2465 if (ShouldPushCommentsInCurrentLine) {
2466 pushToken(FormatTok);
2468 CommentsBeforeNextToken.push_back(FormatTok);
2473 void UnwrappedLineParser::readToken(
int LevelDifference) {
2478 while (!Line->InPPDirective && FormatTok->
Tok.
is(tok::hash) &&
2480 distributeComments(Comments, FormatTok);
2484 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
2486 assert((LevelDifference >= 0 ||
2487 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2488 "LevelDifference makes Line->Level negative");
2489 Line->Level += LevelDifference;
2493 flushComments(isOnNewLine(*FormatTok));
2496 while (FormatTok->
Type == TT_ConflictStart ||
2497 FormatTok->
Type == TT_ConflictEnd ||
2498 FormatTok->
Type == TT_ConflictAlternative) {
2499 if (FormatTok->
Type == TT_ConflictStart) {
2500 conditionalCompilationStart(
false);
2501 }
else if (FormatTok->
Type == TT_ConflictAlternative) {
2502 conditionalCompilationAlternative();
2503 }
else if (FormatTok->
Type == TT_ConflictEnd) {
2504 conditionalCompilationEnd();
2510 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
2511 !Line->InPPDirective) {
2515 if (!FormatTok->
Tok.
is(tok::comment)) {
2516 distributeComments(Comments, FormatTok);
2521 Comments.push_back(FormatTok);
2524 distributeComments(Comments,
nullptr);
2530 if (MustBreakBeforeNextToken) {
2531 Line->Tokens.back().Tok->MustBreakBefore =
true;
2532 MustBreakBeforeNextToken =
false;
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {...
Parser - This implements a parser for the C family of languages.
bool isAnyIdentifier() const
Return true if this is a raw identifier (when lexing in raw mode) or a non-keyword identifier (when l...
tok::TokenKind getKind() const
bool isLiteral() const
Return true if this is a "literal", like a numeric constant, string, etc.
Token - This structure provides full information about a lexed token.
void setKind(tok::TokenKind K)
bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const
Return true if we have an ObjC keyword identifier.
const AnnotatedLine * Line
const FunctionProtoType * T
SourceLocation getEnd() const
IdentifierInfo * getIdentifierInfo() const
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.
tok::ObjCKeywordKind getObjCKeywordID() const
Return the ObjC keyword kind.
ast_type_traits::DynTypedNode Node
bool isNot(tok::TokenKind K) const
Dataflow Directional Tag Classes.
tok::PPKeywordKind getPPKeywordID() const
Return the preprocessor keyword ID for this identifier.
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Represents a complete lambda introducer.
SourceLocation getBegin() const
void startToken()
Reset all flags to cleared.