16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/Support/Debug.h" 18 #include "llvm/Support/raw_ostream.h" 22 #define DEBUG_TYPE "format-parser" 38 class ScopedDeclarationState {
41 bool MustBeDeclaration)
42 :
Line(Line), Stack(Stack) {
44 Stack.push_back(MustBeDeclaration);
46 ~ScopedDeclarationState() {
56 std::vector<bool> &Stack;
59 static bool isLineComment(
const FormatToken &FormatTok) {
60 return FormatTok.
is(tok::comment) && !FormatTok.
TokenText.startswith(
"/*");
66 static bool continuesLineComment(
const FormatToken &FormatTok,
69 if (!Previous || !MinColumnToken)
71 unsigned MinContinueColumn =
72 MinColumnToken->
OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
73 return isLineComment(FormatTok) && FormatTok.
NewlinesBefore == 1 &&
74 isLineComment(*Previous) &&
82 :
Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
83 PreviousLineLevel(Line.
Level), PreviousTokenSource(TokenSource),
84 Token(
nullptr), PreviousToken(
nullptr) {
85 FakeEOF.Tok.startToken();
92 ~ScopedMacroState()
override {
93 TokenSource = PreviousTokenSource;
96 Line.
Level = PreviousLineLevel;
103 PreviousToken =
Token;
104 Token = PreviousTokenSource->getNextToken();
110 unsigned getPosition()
override {
return PreviousTokenSource->getPosition(); }
113 PreviousToken =
nullptr;
114 Token = PreviousTokenSource->setPosition(Position);
121 !continuesLineComment(*
Token, PreviousToken,
129 unsigned PreviousLineLevel;
141 bool SwitchToPreprocessorLines =
false)
142 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
143 if (SwitchToPreprocessorLines)
144 Parser.CurrentLines = &Parser.PreprocessorDirectives;
145 else if (!Parser.Line->Tokens.empty())
146 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
147 PreBlockLine = std::move(Parser.Line);
148 Parser.Line = llvm::make_unique<UnwrappedLine>();
149 Parser.Line->Level = PreBlockLine->Level;
150 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
154 if (!
Parser.Line->Tokens.empty()) {
155 Parser.addUnwrappedLine();
157 assert(
Parser.Line->Tokens.empty());
158 Parser.Line = std::move(PreBlockLine);
159 if (
Parser.CurrentLines == &
Parser.PreprocessorDirectives)
160 Parser.MustBreakBeforeNextToken =
true;
161 Parser.CurrentLines = OriginalLines;
167 std::unique_ptr<UnwrappedLine> PreBlockLine;
176 Style.BraceWrapping.AfterControlStatement,
177 Style.BraceWrapping.IndentBraces) {
180 bool WrapBrace,
bool IndentBrace)
181 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
183 Parser->addUnwrappedLine();
191 unsigned OldLineLevel;
199 : Tokens(Tokens), Position(-1) {}
203 return Tokens[Position];
207 assert(Position >= 0);
213 return Tokens[Position];
216 void reset() { Position = -1; }
227 unsigned FirstStartColumn,
231 CurrentLines(&Lines), Style(Style), Keywords(Keywords),
232 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
233 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
234 IncludeGuard(Style.IndentPPDirectives ==
FormatStyle::PPDIS_None
237 IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
239 void UnwrappedLineParser::reset() {
241 IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None
244 IncludeGuardToken =
nullptr;
246 CommentsBeforeNextToken.clear();
248 MustBreakBeforeNextToken =
false;
249 PreprocessorDirectives.clear();
250 CurrentLines = &Lines;
251 DeclarationScopeStack.clear();
253 Line->FirstStartColumn = FirstStartColumn;
257 IndexedTokenSource TokenSource(AllTokens);
258 Line->FirstStartColumn = FirstStartColumn;
260 LLVM_DEBUG(llvm::dbgs() <<
"----\n");
262 Tokens = &TokenSource;
270 if (IncludeGuard == IG_Found)
271 for (
auto &Line : Lines)
272 if (Line.InPPDirective && Line.Level > 0)
276 pushToken(FormatTok);
286 while (!PPLevelBranchIndex.empty() &&
287 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
288 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
289 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
291 if (!PPLevelBranchIndex.empty()) {
292 ++PPLevelBranchIndex.back();
293 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
294 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
296 }
while (!PPLevelBranchIndex.empty());
299 void UnwrappedLineParser::parseFile() {
302 bool MustBeDeclaration =
303 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
304 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
306 if (Style.Language == FormatStyle::LK_TextProto)
320 if (Style.Language == FormatStyle::LK_TextProto &&
321 !CommentsBeforeNextToken.empty())
327 void UnwrappedLineParser::parseLevel(
bool HasOpeningBrace) {
328 bool SwitchLabelEncountered =
false;
331 if (FormatTok->
Type == TT_MacroBlockBegin) {
333 }
else if (FormatTok->
Type == TT_MacroBlockEnd) {
345 if (!FormatTok->
is(TT_MacroBlockBegin) && tryToParseBracedList())
356 case tok::kw_default: {
361 }
while (Next && Next->
is(tok::comment));
363 if (Next && Next->
isNot(tok::colon)) {
366 parseStructuralElement();
373 if (Style.Language == FormatStyle::LK_JavaScript &&
374 Line->MustBeDeclaration) {
376 parseStructuralElement();
379 if (!SwitchLabelEncountered &&
380 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
382 SwitchLabelEncountered =
true;
383 parseStructuralElement();
386 parseStructuralElement();
392 void UnwrappedLineParser::calculateBraceTypes(
bool ExpectClassBody) {
404 assert(Tok->
Tok.
is(tok::l_brace));
408 unsigned ReadTokens = 0;
412 }
while (NextTok->
is(tok::comment));
416 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) {
417 if (PrevTok->
isOneOf(tok::colon, tok::less))
428 else if (PrevTok->
is(tok::r_paren))
434 LBraceStack.push_back(Tok);
437 if (LBraceStack.empty())
439 if (LBraceStack.back()->BlockKind ==
BK_Unknown) {
440 bool ProbablyBracedList =
false;
441 if (Style.Language == FormatStyle::LK_Proto) {
442 ProbablyBracedList = NextTok->
isOneOf(tok::comma, tok::r_square);
446 bool NextIsObjCMethod = NextTok->
isOneOf(tok::plus, tok::minus) &&
458 (Style.Language == FormatStyle::LK_JavaScript &&
461 (Style.isCpp() && NextTok->
is(tok::l_paren)) ||
462 NextTok->
isOneOf(tok::comma, tok::period, tok::colon,
463 tok::r_paren, tok::r_square, tok::l_brace,
465 (NextTok->
is(tok::identifier) &&
466 !PrevTok->
isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
467 (NextTok->
is(tok::semi) &&
468 (!ExpectClassBody || LBraceStack.size() != 1)) ||
470 if (NextTok->
is(tok::l_square)) {
475 ProbablyBracedList = NextTok->
isNot(tok::l_square);
478 if (ProbablyBracedList) {
483 LBraceStack.back()->BlockKind =
BK_Block;
486 LBraceStack.pop_back();
488 case tok::identifier:
489 if (!Tok->
is(TT_StatementMacro))
500 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind ==
BK_Unknown)
501 LBraceStack.back()->BlockKind =
BK_Block;
511 for (
unsigned i = 0, e = LBraceStack.size();
i != e; ++
i) {
522 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
525 size_t UnwrappedLineParser::computePPHash()
const {
527 for (
const auto &
i : PPStack) {
534 void UnwrappedLineParser::parseBlock(
bool MustBeDeclaration,
bool AddLevel,
536 assert(FormatTok->
isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
537 "'{' or macro block token expected");
538 const bool MacroBlock = FormatTok->
is(TT_MacroBlockBegin);
541 size_t PPStartHash = computePPHash();
543 unsigned InitialLevel = Line->Level;
544 nextToken(AddLevel ? 1 : 0);
546 if (MacroBlock && FormatTok->
is(tok::l_paren))
549 size_t NbPreprocessorDirectives =
550 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
552 size_t OpeningLineIndex =
553 CurrentLines->empty()
555 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
557 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
566 if (MacroBlock ? !FormatTok->
is(TT_MacroBlockEnd)
567 : !FormatTok->
is(tok::r_brace)) {
568 Line->Level = InitialLevel;
573 size_t PPEndHash = computePPHash();
576 nextToken(AddLevel ? -1 : 0);
578 if (MacroBlock && FormatTok->
is(tok::l_paren))
581 if (MunchSemi && FormatTok->
Tok.
is(tok::semi))
583 Line->Level = InitialLevel;
585 if (PPStartHash == PPEndHash) {
586 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
589 (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
590 CurrentLines->size() - 1;
598 if (Line.
Tokens.size() < 4)
600 auto I = Line.
Tokens.begin();
601 if (I->Tok->TokenText !=
"goog")
604 if (I->Tok->isNot(tok::period))
607 if (I->Tok->TokenText !=
"scope")
610 return I->Tok->is(tok::l_paren);
619 if (Line.
Tokens.size() < 3)
621 auto I = Line.
Tokens.begin();
622 if (I->Tok->isNot(tok::l_paren))
628 return I->Tok->is(tok::l_paren);
633 if (InitialToken.
isOneOf(tok::kw_namespace, TT_NamespaceMacro))
634 return Style.BraceWrapping.AfterNamespace;
635 if (InitialToken.
is(tok::kw_class))
636 return Style.BraceWrapping.AfterClass;
637 if (InitialToken.
is(tok::kw_union))
638 return Style.BraceWrapping.AfterUnion;
639 if (InitialToken.
is(tok::kw_struct))
640 return Style.BraceWrapping.AfterStruct;
644 void UnwrappedLineParser::parseChildBlock() {
648 bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript &&
651 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
653 Line->Level += SkipIndent ? 0 : 1;
655 flushComments(isOnNewLine(*FormatTok));
656 Line->Level -= SkipIndent ? 0 : 1;
661 void UnwrappedLineParser::parsePPDirective() {
662 assert(FormatTok->
Tok.
is(tok::hash) &&
"'#' expected");
663 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
698 void UnwrappedLineParser::conditionalCompilationCondition(
bool Unreachable) {
699 size_t Line = CurrentLines->size();
700 if (CurrentLines == &PreprocessorDirectives)
701 Line += Lines.size();
704 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
705 PPStack.push_back({PP_Unreachable, Line});
707 PPStack.push_back({PP_Conditional, Line});
710 void UnwrappedLineParser::conditionalCompilationStart(
bool Unreachable) {
712 assert(PPBranchLevel >= 0 && PPBranchLevel <= (
int)PPLevelBranchIndex.size());
713 if (PPBranchLevel == (
int)PPLevelBranchIndex.size()) {
714 PPLevelBranchIndex.push_back(0);
715 PPLevelBranchCount.push_back(0);
717 PPChainBranchIndex.push(0);
718 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
719 conditionalCompilationCondition(Unreachable || Skip);
722 void UnwrappedLineParser::conditionalCompilationAlternative() {
723 if (!PPStack.empty())
725 assert(PPBranchLevel < (
int)PPLevelBranchIndex.size());
726 if (!PPChainBranchIndex.empty())
727 ++PPChainBranchIndex.top();
728 conditionalCompilationCondition(
729 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
730 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
733 void UnwrappedLineParser::conditionalCompilationEnd() {
734 assert(PPBranchLevel < (
int)PPLevelBranchIndex.size());
735 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
736 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
737 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
741 if (PPBranchLevel > -1)
743 if (!PPChainBranchIndex.empty())
744 PPChainBranchIndex.pop();
745 if (!PPStack.empty())
749 void UnwrappedLineParser::parsePPIf(
bool IfDef) {
752 bool Unreachable =
false;
753 if (!IfDef && (FormatTok->
is(tok::kw_false) || FormatTok->
TokenText ==
"0"))
755 if (IfDef && !IfNDef && FormatTok->
TokenText ==
"SWIG")
757 conditionalCompilationStart(Unreachable);
758 FormatToken *IfCondition = FormatTok;
761 bool MaybeIncludeGuard = IfNDef;
762 if (IncludeGuard == IG_Inited && MaybeIncludeGuard)
763 for (
auto &Line : Lines) {
764 if (!Line.Tokens.front().Tok->is(tok::comment)) {
765 MaybeIncludeGuard =
false;
766 IncludeGuard = IG_Rejected;
773 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
774 IncludeGuard = IG_IfNdefed;
775 IncludeGuardToken = IfCondition;
779 void UnwrappedLineParser::parsePPElse() {
781 if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
782 IncludeGuard = IG_Rejected;
783 conditionalCompilationAlternative();
784 if (PPBranchLevel > -1)
790 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
792 void UnwrappedLineParser::parsePPEndIf() {
793 conditionalCompilationEnd();
798 FormatToken *PeekNext = AllTokens[TokenPosition];
799 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
801 Style.IndentPPDirectives != FormatStyle::PPDIS_None)
802 IncludeGuard = IG_Found;
805 void UnwrappedLineParser::parsePPDefine() {
809 IncludeGuard = IG_Rejected;
810 IncludeGuardToken =
nullptr;
815 if (IncludeGuard == IG_IfNdefed &&
817 IncludeGuard = IG_Defined;
818 IncludeGuardToken =
nullptr;
819 for (
auto &Line : Lines) {
820 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
821 IncludeGuard = IG_Rejected;
828 if (FormatTok->
Tok.
getKind() == tok::l_paren &&
833 if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
834 Line->Level += PPBranchLevel + 1;
846 void UnwrappedLineParser::parsePPUnknown() {
850 if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
851 Line->Level += PPBranchLevel + 1;
861 return Tok.
isNot(tok::semi) && Tok.
isNot(tok::l_brace) &&
862 Tok.
isNot(tok::l_square) &&
865 Tok.
isNot(tok::period) && Tok.
isNot(tok::periodstar) &&
866 Tok.
isNot(tok::arrow) && Tok.
isNot(tok::arrowstar) &&
867 Tok.
isNot(tok::less) && Tok.
isNot(tok::greater) &&
868 Tok.
isNot(tok::slash) && Tok.
isNot(tok::percent) &&
869 Tok.
isNot(tok::lessless) && Tok.
isNot(tok::greatergreater) &&
870 Tok.
isNot(tok::equal) && Tok.
isNot(tok::plusequal) &&
871 Tok.
isNot(tok::minusequal) && Tok.
isNot(tok::starequal) &&
872 Tok.
isNot(tok::slashequal) && Tok.
isNot(tok::percentequal) &&
873 Tok.
isNot(tok::ampequal) && Tok.
isNot(tok::pipeequal) &&
874 Tok.
isNot(tok::caretequal) && Tok.
isNot(tok::greatergreaterequal) &&
875 Tok.
isNot(tok::lesslessequal) &&
879 Tok.
isNot(tok::colon) &&
881 Tok.
isNot(tok::kw_noexcept);
887 return FormatTok->
is(tok::identifier) &&
902 FormatTok->
isOneOf(tok::kw_true, tok::kw_false) ||
913 tok::kw_if, tok::kw_else,
915 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
917 tok::kw_switch, tok::kw_case,
919 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.
kw_finally,
921 tok::kw_const, tok::kw_class, Keywords.
kw_var, Keywords.
kw_let,
934 void UnwrappedLineParser::readTokenWithJavaScriptASI() {
940 CommentsBeforeNextToken.empty()
942 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
947 bool PreviousStartsTemplateExpr =
948 Previous->
is(TT_TemplateString) && Previous->
TokenText.endswith(
"${");
949 if (PreviousMustBeValue || Previous->
is(tok::r_paren)) {
952 bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
954 return LineNode.Tok->is(tok::at);
955 }) != Line->Tokens.end();
959 if (Next->
is(tok::exclaim) && PreviousMustBeValue)
960 return addUnwrappedLine();
962 bool NextEndsTemplateExpr =
963 Next->
is(TT_TemplateString) && Next->
TokenText.startswith(
"}");
964 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
965 (PreviousMustBeValue ||
966 Previous->
isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
968 return addUnwrappedLine();
969 if ((PreviousMustBeValue || Previous->
is(tok::r_paren)) &&
971 return addUnwrappedLine();
974 void UnwrappedLineParser::parseStructuralElement() {
975 assert(!FormatTok->
is(tok::l_brace));
976 if (Style.Language == FormatStyle::LK_TableGen &&
979 if (FormatTok->
is(tok::string_literal))
987 if (FormatTok->
is(tok::l_brace)) {
988 FormatTok->
Type = TT_InlineASMBrace;
991 if (FormatTok->
is(tok::r_brace)) {
992 FormatTok->
Type = TT_InlineASMBrace;
1002 case tok::kw_namespace:
1005 case tok::kw_public:
1006 case tok::kw_protected:
1007 case tok::kw_private:
1008 if (Style.Language == FormatStyle::LK_Java ||
1009 Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp())
1012 parseAccessSpecifier();
1019 parseForOrWhileLoop();
1024 case tok::kw_switch:
1025 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1030 case tok::kw_default:
1031 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1035 if (FormatTok->
is(tok::colon)) {
1042 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1051 case tok::kw_extern:
1053 if (FormatTok->
Tok.
is(tok::string_literal)) {
1055 if (FormatTok->
Tok.
is(tok::l_brace)) {
1056 if (Style.BraceWrapping.AfterExternBlock) {
1060 parseBlock(
true,
false);
1067 case tok::kw_export:
1068 if (Style.Language == FormatStyle::LK_JavaScript) {
1069 parseJavaScriptEs6ImportExport();
1076 case tok::kw_inline:
1078 if (FormatTok->
Tok.
is(tok::kw_namespace)) {
1083 case tok::identifier:
1084 if (FormatTok->
is(TT_ForEachMacro)) {
1085 parseForOrWhileLoop();
1088 if (FormatTok->
is(TT_MacroBlockBegin)) {
1089 parseBlock(
false,
true,
1094 if (Style.Language == FormatStyle::LK_JavaScript) {
1095 parseJavaScriptEs6ImportExport();
1098 if (Style.Language == FormatStyle::LK_Proto) {
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)) {
1121 if (Style.isCpp() && FormatTok->
is(TT_StatementMacro)) {
1122 parseStatementMacro();
1125 if (Style.isCpp() && FormatTok->
is(TT_NamespaceMacro)) {
1139 if (FormatTok->
Tok.
is(tok::l_brace)) {
1143 }
else if (Style.Language == FormatStyle::LK_Java &&
1149 case tok::objc_public:
1150 case tok::objc_protected:
1151 case tok::objc_package:
1152 case tok::objc_private:
1153 return parseAccessSpecifier();
1154 case tok::objc_interface:
1155 case tok::objc_implementation:
1156 return parseObjCInterfaceOrImplementation();
1157 case tok::objc_protocol:
1158 if (parseObjCProtocol())
1163 case tok::objc_optional:
1164 case tok::objc_required:
1168 case tok::objc_autoreleasepool:
1170 if (FormatTok->
Tok.
is(tok::l_brace)) {
1171 if (Style.BraceWrapping.AfterControlStatement)
1177 case tok::objc_synchronized:
1179 if (FormatTok->
Tok.
is(tok::l_paren))
1182 if (FormatTok->
Tok.
is(tok::l_brace)) {
1183 if (Style.BraceWrapping.AfterControlStatement)
1200 if (Previous && Previous->
is(tok::less)) {
1210 if (!Style.isCpp()) {
1215 case tok::kw_typedef:
1221 case tok::kw_struct:
1228 if (Style.Language == FormatStyle::LK_Java ||
1229 Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) {
1230 if (FormatTok->
is(tok::semi))
1239 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
1240 FormatTok->
is(tok::kw_class))
1242 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
1258 case tok::kw_operator:
1268 if (FormatTok->
is(tok::l_paren))
1270 if (FormatTok->
is(tok::l_brace))
1274 if (!tryToParseBracedList()) {
1279 if (Style.BraceWrapping.AfterFunction)
1281 FormatTok->
Type = TT_FunctionLBrace;
1291 if (Style.BraceWrapping.AfterFunction)
1295 case tok::identifier: {
1296 if (FormatTok->
is(TT_MacroBlockEnd)) {
1305 size_t TokenCount = Line->Tokens.size();
1306 if (Style.Language == FormatStyle::LK_JavaScript &&
1308 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1310 tryToParseJSFunction();
1313 if ((Style.Language == FormatStyle::LK_JavaScript ||
1314 Style.Language == FormatStyle::LK_Java) &&
1316 if (Style.Language == FormatStyle::LK_JavaScript) {
1334 if (Style.isCpp() && FormatTok->
is(TT_StatementMacro)) {
1335 parseStatementMacro();
1345 if (Style.Language == FormatStyle::LK_JavaScript)
1348 TokenCount = Line->Tokens.size();
1349 if (TokenCount == 1 ||
1350 (TokenCount == 2 && Line->Tokens.front().Tok->is(tok::comment))) {
1351 if (FormatTok->
Tok.
is(tok::colon) && !Line->MustBeDeclaration) {
1352 Line->Tokens.begin()->Tok->MustBreakBefore =
true;
1358 bool FunctionLike = FormatTok->
is(tok::l_paren);
1362 bool FollowedByNewline =
1363 CommentsBeforeNextToken.empty()
1365 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1367 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
1379 if (FormatTok->
is(TT_JsFatArrow)) {
1381 if (FormatTok->
is(tok::l_brace))
1387 if (FormatTok->
Tok.
is(tok::l_brace)) {
1390 }
else if (Style.Language == FormatStyle::LK_Proto &&
1391 FormatTok->
Tok.
is(tok::less)) {
1393 parseBracedList(
false,
1410 bool UnwrappedLineParser::tryToParseLambda() {
1411 if (!Style.isCpp()) {
1415 assert(FormatTok->
is(tok::l_square));
1417 if (!tryToParseLambdaIntroducer())
1420 bool SeenArrow =
false;
1422 while (FormatTok->
isNot(tok::l_brace)) {
1439 case tok::identifier:
1440 case tok::numeric_constant:
1441 case tok::coloncolon:
1442 case tok::kw_mutable:
1443 case tok::kw_noexcept:
1468 case tok::equalequal:
1469 case tok::exclaimequal:
1470 case tok::greaterequal:
1471 case tok::lessequal:
1485 FormatTok->
Type = TT_LambdaArrow;
1493 FormatTok->
Type = TT_LambdaLBrace;
1494 LSquare.Type = TT_LambdaLSquare;
1499 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1502 (Previous->
isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1503 tok::kw_delete, tok::l_square) ||
1510 if (FormatTok->
is(tok::l_square)) {
1517 void UnwrappedLineParser::tryToParseJSFunction() {
1526 if (FormatTok->
is(tok::star)) {
1527 FormatTok->
Type = TT_OverloadedOperator;
1532 if (FormatTok->
is(tok::identifier))
1535 if (FormatTok->
isNot(tok::l_paren))
1541 if (FormatTok->
is(tok::colon)) {
1547 if (FormatTok->
is(tok::l_brace))
1548 tryToParseBracedList();
1550 while (!FormatTok->
isOneOf(tok::l_brace, tok::semi) && !eof())
1554 if (FormatTok->
is(tok::semi))
1560 bool UnwrappedLineParser::tryToParseBracedList() {
1562 calculateBraceTypes();
1571 bool UnwrappedLineParser::parseBracedList(
bool ContinueOnSemicolons,
1573 bool HasError =
false;
1578 if (Style.Language == FormatStyle::LK_JavaScript) {
1581 tryToParseJSFunction();
1584 if (FormatTok->
is(TT_JsFatArrow)) {
1588 if (FormatTok->
is(tok::l_brace)) {
1593 if (FormatTok->
is(tok::l_brace)) {
1595 if (tryToParseBracedList())
1600 if (FormatTok->
Tok.
getKind() == ClosingBraceKind) {
1607 if (FormatTok->
is(tok::l_brace)) {
1618 if (Style.Language == FormatStyle::LK_JavaScript) {
1619 if (FormatTok->
is(tok::l_brace))
1632 if (Style.Language == FormatStyle::LK_Proto) {
1634 parseBracedList(
false,
1645 if (Style.Language == FormatStyle::LK_JavaScript) {
1650 if (!ContinueOnSemicolons)
1665 void UnwrappedLineParser::parseParens() {
1666 assert(FormatTok->
Tok.
is(tok::l_paren) &&
"'(' expected.");
1672 if (Style.Language == FormatStyle::LK_Java && FormatTok->
is(tok::l_brace))
1685 if (!tryToParseBracedList())
1690 if (FormatTok->
Tok.
is(tok::l_brace)) {
1696 if (Style.Language == FormatStyle::LK_JavaScript)
1701 case tok::identifier:
1702 if (Style.Language == FormatStyle::LK_JavaScript &&
1705 tryToParseJSFunction();
1717 if (!LambdaIntroducer) {
1718 assert(FormatTok->
Tok.
is(tok::l_square) &&
"'[' expected.");
1719 if (tryToParseLambda())
1736 case tok::l_brace: {
1737 if (!tryToParseBracedList())
1743 if (FormatTok->
Tok.
is(tok::l_brace)) {
1755 void UnwrappedLineParser::parseIfThenElse() {
1756 assert(FormatTok->
Tok.
is(tok::kw_if) &&
"'if' expected");
1758 if (FormatTok->
Tok.
is(tok::kw_constexpr))
1760 if (FormatTok->
Tok.
is(tok::l_paren))
1762 bool NeedsUnwrappedLine =
false;
1763 if (FormatTok->
Tok.
is(tok::l_brace)) {
1766 if (Style.BraceWrapping.BeforeElse)
1769 NeedsUnwrappedLine =
true;
1773 parseStructuralElement();
1776 if (FormatTok->
Tok.
is(tok::kw_else)) {
1778 if (FormatTok->
Tok.
is(tok::l_brace)) {
1782 }
else if (FormatTok->
Tok.
is(tok::kw_if)) {
1787 parseStructuralElement();
1792 }
else if (NeedsUnwrappedLine) {
1797 void UnwrappedLineParser::parseTryCatch() {
1798 assert(FormatTok->
isOneOf(tok::kw_try, tok::kw___try) &&
"'try' expected");
1800 bool NeedsUnwrappedLine =
false;
1801 if (FormatTok->
is(tok::colon)) {
1804 while (FormatTok->
is(tok::identifier)) {
1806 if (FormatTok->
is(tok::l_paren))
1808 if (FormatTok->
is(tok::comma))
1813 if (Style.Language == FormatStyle::LK_Java && FormatTok->
is(tok::l_paren)) {
1816 if (FormatTok->
is(tok::l_brace)) {
1819 if (Style.BraceWrapping.BeforeCatch) {
1822 NeedsUnwrappedLine =
true;
1824 }
else if (!FormatTok->
is(tok::kw_catch)) {
1830 parseStructuralElement();
1834 if (FormatTok->
is(tok::at))
1837 tok::kw___finally) ||
1838 ((Style.Language == FormatStyle::LK_Java ||
1839 Style.Language == FormatStyle::LK_JavaScript) &&
1845 while (FormatTok->
isNot(tok::l_brace)) {
1846 if (FormatTok->
is(tok::l_paren)) {
1854 NeedsUnwrappedLine =
false;
1857 if (Style.BraceWrapping.BeforeCatch)
1860 NeedsUnwrappedLine =
true;
1862 if (NeedsUnwrappedLine)
1866 void UnwrappedLineParser::parseNamespace() {
1867 assert(FormatTok->
isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
1868 "'namespace' expected");
1872 if (InitialToken.is(TT_NamespaceMacro)) {
1875 while (FormatTok->
isOneOf(tok::identifier, tok::coloncolon))
1878 if (FormatTok->
Tok.
is(tok::l_brace)) {
1882 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1883 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1884 DeclarationScopeStack.size() > 1);
1885 parseBlock(
true, AddLevel);
1888 if (FormatTok->
Tok.
is(tok::semi))
1895 void UnwrappedLineParser::parseNew() {
1896 assert(FormatTok->
is(tok::kw_new) &&
"'new' expected");
1898 if (Style.Language != FormatStyle::LK_Java)
1904 if (FormatTok->
isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1908 if (FormatTok->
is(tok::l_paren)) {
1912 if (FormatTok->
is(tok::l_brace))
1920 void UnwrappedLineParser::parseForOrWhileLoop() {
1921 assert(FormatTok->
isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
1922 "'for', 'while' or foreach macro expected");
1925 if (Style.Language == FormatStyle::LK_JavaScript &&
1928 if (FormatTok->
Tok.
is(tok::l_paren))
1930 if (FormatTok->
Tok.
is(tok::l_brace)) {
1937 parseStructuralElement();
1942 void UnwrappedLineParser::parseDoWhile() {
1943 assert(FormatTok->
Tok.
is(tok::kw_do) &&
"'do' expected");
1945 if (FormatTok->
Tok.
is(tok::l_brace)) {
1948 if (Style.BraceWrapping.IndentBraces)
1953 parseStructuralElement();
1958 if (!FormatTok->
Tok.
is(tok::kw_while)) {
1964 parseStructuralElement();
1967 void UnwrappedLineParser::parseLabel() {
1969 unsigned OldLineLevel = Line->Level;
1970 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1972 if (CommentsBeforeNextToken.empty() && FormatTok->
Tok.
is(tok::l_brace)) {
1974 Style.BraceWrapping.AfterCaseLabel,
1975 Style.BraceWrapping.IndentBraces);
1977 if (FormatTok->
Tok.
is(tok::kw_break)) {
1978 if (Style.BraceWrapping.AfterControlStatement)
1980 parseStructuralElement();
1984 if (FormatTok->
is(tok::semi))
1988 Line->Level = OldLineLevel;
1989 if (FormatTok->
isNot(tok::l_brace)) {
1990 parseStructuralElement();
1995 void UnwrappedLineParser::parseCaseLabel() {
1996 assert(FormatTok->
Tok.
is(tok::kw_case) &&
"'case' expected");
2000 }
while (!eof() && !FormatTok->
Tok.
is(tok::colon));
2004 void UnwrappedLineParser::parseSwitch() {
2005 assert(FormatTok->
Tok.
is(tok::kw_switch) &&
"'switch' expected");
2007 if (FormatTok->
Tok.
is(tok::l_paren))
2009 if (FormatTok->
Tok.
is(tok::l_brace)) {
2016 parseStructuralElement();
2021 void UnwrappedLineParser::parseAccessSpecifier() {
2027 if (FormatTok->
Tok.
is(tok::colon))
2032 bool UnwrappedLineParser::parseEnum() {
2034 if (FormatTok->
Tok.
is(tok::kw_enum))
2040 if (Style.Language == FormatStyle::LK_JavaScript &&
2041 FormatTok->
isOneOf(tok::colon, tok::question))
2045 if (Style.Language == FormatStyle::LK_Proto && FormatTok->
is(tok::equal))
2049 if (FormatTok->
Tok.
is(tok::kw_class) || FormatTok->
Tok.
is(tok::kw_struct))
2053 FormatTok->
isOneOf(tok::colon, tok::coloncolon, tok::less,
2054 tok::greater, tok::comma, tok::question)) {
2057 if (FormatTok->
is(tok::l_paren))
2059 if (FormatTok->
is(tok::identifier)) {
2063 if (Style.isCpp() && FormatTok->
is(tok::identifier))
2069 if (FormatTok->
isNot(tok::l_brace))
2073 if (Style.Language == FormatStyle::LK_Java) {
2075 parseJavaEnumBody();
2078 if (Style.Language == FormatStyle::LK_Proto) {
2085 bool HasError = !parseBracedList(
true);
2087 if (FormatTok->
is(tok::semi))
2098 void UnwrappedLineParser::parseJavaEnumBody() {
2103 bool IsSimple =
true;
2106 if (Tok->
is(tok::r_brace))
2108 if (Tok->
isOneOf(tok::l_brace, tok::semi)) {
2133 if (FormatTok->
is(tok::l_brace)) {
2135 parseBlock(
true,
true,
2137 }
else if (FormatTok->
is(tok::l_paren)) {
2139 }
else if (FormatTok->
is(tok::comma)) {
2142 }
else if (FormatTok->
is(tok::semi)) {
2146 }
else if (FormatTok->
is(tok::r_brace)) {
2161 void UnwrappedLineParser::parseRecord(
bool ParseAsExpr) {
2167 while (FormatTok->
isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2168 tok::kw___attribute, tok::kw___declspec,
2170 ((Style.Language == FormatStyle::LK_Java ||
2171 Style.Language == FormatStyle::LK_JavaScript) &&
2172 FormatTok->
isOneOf(tok::period, tok::comma))) {
2173 if (Style.Language == FormatStyle::LK_JavaScript &&
2179 if (FormatTok->
is(tok::l_brace)) {
2180 tryToParseBracedList();
2184 bool IsNonMacroIdentifier =
2185 FormatTok->
is(tok::identifier) &&
2189 if (!IsNonMacroIdentifier && FormatTok->
Tok.
is(tok::l_paren))
2203 if (FormatTok->
isOneOf(tok::colon, tok::less)) {
2205 if (FormatTok->
is(tok::l_brace)) {
2206 calculateBraceTypes(
true);
2207 if (!tryToParseBracedList())
2210 if (FormatTok->
Tok.
is(tok::semi))
2215 if (FormatTok->
Tok.
is(tok::l_brace)) {
2222 parseBlock(
true,
true,
2231 void UnwrappedLineParser::parseObjCMethod() {
2232 assert(FormatTok->
Tok.
isOneOf(tok::l_paren, tok::identifier) &&
2233 "'(' or identifier expected.");
2235 if (FormatTok->
Tok.
is(tok::semi)) {
2239 }
else if (FormatTok->
Tok.
is(tok::l_brace)) {
2240 if (Style.BraceWrapping.AfterFunction)
2251 void UnwrappedLineParser::parseObjCProtocolList() {
2252 assert(FormatTok->
Tok.
is(tok::less) &&
"'<' expected.");
2256 if (FormatTok->
isOneOf(tok::semi, tok::l_brace) ||
2259 }
while (!eof() && FormatTok->
Tok.
isNot(tok::greater));
2263 void UnwrappedLineParser::parseObjCUntilAtEnd() {
2270 if (FormatTok->
is(tok::l_brace)) {
2274 }
else if (FormatTok->
is(tok::r_brace)) {
2278 }
else if (FormatTok->
isOneOf(tok::minus, tok::plus)) {
2282 parseStructuralElement();
2287 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
2295 if (FormatTok->
Tok.
is(tok::less)) {
2303 unsigned NumOpenAngles = 1;
2307 if (FormatTok->
isOneOf(tok::semi, tok::l_brace) ||
2310 if (FormatTok->
Tok.
is(tok::less))
2312 else if (FormatTok->
Tok.
is(tok::greater)) {
2313 assert(NumOpenAngles > 0 &&
"'>' makes NumOpenAngles negative");
2316 }
while (!eof() && NumOpenAngles != 0);
2319 if (FormatTok->
Tok.
is(tok::colon)) {
2322 }
else if (FormatTok->
Tok.
is(tok::l_paren))
2326 if (FormatTok->
Tok.
is(tok::less))
2327 parseObjCProtocolList();
2329 if (FormatTok->
Tok.
is(tok::l_brace)) {
2330 if (Style.BraceWrapping.AfterObjCDeclaration)
2339 parseObjCUntilAtEnd();
2344 bool UnwrappedLineParser::parseObjCProtocol() {
2348 if (FormatTok->
is(tok::l_paren))
2359 if (FormatTok->
Tok.
is(tok::less))
2360 parseObjCProtocolList();
2363 if (FormatTok->
Tok.
is(tok::semi)) {
2370 parseObjCUntilAtEnd();
2374 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
2375 bool IsImport = FormatTok->
is(Keywords.
kw_import);
2376 assert(IsImport || FormatTok->
is(tok::kw_export));
2380 if (FormatTok->
is(tok::kw_default))
2397 if (!IsImport && !FormatTok->
isOneOf(tok::l_brace, tok::star) &&
2402 if (FormatTok->
is(tok::semi))
2404 if (Line->Tokens.empty()) {
2409 if (FormatTok->
is(tok::l_brace)) {
2419 void UnwrappedLineParser::parseStatementMacro() {
2421 if (FormatTok->
is(tok::l_paren))
2423 if (FormatTok->
is(tok::semi))
2429 StringRef Prefix =
"") {
2430 llvm::dbgs() << Prefix <<
"Line(" << Line.
Level 2433 for (std::list<UnwrappedLineNode>::const_iterator I = Line.
Tokens.begin(),
2436 llvm::dbgs() << I->Tok->Tok.getName() <<
"[" 2437 <<
"T=" << I->Tok->Type <<
", OC=" << I->Tok->OriginalColumn
2440 for (std::list<UnwrappedLineNode>::const_iterator I = Line.
Tokens.begin(),
2451 llvm::dbgs() <<
"\n";
2454 void UnwrappedLineParser::addUnwrappedLine() {
2455 if (Line->Tokens.empty())
2458 if (CurrentLines == &Lines)
2461 CurrentLines->push_back(std::move(*Line));
2462 Line->Tokens.clear();
2464 Line->FirstStartColumn = 0;
2465 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
2466 CurrentLines->append(
2467 std::make_move_iterator(PreprocessorDirectives.begin()),
2468 std::make_move_iterator(PreprocessorDirectives.end()));
2469 PreprocessorDirectives.clear();
2475 bool UnwrappedLineParser::eof()
const {
return FormatTok->
Tok.
is(
tok::eof); }
2477 bool UnwrappedLineParser::isOnNewLine(
const FormatToken &FormatTok) {
2486 llvm::Regex &CommentPragmasRegex) {
2490 StringRef IndentContent = FormatTok.
TokenText;
2491 if (FormatTok.
TokenText.startswith(
"//") ||
2493 IndentContent = FormatTok.
TokenText.substr(2);
2494 if (CommentPragmasRegex.match(IndentContent))
2569 if (PreviousToken && PreviousToken->
is(tok::l_brace) &&
2570 isLineComment(*Node.
Tok)) {
2571 MinColumnToken = PreviousToken;
2574 PreviousToken = Node.
Tok;
2578 MinColumnToken = Node.
Tok;
2581 if (PreviousToken && PreviousToken->
is(tok::l_brace)) {
2582 MinColumnToken = PreviousToken;
2585 return continuesLineComment(FormatTok, Line.
Tokens.back().Tok,
2589 void UnwrappedLineParser::flushComments(
bool NewlineBeforeNext) {
2590 bool JustComments = Line->Tokens.empty();
2592 I = CommentsBeforeNextToken.begin(),
2593 E = CommentsBeforeNextToken.end();
2605 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
2609 if (NewlineBeforeNext && JustComments)
2611 CommentsBeforeNextToken.clear();
2614 void UnwrappedLineParser::nextToken(
int LevelDifference) {
2617 flushComments(isOnNewLine(*FormatTok));
2618 pushToken(FormatTok);
2620 if (Style.Language != FormatStyle::LK_JavaScript)
2621 readToken(LevelDifference);
2623 readTokenWithJavaScriptASI();
2627 void UnwrappedLineParser::distributeComments(
2648 if (Comments.empty())
2650 bool ShouldPushCommentsInCurrentLine =
true;
2651 bool HasTrailAlignedWithNextToken =
false;
2652 unsigned StartOfTrailAlignedWithNextToken = 0;
2655 for (
unsigned i = Comments.size() - 1;
i > 0; --
i) {
2657 HasTrailAlignedWithNextToken =
true;
2658 StartOfTrailAlignedWithNextToken =
i;
2662 for (
unsigned i = 0, e = Comments.size();
i < e; ++
i) {
2664 if (HasTrailAlignedWithNextToken &&
i == StartOfTrailAlignedWithNextToken) {
2671 (isOnNewLine(*FormatTok) || FormatTok->
IsFirst)) {
2672 ShouldPushCommentsInCurrentLine =
false;
2674 if (ShouldPushCommentsInCurrentLine) {
2675 pushToken(FormatTok);
2677 CommentsBeforeNextToken.push_back(FormatTok);
2682 void UnwrappedLineParser::readToken(
int LevelDifference) {
2687 while (!Line->InPPDirective && FormatTok->
Tok.
is(tok::hash) &&
2689 distributeComments(Comments, FormatTok);
2693 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
2695 assert((LevelDifference >= 0 ||
2696 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2697 "LevelDifference makes Line->Level negative");
2698 Line->Level += LevelDifference;
2702 if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&
2704 Line->Level += PPBranchLevel;
2705 flushComments(isOnNewLine(*FormatTok));
2708 while (FormatTok->
Type == TT_ConflictStart ||
2709 FormatTok->
Type == TT_ConflictEnd ||
2710 FormatTok->
Type == TT_ConflictAlternative) {
2711 if (FormatTok->
Type == TT_ConflictStart) {
2712 conditionalCompilationStart(
false);
2713 }
else if (FormatTok->
Type == TT_ConflictAlternative) {
2714 conditionalCompilationAlternative();
2715 }
else if (FormatTok->
Type == TT_ConflictEnd) {
2716 conditionalCompilationEnd();
2722 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
2723 !Line->InPPDirective) {
2727 if (!FormatTok->
Tok.
is(tok::comment)) {
2728 distributeComments(Comments, FormatTok);
2733 Comments.push_back(FormatTok);
2736 distributeComments(Comments,
nullptr);
2742 if (MustBreakBeforeNextToken) {
2743 Line->Tokens.back().Tok->MustBreakBefore =
true;
2744 MustBreakBeforeNextToken =
false;
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
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.
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
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