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