clang 23.0.0git
WhitespaceManager.cpp
Go to the documentation of this file.
1//===--- WhitespaceManager.cpp - Format C++ code --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements WhitespaceManager class.
11///
12//===----------------------------------------------------------------------===//
13
14#include "WhitespaceManager.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SmallVector.h"
17#include <algorithm>
18#include <optional>
19
20namespace clang {
21namespace format {
22
24 const Change &C1, const Change &C2) const {
25 return SourceMgr.isBeforeInTranslationUnit(
30 SourceMgr.isBeforeInTranslationUnit(
33}
34
55
57 unsigned Spaces,
58 unsigned StartOfTokenColumn,
59 bool IsAligned, bool InPPDirective,
60 unsigned IndentedFromColumn) {
61 if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
62 return;
63 Tok.setDecision((Newlines > 0) ? FD_Break : FD_Continue);
64 Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
65 Spaces, StartOfTokenColumn, IndentedFromColumn,
66 Newlines, "", "", IsAligned,
67 InPPDirective && !Tok.IsFirst,
68 /*IsInsideToken=*/false));
69}
70
72 bool InPPDirective) {
73 if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
74 return;
75 Changes.push_back(Change(
76 Tok, /*CreateReplacement=*/false, Tok.WhitespaceRange, /*Spaces=*/0,
77 Tok.OriginalColumn, /*IndentedFromColumn=*/0, Tok.NewlinesBefore, "", "",
78 /*IsAligned=*/false, InPPDirective && !Tok.IsFirst,
79 /*IsInsideToken=*/false));
80}
81
82llvm::Error
84 return Replaces.add(Replacement);
85}
86
87bool WhitespaceManager::inputUsesCRLF(StringRef Text, bool DefaultToCRLF) {
88 size_t LF = Text.count('\n');
89 size_t CR = Text.count('\r') * 2;
90 return LF == CR ? DefaultToCRLF : CR > LF;
91}
92
94 const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
95 StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
96 unsigned Newlines, int Spaces) {
97 if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
98 return;
99 SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
100 Changes.push_back(
101 Change(Tok, /*CreateReplacement=*/true,
102 SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
103 std::max(0, Spaces), /*IndentedFromColumn=*/0, Newlines,
104 PreviousPostfix, CurrentPrefix,
105 /*IsAligned=*/true, InPPDirective && !Tok.IsFirst,
106 /*IsInsideToken=*/true));
107}
108
110 if (Changes.empty())
111 return Replaces;
112
113 llvm::sort(Changes, Change::IsBeforeInFile(SourceMgr));
114 calculateLineBreakInformation();
115 alignConsecutiveMacros();
116 alignConsecutiveShortCaseStatements(/*IsExpr=*/true);
117 alignConsecutiveShortCaseStatements(/*IsExpr=*/false);
118 alignConsecutiveDeclarations();
119 alignConsecutiveBitFields();
120 alignConsecutiveAssignments();
121 if (Style.isTableGen()) {
122 alignConsecutiveTableGenBreakingDAGArgColons();
123 alignConsecutiveTableGenCondOperatorColons();
124 alignConsecutiveTableGenDefinitions();
125 }
126 alignChainedConditionals();
127 alignTrailingComments();
128 alignEscapedNewlines();
129 alignArrayInitializers();
130 generateChanges();
131
132 return Replaces;
133}
134
135void WhitespaceManager::calculateLineBreakInformation() {
136 Changes[0].PreviousEndOfTokenColumn = 0;
137 Change *LastOutsideTokenChange = &Changes[0];
138 for (unsigned I = 1, e = Changes.size(); I != e; ++I) {
139 auto &C = Changes[I];
140 auto &P = Changes[I - 1];
141 auto &PrevTokLength = P.TokenLength;
142 SourceLocation OriginalWhitespaceStart =
143 C.OriginalWhitespaceRange.getBegin();
144 SourceLocation PreviousOriginalWhitespaceEnd =
145 P.OriginalWhitespaceRange.getEnd();
146 unsigned OriginalWhitespaceStartOffset =
147 SourceMgr.getFileOffset(OriginalWhitespaceStart);
148 unsigned PreviousOriginalWhitespaceEndOffset =
149 SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
150 assert(PreviousOriginalWhitespaceEndOffset <=
151 OriginalWhitespaceStartOffset);
152 const char *const PreviousOriginalWhitespaceEndData =
153 SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
154 StringRef Text(PreviousOriginalWhitespaceEndData,
155 SourceMgr.getCharacterData(OriginalWhitespaceStart) -
156 PreviousOriginalWhitespaceEndData);
157 // Usually consecutive changes would occur in consecutive tokens. This is
158 // not the case however when analyzing some preprocessor runs of the
159 // annotated lines. For example, in this code:
160 //
161 // #if A // line 1
162 // int i = 1;
163 // #else B // line 2
164 // int i = 2;
165 // #endif // line 3
166 //
167 // one of the runs will produce the sequence of lines marked with line 1, 2
168 // and 3. So the two consecutive whitespace changes just before '// line 2'
169 // and before '#endif // line 3' span multiple lines and tokens:
170 //
171 // #else B{change X}[// line 2
172 // int i = 2;
173 // ]{change Y}#endif // line 3
174 //
175 // For this reason, if the text between consecutive changes spans multiple
176 // newlines, the token length must be adjusted to the end of the original
177 // line of the token.
178 auto NewlinePos = Text.find_first_of('\n');
179 if (NewlinePos == StringRef::npos) {
180 PrevTokLength = OriginalWhitespaceStartOffset -
181 PreviousOriginalWhitespaceEndOffset +
182 C.PreviousLinePostfix.size() + P.CurrentLinePrefix.size();
183 if (!P.IsInsideToken)
184 PrevTokLength = std::min(PrevTokLength, P.Tok->ColumnWidth);
185 } else {
186 PrevTokLength = NewlinePos + P.CurrentLinePrefix.size();
187 }
188
189 // If there are multiple changes in this token, sum up all the changes until
190 // the end of the line.
191 if (P.IsInsideToken && P.NewlinesBefore == 0)
192 LastOutsideTokenChange->TokenLength += PrevTokLength + P.Spaces;
193 else
194 LastOutsideTokenChange = &P;
195
196 C.PreviousEndOfTokenColumn = P.StartOfTokenColumn + PrevTokLength;
197
198 P.IsTrailingComment =
199 (C.NewlinesBefore > 0 || C.Tok->is(tok::eof) ||
200 (C.IsInsideToken && C.Tok->is(tok::comment))) &&
201 P.Tok->is(tok::comment) &&
202 // FIXME: This is a dirty hack. The problem is that
203 // BreakableLineCommentSection does comment reflow changes and here is
204 // the aligning of trailing comments. Consider the case where we reflow
205 // the second line up in this example:
206 //
207 // // line 1
208 // // line 2
209 //
210 // That amounts to 2 changes by BreakableLineCommentSection:
211 // - the first, delimited by (), for the whitespace between the tokens,
212 // - and second, delimited by [], for the whitespace at the beginning
213 // of the second token:
214 //
215 // // line 1(
216 // )[// ]line 2
217 //
218 // So in the end we have two changes like this:
219 //
220 // // line1()[ ]line 2
221 //
222 // Note that the OriginalWhitespaceStart of the second change is the
223 // same as the PreviousOriginalWhitespaceEnd of the first change.
224 // In this case, the below check ensures that the second change doesn't
225 // get treated as a trailing comment change here, since this might
226 // trigger additional whitespace to be wrongly inserted before "line 2"
227 // by the comment aligner here.
228 //
229 // For a proper solution we need a mechanism to say to WhitespaceManager
230 // that a particular change breaks the current sequence of trailing
231 // comments.
232 OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
233 }
234 // FIXME: The last token is currently not always an eof token; in those
235 // cases, setting TokenLength of the last token to 0 is wrong.
236 Changes.back().TokenLength = 0;
237 Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
238
239 const WhitespaceManager::Change *LastBlockComment = nullptr;
240 for (auto &Change : Changes) {
241 // Reset the IsTrailingComment flag for changes inside of trailing comments
242 // so they don't get realigned later. Comment line breaks however still need
243 // to be aligned.
246 Change.StartOfBlockComment = nullptr;
248 if (Change.Tok->is(tok::comment)) {
249 if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken) {
250 LastBlockComment = &Change;
251 } else if ((Change.StartOfBlockComment = LastBlockComment)) {
255 }
256 } else {
257 LastBlockComment = nullptr;
258 }
259 }
260
261 // Compute conditional nesting level
262 // Level is increased for each conditional, unless this conditional continues
263 // a chain of conditional, i.e. starts immediately after the colon of another
264 // conditional.
265 SmallVector<bool, 16> ScopeStack;
266 int ConditionalsLevel = 0;
267 for (auto &Change : Changes) {
268 for (unsigned i = 0, e = Change.Tok->FakeLParens.size(); i != e; ++i) {
269 bool isNestedConditional =
270 Change.Tok->FakeLParens[e - 1 - i] == prec::Conditional &&
271 !(i == 0 && Change.Tok->Previous &&
272 Change.Tok->Previous->is(TT_ConditionalExpr) &&
273 Change.Tok->Previous->is(tok::colon));
274 if (isNestedConditional)
275 ++ConditionalsLevel;
276 ScopeStack.push_back(isNestedConditional);
277 }
278
279 Change.ConditionalsLevel = ConditionalsLevel;
280
281 for (unsigned i = Change.Tok->FakeRParens; i > 0 && ScopeStack.size(); --i)
282 if (ScopeStack.pop_back_val())
283 --ConditionalsLevel;
284 }
285}
286
287// Sets the spaces in front of a Change, and updates the start/end columns of
288// subsequent tokens so that trailing comments and escaped newlines can be
289// aligned properly.
290static void
291SetChangeSpaces(unsigned Start, unsigned Spaces,
293 auto &FirstChange = Changes[Start];
294 const int ColumnChange = Spaces - FirstChange.Spaces;
295
296 if (ColumnChange == 0)
297 return;
298
299 FirstChange.Spaces += ColumnChange;
300 FirstChange.StartOfTokenColumn += ColumnChange;
301
302 for (auto I = Start + 1; I < Changes.size(); I++) {
303 auto &Change = Changes[I];
304
305 Change.PreviousEndOfTokenColumn += ColumnChange;
306
307 if (Change.NewlinesBefore > 0)
308 break;
309
310 Change.StartOfTokenColumn += ColumnChange;
311 }
312}
313
314// Changes the spaces in front of a change by Delta, and updates the start/end
315// columns of subsequent tokens so that trailing comments and escaped newlines
316// can be aligned properly.
317static void
318IncrementChangeSpaces(unsigned Start, int Delta,
320 assert(Delta > 0 || (abs(Delta) <= Changes[Start].Spaces));
321 SetChangeSpaces(Start, Changes[Start].Spaces + Delta, Changes);
322}
323
324// Align a single sequence of tokens, see AlignTokens below.
325// Column - The tokens indexed in Matches are moved to this column.
326// RightJustify - Whether it is the token's right end or left end that gets
327// moved to that column.
328static void
329AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
330 unsigned Column, bool RightJustify,
331 ArrayRef<unsigned> Matches,
333 unsigned OriginalMatchColumn = 0;
334 int Shift = 0;
335
336 // ScopeStack keeps track of the current scope depth. It contains the levels
337 // of at most 2 scopes. The first one is the one that the matched token is
338 // in. The second one is the one that should not be moved by this procedure.
339 // The "Matches" indices should only have tokens from the outer-most scope.
340 // However, we do need to pay special attention to one class of tokens
341 // that are not in the outer-most scope, and that is the continuations of an
342 // unwrapped line whose positions are derived from a token to the right of the
343 // aligned token, as illustrated by this example:
344 // double a(int x);
345 // int b(int y,
346 // double z);
347 // In the above example, we need to take special care to ensure that
348 // 'double z' is indented along with its owning function 'b', because its
349 // position is derived from the '(' token to the right of the 'b' token.
350 // The same holds for calling a function:
351 // double a = foo(x);
352 // int b = bar(foo(y),
353 // foor(z));
354 // Similar for broken string literals:
355 // double x = 3.14;
356 // auto s = "Hello"
357 // "World";
358 // Special handling is required for 'nested' ternary operators.
360
361 for (unsigned i = Start; i != End; ++i) {
362 auto &CurrentChange = Changes[i];
363 if (!Matches.empty() && Matches[0] < i)
364 Matches.consume_front();
365 assert(Matches.empty() || Matches[0] >= i);
366 while (!ScopeStack.empty() &&
367 CurrentChange.indentAndNestingLevel() < ScopeStack.back()) {
368 ScopeStack.pop_back();
369 }
370
371 // Keep track of the level that should not move with the aligned token.
372 if (ScopeStack.size() == 1u && CurrentChange.NewlinesBefore != 0u &&
373 CurrentChange.indentAndNestingLevel() > ScopeStack[0] &&
374 CurrentChange.IndentedFromColumn < OriginalMatchColumn) {
375 ScopeStack.push_back(CurrentChange.indentAndNestingLevel());
376 }
377
378 bool InsideNestedScope =
379 !ScopeStack.empty() &&
380 (CurrentChange.indentAndNestingLevel() > ScopeStack[0] ||
381 (CurrentChange.indentAndNestingLevel() == ScopeStack[0] &&
382 CurrentChange.IndentedFromColumn >= OriginalMatchColumn));
383
384 if (CurrentChange.NewlinesBefore > 0 && !InsideNestedScope)
385 Shift = 0;
386
387 // If this is the first matching token to be aligned, remember by how many
388 // spaces it has to be shifted, so the rest of the changes on the line are
389 // shifted by the same amount
390 if (!Matches.empty() && Matches[0] == i) {
391 OriginalMatchColumn = CurrentChange.StartOfTokenColumn;
392 Shift = Column - (RightJustify ? CurrentChange.TokenLength : 0) -
393 CurrentChange.StartOfTokenColumn;
394 ScopeStack = {CurrentChange.indentAndNestingLevel()};
395 }
396
397 if (Shift == 0)
398 continue;
399
400 // This is for lines that are split across multiple lines, as mentioned in
401 // the ScopeStack comment. The stack size being 1 means that the token is
402 // not in a scope that should not move.
403 if ((!Matches.empty() && Matches[0] == i) ||
404 (ScopeStack.size() == 1u && CurrentChange.NewlinesBefore > 0 &&
405 InsideNestedScope)) {
406 CurrentChange.IndentedFromColumn += Shift;
407 IncrementChangeSpaces(i, Shift, Changes);
408 }
409
410 // We should not remove required spaces unless we break the line before.
411 assert(Shift > 0 || Changes[i].NewlinesBefore > 0 ||
412 CurrentChange.Spaces >=
413 static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) ||
414 CurrentChange.Tok->is(tok::eof));
415
416 // If PointerAlignment is PAS_Right, keep *s or &s next to the token,
417 // except if the token is equal, then a space is needed.
418 if ((Style.PointerAlignment == FormatStyle::PAS_Right ||
419 Style.ReferenceAlignment == FormatStyle::RAS_Right) &&
420 CurrentChange.Spaces != 0 &&
421 CurrentChange.Tok->isNoneOf(tok::equal, tok::r_paren,
422 TT_TemplateCloser)) {
423 const bool ReferenceNotRightAligned =
424 Style.ReferenceAlignment != FormatStyle::RAS_Right &&
425 Style.ReferenceAlignment != FormatStyle::RAS_Pointer;
426 for (int Previous = i - 1;
427 Previous >= 0 && Changes[Previous].Tok->is(TT_PointerOrReference);
428 --Previous) {
429 assert(Changes[Previous].Tok->isPointerOrReference());
430 if (Changes[Previous].Tok->isNot(tok::star)) {
431 if (ReferenceNotRightAligned)
432 continue;
433 } else if (Style.PointerAlignment != FormatStyle::PAS_Right) {
434 continue;
435 }
436
437 IncrementChangeSpaces(Previous + 1, -Shift, Changes);
438 IncrementChangeSpaces(Previous, Shift, Changes);
439 }
440 }
441 }
442}
443
444// Walk through a subset of the changes, starting at StartAt, and find
445// sequences of matching tokens to align. To do so, keep track of the lines and
446// whether or not a matching token was found on a line. If a matching token is
447// found, extend the current sequence. If the current line cannot be part of a
448// sequence, e.g. because there is an empty line before it or it contains only
449// non-matching tokens, finalize the previous sequence.
450// The value returned is the token on which we stopped, either because we
451// exhausted all items inside Changes, or because we hit a scope level higher
452// than our initial scope.
453// This function is recursive. Each invocation processes only the scope level
454// equal to the initial level, which is the level of Changes[StartAt].
455// If we encounter a scope level greater than the initial level, then we call
456// ourselves recursively, thereby avoiding the pollution of the current state
457// with the alignment requirements of the nested sub-level. This recursive
458// behavior is necessary for aligning function prototypes that have one or more
459// arguments.
460// If this function encounters a scope level less than the initial level,
461// it returns the current position.
462// There is a non-obvious subtlety in the recursive behavior: Even though we
463// defer processing of nested levels to recursive invocations of this
464// function, when it comes time to align a sequence of tokens, we run the
465// alignment on the entire sequence, including the nested levels.
466// When doing so, most of the nested tokens are skipped, because their
467// alignment was already handled by the recursive invocations of this function.
468// However, the special exception is that we do NOT skip function parameters
469// that are split across multiple lines. See the test case in FormatTest.cpp
470// that mentions "split function parameter alignment" for an example of this.
471// When the parameter RightJustify is true, the operator will be
472// right-justified. It is used to align compound assignments like `+=` and `=`.
473// When RightJustify and ACS.PadOperators are true, operators in each block to
474// be aligned will be padded on the left to the same length before aligning.
475//
476// The simple check will not look at the indentaion and nesting level to recurse
477// into the line for alignment. It will also not count the commas. This is e.g.
478// for aligning macro definitions.
479template <typename F, bool SimpleCheck = false>
480static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
482 unsigned StartAt,
483 const FormatStyle::AlignConsecutiveStyle &ACS = {},
484 bool RightJustify = false) {
485 // We arrange each line in 3 parts. The operator to be aligned (the anchor),
486 // and text to its left and right. In the aligned text the width of each part
487 // will be the maximum of that over the block that has been aligned.
488
489 // Maximum widths of each part so far.
490 // When RightJustify is true and ACS.PadOperators is false, the part from
491 // start of line to the right end of the anchor. Otherwise, only the part to
492 // the left of the anchor. Including the space that exists on its left from
493 // the start. Not including the padding added on the left to right-justify the
494 // anchor.
495 unsigned WidthLeft = 0;
496 // The operator to be aligned when RightJustify is true and ACS.PadOperators
497 // is false. 0 otherwise.
498 unsigned WidthAnchor = 0;
499 // Width to the right of the anchor. Plus width of the anchor when
500 // RightJustify is false.
501 unsigned WidthRight = 0;
502
503 // Number of the start and the end of the current token sequence.
504 unsigned StartOfSequence = 0;
505 unsigned EndOfSequence = 0;
506
507 // The positions of the tokens to be aligned.
508 SmallVector<unsigned> MatchedIndices;
509
510 // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
511 // abort when we hit any token in a higher scope than the starting one.
512 const auto IndentAndNestingLevel =
513 StartAt < Changes.size() ? Changes[StartAt].indentAndNestingLevel()
514 : std::tuple<unsigned, unsigned, unsigned>();
515
516 // Keep track of the number of commas before the matching tokens, we will only
517 // align a sequence of matching tokens if they are preceded by the same number
518 // of commas.
519 unsigned CommasBeforeLastMatch = 0;
520 unsigned CommasBeforeMatch = 0;
521
522 // The column number of the matching token on the current line.
523 std::optional<unsigned> MatchingColumn;
524
525 // Whether the current line consists purely of comments.
526 bool LineIsComment = true;
527
528 // Aligns a sequence of matching tokens, on the MinColumn column.
529 //
530 // Sequences start from the first matching token to align, and end at the
531 // first token of the first line that doesn't need to be aligned.
532 //
533 // We need to adjust the StartOfTokenColumn of each Change that is on a line
534 // containing any matching token to be aligned and located after such token.
535 auto AlignCurrentSequence = [&] {
536 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
537 AlignTokenSequence(Style, StartOfSequence, EndOfSequence,
538 WidthLeft + WidthAnchor, RightJustify, MatchedIndices,
539 Changes);
540 }
541 WidthLeft = 0;
542 WidthAnchor = 0;
543 WidthRight = 0;
544 StartOfSequence = 0;
545 EndOfSequence = 0;
546 MatchedIndices.clear();
547 };
548
549 unsigned I = StartAt;
550 const auto E = Changes.size();
551 for (; I != E; ++I) {
552 auto &CurrentChange = Changes[I];
553 if (CurrentChange.indentAndNestingLevel() < IndentAndNestingLevel)
554 break;
555
556 if (CurrentChange.NewlinesBefore != 0) {
557 CommasBeforeMatch = 0;
558 EndOfSequence = I;
559
560 // Whether to break the alignment sequence because of an empty line.
561 bool EmptyLineBreak =
562 (CurrentChange.NewlinesBefore > 1) && !ACS.AcrossEmptyLines;
563
564 // Whether to break the alignment sequence because of a line without a
565 // match.
566 bool NoMatchBreak =
567 !MatchingColumn && !(LineIsComment && ACS.AcrossComments);
568
569 if (EmptyLineBreak || NoMatchBreak)
570 AlignCurrentSequence();
571
572 // A new line starts, re-initialize line status tracking bools.
573 // Keep the match state if a string literal is continued on this line.
574 if (MatchingColumn && CurrentChange.IndentedFromColumn < *MatchingColumn)
575 MatchingColumn.reset();
576 LineIsComment = true;
577 }
578
579 if (CurrentChange.Tok->isNot(tok::comment))
580 LineIsComment = false;
581
582 if (!SimpleCheck) {
583 if (CurrentChange.Tok->is(tok::comma)) {
584 ++CommasBeforeMatch;
585 } else if (CurrentChange.indentAndNestingLevel() >
586 IndentAndNestingLevel) {
587 // Call AlignTokens recursively, skipping over this scope block.
588 const auto StoppedAt =
589 AlignTokens(Style, Matches, Changes, I, ACS, RightJustify);
590 I = StoppedAt - 1;
591 continue;
592 }
593 }
594
595 if (!Matches(CurrentChange))
596 continue;
597
598 // If there is more than one matching token per line, or if the number of
599 // preceding commas, do not match anymore, end the sequence.
600 if ((CurrentChange.NewlinesBefore == 0U && MatchingColumn) ||
601 CommasBeforeMatch != CommasBeforeLastMatch) {
602 MatchedIndices.push_back(I);
603 AlignCurrentSequence();
604 }
605
606 CommasBeforeLastMatch = CommasBeforeMatch;
607 MatchingColumn = CurrentChange.StartOfTokenColumn;
608
609 if (StartOfSequence == 0)
610 StartOfSequence = I;
611
612 unsigned ChangeWidthLeft = CurrentChange.StartOfTokenColumn;
613 unsigned ChangeWidthAnchor = 0;
614 unsigned ChangeWidthRight = 0;
615 unsigned CurrentChangeWidthRight = 0;
616 if (RightJustify)
617 if (ACS.PadOperators)
618 ChangeWidthAnchor = CurrentChange.TokenLength;
619 else
620 ChangeWidthLeft += CurrentChange.TokenLength;
621 else
622 CurrentChangeWidthRight = CurrentChange.TokenLength;
623 const FormatToken *MatchingParenToEncounter = nullptr;
624 for (unsigned J = I + 1;
625 J != E && (Changes[J].NewlinesBefore == 0 ||
626 MatchingParenToEncounter || Changes[J].IsAligned);
627 ++J) {
628 const auto &Change = Changes[J];
629 const auto *Tok = Change.Tok;
630
631 if (Tok->MatchingParen) {
632 if (Tok->isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
633 TT_TemplateOpener) &&
634 !MatchingParenToEncounter) {
635 // If the next token is on the next line, we probably don't need to
636 // check the following lengths, because it most likely isn't aligned
637 // with the rest.
638 if (J + 1 != E && Changes[J + 1].NewlinesBefore == 0)
639 MatchingParenToEncounter = Tok->MatchingParen;
640 } else if (MatchingParenToEncounter == Tok->MatchingParen) {
641 MatchingParenToEncounter = nullptr;
642 }
643 }
644
645 if (Change.NewlinesBefore != 0) {
646 ChangeWidthRight = std::max(ChangeWidthRight, CurrentChangeWidthRight);
647 const auto ChangeWidthStart = ChangeWidthLeft + ChangeWidthAnchor;
648 // If the position of the current token is columnwise before the begin
649 // of the alignment, we drop out here, because the next line does not
650 // have to be moved with the previous one(s) for the alignment. E.g.:
651 // int i1 = 1; | <- ColumnLimit | int i1 = 1;
652 // int j = 0; | Without the break -> | int j = 0;
653 // int k = bar( | We still want to align the = | int k = bar(
654 // argument1, | here, even if we can't move | argument1,
655 // argument2); | the following lines. | argument2);
656 if (Change.IndentedFromColumn < ChangeWidthStart)
657 break;
658 CurrentChangeWidthRight = Change.Spaces - ChangeWidthStart;
659 } else {
660 CurrentChangeWidthRight += Change.Spaces;
661 }
662
663 // Changes are generally 1:1 with the tokens, but a change could also be
664 // inside of a token, in which case it's counted more than once: once for
665 // the whitespace surrounding the token (!IsInsideToken) and once for
666 // each whitespace change within it (IsInsideToken).
667 // Therefore, changes inside of a token should only count the space.
669 CurrentChangeWidthRight += Change.TokenLength;
670 }
671
672 ChangeWidthRight = std::max(ChangeWidthRight, CurrentChangeWidthRight);
673
674 // If we are restricted by the maximum column width, end the sequence.
675 unsigned NewLeft = std::max(ChangeWidthLeft, WidthLeft);
676 unsigned NewAnchor = std::max(ChangeWidthAnchor, WidthAnchor);
677 unsigned NewRight = std::max(ChangeWidthRight, WidthRight);
678 // `ColumnLimit == 0` means there is no column limit.
679 if (Style.ColumnLimit != 0 &&
680 Style.ColumnLimit < NewLeft + NewAnchor + NewRight) {
681 AlignCurrentSequence();
682 StartOfSequence = I;
683 WidthLeft = ChangeWidthLeft;
684 WidthAnchor = ChangeWidthAnchor;
685 WidthRight = ChangeWidthRight;
686 } else {
687 WidthLeft = NewLeft;
688 WidthAnchor = NewAnchor;
689 WidthRight = NewRight;
690 }
691 MatchedIndices.push_back(I);
692 }
693
694 // Pass entire lines to the function so that it can update the state of all
695 // tokens that move.
696 for (EndOfSequence = I;
697 EndOfSequence < E && Changes[EndOfSequence].NewlinesBefore == 0;
698 ++EndOfSequence) {
699 }
700 AlignCurrentSequence();
701 // The return value should still be where the level ends. The rest of the line
702 // may contain stuff to be aligned within an outer level.
703 return I;
704}
705
706// Aligns a sequence of matching tokens, on the MinColumn column.
707//
708// Sequences start from the first matching token to align, and end at the
709// first token of the first line that doesn't need to be aligned.
710//
711// We need to adjust the StartOfTokenColumn of each Change that is on a line
712// containing any matching token to be aligned and located after such token.
714 unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn,
715 std::function<bool(const WhitespaceManager::Change &C)> Matches,
717 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
718 bool FoundMatchOnLine = false;
719
720 for (unsigned I = StartOfSequence; I != EndOfSequence; ++I) {
721 if (Changes[I].NewlinesBefore > 0)
722 FoundMatchOnLine = false;
723
724 // If this is the first matching token to be aligned, remember by how many
725 // spaces it has to be shifted, so the rest of the changes on the line are
726 // shifted by the same amount.
727 if (!FoundMatchOnLine && Matches(Changes[I])) {
728 FoundMatchOnLine = true;
729 int Shift = MinColumn - Changes[I].StartOfTokenColumn;
730 IncrementChangeSpaces(I, Shift, Changes);
731 }
732 }
733 }
734
735 MinColumn = 0;
736 StartOfSequence = 0;
737 EndOfSequence = 0;
738}
739
740void WhitespaceManager::alignConsecutiveMacros() {
741 if (!Style.AlignConsecutiveMacros.Enabled)
742 return;
743
744 auto AlignMacrosMatches = [](const Change &C) {
745 const FormatToken *Current = C.Tok;
746 assert(Current);
747
748 if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
749 return false;
750
751 Current = Current->Previous;
752
753 // If token is a ")", skip over the parameter list, to the
754 // token that precedes the "("
755 if (Current->is(tok::r_paren)) {
756 const auto *MatchingParen = Current->MatchingParen;
757 // For a macro function, 0 spaces are required between the
758 // identifier and the lparen that opens the parameter list.
759 if (!MatchingParen || MatchingParen->SpacesRequiredBefore > 0 ||
760 !MatchingParen->Previous) {
761 return false;
762 }
763 Current = MatchingParen->Previous;
764 } else if (Current->Next->SpacesRequiredBefore != 1) {
765 // For a simple macro, 1 space is required between the
766 // identifier and the first token of the defined value.
767 return false;
768 }
769
770 return Current->endsSequence(tok::identifier, tok::pp_define);
771 };
772
773 AlignTokens<decltype(AlignMacrosMatches) &, /*SimpleCheck=*/true>(
774 Style, AlignMacrosMatches, Changes, 0, Style.AlignConsecutiveMacros);
775}
776
777void WhitespaceManager::alignConsecutiveAssignments() {
778 if (!Style.AlignConsecutiveAssignments.Enabled)
779 return;
780
782 Style,
783 [&](const Change &C) {
784 // Do not align on equal signs that are first on a line.
785 if (C.NewlinesBefore > 0)
786 return false;
787
788 // Do not align on equal signs that are last on a line.
789 if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
790 return false;
791
792 // Do not align operator= overloads.
793 FormatToken *Previous = C.Tok->getPreviousNonComment();
794 if (Previous && Previous->is(tok::kw_operator))
795 return false;
796
797 return Style.AlignConsecutiveAssignments.AlignCompound
798 ? C.Tok->getPrecedence() == prec::Assignment
799 : (C.Tok->is(tok::equal) ||
800 // In Verilog the '<=' is not a compound assignment, thus
801 // it is aligned even when the AlignCompound option is not
802 // set.
803 (Style.isVerilog() && C.Tok->is(tok::lessequal) &&
804 C.Tok->getPrecedence() == prec::Assignment));
805 },
806 Changes, /*StartAt=*/0, Style.AlignConsecutiveAssignments,
807 /*RightJustify=*/true);
808}
809
810void WhitespaceManager::alignConsecutiveBitFields() {
811 alignConsecutiveColons(Style.AlignConsecutiveBitFields, TT_BitFieldColon);
812}
813
814void WhitespaceManager::alignConsecutiveColons(
815 const FormatStyle::AlignConsecutiveStyle &AlignStyle, TokenType Type) {
816 if (!AlignStyle.Enabled)
817 return;
818
820 Style,
821 [&](Change const &C) {
822 // Do not align on ':' that is first on a line.
823 if (C.NewlinesBefore > 0)
824 return false;
825
826 // Do not align on ':' that is last on a line.
827 if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
828 return false;
829
830 return C.Tok->is(Type);
831 },
832 Changes, /*StartAt=*/0, AlignStyle);
833}
834
835void WhitespaceManager::alignConsecutiveShortCaseStatements(bool IsExpr) {
836 if (!Style.AlignConsecutiveShortCaseStatements.Enabled ||
837 !(IsExpr ? Style.AllowShortCaseExpressionOnASingleLine
838 : Style.AllowShortCaseLabelsOnASingleLine)) {
839 return;
840 }
841
842 const auto Type = IsExpr ? TT_CaseLabelArrow : TT_CaseLabelColon;
843 const auto &Option = Style.AlignConsecutiveShortCaseStatements;
844 const bool AlignArrowOrColon =
845 IsExpr ? Option.AlignCaseArrows : Option.AlignCaseColons;
846
847 auto Matches = [&](const Change &C) {
848 if (AlignArrowOrColon)
849 return C.Tok->is(Type);
850
851 // Ignore 'IsInsideToken' to allow matching trailing comments which
852 // need to be reflowed as that causes the token to appear in two
853 // different changes, which will cause incorrect alignment as we'll
854 // reflow early due to detecting multiple aligning tokens per line.
855 return !C.IsInsideToken && C.Tok->Previous && C.Tok->Previous->is(Type);
856 };
857
858 unsigned MinColumn = 0;
859
860 // Empty case statements don't break the alignment, but don't necessarily
861 // match our predicate, so we need to track their column so they can push out
862 // our alignment.
863 unsigned MinEmptyCaseColumn = 0;
864
865 // Start and end of the token sequence we're processing.
866 unsigned StartOfSequence = 0;
867 unsigned EndOfSequence = 0;
868
869 // Whether a matching token has been found on the current line.
870 bool FoundMatchOnLine = false;
871
872 bool LineIsComment = true;
873 bool LineIsEmptyCase = false;
874
875 unsigned I = 0;
876 for (unsigned E = Changes.size(); I != E; ++I) {
877 if (Changes[I].NewlinesBefore != 0) {
878 // Whether to break the alignment sequence because of an empty line.
879 bool EmptyLineBreak =
880 (Changes[I].NewlinesBefore > 1) &&
881 !Style.AlignConsecutiveShortCaseStatements.AcrossEmptyLines;
882
883 // Whether to break the alignment sequence because of a line without a
884 // match.
885 bool NoMatchBreak =
886 !FoundMatchOnLine &&
887 !(LineIsComment &&
888 Style.AlignConsecutiveShortCaseStatements.AcrossComments) &&
889 !LineIsEmptyCase;
890
891 if (EmptyLineBreak || NoMatchBreak) {
892 AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn,
893 Matches, Changes);
894 MinEmptyCaseColumn = 0;
895 }
896
897 // A new line starts, re-initialize line status tracking bools.
898 FoundMatchOnLine = false;
899 LineIsComment = true;
900 LineIsEmptyCase = false;
901 }
902
903 if (Changes[I].Tok->isNot(tok::comment))
904 LineIsComment = false;
905
906 if (Changes[I].Tok->is(Type)) {
907 LineIsEmptyCase =
908 !Changes[I].Tok->Next || Changes[I].Tok->Next->isTrailingComment();
909
910 if (LineIsEmptyCase) {
911 if (Style.AlignConsecutiveShortCaseStatements.AlignCaseColons) {
912 MinEmptyCaseColumn =
913 std::max(MinEmptyCaseColumn, Changes[I].StartOfTokenColumn);
914 } else {
915 MinEmptyCaseColumn =
916 std::max(MinEmptyCaseColumn, Changes[I].StartOfTokenColumn + 2);
917 }
918 }
919 }
920
921 if (!Matches(Changes[I]))
922 continue;
923
924 if (LineIsEmptyCase)
925 continue;
926
927 FoundMatchOnLine = true;
928
929 if (StartOfSequence == 0)
930 StartOfSequence = I;
931
932 EndOfSequence = I + 1;
933
934 MinColumn = std::max(MinColumn, Changes[I].StartOfTokenColumn);
935
936 // Allow empty case statements to push out our alignment.
937 MinColumn = std::max(MinColumn, MinEmptyCaseColumn);
938 }
939
940 AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
941 Changes);
942}
943
944void WhitespaceManager::alignConsecutiveTableGenBreakingDAGArgColons() {
945 alignConsecutiveColons(Style.AlignConsecutiveTableGenBreakingDAGArgColons,
946 TT_TableGenDAGArgListColonToAlign);
947}
948
949void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
950 alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
951 TT_TableGenCondOperatorColon);
952}
953
954void WhitespaceManager::alignConsecutiveTableGenDefinitions() {
955 alignConsecutiveColons(Style.AlignConsecutiveTableGenDefinitionColons,
956 TT_InheritanceColon);
957}
958
959void WhitespaceManager::alignConsecutiveDeclarations() {
960 if (!Style.AlignConsecutiveDeclarations.Enabled)
961 return;
962
964 Style,
965 [&](Change const &C) {
966 if (C.Tok->is(TT_FunctionTypeLParen))
967 return Style.AlignConsecutiveDeclarations.AlignFunctionPointers;
968 if (C.Tok->is(TT_FunctionDeclarationName))
969 return Style.AlignConsecutiveDeclarations.AlignFunctionDeclarations;
970 if (C.Tok->isNot(TT_StartOfName))
971 return false;
972 if (C.Tok->Previous &&
973 C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
974 return false;
975 // Check if there is a subsequent name that starts the same declaration.
976 for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
977 if (Next->is(tok::comment))
978 continue;
979 if (Next->is(TT_PointerOrReference))
980 return false;
981 if (!Next->Tok.getIdentifierInfo())
982 break;
983 if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
984 tok::kw_operator)) {
985 return false;
986 }
987 }
988 return true;
989 },
990 Changes, /*StartAt=*/0, Style.AlignConsecutiveDeclarations);
991}
992
993void WhitespaceManager::alignChainedConditionals() {
994 if (Style.BreakBeforeTernaryOperators) {
996 Style,
997 [](Change const &C) {
998 // Align question operators and last colon
999 return C.Tok->is(TT_ConditionalExpr) &&
1000 ((C.Tok->is(tok::question) && !C.NewlinesBefore) ||
1001 (C.Tok->is(tok::colon) && C.Tok->Next &&
1002 (C.Tok->Next->FakeLParens.empty() ||
1003 C.Tok->Next->FakeLParens.back() != prec::Conditional)));
1004 },
1005 Changes, /*StartAt=*/0);
1006 } else {
1007 static auto AlignWrappedOperand = [](Change const &C) {
1008 FormatToken *Previous = C.Tok->getPreviousNonComment();
1009 return C.NewlinesBefore && Previous && Previous->is(TT_ConditionalExpr) &&
1010 (Previous->is(tok::colon) &&
1011 (C.Tok->FakeLParens.empty() ||
1012 C.Tok->FakeLParens.back() != prec::Conditional));
1013 };
1014 // Ensure we keep alignment of wrapped operands with non-wrapped operands
1015 // Since we actually align the operators, the wrapped operands need the
1016 // extra offset to be properly aligned.
1017 for (Change &C : Changes)
1018 if (AlignWrappedOperand(C))
1019 C.StartOfTokenColumn -= 2;
1021 Style,
1022 [this](Change const &C) {
1023 // Align question operators if next operand is not wrapped, as
1024 // well as wrapped operands after question operator or last
1025 // colon in conditional sequence
1026 return (C.Tok->is(TT_ConditionalExpr) && C.Tok->is(tok::question) &&
1027 &C != &Changes.back() && (&C + 1)->NewlinesBefore == 0 &&
1028 !(&C + 1)->IsTrailingComment) ||
1029 AlignWrappedOperand(C);
1030 },
1031 Changes, /*StartAt=*/0);
1032 }
1033}
1034
1035void WhitespaceManager::alignTrailingComments() {
1036 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Never)
1037 return;
1038
1039 const int Size = Changes.size();
1040 if (Size == 0)
1041 return;
1042
1043 int MinColumn = 0;
1044 int StartOfSequence = 0;
1045 bool BreakBeforeNext = false;
1046 bool IsInPP = Changes.front().Tok->Tok.is(tok::hash);
1047 int NewLineThreshold = 1;
1048 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Always)
1049 NewLineThreshold = Style.AlignTrailingComments.OverEmptyLines + 1;
1050
1051 for (int I = 0, MaxColumn = INT_MAX, Newlines = 0; I < Size; ++I) {
1052 auto &C = Changes[I];
1053 if (C.StartOfBlockComment)
1054 continue;
1055 if (C.NewlinesBefore != 0) {
1056 Newlines += C.NewlinesBefore;
1057 const bool WasInPP = std::exchange(
1058 IsInPP, C.Tok->Tok.is(tok::hash) || (IsInPP && C.IsTrailingComment) ||
1059 C.ContinuesPPDirective);
1060 if (IsInPP != WasInPP && !Style.AlignTrailingComments.AlignPPAndNotPP) {
1061 alignTrailingComments(StartOfSequence, I, MinColumn);
1062 MinColumn = 0;
1063 MaxColumn = INT_MAX;
1064 StartOfSequence = I;
1065 Newlines = 0;
1066 }
1067 }
1068 if (!C.IsTrailingComment)
1069 continue;
1070
1071 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) {
1072 const int OriginalSpaces =
1073 C.OriginalWhitespaceRange.getEnd().getRawEncoding() -
1074 C.OriginalWhitespaceRange.getBegin().getRawEncoding() -
1075 C.Tok->LastNewlineOffset;
1076 assert(OriginalSpaces >= 0);
1077 const auto RestoredLineLength =
1078 C.StartOfTokenColumn + C.TokenLength + OriginalSpaces;
1079 // If leaving comments makes the line exceed the column limit, give up to
1080 // leave the comments.
1081 if (RestoredLineLength >= Style.ColumnLimit && Style.ColumnLimit > 0)
1082 break;
1083
1084 int Spaces =
1085 C.NewlinesBefore > 0 ? C.Tok->OriginalColumn : OriginalSpaces;
1086 setChangeSpaces(I, Spaces);
1087 continue;
1088 }
1089
1090 const int ChangeMinColumn = C.StartOfTokenColumn;
1091 int ChangeMaxColumn;
1092
1093 // If we don't create a replacement for this change, we have to consider
1094 // it to be immovable.
1095 if (!C.CreateReplacement)
1096 ChangeMaxColumn = ChangeMinColumn;
1097 else if (Style.ColumnLimit == 0)
1098 ChangeMaxColumn = INT_MAX;
1099 else if (Style.ColumnLimit >= C.TokenLength)
1100 ChangeMaxColumn = Style.ColumnLimit - C.TokenLength;
1101 else
1102 ChangeMaxColumn = ChangeMinColumn;
1103
1104 if (I + 1 < Size && Changes[I + 1].ContinuesPPDirective &&
1105 ChangeMaxColumn >= 2) {
1106 ChangeMaxColumn -= 2;
1107 }
1108
1109 bool WasAlignedWithStartOfNextLine = false;
1110 if (C.NewlinesBefore >= 1) { // A comment on its own line.
1111 const auto CommentColumn =
1112 SourceMgr.getSpellingColumnNumber(C.OriginalWhitespaceRange.getEnd());
1113 for (int J = I + 1; J < Size; ++J) {
1114 if (Changes[J].Tok->is(tok::comment))
1115 continue;
1116
1117 const auto NextColumn = SourceMgr.getSpellingColumnNumber(
1118 Changes[J].OriginalWhitespaceRange.getEnd());
1119 // The start of the next token was previously aligned with the
1120 // start of this comment.
1121 WasAlignedWithStartOfNextLine =
1122 CommentColumn == NextColumn ||
1123 CommentColumn == NextColumn + Style.IndentWidth;
1124 break;
1125 }
1126 }
1127
1128 // We don't want to align comments which end a scope, which are here
1129 // identified by most closing braces.
1130 auto DontAlignThisComment = [](const auto *Tok) {
1131 if (Tok->is(tok::semi)) {
1132 Tok = Tok->getPreviousNonComment();
1133 if (!Tok)
1134 return false;
1135 }
1136 if (Tok->is(tok::r_paren)) {
1137 // Back up past the parentheses and a `TT_DoWhile` that may precede.
1138 Tok = Tok->MatchingParen;
1139 if (!Tok)
1140 return false;
1141 Tok = Tok->getPreviousNonComment();
1142 if (!Tok)
1143 return false;
1144 if (Tok->is(TT_DoWhile)) {
1145 const auto *Prev = Tok->getPreviousNonComment();
1146 if (!Prev) {
1147 // A do-while-loop without braces.
1148 return true;
1149 }
1150 Tok = Prev;
1151 }
1152 }
1153
1154 if (Tok->isNot(tok::r_brace))
1155 return false;
1156
1157 while (Tok->Previous && Tok->Previous->is(tok::r_brace))
1158 Tok = Tok->Previous;
1159 return Tok->NewlinesBefore > 0;
1160 };
1161
1162 if (I > 0 && C.NewlinesBefore == 0 &&
1163 DontAlignThisComment(Changes[I - 1].Tok)) {
1164 alignTrailingComments(StartOfSequence, I, MinColumn);
1165 // Reset to initial values, but skip this change for the next alignment
1166 // pass.
1167 MinColumn = 0;
1168 MaxColumn = INT_MAX;
1169 StartOfSequence = I + 1;
1170 } else if (BreakBeforeNext || Newlines > NewLineThreshold ||
1171 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
1172 // Break the comment sequence if the previous line did not end
1173 // in a trailing comment.
1174 (C.NewlinesBefore == 1 && I > 0 &&
1175 !Changes[I - 1].IsTrailingComment) ||
1176 WasAlignedWithStartOfNextLine) {
1177 alignTrailingComments(StartOfSequence, I, MinColumn);
1178 MinColumn = ChangeMinColumn;
1179 MaxColumn = ChangeMaxColumn;
1180 StartOfSequence = I;
1181 } else {
1182 MinColumn = std::max(MinColumn, ChangeMinColumn);
1183 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
1184 }
1185 BreakBeforeNext = (I == 0) || (C.NewlinesBefore > 1) ||
1186 // Never start a sequence with a comment at the beginning
1187 // of the line.
1188 (C.NewlinesBefore == 1 && StartOfSequence == I);
1189 Newlines = 0;
1190 }
1191 alignTrailingComments(StartOfSequence, Size, MinColumn);
1192}
1193
1194void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
1195 unsigned Column) {
1196 for (unsigned i = Start; i != End; ++i) {
1197 int Shift = 0;
1198 if (Changes[i].IsTrailingComment)
1199 Shift = Column - Changes[i].StartOfTokenColumn;
1200 if (Changes[i].StartOfBlockComment) {
1201 Shift = Changes[i].IndentationOffset +
1202 Changes[i].StartOfBlockComment->StartOfTokenColumn -
1203 Changes[i].StartOfTokenColumn;
1204 }
1205 if (Shift <= 0)
1206 continue;
1207
1208 setChangeSpaces(i, Changes[i].Spaces + Shift);
1209 }
1210}
1211
1212void WhitespaceManager::alignEscapedNewlines() {
1213 const auto Align = Style.AlignEscapedNewlines;
1214 if (Align == FormatStyle::ENAS_DontAlign)
1215 return;
1216
1217 const bool WithLastLine = Align == FormatStyle::ENAS_LeftWithLastLine;
1218 const bool AlignLeft = Align == FormatStyle::ENAS_Left || WithLastLine;
1219 const auto MaxColumn = Style.ColumnLimit;
1220 unsigned MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
1221 unsigned StartOfMacro = 0;
1222 for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
1223 Change &C = Changes[i];
1224 if (C.NewlinesBefore == 0 && (!WithLastLine || C.Tok->isNot(tok::eof)))
1225 continue;
1226 const bool InPPDirective = C.ContinuesPPDirective;
1227 const auto BackslashColumn = C.PreviousEndOfTokenColumn + 2;
1228 if (InPPDirective ||
1229 (WithLastLine && (MaxColumn == 0 || BackslashColumn <= MaxColumn))) {
1230 MaxEndOfLine = std::max(BackslashColumn, MaxEndOfLine);
1231 }
1232 if (!InPPDirective) {
1233 alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
1234 MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
1235 StartOfMacro = i;
1236 }
1237 }
1238 alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
1239}
1240
1241void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
1242 unsigned Column) {
1243 for (unsigned i = Start; i < End; ++i) {
1244 Change &C = Changes[i];
1245 if (C.NewlinesBefore > 0) {
1246 assert(C.ContinuesPPDirective);
1247 if (C.PreviousEndOfTokenColumn + 1 > Column)
1248 C.EscapedNewlineColumn = 0;
1249 else
1250 C.EscapedNewlineColumn = Column;
1251 }
1252 }
1253}
1254
1255void WhitespaceManager::alignArrayInitializers() {
1256 if (Style.AlignArrayOfStructures == FormatStyle::AIAS_None)
1257 return;
1258
1259 for (unsigned ChangeIndex = 1U, ChangeEnd = Changes.size();
1260 ChangeIndex < ChangeEnd; ++ChangeIndex) {
1261 auto &C = Changes[ChangeIndex];
1262 if (C.Tok->IsArrayInitializer) {
1263 bool FoundComplete = false;
1264 for (unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
1265 ++InsideIndex) {
1266 const auto *Tok = Changes[InsideIndex].Tok;
1267 if (Tok->is(tok::pp_define))
1268 break;
1269 if (Tok == C.Tok->MatchingParen) {
1270 alignArrayInitializers(ChangeIndex, InsideIndex + 1);
1271 ChangeIndex = InsideIndex + 1;
1272 FoundComplete = true;
1273 break;
1274 }
1275 }
1276 if (!FoundComplete)
1277 ChangeIndex = ChangeEnd;
1278 }
1279 }
1280}
1281
1282void WhitespaceManager::alignArrayInitializers(unsigned Start, unsigned End) {
1283
1284 if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Right)
1285 alignArrayInitializersRightJustified(getCells(Start, End));
1286 else if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Left)
1287 alignArrayInitializersLeftJustified(getCells(Start, End));
1288}
1289
1290void WhitespaceManager::alignArrayInitializersRightJustified(
1291 CellDescriptions &&CellDescs) {
1292 if (!CellDescs.isRectangular())
1293 return;
1294
1295 const int BracePadding =
1296 Style.Cpp11BracedListStyle != FormatStyle::BLS_Block ? 0 : 1;
1297 auto &Cells = CellDescs.Cells;
1298 // Now go through and fixup the spaces.
1299 auto *CellIter = Cells.begin();
1300 for (auto i = 0U; i < CellDescs.CellCounts[0]; ++i, ++CellIter) {
1301 unsigned NetWidth = 0U;
1302 if (isSplitCell(*CellIter))
1303 NetWidth = getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1304 auto CellWidth = getMaximumCellWidth(CellIter, NetWidth);
1305
1306 if (Changes[CellIter->Index].Tok->is(tok::r_brace)) {
1307 // So in here we want to see if there is a brace that falls
1308 // on a line that was split. If so on that line we make sure that
1309 // the spaces in front of the brace are enough.
1310 const auto *Next = CellIter;
1311 do {
1312 const FormatToken *Previous = Changes[Next->Index].Tok->Previous;
1313 if (Previous && Previous->isNot(TT_LineComment)) {
1314 Changes[Next->Index].NewlinesBefore = 0;
1315 setChangeSpaces(Next->Index, BracePadding);
1316 }
1317 Next = Next->NextColumnElement;
1318 } while (Next);
1319 // Unless the array is empty, we need the position of all the
1320 // immediately adjacent cells
1321 if (CellIter != Cells.begin()) {
1322 auto ThisNetWidth =
1323 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1324 auto MaxNetWidth = getMaximumNetWidth(
1325 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1326 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1327 if (ThisNetWidth < MaxNetWidth)
1328 setChangeSpaces(CellIter->Index, MaxNetWidth - ThisNetWidth);
1329 auto RowCount = 1U;
1330 auto Offset = std::distance(Cells.begin(), CellIter);
1331 for (const auto *Next = CellIter->NextColumnElement; Next;
1332 Next = Next->NextColumnElement) {
1333 if (RowCount >= CellDescs.CellCounts.size())
1334 break;
1335 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1336 auto *End = Start + Offset;
1337 ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1338 if (ThisNetWidth < MaxNetWidth)
1339 setChangeSpaces(Next->Index, MaxNetWidth - ThisNetWidth);
1340 ++RowCount;
1341 }
1342 }
1343 } else {
1344 auto ThisWidth =
1345 calculateCellWidth(CellIter->Index, CellIter->EndIndex, true) +
1346 NetWidth;
1347 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1348 int Spaces = (CellWidth - (ThisWidth + NetWidth));
1349 Spaces += (i > 0) ? 1 : BracePadding;
1350
1351 setChangeSpaces(CellIter->Index, Spaces);
1352 }
1353 alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
1354 for (const auto *Next = CellIter->NextColumnElement; Next;
1355 Next = Next->NextColumnElement) {
1356 ThisWidth =
1357 calculateCellWidth(Next->Index, Next->EndIndex, true) + NetWidth;
1358 if (Changes[Next->Index].NewlinesBefore == 0) {
1359 int Spaces = (CellWidth - ThisWidth);
1360 Spaces += (i > 0) ? 1 : BracePadding;
1361
1362 setChangeSpaces(Next->Index, Spaces);
1363 }
1364 alignToStartOfCell(Next->Index, Next->EndIndex);
1365 }
1366 }
1367 }
1368}
1369
1370void WhitespaceManager::alignArrayInitializersLeftJustified(
1371 CellDescriptions &&CellDescs) {
1372
1373 if (!CellDescs.isRectangular())
1374 return;
1375
1376 const int BracePadding =
1377 Style.Cpp11BracedListStyle != FormatStyle::BLS_Block ? 0 : 1;
1378 auto &Cells = CellDescs.Cells;
1379 // Now go through and fixup the spaces.
1380 auto *CellIter = Cells.begin();
1381 // The first cell of every row needs to be against the left brace.
1382 for (const auto *Next = CellIter; Next; Next = Next->NextColumnElement) {
1383 auto &Change = Changes[Next->Index];
1384 int Spaces =
1385 Change.NewlinesBefore == 0 ? BracePadding : CellDescs.InitialSpaces;
1386 setChangeSpaces(Next->Index, Spaces);
1387 }
1388 ++CellIter;
1389 for (auto i = 1U; i < CellDescs.CellCounts[0]; i++, ++CellIter) {
1390 auto MaxNetWidth = getMaximumNetWidth(
1391 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1392 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1393 auto ThisNetWidth =
1394 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1395 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1396 int Spaces =
1397 MaxNetWidth - ThisNetWidth +
1398 (Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1
1399 : BracePadding);
1400 setChangeSpaces(CellIter->Index, Spaces);
1401 }
1402 auto RowCount = 1U;
1403 auto Offset = std::distance(Cells.begin(), CellIter);
1404 for (const auto *Next = CellIter->NextColumnElement; Next;
1405 Next = Next->NextColumnElement) {
1406 if (RowCount >= CellDescs.CellCounts.size())
1407 break;
1408 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1409 auto *End = Start + Offset;
1410 auto ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1411 if (Changes[Next->Index].NewlinesBefore == 0) {
1412 int Spaces =
1413 MaxNetWidth - ThisNetWidth +
1414 (Changes[Next->Index].Tok->isNot(tok::r_brace) ? 1 : BracePadding);
1415 setChangeSpaces(Next->Index, Spaces);
1416 }
1417 ++RowCount;
1418 }
1419 }
1420}
1421
1422bool WhitespaceManager::isSplitCell(const CellDescription &Cell) {
1423 if (Cell.HasSplit)
1424 return true;
1425 for (const auto *Next = Cell.NextColumnElement; Next;
1426 Next = Next->NextColumnElement) {
1427 if (Next->HasSplit)
1428 return true;
1429 }
1430 return false;
1431}
1432
1433WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
1434 unsigned End) {
1435
1436 unsigned Depth = 0;
1437 unsigned Cell = 0;
1438 SmallVector<unsigned> CellCounts;
1439 unsigned InitialSpaces = 0;
1440 unsigned InitialTokenLength = 0;
1441 unsigned EndSpaces = 0;
1442 SmallVector<CellDescription> Cells;
1443 const FormatToken *MatchingParen = nullptr;
1444 for (unsigned i = Start; i < End; ++i) {
1445 auto &C = Changes[i];
1446 if (C.Tok->is(tok::l_brace))
1447 ++Depth;
1448 else if (C.Tok->is(tok::r_brace))
1449 --Depth;
1450 if (Depth == 2) {
1451 if (C.Tok->is(tok::l_brace)) {
1452 Cell = 0;
1453 MatchingParen = C.Tok->MatchingParen;
1454 if (InitialSpaces == 0) {
1455 InitialSpaces = C.Spaces + C.TokenLength;
1456 InitialTokenLength = C.TokenLength;
1457 auto j = i - 1;
1458 for (; Changes[j].NewlinesBefore == 0 && j > Start; --j) {
1459 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1460 InitialTokenLength += Changes[j].TokenLength;
1461 }
1462 if (C.NewlinesBefore == 0) {
1463 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1464 InitialTokenLength += Changes[j].TokenLength;
1465 }
1466 }
1467 } else if (C.Tok->is(tok::comma)) {
1468 if (!Cells.empty())
1469 Cells.back().EndIndex = i;
1470 if (const auto *Next = C.Tok->getNextNonComment();
1471 Next && Next->isNot(tok::r_brace)) { // dangling comma
1472 ++Cell;
1473 }
1474 }
1475 } else if (Depth == 1) {
1476 if (C.Tok == MatchingParen) {
1477 if (!Cells.empty())
1478 Cells.back().EndIndex = i;
1479 Cells.push_back(CellDescription{i, ++Cell, i + 1, false, nullptr});
1480 CellCounts.push_back(C.Tok->Previous->isNot(tok::comma) ? Cell + 1
1481 : Cell);
1482 // Go to the next non-comment and ensure there is a break in front
1483 const auto *NextNonComment = C.Tok->getNextNonComment();
1484 while (NextNonComment && NextNonComment->is(tok::comma))
1485 NextNonComment = NextNonComment->getNextNonComment();
1486 auto j = i;
1487 while (j < End && Changes[j].Tok != NextNonComment)
1488 ++j;
1489 if (j < End && Changes[j].NewlinesBefore == 0 &&
1490 Changes[j].Tok->isNot(tok::r_brace)) {
1491 Changes[j].NewlinesBefore = 1;
1492 // Account for the added token lengths
1493 setChangeSpaces(j, InitialSpaces - InitialTokenLength);
1494 }
1495 } else if (C.Tok->is(tok::comment) && C.Tok->NewlinesBefore == 0) {
1496 // Trailing comments stay at a space past the last token
1497 setChangeSpaces(i, Changes[i - 1].Tok->is(tok::comma) ? 1 : 2);
1498 } else if (C.Tok->is(tok::l_brace)) {
1499 // We need to make sure that the ending braces is aligned to the
1500 // start of our initializer
1501 auto j = i - 1;
1502 for (; j > 0 && !Changes[j].Tok->ArrayInitializerLineStart; --j)
1503 ; // Nothing the loop does the work
1504 EndSpaces = Changes[j].Spaces;
1505 }
1506 } else if (Depth == 0 && C.Tok->is(tok::r_brace)) {
1507 C.NewlinesBefore = 1;
1508 setChangeSpaces(i, EndSpaces);
1509 }
1510 if (C.Tok->StartsColumn) {
1511 // This gets us past tokens that have been split over multiple
1512 // lines
1513 bool HasSplit = false;
1514 if (Changes[i].NewlinesBefore > 0) {
1515 // So if we split a line previously and the tail line + this token is
1516 // less then the column limit we remove the split here and just put
1517 // the column start at a space past the comma
1518 //
1519 // FIXME This if branch covers the cases where the column is not
1520 // the first column. This leads to weird pathologies like the formatting
1521 // auto foo = Items{
1522 // Section{
1523 // 0, bar(),
1524 // }
1525 // };
1526 // Well if it doesn't lead to that it's indicative that the line
1527 // breaking should be revisited. Unfortunately alot of other options
1528 // interact with this
1529 auto j = i - 1;
1530 if ((j - 1) > Start && Changes[j].Tok->is(tok::comma) &&
1531 Changes[j - 1].NewlinesBefore > 0) {
1532 --j;
1533 auto LineLimit = Changes[j].Spaces + Changes[j].TokenLength;
1534 if (LineLimit < Style.ColumnLimit) {
1535 Changes[i].NewlinesBefore = 0;
1536 setChangeSpaces(i, 1);
1537 }
1538 }
1539 }
1540 while (Changes[i].NewlinesBefore > 0 && Changes[i].Tok == C.Tok) {
1541 setChangeSpaces(i, InitialSpaces);
1542 ++i;
1543 HasSplit = true;
1544 }
1545 if (Changes[i].Tok != C.Tok)
1546 --i;
1547 Cells.push_back(CellDescription{i, Cell, i, HasSplit, nullptr});
1548 }
1549 }
1550
1551 return linkCells({Cells, CellCounts, InitialSpaces});
1552}
1553
1554unsigned WhitespaceManager::calculateCellWidth(unsigned Start, unsigned End,
1555 bool WithSpaces) const {
1556 unsigned CellWidth = 0;
1557 for (auto i = Start; i < End; i++) {
1558 if (Changes[i].NewlinesBefore > 0)
1559 CellWidth = 0;
1560 CellWidth += Changes[i].TokenLength;
1561 CellWidth += (WithSpaces ? Changes[i].Spaces : 0);
1562 }
1563 return CellWidth;
1564}
1565
1566void WhitespaceManager::alignToStartOfCell(unsigned Start, unsigned End) {
1567 if ((End - Start) <= 1)
1568 return;
1569 // If the line is broken anywhere in there make sure everything
1570 // is aligned to the parent
1571 for (auto i = Start + 1; i < End; i++)
1572 if (Changes[i].NewlinesBefore > 0)
1573 setChangeSpaces(i, Changes[Start].Spaces);
1574}
1575
1576WhitespaceManager::CellDescriptions
1577WhitespaceManager::linkCells(CellDescriptions &&CellDesc) {
1578 auto &Cells = CellDesc.Cells;
1579 for (auto *CellIter = Cells.begin(); CellIter != Cells.end(); ++CellIter) {
1580 if (!CellIter->NextColumnElement && (CellIter + 1) != Cells.end()) {
1581 for (auto *NextIter = CellIter + 1; NextIter != Cells.end(); ++NextIter) {
1582 if (NextIter->Cell == CellIter->Cell) {
1583 CellIter->NextColumnElement = &(*NextIter);
1584 break;
1585 }
1586 }
1587 }
1588 }
1589 return std::move(CellDesc);
1590}
1591
1592void WhitespaceManager::setChangeSpaces(unsigned Start, unsigned Spaces) {
1593 SetChangeSpaces(Start, Spaces, Changes);
1594}
1595
1596void WhitespaceManager::generateChanges() {
1597 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
1598 const Change &C = Changes[i];
1599 if (i > 0) {
1600 auto Last = Changes[i - 1].OriginalWhitespaceRange;
1601 auto New = Changes[i].OriginalWhitespaceRange;
1602 // Do not generate two replacements for the same location. As a special
1603 // case, it is allowed if there is a replacement for the empty range
1604 // between 2 tokens and another non-empty range at the start of the second
1605 // token. We didn't implement logic to combine replacements for 2
1606 // consecutive source ranges into a single replacement, because the
1607 // program works fine without it.
1608 //
1609 // We can't eliminate empty original whitespace ranges. They appear when
1610 // 2 tokens have no whitespace in between in the input. It does not
1611 // matter whether whitespace is to be added. If no whitespace is to be
1612 // added, the replacement will be empty, and it gets eliminated after this
1613 // step in storeReplacement. For example, if the input is `foo();`,
1614 // there will be a replacement for the range between every consecutive
1615 // pair of tokens.
1616 //
1617 // A replacement at the start of a token can be added by
1618 // BreakableStringLiteralUsingOperators::insertBreak when it adds braces
1619 // around the string literal. Say Verilog code is being formatted and the
1620 // first line is to become the next 2 lines.
1621 // x("long string");
1622 // x({"long ",
1623 // "string"});
1624 // There will be a replacement for the empty range between the parenthesis
1625 // and the string and another replacement for the quote character. The
1626 // replacement for the empty range between the parenthesis and the quote
1627 // comes from ContinuationIndenter::addTokenOnCurrentLine when it changes
1628 // the original empty range between the parenthesis and the string to
1629 // another empty one. The replacement for the quote character comes from
1630 // BreakableStringLiteralUsingOperators::insertBreak when it adds the
1631 // brace. In the example, the replacement for the empty range is the same
1632 // as the original text. However, eliminating replacements that are same
1633 // as the original does not help in general. For example, a newline can
1634 // be inserted, causing the first line to become the next 3 lines.
1635 // xxxxxxxxxxx("long string");
1636 // xxxxxxxxxxx(
1637 // {"long ",
1638 // "string"});
1639 // In that case, the empty range between the parenthesis and the string
1640 // will be replaced by a newline and 4 spaces. So we will still have to
1641 // deal with a replacement for an empty source range followed by a
1642 // replacement for a non-empty source range.
1643 if (Last.getBegin() == New.getBegin() &&
1644 (Last.getEnd() != Last.getBegin() ||
1645 New.getEnd() == New.getBegin())) {
1646 continue;
1647 }
1648 }
1649 if (C.CreateReplacement) {
1650 std::string ReplacementText = C.PreviousLinePostfix;
1651 if (C.ContinuesPPDirective) {
1652 appendEscapedNewlineText(ReplacementText, C.NewlinesBefore,
1653 C.PreviousEndOfTokenColumn,
1654 C.EscapedNewlineColumn);
1655 } else {
1656 appendNewlineText(ReplacementText, C);
1657 }
1658 // FIXME: This assert should hold if we computed the column correctly.
1659 // assert((int)C.StartOfTokenColumn >= C.Spaces);
1660 appendIndentText(
1661 ReplacementText, C.Tok->IndentLevel, std::max(0, C.Spaces),
1662 std::max((int)C.StartOfTokenColumn, C.Spaces) - std::max(0, C.Spaces),
1663 C.IsAligned);
1664 ReplacementText.append(C.CurrentLinePrefix);
1665 storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
1666 }
1667 }
1668}
1669
1670void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
1671 unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
1672 SourceMgr.getFileOffset(Range.getBegin());
1673 // Don't create a replacement, if it does not change anything.
1674 if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
1675 WhitespaceLength) == Text) {
1676 return;
1677 }
1678 auto Err = Replaces.add(tooling::Replacement(
1679 SourceMgr, CharSourceRange::getCharRange(Range), Text));
1680 // FIXME: better error handling. For now, just print an error message in the
1681 // release version.
1682 if (Err) {
1683 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
1684 assert(false);
1685 }
1686}
1687
1688void WhitespaceManager::appendNewlineText(std::string &Text, const Change &C) {
1689 if (C.NewlinesBefore <= 0)
1690 return;
1691
1692 StringRef Newline = UseCRLF ? "\r\n" : "\n";
1693 Text.append(Newline);
1694
1695 if (C.Tok->HasFormFeedBefore)
1696 Text.append("\f");
1697
1698 for (unsigned I = 1; I < C.NewlinesBefore; ++I)
1699 Text.append(Newline);
1700}
1701
1702void WhitespaceManager::appendEscapedNewlineText(
1703 std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
1704 unsigned EscapedNewlineColumn) {
1705 if (Newlines > 0) {
1706 unsigned Spaces =
1707 std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
1708 for (unsigned i = 0; i < Newlines; ++i) {
1709 Text.append(Spaces, ' ');
1710 Text.append(UseCRLF ? "\\\r\n" : "\\\n");
1711 Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
1712 }
1713 }
1714}
1715
1716void WhitespaceManager::appendIndentText(std::string &Text,
1717 unsigned IndentLevel, unsigned Spaces,
1718 unsigned WhitespaceStartColumn,
1719 bool IsAligned) {
1720 switch (Style.UseTab) {
1721 case FormatStyle::UT_Never:
1722 Text.append(Spaces, ' ');
1723 break;
1724 case FormatStyle::UT_Always: {
1725 if (Style.TabWidth) {
1726 unsigned FirstTabWidth =
1727 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
1728
1729 // Insert only spaces when we want to end up before the next tab.
1730 if (Spaces < FirstTabWidth || Spaces == 1) {
1731 Text.append(Spaces, ' ');
1732 break;
1733 }
1734 // Align to the next tab.
1735 Spaces -= FirstTabWidth;
1736 Text.append("\t");
1737
1738 Text.append(Spaces / Style.TabWidth, '\t');
1739 Text.append(Spaces % Style.TabWidth, ' ');
1740 } else if (Spaces == 1) {
1741 Text.append(Spaces, ' ');
1742 }
1743 break;
1744 }
1745 case FormatStyle::UT_ForIndentation:
1746 if (WhitespaceStartColumn == 0) {
1747 unsigned Indentation = IndentLevel * Style.IndentWidth;
1748 Spaces = appendTabIndent(Text, Spaces, Indentation);
1749 }
1750 Text.append(Spaces, ' ');
1751 break;
1752 case FormatStyle::UT_ForContinuationAndIndentation:
1753 if (WhitespaceStartColumn == 0)
1754 Spaces = appendTabIndent(Text, Spaces, Spaces);
1755 Text.append(Spaces, ' ');
1756 break;
1757 case FormatStyle::UT_AlignWithSpaces:
1758 if (WhitespaceStartColumn == 0) {
1759 unsigned Indentation =
1760 IsAligned ? IndentLevel * Style.IndentWidth : Spaces;
1761 Spaces = appendTabIndent(Text, Spaces, Indentation);
1762 }
1763 Text.append(Spaces, ' ');
1764 break;
1765 }
1766}
1767
1768unsigned WhitespaceManager::appendTabIndent(std::string &Text, unsigned Spaces,
1769 unsigned Indentation) {
1770 // This happens, e.g. when a line in a block comment is indented less than the
1771 // first one.
1772 if (Indentation > Spaces)
1773 Indentation = Spaces;
1774 if (Style.TabWidth) {
1775 unsigned Tabs = Indentation / Style.TabWidth;
1776 Text.append(Tabs, '\t');
1777 Spaces -= Tabs * Style.TabWidth;
1778 }
1779 return Spaces;
1780}
1781
1782} // namespace format
1783} // namespace clang
int Newlines
The number of newlines immediately before the Token after formatting.
FormatToken()
Token Tok
The Token.
unsigned NewlinesBefore
The number of newlines immediately before the Token.
FormatToken * MatchingParen
If this is a bracket, this points to the matching one.
unsigned IndentLevel
The indent level of this token. Copied from the surrounding line.
FormatToken * Previous
The previous token in the unwrapped line.
FormatToken * Next
The next token in the unwrapped line.
WhitespaceManager class manages whitespace around tokens and their replacements.
__DEVICE__ long long abs(long long __n)
static CharSourceRange getCharRange(SourceRange R)
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Functor to sort changes in original source order.
bool operator()(const Change &C1, const Change &C2) const
void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars, StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective, unsigned Newlines, int Spaces)
Inserts or replaces whitespace in the middle of a token.
void replaceWhitespace(FormatToken &Tok, unsigned Newlines, unsigned Spaces, unsigned StartOfTokenColumn, bool IsAligned=false, bool InPPDirective=false, unsigned IndentedFromColumn=0)
Replaces the whitespace in front of Tok.
void addUntouchableToken(const FormatToken &Tok, bool InPPDirective)
Adds information about an unchangeable token's whitespace.
static bool inputUsesCRLF(StringRef Text, bool DefaultToCRLF)
Infers whether the input is using CRLF.
llvm::Error addReplacement(const tooling::Replacement &Replacement)
const tooling::Replacements & generateReplacements()
Returns all the Replacements created during formatting.
A text replacement.
Definition Replacement.h:83
Maintains a set of replacements that are conflict-free.
#define INT_MAX
Definition limits.h:50
static void IncrementChangeSpaces(unsigned Start, int Delta, MutableArrayRef< WhitespaceManager::Change > Changes)
@ MR_ExpandedArg
The token was expanded from a macro argument when formatting the expanded token sequence.
static void AlignMatchingTokenSequence(unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn, std::function< bool(const WhitespaceManager::Change &C)> Matches, SmallVector< WhitespaceManager::Change, 16 > &Changes)
static void AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End, unsigned Column, bool RightJustify, ArrayRef< unsigned > Matches, SmallVector< WhitespaceManager::Change, 16 > &Changes)
static void SetChangeSpaces(unsigned Start, unsigned Spaces, MutableArrayRef< WhitespaceManager::Change > Changes)
TokenType
Determines the semantic type of a syntactic token, e.g.
static unsigned AlignTokens(const FormatStyle &Style, F &&Matches, SmallVector< WhitespaceManager::Change, 16 > &Changes, unsigned StartAt, const FormatStyle::AlignConsecutiveStyle &ACS={}, bool RightJustify=false)
The JSON file list parser is used to communicate input to InstallAPI.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Type
The name was classified as a type.
Definition Sema.h:564
int const char * function
Definition c++config.h:31
#define false
Definition stdbool.h:26
A wrapper around a Token storing information about the whitespace characters preceding it.
unsigned FakeRParens
Insert this many fake ) after this token for correct indentation.
SmallVector< prec::Level, 4 > FakeLParens
Stores the number of required fake parentheses and the corresponding operator precedence.
bool is(tok::TokenKind Kind) const
FormatToken * Previous
The previous token in the unwrapped line.
Represents a change before a token, a break inside a token, or the layout of an unchanged token (or w...
Change(const FormatToken &Tok, bool CreateReplacement, SourceRange OriginalWhitespaceRange, int Spaces, unsigned StartOfTokenColumn, unsigned IndentedFromColumn, unsigned NewlinesBefore, StringRef PreviousLinePostfix, StringRef CurrentLinePrefix, bool IsAligned, bool ContinuesPPDirective, bool IsInsideToken)
Creates a Change.