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) {
86 FakeEOF.Tok.startToken();
93 ~ScopedMacroState()
override {
94 TokenSource = PreviousTokenSource;
97 Line.
Level = PreviousLineLevel;
104 PreviousToken =
Token;
105 Token = PreviousTokenSource->getNextToken();
111 unsigned getPosition()
override {
return PreviousTokenSource->getPosition(); }
114 PreviousToken =
nullptr;
115 Token = PreviousTokenSource->setPosition(Position);
122 !continuesLineComment(*
Token, PreviousToken,
130 unsigned PreviousLineLevel;
142 bool SwitchToPreprocessorLines =
false)
143 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
144 if (SwitchToPreprocessorLines)
145 Parser.CurrentLines = &Parser.PreprocessorDirectives;
146 else if (!Parser.Line->Tokens.empty())
147 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
148 PreBlockLine = std::move(Parser.Line);
149 Parser.Line = llvm::make_unique<UnwrappedLine>();
150 Parser.Line->Level = PreBlockLine->Level;
151 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
155 if (!
Parser.Line->Tokens.empty()) {
156 Parser.addUnwrappedLine();
158 assert(
Parser.Line->Tokens.empty());
159 Parser.Line = std::move(PreBlockLine);
160 if (
Parser.CurrentLines == &
Parser.PreprocessorDirectives)
161 Parser.MustBreakBeforeNextToken =
true;
162 Parser.CurrentLines = OriginalLines;
168 std::unique_ptr<UnwrappedLine> PreBlockLine;
176 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
178 Parser->addUnwrappedLine();
186 unsigned OldLineLevel;
194 : Tokens(Tokens), Position(-1) {}
198 return Tokens[Position];
202 assert(Position >= 0);
208 return Tokens[Position];
211 void reset() { Position = -1; }
222 unsigned FirstStartColumn,
226 CurrentLines(&Lines), Style(Style), Keywords(Keywords),
227 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
228 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
229 IncludeGuard(Style.IndentPPDirectives ==
FormatStyle::PPDIS_None
232 IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
234 void UnwrappedLineParser::reset() {
239 IncludeGuardToken =
nullptr;
241 CommentsBeforeNextToken.clear();
243 MustBreakBeforeNextToken =
false;
244 PreprocessorDirectives.clear();
245 CurrentLines = &Lines;
246 DeclarationScopeStack.clear();
248 Line->FirstStartColumn = FirstStartColumn;
252 IndexedTokenSource TokenSource(AllTokens);
253 Line->FirstStartColumn = FirstStartColumn;
255 LLVM_DEBUG(llvm::dbgs() <<
"----\n");
257 Tokens = &TokenSource;
265 if (IncludeGuard == IG_Found)
266 for (
auto &Line : Lines)
267 if (Line.InPPDirective && Line.Level > 0)
271 pushToken(FormatTok);
281 while (!PPLevelBranchIndex.empty() &&
282 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
283 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
284 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
286 if (!PPLevelBranchIndex.empty()) {
287 ++PPLevelBranchIndex.back();
288 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
289 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
291 }
while (!PPLevelBranchIndex.empty());
294 void UnwrappedLineParser::parseFile() {
297 bool MustBeDeclaration =
299 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
316 !CommentsBeforeNextToken.empty())
322 void UnwrappedLineParser::parseLevel(
bool HasOpeningBrace) {
323 bool SwitchLabelEncountered =
false;
326 if (FormatTok->
Type == TT_MacroBlockBegin) {
328 }
else if (FormatTok->
Type == TT_MacroBlockEnd) {
340 if (!FormatTok->
is(TT_MacroBlockBegin) && tryToParseBracedList())
351 case tok::kw_default: {
356 }
while (Next && Next->
is(tok::comment));
358 if (Next && Next->
isNot(tok::colon)) {
361 parseStructuralElement();
369 Line->MustBeDeclaration) {
371 parseStructuralElement();
374 if (!SwitchLabelEncountered &&
377 SwitchLabelEncountered =
true;
378 parseStructuralElement();
381 parseStructuralElement();
387 void UnwrappedLineParser::calculateBraceTypes(
bool ExpectClassBody) {
399 assert(Tok->
Tok.
is(tok::l_brace));
403 unsigned ReadTokens = 0;
407 }
while (NextTok->
is(tok::comment));
412 if (PrevTok->
isOneOf(tok::colon, tok::less))
423 else if (PrevTok->
is(tok::r_paren))
429 LBraceStack.push_back(Tok);
432 if (LBraceStack.empty())
434 if (LBraceStack.back()->BlockKind ==
BK_Unknown) {
435 bool ProbablyBracedList =
false;
437 ProbablyBracedList = NextTok->
isOneOf(tok::comma, tok::r_square);
441 bool NextIsObjCMethod = NextTok->
isOneOf(tok::plus, tok::minus) &&
456 (Style.
isCpp() && NextTok->
is(tok::l_paren)) ||
457 NextTok->
isOneOf(tok::comma, tok::period, tok::colon,
458 tok::r_paren, tok::r_square, tok::l_brace,
460 (NextTok->
is(tok::identifier) &&
461 !PrevTok->
isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
462 (NextTok->
is(tok::semi) &&
463 (!ExpectClassBody || LBraceStack.size() != 1)) ||
465 if (NextTok->
is(tok::l_square)) {
470 ProbablyBracedList = NextTok->
isNot(tok::l_square);
473 if (ProbablyBracedList) {
478 LBraceStack.back()->BlockKind =
BK_Block;
481 LBraceStack.pop_back();
483 case tok::identifier:
484 if (!Tok->
is(TT_StatementMacro))
495 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind ==
BK_Unknown)
496 LBraceStack.back()->BlockKind =
BK_Block;
506 for (
unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
508 LBraceStack[i]->BlockKind =
BK_Block;
517 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
520 size_t UnwrappedLineParser::computePPHash()
const {
522 for (
const auto &i : PPStack) {
529 void UnwrappedLineParser::parseBlock(
bool MustBeDeclaration,
bool AddLevel,
531 assert(FormatTok->
isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
532 "'{' or macro block token expected");
533 const bool MacroBlock = FormatTok->
is(TT_MacroBlockBegin);
536 size_t PPStartHash = computePPHash();
538 unsigned InitialLevel = Line->Level;
539 nextToken(AddLevel ? 1 : 0);
541 if (MacroBlock && FormatTok->
is(tok::l_paren))
544 size_t NbPreprocessorDirectives =
545 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
547 size_t OpeningLineIndex =
548 CurrentLines->empty()
550 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
552 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
561 if (MacroBlock ? !FormatTok->
is(TT_MacroBlockEnd)
562 : !FormatTok->
is(tok::r_brace)) {
563 Line->Level = InitialLevel;
568 size_t PPEndHash = computePPHash();
571 nextToken(AddLevel ? -1 : 0);
573 if (MacroBlock && FormatTok->
is(tok::l_paren))
576 if (MunchSemi && FormatTok->
Tok.
is(tok::semi))
578 Line->Level = InitialLevel;
580 if (PPStartHash == PPEndHash) {
581 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
584 (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
585 CurrentLines->size() - 1;
593 if (Line.
Tokens.size() < 4)
595 auto I = Line.
Tokens.begin();
596 if (I->Tok->TokenText !=
"goog")
599 if (I->Tok->isNot(tok::period))
602 if (I->Tok->TokenText !=
"scope")
605 return I->Tok->is(tok::l_paren);
614 if (Line.
Tokens.size() < 3)
616 auto I = Line.
Tokens.begin();
617 if (I->Tok->isNot(tok::l_paren))
623 return I->Tok->is(tok::l_paren);
628 if (InitialToken.
is(tok::kw_namespace))
630 if (InitialToken.
is(tok::kw_class))
632 if (InitialToken.
is(tok::kw_union))
634 if (InitialToken.
is(tok::kw_struct))
639 void UnwrappedLineParser::parseChildBlock() {
646 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
648 Line->Level += SkipIndent ? 0 : 1;
650 flushComments(isOnNewLine(*FormatTok));
651 Line->Level -= SkipIndent ? 0 : 1;
656 void UnwrappedLineParser::parsePPDirective() {
657 assert(FormatTok->
Tok.
is(tok::hash) &&
"'#' expected");
658 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
692 void UnwrappedLineParser::conditionalCompilationCondition(
bool Unreachable) {
693 size_t Line = CurrentLines->size();
694 if (CurrentLines == &PreprocessorDirectives)
695 Line += Lines.size();
698 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
699 PPStack.push_back({PP_Unreachable, Line});
701 PPStack.push_back({PP_Conditional, Line});
704 void UnwrappedLineParser::conditionalCompilationStart(
bool Unreachable) {
706 assert(PPBranchLevel >= 0 && PPBranchLevel <= (
int)PPLevelBranchIndex.size());
707 if (PPBranchLevel == (
int)PPLevelBranchIndex.size()) {
708 PPLevelBranchIndex.push_back(0);
709 PPLevelBranchCount.push_back(0);
711 PPChainBranchIndex.push(0);
712 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
713 conditionalCompilationCondition(Unreachable || Skip);
716 void UnwrappedLineParser::conditionalCompilationAlternative() {
717 if (!PPStack.empty())
719 assert(PPBranchLevel < (
int)PPLevelBranchIndex.size());
720 if (!PPChainBranchIndex.empty())
721 ++PPChainBranchIndex.top();
722 conditionalCompilationCondition(
723 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
724 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
727 void UnwrappedLineParser::conditionalCompilationEnd() {
728 assert(PPBranchLevel < (
int)PPLevelBranchIndex.size());
729 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
730 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
731 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
735 if (PPBranchLevel > -1)
737 if (!PPChainBranchIndex.empty())
738 PPChainBranchIndex.pop();
739 if (!PPStack.empty())
743 void UnwrappedLineParser::parsePPIf(
bool IfDef) {
744 bool IfNDef = FormatTok->
is(tok::pp_ifndef);
746 bool Unreachable =
false;
747 if (!IfDef && (FormatTok->
is(tok::kw_false) || FormatTok->
TokenText ==
"0"))
749 if (IfDef && !IfNDef && FormatTok->
TokenText ==
"SWIG")
751 conditionalCompilationStart(Unreachable);
752 FormatToken *IfCondition = FormatTok;
755 bool MaybeIncludeGuard = IfNDef;
756 if (IncludeGuard == IG_Inited && MaybeIncludeGuard)
757 for (
auto &Line : Lines) {
758 if (!Line.Tokens.front().Tok->is(tok::comment)) {
759 MaybeIncludeGuard =
false;
760 IncludeGuard = IG_Rejected;
767 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
768 IncludeGuard = IG_IfNdefed;
769 IncludeGuardToken = IfCondition;
773 void UnwrappedLineParser::parsePPElse() {
775 if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
776 IncludeGuard = IG_Rejected;
777 conditionalCompilationAlternative();
778 if (PPBranchLevel > -1)
784 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
786 void UnwrappedLineParser::parsePPEndIf() {
787 conditionalCompilationEnd();
792 FormatToken *PeekNext = AllTokens[TokenPosition];
793 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
796 IncludeGuard = IG_Found;
799 void UnwrappedLineParser::parsePPDefine() {
802 if (FormatTok->
Tok.
getKind() != tok::identifier) {
803 IncludeGuard = IG_Rejected;
804 IncludeGuardToken =
nullptr;
809 if (IncludeGuard == IG_IfNdefed &&
811 IncludeGuard = IG_Defined;
812 IncludeGuardToken =
nullptr;
813 for (
auto &Line : Lines) {
814 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
815 IncludeGuard = IG_Rejected;
822 if (FormatTok->
Tok.
getKind() == tok::l_paren &&
828 Line->Level += PPBranchLevel + 1;
840 void UnwrappedLineParser::parsePPUnknown() {
845 Line->Level += PPBranchLevel + 1;
855 return Tok.
isNot(tok::semi) && Tok.
isNot(tok::l_brace) &&
856 Tok.
isNot(tok::l_square) &&
859 Tok.
isNot(tok::period) && Tok.
isNot(tok::periodstar) &&
860 Tok.
isNot(tok::arrow) && Tok.
isNot(tok::arrowstar) &&
861 Tok.
isNot(tok::less) && Tok.
isNot(tok::greater) &&
862 Tok.
isNot(tok::slash) && Tok.
isNot(tok::percent) &&
863 Tok.
isNot(tok::lessless) && Tok.
isNot(tok::greatergreater) &&
864 Tok.
isNot(tok::equal) && Tok.
isNot(tok::plusequal) &&
865 Tok.
isNot(tok::minusequal) && Tok.
isNot(tok::starequal) &&
866 Tok.
isNot(tok::slashequal) && Tok.
isNot(tok::percentequal) &&
867 Tok.
isNot(tok::ampequal) && Tok.
isNot(tok::pipeequal) &&
868 Tok.
isNot(tok::caretequal) && Tok.
isNot(tok::greatergreaterequal) &&
869 Tok.
isNot(tok::lesslessequal) &&
873 Tok.
isNot(tok::colon) &&
875 Tok.
isNot(tok::kw_noexcept);
881 return FormatTok->
is(tok::identifier) &&
896 FormatTok->
isOneOf(tok::kw_true, tok::kw_false) ||
907 tok::kw_if, tok::kw_else,
909 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
911 tok::kw_switch, tok::kw_case,
913 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.
kw_finally,
915 tok::kw_const, tok::kw_class, Keywords.
kw_var, Keywords.
kw_let,
928 void UnwrappedLineParser::readTokenWithJavaScriptASI() {
934 CommentsBeforeNextToken.empty()
936 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
941 bool PreviousStartsTemplateExpr =
942 Previous->
is(TT_TemplateString) && Previous->
TokenText.endswith(
"${");
943 if (PreviousMustBeValue || Previous->
is(tok::r_paren)) {
946 bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
948 return LineNode.Tok->is(tok::at);
949 }) != Line->Tokens.end();
953 if (Next->
is(tok::exclaim) && PreviousMustBeValue)
954 return addUnwrappedLine();
956 bool NextEndsTemplateExpr =
957 Next->
is(TT_TemplateString) && Next->
TokenText.startswith(
"}");
958 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
959 (PreviousMustBeValue ||
960 Previous->
isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
962 return addUnwrappedLine();
963 if ((PreviousMustBeValue || Previous->
is(tok::r_paren)) &&
965 return addUnwrappedLine();
968 void UnwrappedLineParser::parseStructuralElement() {
969 assert(!FormatTok->
is(tok::l_brace));
971 FormatTok->
is(tok::pp_include)) {
973 if (FormatTok->
is(tok::string_literal))
981 if (FormatTok->
is(tok::l_brace)) {
982 FormatTok->
Type = TT_InlineASMBrace;
985 if (FormatTok->
is(tok::r_brace)) {
986 FormatTok->
Type = TT_InlineASMBrace;
996 case tok::kw_namespace:
1000 case tok::kw_protected:
1001 case tok::kw_private:
1006 parseAccessSpecifier();
1013 parseForOrWhileLoop();
1018 case tok::kw_switch:
1024 case tok::kw_default:
1029 if (FormatTok->
is(tok::colon)) {
1045 case tok::kw_extern:
1047 if (FormatTok->
Tok.
is(tok::string_literal)) {
1049 if (FormatTok->
Tok.
is(tok::l_brace)) {
1054 parseBlock(
true,
false);
1061 case tok::kw_export:
1063 parseJavaScriptEs6ImportExport();
1070 case tok::kw_inline:
1072 if (FormatTok->
Tok.
is(tok::kw_namespace)) {
1077 case tok::identifier:
1078 if (FormatTok->
is(TT_ForEachMacro)) {
1079 parseForOrWhileLoop();
1082 if (FormatTok->
is(TT_MacroBlockBegin)) {
1083 parseBlock(
false,
true,
1089 parseJavaScriptEs6ImportExport();
1094 if (FormatTok->
is(tok::kw_public))
1096 if (!FormatTok->
is(tok::string_literal))
1099 if (FormatTok->
is(tok::semi))
1105 if (Style.
isCpp() &&
1109 if (FormatTok->
is(tok::colon)) {
1115 if (Style.
isCpp() && FormatTok->
is(TT_StatementMacro)) {
1116 parseStatementMacro();
1129 if (FormatTok->
Tok.
is(tok::l_brace)) {
1139 case tok::objc_public:
1140 case tok::objc_protected:
1141 case tok::objc_package:
1142 case tok::objc_private:
1143 return parseAccessSpecifier();
1144 case tok::objc_interface:
1145 case tok::objc_implementation:
1146 return parseObjCInterfaceOrImplementation();
1147 case tok::objc_protocol:
1148 if (parseObjCProtocol())
1153 case tok::objc_optional:
1154 case tok::objc_required:
1158 case tok::objc_autoreleasepool:
1160 if (FormatTok->
Tok.
is(tok::l_brace)) {
1167 case tok::objc_synchronized:
1169 if (FormatTok->
Tok.
is(tok::l_paren))
1172 if (FormatTok->
Tok.
is(tok::l_brace)) {
1190 if (Previous && Previous->
is(tok::less)) {
1200 if (!Style.
isCpp()) {
1205 case tok::kw_typedef:
1211 case tok::kw_struct:
1220 if (FormatTok->
is(tok::semi))
1230 FormatTok->
is(tok::kw_class))
1248 case tok::kw_operator:
1258 if (FormatTok->
is(tok::l_paren))
1260 if (FormatTok->
is(tok::l_brace))
1264 if (!tryToParseBracedList()) {
1271 FormatTok->
Type = TT_FunctionLBrace;
1285 case tok::identifier: {
1286 if (FormatTok->
is(TT_MacroBlockEnd)) {
1295 size_t TokenCount = Line->Tokens.size();
1298 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1300 tryToParseJSFunction();
1324 if (Style.
isCpp() && FormatTok->
is(TT_StatementMacro)) {
1325 parseStatementMacro();
1332 if (Line->Tokens.size() == 1 &&
1336 if (FormatTok->
Tok.
is(tok::colon) && !Line->MustBeDeclaration) {
1337 Line->Tokens.begin()->Tok->MustBreakBefore =
true;
1343 bool FunctionLike = FormatTok->
is(tok::l_paren);
1347 bool FollowedByNewline =
1348 CommentsBeforeNextToken.empty()
1350 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1352 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
1364 if (FormatTok->
is(TT_JsFatArrow)) {
1366 if (FormatTok->
is(tok::l_brace))
1372 if (FormatTok->
Tok.
is(tok::l_brace)) {
1376 FormatTok->
Tok.
is(tok::less)) {
1378 parseBracedList(
false,
1395 bool UnwrappedLineParser::tryToParseLambda() {
1396 if (!Style.
isCpp()) {
1400 assert(FormatTok->
is(tok::l_square));
1402 if (!tryToParseLambdaIntroducer())
1405 while (FormatTok->
isNot(tok::l_brace)) {
1422 case tok::identifier:
1423 case tok::numeric_constant:
1424 case tok::coloncolon:
1425 case tok::kw_mutable:
1429 FormatTok->
Type = TT_LambdaArrow;
1436 LSquare.Type = TT_LambdaLSquare;
1441 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1444 (Previous->
isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1445 tok::kw_delete, tok::l_square) ||
1452 if (FormatTok->
is(tok::l_square)) {
1459 void UnwrappedLineParser::tryToParseJSFunction() {
1468 if (FormatTok->
is(tok::star)) {
1469 FormatTok->
Type = TT_OverloadedOperator;
1474 if (FormatTok->
is(tok::identifier))
1477 if (FormatTok->
isNot(tok::l_paren))
1483 if (FormatTok->
is(tok::colon)) {
1489 if (FormatTok->
is(tok::l_brace))
1490 tryToParseBracedList();
1492 while (!FormatTok->
isOneOf(tok::l_brace, tok::semi) && !eof())
1496 if (FormatTok->
is(tok::semi))
1502 bool UnwrappedLineParser::tryToParseBracedList() {
1504 calculateBraceTypes();
1513 bool UnwrappedLineParser::parseBracedList(
bool ContinueOnSemicolons,
1515 bool HasError =
false;
1523 tryToParseJSFunction();
1526 if (FormatTok->
is(TT_JsFatArrow)) {
1530 if (FormatTok->
is(tok::l_brace)) {
1535 if (FormatTok->
is(tok::l_brace)) {
1537 if (tryToParseBracedList())
1542 if (FormatTok->
Tok.
getKind() == ClosingBraceKind) {
1549 if (FormatTok->
is(tok::l_brace)) {
1561 if (FormatTok->
is(tok::l_brace))
1576 parseBracedList(
false,
1592 if (!ContinueOnSemicolons)
1607 void UnwrappedLineParser::parseParens() {
1608 assert(FormatTok->
Tok.
is(tok::l_paren) &&
"'(' expected.");
1627 if (!tryToParseBracedList())
1632 if (FormatTok->
Tok.
is(tok::l_brace)) {
1643 case tok::identifier:
1647 tryToParseJSFunction();
1659 if (!LambdaIntroducer) {
1660 assert(FormatTok->
Tok.
is(tok::l_square) &&
"'[' expected.");
1661 if (tryToParseLambda())
1678 case tok::l_brace: {
1679 if (!tryToParseBracedList())
1685 if (FormatTok->
Tok.
is(tok::l_brace)) {
1697 void UnwrappedLineParser::parseIfThenElse() {
1698 assert(FormatTok->
Tok.
is(tok::kw_if) &&
"'if' expected");
1700 if (FormatTok->
Tok.
is(tok::kw_constexpr))
1702 if (FormatTok->
Tok.
is(tok::l_paren))
1704 bool NeedsUnwrappedLine =
false;
1705 if (FormatTok->
Tok.
is(tok::l_brace)) {
1711 NeedsUnwrappedLine =
true;
1715 parseStructuralElement();
1718 if (FormatTok->
Tok.
is(tok::kw_else)) {
1720 if (FormatTok->
Tok.
is(tok::l_brace)) {
1724 }
else if (FormatTok->
Tok.
is(tok::kw_if)) {
1729 parseStructuralElement();
1734 }
else if (NeedsUnwrappedLine) {
1739 void UnwrappedLineParser::parseTryCatch() {
1740 assert(FormatTok->
isOneOf(tok::kw_try, tok::kw___try) &&
"'try' expected");
1742 bool NeedsUnwrappedLine =
false;
1743 if (FormatTok->
is(tok::colon)) {
1746 while (FormatTok->
is(tok::identifier)) {
1748 if (FormatTok->
is(tok::l_paren))
1750 if (FormatTok->
is(tok::comma))
1758 if (FormatTok->
is(tok::l_brace)) {
1764 NeedsUnwrappedLine =
true;
1766 }
else if (!FormatTok->
is(tok::kw_catch)) {
1772 parseStructuralElement();
1776 if (FormatTok->
is(tok::at))
1779 tok::kw___finally) ||
1787 while (FormatTok->
isNot(tok::l_brace)) {
1788 if (FormatTok->
is(tok::l_paren)) {
1796 NeedsUnwrappedLine =
false;
1802 NeedsUnwrappedLine =
true;
1804 if (NeedsUnwrappedLine)
1808 void UnwrappedLineParser::parseNamespace() {
1809 assert(FormatTok->
Tok.
is(tok::kw_namespace) &&
"'namespace' expected");
1813 while (FormatTok->
isOneOf(tok::identifier, tok::coloncolon))
1815 if (FormatTok->
Tok.
is(tok::l_brace)) {
1821 DeclarationScopeStack.size() > 1);
1822 parseBlock(
true, AddLevel);
1825 if (FormatTok->
Tok.
is(tok::semi))
1832 void UnwrappedLineParser::parseNew() {
1833 assert(FormatTok->
is(tok::kw_new) &&
"'new' expected");
1841 if (FormatTok->
isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1845 if (FormatTok->
is(tok::l_paren)) {
1849 if (FormatTok->
is(tok::l_brace))
1857 void UnwrappedLineParser::parseForOrWhileLoop() {
1858 assert(FormatTok->
isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
1859 "'for', 'while' or foreach macro expected");
1865 if (FormatTok->
Tok.
is(tok::l_paren))
1867 if (FormatTok->
Tok.
is(tok::l_brace)) {
1874 parseStructuralElement();
1879 void UnwrappedLineParser::parseDoWhile() {
1880 assert(FormatTok->
Tok.
is(tok::kw_do) &&
"'do' expected");
1882 if (FormatTok->
Tok.
is(tok::l_brace)) {
1890 parseStructuralElement();
1895 if (!FormatTok->
Tok.
is(tok::kw_while)) {
1901 parseStructuralElement();
1904 void UnwrappedLineParser::parseLabel() {
1906 unsigned OldLineLevel = Line->Level;
1907 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1909 if (CommentsBeforeNextToken.empty() && FormatTok->
Tok.
is(tok::l_brace)) {
1912 if (FormatTok->
Tok.
is(tok::kw_break)) {
1915 parseStructuralElement();
1919 if (FormatTok->
is(tok::semi))
1923 Line->Level = OldLineLevel;
1924 if (FormatTok->
isNot(tok::l_brace)) {
1925 parseStructuralElement();
1930 void UnwrappedLineParser::parseCaseLabel() {
1931 assert(FormatTok->
Tok.
is(tok::kw_case) &&
"'case' expected");
1935 }
while (!eof() && !FormatTok->
Tok.
is(tok::colon));
1939 void UnwrappedLineParser::parseSwitch() {
1940 assert(FormatTok->
Tok.
is(tok::kw_switch) &&
"'switch' expected");
1942 if (FormatTok->
Tok.
is(tok::l_paren))
1944 if (FormatTok->
Tok.
is(tok::l_brace)) {
1951 parseStructuralElement();
1956 void UnwrappedLineParser::parseAccessSpecifier() {
1962 if (FormatTok->
Tok.
is(tok::colon))
1967 bool UnwrappedLineParser::parseEnum() {
1969 if (FormatTok->
Tok.
is(tok::kw_enum))
1976 FormatTok->
isOneOf(tok::colon, tok::question))
1980 if (FormatTok->
Tok.
is(tok::kw_class) || FormatTok->
Tok.
is(tok::kw_struct))
1984 FormatTok->
isOneOf(tok::colon, tok::coloncolon, tok::less,
1985 tok::greater, tok::comma, tok::question)) {
1988 if (FormatTok->
is(tok::l_paren))
1990 if (FormatTok->
is(tok::identifier)) {
1994 if (Style.
isCpp() && FormatTok->
is(tok::identifier))
2000 if (FormatTok->
isNot(tok::l_brace))
2006 parseJavaEnumBody();
2016 bool HasError = !parseBracedList(
true);
2018 if (FormatTok->
is(tok::semi))
2029 void UnwrappedLineParser::parseJavaEnumBody() {
2034 bool IsSimple =
true;
2037 if (Tok->
is(tok::r_brace))
2039 if (Tok->
isOneOf(tok::l_brace, tok::semi)) {
2064 if (FormatTok->
is(tok::l_brace)) {
2066 parseBlock(
true,
true,
2068 }
else if (FormatTok->
is(tok::l_paren)) {
2070 }
else if (FormatTok->
is(tok::comma)) {
2073 }
else if (FormatTok->
is(tok::semi)) {
2077 }
else if (FormatTok->
is(tok::r_brace)) {
2092 void UnwrappedLineParser::parseRecord(
bool ParseAsExpr) {
2098 while (FormatTok->
isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2099 tok::kw___attribute, tok::kw___declspec,
2103 FormatTok->
isOneOf(tok::period, tok::comma))) {
2110 if (FormatTok->
is(tok::l_brace)) {
2111 tryToParseBracedList();
2115 bool IsNonMacroIdentifier =
2116 FormatTok->
is(tok::identifier) &&
2120 if (!IsNonMacroIdentifier && FormatTok->
Tok.
is(tok::l_paren))
2134 if (FormatTok->
isOneOf(tok::colon, tok::less)) {
2136 if (FormatTok->
is(tok::l_brace)) {
2137 calculateBraceTypes(
true);
2138 if (!tryToParseBracedList())
2141 if (FormatTok->
Tok.
is(tok::semi))
2146 if (FormatTok->
Tok.
is(tok::l_brace)) {
2153 parseBlock(
true,
true,
2162 void UnwrappedLineParser::parseObjCMethod() {
2163 assert(FormatTok->
Tok.
isOneOf(tok::l_paren, tok::identifier) &&
2164 "'(' or identifier expected.");
2166 if (FormatTok->
Tok.
is(tok::semi)) {
2170 }
else if (FormatTok->
Tok.
is(tok::l_brace)) {
2182 void UnwrappedLineParser::parseObjCProtocolList() {
2183 assert(FormatTok->
Tok.
is(tok::less) &&
"'<' expected.");
2187 if (FormatTok->
isOneOf(tok::semi, tok::l_brace) ||
2190 }
while (!eof() && FormatTok->
Tok.
isNot(tok::greater));
2194 void UnwrappedLineParser::parseObjCUntilAtEnd() {
2201 if (FormatTok->
is(tok::l_brace)) {
2205 }
else if (FormatTok->
is(tok::r_brace)) {
2209 }
else if (FormatTok->
isOneOf(tok::minus, tok::plus)) {
2213 parseStructuralElement();
2218 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
2226 if (FormatTok->
Tok.
is(tok::less)) {
2234 unsigned NumOpenAngles = 1;
2238 if (FormatTok->
isOneOf(tok::semi, tok::l_brace) ||
2241 if (FormatTok->
Tok.
is(tok::less))
2243 else if (FormatTok->
Tok.
is(tok::greater)) {
2244 assert(NumOpenAngles > 0 &&
"'>' makes NumOpenAngles negative");
2247 }
while (!eof() && NumOpenAngles != 0);
2250 if (FormatTok->
Tok.
is(tok::colon)) {
2253 }
else if (FormatTok->
Tok.
is(tok::l_paren))
2257 if (FormatTok->
Tok.
is(tok::less))
2258 parseObjCProtocolList();
2260 if (FormatTok->
Tok.
is(tok::l_brace)) {
2270 parseObjCUntilAtEnd();
2275 bool UnwrappedLineParser::parseObjCProtocol() {
2279 if (FormatTok->
is(tok::l_paren))
2290 if (FormatTok->
Tok.
is(tok::less))
2291 parseObjCProtocolList();
2294 if (FormatTok->
Tok.
is(tok::semi)) {
2301 parseObjCUntilAtEnd();
2305 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
2306 bool IsImport = FormatTok->
is(Keywords.
kw_import);
2307 assert(IsImport || FormatTok->
is(tok::kw_export));
2311 if (FormatTok->
is(tok::kw_default))
2328 if (!IsImport && !FormatTok->
isOneOf(tok::l_brace, tok::star) &&
2333 if (FormatTok->
is(tok::semi))
2335 if (Line->Tokens.empty()) {
2340 if (FormatTok->
is(tok::l_brace)) {
2350 void UnwrappedLineParser::parseStatementMacro()
2353 if (FormatTok->
is(tok::l_paren))
2355 if (FormatTok->
is(tok::semi))
2361 StringRef Prefix =
"") {
2362 llvm::dbgs() << Prefix <<
"Line(" << Line.
Level 2365 for (std::list<UnwrappedLineNode>::const_iterator I = Line.
Tokens.begin(),
2368 llvm::dbgs() << I->Tok->Tok.getName() <<
"[" 2369 <<
"T=" << I->Tok->Type <<
", OC=" << I->Tok->OriginalColumn
2372 for (std::list<UnwrappedLineNode>::const_iterator I = Line.
Tokens.begin(),
2383 llvm::dbgs() <<
"\n";
2386 void UnwrappedLineParser::addUnwrappedLine() {
2387 if (Line->Tokens.empty())
2390 if (CurrentLines == &Lines)
2393 CurrentLines->push_back(std::move(*Line));
2394 Line->Tokens.clear();
2396 Line->FirstStartColumn = 0;
2397 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
2398 CurrentLines->append(
2399 std::make_move_iterator(PreprocessorDirectives.begin()),
2400 std::make_move_iterator(PreprocessorDirectives.end()));
2401 PreprocessorDirectives.clear();
2407 bool UnwrappedLineParser::eof()
const {
return FormatTok->
Tok.
is(
tok::eof); }
2409 bool UnwrappedLineParser::isOnNewLine(
const FormatToken &FormatTok) {
2418 llvm::Regex &CommentPragmasRegex) {
2422 StringRef IndentContent = FormatTok.
TokenText;
2423 if (FormatTok.
TokenText.startswith(
"//") ||
2425 IndentContent = FormatTok.
TokenText.substr(2);
2426 if (CommentPragmasRegex.match(IndentContent))
2501 if (PreviousToken && PreviousToken->
is(tok::l_brace) &&
2502 isLineComment(*Node.
Tok)) {
2503 MinColumnToken = PreviousToken;
2506 PreviousToken = Node.
Tok;
2510 MinColumnToken = Node.
Tok;
2513 if (PreviousToken && PreviousToken->
is(tok::l_brace)) {
2514 MinColumnToken = PreviousToken;
2517 return continuesLineComment(FormatTok, Line.
Tokens.back().Tok,
2521 void UnwrappedLineParser::flushComments(
bool NewlineBeforeNext) {
2522 bool JustComments = Line->Tokens.empty();
2524 I = CommentsBeforeNextToken.begin(),
2525 E = CommentsBeforeNextToken.end();
2537 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
2541 if (NewlineBeforeNext && JustComments)
2543 CommentsBeforeNextToken.clear();
2546 void UnwrappedLineParser::nextToken(
int LevelDifference) {
2549 flushComments(isOnNewLine(*FormatTok));
2550 pushToken(FormatTok);
2553 readToken(LevelDifference);
2555 readTokenWithJavaScriptASI();
2559 void UnwrappedLineParser::distributeComments(
2580 if (Comments.empty())
2582 bool ShouldPushCommentsInCurrentLine =
true;
2583 bool HasTrailAlignedWithNextToken =
false;
2584 unsigned StartOfTrailAlignedWithNextToken = 0;
2587 for (
unsigned i = Comments.size() - 1; i > 0; --i) {
2589 HasTrailAlignedWithNextToken =
true;
2590 StartOfTrailAlignedWithNextToken = i;
2594 for (
unsigned i = 0, e = Comments.size(); i < e; ++i) {
2596 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
2603 (isOnNewLine(*FormatTok) || FormatTok->
IsFirst)) {
2604 ShouldPushCommentsInCurrentLine =
false;
2606 if (ShouldPushCommentsInCurrentLine) {
2607 pushToken(FormatTok);
2609 CommentsBeforeNextToken.push_back(FormatTok);
2614 void UnwrappedLineParser::readToken(
int LevelDifference) {
2619 while (!Line->InPPDirective && FormatTok->
Tok.
is(tok::hash) &&
2621 distributeComments(Comments, FormatTok);
2625 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
2627 assert((LevelDifference >= 0 ||
2628 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2629 "LevelDifference makes Line->Level negative");
2630 Line->Level += LevelDifference;
2634 flushComments(isOnNewLine(*FormatTok));
2637 while (FormatTok->
Type == TT_ConflictStart ||
2638 FormatTok->
Type == TT_ConflictEnd ||
2639 FormatTok->
Type == TT_ConflictAlternative) {
2640 if (FormatTok->
Type == TT_ConflictStart) {
2641 conditionalCompilationStart(
false);
2642 }
else if (FormatTok->
Type == TT_ConflictAlternative) {
2643 conditionalCompilationAlternative();
2644 }
else if (FormatTok->
Type == TT_ConflictEnd) {
2645 conditionalCompilationEnd();
2651 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
2652 !Line->InPPDirective) {
2656 if (!FormatTok->
Tok.
is(tok::comment)) {
2657 distributeComments(Comments, FormatTok);
2662 Comments.push_back(FormatTok);
2665 distributeComments(Comments,
nullptr);
2671 if (MustBreakBeforeNextToken) {
2672 Line->Tokens.back().Tok->MustBreakBefore =
true;
2673 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.
bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const
Return true if we have an ObjC keyword identifier.
const AnnotatedLine * Line
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.
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const
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