23#include "llvm/ADT/StringSet.h"
24#include "llvm/Support/Debug.h"
27#define DEBUG_TYPE "format-indenter"
44 Previous->isOneOf(tok::kw_return, TT_RequiresClause));
93 int MatchingStackIndex = Stack.size() - 1;
99 while (MatchingStackIndex >= 0 && Stack[MatchingStackIndex].Tok != LBrace)
100 --MatchingStackIndex;
101 return MatchingStackIndex >= 0 ? &Stack[MatchingStackIndex] :
nullptr;
103 for (; End->Next; End = End->Next) {
104 if (End->Next->CanBreakBefore)
106 if (!End->Next->closesScope())
108 if (End->Next->MatchingParen &&
109 End->Next->MatchingParen->isOneOf(
110 tok::l_brace, TT_ArrayInitializerLSquare, tok::less)) {
111 const ParenState *State = FindParenState(End->Next->MatchingParen);
112 if (State && State->BreakBeforeClosingBrace)
136 if (Current.
is(TT_CtorInitializerComma) &&
143 ((
Previous.isNot(TT_CtorInitializerComma) ||
146 (
Previous.isNot(TT_InheritanceComma) ||
153 bool HasTwoOperands =
Token.OperatorIndex == 0 && !
Token.NextOperator;
154 return Token.
is(TT_BinaryOperator) && !HasTwoOperands &&
177 if (LessTok.
isNot(tok::less))
189 if (TokenText.size() < 5
190 || !TokenText.starts_with(
"R\"") || !TokenText.ends_with(
"\"")) {
197 size_t LParenPos = TokenText.substr(0, 19).find_first_of(
'(');
198 if (LParenPos == StringRef::npos)
200 StringRef Delimiter = TokenText.substr(2, LParenPos - 2);
203 size_t RParenPos = TokenText.size() - Delimiter.size() - 2;
204 if (TokenText[RParenPos] !=
')')
206 if (!TokenText.substr(RParenPos + 1).starts_with(Delimiter))
218 return StringRef(Format.CanonicalDelimiter);
225 std::optional<FormatStyle> LanguageStyle =
227 if (!LanguageStyle) {
230 RawStringFormat.Language, &PredefinedStyle)) {
232 PredefinedStyle.
Language = RawStringFormat.Language;
234 LanguageStyle = PredefinedStyle;
237 for (StringRef Delimiter : RawStringFormat.Delimiters)
239 for (StringRef EnclosingFunction : RawStringFormat.EnclosingFunctions)
244std::optional<FormatStyle>
252std::optional<FormatStyle>
254 StringRef EnclosingFunction)
const {
266 bool BinPackInconclusiveFunctions)
267 : Style(Style), Keywords(Keywords), SourceMgr(SourceMgr),
268 Whitespaces(Whitespaces), Encoding(Encoding),
269 BinPackInconclusiveFunctions(BinPackInconclusiveFunctions),
270 CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {}
273 unsigned FirstStartColumn,
277 State.FirstIndent = FirstIndent;
278 if (FirstStartColumn &&
Line->First->NewlinesBefore == 0)
279 State.Column = FirstStartColumn;
281 State.Column = FirstIndent;
291 State.NextToken =
Line->First;
292 State.Stack.push_back(
ParenState(
nullptr, FirstIndent, FirstIndent,
295 State.NoContinuation =
false;
296 State.StartOfStringLiteral = 0;
297 State.NoLineBreak =
false;
298 State.StartOfLineLevel = 0;
299 State.LowestLevelOnLine = 0;
300 State.IgnoreStackForComparison =
false;
305 auto &CurrentState = State.Stack.back();
306 CurrentState.AvoidBinPacking =
true;
307 CurrentState.BreakBeforeParameter =
true;
308 CurrentState.AlignColons =
false;
312 moveStateToNextToken(State, DryRun,
false);
319 const auto &CurrentState = State.Stack.back();
320 assert(&
Previous == Current.Previous);
321 if (!Current.CanBreakBefore && !(CurrentState.BreakBeforeClosingBrace &&
322 Current.closesBlockOrBlockTypeList(Style))) {
327 if (!Current.MustBreakBefore &&
Previous.is(tok::l_brace) &&
330 Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma)) {
339 State.LowestLevelOnLine < State.StartOfLineLevel &&
340 State.LowestLevelOnLine < Current.NestingLevel) {
343 if (Current.isMemberAccess() && CurrentState.ContainsUnwrappedBuilder)
348 if (
Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
349 State.Stack[State.Stack.size() - 2].NestedBlockInlined &&
350 State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks &&
357 if (Current.is(TT_FunctionDeclarationName)) {
364 assert(State.Column >= State.FirstIndent);
365 if (State.Column - State.FirstIndent < 6)
372 if (!Current.isOneOf(TT_BinaryOperator, tok::comma) &&
377 Current.isNot(TT_LambdaLBrace)) &&
378 CurrentState.NoLineBreakInOperand) {
385 if (Current.is(TT_ConditionalExpr) &&
Previous.is(tok::r_paren) &&
387 Previous.MatchingParen->Previous->MatchingParen &&
388 Previous.MatchingParen->Previous->MatchingParen->is(TT_LambdaLBrace)) {
390 assert(
Previous.MatchingParen->Previous->is(tok::r_brace));
394 return !State.NoLineBreak && !CurrentState.NoLineBreak;
400 const auto &CurrentState = State.Stack.back();
402 Current.is(TT_LambdaLBrace) &&
Previous.isNot(TT_LineComment)) {
406 if (Current.MustBreakBefore ||
407 (Current.is(TT_InlineASMColon) &&
413 if (CurrentState.BreakBeforeClosingBrace &&
414 (Current.closesBlockOrBlockTypeList(Style) ||
415 (Current.is(tok::r_brace) &&
416 Current.isBlockIndentedInitRBrace(Style)))) {
419 if (CurrentState.BreakBeforeClosingParen && Current.is(tok::r_paren))
423 Current.ObjCSelectorNameParts > 1 &&
424 Current.startsSequence(TT_SelectorName, tok::colon, tok::caret)) {
429 if (CurrentState.IsCSharpGenericTypeConstraint &&
430 Previous.isNot(TT_CSharpGenericTypeConstraintComma)) {
434 (
Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) &&
435 State.Line->First->isNot(TT_AttributeSquare) && Style.
isCpp() &&
444 Previous.is(TT_ConditionalExpr))) &&
445 CurrentState.BreakBeforeParameter && !Current.isTrailingComment() &&
446 !Current.isOneOf(tok::r_paren, tok::r_brace)) {
449 if (CurrentState.IsChainedConditional &&
451 Current.is(tok::colon)) ||
457 (
Previous.is(TT_ArrayInitializerLSquare) &&
466 const FormatToken &BreakConstructorInitializersToken =
470 if (BreakConstructorInitializersToken.
is(TT_CtorInitializerColon) &&
471 (State.Column + State.Line->Last->TotalLength -
Previous.TotalLength >
473 CurrentState.BreakBeforeParameter) &&
474 (!Current.isTrailingComment() || Current.NewlinesBefore > 0) &&
481 if (Current.is(TT_ObjCMethodExpr) &&
Previous.isNot(TT_SelectorName) &&
482 State.Line->startsWith(TT_ObjCMethodSpecifier)) {
485 if (Current.is(TT_SelectorName) &&
Previous.isNot(tok::at) &&
486 CurrentState.ObjCSelectorNameFound && CurrentState.BreakBeforeParameter &&
488 !Current.startsSequence(TT_SelectorName, tok::colon, tok::caret))) {
492 unsigned NewLineColumn = getNewLineColumn(State);
493 if (Current.isMemberAccess() && Style.
ColumnLimit != 0 &&
495 (State.Column > NewLineColumn ||
496 Current.NestingLevel < State.StartOfLineLevel)) {
501 (CurrentState.CallContinuation != 0 ||
502 CurrentState.BreakBeforeParameter) &&
508 !(State.Column <= NewLineColumn && Style.
isJavaScript()) &&
509 !(
Previous.closesScopeAfterBlock() && State.Column <= NewLineColumn)) {
515 if (
Previous.ClosesTemplateDeclaration && CurrentState.BreakBeforeParameter &&
516 Current.CanBreakBefore) {
520 if (State.Line->First->isNot(tok::kw_enum) && State.Column <= NewLineColumn)
525 Previous.is(tok::comma) || Current.NestingLevel < 2) &&
526 !
Previous.isOneOf(tok::kw_return, tok::lessless, tok::at,
528 !
Previous.isOneOf(TT_InlineASMColon, TT_ConditionalExpr) &&
529 nextIsMultilineString(State)) {
536 if (
Previous.is(TT_BinaryOperator) && Current.CanBreakBefore) {
537 const auto PreviousPrecedence =
Previous.getPrecedence();
539 CurrentState.BreakBeforeParameter && !Current.isTrailingComment()) {
540 const bool LHSIsBinaryExpr =
553 const bool IsComparison =
558 Previous.Previous->isNot(TT_BinaryOperator);
562 }
else if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore &&
563 CurrentState.BreakBeforeParameter) {
568 if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator) &&
569 CurrentState.BreakBeforeParameter && CurrentState.FirstLessLess == 0) {
573 if (Current.NestingLevel == 0 && !Current.isTrailingComment()) {
578 if (
Previous.ClosesTemplateDeclaration) {
579 if (Current.is(tok::kw_concept)) {
589 if (Current.is(TT_RequiresClause)) {
600 Current.NewlinesBefore > 0);
602 if (
Previous.is(TT_FunctionAnnotationRParen) &&
606 if (
Previous.is(TT_LeadingJavaAnnotation) && Current.isNot(tok::l_paren) &&
607 Current.isNot(TT_LeadingJavaAnnotation)) {
616 static const llvm::StringSet<> BreakBeforeDecoratedTokens = {
"get",
"set",
618 if (BreakBeforeDecoratedTokens.contains(Current.TokenText))
622 if (Current.is(TT_FunctionDeclarationName) &&
623 !State.Line->ReturnTypeWrapped &&
630 CurrentState.BreakBeforeParameter) {
639 !Current.isOneOf(tok::r_brace, tok::comment)) {
643 if (Current.is(tok::lessless) &&
646 Previous.TokenText ==
"\'\\n\'")))) {
653 if (State.NoContinuation)
661 unsigned ExtraSpaces) {
663 assert(State.NextToken->Previous);
666 assert(!State.Stack.empty());
667 State.NoContinuation =
false;
669 if (Current.is(TT_ImplicitStringLiteral) &&
670 (!
Previous.Tok.getIdentifierInfo() ||
671 Previous.Tok.getIdentifierInfo()->getPPKeywordID() ==
672 tok::pp_not_keyword)) {
675 if (Current.LastNewlineOffset != 0) {
678 State.Column = EndColumn;
680 unsigned StartColumn =
682 assert(EndColumn >= StartColumn);
683 State.Column += EndColumn - StartColumn;
685 moveStateToNextToken(State, DryRun,
false);
689 unsigned Penalty = 0;
691 Penalty = addTokenOnNewLine(State, DryRun);
693 addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
695 return moveStateToNextToken(State, DryRun, Newline) + Penalty;
698void ContinuationIndenter::addTokenOnCurrentLine(
LineState &State,
bool DryRun,
699 unsigned ExtraSpaces) {
701 assert(State.NextToken->Previous);
703 auto &CurrentState = State.Stack.back();
705 bool DisallowLineBreaksOnThisLine =
707 Style.
isCpp() && [&Current] {
713 const auto *Prev = Current.Previous;
717 if (Prev->is(TT_BlockComment) && Current.SpacesRequiredBefore == 0)
719 const auto *PrevNonComment = Current.getPreviousNonComment();
720 if (!PrevNonComment || PrevNonComment->isNot(tok::l_paren))
722 if (Current.isOneOf(tok::comment, tok::l_paren, TT_LambdaLSquare))
724 auto BlockParameterCount = PrevNonComment->BlockParameterCount;
725 if (BlockParameterCount == 0)
729 if (BlockParameterCount > 1)
733 if (!PrevNonComment->Role)
735 auto Comma = PrevNonComment->Role->lastComma();
738 auto Next = Comma->getNextNonComment();
740 !Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret);
743 if (DisallowLineBreaksOnThisLine)
744 State.NoLineBreak =
true;
746 if (Current.is(tok::equal) &&
747 (State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) &&
748 CurrentState.VariablePos == 0 &&
750 Previous.Previous->isNot(TT_DesignatedInitializerPeriod))) {
751 CurrentState.VariablePos = State.Column;
754 while (Tok && CurrentState.VariablePos >= Tok->ColumnWidth) {
755 CurrentState.VariablePos -= Tok->ColumnWidth;
756 if (Tok->SpacesRequiredBefore != 0)
760 if (
Previous.PartOfMultiVariableDeclStmt)
761 CurrentState.LastSpace = CurrentState.VariablePos;
764 unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
767 int PPColumnCorrection = 0;
769 Previous.is(tok::hash) && State.FirstIndent > 0 &&
773 Spaces += State.FirstIndent;
780 PPColumnCorrection = -1;
785 State.Column + Spaces + PPColumnCorrection,
786 false, State.Line->InMacroBody);
792 Current.is(TT_InheritanceColon)) {
793 CurrentState.NoLineBreak =
true;
797 CurrentState.NoLineBreak =
true;
800 if (Current.is(TT_SelectorName) && !CurrentState.ObjCSelectorNameFound) {
801 unsigned MinIndent = std::max(
803 unsigned FirstColonPos = State.Column + Spaces + Current.ColumnWidth;
804 if (Current.LongestObjCSelectorName == 0)
805 CurrentState.AlignColons =
false;
806 else if (MinIndent + Current.LongestObjCSelectorName > FirstColonPos)
807 CurrentState.ColonPos = MinIndent + Current.LongestObjCSelectorName;
809 CurrentState.ColonPos = FirstColonPos;
816 auto IsOpeningBracket = [&](
const FormatToken &Tok) {
817 auto IsStartOfBracedList = [&]() {
818 return Tok.is(tok::l_brace) && Tok.isNot(
BK_Block) &&
821 if (!Tok.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
822 !IsStartOfBracedList()) {
827 if (Tok.Previous->isIf())
829 return !Tok.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
832 auto IsFunctionCallParen = [](
const FormatToken &Tok) {
833 return Tok.is(tok::l_paren) && Tok.ParameterCount > 0 && Tok.Previous &&
834 Tok.Previous->is(tok::identifier);
836 const auto IsInTemplateString = [
this](
const FormatToken &Tok) {
839 for (
const auto *Prev = &Tok; Prev; Prev = Prev->Previous) {
840 if (Prev->is(TT_TemplateString) && Prev->opensScope())
842 if (Prev->is(TT_TemplateString) && Prev->closesScope())
848 const auto IsSimpleFunction = [&](
const FormatToken &Tok) {
849 if (!Tok.FakeLParens.empty() && Tok.FakeLParens.back() >
prec::Unknown)
851 const auto *
Previous = Tok.Previous;
853 TT_LambdaDefinitionLParen) &&
857 if (IsOpeningBracket(Tok) || IsInTemplateString(Tok))
859 const auto *Next = Tok.Next;
860 return !Next || Next->isMemberAccess() ||
861 Next->is(TT_FunctionDeclarationLParen) || IsFunctionCallParen(*Next);
865 IsOpeningBracket(
Previous) && State.Column > getNewLineColumn(State) &&
873 !IsSimpleFunction(Current)) {
874 CurrentState.NoLineBreak =
true;
878 CurrentState.NoLineBreak =
true;
885 !CurrentState.IsCSharpGenericTypeConstraint &&
Previous.opensScope() &&
887 Previous.isNot(TT_TableGenDAGArgOpener) &&
888 Previous.isNot(TT_TableGenDAGArgOpenerToBreak) &&
889 !(Current.MacroParent &&
Previous.MacroParent) &&
890 (Current.isNot(TT_LineComment) ||
892 !IsInTemplateString(Current)) {
893 CurrentState.Indent = State.Column + Spaces;
894 CurrentState.IsAligned =
true;
897 CurrentState.NoLineBreak =
true;
899 CurrentState.NoLineBreak =
true;
902 State.Column > getNewLineColumn(State)) {
903 CurrentState.ContainsUnwrappedBuilder =
true;
906 if (Current.is(TT_TrailingReturnArrow) &&
908 CurrentState.NoLineBreak =
true;
910 if (Current.isMemberAccess() &&
Previous.is(tok::r_paren) &&
919 CurrentState.NoLineBreak =
true;
926 const FormatToken *
P = Current.getPreviousNonComment();
927 if (Current.isNot(tok::comment) &&
P &&
928 (
P->isOneOf(TT_BinaryOperator, tok::comma) ||
929 (
P->is(TT_ConditionalExpr) &&
P->is(tok::colon))) &&
930 !
P->isOneOf(TT_OverloadedOperator, TT_CtorInitializerComma) &&
934 bool BreakBeforeOperator =
935 P->MustBreakBefore ||
P->is(tok::lessless) ||
936 (
P->is(TT_BinaryOperator) &&
942 bool HasTwoOperands =
P->OperatorIndex == 0 && !
P->NextOperator &&
943 P->isNot(TT_ConditionalExpr);
944 if ((!BreakBeforeOperator &&
947 (!CurrentState.LastOperatorWrapped && BreakBeforeOperator)) {
948 CurrentState.NoLineBreakInOperand =
true;
952 State.Column += Spaces;
953 if (Current.isNot(tok::comment) &&
Previous.is(tok::l_paren) &&
958 CurrentState.LastSpace = State.Column;
959 CurrentState.NestedBlockIndent = State.Column;
960 }
else if (!Current.isOneOf(tok::comment, tok::caret) &&
962 Previous.isNot(TT_OverloadedOperator)) ||
964 CurrentState.LastSpace = State.Column;
965 }
else if (
Previous.is(TT_CtorInitializerColon) &&
966 (!Current.isTrailingComment() || Current.NewlinesBefore > 0) &&
969 CurrentState.Indent = State.Column;
970 CurrentState.LastSpace = State.Column;
971 }
else if (
Previous.isOneOf(TT_ConditionalExpr, TT_CtorInitializerColon)) {
972 CurrentState.LastSpace = State.Column;
973 }
else if (
Previous.is(TT_BinaryOperator) &&
977 Current.StartsBinaryExpression)) {
981 CurrentState.LastSpace = State.Column;
982 }
else if (
Previous.is(TT_InheritanceColon)) {
983 CurrentState.Indent = State.Column;
984 CurrentState.LastSpace = State.Column;
985 }
else if (Current.is(TT_CSharpGenericTypeConstraintColon)) {
986 CurrentState.ColonPos = State.Column;
994 const FormatToken *Next =
Previous.MatchingParen->getNextNonComment();
995 if (Next && Next->isMemberAccess() && State.Stack.size() > 1 &&
996 State.Stack[State.Stack.size() - 2].CallContinuation == 0) {
997 CurrentState.LastSpace = State.Column;
1003unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
1005 FormatToken &Current = *State.NextToken;
1006 assert(State.NextToken->Previous);
1007 const FormatToken &
Previous = *State.NextToken->Previous;
1008 auto &CurrentState = State.Stack.back();
1012 unsigned Penalty = 0;
1014 const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
1015 const FormatToken *NextNonComment =
Previous.getNextNonComment();
1016 if (!NextNonComment)
1017 NextNonComment = &Current;
1020 if (!CurrentState.ContainsLineBreak)
1022 CurrentState.ContainsLineBreak =
true;
1024 Penalty += State.NextToken->SplitPenalty;
1029 if (NextNonComment->is(tok::lessless) && CurrentState.FirstLessLess == 0 &&
1031 CurrentState.BreakBeforeParameter)) {
1035 State.Column = getNewLineColumn(State);
1049 if (State.Column > State.FirstIndent) {
1064 if (Current.isNot(TT_TrailingReturnArrow) &&
1066 !PreviousNonComment || PreviousNonComment->isNot(tok::equal) ||
1068 CurrentState.NestedBlockIndent = State.Column;
1071 if (NextNonComment->isMemberAccess()) {
1072 if (CurrentState.CallContinuation == 0)
1073 CurrentState.CallContinuation = State.Column;
1074 }
else if (NextNonComment->is(TT_SelectorName)) {
1075 if (!CurrentState.ObjCSelectorNameFound) {
1076 if (NextNonComment->LongestObjCSelectorName == 0) {
1077 CurrentState.AlignColons =
false;
1079 CurrentState.ColonPos =
1081 ? std::max(CurrentState.Indent,
1083 : CurrentState.Indent) +
1085 NextNonComment->ColumnWidth);
1087 }
else if (CurrentState.AlignColons &&
1088 CurrentState.ColonPos <= NextNonComment->ColumnWidth) {
1089 CurrentState.ColonPos = State.Column + NextNonComment->ColumnWidth;
1091 }
else if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
1092 PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) {
1102 if (State.Stack.size() > 1) {
1103 State.Stack[State.Stack.size() - 2].LastSpace =
1104 std::max(CurrentState.LastSpace, CurrentState.Indent) +
1109 if ((PreviousNonComment &&
1110 PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
1111 !CurrentState.AvoidBinPacking) ||
1113 CurrentState.BreakBeforeParameter =
false;
1115 if (PreviousNonComment &&
1116 (PreviousNonComment->isOneOf(TT_TemplateCloser, TT_JavaAnnotation) ||
1117 PreviousNonComment->ClosesRequiresClause) &&
1118 Current.NestingLevel == 0) {
1119 CurrentState.BreakBeforeParameter =
false;
1121 if (NextNonComment->is(tok::question) ||
1122 (PreviousNonComment && PreviousNonComment->is(tok::question))) {
1123 CurrentState.BreakBeforeParameter =
true;
1125 if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore)
1126 CurrentState.BreakBeforeParameter =
false;
1130 if (Current.is(tok::r_brace) && Current.MatchingParen &&
1133 !Current.MatchingParen->Children.empty()) {
1137 MaxEmptyLinesToKeep = 1;
1140 std::max(1u, std::min(Current.NewlinesBefore, MaxEmptyLinesToKeep));
1141 bool ContinuePPDirective =
1144 CurrentState.IsAligned, ContinuePPDirective);
1147 if (!Current.isTrailingComment())
1148 CurrentState.LastSpace = State.Column;
1149 if (Current.is(tok::lessless)) {
1153 CurrentState.LastSpace += 3;
1156 State.StartOfLineLevel = Current.NestingLevel;
1157 State.LowestLevelOnLine = Current.NestingLevel;
1161 bool NestedBlockSpecialCase =
1162 (!Style.
isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
1163 State.Stack[State.Stack.size() - 2].NestedBlockInlined) ||
1167 NestedBlockSpecialCase =
1168 NestedBlockSpecialCase ||
1169 (Current.MatchingParen &&
1170 Current.MatchingParen->is(TT_RequiresExpressionLBrace));
1171 if (!NestedBlockSpecialCase) {
1172 auto ParentLevelIt = std::next(State.Stack.rbegin());
1174 Current.MatchingParen && Current.MatchingParen->is(TT_LambdaLBrace)) {
1189 auto FindCurrentLevel = [&](
const auto &It) {
1190 return std::find_if(It, State.Stack.rend(), [](
const auto &PState) {
1191 return PState.Tok != nullptr;
1194 auto MaybeIncrement = [&](
const auto &It) {
1195 return It != State.Stack.rend() ? std::next(It) : It;
1197 auto LambdaLevelIt = FindCurrentLevel(State.Stack.rbegin());
1198 auto LevelContainingLambdaIt =
1199 FindCurrentLevel(MaybeIncrement(LambdaLevelIt));
1200 ParentLevelIt = MaybeIncrement(LevelContainingLambdaIt);
1202 for (
auto I = ParentLevelIt,
E = State.Stack.rend(); I !=
E; ++I)
1203 I->BreakBeforeParameter =
true;
1206 if (PreviousNonComment &&
1207 !PreviousNonComment->isOneOf(tok::comma, tok::colon, tok::semi) &&
1208 ((PreviousNonComment->isNot(TT_TemplateCloser) &&
1209 !PreviousNonComment->ClosesRequiresClause) ||
1210 Current.NestingLevel != 0) &&
1211 !PreviousNonComment->isOneOf(
1212 TT_BinaryOperator, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
1213 TT_LeadingJavaAnnotation) &&
1214 Current.isNot(TT_BinaryOperator) && !PreviousNonComment->opensScope() &&
1218 Current.isNot(TT_LambdaLBrace))) {
1219 CurrentState.BreakBeforeParameter =
true;
1224 if (PreviousNonComment &&
1225 (PreviousNonComment->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
1227 CurrentState.BreakBeforeClosingBrace =
true;
1230 if (PreviousNonComment && PreviousNonComment->is(tok::l_paren)) {
1231 CurrentState.BreakBeforeClosingParen =
1235 if (CurrentState.AvoidBinPacking) {
1240 bool PreviousIsBreakingCtorInitializerColon =
1241 PreviousNonComment && PreviousNonComment->is(TT_CtorInitializerColon) &&
1243 bool AllowAllConstructorInitializersOnNextLine =
1246 if (!(
Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) ||
1247 PreviousIsBreakingCtorInitializerColon) ||
1249 State.Line->MustBeDeclaration) ||
1251 !State.Line->MustBeDeclaration) ||
1252 (!AllowAllConstructorInitializersOnNextLine &&
1253 PreviousIsBreakingCtorInitializerColon) ||
1255 CurrentState.BreakBeforeParameter =
true;
1261 if (PreviousIsBreakingCtorInitializerColon &&
1262 AllowAllConstructorInitializersOnNextLine) {
1263 CurrentState.BreakBeforeParameter =
false;
1268 CurrentState.BreakBeforeParameter =
true;
1273unsigned ContinuationIndenter::getNewLineColumn(
const LineState &State) {
1274 if (!State.NextToken || !State.NextToken->Previous)
1277 FormatToken &Current = *State.NextToken;
1278 const auto &CurrentState = State.Stack.back();
1280 if (CurrentState.IsCSharpGenericTypeConstraint &&
1281 Current.isNot(TT_CSharpGenericTypeConstraint)) {
1282 return CurrentState.ColonPos + 2;
1285 const FormatToken &
Previous = *Current.Previous;
1287 unsigned ContinuationIndent =
1288 std::max(CurrentState.LastSpace, CurrentState.Indent) +
1290 const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
1291 const FormatToken *NextNonComment =
Previous.getNextNonComment();
1292 if (!NextNonComment)
1293 NextNonComment = &Current;
1298 return std::max(CurrentState.LastSpace,
1304 if (Style.
isVerilog() && PreviousNonComment &&
1306 return State.FirstIndent;
1310 State.Line->First->is(tok::kw_enum)) {
1311 return (Style.
IndentWidth * State.Line->First->IndentLevel) +
1315 if ((NextNonComment->is(tok::l_brace) && NextNonComment->is(
BK_Block)) ||
1317 if (Current.NestingLevel == 0 ||
1319 State.NextToken->is(TT_LambdaLBrace))) {
1320 return State.FirstIndent;
1322 return CurrentState.Indent;
1324 if (Current.is(TT_TrailingReturnArrow) &&
1325 Previous.isOneOf(tok::kw_noexcept, tok::kw_mutable, tok::kw_constexpr,
1326 tok::kw_consteval, tok::kw_static, TT_AttributeSquare)) {
1327 return ContinuationIndent;
1329 if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
1331 State.Stack.size() > 1) {
1332 if (Current.closesBlockOrBlockTypeList(Style))
1333 return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
1334 if (Current.MatchingParen && Current.MatchingParen->is(
BK_BracedInit))
1335 return State.Stack[State.Stack.size() - 2].LastSpace;
1336 return State.FirstIndent;
1353 if (Current.is(tok::r_paren) && State.Stack.size() > 1 &&
1355 Current.Next->isOneOf(tok::semi, tok::kw_const, tok::l_brace))) {
1356 return State.Stack[State.Stack.size() - 2].LastSpace;
1360 if (Style.
isTableGen() && Current.is(TT_TableGenDAGArgCloser) &&
1361 State.Stack.size() > 1) {
1362 return State.Stack[State.Stack.size() - 2].LastSpace;
1365 (Current.is(tok::r_paren) ||
1366 (Current.is(tok::r_brace) && Current.MatchingParen &&
1368 State.Stack.size() > 1) {
1369 return State.Stack[State.Stack.size() - 2].LastSpace;
1371 if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope())
1372 return State.Stack[State.Stack.size() - 2].LastSpace;
1380 if (Current.is(tok::identifier) && Current.Next &&
1381 (!Style.
isVerilog() || Current.Next->is(tok::colon)) &&
1382 (Current.Next->is(TT_DictLiteral) ||
1383 (Style.
isProto() && Current.Next->isOneOf(tok::less, tok::l_brace)))) {
1384 return CurrentState.Indent;
1386 if (NextNonComment->is(TT_ObjCStringLiteral) &&
1387 State.StartOfStringLiteral != 0) {
1388 return State.StartOfStringLiteral - 1;
1390 if (NextNonComment->isStringLiteral() && State.StartOfStringLiteral != 0)
1391 return State.StartOfStringLiteral;
1392 if (NextNonComment->is(tok::lessless) && CurrentState.FirstLessLess != 0)
1393 return CurrentState.FirstLessLess;
1394 if (NextNonComment->isMemberAccess()) {
1395 if (CurrentState.CallContinuation == 0)
1396 return ContinuationIndent;
1397 return CurrentState.CallContinuation;
1399 if (CurrentState.QuestionColumn != 0 &&
1400 ((NextNonComment->is(tok::colon) &&
1401 NextNonComment->is(TT_ConditionalExpr)) ||
1402 Previous.is(TT_ConditionalExpr))) {
1403 if (((NextNonComment->is(tok::colon) && NextNonComment->Next &&
1404 !NextNonComment->Next->FakeLParens.empty() &&
1406 (
Previous.is(tok::colon) && !Current.FakeLParens.empty() &&
1408 !CurrentState.IsWrappedConditional) {
1413 unsigned Indent = CurrentState.Indent;
1420 return CurrentState.QuestionColumn;
1422 if (
Previous.is(tok::comma) && CurrentState.VariablePos != 0)
1423 return CurrentState.VariablePos;
1424 if (Current.is(TT_RequiresClause)) {
1431 return CurrentState.Indent;
1436 if (NextNonComment->isOneOf(TT_CtorInitializerColon, TT_InheritanceColon,
1437 TT_InheritanceComma)) {
1440 if ((PreviousNonComment &&
1441 (PreviousNonComment->ClosesTemplateDeclaration ||
1442 PreviousNonComment->ClosesRequiresClause ||
1443 (PreviousNonComment->is(TT_AttributeMacro) &&
1444 Current.isNot(tok::l_paren)) ||
1445 PreviousNonComment->isOneOf(
1446 TT_AttributeRParen, TT_AttributeSquare, TT_FunctionAnnotationRParen,
1447 TT_JavaAnnotation, TT_LeadingJavaAnnotation))) ||
1449 NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName))) {
1450 return std::max(CurrentState.LastSpace, CurrentState.Indent);
1452 if (NextNonComment->is(TT_SelectorName)) {
1453 if (!CurrentState.ObjCSelectorNameFound) {
1454 unsigned MinIndent = CurrentState.Indent;
1456 MinIndent = std::max(MinIndent,
1469 std::max(NextNonComment->LongestObjCSelectorName,
1470 NextNonComment->ColumnWidth) -
1471 NextNonComment->ColumnWidth;
1473 if (!CurrentState.AlignColons)
1474 return CurrentState.Indent;
1475 if (CurrentState.ColonPos > NextNonComment->ColumnWidth)
1476 return CurrentState.ColonPos - NextNonComment->ColumnWidth;
1477 return CurrentState.Indent;
1479 if (NextNonComment->is(tok::colon) && NextNonComment->is(TT_ObjCMethodExpr))
1480 return CurrentState.ColonPos;
1481 if (NextNonComment->is(TT_ArraySubscriptLSquare)) {
1482 if (CurrentState.StartOfArraySubscripts != 0) {
1483 return CurrentState.StartOfArraySubscripts;
1486 return CurrentState.Indent;
1488 return ContinuationIndent;
1493 if (State.Line->InPragmaDirective) {
1494 FormatToken *PragmaType = State.Line->First->Next->Next;
1495 if (PragmaType && PragmaType->TokenText ==
"omp")
1501 if (NextNonComment->is(tok::identifier) && NextNonComment->FakeRParens == 0 &&
1502 NextNonComment->Next && NextNonComment->Next->is(TT_ObjCMethodExpr)) {
1503 return CurrentState.Indent;
1506 if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) ||
1507 Previous.isOneOf(tok::coloncolon, tok::equal, TT_JsTypeColon)) {
1508 return ContinuationIndent;
1510 if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
1511 PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) {
1512 return ContinuationIndent;
1514 if (NextNonComment->is(TT_CtorInitializerComma))
1515 return CurrentState.Indent;
1516 if (PreviousNonComment && PreviousNonComment->is(TT_CtorInitializerColon) &&
1518 return CurrentState.Indent;
1520 if (PreviousNonComment && PreviousNonComment->is(TT_InheritanceColon) &&
1522 return CurrentState.Indent;
1525 Previous.isNot(TT_TableGenDAGArgOperatorToBreak) &&
1526 !Current.isBinaryOperator() &&
1527 !Current.isOneOf(tok::colon, tok::comment)) {
1528 return ContinuationIndent;
1530 if (Current.is(TT_ProtoExtensionLSquare))
1531 return CurrentState.Indent;
1532 if (Current.isBinaryOperator() && CurrentState.UnindentOperator) {
1533 return CurrentState.Indent - Current.Tok.getLength() -
1534 Current.SpacesRequiredBefore;
1536 if (Current.is(tok::comment) && NextNonComment->isBinaryOperator() &&
1537 CurrentState.UnindentOperator) {
1538 return CurrentState.Indent - NextNonComment->Tok.getLength() -
1539 NextNonComment->SpacesRequiredBefore;
1541 if (CurrentState.Indent == State.FirstIndent && PreviousNonComment &&
1542 !PreviousNonComment->isOneOf(tok::r_brace, TT_CtorInitializerComma)) {
1547 return CurrentState.Indent;
1562unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
1563 bool DryRun,
bool Newline) {
1564 assert(State.Stack.size());
1565 const FormatToken &Current = *State.NextToken;
1566 auto &CurrentState = State.Stack.back();
1568 if (Current.is(TT_CSharpGenericTypeConstraint))
1569 CurrentState.IsCSharpGenericTypeConstraint =
true;
1570 if (Current.isOneOf(tok::comma, TT_BinaryOperator))
1571 CurrentState.NoLineBreakInOperand =
false;
1572 if (Current.isOneOf(TT_InheritanceColon, TT_CSharpGenericTypeConstraintColon))
1573 CurrentState.AvoidBinPacking =
true;
1574 if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator)) {
1575 if (CurrentState.FirstLessLess == 0)
1576 CurrentState.FirstLessLess = State.Column;
1578 CurrentState.LastOperatorWrapped = Newline;
1580 if (Current.is(TT_BinaryOperator) && Current.isNot(tok::lessless))
1581 CurrentState.LastOperatorWrapped = Newline;
1582 if (Current.is(TT_ConditionalExpr) && Current.Previous &&
1583 Current.Previous->isNot(TT_ConditionalExpr)) {
1584 CurrentState.LastOperatorWrapped = Newline;
1586 if (Current.is(TT_ArraySubscriptLSquare) &&
1587 CurrentState.StartOfArraySubscripts == 0) {
1588 CurrentState.StartOfArraySubscripts = State.Column;
1591 auto IsWrappedConditional = [](
const FormatToken &Tok) {
1592 if (!(Tok.is(TT_ConditionalExpr) && Tok.is(tok::question)))
1594 if (Tok.MustBreakBefore)
1597 const FormatToken *Next = Tok.getNextNonComment();
1598 return Next && Next->MustBreakBefore;
1600 if (IsWrappedConditional(Current))
1601 CurrentState.IsWrappedConditional =
true;
1603 CurrentState.QuestionColumn = State.Column;
1605 const FormatToken *
Previous = Current.Previous;
1609 CurrentState.QuestionColumn = State.Column;
1611 if (!Current.opensScope() && !Current.closesScope() &&
1612 Current.isNot(TT_PointerOrReference)) {
1613 State.LowestLevelOnLine =
1614 std::min(State.LowestLevelOnLine, Current.NestingLevel);
1616 if (Current.isMemberAccess())
1617 CurrentState.StartOfFunctionCall = !Current.NextOperator ? 0 : State.Column;
1618 if (Current.is(TT_SelectorName))
1619 CurrentState.ObjCSelectorNameFound =
true;
1620 if (Current.is(TT_CtorInitializerColon) &&
1631 CurrentState.NestedBlockIndent = CurrentState.Indent;
1633 CurrentState.AvoidBinPacking =
true;
1634 CurrentState.BreakBeforeParameter =
1639 CurrentState.BreakBeforeParameter =
false;
1642 if (Current.is(TT_CtorInitializerColon) &&
1644 CurrentState.Indent =
1646 CurrentState.NestedBlockIndent = CurrentState.Indent;
1648 CurrentState.AvoidBinPacking =
true;
1650 CurrentState.BreakBeforeParameter =
false;
1652 if (Current.is(TT_InheritanceColon)) {
1653 CurrentState.Indent =
1656 if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && Newline)
1657 CurrentState.NestedBlockIndent = State.Column + Current.ColumnWidth + 1;
1658 if (Current.isOneOf(TT_LambdaLSquare, TT_TrailingReturnArrow))
1659 CurrentState.LastSpace = State.Column;
1660 if (Current.is(TT_RequiresExpression) &&
1662 CurrentState.NestedBlockIndent = State.Column;
1666 const FormatToken *
Previous = Current.getPreviousNonComment();
1674 if (Current.isNot(tok::comment) && !Current.ClosesRequiresClause &&
1676 Previous->isNot(TT_DictLiteral) && State.Stack.size() > 1 &&
1677 !CurrentState.HasMultipleNestedBlocks) {
1678 if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline)
1679 for (ParenState &PState : llvm::drop_end(State.Stack))
1680 PState.NoLineBreak =
true;
1681 State.Stack[State.Stack.size() - 2].NestedBlockInlined =
false;
1683 if (
Previous && (
Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr) ||
1684 (
Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) &&
1685 !
Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)))) {
1686 CurrentState.NestedBlockInlined =
1690 moveStatePastFakeLParens(State, Newline);
1691 moveStatePastScopeCloser(State);
1694 bool AllowBreak = !State.Stack.back().NoLineBreak &&
1695 !State.Stack.back().NoLineBreakInOperand;
1696 moveStatePastScopeOpener(State, Newline);
1697 moveStatePastFakeRParens(State);
1699 if (Current.is(TT_ObjCStringLiteral) && State.StartOfStringLiteral == 0)
1700 State.StartOfStringLiteral = State.Column + 1;
1701 if (Current.is(TT_CSharpStringLiteral) && State.StartOfStringLiteral == 0) {
1702 State.StartOfStringLiteral = State.Column + 1;
1703 }
else if (Current.is(TT_TableGenMultiLineString) &&
1704 State.StartOfStringLiteral == 0) {
1705 State.StartOfStringLiteral = State.Column + 1;
1706 }
else if (Current.isStringLiteral() && State.StartOfStringLiteral == 0) {
1707 State.StartOfStringLiteral = State.Column;
1708 }
else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
1709 !Current.isStringLiteral()) {
1710 State.StartOfStringLiteral = 0;
1713 State.Column += Current.ColumnWidth;
1714 State.NextToken = State.NextToken->Next;
1719 if (Style.
isVerilog() && State.NextToken &&
1720 State.NextToken->MustBreakBefore &&
1723 CurrentState.Indent = State.FirstIndent;
1727 handleEndOfLine(Current, State, DryRun, AllowBreak, Newline);
1730 Current.Role->formatFromToken(State,
this, DryRun);
1737 Penalty +=
Previous->Role->formatAfterToken(State,
this, DryRun);
1742void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
1744 const FormatToken &Current = *State.NextToken;
1745 if (Current.FakeLParens.empty())
1748 const FormatToken *
Previous = Current.getPreviousNonComment();
1753 bool SkipFirstExtraIndent =
1756 Previous->isOneOf(tok::semi, tok::kw_return, TT_RequiresClause) ||
1760 for (
const auto &PrecedenceLevel : llvm::reverse(Current.FakeLParens)) {
1761 const auto &CurrentState = State.Stack.back();
1762 ParenState NewParenState = CurrentState;
1763 NewParenState.Tok =
nullptr;
1764 NewParenState.ContainsLineBreak =
false;
1765 NewParenState.LastOperatorWrapped =
true;
1766 NewParenState.IsChainedConditional =
false;
1767 NewParenState.IsWrappedConditional =
false;
1768 NewParenState.UnindentOperator =
false;
1769 NewParenState.NoLineBreak =
1770 NewParenState.NoLineBreak || CurrentState.NoLineBreakInOperand;
1774 NewParenState.AvoidBinPacking =
false;
1779 if (!Current.isTrailingComment() &&
1785 PrecedenceLevel >
prec::Comma || Current.NestingLevel == 0) &&
1788 TT_TableGenDAGArgListCommaToBreak)))) {
1789 NewParenState.Indent = std::max(
1790 std::max(State.Column, NewParenState.Indent), CurrentState.LastSpace);
1797 State.Stack.size() > 1) {
1798 NewParenState.Indent = State.Stack[State.Stack.size() - 2].Indent +
1810 NewParenState.UnindentOperator =
true;
1813 NewParenState.IsAligned =
true;
1823 NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
1825 Current.isNot(TT_UnaryOperator) &&
1827 NewParenState.StartOfFunctionCall = State.Column;
1837 &PrecedenceLevel == &Current.FakeLParens.back() &&
1838 !CurrentState.IsWrappedConditional) {
1839 NewParenState.IsChainedConditional =
true;
1840 NewParenState.UnindentOperator = State.Stack.back().UnindentOperator;
1843 !Current.isTrailingComment())) {
1847 NewParenState.BreakBeforeParameter =
false;
1848 State.Stack.push_back(NewParenState);
1849 SkipFirstExtraIndent =
false;
1853void ContinuationIndenter::moveStatePastFakeRParens(LineState &State) {
1854 for (
unsigned i = 0, e = State.NextToken->FakeRParens; i != e; ++i) {
1855 unsigned VariablePos = State.Stack.back().VariablePos;
1856 if (State.Stack.size() == 1) {
1860 State.Stack.pop_back();
1861 State.Stack.back().VariablePos = VariablePos;
1867 State.Stack.back().LastSpace -= Style.
IndentWidth;
1871void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
1873 const FormatToken &Current = *State.NextToken;
1874 if (!Current.opensScope())
1877 const auto &CurrentState = State.Stack.back();
1880 if (Current.isOneOf(tok::less, tok::l_paren) &&
1881 CurrentState.IsCSharpGenericTypeConstraint) {
1885 if (Current.MatchingParen && Current.is(
BK_Block)) {
1886 moveStateToNewBlock(State, Newline);
1891 unsigned LastSpace = CurrentState.LastSpace;
1892 bool AvoidBinPacking;
1893 bool BreakBeforeParameter =
false;
1894 unsigned NestedBlockIndent = std::max(CurrentState.StartOfFunctionCall,
1895 CurrentState.NestedBlockIndent);
1896 if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
1898 if (Current.opensBlockOrBlockTypeList(Style)) {
1900 std::min(State.Column, CurrentState.NestedBlockIndent);
1901 }
else if (Current.is(tok::l_brace)) {
1908 const FormatToken *NextNonComment = Current.getNextNonComment();
1909 bool EndsInComma = Current.MatchingParen &&
1910 Current.MatchingParen->Previous &&
1911 Current.MatchingParen->Previous->is(tok::comma);
1912 AvoidBinPacking = EndsInComma || Current.is(TT_DictLiteral) ||
1914 (NextNonComment && NextNonComment->isOneOf(
1915 TT_DesignatedInitializerPeriod,
1916 TT_DesignatedInitializerLSquare));
1917 BreakBeforeParameter = EndsInComma;
1918 if (Current.ParameterCount > 1)
1919 NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1);
1923 std::max(CurrentState.LastSpace, CurrentState.StartOfFunctionCall);
1925 if (Style.
isTableGen() && Current.is(TT_TableGenDAGArgOpenerToBreak) &&
1931 const FormatToken *Next = Current.Next;
1932 if (Next && Next->is(TT_TableGenDAGArgOperatorID))
1933 NewIndent = State.Column + Next->TokenText.size() + 2;
1940 if (Current.is(tok::less) && Current.ParentBracket == tok::l_paren) {
1941 NewIndent = std::max(NewIndent, CurrentState.Indent);
1942 LastSpace = std::max(LastSpace, CurrentState.Indent);
1946 Current.MatchingParen &&
1947 Current.MatchingParen->getPreviousNonComment() &&
1948 Current.MatchingParen->getPreviousNonComment()->is(tok::comma);
1952 bool ObjCBinPackProtocolList =
1957 bool BinPackDeclaration =
1959 (State.Line->Type ==
LT_ObjCDecl && ObjCBinPackProtocolList);
1961 bool GenericSelection =
1962 Current.getPreviousNonComment() &&
1963 Current.getPreviousNonComment()->is(tok::kw__Generic);
1966 (CurrentState.IsCSharpGenericTypeConstraint) || GenericSelection ||
1968 (State.Line->MustBeDeclaration && !BinPackDeclaration) ||
1974 if (Current.is(TT_ObjCMethodExpr) && Current.MatchingParen &&
1981 BreakBeforeParameter =
true;
1986 for (
const FormatToken *Tok = &Current;
1987 Tok && Tok != Current.MatchingParen; Tok = Tok->Next) {
1988 if (Tok->MustBreakBefore ||
1989 (Tok->CanBreakBefore && Tok->NewlinesBefore > 0)) {
1990 BreakBeforeParameter =
true;
1998 BreakBeforeParameter =
true;
2004 Current.Children.empty() &&
2005 !Current.isOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) &&
2006 (CurrentState.NoLineBreak || CurrentState.NoLineBreakInOperand ||
2007 (Current.is(TT_TemplateOpener) &&
2008 CurrentState.ContainsUnwrappedBuilder));
2009 State.Stack.push_back(
2010 ParenState(&Current, NewIndent, LastSpace, AvoidBinPacking, NoLineBreak));
2011 auto &NewState = State.Stack.back();
2012 NewState.NestedBlockIndent = NestedBlockIndent;
2013 NewState.BreakBeforeParameter = BreakBeforeParameter;
2014 NewState.HasMultipleNestedBlocks = (Current.BlockParameterCount > 1);
2017 Current.is(tok::l_paren)) {
2019 FormatToken
const *next = Current.Next;
2021 if (next->is(TT_LambdaLSquare)) {
2022 NewState.HasMultipleNestedBlocks =
true;
2029 NewState.IsInsideObjCArrayLiteral = Current.is(TT_ArrayInitializerLSquare) &&
2031 Current.Previous->is(tok::at);
2034void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
2035 const FormatToken &Current = *State.NextToken;
2036 if (!Current.closesScope())
2041 if (State.Stack.size() > 1 &&
2042 (Current.isOneOf(tok::r_paren, tok::r_square, TT_TemplateString) ||
2043 (Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
2044 State.NextToken->is(TT_TemplateCloser) ||
2045 State.NextToken->is(TT_TableGenListCloser) ||
2046 (Current.is(tok::greater) && Current.is(TT_DictLiteral)))) {
2047 State.Stack.pop_back();
2050 auto &CurrentState = State.Stack.back();
2062 if (CurrentState.BreakBeforeParameter && Current.MatchingParen &&
2063 Current.MatchingParen->Previous) {
2064 const FormatToken &CurrentScopeOpener = *Current.MatchingParen->Previous;
2065 if (CurrentScopeOpener.is(TT_ObjCMethodExpr) &&
2066 CurrentScopeOpener.MatchingParen) {
2067 int NecessarySpaceInLine =
2069 CurrentScopeOpener.TotalLength - Current.TotalLength - 1;
2070 if (State.Column + Current.ColumnWidth + NecessarySpaceInLine <=
2072 CurrentState.BreakBeforeParameter =
false;
2077 if (Current.is(tok::r_square)) {
2079 const FormatToken *NextNonComment = Current.getNextNonComment();
2080 if (NextNonComment && NextNonComment->isNot(tok::l_square))
2081 CurrentState.StartOfArraySubscripts = 0;
2085void ContinuationIndenter::moveStateToNewBlock(LineState &State,
bool NewLine) {
2087 State.NextToken->is(TT_LambdaLBrace) &&
2088 !State.Line->MightBeFunctionDecl) {
2089 State.Stack.back().NestedBlockIndent = State.FirstIndent;
2091 unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
2093 unsigned NewIndent =
2094 NestedBlockIndent + (State.NextToken->is(TT_ObjCBlockLBrace)
2104 State.NextToken->is(TT_LambdaLBrace);
2106 State.Stack.push_back(ParenState(State.NextToken, NewIndent,
2107 State.Stack.back().LastSpace,
2108 true, NoLineBreak));
2109 State.Stack.back().NestedBlockIndent = NestedBlockIndent;
2110 State.Stack.back().BreakBeforeParameter =
true;
2116 size_t LastNewlinePos =
Text.find_last_of(
"\n");
2117 if (LastNewlinePos == StringRef::npos) {
2118 return StartColumn +
2122 0, TabWidth, Encoding);
2126unsigned ContinuationIndenter::reformatRawStringLiteral(
2127 const FormatToken &Current, LineState &State,
2128 const FormatStyle &RawStringStyle,
bool DryRun,
bool Newline) {
2129 unsigned StartColumn = State.Column - Current.ColumnWidth;
2131 StringRef NewDelimiter =
2133 if (NewDelimiter.empty())
2134 NewDelimiter = OldDelimiter;
2137 unsigned OldPrefixSize = 3 + OldDelimiter.size();
2138 unsigned OldSuffixSize = 2 + OldDelimiter.size();
2141 std::string RawText = std::string(
2142 Current.TokenText.substr(OldPrefixSize).drop_back(OldSuffixSize));
2143 if (NewDelimiter != OldDelimiter) {
2146 std::string CanonicalDelimiterSuffix = (
")" + NewDelimiter +
"\"").str();
2147 if (StringRef(RawText).contains(CanonicalDelimiterSuffix))
2148 NewDelimiter = OldDelimiter;
2151 unsigned NewPrefixSize = 3 + NewDelimiter.size();
2152 unsigned NewSuffixSize = 2 + NewDelimiter.size();
2155 unsigned FirstStartColumn = StartColumn + NewPrefixSize;
2166 bool ContentStartsOnNewline = Current.TokenText[OldPrefixSize] ==
'\n';
2188 unsigned CurrentIndent =
2189 (!Newline && Current.Next && Current.Next->is(tok::r_paren))
2190 ? State.Stack.back().NestedBlockIndent
2191 : State.Stack.back().Indent;
2192 unsigned NextStartColumn = ContentStartsOnNewline
2204 unsigned LastStartColumn =
2205 Current.NewlinesBefore ? FirstStartColumn - NewPrefixSize : CurrentIndent;
2208 RawStringStyle, RawText, {tooling::Range(0, RawText.size())},
2209 FirstStartColumn, NextStartColumn, LastStartColumn,
"<stdin>",
2213 tooling::Replacements NoFixes;
2215 return addMultilineToken(Current, State);
2217 if (NewDelimiter != OldDelimiter) {
2220 SourceLocation PrefixDelimiterStart =
2221 Current.Tok.getLocation().getLocWithOffset(2);
2222 auto PrefixErr = Whitespaces.
addReplacement(tooling::Replacement(
2223 SourceMgr, PrefixDelimiterStart, OldDelimiter.size(), NewDelimiter));
2226 <<
"Failed to update the prefix delimiter of a raw string: "
2227 << llvm::toString(std::move(PrefixErr)) <<
"\n";
2231 SourceLocation SuffixDelimiterStart =
2232 Current.Tok.getLocation().getLocWithOffset(Current.TokenText.size() -
2233 1 - OldDelimiter.size());
2234 auto SuffixErr = Whitespaces.
addReplacement(tooling::Replacement(
2235 SourceMgr, SuffixDelimiterStart, OldDelimiter.size(), NewDelimiter));
2238 <<
"Failed to update the suffix delimiter of a raw string: "
2239 << llvm::toString(std::move(SuffixErr)) <<
"\n";
2242 SourceLocation OriginLoc =
2243 Current.Tok.getLocation().getLocWithOffset(OldPrefixSize);
2244 for (
const tooling::Replacement &Fix : Fixes.first) {
2246 SourceMgr, OriginLoc.getLocWithOffset(Fix.getOffset()),
2247 Fix.getLength(), Fix.getReplacementText()));
2249 llvm::errs() <<
"Failed to reformat raw string: "
2250 << llvm::toString(std::move(Err)) <<
"\n";
2255 *NewCode, FirstStartColumn, Style.
TabWidth, Encoding);
2256 State.Column = RawLastLineEndColumn + NewSuffixSize;
2260 unsigned PrefixExcessCharacters =
2265 ContentStartsOnNewline || (NewCode->find(
'\n') != std::string::npos);
2268 for (ParenState &
Paren : State.Stack)
2269 Paren.BreakBeforeParameter =
true;
2274unsigned ContinuationIndenter::addMultilineToken(
const FormatToken &Current,
2277 for (ParenState &
Paren : State.Stack)
2278 Paren.BreakBeforeParameter =
true;
2280 unsigned ColumnsUsed = State.Column;
2283 State.Column = Current.LastLineColumnWidth;
2290unsigned ContinuationIndenter::handleEndOfLine(
const FormatToken &Current,
2291 LineState &State,
bool DryRun,
2292 bool AllowBreak,
bool Newline) {
2293 unsigned Penalty = 0;
2296 auto RawStringStyle = getRawStringStyle(Current, State);
2297 if (RawStringStyle && !Current.Finalized) {
2298 Penalty = reformatRawStringLiteral(Current, State, *RawStringStyle, DryRun,
2300 }
else if (Current.IsMultiline && Current.isNot(TT_BlockComment)) {
2303 Penalty = addMultilineToken(Current, State);
2306 LineState OriginalState = State;
2310 bool Strict =
false;
2313 bool Exceeded =
false;
2314 std::tie(Penalty, Exceeded) = breakProtrudingToken(
2315 Current, State, AllowBreak,
true, Strict);
2319 LineState StrictState = OriginalState;
2320 unsigned StrictPenalty =
2321 breakProtrudingToken(Current, StrictState, AllowBreak,
2324 Strict = StrictPenalty <= Penalty;
2326 Penalty = StrictPenalty;
2327 State = StrictState;
2333 breakProtrudingToken(Current, OriginalState, AllowBreak,
false,
2338 unsigned ExcessCharacters = State.Column -
getColumnLimit(State);
2349 if (!Tok || Tok->isNot(tok::l_paren))
2354 if (Tok->is(TT_TemplateCloser)) {
2359 if (!Tok || Tok->isNot(tok::identifier))
2364std::optional<FormatStyle>
2365ContinuationIndenter::getRawStringStyle(
const FormatToken &Current,
2366 const LineState &State) {
2367 if (!Current.isStringLiteral())
2368 return std::nullopt;
2371 return std::nullopt;
2373 if (!RawStringStyle && Delimiter->empty()) {
2377 if (!RawStringStyle)
2378 return std::nullopt;
2380 return RawStringStyle;
2383std::unique_ptr<BreakableToken>
2384ContinuationIndenter::createBreakableToken(
const FormatToken &Current,
2385 LineState &State,
bool AllowBreak) {
2386 unsigned StartColumn = State.Column - Current.ColumnWidth;
2387 if (Current.isStringLiteral()) {
2404 if (Current.IsUnterminatedLiteral)
2408 if (State.Stack.back().IsInsideObjCArrayLiteral)
2415 if (Style.
isVerilog() && Current.Previous &&
2416 Current.Previous->isOneOf(tok::kw_export, Keywords.
kw_import)) {
2419 StringRef
Text = Current.TokenText;
2425 unsigned UnbreakableTailLength = (State.NextToken &&
canBreak(State))
2427 : Current.UnbreakableTailLength;
2433 Text.ends_with(
"'")) {
2435 }
else if (Style.
isCSharp() &&
Text.starts_with(
"@\"") &&
2436 Text.ends_with(
"\"")) {
2438 }
else if (
Text.starts_with(
"\"") &&
Text.ends_with(
"\"")) {
2443 return std::make_unique<BreakableStringLiteralUsingOperators>(
2444 Current, QuoteStyle,
2446 UnbreakableTailLength, State.Line->InPPDirective, Encoding, Style);
2455 if ((
Text.ends_with(Postfix =
"\"") &&
2456 (
Text.starts_with(Prefix =
"@\"") ||
Text.starts_with(Prefix =
"\"") ||
2457 Text.starts_with(Prefix =
"u\"") ||
2458 Text.starts_with(Prefix =
"U\"") ||
2459 Text.starts_with(Prefix =
"u8\"") ||
2460 Text.starts_with(Prefix =
"L\""))) ||
2461 (
Text.starts_with(Prefix =
"_T(\"") &&
2462 Text.ends_with(Postfix =
"\")"))) {
2463 return std::make_unique<BreakableStringLiteral>(
2464 Current, StartColumn, Prefix, Postfix, UnbreakableTailLength,
2465 State.Line->InPPDirective, Encoding, Style);
2467 }
else if (Current.is(TT_BlockComment)) {
2475 return std::make_unique<BreakableBlockComment>(
2476 Current, StartColumn, Current.OriginalColumn, !Current.Previous,
2477 State.Line->InPPDirective, Encoding, Style, Whitespaces.
useCRLF());
2478 }
else if (Current.is(TT_LineComment) &&
2479 (!Current.Previous ||
2480 Current.Previous->isNot(TT_ImplicitStringLiteral))) {
2481 bool RegularComments = [&]() {
2482 for (
const FormatToken *
T = &Current;
T &&
T->is(TT_LineComment);
2484 if (!(
T->TokenText.starts_with(
"//") ||
T->TokenText.starts_with(
"#")))
2490 CommentPragmasRegex.match(Current.TokenText.substr(2)) ||
2494 return std::make_unique<BreakableLineCommentSection>(
2495 Current, StartColumn,
false, Encoding, Style);
2500std::pair<unsigned, bool>
2501ContinuationIndenter::breakProtrudingToken(
const FormatToken &Current,
2502 LineState &State,
bool AllowBreak,
2503 bool DryRun,
bool Strict) {
2504 std::unique_ptr<const BreakableToken> Token =
2505 createBreakableToken(Current, State, AllowBreak);
2508 assert(Token->getLineCount() > 0);
2510 if (Current.is(TT_LineComment)) {
2514 if (ColumnLimit == 0) {
2517 ColumnLimit = std::numeric_limits<
decltype(ColumnLimit)>
::max();
2519 if (Current.UnbreakableTailLength >= ColumnLimit)
2523 unsigned StartColumn = State.Column - Current.ColumnWidth;
2524 unsigned NewBreakPenalty = Current.isStringLiteral()
2529 bool Exceeded =
false;
2531 bool BreakInserted = Token->introducesBreakBeforeToken();
2534 bool NewBreakBefore =
false;
2538 bool Reflow =
false;
2541 unsigned TailOffset = 0;
2543 unsigned ContentStartColumn =
2544 Token->getContentStartColumn(0,
false);
2546 unsigned RemainingTokenColumns =
2547 Token->getRemainingLength(0, TailOffset, ContentStartColumn);
2550 Token->adaptStartOfLine(0, Whitespaces);
2552 unsigned ContentIndent = 0;
2553 unsigned Penalty = 0;
2554 LLVM_DEBUG(llvm::dbgs() <<
"Breaking protruding token at column "
2555 << StartColumn <<
".\n");
2556 for (
unsigned LineIndex = 0, EndIndex = Token->getLineCount();
2557 LineIndex != EndIndex; ++LineIndex) {
2558 LLVM_DEBUG(llvm::dbgs()
2559 <<
" Line: " << LineIndex <<
" (Reflow: " << Reflow <<
")\n");
2560 NewBreakBefore =
false;
2564 bool TryReflow = Reflow;
2566 while (ContentStartColumn + RemainingTokenColumns > ColumnLimit) {
2567 LLVM_DEBUG(llvm::dbgs() <<
" Over limit, need: "
2568 << (ContentStartColumn + RemainingTokenColumns)
2569 <<
", space: " << ColumnLimit
2570 <<
", reflown prefix: " << ContentStartColumn
2571 <<
", offset in line: " << TailOffset <<
"\n");
2577 Token->getSplit(LineIndex, TailOffset, ColumnLimit,
2578 ContentStartColumn, CommentPragmasRegex);
2579 if (
Split.first == StringRef::npos) {
2582 if (LineIndex < EndIndex - 1) {
2586 (ContentStartColumn + RemainingTokenColumns - ColumnLimit);
2588 LLVM_DEBUG(llvm::dbgs() <<
" No break opportunity.\n");
2591 assert(
Split.first != 0);
2593 if (Token->supportsReflow()) {
2613 unsigned ToSplitColumns = Token->getRangeLength(
2614 LineIndex, TailOffset,
Split.first, ContentStartColumn);
2615 LLVM_DEBUG(llvm::dbgs() <<
" ToSplit: " << ToSplitColumns <<
"\n");
2618 LineIndex, TailOffset +
Split.first +
Split.second, ColumnLimit,
2619 ContentStartColumn + ToSplitColumns + 1, CommentPragmasRegex);
2622 unsigned ToNextSplitColumns = 0;
2623 if (NextSplit.first == StringRef::npos) {
2624 ToNextSplitColumns = Token->getRemainingLength(LineIndex, TailOffset,
2625 ContentStartColumn);
2627 ToNextSplitColumns = Token->getRangeLength(
2628 LineIndex, TailOffset,
2629 Split.first +
Split.second + NextSplit.first, ContentStartColumn);
2633 ToNextSplitColumns =
2634 Token->getLengthAfterCompression(ToNextSplitColumns, Split);
2635 LLVM_DEBUG(llvm::dbgs()
2636 <<
" ContentStartColumn: " << ContentStartColumn <<
"\n");
2637 LLVM_DEBUG(llvm::dbgs()
2638 <<
" ToNextSplit: " << ToNextSplitColumns <<
"\n");
2641 bool ContinueOnLine =
2642 ContentStartColumn + ToNextSplitColumns <= ColumnLimit;
2643 unsigned ExcessCharactersPenalty = 0;
2644 if (!ContinueOnLine && !Strict) {
2647 ExcessCharactersPenalty =
2648 (ContentStartColumn + ToNextSplitColumns - ColumnLimit) *
2650 LLVM_DEBUG(llvm::dbgs()
2651 <<
" Penalty excess: " << ExcessCharactersPenalty
2652 <<
"\n break : " << NewBreakPenalty <<
"\n");
2653 if (ExcessCharactersPenalty < NewBreakPenalty) {
2655 ContinueOnLine =
true;
2658 if (ContinueOnLine) {
2659 LLVM_DEBUG(llvm::dbgs() <<
" Continuing on line...\n");
2664 Token->compressWhitespace(LineIndex, TailOffset, Split,
2668 ContentStartColumn += ToSplitColumns + 1;
2669 Penalty += ExcessCharactersPenalty;
2671 RemainingTokenColumns = Token->getRemainingLength(
2672 LineIndex, TailOffset, ContentStartColumn);
2676 LLVM_DEBUG(llvm::dbgs() <<
" Breaking...\n");
2681 ContentIndent = Token->getContentIndent(LineIndex);
2682 LLVM_DEBUG(llvm::dbgs()
2683 <<
" ContentIndent: " << ContentIndent <<
"\n");
2684 ContentStartColumn = ContentIndent + Token->getContentStartColumn(
2687 unsigned NewRemainingTokenColumns = Token->getRemainingLength(
2688 LineIndex, TailOffset +
Split.first +
Split.second,
2689 ContentStartColumn);
2690 if (NewRemainingTokenColumns == 0) {
2693 ContentStartColumn =
2694 Token->getContentStartColumn(LineIndex,
true);
2695 NewRemainingTokenColumns = Token->getRemainingLength(
2696 LineIndex, TailOffset +
Split.first +
Split.second,
2697 ContentStartColumn);
2703 if (NewRemainingTokenColumns >= RemainingTokenColumns) {
2708 LLVM_DEBUG(llvm::dbgs() <<
" Breaking at: " << TailOffset +
Split.first
2709 <<
", " <<
Split.second <<
"\n");
2711 Token->insertBreak(LineIndex, TailOffset, Split, ContentIndent,
2715 Penalty += NewBreakPenalty;
2717 RemainingTokenColumns = NewRemainingTokenColumns;
2718 BreakInserted =
true;
2719 NewBreakBefore =
true;
2723 if (LineIndex + 1 != EndIndex) {
2724 unsigned NextLineIndex = LineIndex + 1;
2725 if (NewBreakBefore) {
2744 ContentStartColumn += RemainingTokenColumns + 1;
2749 Token->getReflowSplit(NextLineIndex, CommentPragmasRegex);
2750 LLVM_DEBUG(llvm::dbgs()
2751 <<
" Size of reflown text: " << ContentStartColumn
2752 <<
"\n Potential reflow split: ");
2753 if (SplitBeforeNext.first != StringRef::npos) {
2754 LLVM_DEBUG(llvm::dbgs() << SplitBeforeNext.first <<
", "
2755 << SplitBeforeNext.second <<
"\n");
2756 TailOffset = SplitBeforeNext.first + SplitBeforeNext.second;
2759 RemainingTokenColumns = Token->getRemainingLength(
2760 NextLineIndex, TailOffset, ContentStartColumn);
2762 if (ContentStartColumn + RemainingTokenColumns > ColumnLimit) {
2763 LLVM_DEBUG(llvm::dbgs()
2764 <<
" Over limit after reflow, need: "
2765 << (ContentStartColumn + RemainingTokenColumns)
2766 <<
", space: " << ColumnLimit
2767 <<
", reflown prefix: " << ContentStartColumn
2768 <<
", offset in line: " << TailOffset <<
"\n");
2774 Token->getSplit(NextLineIndex, TailOffset, ColumnLimit,
2775 ContentStartColumn, CommentPragmasRegex);
2776 if (
Split.first == StringRef::npos) {
2777 LLVM_DEBUG(llvm::dbgs() <<
" Did not find later break\n");
2783 unsigned ToSplitColumns = Token->getRangeLength(
2784 NextLineIndex, TailOffset,
Split.first, ContentStartColumn);
2785 if (ContentStartColumn + ToSplitColumns > ColumnLimit) {
2786 LLVM_DEBUG(llvm::dbgs() <<
" Next split protrudes, need: "
2787 << (ContentStartColumn + ToSplitColumns)
2788 <<
", space: " << ColumnLimit);
2789 unsigned ExcessCharactersPenalty =
2790 (ContentStartColumn + ToSplitColumns - ColumnLimit) *
2792 if (NewBreakPenalty < ExcessCharactersPenalty)
2798 LLVM_DEBUG(llvm::dbgs() <<
"not found.\n");
2806 ContentStartColumn =
2807 Token->getContentStartColumn(NextLineIndex,
false);
2808 RemainingTokenColumns = Token->getRemainingLength(
2809 NextLineIndex, TailOffset, ContentStartColumn);
2812 Token->adaptStartOfLine(NextLineIndex, Whitespaces);
2827 if (NewBreakBefore) {
2828 assert(Penalty >= NewBreakPenalty);
2829 Penalty -= NewBreakPenalty;
2832 Token->reflow(NextLineIndex, Whitespaces);
2838 Token->getSplitAfterLastLine(TailOffset);
2839 if (SplitAfterLastLine.first != StringRef::npos) {
2840 LLVM_DEBUG(llvm::dbgs() <<
"Replacing whitespace after last line.\n");
2845 (ContentStartColumn + RemainingTokenColumns - ColumnLimit);
2848 Token->replaceWhitespaceAfterLastLine(TailOffset, SplitAfterLastLine,
2851 ContentStartColumn =
2852 Token->getContentStartColumn(Token->getLineCount() - 1,
true);
2853 RemainingTokenColumns = Token->getRemainingLength(
2854 Token->getLineCount() - 1,
2855 TailOffset + SplitAfterLastLine.first + SplitAfterLastLine.second,
2856 ContentStartColumn);
2859 State.Column = ContentStartColumn + RemainingTokenColumns -
2860 Current.UnbreakableTailLength;
2862 if (BreakInserted) {
2864 Token->updateAfterBroken(Whitespaces);
2869 if (Current.isNot(TT_LineComment))
2870 for (ParenState &
Paren : State.Stack)
2871 Paren.BreakBeforeParameter =
true;
2873 if (Current.is(TT_BlockComment))
2874 State.NoContinuation =
true;
2876 State.Stack.back().LastSpace = StartColumn;
2879 Token->updateNextToken(State);
2881 return {Penalty, Exceeded};
2886 return Style.
ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
2889bool ContinuationIndenter::nextIsMultilineString(
const LineState &State) {
2891 if (!Current.isStringLiteral() || Current.is(TT_ImplicitStringLiteral))
2896 if (Current.TokenText.starts_with(
"R\""))
2898 if (Current.IsMultiline)
2900 if (Current.getNextNonComment() &&
2901 Current.getNextNonComment()->isStringLiteral()) {
2905 State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
Declares BreakableToken, BreakableStringLiteral, BreakableComment, BreakableBlockComment and Breakabl...
This file implements an indenter that manages the indentation of continuations.
Defines and computes precedence levels for binary/ternary operators.
Defines the SourceManager interface.
unsigned LongestObjCSelectorName
Defines the clang::TokenKind enum and support functions.
WhitespaceManager class manages whitespace around tokens and their replacements.
__DEVICE__ int max(int __a, int __b)
This class handles loading and caching of source files into memory.
unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
Token - This structure provides full information about a lexed token.
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)) {....
The JSON file list parser is used to communicate input to InstallAPI.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
Language
The language for the input, used to select and validate the language standard and possible actions.
const FunctionProtoType * T