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 return;
818
820 Style,
821 [&](const Change &C) {
822 // Do not align on equal signs that are first on a line.
823 if (C.NewlinesBefore > 0)
824 return false;
825
826 // Do not align on equal signs that are last on a line.
827 if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
828 return false;
829
830 // Do not align operator= overloads.
831 FormatToken *Previous = C.Tok->getPreviousNonComment();
832 if (Previous && Previous->is(tok::kw_operator))
833 return false;
834
835 return Style.AlignConsecutiveAssignments.AlignCompound
836 ? C.Tok->getPrecedence() == prec::Assignment
837 : (C.Tok->is(tok::equal) ||
838 // In Verilog the '<=' is not a compound assignment, thus
839 // it is aligned even when the AlignCompound option is not
840 // set.
841 (Style.isVerilog() && C.Tok->is(tok::lessequal) &&
842 C.Tok->getPrecedence() == prec::Assignment));
843 },
844 Changes, /*StartAt=*/0, Style.AlignConsecutiveAssignments,
845 /*RightJustify=*/true);
846}
847
848void WhitespaceManager::alignConsecutiveBitFields() {
849 alignConsecutiveColons(Style.AlignConsecutiveBitFields, TT_BitFieldColon);
850}
851
852void WhitespaceManager::alignConsecutiveColons(
853 const FormatStyle::AlignConsecutiveStyle &AlignStyle, TokenType Type) {
854 if (!AlignStyle.Enabled)
855 return;
856
858 Style,
859 [&](Change const &C) {
860 // Do not align on ':' that is first on a line.
861 if (C.NewlinesBefore > 0)
862 return false;
863
864 // Do not align on ':' that is last on a line.
865 if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
866 return false;
867
868 return C.Tok->is(Type);
869 },
870 Changes, /*StartAt=*/0, AlignStyle);
871}
872
873void WhitespaceManager::alignConsecutiveShortCaseStatements(bool IsExpr) {
874 if (!Style.AlignConsecutiveShortCaseStatements.Enabled ||
875 !(IsExpr ? Style.AllowShortCaseExpressionOnASingleLine
876 : Style.AllowShortCaseLabelsOnASingleLine)) {
877 return;
878 }
879
880 const auto Type = IsExpr ? TT_CaseLabelArrow : TT_CaseLabelColon;
881 const auto &Option = Style.AlignConsecutiveShortCaseStatements;
882 const bool AlignArrowOrColon =
883 IsExpr ? Option.AlignCaseArrows : Option.AlignCaseColons;
884
885 FormatStyle::AlignConsecutiveStyle AlignStyle{};
886 AlignStyle.AcrossComments = Option.AcrossComments;
887 AlignStyle.AcrossEmptyLines = Option.AcrossEmptyLines;
888
889 auto Matches = [Type](const Change &C) { return C.Tok->is(Type); };
890 if (AlignArrowOrColon) {
892 Style, Matches, Changes, /*StartAt=*/0, AlignStyle);
893 } else {
895 Style, Matches, Changes, /*StartAt=*/0, AlignStyle);
896 }
897}
898
899void WhitespaceManager::alignConsecutiveTableGenBreakingDAGArgColons() {
900 alignConsecutiveColons(Style.AlignConsecutiveTableGenBreakingDAGArgColons,
901 TT_TableGenDAGArgListColonToAlign);
902}
903
904void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
905 alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
906 TT_TableGenCondOperatorColon);
907}
908
909void WhitespaceManager::alignConsecutiveTableGenDefinitions() {
910 alignConsecutiveColons(Style.AlignConsecutiveTableGenDefinitionColons,
911 TT_InheritanceColon);
912}
913
914void WhitespaceManager::alignConsecutiveDeclarations() {
915 if (!Style.AlignConsecutiveDeclarations.Enabled)
916 return;
917
919 Style,
920 [&](Change const &C) {
921 if (C.Tok->is(TT_FunctionTypeLParen))
922 return Style.AlignConsecutiveDeclarations.AlignFunctionPointers;
923 if (C.Tok->is(TT_FunctionDeclarationName))
924 return Style.AlignConsecutiveDeclarations.AlignFunctionDeclarations;
925 if (C.Tok->isNot(TT_StartOfName))
926 return false;
927 if (C.Tok->Previous &&
928 C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
929 return false;
930 // Check if there is a subsequent name that starts the same declaration.
931 for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
932 if (Next->is(tok::comment))
933 continue;
934 if (Next->is(TT_PointerOrReference))
935 return false;
936 if (!Next->Tok.getIdentifierInfo())
937 break;
938 if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
939 tok::kw_operator)) {
940 return false;
941 }
942 }
943 return true;
944 },
945 Changes, /*StartAt=*/0, Style.AlignConsecutiveDeclarations);
946}
947
948void WhitespaceManager::alignChainedConditionals() {
949 if (Style.BreakBeforeTernaryOperators) {
951 Style,
952 [](Change const &C) {
953 // Align question operators and last colon
954 return C.Tok->is(TT_ConditionalExpr) &&
955 ((C.Tok->is(tok::question) && !C.NewlinesBefore) ||
956 (C.Tok->is(tok::colon) && C.Tok->Next &&
957 (C.Tok->Next->FakeLParens.empty() ||
958 C.Tok->Next->FakeLParens.back() != prec::Conditional)));
959 },
960 Changes, /*StartAt=*/0);
961 } else {
962 static auto AlignWrappedOperand = [](Change const &C) {
963 FormatToken *Previous = C.Tok->getPreviousNonComment();
964 return C.NewlinesBefore && Previous && Previous->is(TT_ConditionalExpr) &&
965 (Previous->is(tok::colon) &&
966 (C.Tok->FakeLParens.empty() ||
967 C.Tok->FakeLParens.back() != prec::Conditional));
968 };
969 // Ensure we keep alignment of wrapped operands with non-wrapped operands
970 // Since we actually align the operators, the wrapped operands need the
971 // extra offset to be properly aligned.
972 for (Change &C : Changes)
973 if (AlignWrappedOperand(C))
974 C.StartOfTokenColumn -= 2;
976 Style,
977 [this](Change const &C) {
978 // Align question operators if next operand is not wrapped, as
979 // well as wrapped operands after question operator or last
980 // colon in conditional sequence
981 return (C.Tok->is(TT_ConditionalExpr) && C.Tok->is(tok::question) &&
982 &C != &Changes.back() && (&C + 1)->NewlinesBefore == 0 &&
983 !(&C + 1)->IsTrailingComment) ||
984 AlignWrappedOperand(C);
985 },
986 Changes, /*StartAt=*/0);
987 }
988}
989
990void WhitespaceManager::alignTrailingComments() {
991 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Never)
992 return;
993
994 const int Size = Changes.size();
995 if (Size == 0)
996 return;
997
998 int MinColumn = 0;
999 int StartOfSequence = 0;
1000 bool BreakBeforeNext = false;
1001 bool IsInPP = Changes.front().Tok->Tok.is(tok::hash);
1002 int NewLineThreshold = 1;
1003 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Always)
1004 NewLineThreshold = Style.AlignTrailingComments.OverEmptyLines + 1;
1005
1006 for (int I = 0, MaxColumn = INT_MAX, Newlines = 0; I < Size; ++I) {
1007 auto &C = Changes[I];
1008 if (C.StartOfBlockComment)
1009 continue;
1010 if (C.NewlinesBefore != 0) {
1011 Newlines += C.NewlinesBefore;
1012 const bool WasInPP = std::exchange(
1013 IsInPP, C.Tok->Tok.is(tok::hash) || (IsInPP && C.IsTrailingComment) ||
1014 C.ContinuesPPDirective);
1015 if (IsInPP != WasInPP && !Style.AlignTrailingComments.AlignPPAndNotPP) {
1016 alignTrailingComments(StartOfSequence, I, MinColumn);
1017 MinColumn = 0;
1018 MaxColumn = INT_MAX;
1019 StartOfSequence = I;
1020 Newlines = 0;
1021 }
1022 }
1023 if (!C.IsTrailingComment)
1024 continue;
1025
1026 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) {
1027 const int OriginalSpaces =
1028 C.OriginalWhitespaceRange.getEnd().getRawEncoding() -
1029 C.OriginalWhitespaceRange.getBegin().getRawEncoding() -
1030 C.Tok->LastNewlineOffset;
1031 assert(OriginalSpaces >= 0);
1032 const auto RestoredLineLength =
1033 C.StartOfTokenColumn + C.TokenLength + OriginalSpaces;
1034 // If leaving comments makes the line exceed the column limit, give up to
1035 // leave the comments.
1036 if (RestoredLineLength >= Style.ColumnLimit && Style.ColumnLimit > 0)
1037 break;
1038
1039 int Spaces =
1040 C.NewlinesBefore > 0 ? C.Tok->OriginalColumn : OriginalSpaces;
1041 setChangeSpaces(I, Spaces);
1042 continue;
1043 }
1044
1045 const int ChangeMinColumn = C.StartOfTokenColumn;
1046 int ChangeMaxColumn;
1047
1048 // If we don't create a replacement for this change, we have to consider
1049 // it to be immovable.
1050 if (!C.CreateReplacement)
1051 ChangeMaxColumn = ChangeMinColumn;
1052 else if (Style.ColumnLimit == 0)
1053 ChangeMaxColumn = INT_MAX;
1054 else if (Style.ColumnLimit >= C.TokenLength)
1055 ChangeMaxColumn = Style.ColumnLimit - C.TokenLength;
1056 else
1057 ChangeMaxColumn = ChangeMinColumn;
1058
1059 if (I + 1 < Size && Changes[I + 1].ContinuesPPDirective &&
1060 ChangeMaxColumn >= 2) {
1061 ChangeMaxColumn -= 2;
1062 }
1063
1064 bool WasAlignedWithStartOfNextLine = false;
1065 if (C.NewlinesBefore >= 1) { // A comment on its own line.
1066 const auto CommentColumn =
1067 SourceMgr.getSpellingColumnNumber(C.OriginalWhitespaceRange.getEnd());
1068 for (int J = I + 1; J < Size; ++J) {
1069 if (Changes[J].Tok->is(tok::comment))
1070 continue;
1071
1072 if (!C.AlignedTo)
1073 C.AlignedTo = C.Tok->getPrevious(tok::comment);
1074 const auto NextColumn = SourceMgr.getSpellingColumnNumber(
1075 Changes[J].OriginalWhitespaceRange.getEnd());
1076 // The start of the next token was previously aligned with the
1077 // start of this comment.
1078 WasAlignedWithStartOfNextLine =
1079 CommentColumn == NextColumn ||
1080 CommentColumn == NextColumn + Style.IndentWidth;
1081 break;
1082 }
1083 }
1084
1085 // We don't want to align comments which end a scope, which are here
1086 // identified by most closing braces.
1087 auto DontAlignThisComment = [](const auto *Tok) {
1088 if (Tok->is(tok::semi)) {
1089 Tok = Tok->getPreviousNonComment();
1090 if (!Tok)
1091 return false;
1092 }
1093 if (Tok->is(tok::r_paren)) {
1094 // Back up past the parentheses and a `TT_DoWhile` that may precede.
1095 Tok = Tok->MatchingParen;
1096 if (!Tok)
1097 return false;
1098 Tok = Tok->getPreviousNonComment();
1099 if (!Tok)
1100 return false;
1101 if (Tok->is(TT_DoWhile)) {
1102 const auto *Prev = Tok->getPreviousNonComment();
1103 if (!Prev) {
1104 // A do-while-loop without braces.
1105 return true;
1106 }
1107 Tok = Prev;
1108 }
1109 }
1110
1111 if (Tok->isNot(tok::r_brace))
1112 return false;
1113
1114 while (Tok->Previous && Tok->Previous->is(tok::r_brace))
1115 Tok = Tok->Previous;
1116 return Tok->NewlinesBefore > 0;
1117 };
1118
1119 if (I > 0 && C.NewlinesBefore == 0 &&
1120 DontAlignThisComment(Changes[I - 1].Tok)) {
1121 alignTrailingComments(StartOfSequence, I, MinColumn);
1122 // Reset to initial values, but skip this change for the next alignment
1123 // pass.
1124 MinColumn = 0;
1125 MaxColumn = INT_MAX;
1126 StartOfSequence = I + 1;
1127 } else if (BreakBeforeNext || Newlines > NewLineThreshold ||
1128 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
1129 // Break the comment sequence if the previous line did not end
1130 // in a trailing comment.
1131 (C.NewlinesBefore == 1 && I > 0 &&
1132 !Changes[I - 1].IsTrailingComment) ||
1133 WasAlignedWithStartOfNextLine) {
1134 alignTrailingComments(StartOfSequence, I, MinColumn);
1135 MinColumn = ChangeMinColumn;
1136 MaxColumn = ChangeMaxColumn;
1137 StartOfSequence = I;
1138 } else {
1139 MinColumn = std::max(MinColumn, ChangeMinColumn);
1140 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
1141 }
1142 BreakBeforeNext = (I == 0) || (C.NewlinesBefore > 1) ||
1143 // Never start a sequence with a comment at the beginning
1144 // of the line.
1145 (C.NewlinesBefore == 1 && StartOfSequence == I);
1146 Newlines = 0;
1147 }
1148 alignTrailingComments(StartOfSequence, Size, MinColumn);
1149}
1150
1151void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
1152 unsigned Column) {
1153 for (unsigned i = Start; i != End; ++i) {
1154 int Shift = 0;
1155 if (Changes[i].IsTrailingComment)
1156 Shift = Column - Changes[i].StartOfTokenColumn;
1157 if (Changes[i].StartOfBlockComment) {
1158 Shift = Changes[i].IndentationOffset +
1159 Changes[i].StartOfBlockComment->StartOfTokenColumn -
1160 Changes[i].StartOfTokenColumn;
1161 }
1162 if (Shift <= 0)
1163 continue;
1164
1165 setChangeSpaces(i, Changes[i].Spaces + Shift);
1166 }
1167}
1168
1169void WhitespaceManager::alignEscapedNewlines() {
1170 const auto Align = Style.AlignEscapedNewlines;
1171 if (Align == FormatStyle::ENAS_DontAlign)
1172 return;
1173
1174 const bool WithLastLine = Align == FormatStyle::ENAS_LeftWithLastLine;
1175 const bool AlignLeft = Align == FormatStyle::ENAS_Left || WithLastLine;
1176 const auto MaxColumn = Style.ColumnLimit;
1177 unsigned MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
1178 unsigned StartOfMacro = 0;
1179 for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
1180 Change &C = Changes[i];
1181 if (C.NewlinesBefore == 0 && (!WithLastLine || C.Tok->isNot(tok::eof)))
1182 continue;
1183 const bool InPPDirective = C.ContinuesPPDirective;
1184 const auto BackslashColumn = C.PreviousEndOfTokenColumn + 2;
1185 if (InPPDirective ||
1186 (WithLastLine && (MaxColumn == 0 || BackslashColumn <= MaxColumn))) {
1187 MaxEndOfLine = std::max(BackslashColumn, MaxEndOfLine);
1188 }
1189 if (!InPPDirective) {
1190 alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
1191 MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
1192 StartOfMacro = i;
1193 }
1194 }
1195 alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
1196}
1197
1198void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
1199 unsigned Column) {
1200 for (unsigned i = Start; i < End; ++i) {
1201 Change &C = Changes[i];
1202 if (C.NewlinesBefore > 0) {
1203 assert(C.ContinuesPPDirective);
1204 if (C.PreviousEndOfTokenColumn + 1 > Column)
1205 C.EscapedNewlineColumn = 0;
1206 else
1207 C.EscapedNewlineColumn = Column;
1208 }
1209 }
1210}
1211
1212void WhitespaceManager::alignArrayInitializers() {
1213 if (Style.AlignArrayOfStructures == FormatStyle::AIAS_None)
1214 return;
1215
1216 for (unsigned ChangeIndex = 1U, ChangeEnd = Changes.size();
1217 ChangeIndex < ChangeEnd; ++ChangeIndex) {
1218 auto &C = Changes[ChangeIndex];
1219 if (C.Tok->IsArrayInitializer) {
1220 bool FoundComplete = false;
1221 for (unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
1222 ++InsideIndex) {
1223 const auto *Tok = Changes[InsideIndex].Tok;
1224 if (Tok->is(tok::pp_define))
1225 break;
1226 if (Tok == C.Tok->MatchingParen) {
1227 alignArrayInitializers(ChangeIndex, InsideIndex + 1);
1228 ChangeIndex = InsideIndex + 1;
1229 FoundComplete = true;
1230 break;
1231 }
1232 }
1233 if (!FoundComplete)
1234 ChangeIndex = ChangeEnd;
1235 }
1236 }
1237}
1238
1239void WhitespaceManager::alignArrayInitializers(unsigned Start, unsigned End) {
1240
1241 if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Right)
1242 alignArrayInitializersRightJustified(getCells(Start, End));
1243 else if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Left)
1244 alignArrayInitializersLeftJustified(getCells(Start, End));
1245}
1246
1247void WhitespaceManager::alignArrayInitializersRightJustified(
1248 CellDescriptions &&CellDescs) {
1249 if (!CellDescs.isRectangular())
1250 return;
1251
1252 const int BracePadding =
1253 Style.Cpp11BracedListStyle != FormatStyle::BLS_Block ? 0 : 1;
1254 auto &Cells = CellDescs.Cells;
1255 // Now go through and fixup the spaces.
1256 auto *CellIter = Cells.begin();
1257 for (auto i = 0U; i < CellDescs.CellCounts[0]; ++i, ++CellIter) {
1258 unsigned NetWidth = 0U;
1259 if (isSplitCell(*CellIter))
1260 NetWidth = getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1261 auto CellWidth = getMaximumCellWidth(CellIter, NetWidth);
1262
1263 if (Changes[CellIter->Index].Tok->is(tok::r_brace)) {
1264 // So in here we want to see if there is a brace that falls
1265 // on a line that was split. If so on that line we make sure that
1266 // the spaces in front of the brace are enough.
1267 const auto *Next = CellIter;
1268 do {
1269 const FormatToken *Previous = Changes[Next->Index].Tok->Previous;
1270 if (Previous && Previous->isNot(TT_LineComment)) {
1271 Changes[Next->Index].NewlinesBefore = 0;
1272 setChangeSpaces(Next->Index, BracePadding);
1273 }
1274 Next = Next->NextColumnElement;
1275 } while (Next);
1276 // Unless the array is empty, we need the position of all the
1277 // immediately adjacent cells
1278 if (CellIter != Cells.begin()) {
1279 auto ThisNetWidth =
1280 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1281 auto MaxNetWidth = getMaximumNetWidth(
1282 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1283 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1284 if (ThisNetWidth < MaxNetWidth)
1285 setChangeSpaces(CellIter->Index, MaxNetWidth - ThisNetWidth);
1286 auto RowCount = 1U;
1287 auto Offset = std::distance(Cells.begin(), CellIter);
1288 for (const auto *Next = CellIter->NextColumnElement; Next;
1289 Next = Next->NextColumnElement) {
1290 if (RowCount >= CellDescs.CellCounts.size())
1291 break;
1292 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1293 auto *End = Start + Offset;
1294 ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1295 if (ThisNetWidth < MaxNetWidth)
1296 setChangeSpaces(Next->Index, MaxNetWidth - ThisNetWidth);
1297 ++RowCount;
1298 }
1299 }
1300 } else {
1301 auto ThisWidth =
1302 calculateCellWidth(CellIter->Index, CellIter->EndIndex, true) +
1303 NetWidth;
1304 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1305 int Spaces = (CellWidth - (ThisWidth + NetWidth));
1306 Spaces += (i > 0) ? 1 : BracePadding;
1307
1308 setChangeSpaces(CellIter->Index, Spaces);
1309 }
1310 alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
1311 for (const auto *Next = CellIter->NextColumnElement; Next;
1312 Next = Next->NextColumnElement) {
1313 ThisWidth =
1314 calculateCellWidth(Next->Index, Next->EndIndex, true) + NetWidth;
1315 if (Changes[Next->Index].NewlinesBefore == 0) {
1316 int Spaces = (CellWidth - ThisWidth);
1317 Spaces += (i > 0) ? 1 : BracePadding;
1318
1319 setChangeSpaces(Next->Index, Spaces);
1320 }
1321 alignToStartOfCell(Next->Index, Next->EndIndex);
1322 }
1323 }
1324 }
1325}
1326
1327void WhitespaceManager::alignArrayInitializersLeftJustified(
1328 CellDescriptions &&CellDescs) {
1329
1330 if (!CellDescs.isRectangular())
1331 return;
1332
1333 const int BracePadding =
1334 Style.Cpp11BracedListStyle != FormatStyle::BLS_Block ? 0 : 1;
1335 auto &Cells = CellDescs.Cells;
1336 // Now go through and fixup the spaces.
1337 auto *CellIter = Cells.begin();
1338 // The first cell of every row needs to be against the left brace.
1339 for (const auto *Next = CellIter; Next; Next = Next->NextColumnElement) {
1340 auto &Change = Changes[Next->Index];
1341 int Spaces =
1342 Change.NewlinesBefore == 0 ? BracePadding : CellDescs.InitialSpaces;
1343 setChangeSpaces(Next->Index, Spaces);
1344 }
1345 ++CellIter;
1346 for (auto i = 1U; i < CellDescs.CellCounts[0]; i++, ++CellIter) {
1347 auto MaxNetWidth = getMaximumNetWidth(
1348 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1349 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1350 auto ThisNetWidth =
1351 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1352 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1353 int Spaces =
1354 MaxNetWidth - ThisNetWidth +
1355 (Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1
1356 : BracePadding);
1357 setChangeSpaces(CellIter->Index, Spaces);
1358 }
1359 auto RowCount = 1U;
1360 auto Offset = std::distance(Cells.begin(), CellIter);
1361 for (const auto *Next = CellIter->NextColumnElement; Next;
1362 Next = Next->NextColumnElement) {
1363 if (RowCount >= CellDescs.CellCounts.size())
1364 break;
1365 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1366 auto *End = Start + Offset;
1367 auto ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1368 if (Changes[Next->Index].NewlinesBefore == 0) {
1369 int Spaces =
1370 MaxNetWidth - ThisNetWidth +
1371 (Changes[Next->Index].Tok->isNot(tok::r_brace) ? 1 : BracePadding);
1372 setChangeSpaces(Next->Index, Spaces);
1373 }
1374 ++RowCount;
1375 }
1376 }
1377}
1378
1379bool WhitespaceManager::isSplitCell(const CellDescription &Cell) {
1380 if (Cell.HasSplit)
1381 return true;
1382 for (const auto *Next = Cell.NextColumnElement; Next;
1383 Next = Next->NextColumnElement) {
1384 if (Next->HasSplit)
1385 return true;
1386 }
1387 return false;
1388}
1389
1390WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
1391 unsigned End) {
1392
1393 unsigned Depth = 0;
1394 unsigned Cell = 0;
1395 SmallVector<unsigned> CellCounts;
1396 unsigned InitialSpaces = 0;
1397 unsigned InitialTokenLength = 0;
1398 unsigned EndSpaces = 0;
1399 SmallVector<CellDescription> Cells;
1400 const FormatToken *MatchingParen = nullptr;
1401 for (unsigned i = Start; i < End; ++i) {
1402 auto &C = Changes[i];
1403 if (C.Tok->is(tok::l_brace))
1404 ++Depth;
1405 else if (C.Tok->is(tok::r_brace))
1406 --Depth;
1407 if (Depth == 2) {
1408 if (C.Tok->is(tok::l_brace)) {
1409 Cell = 0;
1410 MatchingParen = C.Tok->MatchingParen;
1411 if (InitialSpaces == 0) {
1412 InitialSpaces = C.Spaces + C.TokenLength;
1413 InitialTokenLength = C.TokenLength;
1414 auto j = i - 1;
1415 for (; Changes[j].NewlinesBefore == 0 && j > Start; --j) {
1416 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1417 InitialTokenLength += Changes[j].TokenLength;
1418 }
1419 if (C.NewlinesBefore == 0) {
1420 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1421 InitialTokenLength += Changes[j].TokenLength;
1422 }
1423 }
1424 } else if (C.Tok->is(tok::comma)) {
1425 if (!Cells.empty())
1426 Cells.back().EndIndex = i;
1427 if (const auto *Next = C.Tok->getNextNonComment();
1428 Next && Next->isNot(tok::r_brace)) { // dangling comma
1429 ++Cell;
1430 }
1431 }
1432 } else if (Depth == 1) {
1433 if (C.Tok == MatchingParen) {
1434 if (!Cells.empty())
1435 Cells.back().EndIndex = i;
1436 Cells.push_back(CellDescription{i, ++Cell, i + 1, false, nullptr});
1437 CellCounts.push_back(C.Tok->Previous->isNot(tok::comma) ? Cell + 1
1438 : Cell);
1439 // Go to the next non-comment and ensure there is a break in front
1440 const auto *NextNonComment = C.Tok->getNextNonComment();
1441 while (NextNonComment && NextNonComment->is(tok::comma))
1442 NextNonComment = NextNonComment->getNextNonComment();
1443 auto j = i;
1444 while (j < End && Changes[j].Tok != NextNonComment)
1445 ++j;
1446 if (j < End && Changes[j].NewlinesBefore == 0 &&
1447 Changes[j].Tok->isNot(tok::r_brace)) {
1448 Changes[j].NewlinesBefore = 1;
1449 // Account for the added token lengths
1450 setChangeSpaces(j, InitialSpaces - InitialTokenLength);
1451 }
1452 } else if (C.Tok->is(tok::comment) && C.Tok->NewlinesBefore == 0) {
1453 // Trailing comments stay at a space past the last token
1454 setChangeSpaces(i, Changes[i - 1].Tok->is(tok::comma) ? 1 : 2);
1455 } else if (C.Tok->is(tok::l_brace)) {
1456 // We need to make sure that the ending braces is aligned to the
1457 // start of our initializer
1458 auto j = i - 1;
1459 for (; j > 0 && !Changes[j].Tok->ArrayInitializerLineStart; --j)
1460 ; // Nothing the loop does the work
1461 EndSpaces = Changes[j].Spaces;
1462 }
1463 } else if (Depth == 0 && C.Tok->is(tok::r_brace)) {
1464 C.NewlinesBefore = 1;
1465 setChangeSpaces(i, EndSpaces);
1466 }
1467 if (C.Tok->StartsColumn) {
1468 // This gets us past tokens that have been split over multiple
1469 // lines
1470 bool HasSplit = false;
1471 if (Changes[i].NewlinesBefore > 0) {
1472 // So if we split a line previously and the tail line + this token is
1473 // less then the column limit we remove the split here and just put
1474 // the column start at a space past the comma
1475 //
1476 // FIXME This if branch covers the cases where the column is not
1477 // the first column. This leads to weird pathologies like the formatting
1478 // auto foo = Items{
1479 // Section{
1480 // 0, bar(),
1481 // }
1482 // };
1483 // Well if it doesn't lead to that it's indicative that the line
1484 // breaking should be revisited. Unfortunately alot of other options
1485 // interact with this
1486 auto j = i - 1;
1487 if ((j - 1) > Start && Changes[j].Tok->is(tok::comma) &&
1488 Changes[j - 1].NewlinesBefore > 0) {
1489 --j;
1490 auto LineLimit = Changes[j].Spaces + Changes[j].TokenLength;
1491 if (LineLimit < Style.ColumnLimit) {
1492 Changes[i].NewlinesBefore = 0;
1493 setChangeSpaces(i, 1);
1494 }
1495 }
1496 }
1497 while (Changes[i].NewlinesBefore > 0 && Changes[i].Tok == C.Tok) {
1498 setChangeSpaces(i, InitialSpaces);
1499 ++i;
1500 HasSplit = true;
1501 }
1502 if (Changes[i].Tok != C.Tok)
1503 --i;
1504 Cells.push_back(CellDescription{i, Cell, i, HasSplit, nullptr});
1505 }
1506 }
1507
1508 return linkCells({Cells, CellCounts, InitialSpaces});
1509}
1510
1511unsigned WhitespaceManager::calculateCellWidth(unsigned Start, unsigned End,
1512 bool WithSpaces) const {
1513 unsigned CellWidth = 0;
1514 for (auto i = Start; i < End; i++) {
1515 if (Changes[i].NewlinesBefore > 0)
1516 CellWidth = 0;
1517 CellWidth += Changes[i].TokenLength;
1518 CellWidth += (WithSpaces ? Changes[i].Spaces : 0);
1519 }
1520 return CellWidth;
1521}
1522
1523void WhitespaceManager::alignToStartOfCell(unsigned Start, unsigned End) {
1524 if ((End - Start) <= 1)
1525 return;
1526 // If the line is broken anywhere in there make sure everything
1527 // is aligned to the parent
1528 for (auto i = Start + 1; i < End; i++)
1529 if (Changes[i].NewlinesBefore > 0)
1530 setChangeSpaces(i, Changes[Start].Spaces);
1531}
1532
1533WhitespaceManager::CellDescriptions
1534WhitespaceManager::linkCells(CellDescriptions &&CellDesc) {
1535 auto &Cells = CellDesc.Cells;
1536 for (auto *CellIter = Cells.begin(); CellIter != Cells.end(); ++CellIter) {
1537 if (!CellIter->NextColumnElement && (CellIter + 1) != Cells.end()) {
1538 for (auto *NextIter = CellIter + 1; NextIter != Cells.end(); ++NextIter) {
1539 if (NextIter->Cell == CellIter->Cell) {
1540 CellIter->NextColumnElement = &(*NextIter);
1541 break;
1542 }
1543 }
1544 }
1545 }
1546 return std::move(CellDesc);
1547}
1548
1549void WhitespaceManager::setChangeSpaces(unsigned Start, unsigned Spaces) {
1550 SetChangeSpaces(Start, Spaces, Changes);
1551}
1552
1553void WhitespaceManager::generateChanges() {
1554 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
1555 const Change &C = Changes[i];
1556 if (i > 0) {
1557 auto Last = Changes[i - 1].OriginalWhitespaceRange;
1558 auto New = Changes[i].OriginalWhitespaceRange;
1559 // Do not generate two replacements for the same location. As a special
1560 // case, it is allowed if there is a replacement for the empty range
1561 // between 2 tokens and another non-empty range at the start of the second
1562 // token. We didn't implement logic to combine replacements for 2
1563 // consecutive source ranges into a single replacement, because the
1564 // program works fine without it.
1565 //
1566 // We can't eliminate empty original whitespace ranges. They appear when
1567 // 2 tokens have no whitespace in between in the input. It does not
1568 // matter whether whitespace is to be added. If no whitespace is to be
1569 // added, the replacement will be empty, and it gets eliminated after this
1570 // step in storeReplacement. For example, if the input is `foo();`,
1571 // there will be a replacement for the range between every consecutive
1572 // pair of tokens.
1573 //
1574 // A replacement at the start of a token can be added by
1575 // BreakableStringLiteralUsingOperators::insertBreak when it adds braces
1576 // around the string literal. Say Verilog code is being formatted and the
1577 // first line is to become the next 2 lines.
1578 // x("long string");
1579 // x({"long ",
1580 // "string"});
1581 // There will be a replacement for the empty range between the parenthesis
1582 // and the string and another replacement for the quote character. The
1583 // replacement for the empty range between the parenthesis and the quote
1584 // comes from ContinuationIndenter::addTokenOnCurrentLine when it changes
1585 // the original empty range between the parenthesis and the string to
1586 // another empty one. The replacement for the quote character comes from
1587 // BreakableStringLiteralUsingOperators::insertBreak when it adds the
1588 // brace. In the example, the replacement for the empty range is the same
1589 // as the original text. However, eliminating replacements that are same
1590 // as the original does not help in general. For example, a newline can
1591 // be inserted, causing the first line to become the next 3 lines.
1592 // xxxxxxxxxxx("long string");
1593 // xxxxxxxxxxx(
1594 // {"long ",
1595 // "string"});
1596 // In that case, the empty range between the parenthesis and the string
1597 // will be replaced by a newline and 4 spaces. So we will still have to
1598 // deal with a replacement for an empty source range followed by a
1599 // replacement for a non-empty source range.
1600 if (Last.getBegin() == New.getBegin() &&
1601 (Last.getEnd() != Last.getBegin() ||
1602 New.getEnd() == New.getBegin())) {
1603 continue;
1604 }
1605 }
1606 if (C.CreateReplacement) {
1607 std::string ReplacementText = C.PreviousLinePostfix;
1608 if (C.ContinuesPPDirective) {
1609 appendEscapedNewlineText(ReplacementText, C.NewlinesBefore,
1610 C.PreviousEndOfTokenColumn,
1611 C.EscapedNewlineColumn);
1612 } else {
1613 appendNewlineText(ReplacementText, C);
1614 }
1615 // FIXME: This assert should hold if we computed the column correctly.
1616 // assert((int)C.StartOfTokenColumn >= C.Spaces);
1617 unsigned IndentLevel = indentLevelFor(C);
1618 appendIndentText(ReplacementText, IndentLevel, std::max(0, C.Spaces),
1619 std::max((int)C.StartOfTokenColumn, C.Spaces) -
1620 std::max(0, C.Spaces),
1621 C.AlignedTo);
1622 C.Tok->AppliedIndentLevel =
1623 C.AlignedTo ? IndentLevel : std::max(0, C.Spaces) / Style.IndentWidth;
1624 ReplacementText.append(C.CurrentLinePrefix);
1625 storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
1626 }
1627 }
1628}
1629
1630void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
1631 unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
1632 SourceMgr.getFileOffset(Range.getBegin());
1633 // Don't create a replacement, if it does not change anything.
1634 if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
1635 WhitespaceLength) == Text) {
1636 return;
1637 }
1638 auto Err = Replaces.add(tooling::Replacement(
1639 SourceMgr, CharSourceRange::getCharRange(Range), Text));
1640 // FIXME: better error handling. For now, just print an error message in the
1641 // release version.
1642 if (Err) {
1643 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
1644 assert(false);
1645 }
1646}
1647
1648void WhitespaceManager::appendNewlineText(std::string &Text, const Change &C) {
1649 if (C.NewlinesBefore <= 0)
1650 return;
1651
1652 StringRef Newline = UseCRLF ? "\r\n" : "\n";
1653 Text.append(Newline);
1654
1655 if (C.Tok->HasFormFeedBefore)
1656 Text.append("\f");
1657
1658 for (unsigned I = 1; I < C.NewlinesBefore; ++I)
1659 Text.append(Newline);
1660}
1661
1662void WhitespaceManager::appendEscapedNewlineText(
1663 std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
1664 unsigned EscapedNewlineColumn) {
1665 if (Newlines > 0) {
1666 unsigned Spaces =
1667 std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
1668 for (unsigned i = 0; i < Newlines; ++i) {
1669 Text.append(Spaces, ' ');
1670 Text.append(UseCRLF ? "\\\r\n" : "\\\n");
1671 Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
1672 }
1673 }
1674}
1675
1676void WhitespaceManager::appendIndentText(std::string &Text,
1677 unsigned IndentLevel, unsigned Spaces,
1678 unsigned WhitespaceStartColumn,
1679 bool IsAligned) {
1680 switch (Style.UseTab) {
1681 case FormatStyle::UT_Never:
1682 Text.append(Spaces, ' ');
1683 break;
1684 case FormatStyle::UT_Always: {
1685 if (Style.TabWidth) {
1686 unsigned FirstTabWidth =
1687 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
1688
1689 // Insert only spaces when we want to end up before the next tab.
1690 if (Spaces < FirstTabWidth || Spaces == 1) {
1691 Text.append(Spaces, ' ');
1692 break;
1693 }
1694 // Align to the next tab.
1695 Spaces -= FirstTabWidth;
1696 Text.append("\t");
1697
1698 Text.append(Spaces / Style.TabWidth, '\t');
1699 Text.append(Spaces % Style.TabWidth, ' ');
1700 } else if (Spaces == 1) {
1701 Text.append(Spaces, ' ');
1702 }
1703 break;
1704 }
1705 case FormatStyle::UT_ForIndentation:
1706 if (WhitespaceStartColumn == 0) {
1707 unsigned Indentation = IndentLevel * Style.IndentWidth;
1708 Spaces = appendTabIndent(Text, Spaces, Indentation);
1709 }
1710 Text.append(Spaces, ' ');
1711 break;
1712 case FormatStyle::UT_ForContinuationAndIndentation:
1713 if (WhitespaceStartColumn == 0)
1714 Spaces = appendTabIndent(Text, Spaces, Spaces);
1715 Text.append(Spaces, ' ');
1716 break;
1717 case FormatStyle::UT_AlignWithSpaces:
1718 if (WhitespaceStartColumn == 0) {
1719 unsigned Indentation =
1720 IsAligned ? IndentLevel * Style.IndentWidth : Spaces;
1721 Spaces = appendTabIndent(Text, Spaces, Indentation);
1722 }
1723 Text.append(Spaces, ' ');
1724 break;
1725 }
1726}
1727
1728unsigned WhitespaceManager::appendTabIndent(std::string &Text, unsigned Spaces,
1729 unsigned Indentation) {
1730 // This happens, e.g. when a line in a block comment is indented less than the
1731 // first one.
1732 if (Indentation > Spaces)
1733 Indentation = Spaces;
1734 if (Style.TabWidth) {
1735 unsigned Tabs = Indentation / Style.TabWidth;
1736 Text.append(Tabs, '\t');
1737 Spaces -= Tabs * Style.TabWidth;
1738 }
1739 return Spaces;
1740}
1741
1742} // namespace format
1743} // 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.