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