clang 17.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
19namespace clang {
20namespace format {
21
23 const Change &C1, const Change &C2) const {
24 return SourceMgr.isBeforeInTranslationUnit(
27}
28
32 int Spaces, unsigned StartOfTokenColumn,
33 unsigned NewlinesBefore,
34 StringRef PreviousLinePostfix,
35 StringRef CurrentLinePrefix, bool IsAligned,
46}
47
49 unsigned Spaces,
50 unsigned StartOfTokenColumn,
51 bool IsAligned, bool InPPDirective) {
52 if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
53 return;
54 Tok.setDecision((Newlines > 0) ? FD_Break : FD_Continue);
55 Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
56 Spaces, StartOfTokenColumn, Newlines, "", "",
57 IsAligned, InPPDirective && !Tok.IsFirst,
58 /*IsInsideToken=*/false));
59}
60
62 bool InPPDirective) {
63 if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
64 return;
65 Changes.push_back(Change(Tok, /*CreateReplacement=*/false,
66 Tok.WhitespaceRange, /*Spaces=*/0,
67 Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
68 /*IsAligned=*/false, InPPDirective && !Tok.IsFirst,
69 /*IsInsideToken=*/false));
70}
71
72llvm::Error
74 return Replaces.add(Replacement);
75}
76
77bool WhitespaceManager::inputUsesCRLF(StringRef Text, bool DefaultToCRLF) {
78 size_t LF = Text.count('\n');
79 size_t CR = Text.count('\r') * 2;
80 return LF == CR ? DefaultToCRLF : CR > LF;
81}
82
84 const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
85 StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
86 unsigned Newlines, int Spaces) {
87 if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
88 return;
90 Changes.push_back(
91 Change(Tok, /*CreateReplacement=*/true,
92 SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
93 std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
94 /*IsAligned=*/true, InPPDirective && !Tok.IsFirst,
95 /*IsInsideToken=*/true));
96}
97
99 if (Changes.empty())
100 return Replaces;
101
102 llvm::sort(Changes, Change::IsBeforeInFile(SourceMgr));
103 calculateLineBreakInformation();
104 alignConsecutiveMacros();
105 alignConsecutiveDeclarations();
106 alignConsecutiveBitFields();
107 alignConsecutiveAssignments();
108 alignChainedConditionals();
109 alignTrailingComments();
110 alignEscapedNewlines();
111 alignArrayInitializers();
112 generateChanges();
113
114 return Replaces;
115}
116
117void WhitespaceManager::calculateLineBreakInformation() {
118 Changes[0].PreviousEndOfTokenColumn = 0;
119 Change *LastOutsideTokenChange = &Changes[0];
120 for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
121 SourceLocation OriginalWhitespaceStart =
122 Changes[i].OriginalWhitespaceRange.getBegin();
123 SourceLocation PreviousOriginalWhitespaceEnd =
124 Changes[i - 1].OriginalWhitespaceRange.getEnd();
125 unsigned OriginalWhitespaceStartOffset =
126 SourceMgr.getFileOffset(OriginalWhitespaceStart);
127 unsigned PreviousOriginalWhitespaceEndOffset =
128 SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
129 assert(PreviousOriginalWhitespaceEndOffset <=
130 OriginalWhitespaceStartOffset);
131 const char *const PreviousOriginalWhitespaceEndData =
132 SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
133 StringRef Text(PreviousOriginalWhitespaceEndData,
134 SourceMgr.getCharacterData(OriginalWhitespaceStart) -
135 PreviousOriginalWhitespaceEndData);
136 // Usually consecutive changes would occur in consecutive tokens. This is
137 // not the case however when analyzing some preprocessor runs of the
138 // annotated lines. For example, in this code:
139 //
140 // #if A // line 1
141 // int i = 1;
142 // #else B // line 2
143 // int i = 2;
144 // #endif // line 3
145 //
146 // one of the runs will produce the sequence of lines marked with line 1, 2
147 // and 3. So the two consecutive whitespace changes just before '// line 2'
148 // and before '#endif // line 3' span multiple lines and tokens:
149 //
150 // #else B{change X}[// line 2
151 // int i = 2;
152 // ]{change Y}#endif // line 3
153 //
154 // For this reason, if the text between consecutive changes spans multiple
155 // newlines, the token length must be adjusted to the end of the original
156 // line of the token.
157 auto NewlinePos = Text.find_first_of('\n');
158 if (NewlinePos == StringRef::npos) {
159 Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
160 PreviousOriginalWhitespaceEndOffset +
161 Changes[i].PreviousLinePostfix.size() +
162 Changes[i - 1].CurrentLinePrefix.size();
163 } else {
164 Changes[i - 1].TokenLength =
165 NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
166 }
167
168 // If there are multiple changes in this token, sum up all the changes until
169 // the end of the line.
170 if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0) {
171 LastOutsideTokenChange->TokenLength +=
172 Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
173 } else {
174 LastOutsideTokenChange = &Changes[i - 1];
175 }
176
177 Changes[i].PreviousEndOfTokenColumn =
178 Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
179
180 Changes[i - 1].IsTrailingComment =
181 (Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
182 (Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
183 Changes[i - 1].Tok->is(tok::comment) &&
184 // FIXME: This is a dirty hack. The problem is that
185 // BreakableLineCommentSection does comment reflow changes and here is
186 // the aligning of trailing comments. Consider the case where we reflow
187 // the second line up in this example:
188 //
189 // // line 1
190 // // line 2
191 //
192 // That amounts to 2 changes by BreakableLineCommentSection:
193 // - the first, delimited by (), for the whitespace between the tokens,
194 // - and second, delimited by [], for the whitespace at the beginning
195 // of the second token:
196 //
197 // // line 1(
198 // )[// ]line 2
199 //
200 // So in the end we have two changes like this:
201 //
202 // // line1()[ ]line 2
203 //
204 // Note that the OriginalWhitespaceStart of the second change is the
205 // same as the PreviousOriginalWhitespaceEnd of the first change.
206 // In this case, the below check ensures that the second change doesn't
207 // get treated as a trailing comment change here, since this might
208 // trigger additional whitespace to be wrongly inserted before "line 2"
209 // by the comment aligner here.
210 //
211 // For a proper solution we need a mechanism to say to WhitespaceManager
212 // that a particular change breaks the current sequence of trailing
213 // comments.
214 OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
215 }
216 // FIXME: The last token is currently not always an eof token; in those
217 // cases, setting TokenLength of the last token to 0 is wrong.
218 Changes.back().TokenLength = 0;
219 Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
220
221 const WhitespaceManager::Change *LastBlockComment = nullptr;
222 for (auto &Change : Changes) {
223 // Reset the IsTrailingComment flag for changes inside of trailing comments
224 // so they don't get realigned later. Comment line breaks however still need
225 // to be aligned.
226 if (Change.IsInsideToken && Change.NewlinesBefore == 0)
227 Change.IsTrailingComment = false;
228 Change.StartOfBlockComment = nullptr;
229 Change.IndentationOffset = 0;
230 if (Change.Tok->is(tok::comment)) {
231 if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken) {
232 LastBlockComment = &Change;
233 } else if ((Change.StartOfBlockComment = LastBlockComment)) {
234 Change.IndentationOffset =
235 Change.StartOfTokenColumn -
236 Change.StartOfBlockComment->StartOfTokenColumn;
237 }
238 } else {
239 LastBlockComment = nullptr;
240 }
241 }
242
243 // Compute conditional nesting level
244 // Level is increased for each conditional, unless this conditional continues
245 // a chain of conditional, i.e. starts immediately after the colon of another
246 // conditional.
247 SmallVector<bool, 16> ScopeStack;
248 int ConditionalsLevel = 0;
249 for (auto &Change : Changes) {
250 for (unsigned i = 0, e = Change.Tok->FakeLParens.size(); i != e; ++i) {
251 bool isNestedConditional =
252 Change.Tok->FakeLParens[e - 1 - i] == prec::Conditional &&
253 !(i == 0 && Change.Tok->Previous &&
254 Change.Tok->Previous->is(TT_ConditionalExpr) &&
255 Change.Tok->Previous->is(tok::colon));
256 if (isNestedConditional)
257 ++ConditionalsLevel;
258 ScopeStack.push_back(isNestedConditional);
259 }
260
261 Change.ConditionalsLevel = ConditionalsLevel;
262
263 for (unsigned i = Change.Tok->FakeRParens; i > 0 && ScopeStack.size(); --i)
264 if (ScopeStack.pop_back_val())
265 --ConditionalsLevel;
266 }
267}
268
269// Align a single sequence of tokens, see AlignTokens below.
270// Column - The token for which Matches returns true is moved to this column.
271// RightJustify - Whether it is the token's right end or left end that gets
272// moved to that column.
273template <typename F>
274static void
275AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
276 unsigned Column, bool RightJustify, F &&Matches,
278 bool FoundMatchOnLine = false;
279 int Shift = 0;
280
281 // ScopeStack keeps track of the current scope depth. It contains indices of
282 // the first token on each scope.
283 // We only run the "Matches" function on tokens from the outer-most scope.
284 // However, we do need to pay special attention to one class of tokens
285 // that are not in the outer-most scope, and that is function parameters
286 // which are split across multiple lines, as illustrated by this example:
287 // double a(int x);
288 // int b(int y,
289 // double z);
290 // In the above example, we need to take special care to ensure that
291 // 'double z' is indented along with it's owning function 'b'.
292 // The same holds for calling a function:
293 // double a = foo(x);
294 // int b = bar(foo(y),
295 // foor(z));
296 // Similar for broken string literals:
297 // double x = 3.14;
298 // auto s = "Hello"
299 // "World";
300 // Special handling is required for 'nested' ternary operators.
301 SmallVector<unsigned, 16> ScopeStack;
302
303 for (unsigned i = Start; i != End; ++i) {
304 if (ScopeStack.size() != 0 &&
305 Changes[i].indentAndNestingLevel() <
306 Changes[ScopeStack.back()].indentAndNestingLevel()) {
307 ScopeStack.pop_back();
308 }
309
310 // Compare current token to previous non-comment token to ensure whether
311 // it is in a deeper scope or not.
312 unsigned PreviousNonComment = i - 1;
313 while (PreviousNonComment > Start &&
314 Changes[PreviousNonComment].Tok->is(tok::comment)) {
315 --PreviousNonComment;
316 }
317 if (i != Start && Changes[i].indentAndNestingLevel() >
318 Changes[PreviousNonComment].indentAndNestingLevel()) {
319 ScopeStack.push_back(i);
320 }
321
322 bool InsideNestedScope = ScopeStack.size() != 0;
323 bool ContinuedStringLiteral = i > Start &&
324 Changes[i].Tok->is(tok::string_literal) &&
325 Changes[i - 1].Tok->is(tok::string_literal);
326 bool SkipMatchCheck = InsideNestedScope || ContinuedStringLiteral;
327
328 if (Changes[i].NewlinesBefore > 0 && !SkipMatchCheck) {
329 Shift = 0;
330 FoundMatchOnLine = false;
331 }
332
333 // If this is the first matching token to be aligned, remember by how many
334 // spaces it has to be shifted, so the rest of the changes on the line are
335 // shifted by the same amount
336 if (!FoundMatchOnLine && !SkipMatchCheck && Matches(Changes[i])) {
337 FoundMatchOnLine = true;
338 Shift = Column - (RightJustify ? Changes[i].TokenLength : 0) -
339 Changes[i].StartOfTokenColumn;
340 Changes[i].Spaces += Shift;
341 // FIXME: This is a workaround that should be removed when we fix
342 // http://llvm.org/PR53699. An assertion later below verifies this.
343 if (Changes[i].NewlinesBefore == 0) {
344 Changes[i].Spaces =
345 std::max(Changes[i].Spaces,
346 static_cast<int>(Changes[i].Tok->SpacesRequiredBefore));
347 }
348 }
349
350 // This is for function parameters that are split across multiple lines,
351 // as mentioned in the ScopeStack comment.
352 if (InsideNestedScope && Changes[i].NewlinesBefore > 0) {
353 unsigned ScopeStart = ScopeStack.back();
354 auto ShouldShiftBeAdded = [&] {
355 // Function declaration
356 if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
357 return true;
358
359 // Lambda.
360 if (Changes[ScopeStart - 1].Tok->is(TT_LambdaLBrace))
361 return false;
362
363 // Continued function declaration
364 if (ScopeStart > Start + 1 &&
365 Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) {
366 return true;
367 }
368
369 // Continued function call
370 if (ScopeStart > Start + 1 &&
371 Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
372 Changes[ScopeStart - 1].Tok->is(tok::l_paren) &&
373 Changes[ScopeStart].Tok->isNot(TT_LambdaLSquare)) {
374 if (Changes[i].Tok->MatchingParen &&
375 Changes[i].Tok->MatchingParen->is(TT_LambdaLBrace)) {
376 return false;
377 }
378 if (Changes[ScopeStart].NewlinesBefore > 0)
379 return false;
380 if (Changes[i].Tok->is(tok::l_brace) &&
381 Changes[i].Tok->is(BK_BracedInit)) {
382 return true;
383 }
384 return Style.BinPackArguments;
385 }
386
387 // Ternary operator
388 if (Changes[i].Tok->is(TT_ConditionalExpr))
389 return true;
390
391 // Period Initializer .XXX = 1.
392 if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
393 return true;
394
395 // Continued ternary operator
396 if (Changes[i].Tok->Previous &&
397 Changes[i].Tok->Previous->is(TT_ConditionalExpr)) {
398 return true;
399 }
400
401 // Continued direct-list-initialization using braced list.
402 if (ScopeStart > Start + 1 &&
403 Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
404 Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
405 Changes[i].Tok->is(tok::l_brace) &&
406 Changes[i].Tok->is(BK_BracedInit)) {
407 return true;
408 }
409
410 // Continued braced list.
411 if (ScopeStart > Start + 1 &&
412 Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
413 Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
414 Changes[i].Tok->isNot(tok::r_brace)) {
415 for (unsigned OuterScopeStart : llvm::reverse(ScopeStack)) {
416 // Lambda.
417 if (OuterScopeStart > Start &&
418 Changes[OuterScopeStart - 1].Tok->is(TT_LambdaLBrace)) {
419 return false;
420 }
421 }
422 if (Changes[ScopeStart].NewlinesBefore > 0)
423 return false;
424 return true;
425 }
426
427 return false;
428 };
429
430 if (ShouldShiftBeAdded())
431 Changes[i].Spaces += Shift;
432 }
433
434 if (ContinuedStringLiteral)
435 Changes[i].Spaces += Shift;
436
437 // We should not remove required spaces unless we break the line before.
438 assert(Shift >= 0 || Changes[i].NewlinesBefore > 0 ||
439 Changes[i].Spaces >=
440 static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) ||
441 Changes[i].Tok->is(tok::eof));
442
443 Changes[i].StartOfTokenColumn += Shift;
444 if (i + 1 != Changes.size())
445 Changes[i + 1].PreviousEndOfTokenColumn += Shift;
446
447 // If PointerAlignment is PAS_Right, keep *s or &s next to the token
448 if (Style.PointerAlignment == FormatStyle::PAS_Right &&
449 Changes[i].Spaces != 0) {
450 for (int Previous = i - 1;
451 Previous >= 0 &&
452 Changes[Previous].Tok->getType() == TT_PointerOrReference;
453 --Previous) {
454 Changes[Previous + 1].Spaces -= Shift;
455 Changes[Previous].Spaces += Shift;
456 Changes[Previous].StartOfTokenColumn += Shift;
457 }
458 }
459 }
460}
461
462// Walk through a subset of the changes, starting at StartAt, and find
463// sequences of matching tokens to align. To do so, keep track of the lines and
464// whether or not a matching token was found on a line. If a matching token is
465// found, extend the current sequence. If the current line cannot be part of a
466// sequence, e.g. because there is an empty line before it or it contains only
467// non-matching tokens, finalize the previous sequence.
468// The value returned is the token on which we stopped, either because we
469// exhausted all items inside Changes, or because we hit a scope level higher
470// than our initial scope.
471// This function is recursive. Each invocation processes only the scope level
472// equal to the initial level, which is the level of Changes[StartAt].
473// If we encounter a scope level greater than the initial level, then we call
474// ourselves recursively, thereby avoiding the pollution of the current state
475// with the alignment requirements of the nested sub-level. This recursive
476// behavior is necessary for aligning function prototypes that have one or more
477// arguments.
478// If this function encounters a scope level less than the initial level,
479// it returns the current position.
480// There is a non-obvious subtlety in the recursive behavior: Even though we
481// defer processing of nested levels to recursive invocations of this
482// function, when it comes time to align a sequence of tokens, we run the
483// alignment on the entire sequence, including the nested levels.
484// When doing so, most of the nested tokens are skipped, because their
485// alignment was already handled by the recursive invocations of this function.
486// However, the special exception is that we do NOT skip function parameters
487// that are split across multiple lines. See the test case in FormatTest.cpp
488// that mentions "split function parameter alignment" for an example of this.
489// When the parameter RightJustify is true, the operator will be
490// right-justified. It is used to align compound assignments like `+=` and `=`.
491// When RightJustify and ACS.PadOperators are true, operators in each block to
492// be aligned will be padded on the left to the same length before aligning.
493template <typename F>
494static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
496 unsigned StartAt,
498 bool RightJustify = false) {
499 // We arrange each line in 3 parts. The operator to be aligned (the anchor),
500 // and text to its left and right. In the aligned text the width of each part
501 // will be the maximum of that over the block that has been aligned. Maximum
502 // widths of each part so far. When RightJustify is true and ACS.PadOperators
503 // is false, the part from start of line to the right end of the anchor.
504 // Otherwise, only the part to the left of the anchor. Including the space
505 // that exists on its left from the start. Not including the padding added on
506 // the left to right-justify the anchor.
507 unsigned WidthLeft = 0;
508 // The operator to be aligned when RightJustify is true and ACS.PadOperators
509 // is false. 0 otherwise.
510 unsigned WidthAnchor = 0;
511 // Width to the right of the anchor. Plus width of the anchor when
512 // RightJustify is false.
513 unsigned WidthRight = 0;
514
515 // Line number of the start and the end of the current token sequence.
516 unsigned StartOfSequence = 0;
517 unsigned EndOfSequence = 0;
518
519 // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
520 // abort when we hit any token in a higher scope than the starting one.
521 auto IndentAndNestingLevel = StartAt < Changes.size()
522 ? Changes[StartAt].indentAndNestingLevel()
523 : std::tuple<unsigned, unsigned, unsigned>();
524
525 // Keep track if the first token has a non-zero indent and nesting level.
526 // This can happen when aligning the contents of "#else" preprocessor blocks,
527 // which is done separately.
528 bool HasInitialIndentAndNesting =
529 StartAt == 0 &&
530 IndentAndNestingLevel > std::tuple<unsigned, unsigned, unsigned>();
531
532 // Keep track of the number of commas before the matching tokens, we will only
533 // align a sequence of matching tokens if they are preceded by the same number
534 // of commas.
535 unsigned CommasBeforeLastMatch = 0;
536 unsigned CommasBeforeMatch = 0;
537
538 // Whether a matching token has been found on the current line.
539 bool FoundMatchOnLine = false;
540
541 // Whether the current line consists purely of comments.
542 bool LineIsComment = true;
543
544 // Aligns a sequence of matching tokens, on the MinColumn column.
545 //
546 // Sequences start from the first matching token to align, and end at the
547 // first token of the first line that doesn't need to be aligned.
548 //
549 // We need to adjust the StartOfTokenColumn of each Change that is on a line
550 // containing any matching token to be aligned and located after such token.
551 auto AlignCurrentSequence = [&] {
552 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
553 AlignTokenSequence(Style, StartOfSequence, EndOfSequence,
554 WidthLeft + WidthAnchor, RightJustify, Matches,
555 Changes);
556 }
557 WidthLeft = 0;
558 WidthAnchor = 0;
559 WidthRight = 0;
560 StartOfSequence = 0;
561 EndOfSequence = 0;
562 };
563
564 unsigned i = StartAt;
565 for (unsigned e = Changes.size(); i != e; ++i) {
566 if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel) {
567 if (!HasInitialIndentAndNesting)
568 break;
569 // The contents of preprocessor blocks are aligned separately.
570 // If the initial preprocessor block is indented or nested (e.g. it's in
571 // a function), do not align and exit after finishing this scope block.
572 // Instead, align, and then lower the baseline indent and nesting level
573 // in order to continue aligning subsequent blocks.
574 EndOfSequence = i;
575 AlignCurrentSequence();
576 IndentAndNestingLevel =
577 Changes[i].indentAndNestingLevel(); // new baseline
578 }
579
580 if (Changes[i].NewlinesBefore != 0) {
581 CommasBeforeMatch = 0;
582 EndOfSequence = i;
583
584 // Whether to break the alignment sequence because of an empty line.
585 bool EmptyLineBreak =
586 (Changes[i].NewlinesBefore > 1) && !ACS.AcrossEmptyLines;
587
588 // Whether to break the alignment sequence because of a line without a
589 // match.
590 bool NoMatchBreak =
591 !FoundMatchOnLine && !(LineIsComment && ACS.AcrossComments);
592
593 if (EmptyLineBreak || NoMatchBreak)
594 AlignCurrentSequence();
595
596 // A new line starts, re-initialize line status tracking bools.
597 // Keep the match state if a string literal is continued on this line.
598 if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
599 !Changes[i - 1].Tok->is(tok::string_literal)) {
600 FoundMatchOnLine = false;
601 }
602 LineIsComment = true;
603 }
604
605 if (!Changes[i].Tok->is(tok::comment))
606 LineIsComment = false;
607
608 if (Changes[i].Tok->is(tok::comma)) {
609 ++CommasBeforeMatch;
610 } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
611 // Call AlignTokens recursively, skipping over this scope block.
612 unsigned StoppedAt =
613 AlignTokens(Style, Matches, Changes, i, ACS, RightJustify);
614 i = StoppedAt - 1;
615 continue;
616 }
617
618 if (!Matches(Changes[i]))
619 continue;
620
621 // If there is more than one matching token per line, or if the number of
622 // preceding commas, do not match anymore, end the sequence.
623 if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
624 AlignCurrentSequence();
625
626 CommasBeforeLastMatch = CommasBeforeMatch;
627 FoundMatchOnLine = true;
628
629 if (StartOfSequence == 0)
630 StartOfSequence = i;
631
632 unsigned ChangeWidthLeft = Changes[i].StartOfTokenColumn;
633 unsigned ChangeWidthAnchor = 0;
634 unsigned ChangeWidthRight = 0;
635 if (RightJustify)
636 if (ACS.PadOperators)
637 ChangeWidthAnchor = Changes[i].TokenLength;
638 else
639 ChangeWidthLeft += Changes[i].TokenLength;
640 else
641 ChangeWidthRight = Changes[i].TokenLength;
642 for (unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) {
643 ChangeWidthRight += Changes[j].Spaces;
644 // Changes are generally 1:1 with the tokens, but a change could also be
645 // inside of a token, in which case it's counted more than once: once for
646 // the whitespace surrounding the token (!IsInsideToken) and once for
647 // each whitespace change within it (IsInsideToken).
648 // Therefore, changes inside of a token should only count the space.
649 if (!Changes[j].IsInsideToken)
650 ChangeWidthRight += Changes[j].TokenLength;
651 }
652
653 // If we are restricted by the maximum column width, end the sequence.
654 unsigned NewLeft = std::max(ChangeWidthLeft, WidthLeft);
655 unsigned NewAnchor = std::max(ChangeWidthAnchor, WidthAnchor);
656 unsigned NewRight = std::max(ChangeWidthRight, WidthRight);
657 // `ColumnLimit == 0` means there is no column limit.
658 if (Style.ColumnLimit != 0 &&
659 Style.ColumnLimit < NewLeft + NewAnchor + NewRight) {
660 AlignCurrentSequence();
661 StartOfSequence = i;
662 WidthLeft = ChangeWidthLeft;
663 WidthAnchor = ChangeWidthAnchor;
664 WidthRight = ChangeWidthRight;
665 } else {
666 WidthLeft = NewLeft;
667 WidthAnchor = NewAnchor;
668 WidthRight = NewRight;
669 }
670 }
671
672 EndOfSequence = i;
673 AlignCurrentSequence();
674 return i;
675}
676
677// Aligns a sequence of matching tokens, on the MinColumn column.
678//
679// Sequences start from the first matching token to align, and end at the
680// first token of the first line that doesn't need to be aligned.
681//
682// We need to adjust the StartOfTokenColumn of each Change that is on a line
683// containing any matching token to be aligned and located after such token.
685 unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn,
686 unsigned &MaxColumn, bool &FoundMatchOnLine,
687 std::function<bool(const WhitespaceManager::Change &C)> AlignMacrosMatches,
689 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
690
691 FoundMatchOnLine = false;
692 int Shift = 0;
693
694 for (unsigned I = StartOfSequence; I != EndOfSequence; ++I) {
695 if (Changes[I].NewlinesBefore > 0) {
696 Shift = 0;
697 FoundMatchOnLine = false;
698 }
699
700 // If this is the first matching token to be aligned, remember by how many
701 // spaces it has to be shifted, so the rest of the changes on the line are
702 // shifted by the same amount
703 if (!FoundMatchOnLine && AlignMacrosMatches(Changes[I])) {
704 FoundMatchOnLine = true;
705 Shift = MinColumn - Changes[I].StartOfTokenColumn;
706 Changes[I].Spaces += Shift;
707 }
708
709 assert(Shift >= 0);
710 Changes[I].StartOfTokenColumn += Shift;
711 if (I + 1 != Changes.size())
712 Changes[I + 1].PreviousEndOfTokenColumn += Shift;
713 }
714 }
715
716 MinColumn = 0;
717 MaxColumn = UINT_MAX;
718 StartOfSequence = 0;
719 EndOfSequence = 0;
720}
721
722void WhitespaceManager::alignConsecutiveMacros() {
723 if (!Style.AlignConsecutiveMacros.Enabled)
724 return;
725
726 auto AlignMacrosMatches = [](const Change &C) {
727 const FormatToken *Current = C.Tok;
728 unsigned SpacesRequiredBefore = 1;
729
730 if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
731 return false;
732
733 Current = Current->Previous;
734
735 // If token is a ")", skip over the parameter list, to the
736 // token that precedes the "("
737 if (Current->is(tok::r_paren) && Current->MatchingParen) {
738 Current = Current->MatchingParen->Previous;
739 SpacesRequiredBefore = 0;
740 }
741
742 if (!Current || !Current->is(tok::identifier))
743 return false;
744
745 if (!Current->Previous || !Current->Previous->is(tok::pp_define))
746 return false;
747
748 // For a macro function, 0 spaces are required between the
749 // identifier and the lparen that opens the parameter list.
750 // For a simple macro, 1 space is required between the
751 // identifier and the first token of the defined value.
752 return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
753 };
754
755 unsigned MinColumn = 0;
756 unsigned MaxColumn = UINT_MAX;
757
758 // Start and end of the token sequence we're processing.
759 unsigned StartOfSequence = 0;
760 unsigned EndOfSequence = 0;
761
762 // Whether a matching token has been found on the current line.
763 bool FoundMatchOnLine = false;
764
765 // Whether the current line consists only of comments
766 bool LineIsComment = true;
767
768 unsigned I = 0;
769 for (unsigned E = Changes.size(); I != E; ++I) {
770 if (Changes[I].NewlinesBefore != 0) {
771 EndOfSequence = I;
772
773 // Whether to break the alignment sequence because of an empty line.
774 bool EmptyLineBreak = (Changes[I].NewlinesBefore > 1) &&
775 !Style.AlignConsecutiveMacros.AcrossEmptyLines;
776
777 // Whether to break the alignment sequence because of a line without a
778 // match.
779 bool NoMatchBreak =
780 !FoundMatchOnLine &&
781 !(LineIsComment && Style.AlignConsecutiveMacros.AcrossComments);
782
783 if (EmptyLineBreak || NoMatchBreak) {
784 AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
785 FoundMatchOnLine, AlignMacrosMatches, Changes);
786 }
787
788 // A new line starts, re-initialize line status tracking bools.
789 FoundMatchOnLine = false;
790 LineIsComment = true;
791 }
792
793 if (!Changes[I].Tok->is(tok::comment))
794 LineIsComment = false;
795
796 if (!AlignMacrosMatches(Changes[I]))
797 continue;
798
799 FoundMatchOnLine = true;
800
801 if (StartOfSequence == 0)
802 StartOfSequence = I;
803
804 unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
805 int LineLengthAfter = -Changes[I].Spaces;
806 for (unsigned j = I; j != E && Changes[j].NewlinesBefore == 0; ++j)
807 LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
808 unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
809
810 MinColumn = std::max(MinColumn, ChangeMinColumn);
811 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
812 }
813
814 EndOfSequence = I;
815 AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
816 FoundMatchOnLine, AlignMacrosMatches, Changes);
817}
818
819void WhitespaceManager::alignConsecutiveAssignments() {
820 if (!Style.AlignConsecutiveAssignments.Enabled)
821 return;
822
824 Style,
825 [&](const Change &C) {
826 // Do not align on equal signs that are first on a line.
827 if (C.NewlinesBefore > 0)
828 return false;
829
830 // Do not align on equal signs that are last on a line.
831 if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
832 return false;
833
834 // Do not align operator= overloads.
835 FormatToken *Previous = C.Tok->getPreviousNonComment();
836 if (Previous && Previous->is(tok::kw_operator))
837 return false;
838
839 return Style.AlignConsecutiveAssignments.AlignCompound
840 ? C.Tok->getPrecedence() == prec::Assignment
841 : (C.Tok->is(tok::equal) ||
842 // In Verilog the '<=' is not a compound assignment, thus
843 // it is aligned even when the AlignCompound option is not
844 // set.
845 (Style.isVerilog() && C.Tok->is(tok::lessequal) &&
846 C.Tok->getPrecedence() == prec::Assignment));
847 },
848 Changes, /*StartAt=*/0, Style.AlignConsecutiveAssignments,
849 /*RightJustify=*/true);
850}
851
852void WhitespaceManager::alignConsecutiveBitFields() {
853 if (!Style.AlignConsecutiveBitFields.Enabled)
854 return;
855
857 Style,
858 [&](Change const &C) {
859 // Do not align on ':' that is first on a line.
860 if (C.NewlinesBefore > 0)
861 return false;
862
863 // Do not align on ':' that is last on a line.
864 if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
865 return false;
866
867 return C.Tok->is(TT_BitFieldColon);
868 },
869 Changes, /*StartAt=*/0, Style.AlignConsecutiveBitFields);
870}
871
872void WhitespaceManager::alignConsecutiveDeclarations() {
873 if (!Style.AlignConsecutiveDeclarations.Enabled)
874 return;
875
877 Style,
878 [](Change const &C) {
879 if (C.Tok->is(TT_FunctionDeclarationName))
880 return true;
881 if (C.Tok->isNot(TT_StartOfName))
882 return false;
883 if (C.Tok->Previous &&
884 C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
885 return false;
886 // Check if there is a subsequent name that starts the same declaration.
887 for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
888 if (Next->is(tok::comment))
889 continue;
890 if (Next->is(TT_PointerOrReference))
891 return false;
892 if (!Next->Tok.getIdentifierInfo())
893 break;
894 if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
895 tok::kw_operator)) {
896 return false;
897 }
898 }
899 return true;
900 },
901 Changes, /*StartAt=*/0, Style.AlignConsecutiveDeclarations);
902}
903
904void WhitespaceManager::alignChainedConditionals() {
905 if (Style.BreakBeforeTernaryOperators) {
907 Style,
908 [](Change const &C) {
909 // Align question operators and last colon
910 return C.Tok->is(TT_ConditionalExpr) &&
911 ((C.Tok->is(tok::question) && !C.NewlinesBefore) ||
912 (C.Tok->is(tok::colon) && C.Tok->Next &&
913 (C.Tok->Next->FakeLParens.size() == 0 ||
914 C.Tok->Next->FakeLParens.back() != prec::Conditional)));
915 },
916 Changes, /*StartAt=*/0);
917 } else {
918 static auto AlignWrappedOperand = [](Change const &C) {
919 FormatToken *Previous = C.Tok->getPreviousNonComment();
920 return C.NewlinesBefore && Previous && Previous->is(TT_ConditionalExpr) &&
921 (Previous->is(tok::colon) &&
922 (C.Tok->FakeLParens.size() == 0 ||
923 C.Tok->FakeLParens.back() != prec::Conditional));
924 };
925 // Ensure we keep alignment of wrapped operands with non-wrapped operands
926 // Since we actually align the operators, the wrapped operands need the
927 // extra offset to be properly aligned.
928 for (Change &C : Changes)
929 if (AlignWrappedOperand(C))
930 C.StartOfTokenColumn -= 2;
932 Style,
933 [this](Change const &C) {
934 // Align question operators if next operand is not wrapped, as
935 // well as wrapped operands after question operator or last
936 // colon in conditional sequence
937 return (C.Tok->is(TT_ConditionalExpr) && C.Tok->is(tok::question) &&
938 &C != &Changes.back() && (&C + 1)->NewlinesBefore == 0 &&
939 !(&C + 1)->IsTrailingComment) ||
940 AlignWrappedOperand(C);
941 },
942 Changes, /*StartAt=*/0);
943 }
944}
945
946void WhitespaceManager::alignTrailingComments() {
947 unsigned MinColumn = 0;
948 unsigned MaxColumn = UINT_MAX;
949 unsigned StartOfSequence = 0;
950 bool BreakBeforeNext = false;
951 unsigned Newlines = 0;
952 unsigned int NewLineThreshold = 1;
953 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Always)
954 NewLineThreshold = Style.AlignTrailingComments.OverEmptyLines + 1;
955
956 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
957 if (Changes[i].StartOfBlockComment)
958 continue;
959 Newlines += Changes[i].NewlinesBefore;
960 if (!Changes[i].IsTrailingComment)
961 continue;
962
963 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) {
964 auto OriginalSpaces =
965 Changes[i].OriginalWhitespaceRange.getEnd().getRawEncoding() -
966 Changes[i].OriginalWhitespaceRange.getBegin().getRawEncoding() -
967 Changes[i].Tok->NewlinesBefore;
968 unsigned RestoredLineLength = Changes[i].StartOfTokenColumn +
969 Changes[i].TokenLength + OriginalSpaces;
970 // If leaving comments makes the line exceed the column limit, give up to
971 // leave the comments.
972 if (RestoredLineLength >= Style.ColumnLimit && Style.ColumnLimit != 0)
973 break;
974 Changes[i].Spaces = OriginalSpaces;
975 continue;
976 }
977
978 unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
979 unsigned ChangeMaxColumn;
980
981 if (Style.ColumnLimit == 0)
982 ChangeMaxColumn = UINT_MAX;
983 else if (Style.ColumnLimit >= Changes[i].TokenLength)
984 ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
985 else
986 ChangeMaxColumn = ChangeMinColumn;
987
988 // If we don't create a replacement for this change, we have to consider
989 // it to be immovable.
990 if (!Changes[i].CreateReplacement)
991 ChangeMaxColumn = ChangeMinColumn;
992
993 if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
994 ChangeMaxColumn -= 2;
995 // If this comment follows an } in column 0, it probably documents the
996 // closing of a namespace and we don't want to align it.
997 bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
998 Changes[i - 1].Tok->is(tok::r_brace) &&
999 Changes[i - 1].StartOfTokenColumn == 0;
1000 bool WasAlignedWithStartOfNextLine = false;
1001 if (Changes[i].NewlinesBefore >= 1) { // A comment on its own line.
1002 unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
1003 Changes[i].OriginalWhitespaceRange.getEnd());
1004 for (unsigned j = i + 1; j != e; ++j) {
1005 if (Changes[j].Tok->is(tok::comment))
1006 continue;
1007
1008 unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
1009 Changes[j].OriginalWhitespaceRange.getEnd());
1010 // The start of the next token was previously aligned with the
1011 // start of this comment.
1012 WasAlignedWithStartOfNextLine =
1013 CommentColumn == NextColumn ||
1014 CommentColumn == NextColumn + Style.IndentWidth;
1015 break;
1016 }
1017 }
1018 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Never ||
1019 FollowsRBraceInColumn0) {
1020 alignTrailingComments(StartOfSequence, i, MinColumn);
1021 MinColumn = ChangeMinColumn;
1022 MaxColumn = ChangeMinColumn;
1023 StartOfSequence = i;
1024 } else if (BreakBeforeNext || Newlines > NewLineThreshold ||
1025 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
1026 // Break the comment sequence if the previous line did not end
1027 // in a trailing comment.
1028 (Changes[i].NewlinesBefore == 1 && i > 0 &&
1029 !Changes[i - 1].IsTrailingComment) ||
1030 WasAlignedWithStartOfNextLine) {
1031 alignTrailingComments(StartOfSequence, i, MinColumn);
1032 MinColumn = ChangeMinColumn;
1033 MaxColumn = ChangeMaxColumn;
1034 StartOfSequence = i;
1035 } else {
1036 MinColumn = std::max(MinColumn, ChangeMinColumn);
1037 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
1038 }
1039 BreakBeforeNext = (i == 0) || (Changes[i].NewlinesBefore > 1) ||
1040 // Never start a sequence with a comment at the beginning
1041 // of the line.
1042 (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
1043 Newlines = 0;
1044 }
1045 alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
1046}
1047
1048void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
1049 unsigned Column) {
1050 for (unsigned i = Start; i != End; ++i) {
1051 int Shift = 0;
1052 if (Changes[i].IsTrailingComment)
1053 Shift = Column - Changes[i].StartOfTokenColumn;
1054 if (Changes[i].StartOfBlockComment) {
1055 Shift = Changes[i].IndentationOffset +
1056 Changes[i].StartOfBlockComment->StartOfTokenColumn -
1057 Changes[i].StartOfTokenColumn;
1058 }
1059 if (Shift <= 0)
1060 continue;
1061 Changes[i].Spaces += Shift;
1062 if (i + 1 != Changes.size())
1063 Changes[i + 1].PreviousEndOfTokenColumn += Shift;
1064 Changes[i].StartOfTokenColumn += Shift;
1065 }
1066}
1067
1068void WhitespaceManager::alignEscapedNewlines() {
1069 if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
1070 return;
1071
1072 bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
1073 unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
1074 unsigned StartOfMacro = 0;
1075 for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
1076 Change &C = Changes[i];
1077 if (C.NewlinesBefore > 0) {
1078 if (C.ContinuesPPDirective) {
1079 MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
1080 } else {
1081 alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
1082 MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
1083 StartOfMacro = i;
1084 }
1085 }
1086 }
1087 alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
1088}
1089
1090void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
1091 unsigned Column) {
1092 for (unsigned i = Start; i < End; ++i) {
1093 Change &C = Changes[i];
1094 if (C.NewlinesBefore > 0) {
1095 assert(C.ContinuesPPDirective);
1096 if (C.PreviousEndOfTokenColumn + 1 > Column)
1097 C.EscapedNewlineColumn = 0;
1098 else
1099 C.EscapedNewlineColumn = Column;
1100 }
1101 }
1102}
1103
1104void WhitespaceManager::alignArrayInitializers() {
1105 if (Style.AlignArrayOfStructures == FormatStyle::AIAS_None)
1106 return;
1107
1108 for (unsigned ChangeIndex = 1U, ChangeEnd = Changes.size();
1109 ChangeIndex < ChangeEnd; ++ChangeIndex) {
1110 auto &C = Changes[ChangeIndex];
1111 if (C.Tok->IsArrayInitializer) {
1112 bool FoundComplete = false;
1113 for (unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
1114 ++InsideIndex) {
1115 if (Changes[InsideIndex].Tok == C.Tok->MatchingParen) {
1116 alignArrayInitializers(ChangeIndex, InsideIndex + 1);
1117 ChangeIndex = InsideIndex + 1;
1118 FoundComplete = true;
1119 break;
1120 }
1121 }
1122 if (!FoundComplete)
1123 ChangeIndex = ChangeEnd;
1124 }
1125 }
1126}
1127
1128void WhitespaceManager::alignArrayInitializers(unsigned Start, unsigned End) {
1129
1130 if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Right)
1131 alignArrayInitializersRightJustified(getCells(Start, End));
1132 else if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Left)
1133 alignArrayInitializersLeftJustified(getCells(Start, End));
1134}
1135
1136void WhitespaceManager::alignArrayInitializersRightJustified(
1137 CellDescriptions &&CellDescs) {
1138 if (!CellDescs.isRectangular())
1139 return;
1140
1141 auto &Cells = CellDescs.Cells;
1142 // Now go through and fixup the spaces.
1143 auto *CellIter = Cells.begin();
1144 for (auto i = 0U; i < CellDescs.CellCounts[0]; ++i, ++CellIter) {
1145 unsigned NetWidth = 0U;
1146 if (isSplitCell(*CellIter))
1147 NetWidth = getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1148 auto CellWidth = getMaximumCellWidth(CellIter, NetWidth);
1149
1150 if (Changes[CellIter->Index].Tok->is(tok::r_brace)) {
1151 // So in here we want to see if there is a brace that falls
1152 // on a line that was split. If so on that line we make sure that
1153 // the spaces in front of the brace are enough.
1154 const auto *Next = CellIter;
1155 do {
1156 const FormatToken *Previous = Changes[Next->Index].Tok->Previous;
1157 if (Previous && Previous->isNot(TT_LineComment)) {
1158 Changes[Next->Index].Spaces = 0;
1159 Changes[Next->Index].NewlinesBefore = 0;
1160 }
1161 Next = Next->NextColumnElement;
1162 } while (Next);
1163 // Unless the array is empty, we need the position of all the
1164 // immediately adjacent cells
1165 if (CellIter != Cells.begin()) {
1166 auto ThisNetWidth =
1167 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1168 auto MaxNetWidth = getMaximumNetWidth(
1169 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1170 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1171 if (ThisNetWidth < MaxNetWidth)
1172 Changes[CellIter->Index].Spaces = (MaxNetWidth - ThisNetWidth);
1173 auto RowCount = 1U;
1174 auto Offset = std::distance(Cells.begin(), CellIter);
1175 for (const auto *Next = CellIter->NextColumnElement; Next;
1176 Next = Next->NextColumnElement) {
1177 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1178 auto *End = Start + Offset;
1179 ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1180 if (ThisNetWidth < MaxNetWidth)
1181 Changes[Next->Index].Spaces = (MaxNetWidth - ThisNetWidth);
1182 ++RowCount;
1183 }
1184 }
1185 } else {
1186 auto ThisWidth =
1187 calculateCellWidth(CellIter->Index, CellIter->EndIndex, true) +
1188 NetWidth;
1189 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1190 Changes[CellIter->Index].Spaces = (CellWidth - (ThisWidth + NetWidth));
1191 Changes[CellIter->Index].Spaces += (i > 0) ? 1 : 0;
1192 }
1193 alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
1194 for (const auto *Next = CellIter->NextColumnElement; Next;
1195 Next = Next->NextColumnElement) {
1196 ThisWidth =
1197 calculateCellWidth(Next->Index, Next->EndIndex, true) + NetWidth;
1198 if (Changes[Next->Index].NewlinesBefore == 0) {
1199 Changes[Next->Index].Spaces = (CellWidth - ThisWidth);
1200 Changes[Next->Index].Spaces += (i > 0) ? 1 : 0;
1201 }
1202 alignToStartOfCell(Next->Index, Next->EndIndex);
1203 }
1204 }
1205 }
1206}
1207
1208void WhitespaceManager::alignArrayInitializersLeftJustified(
1209 CellDescriptions &&CellDescs) {
1210
1211 if (!CellDescs.isRectangular())
1212 return;
1213
1214 auto &Cells = CellDescs.Cells;
1215 // Now go through and fixup the spaces.
1216 auto *CellIter = Cells.begin();
1217 // The first cell needs to be against the left brace.
1218 if (Changes[CellIter->Index].NewlinesBefore == 0)
1219 Changes[CellIter->Index].Spaces = 0;
1220 else
1221 Changes[CellIter->Index].Spaces = CellDescs.InitialSpaces;
1222 ++CellIter;
1223 for (auto i = 1U; i < CellDescs.CellCounts[0]; i++, ++CellIter) {
1224 auto MaxNetWidth = getMaximumNetWidth(
1225 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1226 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1227 auto ThisNetWidth =
1228 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1229 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1230 Changes[CellIter->Index].Spaces =
1231 MaxNetWidth - ThisNetWidth +
1232 (Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1 : 0);
1233 }
1234 auto RowCount = 1U;
1235 auto Offset = std::distance(Cells.begin(), CellIter);
1236 for (const auto *Next = CellIter->NextColumnElement; Next;
1237 Next = Next->NextColumnElement) {
1238 if (RowCount > CellDescs.CellCounts.size())
1239 break;
1240 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1241 auto *End = Start + Offset;
1242 auto ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1243 if (Changes[Next->Index].NewlinesBefore == 0) {
1244 Changes[Next->Index].Spaces =
1245 MaxNetWidth - ThisNetWidth +
1246 (Changes[Next->Index].Tok->isNot(tok::r_brace) ? 1 : 0);
1247 }
1248 ++RowCount;
1249 }
1250 }
1251}
1252
1253bool WhitespaceManager::isSplitCell(const CellDescription &Cell) {
1254 if (Cell.HasSplit)
1255 return true;
1256 for (const auto *Next = Cell.NextColumnElement; Next;
1257 Next = Next->NextColumnElement) {
1258 if (Next->HasSplit)
1259 return true;
1260 }
1261 return false;
1262}
1263
1264WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
1265 unsigned End) {
1266
1267 unsigned Depth = 0;
1268 unsigned Cell = 0;
1269 SmallVector<unsigned> CellCounts;
1270 unsigned InitialSpaces = 0;
1271 unsigned InitialTokenLength = 0;
1272 unsigned EndSpaces = 0;
1273 SmallVector<CellDescription> Cells;
1274 const FormatToken *MatchingParen = nullptr;
1275 for (unsigned i = Start; i < End; ++i) {
1276 auto &C = Changes[i];
1277 if (C.Tok->is(tok::l_brace))
1278 ++Depth;
1279 else if (C.Tok->is(tok::r_brace))
1280 --Depth;
1281 if (Depth == 2) {
1282 if (C.Tok->is(tok::l_brace)) {
1283 Cell = 0;
1284 MatchingParen = C.Tok->MatchingParen;
1285 if (InitialSpaces == 0) {
1286 InitialSpaces = C.Spaces + C.TokenLength;
1287 InitialTokenLength = C.TokenLength;
1288 auto j = i - 1;
1289 for (; Changes[j].NewlinesBefore == 0 && j > Start; --j) {
1290 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1291 InitialTokenLength += Changes[j].TokenLength;
1292 }
1293 if (C.NewlinesBefore == 0) {
1294 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1295 InitialTokenLength += Changes[j].TokenLength;
1296 }
1297 }
1298 } else if (C.Tok->is(tok::comma)) {
1299 if (!Cells.empty())
1300 Cells.back().EndIndex = i;
1301 if (C.Tok->getNextNonComment()->isNot(tok::r_brace)) // dangling comma
1302 ++Cell;
1303 }
1304 } else if (Depth == 1) {
1305 if (C.Tok == MatchingParen) {
1306 if (!Cells.empty())
1307 Cells.back().EndIndex = i;
1308 Cells.push_back(CellDescription{i, ++Cell, i + 1, false, nullptr});
1309 CellCounts.push_back(C.Tok->Previous->isNot(tok::comma) ? Cell + 1
1310 : Cell);
1311 // Go to the next non-comment and ensure there is a break in front
1312 const auto *NextNonComment = C.Tok->getNextNonComment();
1313 while (NextNonComment->is(tok::comma))
1314 NextNonComment = NextNonComment->getNextNonComment();
1315 auto j = i;
1316 while (Changes[j].Tok != NextNonComment && j < End)
1317 ++j;
1318 if (j < End && Changes[j].NewlinesBefore == 0 &&
1319 Changes[j].Tok->isNot(tok::r_brace)) {
1320 Changes[j].NewlinesBefore = 1;
1321 // Account for the added token lengths
1322 Changes[j].Spaces = InitialSpaces - InitialTokenLength;
1323 }
1324 } else if (C.Tok->is(tok::comment)) {
1325 // Trailing comments stay at a space past the last token
1326 C.Spaces = Changes[i - 1].Tok->is(tok::comma) ? 1 : 2;
1327 } else if (C.Tok->is(tok::l_brace)) {
1328 // We need to make sure that the ending braces is aligned to the
1329 // start of our initializer
1330 auto j = i - 1;
1331 for (; j > 0 && !Changes[j].Tok->ArrayInitializerLineStart; --j)
1332 ; // Nothing the loop does the work
1333 EndSpaces = Changes[j].Spaces;
1334 }
1335 } else if (Depth == 0 && C.Tok->is(tok::r_brace)) {
1336 C.NewlinesBefore = 1;
1337 C.Spaces = EndSpaces;
1338 }
1339 if (C.Tok->StartsColumn) {
1340 // This gets us past tokens that have been split over multiple
1341 // lines
1342 bool HasSplit = false;
1343 if (Changes[i].NewlinesBefore > 0) {
1344 // So if we split a line previously and the tail line + this token is
1345 // less then the column limit we remove the split here and just put
1346 // the column start at a space past the comma
1347 //
1348 // FIXME This if branch covers the cases where the column is not
1349 // the first column. This leads to weird pathologies like the formatting
1350 // auto foo = Items{
1351 // Section{
1352 // 0, bar(),
1353 // }
1354 // };
1355 // Well if it doesn't lead to that it's indicative that the line
1356 // breaking should be revisited. Unfortunately alot of other options
1357 // interact with this
1358 auto j = i - 1;
1359 if ((j - 1) > Start && Changes[j].Tok->is(tok::comma) &&
1360 Changes[j - 1].NewlinesBefore > 0) {
1361 --j;
1362 auto LineLimit = Changes[j].Spaces + Changes[j].TokenLength;
1363 if (LineLimit < Style.ColumnLimit) {
1364 Changes[i].NewlinesBefore = 0;
1365 Changes[i].Spaces = 1;
1366 }
1367 }
1368 }
1369 while (Changes[i].NewlinesBefore > 0 && Changes[i].Tok == C.Tok) {
1370 Changes[i].Spaces = InitialSpaces;
1371 ++i;
1372 HasSplit = true;
1373 }
1374 if (Changes[i].Tok != C.Tok)
1375 --i;
1376 Cells.push_back(CellDescription{i, Cell, i, HasSplit, nullptr});
1377 }
1378 }
1379
1380 return linkCells({Cells, CellCounts, InitialSpaces});
1381}
1382
1383unsigned WhitespaceManager::calculateCellWidth(unsigned Start, unsigned End,
1384 bool WithSpaces) const {
1385 unsigned CellWidth = 0;
1386 for (auto i = Start; i < End; i++) {
1387 if (Changes[i].NewlinesBefore > 0)
1388 CellWidth = 0;
1389 CellWidth += Changes[i].TokenLength;
1390 CellWidth += (WithSpaces ? Changes[i].Spaces : 0);
1391 }
1392 return CellWidth;
1393}
1394
1395void WhitespaceManager::alignToStartOfCell(unsigned Start, unsigned End) {
1396 if ((End - Start) <= 1)
1397 return;
1398 // If the line is broken anywhere in there make sure everything
1399 // is aligned to the parent
1400 for (auto i = Start + 1; i < End; i++)
1401 if (Changes[i].NewlinesBefore > 0)
1402 Changes[i].Spaces = Changes[Start].Spaces;
1403}
1404
1405WhitespaceManager::CellDescriptions
1406WhitespaceManager::linkCells(CellDescriptions &&CellDesc) {
1407 auto &Cells = CellDesc.Cells;
1408 for (auto *CellIter = Cells.begin(); CellIter != Cells.end(); ++CellIter) {
1409 if (!CellIter->NextColumnElement && (CellIter + 1) != Cells.end()) {
1410 for (auto *NextIter = CellIter + 1; NextIter != Cells.end(); ++NextIter) {
1411 if (NextIter->Cell == CellIter->Cell) {
1412 CellIter->NextColumnElement = &(*NextIter);
1413 break;
1414 }
1415 }
1416 }
1417 }
1418 return std::move(CellDesc);
1419}
1420
1421void WhitespaceManager::generateChanges() {
1422 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
1423 const Change &C = Changes[i];
1424 if (i > 0 && Changes[i - 1].OriginalWhitespaceRange.getBegin() ==
1425 C.OriginalWhitespaceRange.getBegin()) {
1426 // Do not generate two replacements for the same location.
1427 continue;
1428 }
1429 if (C.CreateReplacement) {
1430 std::string ReplacementText = C.PreviousLinePostfix;
1431 if (C.ContinuesPPDirective) {
1432 appendEscapedNewlineText(ReplacementText, C.NewlinesBefore,
1433 C.PreviousEndOfTokenColumn,
1434 C.EscapedNewlineColumn);
1435 } else {
1436 appendNewlineText(ReplacementText, C.NewlinesBefore);
1437 }
1438 // FIXME: This assert should hold if we computed the column correctly.
1439 // assert((int)C.StartOfTokenColumn >= C.Spaces);
1440 appendIndentText(
1441 ReplacementText, C.Tok->IndentLevel, std::max(0, C.Spaces),
1442 std::max((int)C.StartOfTokenColumn, C.Spaces) - std::max(0, C.Spaces),
1443 C.IsAligned);
1444 ReplacementText.append(C.CurrentLinePrefix);
1445 storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
1446 }
1447 }
1448}
1449
1450void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
1451 unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
1452 SourceMgr.getFileOffset(Range.getBegin());
1453 // Don't create a replacement, if it does not change anything.
1454 if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
1455 WhitespaceLength) == Text) {
1456 return;
1457 }
1458 auto Err = Replaces.add(tooling::Replacement(
1459 SourceMgr, CharSourceRange::getCharRange(Range), Text));
1460 // FIXME: better error handling. For now, just print an error message in the
1461 // release version.
1462 if (Err) {
1463 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
1464 assert(false);
1465 }
1466}
1467
1468void WhitespaceManager::appendNewlineText(std::string &Text,
1469 unsigned Newlines) {
1470 if (UseCRLF) {
1471 Text.reserve(Text.size() + 2 * Newlines);
1472 for (unsigned i = 0; i < Newlines; ++i)
1473 Text.append("\r\n");
1474 } else {
1475 Text.append(Newlines, '\n');
1476 }
1477}
1478
1479void WhitespaceManager::appendEscapedNewlineText(
1480 std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
1481 unsigned EscapedNewlineColumn) {
1482 if (Newlines > 0) {
1483 unsigned Spaces =
1484 std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
1485 for (unsigned i = 0; i < Newlines; ++i) {
1486 Text.append(Spaces, ' ');
1487 Text.append(UseCRLF ? "\\\r\n" : "\\\n");
1488 Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
1489 }
1490 }
1491}
1492
1493void WhitespaceManager::appendIndentText(std::string &Text,
1494 unsigned IndentLevel, unsigned Spaces,
1495 unsigned WhitespaceStartColumn,
1496 bool IsAligned) {
1497 switch (Style.UseTab) {
1499 Text.append(Spaces, ' ');
1500 break;
1502 if (Style.TabWidth) {
1503 unsigned FirstTabWidth =
1504 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
1505
1506 // Insert only spaces when we want to end up before the next tab.
1507 if (Spaces < FirstTabWidth || Spaces == 1) {
1508 Text.append(Spaces, ' ');
1509 break;
1510 }
1511 // Align to the next tab.
1512 Spaces -= FirstTabWidth;
1513 Text.append("\t");
1514
1515 Text.append(Spaces / Style.TabWidth, '\t');
1516 Text.append(Spaces % Style.TabWidth, ' ');
1517 } else if (Spaces == 1) {
1518 Text.append(Spaces, ' ');
1519 }
1520 break;
1521 }
1523 if (WhitespaceStartColumn == 0) {
1524 unsigned Indentation = IndentLevel * Style.IndentWidth;
1525 Spaces = appendTabIndent(Text, Spaces, Indentation);
1526 }
1527 Text.append(Spaces, ' ');
1528 break;
1530 if (WhitespaceStartColumn == 0)
1531 Spaces = appendTabIndent(Text, Spaces, Spaces);
1532 Text.append(Spaces, ' ');
1533 break;
1535 if (WhitespaceStartColumn == 0) {
1536 unsigned Indentation =
1537 IsAligned ? IndentLevel * Style.IndentWidth : Spaces;
1538 Spaces = appendTabIndent(Text, Spaces, Indentation);
1539 }
1540 Text.append(Spaces, ' ');
1541 break;
1542 }
1543}
1544
1545unsigned WhitespaceManager::appendTabIndent(std::string &Text, unsigned Spaces,
1546 unsigned Indentation) {
1547 // This happens, e.g. when a line in a block comment is indented less than the
1548 // first one.
1549 if (Indentation > Spaces)
1550 Indentation = Spaces;
1551 if (Style.TabWidth) {
1552 unsigned Tabs = Indentation / Style.TabWidth;
1553 Text.append(Tabs, '\t');
1554 Spaces -= Tabs * Style.TabWidth;
1555 }
1556 return Spaces;
1557}
1558
1559} // namespace format
1560} // namespace clang
StringRef Text
Definition: Format.cpp:2777
unsigned Offset
Definition: Format.cpp:2778
StateNode * Previous
WhitespaceManager class manages whitespace around tokens and their replacements.
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.
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
const char * getCharacterData(SourceLocation SL, bool *Invalid=nullptr) const
Return a pointer to the start of the specified location in the appropriate spelling MemoryBuffer.
unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
A trivial tuple used to represent a source range.
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 replaceWhitespace(FormatToken &Tok, unsigned Newlines, unsigned Spaces, unsigned StartOfTokenColumn, bool isAligned=false, bool InPPDirective=false)
Replaces the whitespace in front of Tok.
void addUntouchableToken(const FormatToken &Tok, bool InPPDirective)
Adds information about an unchangeable token's whitespace.
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.
Definition: Replacement.h:212
llvm::Error add(const Replacement &R)
Adds a new replacement R to the current set of replacements.
#define UINT_MAX
Definition: limits.h:60
@ MR_ExpandedArg
The token was expanded from a macro argument when formatting the expanded token sequence.
Definition: FormatToken.h:186
static unsigned AlignTokens(const FormatStyle &Style, F &&Matches, SmallVector< WhitespaceManager::Change, 16 > &Changes, unsigned StartAt, const FormatStyle::AlignConsecutiveStyle &ACS={}, bool RightJustify=false)
static void AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End, unsigned Column, bool RightJustify, F &&Matches, SmallVector< WhitespaceManager::Change, 16 > &Changes)
static void AlignMacroSequence(unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn, unsigned &MaxColumn, bool &FoundMatchOnLine, std::function< bool(const WhitespaceManager::Change &C)> AlignMacrosMatches, SmallVector< WhitespaceManager::Change, 16 > &Changes)
for(unsigned I=0, E=TL.getNumArgs();I !=E;++I)
@ C
Languages that the frontend can parse and compile.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
#define false
Definition: stdbool.h:22
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:55
@ UT_AlignWithSpaces
Use tabs for line continuation and indentation, and spaces for alignment.
Definition: Format.h:4178
@ UT_ForContinuationAndIndentation
Fill all leading whitespace with tabs, and use spaces for alignment that appears within a line (e....
Definition: Format.h:4175
@ UT_ForIndentation
Use tabs only for indentation.
Definition: Format.h:4172
@ UT_Always
Use tabs whenever we need to fill whitespace that spans at least from one tab stop to the next one.
Definition: Format.h:4181
@ UT_Never
Never use tab.
Definition: Format.h:4170
@ TCAS_Never
Don't align trailing comments but other formatter applies.
Definition: Format.h:401
@ TCAS_Leave
Leave trailing comments as they are.
Definition: Format.h:383
@ TCAS_Always
Align trailing comments.
Definition: Format.h:392
@ AIAS_Left
Align array column and left justify the columns e.g.:
Definition: Format.h:119
@ AIAS_Right
Align array column and right justify the columns e.g.:
Definition: Format.h:129
@ AIAS_None
Don't align array initializer columns.
Definition: Format.h:131
@ ENAS_DontAlign
Don't align escaped newlines.
Definition: Format.h:308
@ ENAS_Left
Align escaped newlines as far left as possible.
Definition: Format.h:319
@ PAS_Right
Align pointer to the right.
Definition: Format.h:3045
A wrapper around a Token storing information about the whitespace characters preceding it.
Definition: FormatToken.h:249
unsigned OriginalColumn
The original 0-based column of this token, including expanded tabs.
Definition: FormatToken.h:444
SourceLocation getStartOfNonWhitespace() const
Returns actual token start location without leading escaped newlines and whitespace.
Definition: FormatToken.h:728
void setDecision(FormatDecision D)
Definition: FormatToken.h:352
unsigned Finalized
If true, this token has been fully formatted (indented and potentially re-formatted inside),...
Definition: FormatToken.h:323
std::optional< MacroExpansion > MacroCtx
Definition: FormatToken.h:532
unsigned NewlinesBefore
The number of newlines immediately before the Token.
Definition: FormatToken.h:409
unsigned IsFirst
Indicates that this is the first token of the file.
Definition: FormatToken.h:287
SourceRange WhitespaceRange
The range of the whitespace immediately preceding the Token.
Definition: FormatToken.h:277
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 NewlinesBefore, StringRef PreviousLinePostfix, StringRef CurrentLinePrefix, bool IsAligned, bool ContinuesPPDirective, bool IsInsideToken)
Creates a Change.