Lines Matching +full:right +full:- +full:aligned

1 //===--- WhitespaceManager.cpp - Format C++ code --------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
12 //===----------------------------------------------------------------------===//
57 if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
68 if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
92 if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
134 auto &P = Changes[I - 1];
149 SourceMgr.getCharacterData(OriginalWhitespaceStart) -
174 PrevTokLength = OriginalWhitespaceStartOffset -
178 PrevTokLength = std::min(PrevTokLength, P.Tok->ColumnWidth);
186 LastOutsideTokenChange->TokenLength += PrevTokLength + P.Spaces;
193 (C.NewlinesBefore > 0 || C.Tok->is(tok::eof) ||
194 (C.IsInsideToken && C.Tok->is(tok::comment))) &&
195 P.Tok->is(tok::comment) &&
205 // - the first, delimited by (), for the whitespace between the tokens,
206 // - and second, delimited by [], for the whitespace at the beginning
231 Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
237 // to be aligned.
242 if (Change.Tok->is(tok::comment)) {
243 if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken) {
247 Change.StartOfTokenColumn -
248 Change.StartOfBlockComment->StartOfTokenColumn;
262 for (unsigned i = 0, e = Change.Tok->FakeLParens.size(); i != e; ++i) {
264 Change.Tok->FakeLParens[e - 1 - i] == prec::Conditional &&
265 !(i == 0 && Change.Tok->Previous &&
266 Change.Tok->Previous->is(TT_ConditionalExpr) &&
267 Change.Tok->Previous->is(tok::colon));
275 for (unsigned i = Change.Tok->FakeRParens; i > 0 && ScopeStack.size(); --i)
277 --ConditionalsLevel;
282 // Column - The token for which Matches returns true is moved to this column.
283 // RightJustify - Whether it is the token's right end or left end that gets
295 // We only run the "Matches" function on tokens from the outer-most scope.
297 // that are not in the outer-most scope, and that is function parameters
323 // Compare current token to previous non-comment token to ensure whether
325 unsigned PreviousNonComment = i - 1;
327 Changes[PreviousNonComment].Tok->is(tok::comment)) {
328 --PreviousNonComment;
337 CurrentChange.Tok->is(tok::string_literal) &&
338 Changes[i - 1].Tok->is(tok::string_literal);
346 // If this is the first matching token to be aligned, remember by how many
351 Shift = Column - (RightJustify ? CurrentChange.TokenLength : 0) -
359 static_cast<int>(CurrentChange.Tok->SpacesRequiredBefore));
372 if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
376 if (Changes[ScopeStart - 1].Tok->is(TT_LambdaLBrace))
381 Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) {
387 Changes[ScopeStart - 2].Tok->isOneOf(tok::identifier,
389 Changes[ScopeStart - 1].Tok->is(tok::l_paren) &&
390 Changes[ScopeStart].Tok->isNot(TT_LambdaLSquare)) {
391 if (CurrentChange.Tok->MatchingParen &&
392 CurrentChange.Tok->MatchingParen->is(TT_LambdaLBrace)) {
397 if (CurrentChange.Tok->is(tok::l_brace) &&
398 CurrentChange.Tok->is(BK_BracedInit)) {
405 if (CurrentChange.Tok->is(TT_ConditionalExpr))
409 if (CurrentChange.Tok->is(TT_DesignatedInitializerPeriod))
413 if (CurrentChange.Tok->Previous &&
414 CurrentChange.Tok->Previous->is(TT_ConditionalExpr)) {
418 // Continued direct-list-initialization using braced list.
420 Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
421 Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
422 CurrentChange.Tok->is(tok::l_brace) &&
423 CurrentChange.Tok->is(BK_BracedInit)) {
429 Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
430 Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
431 CurrentChange.Tok->isNot(tok::r_brace)) {
435 Changes[OuterScopeStart - 1].Tok->is(TT_LambdaLBrace)) {
445 if (Changes[ScopeStart - 1].Tok->is(TT_TemplateOpener))
461 static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) ||
462 CurrentChange.Tok->is(tok::eof));
473 !CurrentChange.Tok->isOneOf(tok::equal, tok::r_paren,
478 for (int Previous = i - 1;
479 Previous >= 0 && Changes[Previous].Tok->is(TT_PointerOrReference);
480 --Previous) {
481 assert(Changes[Previous].Tok->isPointerOrReference());
482 if (Changes[Previous].Tok->isNot(tok::star)) {
488 Changes[Previous + 1].Spaces -= Shift;
501 // non-matching tokens, finalize the previous sequence.
509 // with the alignment requirements of the nested sub-level. This recursive
514 // There is a non-obvious subtlety in the recursive behavior: Even though we
524 // right-justified. It is used to align compound assignments like `+=` and `=`.
526 // be aligned will be padded on the left to the same length before aligning.
533 // We arrange each line in 3 parts. The operator to be aligned (the anchor),
534 // and text to its left and right. In the aligned text the width of each part
535 // will be the maximum of that over the block that has been aligned. Maximum
537 // is false, the part from start of line to the right end of the anchor.
540 // the left to right-justify the anchor.
542 // The operator to be aligned when RightJustify is true and ACS.PadOperators
545 // Width to the right of the anchor. Plus width of the anchor when
574 // first token of the first line that doesn't need to be aligned.
577 // containing any matching token to be aligned and located after such token.
613 // A new line starts, re-initialize line status tracking bools.
615 if (i == 0 || CurrentChange.Tok->isNot(tok::string_literal) ||
616 Changes[i - 1].Tok->isNot(tok::string_literal)) {
622 if (CurrentChange.Tok->isNot(tok::comment))
625 if (CurrentChange.Tok->is(tok::comma)) {
631 i = StoppedAt - 1;
697 // first token of the first line that doesn't need to be aligned.
700 // containing any matching token to be aligned and located after such token.
715 // If this is the first matching token to be aligned, remember by how many
720 Shift = MinColumn - Changes[I].StartOfTokenColumn;
744 if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
747 Current = Current->Previous;
751 if (Current->is(tok::r_paren) && Current->MatchingParen) {
752 Current = Current->MatchingParen->Previous;
756 if (!Current || Current->isNot(tok::identifier))
759 if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
766 return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
801 // A new line starts, re-initialize line status tracking bools.
806 if (Changes[I].Tok->isNot(tok::comment))
838 if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
842 FormatToken *Previous = C.Tok->getPreviousNonComment();
843 if (Previous && Previous->is(tok::kw_operator))
847 ? C.Tok->getPrecedence() == prec::Assignment
848 : (C.Tok->is(tok::equal) ||
850 // it is aligned even when the AlignCompound option is not
852 (Style.isVerilog() && C.Tok->is(tok::lessequal) &&
853 C.Tok->getPrecedence() == prec::Assignment));
876 if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
879 return C.Tok->is(Type);
898 return C.Tok->is(Type);
904 return !C.IsInsideToken && C.Tok->Previous && C.Tok->Previous->is(Type);
946 // A new line starts, re-initialize line status tracking bools.
952 if (Changes[I].Tok->isNot(tok::comment))
955 if (Changes[I].Tok->is(Type)) {
957 !Changes[I].Tok->Next || Changes[I].Tok->Next->isTrailingComment();
1016 for (const auto *Prev = C.Tok->Previous; Prev; Prev = Prev->Previous)
1017 if (Prev->is(tok::equal))
1019 if (C.Tok->is(TT_FunctionTypeLParen))
1022 if (C.Tok->is(TT_FunctionDeclarationName))
1024 if (C.Tok->isNot(TT_StartOfName))
1026 if (C.Tok->Previous &&
1027 C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
1030 for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
1031 if (Next->is(tok::comment))
1033 if (Next->is(TT_PointerOrReference))
1035 if (!Next->Tok.getIdentifierInfo())
1037 if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
1053 return C.Tok->is(TT_ConditionalExpr) &&
1054 ((C.Tok->is(tok::question) && !C.NewlinesBefore) ||
1055 (C.Tok->is(tok::colon) && C.Tok->Next &&
1056 (C.Tok->Next->FakeLParens.size() == 0 ||
1057 C.Tok->Next->FakeLParens.back() != prec::Conditional)));
1062 FormatToken *Previous = C.Tok->getPreviousNonComment();
1063 return C.NewlinesBefore && Previous && Previous->is(TT_ConditionalExpr) &&
1064 (Previous->is(tok::colon) &&
1065 (C.Tok->FakeLParens.size() == 0 ||
1066 C.Tok->FakeLParens.back() != prec::Conditional));
1068 // Ensure we keep alignment of wrapped operands with non-wrapped operands
1070 // extra offset to be properly aligned.
1073 C.StartOfTokenColumn -= 2;
1080 return (C.Tok->is(TT_ConditionalExpr) && C.Tok->is(tok::question) &&
1081 &C != &Changes.back() && (&C + 1)->NewlinesBefore == 0 &&
1082 !(&C + 1)->IsTrailingComment) ||
1111 C.OriginalWhitespaceRange.getEnd().getRawEncoding() -
1112 C.OriginalWhitespaceRange.getBegin().getRawEncoding() -
1113 C.Tok->LastNewlineOffset;
1121 C.Spaces = C.NewlinesBefore > 0 ? C.Tok->OriginalColumn : OriginalSpaces;
1135 ChangeMaxColumn = Style.ColumnLimit - C.TokenLength;
1141 ChangeMaxColumn -= 2;
1149 if (Changes[J].Tok->is(tok::comment))
1154 // The start of the next token was previously aligned with the
1166 if (Tok->is(tok::semi)) {
1167 Tok = Tok->getPreviousNonComment();
1171 if (Tok->is(tok::r_paren)) {
1173 Tok = Tok->MatchingParen;
1176 Tok = Tok->getPreviousNonComment();
1179 if (Tok->is(TT_DoWhile)) {
1180 const auto *Prev = Tok->getPreviousNonComment();
1182 // A do-while-loop without braces.
1189 if (Tok->isNot(tok::r_brace))
1192 while (Tok->Previous && Tok->Previous->is(tok::r_brace))
1193 Tok = Tok->Previous;
1194 return Tok->NewlinesBefore > 0;
1198 DontAlignThisComment(Changes[I - 1].Tok)) {
1210 !Changes[I - 1].IsTrailingComment) ||
1234 Shift = Column - Changes[i].StartOfTokenColumn;
1237 Changes[i].StartOfBlockComment->StartOfTokenColumn -
1261 if (C.NewlinesBefore == 0 && (!WithLastLine || C.Tok->isNot(tok::eof)))
1299 if (C.Tok->IsArrayInitializer) {
1303 if (Changes[InsideIndex].Tok == C.Tok->MatchingParen) {
1339 if (Changes[CellIter->Index].Tok->is(tok::r_brace)) {
1345 const FormatToken *Previous = Changes[Next->Index].Tok->Previous;
1346 if (Previous && Previous->isNot(TT_LineComment)) {
1347 Changes[Next->Index].Spaces = BracePadding;
1348 Changes[Next->Index].NewlinesBefore = 0;
1350 Next = Next->NextColumnElement;
1361 Changes[CellIter->Index].Spaces = (MaxNetWidth - ThisNetWidth);
1364 for (const auto *Next = CellIter->NextColumnElement; Next;
1365 Next = Next->NextColumnElement) {
1372 Changes[Next->Index].Spaces = (MaxNetWidth - ThisNetWidth);
1378 calculateCellWidth(CellIter->Index, CellIter->EndIndex, true) +
1380 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1381 Changes[CellIter->Index].Spaces = (CellWidth - (ThisWidth + NetWidth));
1382 Changes[CellIter->Index].Spaces += (i > 0) ? 1 : BracePadding;
1384 alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
1385 for (const auto *Next = CellIter->NextColumnElement; Next;
1386 Next = Next->NextColumnElement) {
1388 calculateCellWidth(Next->Index, Next->EndIndex, true) + NetWidth;
1389 if (Changes[Next->Index].NewlinesBefore == 0) {
1390 Changes[Next->Index].Spaces = (CellWidth - ThisWidth);
1391 Changes[Next->Index].Spaces += (i > 0) ? 1 : BracePadding;
1393 alignToStartOfCell(Next->Index, Next->EndIndex);
1410 for (const auto *Next = CellIter; Next; Next = Next->NextColumnElement) {
1411 auto &Change = Changes[Next->Index];
1422 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1423 Changes[CellIter->Index].Spaces =
1424 MaxNetWidth - ThisNetWidth +
1425 (Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1
1430 for (const auto *Next = CellIter->NextColumnElement; Next;
1431 Next = Next->NextColumnElement) {
1437 if (Changes[Next->Index].NewlinesBefore == 0) {
1438 Changes[Next->Index].Spaces =
1439 MaxNetWidth - ThisNetWidth +
1440 (Changes[Next->Index].Tok->isNot(tok::r_brace) ? 1 : BracePadding);
1451 Next = Next->NextColumnElement) {
1452 if (Next->HasSplit)
1471 if (C.Tok->is(tok::l_brace))
1473 else if (C.Tok->is(tok::r_brace))
1474 --Depth;
1476 if (C.Tok->is(tok::l_brace)) {
1478 MatchingParen = C.Tok->MatchingParen;
1482 auto j = i - 1;
1483 for (; Changes[j].NewlinesBefore == 0 && j > Start; --j) {
1492 } else if (C.Tok->is(tok::comma)) {
1495 if (const auto *Next = C.Tok->getNextNonComment();
1496 Next && Next->isNot(tok::r_brace)) { // dangling comma
1505 CellCounts.push_back(C.Tok->Previous->isNot(tok::comma) ? Cell + 1
1507 // Go to the next non-comment and ensure there is a break in front
1508 const auto *NextNonComment = C.Tok->getNextNonComment();
1509 while (NextNonComment && NextNonComment->is(tok::comma))
1510 NextNonComment = NextNonComment->getNextNonComment();
1515 Changes[j].Tok->isNot(tok::r_brace)) {
1518 Changes[j].Spaces = InitialSpaces - InitialTokenLength;
1520 } else if (C.Tok->is(tok::comment) && C.Tok->NewlinesBefore == 0) {
1522 C.Spaces = Changes[i - 1].Tok->is(tok::comma) ? 1 : 2;
1523 } else if (C.Tok->is(tok::l_brace)) {
1524 // We need to make sure that the ending braces is aligned to the
1526 auto j = i - 1;
1527 for (; j > 0 && !Changes[j].Tok->ArrayInitializerLineStart; --j)
1531 } else if (Depth == 0 && C.Tok->is(tok::r_brace)) {
1535 if (C.Tok->StartsColumn) {
1554 auto j = i - 1;
1555 if ((j - 1) > Start && Changes[j].Tok->is(tok::comma) &&
1556 Changes[j - 1].NewlinesBefore > 0) {
1557 --j;
1571 --i;
1592 if ((End - Start) <= 1)
1595 // is aligned to the parent
1605 if (!CellIter->NextColumnElement && (CellIter + 1) != Cells.end()) {
1607 if (NextIter->Cell == CellIter->Cell) {
1608 CellIter->NextColumnElement = &(*NextIter);
1621 auto Last = Changes[i - 1].OriginalWhitespaceRange;
1625 // between 2 tokens and another non-empty range at the start of the second
1663 // replacement for a non-empty source range.
1682 ReplacementText, C.Tok->IndentLevel, std::max(0, C.Spaces),
1683 std::max((int)C.StartOfTokenColumn, C.Spaces) - std::max(0, C.Spaces),
1692 unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
1725 std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
1729 Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
1745 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
1753 Spaces -= FirstTabWidth;
1795 Spaces -= Tabs * Style.TabWidth;