22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/raw_os_ostream.h"
26#include "llvm/Support/raw_ostream.h"
31#define DEBUG_TYPE "format-parser"
38void printLine(llvm::raw_ostream &OS,
const UnwrappedLine &
Line,
39 StringRef Prefix =
"",
bool PrintText =
false) {
40 OS << Prefix <<
"Line(" <<
Line.Level <<
", FSC=" <<
Line.FirstStartColumn
41 <<
")" << (
Line.InPPDirective ?
" MACRO" :
"") <<
": ";
43 for (std::list<UnwrappedLineNode>::const_iterator I =
Line.Tokens.begin(),
44 E =
Line.Tokens.end();
50 OS << I->Tok->Tok.getName() <<
"[" <<
"T=" << (
unsigned)I->Tok->getType()
51 <<
", OC=" << I->Tok->OriginalColumn <<
", \"" << I->Tok->TokenText
53 for (SmallVectorImpl<UnwrappedLine>::const_iterator
54 CI = I->Children.begin(),
55 CE = I->Children.end();
58 printLine(OS, *CI, (Prefix +
" ").str());
66LLVM_ATTRIBUTE_UNUSED
static void printDebugInfo(
const UnwrappedLine &
Line) {
67 printLine(llvm::dbgs(),
Line);
70class ScopedDeclarationState {
72 ScopedDeclarationState(UnwrappedLine &Line, llvm::BitVector &Stack,
73 bool MustBeDeclaration)
74 : Line(Line), Stack(Stack) {
75 Line.MustBeDeclaration = MustBeDeclaration;
76 Stack.push_back(MustBeDeclaration);
78 ~ScopedDeclarationState() {
81 Line.MustBeDeclaration = Stack.back();
83 Line.MustBeDeclaration =
true;
88 llvm::BitVector &Stack;
94 llvm::raw_os_ostream OS(Stream);
102 bool SwitchToPreprocessorLines =
false)
104 if (SwitchToPreprocessorLines)
106 else if (!
Parser.Line->Tokens.empty())
107 Parser.CurrentLines = &
Parser.Line->Tokens.back().Children;
108 PreBlockLine = std::move(
Parser.Line);
109 Parser.Line = std::make_unique<UnwrappedLine>();
110 Parser.Line->Level = PreBlockLine->Level;
111 Parser.Line->PPLevel = PreBlockLine->PPLevel;
112 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
113 Parser.Line->InMacroBody = PreBlockLine->InMacroBody;
117 if (!
Parser.Line->Tokens.empty())
118 Parser.addUnwrappedLine();
119 assert(
Parser.Line->Tokens.empty());
120 Parser.Line = std::move(PreBlockLine);
121 if (
Parser.CurrentLines == &
Parser.PreprocessorDirectives)
122 Parser.MustBreakBeforeNextToken =
true;
123 Parser.CurrentLines = OriginalLines;
129 std::unique_ptr<UnwrappedLine> PreBlockLine;
138 Style.BraceWrapping.AfterControlStatement,
139 Style.BraceWrapping.IndentBraces) {}
141 bool WrapBrace,
bool IndentBrace)
142 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
144 Parser->addUnwrappedLine();
152 unsigned OldLineLevel;
159 llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
162 CurrentLines(&Lines), Style(Style), IsCpp(Style.isCpp()),
163 Keywords(Keywords), CommentPragmasRegex(Style.CommentPragmas),
164 Tokens(nullptr), Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
165 IncludeGuard(Style.IndentPPDirectives ==
FormatStyle::PPDIS_None
168 IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn),
169 Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) {}
171void UnwrappedLineParser::reset() {
176 IncludeGuardToken =
nullptr;
178 CommentsBeforeNextToken.clear();
180 MustBreakBeforeNextToken =
false;
181 IsDecltypeAutoFunction =
false;
182 PreprocessorDirectives.clear();
183 CurrentLines = &Lines;
184 DeclarationScopeStack.clear();
185 NestedTooDeep.clear();
186 NestedLambdas.clear();
188 Line->FirstStartColumn = FirstStartColumn;
190 if (!Unexpanded.empty())
192 Token->MacroCtx.reset();
193 CurrentExpandedLines.clear();
194 ExpandedLines.clear();
202 Line->FirstStartColumn = FirstStartColumn;
204 LLVM_DEBUG(llvm::dbgs() <<
"----\n");
206 Tokens = &TokenSource;
214 if (IncludeGuard == IG_Found) {
215 for (
auto &Line : Lines)
216 if (Line.InPPDirective && Line.Level > 0)
222 pushToken(FormatTok);
227 if (!ExpandedLines.empty()) {
228 LLVM_DEBUG(llvm::dbgs() <<
"Expanded lines:\n");
229 for (
const auto &Line : Lines) {
230 if (!Line.Tokens.empty()) {
231 auto it = ExpandedLines.find(Line.Tokens.begin()->Tok);
232 if (it != ExpandedLines.end()) {
233 for (
const auto &Expanded : it->second) {
234 LLVM_DEBUG(printDebugInfo(Expanded));
240 LLVM_DEBUG(printDebugInfo(Line));
246 LLVM_DEBUG(llvm::dbgs() <<
"Unwrapped lines:\n");
248 LLVM_DEBUG(printDebugInfo(Line));
253 while (!PPLevelBranchIndex.empty() &&
254 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
255 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
256 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
258 if (!PPLevelBranchIndex.empty()) {
259 ++PPLevelBranchIndex.back();
260 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
261 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
263 }
while (!PPLevelBranchIndex.empty());
266void UnwrappedLineParser::parseFile() {
269 bool MustBeDeclaration = !Line->InPPDirective && !Style.
isJavaScript();
270 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
287 !CommentsBeforeNextToken.empty()) {
294void UnwrappedLineParser::parseCSharpGenericTypeConstraint() {
303 parseCSharpGenericTypeConstraint();
312void UnwrappedLineParser::parseCSharpAttribute() {
313 int UnpairedSquareBrackets = 1;
318 --UnpairedSquareBrackets;
319 if (UnpairedSquareBrackets == 0) {
325 ++UnpairedSquareBrackets;
335bool UnwrappedLineParser::precededByCommentOrPPDirective()
const {
336 if (!Lines.empty() && Lines.back().InPPDirective)
350bool UnwrappedLineParser::parseLevel(
const FormatToken *OpeningBrace,
352 FormatToken **IfLeftBrace) {
353 const bool InRequiresExpression =
354 OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
355 const bool IsPrecededByCommentOrPPDirective =
357 FormatToken *IfLBrace =
nullptr;
358 bool HasDoWhile =
false;
359 bool HasLabel =
false;
360 unsigned StatementCount = 0;
361 bool SwitchLabelEncountered =
false;
369 if (FormatTok->
is(TT_MacroBlockBegin))
371 else if (FormatTok->
is(TT_MacroBlockEnd))
374 auto ParseDefault = [
this, OpeningBrace, IfKind, &IfLBrace, &HasDoWhile,
375 &HasLabel, &StatementCount] {
376 parseStructuralElement(OpeningBrace, IfKind, &IfLBrace,
377 HasDoWhile ?
nullptr : &HasDoWhile,
378 HasLabel ?
nullptr : &HasLabel);
380 assert(StatementCount > 0 &&
"StatementCount overflow!");
389 if (InRequiresExpression) {
398 if (!InRequiresExpression && FormatTok->
isNot(TT_MacroBlockBegin)) {
399 if (tryToParseBracedList())
405 assert(StatementCount > 0 &&
"StatementCount overflow!");
411 !OpeningBrace->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace)) {
414 if (FormatTok->
isNot(tok::r_brace) || StatementCount != 1 || HasLabel ||
415 HasDoWhile || IsPrecededByCommentOrPPDirective ||
416 precededByCommentOrPPDirective()) {
420 if (Next->is(tok::comment) && Next->NewlinesBefore == 0)
423 *IfLeftBrace = IfLBrace;
429 case tok::kw_default: {
433 if (Next->isNot(tok::colon)) {
436 parseStructuralElement();
452 if (!SwitchLabelEncountered &&
454 (Line->InPPDirective && Line->Level == 1))) {
457 SwitchLabelEncountered =
true;
458 parseStructuralElement();
463 parseCSharpAttribute();
466 if (handleCppAttributes())
478void UnwrappedLineParser::calculateBraceTypes(
bool ExpectClassBody) {
484 FormatToken *Tok = FormatTok;
485 const FormatToken *PrevTok = Tok->
Previous;
491 const FormatToken *PrevTok;
493 SmallVector<StackEntry, 8> LBraceStack;
494 assert(Tok->is(tok::l_brace));
499 if (!Line->InMacroBody && !Style.
isTableGen()) {
501 while (NextTok->is(tok::hash)) {
504 }
while (NextTok->NewlinesBefore == 0 && NextTok->isNot(tok::eof));
506 while (NextTok->is(tok::comment))
511 switch (Tok->Tok.getKind()) {
514 if (PrevTok->isOneOf(tok::colon, tok::less)) {
525 }
else if (PrevTok->is(tok::r_paren)) {
532 LBraceStack.push_back({Tok, PrevTok});
535 if (LBraceStack.empty())
538 bool ProbablyBracedList =
false;
540 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
544 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
545 NextTok->OriginalColumn == 0;
555 ProbablyBracedList = LBraceStack.back().Tok->is(TT_BracedListLBrace);
557 ProbablyBracedList = ProbablyBracedList ||
559 NextTok->isOneOf(Keywords.
kw_of, Keywords.
kw_in,
562 ProbablyBracedList || (IsCpp && NextTok->is(tok::l_paren));
569 ProbablyBracedList ||
570 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
571 tok::r_paren, tok::r_square, tok::ellipsis);
576 ProbablyBracedList ||
577 (NextTok->is(tok::l_brace) && LBraceStack.back().PrevTok &&
578 LBraceStack.back().PrevTok->isOneOf(tok::identifier,
582 ProbablyBracedList ||
583 (NextTok->is(tok::identifier) &&
584 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace));
586 ProbablyBracedList = ProbablyBracedList ||
587 (NextTok->is(tok::semi) &&
588 (!ExpectClassBody || LBraceStack.size() != 1));
591 ProbablyBracedList ||
592 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
594 if (!Style.
isCSharp() && NextTok->is(tok::l_square)) {
598 ProbablyBracedList = NextTok->
isNot(tok::l_square);
602 if (IsCpp && Line->InMacroBody && PrevTok != FormatTok &&
603 !FormatTok->
Previous && NextTok->
is(tok::eof) &&
607 !PrevTok->isOneOf(tok::semi,
BK_Block, tok::colon)) {
608 ProbablyBracedList =
true;
611 if (ProbablyBracedList) {
616 LBraceStack.back().Tok->setBlockKind(
BK_Block);
619 LBraceStack.pop_back();
621 case tok::identifier:
622 if (Tok->isNot(TT_StatementMacro))
633 if (!LBraceStack.empty() && LBraceStack.back().Tok->is(
BK_Unknown))
634 LBraceStack.back().Tok->setBlockKind(
BK_Block);
642 }
while (Tok->isNot(tok::eof) && !LBraceStack.empty());
645 for (
const auto &Entry : LBraceStack)
653void UnwrappedLineParser::setPreviousRBraceType(
TokenType Type) {
655 Prev && Prev->
is(tok::r_brace)) {
663 seed ^= hasher(
v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
666size_t UnwrappedLineParser::computePPHash()
const {
668 for (
const auto &i : PPStack) {
679bool UnwrappedLineParser::mightFitOnOneLine(
680 UnwrappedLine &ParsedLine,
const FormatToken *OpeningBrace)
const {
682 if (ColumnLimit == 0)
685 auto &Tokens = ParsedLine.Tokens;
686 assert(!Tokens.empty());
688 const auto *LastToken = Tokens.back().Tok;
691 SmallVector<UnwrappedLineNode> SavedTokens(Tokens.size());
694 for (
const auto &Token : Tokens) {
696 auto &SavedToken = SavedTokens[Index++];
697 SavedToken.Tok =
new FormatToken;
698 SavedToken.Tok->copyFrom(*Token.Tok);
699 SavedToken.Children = std::move(Token.Children);
702 AnnotatedLine Line(ParsedLine);
703 assert(Line.Last == LastToken);
705 TokenAnnotator Annotator(Style, Keywords);
706 Annotator.annotate(Line);
707 Annotator.calculateFormattingInformation(Line);
709 auto Length = LastToken->TotalLength;
711 assert(OpeningBrace != Tokens.front().Tok);
712 if (
auto Prev = OpeningBrace->Previous;
713 Prev && Prev->TotalLength + ColumnLimit == OpeningBrace->TotalLength) {
714 Length -= ColumnLimit;
716 Length -= OpeningBrace->TokenText.size() + 1;
719 if (
const auto *FirstToken = Line.First; FirstToken->is(tok::r_brace)) {
720 assert(!OpeningBrace || OpeningBrace->is(TT_ControlStatementLBrace));
721 Length -= FirstToken->TokenText.size() + 1;
725 for (
auto &Token : Tokens) {
726 const auto &SavedToken = SavedTokens[Index++];
727 Token.Tok->copyFrom(*SavedToken.Tok);
728 Token.Children = std::move(SavedToken.Children);
729 delete SavedToken.Tok;
733 assert(!Line.InMacroBody);
734 assert(!Line.InPPDirective);
735 return Line.Level * Style.
IndentWidth + Length <= ColumnLimit;
738FormatToken *UnwrappedLineParser::parseBlock(
bool MustBeDeclaration,
739 unsigned AddLevels,
bool MunchSemi,
742 bool UnindentWhitesmithsBraces) {
743 auto HandleVerilogBlockLabel = [
this]() {
745 if (Style.
isVerilog() && FormatTok->
is(tok::colon)) {
754 const bool VerilogHierarchy =
756 assert((FormatTok->
isOneOf(tok::l_brace, TT_MacroBlockBegin) ||
759 "'{' or macro block token expected");
760 FormatToken *Tok = FormatTok;
761 const bool FollowedByComment = Tokens->peekNextToken()->is(tok::comment);
762 auto Index = CurrentLines->size();
763 const bool MacroBlock = FormatTok->
is(TT_MacroBlockBegin);
768 if (!VerilogHierarchy && AddLevels > 0 &&
773 size_t PPStartHash = computePPHash();
775 const unsigned InitialLevel = Line->Level;
776 if (VerilogHierarchy) {
777 AddLevels += parseVerilogHierarchyHeader();
779 nextToken(AddLevels);
780 HandleVerilogBlockLabel();
784 if (Line->Level > 300)
787 if (MacroBlock && FormatTok->
is(tok::l_paren))
790 size_t NbPreprocessorDirectives =
791 !parsingPPDirective() ? PreprocessorDirectives.size() : 0;
793 size_t OpeningLineIndex =
794 CurrentLines->empty()
796 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
801 if (UnindentWhitesmithsBraces)
804 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
807 Line->Level += AddLevels;
809 FormatToken *IfLBrace =
nullptr;
810 const bool SimpleBlock = parseLevel(Tok, IfKind, &IfLBrace);
815 if (MacroBlock ? FormatTok->
isNot(TT_MacroBlockEnd)
816 : FormatTok->
isNot(tok::r_brace)) {
817 Line->Level = InitialLevel;
822 if (FormatTok->
is(tok::r_brace) && Tok->is(TT_NamespaceLBrace))
825 const bool IsFunctionRBrace =
826 FormatTok->
is(tok::r_brace) && Tok->is(TT_FunctionLBrace);
828 auto RemoveBraces = [=]()
mutable {
831 assert(Tok->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace));
832 assert(FormatTok->
is(tok::r_brace));
833 const bool WrappedOpeningBrace = !Tok->Previous;
834 if (WrappedOpeningBrace && FollowedByComment)
836 const bool HasRequiredIfBraces = IfLBrace && !IfLBrace->Optional;
837 if (KeepBraces && !HasRequiredIfBraces)
839 if (Tok->isNot(TT_ElseLBrace) || !HasRequiredIfBraces) {
840 const FormatToken *
Previous = Tokens->getPreviousToken();
845 assert(!CurrentLines->empty());
846 auto &LastLine = CurrentLines->back();
847 if (LastLine.Level == InitialLevel + 1 && !mightFitOnOneLine(LastLine))
849 if (Tok->is(TT_ElseLBrace))
851 if (WrappedOpeningBrace) {
856 return mightFitOnOneLine((*CurrentLines)[Index], Tok);
858 if (RemoveBraces()) {
859 Tok->MatchingParen = FormatTok;
863 size_t PPEndHash = computePPHash();
866 nextToken(-AddLevels);
872 while (FormatTok->
is(tok::semi)) {
878 HandleVerilogBlockLabel();
880 if (MacroBlock && FormatTok->
is(tok::l_paren))
883 Line->Level = InitialLevel;
885 if (FormatTok->
is(tok::kw_noexcept)) {
890 if (FormatTok->
is(tok::arrow)) {
894 parseStructuralElement();
897 if (MunchSemi && FormatTok->
is(tok::semi))
900 if (PPStartHash == PPEndHash) {
901 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
904 (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
905 CurrentLines->size() - 1;
915 if (
Line.Tokens.size() < 4)
917 auto I =
Line.Tokens.begin();
918 if (I->Tok->TokenText !=
"goog")
921 if (I->Tok->isNot(tok::period))
924 if (I->Tok->TokenText !=
"scope")
927 return I->Tok->is(tok::l_paren);
936 if (
Line.Tokens.size() < 3)
938 auto I =
Line.Tokens.begin();
939 if (I->Tok->isNot(tok::l_paren))
945 return I->Tok->is(tok::l_paren);
951 if (InitialToken.
is(TT_NamespaceMacro))
952 Kind = tok::kw_namespace;
955 case tok::kw_namespace:
970void UnwrappedLineParser::parseChildBlock() {
971 assert(FormatTok->
is(tok::l_brace));
973 const FormatToken *OpeningBrace = FormatTok;
979 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
981 Line->Level += SkipIndent ? 0 : 1;
982 parseLevel(OpeningBrace);
983 flushComments(isOnNewLine(*FormatTok));
984 Line->Level -= SkipIndent ? 0 : 1;
989void UnwrappedLineParser::parsePPDirective() {
990 assert(FormatTok->
is(tok::hash) &&
"'#' expected");
991 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
1001 case tok::pp_define:
1008 case tok::pp_ifndef:
1012 case tok::pp_elifdef:
1013 case tok::pp_elifndef:
1020 case tok::pp_pragma:
1029void UnwrappedLineParser::conditionalCompilationCondition(
bool Unreachable) {
1030 size_t Line = CurrentLines->size();
1031 if (CurrentLines == &PreprocessorDirectives)
1032 Line += Lines.size();
1035 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable)) {
1036 PPStack.push_back({PP_Unreachable, Line});
1038 PPStack.push_back({PP_Conditional, Line});
1042void UnwrappedLineParser::conditionalCompilationStart(
bool Unreachable) {
1044 assert(PPBranchLevel >= 0 && PPBranchLevel <= (
int)PPLevelBranchIndex.size());
1045 if (PPBranchLevel == (
int)PPLevelBranchIndex.size()) {
1046 PPLevelBranchIndex.push_back(0);
1047 PPLevelBranchCount.push_back(0);
1049 PPChainBranchIndex.push(Unreachable ? -1 : 0);
1050 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
1051 conditionalCompilationCondition(Unreachable || Skip);
1054void UnwrappedLineParser::conditionalCompilationAlternative() {
1055 if (!PPStack.empty())
1057 assert(PPBranchLevel < (
int)PPLevelBranchIndex.size());
1058 if (!PPChainBranchIndex.empty())
1059 ++PPChainBranchIndex.top();
1060 conditionalCompilationCondition(
1061 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
1062 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
1065void UnwrappedLineParser::conditionalCompilationEnd() {
1066 assert(PPBranchLevel < (
int)PPLevelBranchIndex.size());
1067 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
1068 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel])
1069 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
1072 if (PPBranchLevel > -1)
1074 if (!PPChainBranchIndex.empty())
1075 PPChainBranchIndex.pop();
1076 if (!PPStack.empty())
1080void UnwrappedLineParser::parsePPIf(
bool IfDef) {
1081 bool IfNDef = FormatTok->
is(tok::pp_ifndef);
1083 bool Unreachable =
false;
1084 if (!IfDef && (FormatTok->
is(tok::kw_false) || FormatTok->
TokenText ==
"0"))
1086 if (IfDef && !IfNDef && FormatTok->
TokenText ==
"SWIG")
1088 conditionalCompilationStart(Unreachable);
1089 FormatToken *IfCondition = FormatTok;
1092 bool MaybeIncludeGuard = IfNDef;
1093 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
1094 for (
auto &Line : Lines) {
1095 if (Line.Tokens.front().Tok->isNot(tok::comment)) {
1096 MaybeIncludeGuard =
false;
1097 IncludeGuard = IG_Rejected;
1105 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
1106 IncludeGuard = IG_IfNdefed;
1107 IncludeGuardToken = IfCondition;
1111void UnwrappedLineParser::parsePPElse() {
1113 if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
1114 IncludeGuard = IG_Rejected;
1116 assert(PPBranchLevel >= -1);
1117 if (PPBranchLevel == -1)
1118 conditionalCompilationStart(
true);
1119 conditionalCompilationAlternative();
1125void UnwrappedLineParser::parsePPEndIf() {
1126 conditionalCompilationEnd();
1130 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 && Tokens->isEOF() &&
1132 IncludeGuard = IG_Found;
1136void UnwrappedLineParser::parsePPDefine() {
1140 IncludeGuard = IG_Rejected;
1141 IncludeGuardToken =
nullptr;
1146 if (IncludeGuard == IG_IfNdefed &&
1148 IncludeGuard = IG_Defined;
1149 IncludeGuardToken =
nullptr;
1150 for (
auto &Line : Lines) {
1151 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
1152 IncludeGuard = IG_Rejected;
1166 if (FormatTok->
Tok.
getKind() == tok::l_paren &&
1171 Line->Level += PPBranchLevel + 1;
1175 Line->PPLevel = PPBranchLevel + (IncludeGuard == IG_Defined ? 0 : 1);
1176 assert((
int)Line->PPLevel >= 0);
1177 Line->InMacroBody =
true;
1188 if (FormatTok->
is(tok::identifier) &&
1189 Tokens->peekNextToken()->is(tok::colon)) {
1202void UnwrappedLineParser::parsePPPragma() {
1203 Line->InPragmaDirective =
true;
1207void UnwrappedLineParser::parsePPUnknown() {
1212 Line->Level += PPBranchLevel + 1;
1222 return !Tok.
isOneOf(tok::semi, tok::l_brace,
1225 tok::period, tok::periodstar, tok::arrow, tok::arrowstar,
1226 tok::less, tok::greater, tok::slash, tok::percent,
1227 tok::lessless, tok::greatergreater, tok::equal,
1228 tok::plusequal, tok::minusequal, tok::starequal,
1229 tok::slashequal, tok::percentequal, tok::ampequal,
1230 tok::pipeequal, tok::caretequal, tok::greatergreaterequal,
1243 return FormatTok->
is(tok::identifier) &&
1258 FormatTok->
isOneOf(tok::kw_true, tok::kw_false) ||
1269 tok::kw_if, tok::kw_else,
1271 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
1273 tok::kw_switch, tok::kw_case,
1275 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.
kw_finally,
1277 tok::kw_const, tok::kw_class, Keywords.
kw_var, Keywords.
kw_let,
1285 return Tok.
isOneOf(tok::kw_char, tok::kw_short, tok::kw_int, tok::kw_long,
1286 tok::kw_unsigned, tok::kw_float, tok::kw_double,
1303 if (FuncName->
isNot(tok::identifier))
1311 !Tok->
isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union)) {
1315 if (Next->isNot(tok::star) && !Next->Tok.getIdentifierInfo())
1319 if (!Tok || Tok->
isNot(tok::r_paren))
1323 if (!Tok || Tok->
isNot(tok::identifier))
1329bool UnwrappedLineParser::parseModuleImport() {
1330 assert(FormatTok->
is(Keywords.
kw_import) &&
"'import' expected");
1332 if (
auto Token = Tokens->peekNextToken(
true);
1334 !
Token->
isOneOf(tok::colon, tok::less, tok::string_literal)) {
1340 if (FormatTok->
is(tok::colon)) {
1344 else if (FormatTok->
is(tok::less)) {
1346 while (!FormatTok->
isOneOf(tok::semi, tok::greater, tok::eof)) {
1349 if (FormatTok->
isNot(tok::comment) &&
1350 !FormatTok->
TokenText.starts_with(
"//")) {
1356 if (FormatTok->
is(tok::semi)) {
1374void UnwrappedLineParser::readTokenWithJavaScriptASI() {
1377 FormatToken *Next = FormatTok;
1380 CommentsBeforeNextToken.empty()
1381 ? Next->NewlinesBefore == 0
1382 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
1387 bool PreviousStartsTemplateExpr =
1389 if (PreviousMustBeValue ||
Previous->is(tok::r_paren)) {
1392 bool HasAt = llvm::any_of(Line->Tokens, [](UnwrappedLineNode &LineNode) {
1393 return LineNode.Tok->is(tok::at);
1398 if (Next->is(tok::exclaim) && PreviousMustBeValue)
1399 return addUnwrappedLine();
1401 bool NextEndsTemplateExpr =
1402 Next->is(TT_TemplateString) && Next->TokenText.starts_with(
"}");
1403 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
1404 (PreviousMustBeValue ||
1405 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
1406 tok::minusminus))) {
1407 return addUnwrappedLine();
1409 if ((PreviousMustBeValue ||
Previous->is(tok::r_paren)) &&
1411 return addUnwrappedLine();
1415void UnwrappedLineParser::parseStructuralElement(
1416 const FormatToken *OpeningBrace, IfStmtKind *IfKind,
1417 FormatToken **IfLeftBrace,
bool *HasDoWhile,
bool *HasLabel) {
1419 FormatTok->
is(tok::pp_include)) {
1421 if (FormatTok->
is(tok::string_literal))
1428 while (FormatTok->
is(tok::l_square) && handleCppAttributes()) {
1432 parseForOrWhileLoop(
false);
1436 parseForOrWhileLoop();
1441 parseIfThenElse(IfKind,
false,
true);
1450 }
else if (FormatTok->
is(tok::l_paren) &&
1451 Tokens->peekNextToken()->is(tok::star)) {
1463 if (FormatTok->
is(tok::l_brace)) {
1466 while (FormatTok && !eof()) {
1467 if (FormatTok->
is(tok::r_brace)) {
1478 case tok::kw_namespace:
1481 case tok::kw_public:
1482 case tok::kw_protected:
1483 case tok::kw_private:
1488 parseAccessSpecifier();
1496 FormatToken *Tok = parseIfThenElse(IfKind);
1507 parseForOrWhileLoop();
1518 case tok::kw_switch:
1525 case tok::kw_default:
1534 if (FormatTok->
is(tok::colon)) {
1567 case tok::kw_extern:
1573 parseVerilogHierarchyHeader();
1576 }
else if (FormatTok->
is(tok::string_literal)) {
1578 if (FormatTok->
is(tok::l_brace)) {
1583 unsigned AddLevels =
1590 parseBlock(
true, AddLevels);
1596 case tok::kw_export:
1598 parseJavaScriptEs6ImportExport();
1603 if (FormatTok->
is(tok::kw_namespace)) {
1607 if (FormatTok->
is(Keywords.
kw_import) && parseModuleImport())
1611 case tok::kw_inline:
1613 if (FormatTok->
is(tok::kw_namespace)) {
1618 case tok::identifier:
1619 if (FormatTok->
is(TT_ForEachMacro)) {
1620 parseForOrWhileLoop();
1623 if (FormatTok->
is(TT_MacroBlockBegin)) {
1624 parseBlock(
false, 1u,
1630 parseJavaScriptEs6ImportExport();
1635 if (FormatTok->
is(tok::kw_public))
1637 if (FormatTok->
isNot(tok::string_literal))
1640 if (FormatTok->
is(tok::semi))
1645 if (IsCpp && parseModuleImport())
1651 if (FormatTok->
is(tok::colon)) {
1657 if (IsCpp && FormatTok->
is(TT_StatementMacro)) {
1658 parseStatementMacro();
1661 if (IsCpp && FormatTok->
is(TT_NamespaceMacro)) {
1670 Tokens->peekNextToken()->is(tok::colon) && !Line->MustBeDeclaration) {
1672 Line->Tokens.begin()->Tok->MustBreakBefore =
true;
1685 const bool InRequiresExpression =
1686 OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
1692 if (FormatTok->
is(tok::l_brace)) {
1702 case tok::objc_public:
1703 case tok::objc_protected:
1704 case tok::objc_package:
1705 case tok::objc_private:
1706 return parseAccessSpecifier();
1707 case tok::objc_interface:
1708 case tok::objc_implementation:
1709 return parseObjCInterfaceOrImplementation();
1710 case tok::objc_protocol:
1711 if (parseObjCProtocol())
1716 case tok::objc_optional:
1717 case tok::objc_required:
1721 case tok::objc_autoreleasepool:
1723 if (FormatTok->
is(tok::l_brace)) {
1732 case tok::objc_synchronized:
1734 if (FormatTok->
is(tok::l_paren)) {
1738 if (FormatTok->
is(tok::l_brace)) {
1756 case tok::kw_requires: {
1758 bool ParsedClause = parseRequires();
1783 case tok::kw_typedef:
1805 case tok::kw_struct:
1807 if (parseStructLike())
1810 case tok::kw_decltype:
1812 if (FormatTok->
is(tok::l_paren)) {
1817 Line->SeenDecltypeAuto =
true;
1825 FormatTok->
is(tok::kw_class)) {
1842 case tok::l_paren: {
1846 if (OpeningBrace || !IsCpp || !
Previous || eof())
1849 Tokens->peekNextToken(
true),
1856 case tok::kw_operator:
1867 while (FormatTok->
is(tok::star))
1871 if (FormatTok->
is(tok::l_paren))
1874 if (FormatTok->
is(tok::l_brace))
1878 if (InRequiresExpression)
1880 if (!tryToParsePropertyAccessor() && !tryToParseBracedList()) {
1881 IsDecltypeAutoFunction = Line->SeenDecltypeAuto;
1899 IsDecltypeAutoFunction =
false;
1917 case tok::identifier: {
1919 Line->MustBeDeclaration) {
1921 parseCSharpGenericTypeConstraint();
1924 if (FormatTok->
is(TT_MacroBlockEnd)) {
1933 size_t TokenCount = Line->Tokens.size();
1937 Line->Tokens.front().Tok->isNot(Keywords.
kw_async)))) {
1938 tryToParseJSFunction();
1948 unsigned StoredPosition = Tokens->getPosition();
1949 FormatToken *Next = Tokens->getNextToken();
1950 FormatTok = Tokens->setPosition(StoredPosition);
1963 parseVerilogTable();
1975 if (parseStructLike())
1980 if (IsCpp && FormatTok->
is(TT_StatementMacro)) {
1981 parseStatementMacro();
1988 FormatToken *PreviousToken = FormatTok;
1996 auto OneTokenSoFar = [&]() {
1997 auto I = Line->Tokens.begin(), E = Line->Tokens.end();
1998 while (I != E && I->Tok->is(tok::comment))
2001 while (I != E && I->Tok->is(tok::hash))
2003 return I != E && (++I == E);
2005 if (OneTokenSoFar()) {
2008 bool FunctionLike = FormatTok->
is(tok::l_paren);
2012 bool FollowedByNewline =
2013 CommentsBeforeNextToken.empty()
2015 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
2017 if (FollowedByNewline && (
Text.size() >= 5 || FunctionLike) &&
2019 if (PreviousToken->isNot(TT_UntouchableMacroFunc))
2020 PreviousToken->setFinalizedType(TT_FunctionLikeOrFreestandingMacro);
2029 FormatTok->
is(TT_FatArrow)) {
2030 tryToParseChildBlock();
2035 if (FormatTok->
is(tok::l_brace)) {
2044 Line->Tokens.begin()->Tok->is(Keywords.
kw_defset)) {
2046 parseBlock(
false, 1u,
2054 FormatTok->
is(tok::less)) {
2056 parseBracedList(
true);
2084 case tok::kw_default:
2087 if (FormatTok->
is(tok::colon)) {
2097 parseVerilogCaseLabel();
2104 parseVerilogCaseLabel();
2115bool UnwrappedLineParser::tryToParsePropertyAccessor() {
2116 assert(FormatTok->
is(tok::l_brace));
2128 unsigned int StoredPosition = Tokens->getPosition();
2129 FormatToken *Tok = Tokens->getNextToken();
2134 bool HasSpecialAccessor =
false;
2135 bool IsTrivialPropertyAccessor =
true;
2137 if (Tok->isOneOf(tok::semi, tok::kw_public, tok::kw_private,
2141 HasSpecialAccessor =
true;
2142 Tok = Tokens->getNextToken();
2145 if (Tok->isNot(tok::r_brace))
2146 IsTrivialPropertyAccessor =
false;
2150 if (!HasSpecialAccessor) {
2151 Tokens->setPosition(StoredPosition);
2157 Tokens->setPosition(StoredPosition);
2165 if (FormatTok->
is(tok::equal)) {
2166 while (!eof() && FormatTok->
isNot(tok::semi))
2179 if (FormatTok->
is(TT_FatArrow)) {
2183 }
while (!eof() && FormatTok->
isNot(tok::semi));
2194 !IsTrivialPropertyAccessor) {
2206bool UnwrappedLineParser::tryToParseLambda() {
2207 assert(FormatTok->
is(tok::l_square));
2212 FormatToken &LSquare = *FormatTok;
2213 if (!tryToParseLambdaIntroducer())
2216 bool SeenArrow =
false;
2217 bool InTemplateParameterList =
false;
2219 while (FormatTok->
isNot(tok::l_brace)) {
2228 parseParens(TT_PointerOrReference);
2236 InTemplateParameterList =
true;
2241 case tok::kw_template:
2242 case tok::kw_typename:
2246 case tok::kw_constexpr:
2247 case tok::kw_consteval:
2250 case tok::identifier:
2251 case tok::numeric_constant:
2252 case tok::coloncolon:
2253 case tok::kw_mutable:
2254 case tok::kw_noexcept:
2255 case tok::kw_static:
2280 case tok::equalequal:
2281 case tok::exclaimequal:
2282 case tok::greaterequal:
2283 case tok::lessequal:
2289 if (SeenArrow || InTemplateParameterList) {
2302 case tok::kw_requires: {
2303 auto *RequiresToken = FormatTok;
2305 parseRequiresClause(RequiresToken);
2309 if (!InTemplateParameterList)
2319 LSquare.setFinalizedType(TT_LambdaLSquare);
2321 NestedLambdas.push_back(Line->SeenDecltypeAuto);
2323 assert(!NestedLambdas.empty());
2324 NestedLambdas.pop_back();
2329bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
2331 const FormatToken *LeftSquare = FormatTok;
2334 !
Previous->isOneOf(tok::kw_return, tok::kw_co_await,
2335 tok::kw_co_yield, tok::kw_co_return)) ||
2337 LeftSquare->isCppStructuredBinding(IsCpp)) {
2342 if (FormatTok->
is(tok::r_square)) {
2343 const FormatToken *Next = Tokens->peekNextToken(
true);
2344 if (Next->is(tok::greater))
2351void UnwrappedLineParser::tryToParseJSFunction() {
2359 if (FormatTok->
is(tok::star)) {
2365 if (FormatTok->
is(tok::identifier))
2368 if (FormatTok->
isNot(tok::l_paren))
2374 if (FormatTok->
is(tok::colon)) {
2380 if (FormatTok->
is(tok::l_brace))
2381 tryToParseBracedList();
2383 while (!FormatTok->
isOneOf(tok::l_brace, tok::semi) && !eof())
2387 if (FormatTok->
is(tok::semi))
2393bool UnwrappedLineParser::tryToParseBracedList() {
2395 calculateBraceTypes();
2404bool UnwrappedLineParser::tryToParseChildBlock() {
2406 assert(FormatTok->
is(TT_FatArrow));
2411 if (FormatTok->
isNot(tok::l_brace))
2417bool UnwrappedLineParser::parseBracedList(
bool IsAngleBracket,
bool IsEnum) {
2418 bool HasError =
false;
2423 if (Style.
isCSharp() && FormatTok->
is(TT_FatArrow) &&
2424 tryToParseChildBlock()) {
2429 tryToParseJSFunction();
2432 if (FormatTok->
is(tok::l_brace)) {
2434 if (tryToParseBracedList())
2439 if (FormatTok->
is(IsAngleBracket ? tok::greater : tok::r_brace)) {
2457 if (FormatTok->
is(tok::l_brace))
2472 parseBracedList(
true);
2506bool UnwrappedLineParser::parseParens(
TokenType AmpAmpTokenType) {
2507 assert(FormatTok->
is(tok::l_paren) &&
"'(' expected.");
2508 auto *LeftParen = FormatTok;
2509 bool SeenEqual =
false;
2510 const bool MightBeStmtExpr = Tokens->peekNextToken()->is(tok::l_brace);
2515 if (parseParens(AmpAmpTokenType))
2521 if (!MightBeStmtExpr && !Line->InMacroBody &&
2523 const auto *Prev = LeftParen->Previous;
2524 const auto *Next = Tokens->peekNextToken();
2525 const bool DoubleParens =
2526 Prev && Prev->is(tok::l_paren) && Next && Next->is(tok::r_paren);
2527 const auto *PrevPrev = Prev ? Prev->getPreviousNonComment() :
nullptr;
2528 const bool Blacklisted =
2530 (PrevPrev->isOneOf(tok::kw___attribute, tok::kw_decltype) ||
2532 (PrevPrev->isOneOf(tok::kw_if, tok::kw_while) ||
2533 PrevPrev->endsSequence(tok::kw_constexpr, tok::kw_if))));
2534 const bool ReturnParens =
2536 ((NestedLambdas.empty() && !IsDecltypeAutoFunction) ||
2537 (!NestedLambdas.empty() && !NestedLambdas.back())) &&
2538 Prev && Prev->isOneOf(tok::kw_return, tok::kw_co_return) && Next &&
2539 Next->is(tok::semi);
2540 if ((DoubleParens && !Blacklisted) || ReturnParens) {
2541 LeftParen->Optional =
true;
2554 if (!tryToParseBracedList())
2559 if (FormatTok->
is(tok::l_brace)) {
2566 if (Style.
isCSharp() && FormatTok->
is(TT_FatArrow))
2567 tryToParseChildBlock();
2577 case tok::identifier:
2579 tryToParseJSFunction();
2583 case tok::kw_requires: {
2584 auto RequiresToken = FormatTok;
2586 parseRequiresExpression(RequiresToken);
2590 if (AmpAmpTokenType != TT_Unknown)
2601void UnwrappedLineParser::parseSquare(
bool LambdaIntroducer) {
2602 if (!LambdaIntroducer) {
2603 assert(FormatTok->
is(tok::l_square) &&
"'[' expected.");
2604 if (tryToParseLambda())
2621 case tok::l_brace: {
2622 if (!tryToParseBracedList())
2628 if (FormatTok->
is(tok::l_brace)) {
2640void UnwrappedLineParser::keepAncestorBraces() {
2644 const int MaxNestingLevels = 2;
2645 const int Size = NestedTooDeep.size();
2646 if (Size >= MaxNestingLevels)
2647 NestedTooDeep[
Size - MaxNestingLevels] =
true;
2648 NestedTooDeep.push_back(
false);
2652 for (
const auto &
Token : llvm::reverse(
Line.Tokens))
2659void UnwrappedLineParser::parseUnbracedBody(
bool CheckEOF) {
2660 FormatToken *Tok =
nullptr;
2662 if (Style.
InsertBraces && !Line->InPPDirective && !Line->Tokens.empty() &&
2663 PreprocessorDirectives.empty() && FormatTok->
isNot(tok::semi)) {
2666 : Line->Tokens.back().Tok;
2668 if (Tok->BraceCount < 0) {
2669 assert(Tok->BraceCount == -1);
2672 Tok->BraceCount = -1;
2678 parseStructuralElement();
2681 assert(!Line->InPPDirective);
2683 for (
const auto &L : llvm::reverse(*CurrentLines)) {
2685 Tok = L.Tokens.back().Tok;
2693 if (CheckEOF && eof())
2703 assert(LeftBrace->
is(tok::l_brace));
2711 assert(RightBrace->
is(tok::r_brace));
2719void UnwrappedLineParser::handleAttributes() {
2723 else if (FormatTok->
is(tok::l_square))
2724 handleCppAttributes();
2727bool UnwrappedLineParser::handleCppAttributes() {
2729 assert(FormatTok->
is(tok::l_square));
2730 if (!tryToParseSimpleAttribute())
2737bool UnwrappedLineParser::isBlockBegin(
const FormatToken &Tok)
const {
2741 : Tok.is(tok::l_brace);
2744FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
2746 bool IsVerilogAssert) {
2747 assert((FormatTok->
is(tok::kw_if) ||
2754 if (IsVerilogAssert) {
2758 if (FormatTok->
is(tok::numeric_constant))
2775 if (FormatTok->
is(tok::exclaim))
2778 bool KeepIfBraces =
true;
2779 if (FormatTok->
is(tok::kw_consteval)) {
2783 if (FormatTok->
isOneOf(tok::kw_constexpr, tok::identifier))
2785 if (FormatTok->
is(tok::l_paren)) {
2792 if (IsVerilogAssert && FormatTok->
is(tok::semi)) {
2798 bool NeedsUnwrappedLine =
false;
2799 keepAncestorBraces();
2801 FormatToken *IfLeftBrace =
nullptr;
2802 IfStmtKind IfBlockKind = IfStmtKind::NotIf;
2804 if (isBlockBegin(*FormatTok)) {
2806 IfLeftBrace = FormatTok;
2808 parseBlock(
false, 1u,
2809 true, KeepIfBraces, &IfBlockKind);
2810 setPreviousRBraceType(TT_ControlStatementRBrace);
2814 NeedsUnwrappedLine =
true;
2815 }
else if (IsVerilogAssert && FormatTok->
is(tok::kw_else)) {
2818 parseUnbracedBody();
2822 assert(!NestedTooDeep.empty());
2823 KeepIfBraces = KeepIfBraces ||
2824 (IfLeftBrace && !IfLeftBrace->MatchingParen) ||
2825 NestedTooDeep.back() || IfBlockKind == IfStmtKind::IfOnly ||
2826 IfBlockKind == IfStmtKind::IfElseIf;
2829 bool KeepElseBraces = KeepIfBraces;
2830 FormatToken *ElseLeftBrace =
nullptr;
2831 IfStmtKind
Kind = IfStmtKind::IfOnly;
2833 if (FormatTok->
is(tok::kw_else)) {
2835 NestedTooDeep.back() =
false;
2836 Kind = IfStmtKind::IfElse;
2840 if (isBlockBegin(*FormatTok)) {
2841 const bool FollowedByIf = Tokens->peekNextToken()->is(tok::kw_if);
2843 ElseLeftBrace = FormatTok;
2845 IfStmtKind ElseBlockKind = IfStmtKind::NotIf;
2846 FormatToken *IfLBrace =
2847 parseBlock(
false, 1u,
2848 true, KeepElseBraces, &ElseBlockKind);
2849 setPreviousRBraceType(TT_ElseRBrace);
2850 if (FormatTok->
is(tok::kw_else)) {
2851 KeepElseBraces = KeepElseBraces ||
2852 ElseBlockKind == IfStmtKind::IfOnly ||
2853 ElseBlockKind == IfStmtKind::IfElseIf;
2854 }
else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) {
2855 KeepElseBraces =
true;
2856 assert(ElseLeftBrace->MatchingParen);
2860 }
else if (!IsVerilogAssert && FormatTok->
is(tok::kw_if)) {
2861 const FormatToken *
Previous = Tokens->getPreviousToken();
2863 const bool IsPrecededByComment =
Previous->is(tok::comment);
2864 if (IsPrecededByComment) {
2868 bool TooDeep =
true;
2870 Kind = IfStmtKind::IfElseIf;
2871 TooDeep = NestedTooDeep.pop_back_val();
2873 ElseLeftBrace = parseIfThenElse(
nullptr, KeepIfBraces);
2875 NestedTooDeep.push_back(TooDeep);
2876 if (IsPrecededByComment)
2879 parseUnbracedBody(
true);
2882 KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse;
2883 if (NeedsUnwrappedLine)
2890 assert(!NestedTooDeep.empty());
2891 KeepElseBraces = KeepElseBraces ||
2892 (ElseLeftBrace && !ElseLeftBrace->MatchingParen) ||
2893 NestedTooDeep.back();
2895 NestedTooDeep.pop_back();
2897 if (!KeepIfBraces && !KeepElseBraces) {
2900 }
else if (IfLeftBrace) {
2901 FormatToken *IfRightBrace = IfLeftBrace->MatchingParen;
2903 assert(IfRightBrace->MatchingParen == IfLeftBrace);
2904 assert(!IfLeftBrace->Optional);
2905 assert(!IfRightBrace->Optional);
2906 IfLeftBrace->MatchingParen =
nullptr;
2907 IfRightBrace->MatchingParen =
nullptr;
2917void UnwrappedLineParser::parseTryCatch() {
2918 assert(FormatTok->
isOneOf(tok::kw_try, tok::kw___try) &&
"'try' expected");
2920 bool NeedsUnwrappedLine =
false;
2921 if (FormatTok->
is(tok::colon)) {
2927 while (FormatTok->
is(tok::comma))
2930 while (FormatTok->
is(tok::identifier)) {
2932 if (FormatTok->
is(tok::l_paren))
2935 FormatTok->
is(tok::l_brace)) {
2938 }
while (FormatTok->
isNot(tok::r_brace));
2944 while (FormatTok->
is(tok::comma))
2952 keepAncestorBraces();
2954 if (FormatTok->
is(tok::l_brace)) {
2960 NeedsUnwrappedLine =
true;
2961 }
else if (FormatTok->
isNot(tok::kw_catch)) {
2967 parseStructuralElement();
2971 if (FormatTok->
is(tok::at))
2974 tok::kw___finally) ||
2982 while (FormatTok->
isNot(tok::l_brace)) {
2983 if (FormatTok->
is(tok::l_paren)) {
2987 if (FormatTok->
isOneOf(tok::semi, tok::r_brace, tok::eof)) {
2989 NestedTooDeep.pop_back();
2994 NeedsUnwrappedLine =
false;
2995 Line->MustBeDeclaration =
false;
3001 NeedsUnwrappedLine =
true;
3005 NestedTooDeep.pop_back();
3007 if (NeedsUnwrappedLine)
3011void UnwrappedLineParser::parseNamespace() {
3012 assert(FormatTok->
isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
3013 "'namespace' expected");
3015 const FormatToken &InitialToken = *FormatTok;
3017 if (InitialToken.is(TT_NamespaceMacro)) {
3020 while (FormatTok->
isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
3021 tok::l_square, tok::period, tok::l_paren) ||
3022 (Style.
isCSharp() && FormatTok->
is(tok::kw_union))) {
3023 if (FormatTok->
is(tok::l_square))
3025 else if (FormatTok->
is(tok::l_paren))
3031 if (FormatTok->
is(tok::l_brace)) {
3037 unsigned AddLevels =
3040 DeclarationScopeStack.size() > 1)
3043 bool ManageWhitesmithsBraces =
3049 if (ManageWhitesmithsBraces)
3054 parseBlock(
true, AddLevels,
true,
3056 ManageWhitesmithsBraces);
3058 addUnwrappedLine(AddLevels > 0 ? LineLevel::Remove : LineLevel::Keep);
3060 if (ManageWhitesmithsBraces)
3066void UnwrappedLineParser::parseNew() {
3067 assert(FormatTok->
is(tok::kw_new) &&
"'new' expected");
3073 if (FormatTok->
is(tok::l_paren))
3077 if (FormatTok->
is(tok::l_brace))
3080 if (FormatTok->
isOneOf(tok::semi, tok::comma))
3093 if (FormatTok->
isOneOf(tok::semi, tok::l_brace, tok::r_brace))
3097 if (FormatTok->
is(tok::l_paren)) {
3101 if (FormatTok->
is(tok::l_brace))
3109void UnwrappedLineParser::parseLoopBody(
bool KeepBraces,
bool WrapRightBrace) {
3110 keepAncestorBraces();
3112 if (isBlockBegin(*FormatTok)) {
3114 FormatToken *LeftBrace = FormatTok;
3116 parseBlock(
false, 1u,
3118 setPreviousRBraceType(TT_ControlStatementRBrace);
3120 assert(!NestedTooDeep.empty());
3121 if (!NestedTooDeep.back())
3127 parseUnbracedBody();
3131 NestedTooDeep.pop_back();
3134void UnwrappedLineParser::parseForOrWhileLoop(
bool HasParens) {
3135 assert((FormatTok->
isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) ||
3142 "'for', 'while' or foreach macro expected");
3144 !FormatTok->
isOneOf(tok::kw_for, tok::kw_while);
3150 if (IsCpp && FormatTok->
is(tok::kw_co_await))
3152 if (HasParens && FormatTok->
is(tok::l_paren)) {
3163 parseVerilogSensitivityList();
3165 Tokens->getPreviousToken()->is(tok::r_paren)) {
3172 parseLoopBody(KeepBraces,
true);
3175void UnwrappedLineParser::parseDoWhile() {
3176 assert(FormatTok->
is(tok::kw_do) &&
"'do' expected");
3182 if (FormatTok->
isNot(tok::kw_while)) {
3195 parseStructuralElement();
3198void UnwrappedLineParser::parseLabel(
bool LeftAlignLabel) {
3200 unsigned OldLineLevel = Line->Level;
3201 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
3207 FormatTok->
is(tok::l_brace)) {
3213 if (FormatTok->
is(tok::kw_break)) {
3222 parseStructuralElement();
3226 if (FormatTok->
is(tok::semi))
3230 Line->Level = OldLineLevel;
3231 if (FormatTok->
isNot(tok::l_brace)) {
3232 parseStructuralElement();
3237void UnwrappedLineParser::parseCaseLabel() {
3238 assert(FormatTok->
is(tok::kw_case) &&
"'case' expected");
3243 if (FormatTok->
is(tok::colon)) {
3251void UnwrappedLineParser::parseSwitch() {
3252 assert(FormatTok->
is(tok::kw_switch) &&
"'switch' expected");
3254 if (FormatTok->
is(tok::l_paren))
3257 keepAncestorBraces();
3259 if (FormatTok->
is(tok::l_brace)) {
3263 setPreviousRBraceType(TT_ControlStatementRBrace);
3268 parseStructuralElement();
3273 NestedTooDeep.pop_back();
3283 case tok::caretequal:
3287 case tok::equalequal:
3289 case tok::exclaimequal:
3291 case tok::greaterequal:
3292 case tok::greatergreater:
3293 case tok::greatergreaterequal:
3297 case tok::lessequal:
3299 case tok::lesslessequal:
3301 case tok::minusequal:
3302 case tok::minusminus:
3304 case tok::percentequal:
3307 case tok::pipeequal:
3310 case tok::plusequal:
3318 case tok::slashequal:
3320 case tok::starequal:
3327void UnwrappedLineParser::parseAccessSpecifier() {
3328 FormatToken *AccessSpecifierCandidate = FormatTok;
3334 if (FormatTok->
is(tok::colon)) {
3337 }
else if (FormatTok->
isNot(tok::coloncolon) &&
3341 }
else if (AccessSpecifierCandidate) {
3343 AccessSpecifierCandidate->Tok.setKind(tok::identifier);
3350bool clang::format::UnwrappedLineParser::parseRequires() {
3351 assert(FormatTok->is(tok::kw_requires) &&
"'requires' expected");
3352 auto RequiresToken = FormatTok;
3358 switch (FormatTok->Tok.getKind()) {
3361 parseRequiresExpression(RequiresToken);
3368 parseRequiresClause(RequiresToken);
3379 auto *PreviousNonComment = RequiresToken->getPreviousNonComment();
3381 if (!PreviousNonComment ||
3382 PreviousNonComment->is(TT_RequiresExpressionLBrace)) {
3385 parseRequiresClause(RequiresToken);
3389 switch (PreviousNonComment->Tok.getKind()) {
3392 case tok::kw_noexcept:
3395 parseRequiresClause(RequiresToken);
3405 auto PrevPrev = PreviousNonComment->getPreviousNonComment();
3406 if (PrevPrev && PrevPrev->is(tok::kw_const)) {
3407 parseRequiresClause(RequiresToken);
3413 if (PreviousNonComment->isTypeOrIdentifier(IsCpp)) {
3415 parseRequiresClause(RequiresToken);
3419 parseRequiresExpression(RequiresToken);
3432 auto PeekNext = [&Lookahead, &NextToken,
this] {
3437 bool FoundType =
false;
3438 bool LastWasColonColon =
false;
3441 for (; Lookahead < 50; PeekNext()) {
3442 switch (NextToken->Tok.getKind()) {
3443 case tok::kw_volatile:
3446 if (OpenAngles == 0) {
3448 parseRequiresExpression(RequiresToken);
3456 case tok::coloncolon:
3457 LastWasColonColon =
true;
3459 case tok::kw_decltype:
3460 case tok::identifier:
3461 if (FoundType && !LastWasColonColon && OpenAngles == 0) {
3463 parseRequiresExpression(RequiresToken);
3467 LastWasColonColon =
false;
3476 if (NextToken->isTypeName(IsCpp)) {
3478 parseRequiresExpression(RequiresToken);
3486 parseRequiresClause(RequiresToken);
3497void UnwrappedLineParser::parseRequiresClause(FormatToken *RequiresToken) {
3499 assert(RequiresToken->is(tok::kw_requires) &&
"'requires' expected");
3504 bool InRequiresExpression =
3505 !RequiresToken->Previous ||
3506 RequiresToken->Previous->is(TT_RequiresExpressionLBrace);
3508 RequiresToken->setFinalizedType(InRequiresExpression
3509 ? TT_RequiresClauseInARequiresExpression
3510 : TT_RequiresClause);
3514 parseConstraintExpression();
3516 if (!InRequiresExpression)
3527void UnwrappedLineParser::parseRequiresExpression(FormatToken *RequiresToken) {
3529 assert(RequiresToken->is(tok::kw_requires) &&
"'requires' expected");
3531 RequiresToken->setFinalizedType(TT_RequiresExpression);
3533 if (FormatTok->
is(tok::l_paren)) {
3538 if (FormatTok->
is(tok::l_brace)) {
3548void UnwrappedLineParser::parseConstraintExpression() {
3555 bool LambdaNextTimeAllowed =
true;
3565 bool TopLevelParensAllowed =
true;
3568 bool LambdaThisTimeAllowed = std::exchange(LambdaNextTimeAllowed,
false);
3571 case tok::kw_requires: {
3572 auto RequiresToken = FormatTok;
3574 parseRequiresExpression(RequiresToken);
3579 if (!TopLevelParensAllowed)
3581 parseParens(TT_BinaryOperator);
3582 TopLevelParensAllowed =
false;
3586 if (!LambdaThisTimeAllowed || !tryToParseLambda())
3593 case tok::kw_struct:
3605 LambdaNextTimeAllowed =
true;
3606 TopLevelParensAllowed =
true;
3611 LambdaNextTimeAllowed = LambdaThisTimeAllowed;
3615 case tok::kw_sizeof:
3617 case tok::greaterequal:
3618 case tok::greatergreater:
3620 case tok::lessequal:
3622 case tok::equalequal:
3624 case tok::exclaimequal:
3629 LambdaNextTimeAllowed =
true;
3630 TopLevelParensAllowed =
true;
3635 case tok::numeric_constant:
3636 case tok::coloncolon:
3639 TopLevelParensAllowed =
false;
3644 case tok::kw_static_cast:
3645 case tok::kw_const_cast:
3646 case tok::kw_reinterpret_cast:
3647 case tok::kw_dynamic_cast:
3649 if (FormatTok->
isNot(tok::less))
3653 parseBracedList(
true);
3669 case tok::coloncolon:
3673 case tok::kw_requires:
3682 if (FormatTok->
is(tok::less)) {
3684 parseBracedList(
true);
3686 TopLevelParensAllowed =
false;
3692bool UnwrappedLineParser::parseEnum() {
3693 const FormatToken &InitialToken = *FormatTok;
3696 if (FormatTok->
is(tok::kw_enum))
3711 if (FormatTok->
isOneOf(tok::kw_class, tok::kw_struct))
3713 while (FormatTok->
is(tok::l_square))
3714 if (!handleCppAttributes())
3719 FormatTok->
isOneOf(tok::colon, tok::coloncolon, tok::less,
3720 tok::greater, tok::comma, tok::question,
3726 while (FormatTok->
is(tok::l_square))
3732 if (FormatTok->
is(tok::l_paren))
3734 if (FormatTok->
is(tok::identifier)) {
3738 if (IsCpp && FormatTok->
is(tok::identifier))
3744 if (FormatTok->
isNot(tok::l_brace))
3751 parseJavaEnumBody();
3769 bool HasError = !parseBracedList(
false,
true);
3773 if (FormatTok->
is(tok::semi))
3777 setPreviousRBraceType(TT_EnumRBrace);
3785bool UnwrappedLineParser::parseStructLike() {
3792 if (FormatTok->
is(tok::semi))
3803class ScopedTokenPosition {
3804 unsigned StoredPosition;
3805 FormatTokenSource *Tokens;
3808 ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) {
3809 assert(Tokens &&
"Tokens expected to not be null");
3810 StoredPosition = Tokens->getPosition();
3813 ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); }
3819bool UnwrappedLineParser::tryToParseSimpleAttribute() {
3820 ScopedTokenPosition AutoPosition(Tokens);
3821 FormatToken *Tok = Tokens->getNextToken();
3823 if (Tok->isNot(tok::l_square))
3827 while (Tok->isNot(tok::eof)) {
3828 if (Tok->is(tok::r_square))
3830 Tok = Tokens->getNextToken();
3832 if (Tok->is(tok::eof))
3834 Tok = Tokens->getNextToken();
3835 if (Tok->isNot(tok::r_square))
3837 Tok = Tokens->getNextToken();
3838 if (Tok->is(tok::semi))
3843void UnwrappedLineParser::parseJavaEnumBody() {
3844 assert(FormatTok->
is(tok::l_brace));
3845 const FormatToken *OpeningBrace = FormatTok;
3850 unsigned StoredPosition = Tokens->getPosition();
3851 bool IsSimple =
true;
3852 FormatToken *Tok = Tokens->getNextToken();
3853 while (Tok->isNot(tok::eof)) {
3854 if (Tok->is(tok::r_brace))
3856 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
3862 Tok = Tokens->getNextToken();
3864 FormatTok = Tokens->setPosition(StoredPosition);
3881 if (FormatTok->
is(tok::l_brace)) {
3883 parseBlock(
true, 1u,
3885 }
else if (FormatTok->
is(tok::l_paren)) {
3887 }
else if (FormatTok->
is(tok::comma)) {
3890 }
else if (FormatTok->
is(tok::semi)) {
3894 }
else if (FormatTok->
is(tok::r_brace)) {
3903 parseLevel(OpeningBrace);
3909void UnwrappedLineParser::parseRecord(
bool ParseAsExpr) {
3910 const FormatToken &InitialToken = *FormatTok;
3913 auto IsNonMacroIdentifier = [](
const FormatToken *Tok) {
3914 return Tok->is(tok::identifier) && Tok->TokenText != Tok->TokenText.upper();
3919 while (FormatTok->
isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
3920 tok::kw_alignas, tok::l_square) ||
3923 FormatTok->
isOneOf(tok::period, tok::comma))) {
3930 if (FormatTok->
is(tok::l_brace)) {
3931 tryToParseBracedList();
3935 if (FormatTok->
is(tok::l_square) && handleCppAttributes())
3939 if (!IsNonMacroIdentifier(FormatTok->
Previous) &&
3940 FormatTok->
is(tok::l_paren)) {
3945 if (FormatTok->
isOneOf(tok::colon, tok::less)) {
3946 int AngleNestingLevel = 0;
3948 if (FormatTok->
is(tok::less))
3949 ++AngleNestingLevel;
3950 else if (FormatTok->
is(tok::greater))
3951 --AngleNestingLevel;
3953 if (AngleNestingLevel == 0 && FormatTok->
is(tok::l_paren) &&
3954 IsNonMacroIdentifier(FormatTok->
Previous)) {
3957 if (FormatTok->
is(tok::l_brace)) {
3958 calculateBraceTypes(
true);
3959 if (!tryToParseBracedList())
3962 if (FormatTok->
is(tok::l_square)) {
3965 !
Previous->isTypeOrIdentifier(IsCpp))) {
3968 if (!tryToParseLambda())
3975 if (FormatTok->
is(tok::semi))
3980 parseCSharpGenericTypeConstraint();
3987 auto GetBraceTypes =
3988 [](
const FormatToken &RecordTok) -> std::pair<TokenType, TokenType> {
3989 switch (RecordTok.Tok.getKind()) {
3991 return {TT_ClassLBrace, TT_ClassRBrace};
3992 case tok::kw_struct:
3993 return {TT_StructLBrace, TT_StructRBrace};
3995 return {TT_UnionLBrace, TT_UnionRBrace};
3998 return {TT_RecordLBrace, TT_RecordRBrace};
4001 if (FormatTok->
is(tok::l_brace)) {
4002 auto [OpenBraceType, ClosingBraceType] = GetBraceTypes(InitialToken);
4011 parseBlock(
true, AddLevels,
false);
4013 setPreviousRBraceType(ClosingBraceType);
4020void UnwrappedLineParser::parseObjCMethod() {
4021 assert(FormatTok->
isOneOf(tok::l_paren, tok::identifier) &&
4022 "'(' or identifier expected.");
4024 if (FormatTok->
is(tok::semi)) {
4028 }
else if (FormatTok->
is(tok::l_brace)) {
4040void UnwrappedLineParser::parseObjCProtocolList() {
4041 assert(FormatTok->
is(tok::less) &&
"'<' expected.");
4045 if (FormatTok->
isOneOf(tok::semi, tok::l_brace) ||
4049 }
while (!eof() && FormatTok->
isNot(tok::greater));
4053void UnwrappedLineParser::parseObjCUntilAtEnd() {
4060 if (FormatTok->
is(tok::l_brace)) {
4064 }
else if (FormatTok->
is(tok::r_brace)) {
4068 }
else if (FormatTok->
isOneOf(tok::minus, tok::plus)) {
4072 parseStructuralElement();
4077void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
4085 if (FormatTok->
is(tok::less))
4086 parseObjCLightweightGenerics();
4087 if (FormatTok->
is(tok::colon)) {
4091 if (FormatTok->
is(tok::less))
4092 parseObjCLightweightGenerics();
4093 }
else if (FormatTok->
is(tok::l_paren)) {
4098 if (FormatTok->
is(tok::less))
4099 parseObjCProtocolList();
4101 if (FormatTok->
is(tok::l_brace)) {
4111 parseObjCUntilAtEnd();
4114void UnwrappedLineParser::parseObjCLightweightGenerics() {
4115 assert(FormatTok->
is(tok::less));
4123 unsigned NumOpenAngles = 1;
4127 if (FormatTok->
isOneOf(tok::semi, tok::l_brace) ||
4131 if (FormatTok->
is(tok::less)) {
4133 }
else if (FormatTok->
is(tok::greater)) {
4134 assert(NumOpenAngles > 0 &&
"'>' makes NumOpenAngles negative");
4137 }
while (!eof() && NumOpenAngles != 0);
4143bool UnwrappedLineParser::parseObjCProtocol() {
4147 if (FormatTok->
is(tok::l_paren)) {
4159 if (FormatTok->
is(tok::less))
4160 parseObjCProtocolList();
4163 if (FormatTok->
is(tok::semi)) {
4170 parseObjCUntilAtEnd();
4174void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
4175 bool IsImport = FormatTok->
is(Keywords.
kw_import);
4176 assert(IsImport || FormatTok->
is(tok::kw_export));
4180 if (FormatTok->
is(tok::kw_default))
4197 if (!IsImport && !FormatTok->
isOneOf(tok::l_brace, tok::star) &&
4200 Tokens->peekNextToken()->isOneOf(tok::l_brace, tok::star))) {
4205 if (FormatTok->
is(tok::semi))
4207 if (Line->Tokens.empty()) {
4212 if (FormatTok->
is(tok::l_brace)) {
4222void UnwrappedLineParser::parseStatementMacro() {
4224 if (FormatTok->
is(tok::l_paren))
4226 if (FormatTok->
is(tok::semi))
4231void UnwrappedLineParser::parseVerilogHierarchyIdentifier() {
4234 if (FormatTok->
isOneOf(tok::star, tok::period, tok::periodstar,
4235 tok::coloncolon, tok::hash) ||
4238 }
else if (FormatTok->
is(tok::l_square)) {
4246void UnwrappedLineParser::parseVerilogSensitivityList() {
4247 if (FormatTok->
isNot(tok::at))
4251 if (FormatTok->
is(tok::at))
4261 parseVerilogHierarchyIdentifier();
4266unsigned UnwrappedLineParser::parseVerilogHierarchyHeader() {
4267 unsigned AddLevels = 0;
4273 parseVerilogSensitivityList();
4274 if (FormatTok->
is(tok::semi))
4282 if (FormatTok->
is(tok::l_paren)) {
4295 if (FormatTok->
is(tok::l_square)) {
4310 Line->IsContinuation =
true;
4317 parseVerilogHierarchyIdentifier();
4318 if (FormatTok->
is(tok::semi))
4326 if (FormatTok->
is(tok::l_paren)) {
4331 if (FormatTok->
is(tok::l_paren)) {
4341 parseVerilogHierarchyIdentifier();
4342 if (FormatTok->
is(tok::l_paren))
4349 parseVerilogHierarchyIdentifier();
4350 }
while (FormatTok->
is(tok::comma));
4354 if (FormatTok->
is(tok::at)) {
4356 parseVerilogSensitivityList();
4359 if (FormatTok->
is(tok::semi))
4367void UnwrappedLineParser::parseVerilogTable() {
4372 auto InitialLevel = Line->Level++;
4374 FormatToken *Tok = FormatTok;
4376 if (Tok->is(tok::semi))
4378 else if (Tok->isOneOf(tok::star, tok::colon, tok::question, tok::minus))
4379 Tok->setFinalizedType(TT_VerilogTableItem);
4381 Line->Level = InitialLevel;
4386void UnwrappedLineParser::parseVerilogCaseLabel() {
4392 auto OrigLevel = Line->Level;
4393 auto FirstLine = CurrentLines->size();
4394 if (Line->Level == 0 || (Line->InPPDirective && Line->Level <= 1))
4398 parseStructuralElement();
4401 if (CurrentLines->size() > FirstLine)
4402 (*CurrentLines)[FirstLine].Level = OrigLevel;
4403 Line->Level = OrigLevel;
4406bool UnwrappedLineParser::containsExpansion(
const UnwrappedLine &
Line)
const {
4407 for (
const auto &N : Line.Tokens) {
4408 if (N.Tok->MacroCtx)
4410 for (
const UnwrappedLine &Child : N.Children)
4411 if (containsExpansion(Child))
4417void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) {
4418 if (Line->Tokens.empty())
4421 if (!parsingPPDirective()) {
4422 llvm::dbgs() <<
"Adding unwrapped line:\n";
4423 printDebugInfo(*Line);
4431 bool ClosesWhitesmithsBlock =
4438 if (!parsingPPDirective() && !InExpansion && containsExpansion(*Line)) {
4440 Reconstruct.emplace(Line->Level, Unexpanded);
4441 Reconstruct->addLine(*Line);
4446 CurrentExpandedLines.push_back(std::move(*Line));
4448 if (Reconstruct->finished()) {
4449 UnwrappedLine Reconstructed = std::move(*Reconstruct).takeResult();
4450 assert(!Reconstructed.Tokens.empty() &&
4451 "Reconstructed must at least contain the macro identifier.");
4452 assert(!parsingPPDirective());
4454 llvm::dbgs() <<
"Adding unexpanded line:\n";
4455 printDebugInfo(Reconstructed);
4457 ExpandedLines[Reconstructed.Tokens.begin()->Tok] = CurrentExpandedLines;
4458 Lines.push_back(std::move(Reconstructed));
4459 CurrentExpandedLines.clear();
4460 Reconstruct.reset();
4465 assert(!Reconstruct || (CurrentLines != &Lines) || PPStack.size() > 0);
4466 CurrentLines->push_back(std::move(*Line));
4468 Line->Tokens.clear();
4470 Line->FirstStartColumn = 0;
4471 Line->IsContinuation =
false;
4472 Line->SeenDecltypeAuto =
false;
4474 if (ClosesWhitesmithsBlock && AdjustLevel == LineLevel::Remove)
4476 if (!parsingPPDirective() && !PreprocessorDirectives.empty()) {
4477 CurrentLines->append(
4478 std::make_move_iterator(PreprocessorDirectives.begin()),
4479 std::make_move_iterator(PreprocessorDirectives.end()));
4480 PreprocessorDirectives.clear();
4486bool UnwrappedLineParser::eof()
const {
return FormatTok->
is(tok::eof); }
4488bool UnwrappedLineParser::isOnNewLine(
const FormatToken &FormatTok) {
4489 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
4490 FormatTok.NewlinesBefore > 0;
4498 const llvm::Regex &CommentPragmasRegex) {
4499 if (
Line.Tokens.empty())
4502 StringRef IndentContent = FormatTok.
TokenText;
4503 if (FormatTok.
TokenText.starts_with(
"//") ||
4504 FormatTok.
TokenText.starts_with(
"/*")) {
4505 IndentContent = FormatTok.
TokenText.substr(2);
4507 if (CommentPragmasRegex.match(IndentContent))
4582 if (PreviousToken && PreviousToken->
is(tok::l_brace) &&
4584 MinColumnToken = PreviousToken;
4587 PreviousToken =
Node.Tok;
4590 if (
Node.Tok->NewlinesBefore > 0)
4591 MinColumnToken =
Node.Tok;
4593 if (PreviousToken && PreviousToken->
is(tok::l_brace))
4594 MinColumnToken = PreviousToken;
4600void UnwrappedLineParser::flushComments(
bool NewlineBeforeNext) {
4601 bool JustComments = Line->Tokens.empty();
4602 for (FormatToken *Tok : CommentsBeforeNextToken) {
4611 Tok->ContinuesLineCommentSection =
4613 if (isOnNewLine(*Tok) && JustComments && !Tok->ContinuesLineCommentSection)
4617 if (NewlineBeforeNext && JustComments)
4619 CommentsBeforeNextToken.clear();
4622void UnwrappedLineParser::nextToken(
int LevelDifference) {
4625 flushComments(isOnNewLine(*FormatTok));
4626 pushToken(FormatTok);
4629 readToken(LevelDifference);
4631 readTokenWithJavaScriptASI();
4641 FormatTok->Tok.setKind(tok::r_brace);
4645void UnwrappedLineParser::distributeComments(
4646 const SmallVectorImpl<FormatToken *> &Comments,
4647 const FormatToken *NextTok) {
4666 if (Comments.empty())
4668 bool ShouldPushCommentsInCurrentLine =
true;
4669 bool HasTrailAlignedWithNextToken =
false;
4670 unsigned StartOfTrailAlignedWithNextToken = 0;
4673 for (
unsigned i = Comments.size() - 1; i > 0; --i) {
4674 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
4675 HasTrailAlignedWithNextToken =
true;
4676 StartOfTrailAlignedWithNextToken = i;
4680 for (
unsigned i = 0, e = Comments.size(); i < e; ++i) {
4681 FormatToken *FormatTok = Comments[i];
4682 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
4683 FormatTok->ContinuesLineCommentSection =
false;
4685 FormatTok->ContinuesLineCommentSection =
4688 if (!FormatTok->ContinuesLineCommentSection &&
4689 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
4690 ShouldPushCommentsInCurrentLine =
false;
4692 if (ShouldPushCommentsInCurrentLine)
4693 pushToken(FormatTok);
4695 CommentsBeforeNextToken.push_back(FormatTok);
4699void UnwrappedLineParser::readToken(
int LevelDifference) {
4700 SmallVector<FormatToken *, 1> Comments;
4701 bool PreviousWasComment =
false;
4702 bool FirstNonCommentOnLine =
false;
4704 FormatTok = Tokens->getNextToken();
4706 while (FormatTok->isOneOf(TT_ConflictStart, TT_ConflictEnd,
4707 TT_ConflictAlternative)) {
4708 if (FormatTok->is(TT_ConflictStart))
4709 conditionalCompilationStart(
false);
4710 else if (FormatTok->is(TT_ConflictAlternative))
4711 conditionalCompilationAlternative();
4712 else if (FormatTok->is(TT_ConflictEnd))
4713 conditionalCompilationEnd();
4714 FormatTok = Tokens->getNextToken();
4715 FormatTok->MustBreakBefore =
true;
4716 FormatTok->MustBreakBeforeFinalized =
true;
4719 auto IsFirstNonCommentOnLine = [](
bool FirstNonCommentOnLine,
4720 const FormatToken &Tok,
4721 bool PreviousWasComment) {
4722 auto IsFirstOnLine = [](
const FormatToken &Tok) {
4723 return Tok.HasUnescapedNewline || Tok.IsFirst;
4728 if (PreviousWasComment)
4729 return FirstNonCommentOnLine || IsFirstOnLine(Tok);
4730 return IsFirstOnLine(Tok);
4733 FirstNonCommentOnLine = IsFirstNonCommentOnLine(
4734 FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
4735 PreviousWasComment = FormatTok->is(tok::comment);
4737 while (!Line->InPPDirective && FormatTok->is(tok::hash) &&
4740 FirstNonCommentOnLine) {
4741 distributeComments(Comments, FormatTok);
4745 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
4747 assert((LevelDifference >= 0 ||
4748 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
4749 "LevelDifference makes Line->Level negative");
4750 Line->Level += LevelDifference;
4755 PPBranchLevel > 0) {
4756 Line->Level += PPBranchLevel;
4758 flushComments(isOnNewLine(*FormatTok));
4760 PreviousWasComment = FormatTok->is(tok::comment);
4761 FirstNonCommentOnLine = IsFirstNonCommentOnLine(
4762 FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
4765 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
4766 !Line->InPPDirective) {
4770 if (FormatTok->is(tok::identifier) &&
4771 Macros.
defined(FormatTok->TokenText) &&
4773 !Line->InPPDirective) {
4774 FormatToken *
ID = FormatTok;
4775 unsigned Position = Tokens->getPosition();
4779 auto PreCall = std::move(Line);
4780 Line.reset(
new UnwrappedLine);
4781 bool OldInExpansion = InExpansion;
4784 auto Args = parseMacroCall();
4785 InExpansion = OldInExpansion;
4786 assert(Line->Tokens.front().Tok ==
ID);
4788 auto UnexpandedLine = std::move(Line);
4790 Line = std::move(PreCall);
4793 llvm::dbgs() <<
"Macro call: " <<
ID->TokenText <<
"(";
4795 llvm::dbgs() <<
"(";
4796 for (
const auto &Arg : Args.value())
4797 for (
const auto &
T : Arg)
4798 llvm::dbgs() <<
T->TokenText <<
" ";
4799 llvm::dbgs() <<
")";
4801 llvm::dbgs() <<
"\n";
4804 !Macros.
hasArity(
ID->TokenText, Args->size())) {
4810 LLVM_DEBUG(llvm::dbgs()
4811 <<
"Macro \"" <<
ID->TokenText
4812 <<
"\" not overloaded for arity " << Args->size()
4813 <<
"or not function-like, using object-like overload.");
4815 UnexpandedLine->Tokens.resize(1);
4816 Tokens->setPosition(Position);
4821 (Args && Macros.
hasArity(
ID->TokenText, Args->size()))) {
4824 Unexpanded[
ID] = std::move(UnexpandedLine);
4825 SmallVector<FormatToken *, 8> Expansion =
4826 Macros.
expand(
ID, std::move(Args));
4827 if (!Expansion.empty())
4828 FormatTok = Tokens->insertTokens(Expansion);
4831 llvm::dbgs() <<
"Expanded: ";
4832 for (
const auto &
T : Expansion)
4833 llvm::dbgs() <<
T->TokenText <<
" ";
4834 llvm::dbgs() <<
"\n";
4838 llvm::dbgs() <<
"Did not expand macro \"" <<
ID->TokenText
4839 <<
"\", because it was used ";
4841 llvm::dbgs() <<
"with " << Args->size();
4843 llvm::dbgs() <<
"without";
4844 llvm::dbgs() <<
" arguments, which doesn't match any definition.\n";
4846 Tokens->setPosition(Position);
4851 if (FormatTok->isNot(tok::comment)) {
4852 distributeComments(Comments, FormatTok);
4857 Comments.push_back(FormatTok);
4860 distributeComments(Comments,
nullptr);
4865template <
typename Iterator>
4866void pushTokens(Iterator
Begin, Iterator End,
4868 for (
auto I =
Begin; I != End; ++I) {
4869 Into.push_back(I->Tok);
4870 for (
const auto &Child : I->Children)
4871 pushTokens(Child.Tokens.begin(), Child.Tokens.end(), Into);
4876std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>>
4877UnwrappedLineParser::parseMacroCall() {
4878 std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>> Args;
4879 assert(Line->Tokens.empty());
4881 if (FormatTok->isNot(tok::l_paren))
4883 unsigned Position = Tokens->getPosition();
4884 FormatToken *Tok = FormatTok;
4887 auto ArgStart = std::prev(Line->Tokens.end());
4891 switch (FormatTok->Tok.getKind()) {
4896 case tok::r_paren: {
4902 Args->push_back({});
4903 pushTokens(std::next(ArgStart), Line->Tokens.end(), Args->back());
4912 Args->push_back({});
4913 pushTokens(std::next(ArgStart), Line->Tokens.end(), Args->back());
4915 ArgStart = std::prev(Line->Tokens.end());
4923 Line->Tokens.resize(1);
4924 Tokens->setPosition(Position);
4929void UnwrappedLineParser::pushToken(FormatToken *Tok) {
4930 Line->Tokens.push_back(UnwrappedLineNode(Tok));
4931 if (MustBreakBeforeNextToken) {
4932 Line->Tokens.back().Tok->MustBreakBefore =
true;
4933 Line->Tokens.back().Tok->MustBreakBeforeFinalized =
true;
4934 MustBreakBeforeNextToken =
false;
This file contains the main building blocks of macro support in clang-format.
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
This file contains the declaration of the UnwrappedLineParser, which turns a stream of tokens into Un...
tok::PPKeywordKind getPPKeywordID() const
Return the preprocessor keyword ID for this identifier.
Implements an efficient mapping from strings to IdentifierInfo nodes.
Parser - This implements a parser for the C family of languages.
This class handles loading and caching of source files into memory.
Token - This structure provides full information about a lexed token.
IdentifierInfo * getIdentifierInfo() const
bool isAnyIdentifier() const
Return true if this is a raw identifier (when lexing in raw mode) or a non-keyword identifier (when l...
bool isLiteral() const
Return true if this is a "literal", like a numeric constant, string, etc.
void setKind(tok::TokenKind K)
tok::ObjCKeywordKind getObjCKeywordID() const
Return the ObjC keyword kind.
tok::TokenKind getKind() const
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const
bool isNot(tok::TokenKind K) const
void setIdentifierInfo(IdentifierInfo *II)
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
bool isLiteral(TokenKind K)
Return true if this is a "literal" kind, like a numeric constant, string, etc.
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
@ Parens
New-expression has a C++98 paren-delimited initializer.