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: {
355 if (Next && Next->
isNot(tok::colon)) {
358 parseStructuralElement();
366 Line->MustBeDeclaration) {
368 parseStructuralElement();
371 if (!SwitchLabelEncountered &&
374 SwitchLabelEncountered =
true;
375 parseStructuralElement();
378 parseStructuralElement();
384 void UnwrappedLineParser::calculateBraceTypes(
bool ExpectClassBody) {
396 assert(Tok->
Tok.
is(tok::l_brace));
400 unsigned ReadTokens = 0;
404 }
while (NextTok->
is(tok::comment));
409 if (PrevTok->
isOneOf(tok::colon, tok::less))
420 else if (PrevTok->
is(tok::r_paren))
426 LBraceStack.push_back(Tok);
429 if (LBraceStack.empty())
431 if (LBraceStack.back()->BlockKind ==
BK_Unknown) {
432 bool ProbablyBracedList =
false;
434 ProbablyBracedList = NextTok->
isOneOf(tok::comma, tok::r_square);
438 bool NextIsObjCMethod = NextTok->
isOneOf(tok::plus, tok::minus) &&
453 (Style.
isCpp() && NextTok->
is(tok::l_paren)) ||
454 NextTok->
isOneOf(tok::comma, tok::period, tok::colon,
455 tok::r_paren, tok::r_square, tok::l_brace,
457 (NextTok->
is(tok::identifier) &&
458 !PrevTok->
isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
459 (NextTok->
is(tok::semi) &&
460 (!ExpectClassBody || LBraceStack.size() != 1)) ||
462 if (NextTok->
is(tok::l_square)) {
467 ProbablyBracedList = NextTok->
isNot(tok::l_square);
470 if (ProbablyBracedList) {
475 LBraceStack.back()->BlockKind =
BK_Block;
478 LBraceStack.pop_back();
488 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind ==
BK_Unknown)
489 LBraceStack.back()->BlockKind =
BK_Block;
499 for (
unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
501 LBraceStack[i]->BlockKind =
BK_Block;
510 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
513 size_t UnwrappedLineParser::computePPHash()
const {
515 for (
const auto &i : PPStack) {
522 void UnwrappedLineParser::parseBlock(
bool MustBeDeclaration,
bool AddLevel,
524 assert(FormatTok->
isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
525 "'{' or macro block token expected");
526 const bool MacroBlock = FormatTok->
is(TT_MacroBlockBegin);
529 size_t PPStartHash = computePPHash();
531 unsigned InitialLevel = Line->Level;
532 nextToken(AddLevel ? 1 : 0);
534 if (MacroBlock && FormatTok->
is(tok::l_paren))
537 size_t NbPreprocessorDirectives =
538 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
540 size_t OpeningLineIndex =
541 CurrentLines->empty()
543 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
545 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
554 if (MacroBlock ? !FormatTok->
is(TT_MacroBlockEnd)
555 : !FormatTok->
is(tok::r_brace)) {
556 Line->Level = InitialLevel;
561 size_t PPEndHash = computePPHash();
564 nextToken(AddLevel ? -1 : 0);
566 if (MacroBlock && FormatTok->
is(tok::l_paren))
569 if (MunchSemi && FormatTok->
Tok.
is(tok::semi))
571 Line->Level = InitialLevel;
573 if (PPStartHash == PPEndHash) {
574 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
577 (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
578 CurrentLines->size() - 1;
586 if (Line.
Tokens.size() < 4)
588 auto I = Line.
Tokens.begin();
589 if (I->Tok->TokenText !=
"goog")
592 if (I->Tok->isNot(tok::period))
595 if (I->Tok->TokenText !=
"scope")
598 return I->Tok->is(tok::l_paren);
607 if (Line.
Tokens.size() < 3)
609 auto I = Line.
Tokens.begin();
610 if (I->Tok->isNot(tok::l_paren))
616 return I->Tok->is(tok::l_paren);
621 if (InitialToken.
is(tok::kw_namespace))
623 if (InitialToken.
is(tok::kw_class))
625 if (InitialToken.
is(tok::kw_union))
627 if (InitialToken.
is(tok::kw_struct))
632 void UnwrappedLineParser::parseChildBlock() {
639 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
641 Line->Level += SkipIndent ? 0 : 1;
643 flushComments(isOnNewLine(*FormatTok));
644 Line->Level -= SkipIndent ? 0 : 1;
649 void UnwrappedLineParser::parsePPDirective() {
650 assert(FormatTok->
Tok.
is(tok::hash) &&
"'#' expected");
651 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
685 void UnwrappedLineParser::conditionalCompilationCondition(
bool Unreachable) {
686 size_t Line = CurrentLines->size();
687 if (CurrentLines == &PreprocessorDirectives)
688 Line += Lines.size();
691 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
692 PPStack.push_back({PP_Unreachable, Line});
694 PPStack.push_back({PP_Conditional, Line});
697 void UnwrappedLineParser::conditionalCompilationStart(
bool Unreachable) {
699 assert(PPBranchLevel >= 0 && PPBranchLevel <= (
int)PPLevelBranchIndex.size());
700 if (PPBranchLevel == (
int)PPLevelBranchIndex.size()) {
701 PPLevelBranchIndex.push_back(0);
702 PPLevelBranchCount.push_back(0);
704 PPChainBranchIndex.push(0);
705 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
706 conditionalCompilationCondition(Unreachable || Skip);
709 void UnwrappedLineParser::conditionalCompilationAlternative() {
710 if (!PPStack.empty())
712 assert(PPBranchLevel < (
int)PPLevelBranchIndex.size());
713 if (!PPChainBranchIndex.empty())
714 ++PPChainBranchIndex.top();
715 conditionalCompilationCondition(
716 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
717 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
720 void UnwrappedLineParser::conditionalCompilationEnd() {
721 assert(PPBranchLevel < (
int)PPLevelBranchIndex.size());
722 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
723 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
724 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
728 if (PPBranchLevel > -1)
730 if (!PPChainBranchIndex.empty())
731 PPChainBranchIndex.pop();
732 if (!PPStack.empty())
736 void UnwrappedLineParser::parsePPIf(
bool IfDef) {
737 bool IfNDef = FormatTok->
is(tok::pp_ifndef);
739 bool Unreachable =
false;
740 if (!IfDef && (FormatTok->
is(tok::kw_false) || FormatTok->
TokenText ==
"0"))
742 if (IfDef && !IfNDef && FormatTok->
TokenText ==
"SWIG")
744 conditionalCompilationStart(Unreachable);
745 FormatToken *IfCondition = FormatTok;
748 bool MaybeIncludeGuard = IfNDef;
749 if (IncludeGuard == IG_Inited && MaybeIncludeGuard)
750 for (
auto &Line : Lines) {
751 if (!Line.Tokens.front().Tok->is(tok::comment)) {
752 MaybeIncludeGuard =
false;
753 IncludeGuard = IG_Rejected;
760 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
761 IncludeGuard = IG_IfNdefed;
762 IncludeGuardToken = IfCondition;
766 void UnwrappedLineParser::parsePPElse() {
768 if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
769 IncludeGuard = IG_Rejected;
770 conditionalCompilationAlternative();
771 if (PPBranchLevel > -1)
777 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
779 void UnwrappedLineParser::parsePPEndIf() {
780 conditionalCompilationEnd();
785 FormatToken *PeekNext = AllTokens[TokenPosition];
786 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
789 IncludeGuard = IG_Found;
792 void UnwrappedLineParser::parsePPDefine() {
795 if (FormatTok->
Tok.
getKind() != tok::identifier) {
796 IncludeGuard = IG_Rejected;
797 IncludeGuardToken =
nullptr;
802 if (IncludeGuard == IG_IfNdefed &&
804 IncludeGuard = IG_Defined;
805 IncludeGuardToken =
nullptr;
806 for (
auto &Line : Lines) {
807 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
808 IncludeGuard = IG_Rejected;
815 if (FormatTok->
Tok.
getKind() == tok::l_paren &&
821 Line->Level += PPBranchLevel + 1;
833 void UnwrappedLineParser::parsePPUnknown() {
838 Line->Level += PPBranchLevel + 1;
848 return Tok.
isNot(tok::semi) && Tok.
isNot(tok::l_brace) &&
849 Tok.
isNot(tok::l_square) &&
852 Tok.
isNot(tok::period) && Tok.
isNot(tok::periodstar) &&
853 Tok.
isNot(tok::arrow) && Tok.
isNot(tok::arrowstar) &&
854 Tok.
isNot(tok::less) && Tok.
isNot(tok::greater) &&
855 Tok.
isNot(tok::slash) && Tok.
isNot(tok::percent) &&
856 Tok.
isNot(tok::lessless) && Tok.
isNot(tok::greatergreater) &&
857 Tok.
isNot(tok::equal) && Tok.
isNot(tok::plusequal) &&
858 Tok.
isNot(tok::minusequal) && Tok.
isNot(tok::starequal) &&
859 Tok.
isNot(tok::slashequal) && Tok.
isNot(tok::percentequal) &&
860 Tok.
isNot(tok::ampequal) && Tok.
isNot(tok::pipeequal) &&
861 Tok.
isNot(tok::caretequal) && Tok.
isNot(tok::greatergreaterequal) &&
862 Tok.
isNot(tok::lesslessequal) &&
866 Tok.
isNot(tok::colon) &&
868 Tok.
isNot(tok::kw_noexcept);
874 return FormatTok->
is(tok::identifier) &&
889 FormatTok->
isOneOf(tok::kw_true, tok::kw_false) ||
900 tok::kw_if, tok::kw_else,
902 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
904 tok::kw_switch, tok::kw_case,
906 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.
kw_finally,
908 tok::kw_const, tok::kw_class, Keywords.
kw_var, Keywords.
kw_let,
921 void UnwrappedLineParser::readTokenWithJavaScriptASI() {
927 CommentsBeforeNextToken.empty()
929 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
934 bool PreviousStartsTemplateExpr =
935 Previous->
is(TT_TemplateString) && Previous->
TokenText.endswith(
"${");
936 if (PreviousMustBeValue || Previous->
is(tok::r_paren)) {
939 bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
941 return LineNode.Tok->is(tok::at);
942 }) != Line->Tokens.end();
946 if (Next->
is(tok::exclaim) && PreviousMustBeValue)
947 return addUnwrappedLine();
949 bool NextEndsTemplateExpr =
950 Next->
is(TT_TemplateString) && Next->
TokenText.startswith(
"}");
951 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
952 (PreviousMustBeValue ||
953 Previous->
isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
955 return addUnwrappedLine();
956 if ((PreviousMustBeValue || Previous->
is(tok::r_paren)) &&
958 return addUnwrappedLine();
961 void UnwrappedLineParser::parseStructuralElement() {
962 assert(!FormatTok->
is(tok::l_brace));
964 FormatTok->
is(tok::pp_include)) {
966 if (FormatTok->
is(tok::string_literal))
974 if (FormatTok->
is(tok::l_brace)) {
975 FormatTok->
Type = TT_InlineASMBrace;
978 if (FormatTok->
is(tok::r_brace)) {
979 FormatTok->
Type = TT_InlineASMBrace;
989 case tok::kw_namespace:
994 if (FormatTok->
Tok.
is(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();
1067 case tok::identifier:
1068 if (FormatTok->
is(TT_ForEachMacro)) {
1069 parseForOrWhileLoop();
1072 if (FormatTok->
is(TT_MacroBlockBegin)) {
1073 parseBlock(
false,
true,
1079 parseJavaScriptEs6ImportExport();
1084 if (FormatTok->
is(tok::kw_public))
1086 if (!FormatTok->
is(tok::string_literal))
1089 if (FormatTok->
is(tok::semi))
1095 if (Style.
isCpp() &&
1099 if (FormatTok->
is(tok::colon)) {
1115 if (FormatTok->
Tok.
is(tok::l_brace)) {
1121 case tok::objc_public:
1122 case tok::objc_protected:
1123 case tok::objc_package:
1124 case tok::objc_private:
1125 return parseAccessSpecifier();
1126 case tok::objc_interface:
1127 case tok::objc_implementation:
1128 return parseObjCInterfaceOrImplementation();
1129 case tok::objc_protocol:
1130 if (parseObjCProtocol())
1135 case tok::objc_optional:
1136 case tok::objc_required:
1140 case tok::objc_autoreleasepool:
1142 if (FormatTok->
Tok.
is(tok::l_brace)) {
1149 case tok::objc_synchronized:
1151 if (FormatTok->
Tok.
is(tok::l_paren))
1154 if (FormatTok->
Tok.
is(tok::l_brace)) {
1172 if (Previous && Previous->
is(tok::less)) {
1182 if (!Style.
isCpp()) {
1187 case tok::kw_typedef:
1193 case tok::kw_struct:
1202 if (FormatTok->
is(tok::semi))
1212 FormatTok->
is(tok::kw_class))
1230 case tok::kw_operator:
1240 if (FormatTok->
is(tok::l_paren))
1242 if (FormatTok->
is(tok::l_brace))
1246 if (!tryToParseBracedList()) {
1253 FormatTok->
Type = TT_FunctionLBrace;
1265 case tok::identifier: {
1266 if (FormatTok->
is(TT_MacroBlockEnd)) {
1275 size_t TokenCount = Line->Tokens.size();
1278 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1280 tryToParseJSFunction();
1307 if (Line->Tokens.size() == 1 &&
1311 if (FormatTok->
Tok.
is(tok::colon) && !Line->MustBeDeclaration) {
1312 Line->Tokens.begin()->Tok->MustBreakBefore =
true;
1318 bool FunctionLike = FormatTok->
is(tok::l_paren);
1322 bool FollowedByNewline =
1323 CommentsBeforeNextToken.empty()
1325 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1327 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
1339 if (FormatTok->
is(TT_JsFatArrow)) {
1341 if (FormatTok->
is(tok::l_brace))
1347 if (FormatTok->
Tok.
is(tok::l_brace)) {
1351 FormatTok->
Tok.
is(tok::less)) {
1353 parseBracedList(
false,
1370 bool UnwrappedLineParser::tryToParseLambda() {
1371 if (!Style.
isCpp()) {
1375 assert(FormatTok->
is(tok::l_square));
1377 if (!tryToParseLambdaIntroducer())
1380 while (FormatTok->
isNot(tok::l_brace)) {
1397 case tok::identifier:
1398 case tok::numeric_constant:
1399 case tok::coloncolon:
1400 case tok::kw_mutable:
1404 FormatTok->
Type = TT_LambdaArrow;
1411 LSquare.Type = TT_LambdaLSquare;
1416 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1419 (Previous->
isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1420 tok::kw_delete, tok::l_square) ||
1427 if (FormatTok->
is(tok::l_square)) {
1434 void UnwrappedLineParser::tryToParseJSFunction() {
1443 if (FormatTok->
is(tok::star)) {
1444 FormatTok->
Type = TT_OverloadedOperator;
1449 if (FormatTok->
is(tok::identifier))
1452 if (FormatTok->
isNot(tok::l_paren))
1458 if (FormatTok->
is(tok::colon)) {
1464 if (FormatTok->
is(tok::l_brace))
1465 tryToParseBracedList();
1467 while (!FormatTok->
isOneOf(tok::l_brace, tok::semi) && !eof())
1471 if (FormatTok->
is(tok::semi))
1477 bool UnwrappedLineParser::tryToParseBracedList() {
1479 calculateBraceTypes();
1488 bool UnwrappedLineParser::parseBracedList(
bool ContinueOnSemicolons,
1490 bool HasError =
false;
1498 tryToParseJSFunction();
1501 if (FormatTok->
is(TT_JsFatArrow)) {
1505 if (FormatTok->
is(tok::l_brace)) {
1510 if (FormatTok->
is(tok::l_brace)) {
1512 if (tryToParseBracedList())
1517 if (FormatTok->
Tok.
getKind() == ClosingBraceKind) {
1524 if (FormatTok->
is(tok::l_brace)) {
1536 if (FormatTok->
is(tok::l_brace))
1551 parseBracedList(
false,
1567 if (!ContinueOnSemicolons)
1582 void UnwrappedLineParser::parseParens() {
1583 assert(FormatTok->
Tok.
is(tok::l_paren) &&
"'(' expected.");
1602 if (!tryToParseBracedList())
1607 if (FormatTok->
Tok.
is(tok::l_brace)) {
1618 case tok::identifier:
1622 tryToParseJSFunction();
1634 if (!LambdaIntroducer) {
1635 assert(FormatTok->
Tok.
is(tok::l_square) &&
"'[' expected.");
1636 if (tryToParseLambda())
1653 case tok::l_brace: {
1654 if (!tryToParseBracedList())
1660 if (FormatTok->
Tok.
is(tok::l_brace)) {
1672 void UnwrappedLineParser::parseIfThenElse() {
1673 assert(FormatTok->
Tok.
is(tok::kw_if) &&
"'if' expected");
1675 if (FormatTok->
Tok.
is(tok::kw_constexpr))
1677 if (FormatTok->
Tok.
is(tok::l_paren))
1679 bool NeedsUnwrappedLine =
false;
1680 if (FormatTok->
Tok.
is(tok::l_brace)) {
1686 NeedsUnwrappedLine =
true;
1690 parseStructuralElement();
1693 if (FormatTok->
Tok.
is(tok::kw_else)) {
1695 if (FormatTok->
Tok.
is(tok::l_brace)) {
1699 }
else if (FormatTok->
Tok.
is(tok::kw_if)) {
1704 parseStructuralElement();
1709 }
else if (NeedsUnwrappedLine) {
1714 void UnwrappedLineParser::parseTryCatch() {
1715 assert(FormatTok->
isOneOf(tok::kw_try, tok::kw___try) &&
"'try' expected");
1717 bool NeedsUnwrappedLine =
false;
1718 if (FormatTok->
is(tok::colon)) {
1721 while (FormatTok->
is(tok::identifier)) {
1723 if (FormatTok->
is(tok::l_paren))
1725 if (FormatTok->
is(tok::comma))
1733 if (FormatTok->
is(tok::l_brace)) {
1739 NeedsUnwrappedLine =
true;
1741 }
else if (!FormatTok->
is(tok::kw_catch)) {
1747 parseStructuralElement();
1751 if (FormatTok->
is(tok::at))
1754 tok::kw___finally) ||
1762 while (FormatTok->
isNot(tok::l_brace)) {
1763 if (FormatTok->
is(tok::l_paren)) {
1771 NeedsUnwrappedLine =
false;
1777 NeedsUnwrappedLine =
true;
1779 if (NeedsUnwrappedLine)
1783 void UnwrappedLineParser::parseNamespace() {
1784 assert(FormatTok->
Tok.
is(tok::kw_namespace) &&
"'namespace' expected");
1788 while (FormatTok->
isOneOf(tok::identifier, tok::coloncolon))
1790 if (FormatTok->
Tok.
is(tok::l_brace)) {
1796 DeclarationScopeStack.size() > 1);
1797 parseBlock(
true, AddLevel);
1800 if (FormatTok->
Tok.
is(tok::semi))
1807 void UnwrappedLineParser::parseNew() {
1808 assert(FormatTok->
is(tok::kw_new) &&
"'new' expected");
1816 if (FormatTok->
isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1820 if (FormatTok->
is(tok::l_paren)) {
1824 if (FormatTok->
is(tok::l_brace))
1832 void UnwrappedLineParser::parseForOrWhileLoop() {
1833 assert(FormatTok->
isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
1834 "'for', 'while' or foreach macro expected");
1840 if (FormatTok->
Tok.
is(tok::l_paren))
1842 if (FormatTok->
Tok.
is(tok::l_brace)) {
1849 parseStructuralElement();
1854 void UnwrappedLineParser::parseDoWhile() {
1855 assert(FormatTok->
Tok.
is(tok::kw_do) &&
"'do' expected");
1857 if (FormatTok->
Tok.
is(tok::l_brace)) {
1865 parseStructuralElement();
1870 if (!FormatTok->
Tok.
is(tok::kw_while)) {
1876 parseStructuralElement();
1879 void UnwrappedLineParser::parseLabel() {
1881 unsigned OldLineLevel = Line->Level;
1882 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1884 if (CommentsBeforeNextToken.empty() && FormatTok->
Tok.
is(tok::l_brace)) {
1887 if (FormatTok->
Tok.
is(tok::kw_break)) {
1890 parseStructuralElement();
1894 if (FormatTok->
is(tok::semi))
1898 Line->Level = OldLineLevel;
1899 if (FormatTok->
isNot(tok::l_brace)) {
1900 parseStructuralElement();
1905 void UnwrappedLineParser::parseCaseLabel() {
1906 assert(FormatTok->
Tok.
is(tok::kw_case) &&
"'case' expected");
1910 }
while (!eof() && !FormatTok->
Tok.
is(tok::colon));
1914 void UnwrappedLineParser::parseSwitch() {
1915 assert(FormatTok->
Tok.
is(tok::kw_switch) &&
"'switch' expected");
1917 if (FormatTok->
Tok.
is(tok::l_paren))
1919 if (FormatTok->
Tok.
is(tok::l_brace)) {
1926 parseStructuralElement();
1931 void UnwrappedLineParser::parseAccessSpecifier() {
1937 if (FormatTok->
Tok.
is(tok::colon))
1942 bool UnwrappedLineParser::parseEnum() {
1944 if (FormatTok->
Tok.
is(tok::kw_enum))
1951 FormatTok->
isOneOf(tok::colon, tok::question))
1955 if (FormatTok->
Tok.
is(tok::kw_class) || FormatTok->
Tok.
is(tok::kw_struct))
1959 FormatTok->
isOneOf(tok::colon, tok::coloncolon, tok::less,
1960 tok::greater, tok::comma, tok::question)) {
1963 if (FormatTok->
is(tok::l_paren))
1965 if (FormatTok->
is(tok::identifier)) {
1969 if (Style.
isCpp() && FormatTok->
is(tok::identifier))
1975 if (FormatTok->
isNot(tok::l_brace))
1981 parseJavaEnumBody();
1991 bool HasError = !parseBracedList(
true);
1993 if (FormatTok->
is(tok::semi))
2004 void UnwrappedLineParser::parseJavaEnumBody() {
2009 bool IsSimple =
true;
2012 if (Tok->
is(tok::r_brace))
2014 if (Tok->
isOneOf(tok::l_brace, tok::semi)) {
2039 if (FormatTok->
is(tok::l_brace)) {
2041 parseBlock(
true,
true,
2043 }
else if (FormatTok->
is(tok::l_paren)) {
2045 }
else if (FormatTok->
is(tok::comma)) {
2048 }
else if (FormatTok->
is(tok::semi)) {
2052 }
else if (FormatTok->
is(tok::r_brace)) {
2067 void UnwrappedLineParser::parseRecord(
bool ParseAsExpr) {
2073 while (FormatTok->
isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2074 tok::kw___attribute, tok::kw___declspec,
2078 FormatTok->
isOneOf(tok::period, tok::comma))) {
2085 if (FormatTok->
is(tok::l_brace)) {
2086 tryToParseBracedList();
2090 bool IsNonMacroIdentifier =
2091 FormatTok->
is(tok::identifier) &&
2095 if (!IsNonMacroIdentifier && FormatTok->
Tok.
is(tok::l_paren))
2109 if (FormatTok->
isOneOf(tok::colon, tok::less)) {
2111 if (FormatTok->
is(tok::l_brace)) {
2112 calculateBraceTypes(
true);
2113 if (!tryToParseBracedList())
2116 if (FormatTok->
Tok.
is(tok::semi))
2121 if (FormatTok->
Tok.
is(tok::l_brace)) {
2128 parseBlock(
true,
true,
2137 void UnwrappedLineParser::parseObjCMethod() {
2138 assert(FormatTok->
Tok.
isOneOf(tok::l_paren, tok::identifier) &&
2139 "'(' or identifier expected.");
2141 if (FormatTok->
Tok.
is(tok::semi)) {
2145 }
else if (FormatTok->
Tok.
is(tok::l_brace)) {
2155 void UnwrappedLineParser::parseObjCProtocolList() {
2156 assert(FormatTok->
Tok.
is(tok::less) &&
"'<' expected.");
2160 if (FormatTok->
isOneOf(tok::semi, tok::l_brace) ||
2163 }
while (!eof() && FormatTok->
Tok.
isNot(tok::greater));
2167 void UnwrappedLineParser::parseObjCUntilAtEnd() {
2174 if (FormatTok->
is(tok::l_brace)) {
2178 }
else if (FormatTok->
is(tok::r_brace)) {
2182 }
else if (FormatTok->
isOneOf(tok::minus, tok::plus)) {
2186 parseStructuralElement();
2191 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
2199 if (FormatTok->
Tok.
is(tok::less)) {
2207 unsigned NumOpenAngles = 1;
2211 if (FormatTok->
isOneOf(tok::semi, tok::l_brace) ||
2214 if (FormatTok->
Tok.
is(tok::less))
2216 else if (FormatTok->
Tok.
is(tok::greater)) {
2217 assert(NumOpenAngles > 0 &&
"'>' makes NumOpenAngles negative");
2220 }
while (!eof() && NumOpenAngles != 0);
2223 if (FormatTok->
Tok.
is(tok::colon)) {
2226 }
else if (FormatTok->
Tok.
is(tok::l_paren))
2230 if (FormatTok->
Tok.
is(tok::less))
2231 parseObjCProtocolList();
2233 if (FormatTok->
Tok.
is(tok::l_brace)) {
2243 parseObjCUntilAtEnd();
2248 bool UnwrappedLineParser::parseObjCProtocol() {
2252 if (FormatTok->
is(tok::l_paren))
2263 if (FormatTok->
Tok.
is(tok::less))
2264 parseObjCProtocolList();
2267 if (FormatTok->
Tok.
is(tok::semi)) {
2274 parseObjCUntilAtEnd();
2278 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
2279 bool IsImport = FormatTok->
is(Keywords.
kw_import);
2280 assert(IsImport || FormatTok->
is(tok::kw_export));
2284 if (FormatTok->
is(tok::kw_default))
2301 if (!IsImport && !FormatTok->
isOneOf(tok::l_brace, tok::star) &&
2306 if (FormatTok->
is(tok::semi))
2308 if (Line->Tokens.empty()) {
2313 if (FormatTok->
is(tok::l_brace)) {
2324 StringRef Prefix =
"") {
2325 llvm::dbgs() << Prefix <<
"Line(" << Line.
Level 2328 for (std::list<UnwrappedLineNode>::const_iterator I = Line.
Tokens.begin(),
2331 llvm::dbgs() << I->Tok->Tok.getName() <<
"[" 2332 <<
"T=" << I->Tok->Type <<
", OC=" << I->Tok->OriginalColumn
2335 for (std::list<UnwrappedLineNode>::const_iterator I = Line.
Tokens.begin(),
2346 llvm::dbgs() <<
"\n";
2349 void UnwrappedLineParser::addUnwrappedLine() {
2350 if (Line->Tokens.empty())
2353 if (CurrentLines == &Lines)
2356 CurrentLines->push_back(std::move(*Line));
2357 Line->Tokens.clear();
2359 Line->FirstStartColumn = 0;
2360 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
2361 CurrentLines->append(
2362 std::make_move_iterator(PreprocessorDirectives.begin()),
2363 std::make_move_iterator(PreprocessorDirectives.end()));
2364 PreprocessorDirectives.clear();
2370 bool UnwrappedLineParser::eof()
const {
return FormatTok->
Tok.
is(
tok::eof); }
2372 bool UnwrappedLineParser::isOnNewLine(
const FormatToken &FormatTok) {
2381 llvm::Regex &CommentPragmasRegex) {
2385 StringRef IndentContent = FormatTok.
TokenText;
2386 if (FormatTok.
TokenText.startswith(
"//") ||
2388 IndentContent = FormatTok.
TokenText.substr(2);
2389 if (CommentPragmasRegex.match(IndentContent))
2464 if (PreviousToken && PreviousToken->
is(tok::l_brace) &&
2465 isLineComment(*Node.
Tok)) {
2466 MinColumnToken = PreviousToken;
2469 PreviousToken = Node.
Tok;
2473 MinColumnToken = Node.
Tok;
2476 if (PreviousToken && PreviousToken->
is(tok::l_brace)) {
2477 MinColumnToken = PreviousToken;
2480 return continuesLineComment(FormatTok, Line.
Tokens.back().Tok,
2484 void UnwrappedLineParser::flushComments(
bool NewlineBeforeNext) {
2485 bool JustComments = Line->Tokens.empty();
2487 I = CommentsBeforeNextToken.begin(),
2488 E = CommentsBeforeNextToken.end();
2500 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
2504 if (NewlineBeforeNext && JustComments)
2506 CommentsBeforeNextToken.clear();
2509 void UnwrappedLineParser::nextToken(
int LevelDifference) {
2512 flushComments(isOnNewLine(*FormatTok));
2513 pushToken(FormatTok);
2516 readToken(LevelDifference);
2518 readTokenWithJavaScriptASI();
2522 void UnwrappedLineParser::distributeComments(
2543 if (Comments.empty())
2545 bool ShouldPushCommentsInCurrentLine =
true;
2546 bool HasTrailAlignedWithNextToken =
false;
2547 unsigned StartOfTrailAlignedWithNextToken = 0;
2550 for (
unsigned i = Comments.size() - 1; i > 0; --i) {
2552 HasTrailAlignedWithNextToken =
true;
2553 StartOfTrailAlignedWithNextToken = i;
2557 for (
unsigned i = 0, e = Comments.size(); i < e; ++i) {
2559 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
2566 (isOnNewLine(*FormatTok) || FormatTok->
IsFirst)) {
2567 ShouldPushCommentsInCurrentLine =
false;
2569 if (ShouldPushCommentsInCurrentLine) {
2570 pushToken(FormatTok);
2572 CommentsBeforeNextToken.push_back(FormatTok);
2577 void UnwrappedLineParser::readToken(
int LevelDifference) {
2582 while (!Line->InPPDirective && FormatTok->
Tok.
is(tok::hash) &&
2584 distributeComments(Comments, FormatTok);
2588 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
2590 assert((LevelDifference >= 0 ||
2591 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2592 "LevelDifference makes Line->Level negative");
2593 Line->Level += LevelDifference;
2597 flushComments(isOnNewLine(*FormatTok));
2600 while (FormatTok->
Type == TT_ConflictStart ||
2601 FormatTok->
Type == TT_ConflictEnd ||
2602 FormatTok->
Type == TT_ConflictAlternative) {
2603 if (FormatTok->
Type == TT_ConflictStart) {
2604 conditionalCompilationStart(
false);
2605 }
else if (FormatTok->
Type == TT_ConflictAlternative) {
2606 conditionalCompilationAlternative();
2607 }
else if (FormatTok->
Type == TT_ConflictEnd) {
2608 conditionalCompilationEnd();
2614 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
2615 !Line->InPPDirective) {
2619 if (!FormatTok->
Tok.
is(tok::comment)) {
2620 distributeComments(Comments, FormatTok);
2625 Comments.push_back(FormatTok);
2628 distributeComments(Comments,
nullptr);
2634 if (MustBreakBeforeNextToken) {
2635 Line->Tokens.back().Tok->MustBreakBefore =
true;
2636 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