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