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