1 //===--- WhitespaceManager.cpp - Format C++ code --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements WhitespaceManager class.
13 //===----------------------------------------------------------------------===//
15 #include "WhitespaceManager.h"
16 #include "llvm/ADT/STLExtras.h"
21 bool WhitespaceManager::Change::IsBeforeInFile::
22 operator()(const Change &C1, const Change &C2) const {
23 return SourceMgr.isBeforeInTranslationUnit(
24 C1.OriginalWhitespaceRange.getBegin(),
25 C2.OriginalWhitespaceRange.getBegin());
28 WhitespaceManager::Change::Change(const FormatToken &Tok,
29 bool CreateReplacement,
30 SourceRange OriginalWhitespaceRange,
31 int Spaces, unsigned StartOfTokenColumn,
32 unsigned NewlinesBefore,
33 StringRef PreviousLinePostfix,
34 StringRef CurrentLinePrefix,
35 bool ContinuesPPDirective, bool IsInsideToken)
36 : Tok(&Tok), CreateReplacement(CreateReplacement),
37 OriginalWhitespaceRange(OriginalWhitespaceRange),
38 StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
39 PreviousLinePostfix(PreviousLinePostfix),
40 CurrentLinePrefix(CurrentLinePrefix),
41 ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces),
42 IsInsideToken(IsInsideToken), IsTrailingComment(false), TokenLength(0),
43 PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0),
44 StartOfBlockComment(nullptr), IndentationOffset(0) {}
46 void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
48 unsigned StartOfTokenColumn,
52 Tok.Decision = (Newlines > 0) ? FD_Break : FD_Continue;
53 Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
54 Spaces, StartOfTokenColumn, Newlines, "", "",
55 InPPDirective && !Tok.IsFirst,
56 /*IsInsideToken=*/false));
59 void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
63 Changes.push_back(Change(Tok, /*CreateReplacement=*/false,
64 Tok.WhitespaceRange, /*Spaces=*/0,
65 Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
66 InPPDirective && !Tok.IsFirst,
67 /*IsInsideToken=*/false));
70 void WhitespaceManager::replaceWhitespaceInToken(
71 const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
72 StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
73 unsigned Newlines, int Spaces) {
76 SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
78 Change(Tok, /*CreateReplacement=*/true,
79 SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
80 std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
81 InPPDirective && !Tok.IsFirst, /*IsInsideToken=*/true));
84 const tooling::Replacements &WhitespaceManager::generateReplacements() {
88 std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr));
89 calculateLineBreakInformation();
90 alignConsecutiveDeclarations();
91 alignConsecutiveAssignments();
92 alignTrailingComments();
93 alignEscapedNewlines();
99 void WhitespaceManager::calculateLineBreakInformation() {
100 Changes[0].PreviousEndOfTokenColumn = 0;
101 Change *LastOutsideTokenChange = &Changes[0];
102 for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
103 SourceLocation OriginalWhitespaceStart =
104 Changes[i].OriginalWhitespaceRange.getBegin();
105 SourceLocation PreviousOriginalWhitespaceEnd =
106 Changes[i - 1].OriginalWhitespaceRange.getEnd();
107 unsigned OriginalWhitespaceStartOffset =
108 SourceMgr.getFileOffset(OriginalWhitespaceStart);
109 unsigned PreviousOriginalWhitespaceEndOffset =
110 SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
111 assert(PreviousOriginalWhitespaceEndOffset <=
112 OriginalWhitespaceStartOffset);
113 const char *const PreviousOriginalWhitespaceEndData =
114 SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
115 StringRef Text(PreviousOriginalWhitespaceEndData,
116 SourceMgr.getCharacterData(OriginalWhitespaceStart) -
117 PreviousOriginalWhitespaceEndData);
118 // Usually consecutive changes would occur in consecutive tokens. This is
119 // not the case however when analyzing some preprocessor runs of the
120 // annotated lines. For example, in this code:
128 // one of the runs will produce the sequence of lines marked with line 1, 2
129 // and 3. So the two consecutive whitespace changes just before '// line 2'
130 // and before '#endif // line 3' span multiple lines and tokens:
132 // #else B{change X}[// line 2
134 // ]{change Y}#endif // line 3
136 // For this reason, if the text between consecutive changes spans multiple
137 // newlines, the token length must be adjusted to the end of the original
138 // line of the token.
139 auto NewlinePos = Text.find_first_of('\n');
140 if (NewlinePos == StringRef::npos) {
141 Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
142 PreviousOriginalWhitespaceEndOffset +
143 Changes[i].PreviousLinePostfix.size() +
144 Changes[i - 1].CurrentLinePrefix.size();
146 Changes[i - 1].TokenLength =
147 NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
150 // If there are multiple changes in this token, sum up all the changes until
151 // the end of the line.
152 if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0)
153 LastOutsideTokenChange->TokenLength +=
154 Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
156 LastOutsideTokenChange = &Changes[i - 1];
158 Changes[i].PreviousEndOfTokenColumn =
159 Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
161 Changes[i - 1].IsTrailingComment =
162 (Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
163 (Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
164 Changes[i - 1].Tok->is(tok::comment) &&
165 // FIXME: This is a dirty hack. The problem is that
166 // BreakableLineCommentSection does comment reflow changes and here is
167 // the aligning of trailing comments. Consider the case where we reflow
168 // the second line up in this example:
173 // That amounts to 2 changes by BreakableLineCommentSection:
174 // - the first, delimited by (), for the whitespace between the tokens,
175 // - and second, delimited by [], for the whitespace at the beginning
176 // of the second token:
181 // So in the end we have two changes like this:
183 // // line1()[ ]line 2
185 // Note that the OriginalWhitespaceStart of the second change is the
186 // same as the PreviousOriginalWhitespaceEnd of the first change.
187 // In this case, the below check ensures that the second change doesn't
188 // get treated as a trailing comment change here, since this might
189 // trigger additional whitespace to be wrongly inserted before "line 2"
190 // by the comment aligner here.
192 // For a proper solution we need a mechanism to say to WhitespaceManager
193 // that a particular change breaks the current sequence of trailing
195 OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
197 // FIXME: The last token is currently not always an eof token; in those
198 // cases, setting TokenLength of the last token to 0 is wrong.
199 Changes.back().TokenLength = 0;
200 Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
202 const WhitespaceManager::Change *LastBlockComment = nullptr;
203 for (auto &Change : Changes) {
204 // Reset the IsTrailingComment flag for changes inside of trailing comments
205 // so they don't get realigned later. Comment line breaks however still need
207 if (Change.IsInsideToken && Change.NewlinesBefore == 0)
208 Change.IsTrailingComment = false;
209 Change.StartOfBlockComment = nullptr;
210 Change.IndentationOffset = 0;
211 if (Change.Tok->is(tok::comment)) {
212 if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken)
213 LastBlockComment = &Change;
215 if ((Change.StartOfBlockComment = LastBlockComment))
216 Change.IndentationOffset =
217 Change.StartOfTokenColumn -
218 Change.StartOfBlockComment->StartOfTokenColumn;
221 LastBlockComment = nullptr;
226 // Align a single sequence of tokens, see AlignTokens below.
227 template <typename F>
229 AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches,
230 SmallVector<WhitespaceManager::Change, 16> &Changes) {
231 bool FoundMatchOnLine = false;
234 // ScopeStack keeps track of the current scope depth. It contains indices of
235 // the first token on each scope.
236 // We only run the "Matches" function on tokens from the outer-most scope.
237 // However, we do need to pay special attention to one class of tokens
238 // that are not in the outer-most scope, and that is function parameters
239 // which are split across multiple lines, as illustrated by this example:
243 // In the above example, we need to take special care to ensure that
244 // 'double z' is indented along with it's owning function 'b'.
245 SmallVector<unsigned, 16> ScopeStack;
247 for (unsigned i = Start; i != End; ++i) {
248 if (ScopeStack.size() != 0 &&
249 Changes[i].nestingAndIndentLevel() <
250 Changes[ScopeStack.back()].nestingAndIndentLevel())
251 ScopeStack.pop_back();
253 if (i != Start && Changes[i].nestingAndIndentLevel() >
254 Changes[i - 1].nestingAndIndentLevel())
255 ScopeStack.push_back(i);
257 bool InsideNestedScope = ScopeStack.size() != 0;
259 if (Changes[i].NewlinesBefore > 0 && !InsideNestedScope) {
261 FoundMatchOnLine = false;
264 // If this is the first matching token to be aligned, remember by how many
265 // spaces it has to be shifted, so the rest of the changes on the line are
266 // shifted by the same amount
267 if (!FoundMatchOnLine && !InsideNestedScope && Matches(Changes[i])) {
268 FoundMatchOnLine = true;
269 Shift = Column - Changes[i].StartOfTokenColumn;
270 Changes[i].Spaces += Shift;
273 // This is for function parameters that are split across multiple lines,
274 // as mentioned in the ScopeStack comment.
275 if (InsideNestedScope && Changes[i].NewlinesBefore > 0) {
276 unsigned ScopeStart = ScopeStack.back();
277 if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) ||
278 (ScopeStart > Start + 1 &&
279 Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)))
280 Changes[i].Spaces += Shift;
284 Changes[i].StartOfTokenColumn += Shift;
285 if (i + 1 != Changes.size())
286 Changes[i + 1].PreviousEndOfTokenColumn += Shift;
290 // Walk through a subset of the changes, starting at StartAt, and find
291 // sequences of matching tokens to align. To do so, keep track of the lines and
292 // whether or not a matching token was found on a line. If a matching token is
293 // found, extend the current sequence. If the current line cannot be part of a
294 // sequence, e.g. because there is an empty line before it or it contains only
295 // non-matching tokens, finalize the previous sequence.
296 // The value returned is the token on which we stopped, either because we
297 // exhausted all items inside Changes, or because we hit a scope level higher
298 // than our initial scope.
299 // This function is recursive. Each invocation processes only the scope level
300 // equal to the initial level, which is the level of Changes[StartAt].
301 // If we encounter a scope level greater than the initial level, then we call
302 // ourselves recursively, thereby avoiding the pollution of the current state
303 // with the alignment requirements of the nested sub-level. This recursive
304 // behavior is necessary for aligning function prototypes that have one or more
306 // If this function encounters a scope level less than the initial level,
307 // it returns the current position.
308 // There is a non-obvious subtlety in the recursive behavior: Even though we
309 // defer processing of nested levels to recursive invocations of this
310 // function, when it comes time to align a sequence of tokens, we run the
311 // alignment on the entire sequence, including the nested levels.
312 // When doing so, most of the nested tokens are skipped, because their
313 // alignment was already handled by the recursive invocations of this function.
314 // However, the special exception is that we do NOT skip function parameters
315 // that are split across multiple lines. See the test case in FormatTest.cpp
316 // that mentions "split function parameter alignment" for an example of this.
317 template <typename F>
318 static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
319 SmallVector<WhitespaceManager::Change, 16> &Changes,
321 unsigned MinColumn = 0;
322 unsigned MaxColumn = UINT_MAX;
324 // Line number of the start and the end of the current token sequence.
325 unsigned StartOfSequence = 0;
326 unsigned EndOfSequence = 0;
328 // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
329 // abort when we hit any token in a higher scope than the starting one.
330 auto NestingAndIndentLevel = StartAt < Changes.size()
331 ? Changes[StartAt].nestingAndIndentLevel()
332 : std::pair<unsigned, unsigned>(0, 0);
334 // Keep track of the number of commas before the matching tokens, we will only
335 // align a sequence of matching tokens if they are preceded by the same number
337 unsigned CommasBeforeLastMatch = 0;
338 unsigned CommasBeforeMatch = 0;
340 // Whether a matching token has been found on the current line.
341 bool FoundMatchOnLine = false;
343 // Aligns a sequence of matching tokens, on the MinColumn column.
345 // Sequences start from the first matching token to align, and end at the
346 // first token of the first line that doesn't need to be aligned.
348 // We need to adjust the StartOfTokenColumn of each Change that is on a line
349 // containing any matching token to be aligned and located after such token.
350 auto AlignCurrentSequence = [&] {
351 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
352 AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
355 MaxColumn = UINT_MAX;
360 unsigned i = StartAt;
361 for (unsigned e = Changes.size(); i != e; ++i) {
362 if (Changes[i].nestingAndIndentLevel() < NestingAndIndentLevel)
365 if (Changes[i].NewlinesBefore != 0) {
366 CommasBeforeMatch = 0;
368 // If there is a blank line, or if the last line didn't contain any
369 // matching token, the sequence ends here.
370 if (Changes[i].NewlinesBefore > 1 || !FoundMatchOnLine)
371 AlignCurrentSequence();
373 FoundMatchOnLine = false;
376 if (Changes[i].Tok->is(tok::comma)) {
378 } else if (Changes[i].nestingAndIndentLevel() > NestingAndIndentLevel) {
379 // Call AlignTokens recursively, skipping over this scope block.
380 unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
385 if (!Matches(Changes[i]))
388 // If there is more than one matching token per line, or if the number of
389 // preceding commas, do not match anymore, end the sequence.
390 if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
391 AlignCurrentSequence();
393 CommasBeforeLastMatch = CommasBeforeMatch;
394 FoundMatchOnLine = true;
396 if (StartOfSequence == 0)
399 unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
400 int LineLengthAfter = -Changes[i].Spaces;
401 for (unsigned j = i; j != e && Changes[j].NewlinesBefore == 0; ++j)
402 LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
403 unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
405 // If we are restricted by the maximum column width, end the sequence.
406 if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
407 CommasBeforeLastMatch != CommasBeforeMatch) {
408 AlignCurrentSequence();
412 MinColumn = std::max(MinColumn, ChangeMinColumn);
413 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
417 AlignCurrentSequence();
421 void WhitespaceManager::alignConsecutiveAssignments() {
422 if (!Style.AlignConsecutiveAssignments)
426 [&](const Change &C) {
427 // Do not align on equal signs that are first on a line.
428 if (C.NewlinesBefore > 0)
431 // Do not align on equal signs that are last on a line.
432 if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
435 return C.Tok->is(tok::equal);
437 Changes, /*StartAt=*/0);
440 void WhitespaceManager::alignConsecutiveDeclarations() {
441 if (!Style.AlignConsecutiveDeclarations)
444 // FIXME: Currently we don't handle properly the PointerAlignment: Right
445 // The * and & are not aligned and are left dangling. Something has to be done
446 // about it, but it raises the question of alignment of code like:
447 // const char* const* v1;
449 // SomeVeryLongType const& v3;
451 [](Change const &C) {
452 // tok::kw_operator is necessary for aligning operator overload
454 return C.Tok->is(TT_StartOfName) ||
455 C.Tok->is(TT_FunctionDeclarationName) ||
456 C.Tok->is(tok::kw_operator);
458 Changes, /*StartAt=*/0);
461 void WhitespaceManager::alignTrailingComments() {
462 unsigned MinColumn = 0;
463 unsigned MaxColumn = UINT_MAX;
464 unsigned StartOfSequence = 0;
465 bool BreakBeforeNext = false;
466 unsigned Newlines = 0;
467 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
468 if (Changes[i].StartOfBlockComment)
470 Newlines += Changes[i].NewlinesBefore;
471 if (!Changes[i].IsTrailingComment)
474 unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
475 unsigned ChangeMaxColumn;
477 if (Style.ColumnLimit == 0)
478 ChangeMaxColumn = UINT_MAX;
479 else if (Style.ColumnLimit >= Changes[i].TokenLength)
480 ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
482 ChangeMaxColumn = ChangeMinColumn;
484 // If we don't create a replacement for this change, we have to consider
485 // it to be immovable.
486 if (!Changes[i].CreateReplacement)
487 ChangeMaxColumn = ChangeMinColumn;
489 if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
490 ChangeMaxColumn -= 2;
491 // If this comment follows an } in column 0, it probably documents the
492 // closing of a namespace and we don't want to align it.
493 bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
494 Changes[i - 1].Tok->is(tok::r_brace) &&
495 Changes[i - 1].StartOfTokenColumn == 0;
496 bool WasAlignedWithStartOfNextLine = false;
497 if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
498 unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
499 Changes[i].OriginalWhitespaceRange.getEnd());
500 for (unsigned j = i + 1; j != e; ++j) {
501 if (Changes[j].Tok->is(tok::comment))
504 unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
505 Changes[j].OriginalWhitespaceRange.getEnd());
506 // The start of the next token was previously aligned with the
507 // start of this comment.
508 WasAlignedWithStartOfNextLine =
509 CommentColumn == NextColumn ||
510 CommentColumn == NextColumn + Style.IndentWidth;
514 if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
515 alignTrailingComments(StartOfSequence, i, MinColumn);
516 MinColumn = ChangeMinColumn;
517 MaxColumn = ChangeMinColumn;
519 } else if (BreakBeforeNext || Newlines > 1 ||
520 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
521 // Break the comment sequence if the previous line did not end
522 // in a trailing comment.
523 (Changes[i].NewlinesBefore == 1 && i > 0 &&
524 !Changes[i - 1].IsTrailingComment) ||
525 WasAlignedWithStartOfNextLine) {
526 alignTrailingComments(StartOfSequence, i, MinColumn);
527 MinColumn = ChangeMinColumn;
528 MaxColumn = ChangeMaxColumn;
531 MinColumn = std::max(MinColumn, ChangeMinColumn);
532 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
535 (i == 0) || (Changes[i].NewlinesBefore > 1) ||
536 // Never start a sequence with a comment at the beginning of
538 (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
541 alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
544 void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
546 for (unsigned i = Start; i != End; ++i) {
548 if (Changes[i].IsTrailingComment) {
549 Shift = Column - Changes[i].StartOfTokenColumn;
551 if (Changes[i].StartOfBlockComment) {
552 Shift = Changes[i].IndentationOffset +
553 Changes[i].StartOfBlockComment->StartOfTokenColumn -
554 Changes[i].StartOfTokenColumn;
557 Changes[i].Spaces += Shift;
558 if (i + 1 != Changes.size())
559 Changes[i + 1].PreviousEndOfTokenColumn += Shift;
560 Changes[i].StartOfTokenColumn += Shift;
564 void WhitespaceManager::alignEscapedNewlines() {
565 if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
568 bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
569 unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
570 unsigned StartOfMacro = 0;
571 for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
572 Change &C = Changes[i];
573 if (C.NewlinesBefore > 0) {
574 if (C.ContinuesPPDirective) {
575 MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
577 alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
578 MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
583 alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
586 void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
588 for (unsigned i = Start; i < End; ++i) {
589 Change &C = Changes[i];
590 if (C.NewlinesBefore > 0) {
591 assert(C.ContinuesPPDirective);
592 if (C.PreviousEndOfTokenColumn + 1 > Column)
593 C.EscapedNewlineColumn = 0;
595 C.EscapedNewlineColumn = Column;
600 void WhitespaceManager::generateChanges() {
601 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
602 const Change &C = Changes[i];
604 assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() !=
605 C.OriginalWhitespaceRange.getBegin() &&
606 "Generating two replacements for the same location");
608 if (C.CreateReplacement) {
609 std::string ReplacementText = C.PreviousLinePostfix;
610 if (C.ContinuesPPDirective)
611 appendNewlineText(ReplacementText, C.NewlinesBefore,
612 C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn);
614 appendNewlineText(ReplacementText, C.NewlinesBefore);
615 appendIndentText(ReplacementText, C.Tok->IndentLevel,
616 std::max(0, C.Spaces),
617 C.StartOfTokenColumn - std::max(0, C.Spaces));
618 ReplacementText.append(C.CurrentLinePrefix);
619 storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
624 void WhitespaceManager::storeReplacement(SourceRange Range,
626 unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
627 SourceMgr.getFileOffset(Range.getBegin());
628 // Don't create a replacement, if it does not change anything.
629 if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
630 WhitespaceLength) == Text)
632 auto Err = Replaces.add(tooling::Replacement(
633 SourceMgr, CharSourceRange::getCharRange(Range), Text));
634 // FIXME: better error handling. For now, just print an error message in the
637 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
642 void WhitespaceManager::appendNewlineText(std::string &Text,
644 for (unsigned i = 0; i < Newlines; ++i)
645 Text.append(UseCRLF ? "\r\n" : "\n");
648 void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines,
649 unsigned PreviousEndOfTokenColumn,
650 unsigned EscapedNewlineColumn) {
653 std::min<int>(EscapedNewlineColumn - 2, PreviousEndOfTokenColumn);
654 for (unsigned i = 0; i < Newlines; ++i) {
655 Text.append(EscapedNewlineColumn - Offset - 1, ' ');
656 Text.append(UseCRLF ? "\\\r\n" : "\\\n");
662 void WhitespaceManager::appendIndentText(std::string &Text,
663 unsigned IndentLevel, unsigned Spaces,
664 unsigned WhitespaceStartColumn) {
665 switch (Style.UseTab) {
666 case FormatStyle::UT_Never:
667 Text.append(Spaces, ' ');
669 case FormatStyle::UT_Always: {
670 unsigned FirstTabWidth =
671 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
672 // Indent with tabs only when there's at least one full tab.
673 if (FirstTabWidth + Style.TabWidth <= Spaces) {
674 Spaces -= FirstTabWidth;
677 Text.append(Spaces / Style.TabWidth, '\t');
678 Text.append(Spaces % Style.TabWidth, ' ');
681 case FormatStyle::UT_ForIndentation:
682 if (WhitespaceStartColumn == 0) {
683 unsigned Indentation = IndentLevel * Style.IndentWidth;
684 // This happens, e.g. when a line in a block comment is indented less than
686 if (Indentation > Spaces)
687 Indentation = Spaces;
688 unsigned Tabs = Indentation / Style.TabWidth;
689 Text.append(Tabs, '\t');
690 Spaces -= Tabs * Style.TabWidth;
692 Text.append(Spaces, ' ');
694 case FormatStyle::UT_ForContinuationAndIndentation:
695 if (WhitespaceStartColumn == 0) {
696 unsigned Tabs = Spaces / Style.TabWidth;
697 Text.append(Tabs, '\t');
698 Spaces -= Tabs * Style.TabWidth;
700 Text.append(Spaces, ' ');
705 } // namespace format