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 
17 namespace clang {
18 namespace format {
19 
20 bool WhitespaceManager::Change::IsBeforeInFile::operator()(
21     const Change &C1, const Change &C2) const {
22   return SourceMgr.isBeforeInTranslationUnit(
23       C1.OriginalWhitespaceRange.getBegin(),
24       C2.OriginalWhitespaceRange.getBegin());
25 }
26 
27 WhitespaceManager::Change::Change(const FormatToken &Tok,
28                                   bool CreateReplacement,
29                                   SourceRange OriginalWhitespaceRange,
30                                   int Spaces, unsigned StartOfTokenColumn,
31                                   unsigned NewlinesBefore,
32                                   StringRef PreviousLinePostfix,
33                                   StringRef CurrentLinePrefix, bool IsAligned,
34                                   bool ContinuesPPDirective, bool IsInsideToken)
35     : Tok(&Tok), CreateReplacement(CreateReplacement),
36       OriginalWhitespaceRange(OriginalWhitespaceRange),
37       StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
38       PreviousLinePostfix(PreviousLinePostfix),
39       CurrentLinePrefix(CurrentLinePrefix), IsAligned(IsAligned),
40       ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces),
41       IsInsideToken(IsInsideToken), IsTrailingComment(false), TokenLength(0),
42       PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0),
43       StartOfBlockComment(nullptr), IndentationOffset(0), ConditionalsLevel(0) {
44 }
45 
46 void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
47                                           unsigned Spaces,
48                                           unsigned StartOfTokenColumn,
49                                           bool IsAligned, bool InPPDirective) {
50   if (Tok.Finalized)
51     return;
52   Tok.setDecision((Newlines > 0) ? FD_Break : FD_Continue);
53   Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
54                            Spaces, StartOfTokenColumn, Newlines, "", "",
55                            IsAligned, InPPDirective && !Tok.IsFirst,
56                            /*IsInsideToken=*/false));
57 }
58 
59 void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
60                                             bool InPPDirective) {
61   if (Tok.Finalized)
62     return;
63   Changes.push_back(Change(Tok, /*CreateReplacement=*/false,
64                            Tok.WhitespaceRange, /*Spaces=*/0,
65                            Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
66                            /*IsAligned=*/false, InPPDirective && !Tok.IsFirst,
67                            /*IsInsideToken=*/false));
68 }
69 
70 llvm::Error
71 WhitespaceManager::addReplacement(const tooling::Replacement &Replacement) {
72   return Replaces.add(Replacement);
73 }
74 
75 void WhitespaceManager::replaceWhitespaceInToken(
76     const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
77     StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
78     unsigned Newlines, int Spaces) {
79   if (Tok.Finalized)
80     return;
81   SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
82   Changes.push_back(
83       Change(Tok, /*CreateReplacement=*/true,
84              SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
85              std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
86              /*IsAligned=*/true, InPPDirective && !Tok.IsFirst,
87              /*IsInsideToken=*/true));
88 }
89 
90 const tooling::Replacements &WhitespaceManager::generateReplacements() {
91   if (Changes.empty())
92     return Replaces;
93 
94   llvm::sort(Changes, Change::IsBeforeInFile(SourceMgr));
95   calculateLineBreakInformation();
96   alignConsecutiveMacros();
97   alignConsecutiveDeclarations();
98   alignConsecutiveBitFields();
99   alignConsecutiveAssignments();
100   alignChainedConditionals();
101   alignTrailingComments();
102   alignEscapedNewlines();
103   generateChanges();
104 
105   return Replaces;
106 }
107 
108 void WhitespaceManager::calculateLineBreakInformation() {
109   Changes[0].PreviousEndOfTokenColumn = 0;
110   Change *LastOutsideTokenChange = &Changes[0];
111   for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
112     SourceLocation OriginalWhitespaceStart =
113         Changes[i].OriginalWhitespaceRange.getBegin();
114     SourceLocation PreviousOriginalWhitespaceEnd =
115         Changes[i - 1].OriginalWhitespaceRange.getEnd();
116     unsigned OriginalWhitespaceStartOffset =
117         SourceMgr.getFileOffset(OriginalWhitespaceStart);
118     unsigned PreviousOriginalWhitespaceEndOffset =
119         SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
120     assert(PreviousOriginalWhitespaceEndOffset <=
121            OriginalWhitespaceStartOffset);
122     const char *const PreviousOriginalWhitespaceEndData =
123         SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
124     StringRef Text(PreviousOriginalWhitespaceEndData,
125                    SourceMgr.getCharacterData(OriginalWhitespaceStart) -
126                        PreviousOriginalWhitespaceEndData);
127     // Usually consecutive changes would occur in consecutive tokens. This is
128     // not the case however when analyzing some preprocessor runs of the
129     // annotated lines. For example, in this code:
130     //
131     // #if A // line 1
132     // int i = 1;
133     // #else B // line 2
134     // int i = 2;
135     // #endif // line 3
136     //
137     // one of the runs will produce the sequence of lines marked with line 1, 2
138     // and 3. So the two consecutive whitespace changes just before '// line 2'
139     // and before '#endif // line 3' span multiple lines and tokens:
140     //
141     // #else B{change X}[// line 2
142     // int i = 2;
143     // ]{change Y}#endif // line 3
144     //
145     // For this reason, if the text between consecutive changes spans multiple
146     // newlines, the token length must be adjusted to the end of the original
147     // line of the token.
148     auto NewlinePos = Text.find_first_of('\n');
149     if (NewlinePos == StringRef::npos) {
150       Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
151                                    PreviousOriginalWhitespaceEndOffset +
152                                    Changes[i].PreviousLinePostfix.size() +
153                                    Changes[i - 1].CurrentLinePrefix.size();
154     } else {
155       Changes[i - 1].TokenLength =
156           NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
157     }
158 
159     // If there are multiple changes in this token, sum up all the changes until
160     // the end of the line.
161     if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0)
162       LastOutsideTokenChange->TokenLength +=
163           Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
164     else
165       LastOutsideTokenChange = &Changes[i - 1];
166 
167     Changes[i].PreviousEndOfTokenColumn =
168         Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
169 
170     Changes[i - 1].IsTrailingComment =
171         (Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
172          (Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
173         Changes[i - 1].Tok->is(tok::comment) &&
174         // FIXME: This is a dirty hack. The problem is that
175         // BreakableLineCommentSection does comment reflow changes and here is
176         // the aligning of trailing comments. Consider the case where we reflow
177         // the second line up in this example:
178         //
179         // // line 1
180         // // line 2
181         //
182         // That amounts to 2 changes by BreakableLineCommentSection:
183         //  - the first, delimited by (), for the whitespace between the tokens,
184         //  - and second, delimited by [], for the whitespace at the beginning
185         //  of the second token:
186         //
187         // // line 1(
188         // )[// ]line 2
189         //
190         // So in the end we have two changes like this:
191         //
192         // // line1()[ ]line 2
193         //
194         // Note that the OriginalWhitespaceStart of the second change is the
195         // same as the PreviousOriginalWhitespaceEnd of the first change.
196         // In this case, the below check ensures that the second change doesn't
197         // get treated as a trailing comment change here, since this might
198         // trigger additional whitespace to be wrongly inserted before "line 2"
199         // by the comment aligner here.
200         //
201         // For a proper solution we need a mechanism to say to WhitespaceManager
202         // that a particular change breaks the current sequence of trailing
203         // comments.
204         OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
205   }
206   // FIXME: The last token is currently not always an eof token; in those
207   // cases, setting TokenLength of the last token to 0 is wrong.
208   Changes.back().TokenLength = 0;
209   Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
210 
211   const WhitespaceManager::Change *LastBlockComment = nullptr;
212   for (auto &Change : Changes) {
213     // Reset the IsTrailingComment flag for changes inside of trailing comments
214     // so they don't get realigned later. Comment line breaks however still need
215     // to be aligned.
216     if (Change.IsInsideToken && Change.NewlinesBefore == 0)
217       Change.IsTrailingComment = false;
218     Change.StartOfBlockComment = nullptr;
219     Change.IndentationOffset = 0;
220     if (Change.Tok->is(tok::comment)) {
221       if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken)
222         LastBlockComment = &Change;
223       else {
224         if ((Change.StartOfBlockComment = LastBlockComment))
225           Change.IndentationOffset =
226               Change.StartOfTokenColumn -
227               Change.StartOfBlockComment->StartOfTokenColumn;
228       }
229     } else {
230       LastBlockComment = nullptr;
231     }
232   }
233 
234   // Compute conditional nesting level
235   // Level is increased for each conditional, unless this conditional continues
236   // a chain of conditional, i.e. starts immediately after the colon of another
237   // conditional.
238   SmallVector<bool, 16> ScopeStack;
239   int ConditionalsLevel = 0;
240   for (auto &Change : Changes) {
241     for (unsigned i = 0, e = Change.Tok->FakeLParens.size(); i != e; ++i) {
242       bool isNestedConditional =
243           Change.Tok->FakeLParens[e - 1 - i] == prec::Conditional &&
244           !(i == 0 && Change.Tok->Previous &&
245             Change.Tok->Previous->is(TT_ConditionalExpr) &&
246             Change.Tok->Previous->is(tok::colon));
247       if (isNestedConditional)
248         ++ConditionalsLevel;
249       ScopeStack.push_back(isNestedConditional);
250     }
251 
252     Change.ConditionalsLevel = ConditionalsLevel;
253 
254     for (unsigned i = Change.Tok->FakeRParens; i > 0 && ScopeStack.size();
255          --i) {
256       if (ScopeStack.pop_back_val())
257         --ConditionalsLevel;
258     }
259   }
260 }
261 
262 // Align a single sequence of tokens, see AlignTokens below.
263 template <typename F>
264 static void
265 AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches,
266                    SmallVector<WhitespaceManager::Change, 16> &Changes) {
267   bool FoundMatchOnLine = false;
268   int Shift = 0;
269 
270   // ScopeStack keeps track of the current scope depth. It contains indices of
271   // the first token on each scope.
272   // We only run the "Matches" function on tokens from the outer-most scope.
273   // However, we do need to pay special attention to one class of tokens
274   // that are not in the outer-most scope, and that is function parameters
275   // which are split across multiple lines, as illustrated by this example:
276   //   double a(int x);
277   //   int    b(int  y,
278   //          double z);
279   // In the above example, we need to take special care to ensure that
280   // 'double z' is indented along with it's owning function 'b'.
281   // Special handling is required for 'nested' ternary operators.
282   SmallVector<unsigned, 16> ScopeStack;
283 
284   for (unsigned i = Start; i != End; ++i) {
285     if (ScopeStack.size() != 0 &&
286         Changes[i].indentAndNestingLevel() <
287             Changes[ScopeStack.back()].indentAndNestingLevel())
288       ScopeStack.pop_back();
289 
290     // Compare current token to previous non-comment token to ensure whether
291     // it is in a deeper scope or not.
292     unsigned PreviousNonComment = i - 1;
293     while (PreviousNonComment > Start &&
294            Changes[PreviousNonComment].Tok->is(tok::comment))
295       PreviousNonComment--;
296     if (i != Start && Changes[i].indentAndNestingLevel() >
297                           Changes[PreviousNonComment].indentAndNestingLevel())
298       ScopeStack.push_back(i);
299 
300     bool InsideNestedScope = ScopeStack.size() != 0;
301 
302     if (Changes[i].NewlinesBefore > 0 && !InsideNestedScope) {
303       Shift = 0;
304       FoundMatchOnLine = false;
305     }
306 
307     // If this is the first matching token to be aligned, remember by how many
308     // spaces it has to be shifted, so the rest of the changes on the line are
309     // shifted by the same amount
310     if (!FoundMatchOnLine && !InsideNestedScope && Matches(Changes[i])) {
311       FoundMatchOnLine = true;
312       Shift = Column - Changes[i].StartOfTokenColumn;
313       Changes[i].Spaces += Shift;
314     }
315 
316     // This is for function parameters that are split across multiple lines,
317     // as mentioned in the ScopeStack comment.
318     if (InsideNestedScope && Changes[i].NewlinesBefore > 0) {
319       unsigned ScopeStart = ScopeStack.back();
320       if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) ||
321           (ScopeStart > Start + 1 &&
322            Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) ||
323           Changes[i].Tok->is(TT_ConditionalExpr) ||
324           (Changes[i].Tok->Previous &&
325            Changes[i].Tok->Previous->is(TT_ConditionalExpr)))
326         Changes[i].Spaces += Shift;
327     }
328 
329     assert(Shift >= 0);
330     Changes[i].StartOfTokenColumn += Shift;
331     if (i + 1 != Changes.size())
332       Changes[i + 1].PreviousEndOfTokenColumn += Shift;
333   }
334 }
335 
336 // Walk through a subset of the changes, starting at StartAt, and find
337 // sequences of matching tokens to align. To do so, keep track of the lines and
338 // whether or not a matching token was found on a line. If a matching token is
339 // found, extend the current sequence. If the current line cannot be part of a
340 // sequence, e.g. because there is an empty line before it or it contains only
341 // non-matching tokens, finalize the previous sequence.
342 // The value returned is the token on which we stopped, either because we
343 // exhausted all items inside Changes, or because we hit a scope level higher
344 // than our initial scope.
345 // This function is recursive. Each invocation processes only the scope level
346 // equal to the initial level, which is the level of Changes[StartAt].
347 // If we encounter a scope level greater than the initial level, then we call
348 // ourselves recursively, thereby avoiding the pollution of the current state
349 // with the alignment requirements of the nested sub-level. This recursive
350 // behavior is necessary for aligning function prototypes that have one or more
351 // arguments.
352 // If this function encounters a scope level less than the initial level,
353 // it returns the current position.
354 // There is a non-obvious subtlety in the recursive behavior: Even though we
355 // defer processing of nested levels to recursive invocations of this
356 // function, when it comes time to align a sequence of tokens, we run the
357 // alignment on the entire sequence, including the nested levels.
358 // When doing so, most of the nested tokens are skipped, because their
359 // alignment was already handled by the recursive invocations of this function.
360 // However, the special exception is that we do NOT skip function parameters
361 // that are split across multiple lines. See the test case in FormatTest.cpp
362 // that mentions "split function parameter alignment" for an example of this.
363 template <typename F>
364 static unsigned AlignTokens(
365     const FormatStyle &Style, F &&Matches,
366     SmallVector<WhitespaceManager::Change, 16> &Changes, unsigned StartAt,
367     const FormatStyle::AlignConsecutiveStyle &ACS = FormatStyle::ACS_None) {
368   unsigned MinColumn = 0;
369   unsigned MaxColumn = UINT_MAX;
370 
371   // Line number of the start and the end of the current token sequence.
372   unsigned StartOfSequence = 0;
373   unsigned EndOfSequence = 0;
374 
375   // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
376   // abort when we hit any token in a higher scope than the starting one.
377   auto IndentAndNestingLevel = StartAt < Changes.size()
378                                    ? Changes[StartAt].indentAndNestingLevel()
379                                    : std::tuple<unsigned, unsigned, unsigned>();
380 
381   // Keep track of the number of commas before the matching tokens, we will only
382   // align a sequence of matching tokens if they are preceded by the same number
383   // of commas.
384   unsigned CommasBeforeLastMatch = 0;
385   unsigned CommasBeforeMatch = 0;
386 
387   // Whether a matching token has been found on the current line.
388   bool FoundMatchOnLine = false;
389 
390   // Whether the current line consists purely of comments.
391   bool LineIsComment = true;
392 
393   // Aligns a sequence of matching tokens, on the MinColumn column.
394   //
395   // Sequences start from the first matching token to align, and end at the
396   // first token of the first line that doesn't need to be aligned.
397   //
398   // We need to adjust the StartOfTokenColumn of each Change that is on a line
399   // containing any matching token to be aligned and located after such token.
400   auto AlignCurrentSequence = [&] {
401     if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
402       AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
403                          Changes);
404     MinColumn = 0;
405     MaxColumn = UINT_MAX;
406     StartOfSequence = 0;
407     EndOfSequence = 0;
408   };
409 
410   unsigned i = StartAt;
411   for (unsigned e = Changes.size(); i != e; ++i) {
412     if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
413       break;
414 
415     if (Changes[i].NewlinesBefore != 0) {
416       CommasBeforeMatch = 0;
417       EndOfSequence = i;
418 
419       // Whether to break the alignment sequence because of an empty line.
420       bool EmptyLineBreak =
421           (Changes[i].NewlinesBefore > 1) &&
422           (ACS != FormatStyle::ACS_AcrossEmptyLines) &&
423           (ACS != FormatStyle::ACS_AcrossEmptyLinesAndComments);
424 
425       // Whether to break the alignment sequence because of a line without a
426       // match.
427       bool NoMatchBreak =
428           !FoundMatchOnLine &&
429           !(LineIsComment &&
430             ((ACS == FormatStyle::ACS_AcrossComments) ||
431              (ACS == FormatStyle::ACS_AcrossEmptyLinesAndComments)));
432 
433       if (EmptyLineBreak || NoMatchBreak)
434         AlignCurrentSequence();
435 
436       // A new line starts, re-initialize line status tracking bools.
437       FoundMatchOnLine = false;
438       LineIsComment = true;
439     }
440 
441     if (!Changes[i].Tok->is(tok::comment)) {
442       LineIsComment = false;
443     }
444 
445     if (Changes[i].Tok->is(tok::comma)) {
446       ++CommasBeforeMatch;
447     } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
448       // Call AlignTokens recursively, skipping over this scope block.
449       unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
450       i = StoppedAt - 1;
451       continue;
452     }
453 
454     if (!Matches(Changes[i]))
455       continue;
456 
457     // If there is more than one matching token per line, or if the number of
458     // preceding commas, do not match anymore, end the sequence.
459     if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
460       AlignCurrentSequence();
461 
462     CommasBeforeLastMatch = CommasBeforeMatch;
463     FoundMatchOnLine = true;
464 
465     if (StartOfSequence == 0)
466       StartOfSequence = i;
467 
468     unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
469     int LineLengthAfter = Changes[i].TokenLength;
470     for (unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) {
471       LineLengthAfter += Changes[j].Spaces;
472       // Changes are generally 1:1 with the tokens, but a change could also be
473       // inside of a token, in which case it's counted more than once: once for
474       // the whitespace surrounding the token (!IsInsideToken) and once for
475       // each whitespace change within it (IsInsideToken).
476       // Therefore, changes inside of a token should only count the space.
477       if (!Changes[j].IsInsideToken)
478         LineLengthAfter += Changes[j].TokenLength;
479     }
480     unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
481 
482     // If we are restricted by the maximum column width, end the sequence.
483     if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
484         CommasBeforeLastMatch != CommasBeforeMatch) {
485       AlignCurrentSequence();
486       StartOfSequence = i;
487     }
488 
489     MinColumn = std::max(MinColumn, ChangeMinColumn);
490     MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
491   }
492 
493   EndOfSequence = i;
494   AlignCurrentSequence();
495   return i;
496 }
497 
498 // Aligns a sequence of matching tokens, on the MinColumn column.
499 //
500 // Sequences start from the first matching token to align, and end at the
501 // first token of the first line that doesn't need to be aligned.
502 //
503 // We need to adjust the StartOfTokenColumn of each Change that is on a line
504 // containing any matching token to be aligned and located after such token.
505 static void AlignMacroSequence(
506     unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn,
507     unsigned &MaxColumn, bool &FoundMatchOnLine,
508     std::function<bool(const WhitespaceManager::Change &C)> AlignMacrosMatches,
509     SmallVector<WhitespaceManager::Change, 16> &Changes) {
510   if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
511 
512     FoundMatchOnLine = false;
513     int Shift = 0;
514 
515     for (unsigned I = StartOfSequence; I != EndOfSequence; ++I) {
516       if (Changes[I].NewlinesBefore > 0) {
517         Shift = 0;
518         FoundMatchOnLine = false;
519       }
520 
521       // If this is the first matching token to be aligned, remember by how many
522       // spaces it has to be shifted, so the rest of the changes on the line are
523       // shifted by the same amount
524       if (!FoundMatchOnLine && AlignMacrosMatches(Changes[I])) {
525         FoundMatchOnLine = true;
526         Shift = MinColumn - Changes[I].StartOfTokenColumn;
527         Changes[I].Spaces += Shift;
528       }
529 
530       assert(Shift >= 0);
531       Changes[I].StartOfTokenColumn += Shift;
532       if (I + 1 != Changes.size())
533         Changes[I + 1].PreviousEndOfTokenColumn += Shift;
534     }
535   }
536 
537   MinColumn = 0;
538   MaxColumn = UINT_MAX;
539   StartOfSequence = 0;
540   EndOfSequence = 0;
541 }
542 
543 void WhitespaceManager::alignConsecutiveMacros() {
544   if (Style.AlignConsecutiveMacros == FormatStyle::ACS_None)
545     return;
546 
547   auto AlignMacrosMatches = [](const Change &C) {
548     const FormatToken *Current = C.Tok;
549     unsigned SpacesRequiredBefore = 1;
550 
551     if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
552       return false;
553 
554     Current = Current->Previous;
555 
556     // If token is a ")", skip over the parameter list, to the
557     // token that precedes the "("
558     if (Current->is(tok::r_paren) && Current->MatchingParen) {
559       Current = Current->MatchingParen->Previous;
560       SpacesRequiredBefore = 0;
561     }
562 
563     if (!Current || !Current->is(tok::identifier))
564       return false;
565 
566     if (!Current->Previous || !Current->Previous->is(tok::pp_define))
567       return false;
568 
569     // For a macro function, 0 spaces are required between the
570     // identifier and the lparen that opens the parameter list.
571     // For a simple macro, 1 space is required between the
572     // identifier and the first token of the defined value.
573     return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
574   };
575 
576   unsigned MinColumn = 0;
577   unsigned MaxColumn = UINT_MAX;
578 
579   // Start and end of the token sequence we're processing.
580   unsigned StartOfSequence = 0;
581   unsigned EndOfSequence = 0;
582 
583   // Whether a matching token has been found on the current line.
584   bool FoundMatchOnLine = false;
585 
586   // Whether the current line consists only of comments
587   bool LineIsComment = true;
588 
589   unsigned I = 0;
590   for (unsigned E = Changes.size(); I != E; ++I) {
591     if (Changes[I].NewlinesBefore != 0) {
592       EndOfSequence = I;
593 
594       // Whether to break the alignment sequence because of an empty line.
595       bool EmptyLineBreak =
596           (Changes[I].NewlinesBefore > 1) &&
597           (Style.AlignConsecutiveMacros != FormatStyle::ACS_AcrossEmptyLines) &&
598           (Style.AlignConsecutiveMacros !=
599            FormatStyle::ACS_AcrossEmptyLinesAndComments);
600 
601       // Whether to break the alignment sequence because of a line without a
602       // match.
603       bool NoMatchBreak =
604           !FoundMatchOnLine &&
605           !(LineIsComment && ((Style.AlignConsecutiveMacros ==
606                                FormatStyle::ACS_AcrossComments) ||
607                               (Style.AlignConsecutiveMacros ==
608                                FormatStyle::ACS_AcrossEmptyLinesAndComments)));
609 
610       if (EmptyLineBreak || NoMatchBreak)
611         AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
612                            FoundMatchOnLine, AlignMacrosMatches, Changes);
613 
614       // A new line starts, re-initialize line status tracking bools.
615       FoundMatchOnLine = false;
616       LineIsComment = true;
617     }
618 
619     if (!Changes[I].Tok->is(tok::comment)) {
620       LineIsComment = false;
621     }
622 
623     if (!AlignMacrosMatches(Changes[I]))
624       continue;
625 
626     FoundMatchOnLine = true;
627 
628     if (StartOfSequence == 0)
629       StartOfSequence = I;
630 
631     unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
632     int LineLengthAfter = -Changes[I].Spaces;
633     for (unsigned j = I; j != E && Changes[j].NewlinesBefore == 0; ++j)
634       LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
635     unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
636 
637     MinColumn = std::max(MinColumn, ChangeMinColumn);
638     MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
639   }
640 
641   EndOfSequence = I;
642   AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
643                      FoundMatchOnLine, AlignMacrosMatches, Changes);
644 }
645 
646 void WhitespaceManager::alignConsecutiveAssignments() {
647   if (Style.AlignConsecutiveAssignments == FormatStyle::ACS_None)
648     return;
649 
650   AlignTokens(
651       Style,
652       [&](const Change &C) {
653         // Do not align on equal signs that are first on a line.
654         if (C.NewlinesBefore > 0)
655           return false;
656 
657         // Do not align on equal signs that are last on a line.
658         if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
659           return false;
660 
661         return C.Tok->is(tok::equal);
662       },
663       Changes, /*StartAt=*/0, Style.AlignConsecutiveAssignments);
664 }
665 
666 void WhitespaceManager::alignConsecutiveBitFields() {
667   if (Style.AlignConsecutiveBitFields == FormatStyle::ACS_None)
668     return;
669 
670   AlignTokens(
671       Style,
672       [&](Change const &C) {
673         // Do not align on ':' that is first on a line.
674         if (C.NewlinesBefore > 0)
675           return false;
676 
677         // Do not align on ':' that is last on a line.
678         if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
679           return false;
680 
681         return C.Tok->is(TT_BitFieldColon);
682       },
683       Changes, /*StartAt=*/0, Style.AlignConsecutiveBitFields);
684 }
685 
686 void WhitespaceManager::alignConsecutiveDeclarations() {
687   if (Style.AlignConsecutiveDeclarations == FormatStyle::ACS_None)
688     return;
689 
690   // FIXME: Currently we don't handle properly the PointerAlignment: Right
691   // The * and & are not aligned and are left dangling. Something has to be done
692   // about it, but it raises the question of alignment of code like:
693   //   const char* const* v1;
694   //   float const* v2;
695   //   SomeVeryLongType const& v3;
696   AlignTokens(
697       Style,
698       [](Change const &C) {
699         // tok::kw_operator is necessary for aligning operator overload
700         // definitions.
701         if (C.Tok->isOneOf(TT_FunctionDeclarationName, tok::kw_operator))
702           return true;
703         if (C.Tok->isNot(TT_StartOfName))
704           return false;
705         if (C.Tok->Previous &&
706             C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
707           return false;
708         // Check if there is a subsequent name that starts the same declaration.
709         for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
710           if (Next->is(tok::comment))
711             continue;
712           if (!Next->Tok.getIdentifierInfo())
713             break;
714           if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
715                             tok::kw_operator))
716             return false;
717         }
718         return true;
719       },
720       Changes, /*StartAt=*/0, Style.AlignConsecutiveDeclarations);
721 }
722 
723 void WhitespaceManager::alignChainedConditionals() {
724   if (Style.BreakBeforeTernaryOperators) {
725     AlignTokens(
726         Style,
727         [](Change const &C) {
728           // Align question operators and last colon
729           return C.Tok->is(TT_ConditionalExpr) &&
730                  ((C.Tok->is(tok::question) && !C.NewlinesBefore) ||
731                   (C.Tok->is(tok::colon) && C.Tok->Next &&
732                    (C.Tok->Next->FakeLParens.size() == 0 ||
733                     C.Tok->Next->FakeLParens.back() != prec::Conditional)));
734         },
735         Changes, /*StartAt=*/0);
736   } else {
737     static auto AlignWrappedOperand = [](Change const &C) {
738       auto Previous = C.Tok->getPreviousNonComment(); // Previous;
739       return C.NewlinesBefore && Previous && Previous->is(TT_ConditionalExpr) &&
740              (Previous->is(tok::question) ||
741               (Previous->is(tok::colon) &&
742                (C.Tok->FakeLParens.size() == 0 ||
743                 C.Tok->FakeLParens.back() != prec::Conditional)));
744     };
745     // Ensure we keep alignment of wrapped operands with non-wrapped operands
746     // Since we actually align the operators, the wrapped operands need the
747     // extra offset to be properly aligned.
748     for (Change &C : Changes) {
749       if (AlignWrappedOperand(C))
750         C.StartOfTokenColumn -= 2;
751     }
752     AlignTokens(
753         Style,
754         [this](Change const &C) {
755           // Align question operators if next operand is not wrapped, as
756           // well as wrapped operands after question operator or last
757           // colon in conditional sequence
758           return (C.Tok->is(TT_ConditionalExpr) && C.Tok->is(tok::question) &&
759                   &C != &Changes.back() && (&C + 1)->NewlinesBefore == 0 &&
760                   !(&C + 1)->IsTrailingComment) ||
761                  AlignWrappedOperand(C);
762         },
763         Changes, /*StartAt=*/0);
764   }
765 }
766 
767 void WhitespaceManager::alignTrailingComments() {
768   unsigned MinColumn = 0;
769   unsigned MaxColumn = UINT_MAX;
770   unsigned StartOfSequence = 0;
771   bool BreakBeforeNext = false;
772   unsigned Newlines = 0;
773   for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
774     if (Changes[i].StartOfBlockComment)
775       continue;
776     Newlines += Changes[i].NewlinesBefore;
777     if (!Changes[i].IsTrailingComment)
778       continue;
779 
780     unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
781     unsigned ChangeMaxColumn;
782 
783     if (Style.ColumnLimit == 0)
784       ChangeMaxColumn = UINT_MAX;
785     else if (Style.ColumnLimit >= Changes[i].TokenLength)
786       ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
787     else
788       ChangeMaxColumn = ChangeMinColumn;
789 
790     // If we don't create a replacement for this change, we have to consider
791     // it to be immovable.
792     if (!Changes[i].CreateReplacement)
793       ChangeMaxColumn = ChangeMinColumn;
794 
795     if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
796       ChangeMaxColumn -= 2;
797     // If this comment follows an } in column 0, it probably documents the
798     // closing of a namespace and we don't want to align it.
799     bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
800                                   Changes[i - 1].Tok->is(tok::r_brace) &&
801                                   Changes[i - 1].StartOfTokenColumn == 0;
802     bool WasAlignedWithStartOfNextLine = false;
803     if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
804       unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
805           Changes[i].OriginalWhitespaceRange.getEnd());
806       for (unsigned j = i + 1; j != e; ++j) {
807         if (Changes[j].Tok->is(tok::comment))
808           continue;
809 
810         unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
811             Changes[j].OriginalWhitespaceRange.getEnd());
812         // The start of the next token was previously aligned with the
813         // start of this comment.
814         WasAlignedWithStartOfNextLine =
815             CommentColumn == NextColumn ||
816             CommentColumn == NextColumn + Style.IndentWidth;
817         break;
818       }
819     }
820     if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
821       alignTrailingComments(StartOfSequence, i, MinColumn);
822       MinColumn = ChangeMinColumn;
823       MaxColumn = ChangeMinColumn;
824       StartOfSequence = i;
825     } else if (BreakBeforeNext || Newlines > 1 ||
826                (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
827                // Break the comment sequence if the previous line did not end
828                // in a trailing comment.
829                (Changes[i].NewlinesBefore == 1 && i > 0 &&
830                 !Changes[i - 1].IsTrailingComment) ||
831                WasAlignedWithStartOfNextLine) {
832       alignTrailingComments(StartOfSequence, i, MinColumn);
833       MinColumn = ChangeMinColumn;
834       MaxColumn = ChangeMaxColumn;
835       StartOfSequence = i;
836     } else {
837       MinColumn = std::max(MinColumn, ChangeMinColumn);
838       MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
839     }
840     BreakBeforeNext = (i == 0) || (Changes[i].NewlinesBefore > 1) ||
841                       // Never start a sequence with a comment at the beginning
842                       // of the line.
843                       (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
844     Newlines = 0;
845   }
846   alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
847 }
848 
849 void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
850                                               unsigned Column) {
851   for (unsigned i = Start; i != End; ++i) {
852     int Shift = 0;
853     if (Changes[i].IsTrailingComment) {
854       Shift = Column - Changes[i].StartOfTokenColumn;
855     }
856     if (Changes[i].StartOfBlockComment) {
857       Shift = Changes[i].IndentationOffset +
858               Changes[i].StartOfBlockComment->StartOfTokenColumn -
859               Changes[i].StartOfTokenColumn;
860     }
861     assert(Shift >= 0);
862     Changes[i].Spaces += Shift;
863     if (i + 1 != Changes.size())
864       Changes[i + 1].PreviousEndOfTokenColumn += Shift;
865     Changes[i].StartOfTokenColumn += Shift;
866   }
867 }
868 
869 void WhitespaceManager::alignEscapedNewlines() {
870   if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
871     return;
872 
873   bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
874   unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
875   unsigned StartOfMacro = 0;
876   for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
877     Change &C = Changes[i];
878     if (C.NewlinesBefore > 0) {
879       if (C.ContinuesPPDirective) {
880         MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
881       } else {
882         alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
883         MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
884         StartOfMacro = i;
885       }
886     }
887   }
888   alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
889 }
890 
891 void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
892                                              unsigned Column) {
893   for (unsigned i = Start; i < End; ++i) {
894     Change &C = Changes[i];
895     if (C.NewlinesBefore > 0) {
896       assert(C.ContinuesPPDirective);
897       if (C.PreviousEndOfTokenColumn + 1 > Column)
898         C.EscapedNewlineColumn = 0;
899       else
900         C.EscapedNewlineColumn = Column;
901     }
902   }
903 }
904 
905 void WhitespaceManager::generateChanges() {
906   for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
907     const Change &C = Changes[i];
908     if (i > 0) {
909       assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() !=
910                  C.OriginalWhitespaceRange.getBegin() &&
911              "Generating two replacements for the same location");
912     }
913     if (C.CreateReplacement) {
914       std::string ReplacementText = C.PreviousLinePostfix;
915       if (C.ContinuesPPDirective)
916         appendEscapedNewlineText(ReplacementText, C.NewlinesBefore,
917                                  C.PreviousEndOfTokenColumn,
918                                  C.EscapedNewlineColumn);
919       else
920         appendNewlineText(ReplacementText, C.NewlinesBefore);
921       appendIndentText(
922           ReplacementText, C.Tok->IndentLevel, std::max(0, C.Spaces),
923           C.StartOfTokenColumn - std::max(0, C.Spaces), C.IsAligned);
924       ReplacementText.append(C.CurrentLinePrefix);
925       storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
926     }
927   }
928 }
929 
930 void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
931   unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
932                               SourceMgr.getFileOffset(Range.getBegin());
933   // Don't create a replacement, if it does not change anything.
934   if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
935                 WhitespaceLength) == Text)
936     return;
937   auto Err = Replaces.add(tooling::Replacement(
938       SourceMgr, CharSourceRange::getCharRange(Range), Text));
939   // FIXME: better error handling. For now, just print an error message in the
940   // release version.
941   if (Err) {
942     llvm::errs() << llvm::toString(std::move(Err)) << "\n";
943     assert(false);
944   }
945 }
946 
947 void WhitespaceManager::appendNewlineText(std::string &Text,
948                                           unsigned Newlines) {
949   for (unsigned i = 0; i < Newlines; ++i)
950     Text.append(UseCRLF ? "\r\n" : "\n");
951 }
952 
953 void WhitespaceManager::appendEscapedNewlineText(
954     std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
955     unsigned EscapedNewlineColumn) {
956   if (Newlines > 0) {
957     unsigned Spaces =
958         std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
959     for (unsigned i = 0; i < Newlines; ++i) {
960       Text.append(Spaces, ' ');
961       Text.append(UseCRLF ? "\\\r\n" : "\\\n");
962       Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
963     }
964   }
965 }
966 
967 void WhitespaceManager::appendIndentText(std::string &Text,
968                                          unsigned IndentLevel, unsigned Spaces,
969                                          unsigned WhitespaceStartColumn,
970                                          bool IsAligned) {
971   switch (Style.UseTab) {
972   case FormatStyle::UT_Never:
973     Text.append(Spaces, ' ');
974     break;
975   case FormatStyle::UT_Always: {
976     if (Style.TabWidth) {
977       unsigned FirstTabWidth =
978           Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
979 
980       // Insert only spaces when we want to end up before the next tab.
981       if (Spaces < FirstTabWidth || Spaces == 1) {
982         Text.append(Spaces, ' ');
983         break;
984       }
985       // Align to the next tab.
986       Spaces -= FirstTabWidth;
987       Text.append("\t");
988 
989       Text.append(Spaces / Style.TabWidth, '\t');
990       Text.append(Spaces % Style.TabWidth, ' ');
991     } else if (Spaces == 1) {
992       Text.append(Spaces, ' ');
993     }
994     break;
995   }
996   case FormatStyle::UT_ForIndentation:
997     if (WhitespaceStartColumn == 0) {
998       unsigned Indentation = IndentLevel * Style.IndentWidth;
999       Spaces = appendTabIndent(Text, Spaces, Indentation);
1000     }
1001     Text.append(Spaces, ' ');
1002     break;
1003   case FormatStyle::UT_ForContinuationAndIndentation:
1004     if (WhitespaceStartColumn == 0)
1005       Spaces = appendTabIndent(Text, Spaces, Spaces);
1006     Text.append(Spaces, ' ');
1007     break;
1008   case FormatStyle::UT_AlignWithSpaces:
1009     if (WhitespaceStartColumn == 0) {
1010       unsigned Indentation =
1011           IsAligned ? IndentLevel * Style.IndentWidth : Spaces;
1012       Spaces = appendTabIndent(Text, Spaces, Indentation);
1013     }
1014     Text.append(Spaces, ' ');
1015     break;
1016   }
1017 }
1018 
1019 unsigned WhitespaceManager::appendTabIndent(std::string &Text, unsigned Spaces,
1020                                             unsigned Indentation) {
1021   // This happens, e.g. when a line in a block comment is indented less than the
1022   // first one.
1023   if (Indentation > Spaces)
1024     Indentation = Spaces;
1025   if (Style.TabWidth) {
1026     unsigned Tabs = Indentation / Style.TabWidth;
1027     Text.append(Tabs, '\t');
1028     Spaces -= Tabs * Style.TabWidth;
1029   }
1030   return Spaces;
1031 }
1032 
1033 } // namespace format
1034 } // namespace clang
1035