blob: 9f1ea7022f0de5e8ad6c565a4791fb90cf1ea263 [file] [log] [blame]
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00001//===--- WhitespaceManager.cpp - Format C++ code --------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file implements WhitespaceManager class.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000011///
12//===----------------------------------------------------------------------===//
13
14#include "WhitespaceManager.h"
15#include "llvm/ADT/STLExtras.h"
16
17namespace clang {
18namespace format {
19
Daniel Jasperb05a81d2014-05-09 13:11:16 +000020bool WhitespaceManager::Change::IsBeforeInFile::
21operator()(const Change &C1, const Change &C2) const {
Manuel Klimek4fe43002013-05-22 12:51:29 +000022 return SourceMgr.isBeforeInTranslationUnit(
23 C1.OriginalWhitespaceRange.getBegin(),
24 C2.OriginalWhitespaceRange.getBegin());
25}
Daniel Jasper6fe2f002013-04-25 08:56:26 +000026
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000027WhitespaceManager::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,
34 bool ContinuesPPDirective, bool IsInsideToken)
35 : Tok(&Tok), CreateReplacement(CreateReplacement),
Manuel Klimek4fe43002013-05-22 12:51:29 +000036 OriginalWhitespaceRange(OriginalWhitespaceRange),
37 StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
38 PreviousLinePostfix(PreviousLinePostfix),
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000039 CurrentLinePrefix(CurrentLinePrefix),
40 ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces),
41 IsInsideToken(IsInsideToken), IsTrailingComment(false), TokenLength(0),
42 PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0),
Manuel Klimek2d293402015-03-03 14:21:48 +000043 StartOfBlockComment(nullptr), IndentationOffset(0) {}
Manuel Klimek4fe43002013-05-22 12:51:29 +000044
Manuel Klimek71814b42013-10-11 21:25:45 +000045void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000046 unsigned Spaces,
Manuel Klimek4fe43002013-05-22 12:51:29 +000047 unsigned StartOfTokenColumn,
48 bool InPPDirective) {
Manuel Klimek71814b42013-10-11 21:25:45 +000049 if (Tok.Finalized)
50 return;
51 Tok.Decision = (Newlines > 0) ? FD_Break : FD_Continue;
Nikola Smiljanic92b397f2017-03-23 02:51:25 +000052 Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
53 Spaces, StartOfTokenColumn, Newlines, "", "",
54 InPPDirective && !Tok.IsFirst,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000055 /*IsInsideToken=*/false));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000056}
57
Manuel Klimek4fe43002013-05-22 12:51:29 +000058void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
59 bool InPPDirective) {
Manuel Klimek71814b42013-10-11 21:25:45 +000060 if (Tok.Finalized)
61 return;
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000062 Changes.push_back(Change(Tok, /*CreateReplacement=*/false,
63 Tok.WhitespaceRange, /*Spaces=*/0,
64 Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
65 InPPDirective && !Tok.IsFirst,
66 /*IsInsideToken=*/false));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000067}
68
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000069llvm::Error
70WhitespaceManager::addReplacement(const tooling::Replacement &Replacement) {
71 return Replaces.add(Replacement);
72}
73
Alexander Kornienko555efc32013-06-11 16:01:49 +000074void WhitespaceManager::replaceWhitespaceInToken(
75 const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
76 StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000077 unsigned Newlines, int Spaces) {
Manuel Klimek71814b42013-10-11 21:25:45 +000078 if (Tok.Finalized)
79 return;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +000080 SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000081 Changes.push_back(
82 Change(Tok, /*CreateReplacement=*/true,
83 SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
84 std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
85 InPPDirective && !Tok.IsFirst, /*IsInsideToken=*/true));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000086}
87
Manuel Klimek4fe43002013-05-22 12:51:29 +000088const tooling::Replacements &WhitespaceManager::generateReplacements() {
89 if (Changes.empty())
90 return Replaces;
91
Fangrui Song55fab262018-09-26 22:16:28 +000092 llvm::sort(Changes, Change::IsBeforeInFile(SourceMgr));
Manuel Klimek4fe43002013-05-22 12:51:29 +000093 calculateLineBreakInformation();
Daniel Jaspere12597c2015-10-01 10:06:54 +000094 alignConsecutiveDeclarations();
Daniel Jaspera44991332015-04-29 13:06:49 +000095 alignConsecutiveAssignments();
Manuel Klimek4fe43002013-05-22 12:51:29 +000096 alignTrailingComments();
97 alignEscapedNewlines();
98 generateChanges();
99
100 return Replaces;
101}
102
103void WhitespaceManager::calculateLineBreakInformation() {
104 Changes[0].PreviousEndOfTokenColumn = 0;
Benjamin Kramerdab50462016-01-11 16:27:16 +0000105 Change *LastOutsideTokenChange = &Changes[0];
Manuel Klimek4fe43002013-05-22 12:51:29 +0000106 for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
Krasimir Georgieve1518822017-06-07 14:05:06 +0000107 SourceLocation OriginalWhitespaceStart =
108 Changes[i].OriginalWhitespaceRange.getBegin();
109 SourceLocation PreviousOriginalWhitespaceEnd =
110 Changes[i - 1].OriginalWhitespaceRange.getEnd();
111 unsigned OriginalWhitespaceStartOffset =
112 SourceMgr.getFileOffset(OriginalWhitespaceStart);
113 unsigned PreviousOriginalWhitespaceEndOffset =
114 SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
115 assert(PreviousOriginalWhitespaceEndOffset <=
116 OriginalWhitespaceStartOffset);
117 const char *const PreviousOriginalWhitespaceEndData =
118 SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
119 StringRef Text(PreviousOriginalWhitespaceEndData,
120 SourceMgr.getCharacterData(OriginalWhitespaceStart) -
121 PreviousOriginalWhitespaceEndData);
122 // Usually consecutive changes would occur in consecutive tokens. This is
123 // not the case however when analyzing some preprocessor runs of the
124 // annotated lines. For example, in this code:
125 //
126 // #if A // line 1
127 // int i = 1;
128 // #else B // line 2
129 // int i = 2;
130 // #endif // line 3
131 //
132 // one of the runs will produce the sequence of lines marked with line 1, 2
133 // and 3. So the two consecutive whitespace changes just before '// line 2'
134 // and before '#endif // line 3' span multiple lines and tokens:
135 //
136 // #else B{change X}[// line 2
137 // int i = 2;
138 // ]{change Y}#endif // line 3
139 //
140 // For this reason, if the text between consecutive changes spans multiple
141 // newlines, the token length must be adjusted to the end of the original
142 // line of the token.
143 auto NewlinePos = Text.find_first_of('\n');
144 if (NewlinePos == StringRef::npos) {
145 Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
146 PreviousOriginalWhitespaceEndOffset +
147 Changes[i].PreviousLinePostfix.size() +
148 Changes[i - 1].CurrentLinePrefix.size();
149 } else {
150 Changes[i - 1].TokenLength =
151 NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
152 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000153
Benjamin Kramerdab50462016-01-11 16:27:16 +0000154 // If there are multiple changes in this token, sum up all the changes until
155 // the end of the line.
Krasimir Georgiev59ed77b2017-06-04 19:27:02 +0000156 if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0)
Benjamin Kramerdab50462016-01-11 16:27:16 +0000157 LastOutsideTokenChange->TokenLength +=
158 Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
159 else
160 LastOutsideTokenChange = &Changes[i - 1];
161
Manuel Klimek4fe43002013-05-22 12:51:29 +0000162 Changes[i].PreviousEndOfTokenColumn =
163 Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
164
165 Changes[i - 1].IsTrailingComment =
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000166 (Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
167 (Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
168 Changes[i - 1].Tok->is(tok::comment) &&
Krasimir Georgiev91834222017-01-25 13:58:58 +0000169 // FIXME: This is a dirty hack. The problem is that
170 // BreakableLineCommentSection does comment reflow changes and here is
171 // the aligning of trailing comments. Consider the case where we reflow
172 // the second line up in this example:
Manuel Klimek89628f62017-09-20 09:51:03 +0000173 //
Krasimir Georgiev91834222017-01-25 13:58:58 +0000174 // // line 1
175 // // line 2
Manuel Klimek89628f62017-09-20 09:51:03 +0000176 //
Krasimir Georgiev91834222017-01-25 13:58:58 +0000177 // That amounts to 2 changes by BreakableLineCommentSection:
178 // - the first, delimited by (), for the whitespace between the tokens,
179 // - and second, delimited by [], for the whitespace at the beginning
180 // of the second token:
Manuel Klimek89628f62017-09-20 09:51:03 +0000181 //
Krasimir Georgiev91834222017-01-25 13:58:58 +0000182 // // line 1(
183 // )[// ]line 2
184 //
185 // So in the end we have two changes like this:
186 //
187 // // line1()[ ]line 2
188 //
189 // Note that the OriginalWhitespaceStart of the second change is the
190 // same as the PreviousOriginalWhitespaceEnd of the first change.
191 // In this case, the below check ensures that the second change doesn't
192 // get treated as a trailing comment change here, since this might
193 // trigger additional whitespace to be wrongly inserted before "line 2"
194 // by the comment aligner here.
195 //
196 // For a proper solution we need a mechanism to say to WhitespaceManager
197 // that a particular change breaks the current sequence of trailing
198 // comments.
199 OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000200 }
Manuel Klimek05c67892013-05-22 14:01:08 +0000201 // FIXME: The last token is currently not always an eof token; in those
202 // cases, setting TokenLength of the last token to 0 is wrong.
203 Changes.back().TokenLength = 0;
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000204 Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000205
206 const WhitespaceManager::Change *LastBlockComment = nullptr;
207 for (auto &Change : Changes) {
Benjamin Kramerdab50462016-01-11 16:27:16 +0000208 // Reset the IsTrailingComment flag for changes inside of trailing comments
Krasimir Georgievd105b722017-02-03 10:18:25 +0000209 // so they don't get realigned later. Comment line breaks however still need
210 // to be aligned.
211 if (Change.IsInsideToken && Change.NewlinesBefore == 0)
Benjamin Kramerdab50462016-01-11 16:27:16 +0000212 Change.IsTrailingComment = false;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000213 Change.StartOfBlockComment = nullptr;
214 Change.IndentationOffset = 0;
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000215 if (Change.Tok->is(tok::comment)) {
216 if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken)
217 LastBlockComment = &Change;
218 else {
219 if ((Change.StartOfBlockComment = LastBlockComment))
220 Change.IndentationOffset =
221 Change.StartOfTokenColumn -
222 Change.StartOfBlockComment->StartOfTokenColumn;
223 }
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000224 } else {
225 LastBlockComment = nullptr;
226 }
227 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000228}
229
Daniel Jasperec90e512015-12-01 12:00:43 +0000230// Align a single sequence of tokens, see AlignTokens below.
231template <typename F>
232static void
233AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches,
234 SmallVector<WhitespaceManager::Change, 16> &Changes) {
235 bool FoundMatchOnLine = false;
236 int Shift = 0;
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000237
238 // ScopeStack keeps track of the current scope depth. It contains indices of
239 // the first token on each scope.
240 // We only run the "Matches" function on tokens from the outer-most scope.
241 // However, we do need to pay special attention to one class of tokens
242 // that are not in the outer-most scope, and that is function parameters
243 // which are split across multiple lines, as illustrated by this example:
244 // double a(int x);
245 // int b(int y,
246 // double z);
247 // In the above example, we need to take special care to ensure that
248 // 'double z' is indented along with it's owning function 'b'.
249 SmallVector<unsigned, 16> ScopeStack;
250
Daniel Jasperec90e512015-12-01 12:00:43 +0000251 for (unsigned i = Start; i != End; ++i) {
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000252 if (ScopeStack.size() != 0 &&
Daniel Jasper4917af62017-08-25 19:14:53 +0000253 Changes[i].indentAndNestingLevel() <
254 Changes[ScopeStack.back()].indentAndNestingLevel())
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000255 ScopeStack.pop_back();
256
Ilya Biryukovf16a6fa2018-08-01 15:32:56 +0000257 // Compare current token to previous non-comment token to ensure whether
258 // it is in a deeper scope or not.
259 unsigned PreviousNonComment = i - 1;
260 while (PreviousNonComment > Start &&
261 Changes[PreviousNonComment].Tok->is(tok::comment))
262 PreviousNonComment--;
Daniel Jasper4917af62017-08-25 19:14:53 +0000263 if (i != Start && Changes[i].indentAndNestingLevel() >
Ilya Biryukovf16a6fa2018-08-01 15:32:56 +0000264 Changes[PreviousNonComment].indentAndNestingLevel())
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000265 ScopeStack.push_back(i);
266
267 bool InsideNestedScope = ScopeStack.size() != 0;
268
269 if (Changes[i].NewlinesBefore > 0 && !InsideNestedScope) {
Daniel Jasperec90e512015-12-01 12:00:43 +0000270 Shift = 0;
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000271 FoundMatchOnLine = false;
Daniel Jasperec90e512015-12-01 12:00:43 +0000272 }
273
274 // If this is the first matching token to be aligned, remember by how many
275 // spaces it has to be shifted, so the rest of the changes on the line are
276 // shifted by the same amount
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000277 if (!FoundMatchOnLine && !InsideNestedScope && Matches(Changes[i])) {
Daniel Jasperec90e512015-12-01 12:00:43 +0000278 FoundMatchOnLine = true;
279 Shift = Column - Changes[i].StartOfTokenColumn;
280 Changes[i].Spaces += Shift;
281 }
282
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000283 // This is for function parameters that are split across multiple lines,
284 // as mentioned in the ScopeStack comment.
285 if (InsideNestedScope && Changes[i].NewlinesBefore > 0) {
286 unsigned ScopeStart = ScopeStack.back();
287 if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) ||
288 (ScopeStart > Start + 1 &&
289 Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)))
290 Changes[i].Spaces += Shift;
291 }
292
Daniel Jasperec90e512015-12-01 12:00:43 +0000293 assert(Shift >= 0);
294 Changes[i].StartOfTokenColumn += Shift;
295 if (i + 1 != Changes.size())
296 Changes[i + 1].PreviousEndOfTokenColumn += Shift;
297 }
298}
299
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000300// Walk through a subset of the changes, starting at StartAt, and find
301// sequences of matching tokens to align. To do so, keep track of the lines and
302// whether or not a matching token was found on a line. If a matching token is
303// found, extend the current sequence. If the current line cannot be part of a
304// sequence, e.g. because there is an empty line before it or it contains only
305// non-matching tokens, finalize the previous sequence.
306// The value returned is the token on which we stopped, either because we
307// exhausted all items inside Changes, or because we hit a scope level higher
308// than our initial scope.
309// This function is recursive. Each invocation processes only the scope level
310// equal to the initial level, which is the level of Changes[StartAt].
311// If we encounter a scope level greater than the initial level, then we call
312// ourselves recursively, thereby avoiding the pollution of the current state
313// with the alignment requirements of the nested sub-level. This recursive
314// behavior is necessary for aligning function prototypes that have one or more
315// arguments.
316// If this function encounters a scope level less than the initial level,
317// it returns the current position.
318// There is a non-obvious subtlety in the recursive behavior: Even though we
319// defer processing of nested levels to recursive invocations of this
320// function, when it comes time to align a sequence of tokens, we run the
321// alignment on the entire sequence, including the nested levels.
322// When doing so, most of the nested tokens are skipped, because their
323// alignment was already handled by the recursive invocations of this function.
324// However, the special exception is that we do NOT skip function parameters
325// that are split across multiple lines. See the test case in FormatTest.cpp
326// that mentions "split function parameter alignment" for an example of this.
Daniel Jasperec90e512015-12-01 12:00:43 +0000327template <typename F>
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000328static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
329 SmallVector<WhitespaceManager::Change, 16> &Changes,
330 unsigned StartAt) {
Daniel Jasperec90e512015-12-01 12:00:43 +0000331 unsigned MinColumn = 0;
332 unsigned MaxColumn = UINT_MAX;
333
334 // Line number of the start and the end of the current token sequence.
335 unsigned StartOfSequence = 0;
336 unsigned EndOfSequence = 0;
337
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000338 // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
339 // abort when we hit any token in a higher scope than the starting one.
Daniel Jasper4917af62017-08-25 19:14:53 +0000340 auto IndentAndNestingLevel = StartAt < Changes.size()
341 ? Changes[StartAt].indentAndNestingLevel()
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000342 : std::pair<unsigned, unsigned>(0, 0);
Daniel Jasperec90e512015-12-01 12:00:43 +0000343
344 // Keep track of the number of commas before the matching tokens, we will only
345 // align a sequence of matching tokens if they are preceded by the same number
346 // of commas.
347 unsigned CommasBeforeLastMatch = 0;
348 unsigned CommasBeforeMatch = 0;
349
350 // Whether a matching token has been found on the current line.
351 bool FoundMatchOnLine = false;
352
353 // Aligns a sequence of matching tokens, on the MinColumn column.
354 //
355 // Sequences start from the first matching token to align, and end at the
356 // first token of the first line that doesn't need to be aligned.
357 //
358 // We need to adjust the StartOfTokenColumn of each Change that is on a line
359 // containing any matching token to be aligned and located after such token.
360 auto AlignCurrentSequence = [&] {
361 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
362 AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
363 Changes);
364 MinColumn = 0;
365 MaxColumn = UINT_MAX;
366 StartOfSequence = 0;
367 EndOfSequence = 0;
368 };
369
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000370 unsigned i = StartAt;
371 for (unsigned e = Changes.size(); i != e; ++i) {
Daniel Jasper4917af62017-08-25 19:14:53 +0000372 if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000373 break;
374
Daniel Jasperec90e512015-12-01 12:00:43 +0000375 if (Changes[i].NewlinesBefore != 0) {
376 CommasBeforeMatch = 0;
377 EndOfSequence = i;
378 // If there is a blank line, or if the last line didn't contain any
379 // matching token, the sequence ends here.
380 if (Changes[i].NewlinesBefore > 1 || !FoundMatchOnLine)
381 AlignCurrentSequence();
382
383 FoundMatchOnLine = false;
384 }
385
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000386 if (Changes[i].Tok->is(tok::comma)) {
Daniel Jasperec90e512015-12-01 12:00:43 +0000387 ++CommasBeforeMatch;
Daniel Jasper4917af62017-08-25 19:14:53 +0000388 } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000389 // Call AlignTokens recursively, skipping over this scope block.
390 unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
391 i = StoppedAt - 1;
392 continue;
Daniel Jasperec90e512015-12-01 12:00:43 +0000393 }
394
395 if (!Matches(Changes[i]))
396 continue;
397
398 // If there is more than one matching token per line, or if the number of
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000399 // preceding commas, do not match anymore, end the sequence.
400 if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
Daniel Jasperec90e512015-12-01 12:00:43 +0000401 AlignCurrentSequence();
402
403 CommasBeforeLastMatch = CommasBeforeMatch;
Daniel Jasperec90e512015-12-01 12:00:43 +0000404 FoundMatchOnLine = true;
405
406 if (StartOfSequence == 0)
407 StartOfSequence = i;
408
409 unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
410 int LineLengthAfter = -Changes[i].Spaces;
411 for (unsigned j = i; j != e && Changes[j].NewlinesBefore == 0; ++j)
412 LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
413 unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
414
415 // If we are restricted by the maximum column width, end the sequence.
416 if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
417 CommasBeforeLastMatch != CommasBeforeMatch) {
418 AlignCurrentSequence();
419 StartOfSequence = i;
420 }
421
422 MinColumn = std::max(MinColumn, ChangeMinColumn);
423 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
424 }
425
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000426 EndOfSequence = i;
Daniel Jasperec90e512015-12-01 12:00:43 +0000427 AlignCurrentSequence();
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000428 return i;
Daniel Jasperec90e512015-12-01 12:00:43 +0000429}
430
Daniel Jaspera44991332015-04-29 13:06:49 +0000431void WhitespaceManager::alignConsecutiveAssignments() {
432 if (!Style.AlignConsecutiveAssignments)
433 return;
434
Paul Hoad5bcf99b2019-03-01 09:09:54 +0000435 AlignTokens(
436 Style,
437 [&](const Change &C) {
438 // Do not align on equal signs that are first on a line.
439 if (C.NewlinesBefore > 0)
440 return false;
Daniel Jaspera44991332015-04-29 13:06:49 +0000441
Paul Hoad5bcf99b2019-03-01 09:09:54 +0000442 // Do not align on equal signs that are last on a line.
443 if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
444 return false;
Daniel Jaspera44991332015-04-29 13:06:49 +0000445
Paul Hoad5bcf99b2019-03-01 09:09:54 +0000446 return C.Tok->is(tok::equal);
447 },
448 Changes, /*StartAt=*/0);
Daniel Jaspera44991332015-04-29 13:06:49 +0000449}
450
Daniel Jaspere12597c2015-10-01 10:06:54 +0000451void WhitespaceManager::alignConsecutiveDeclarations() {
452 if (!Style.AlignConsecutiveDeclarations)
453 return;
454
Daniel Jasperec90e512015-12-01 12:00:43 +0000455 // FIXME: Currently we don't handle properly the PointerAlignment: Right
456 // The * and & are not aligned and are left dangling. Something has to be done
457 // about it, but it raises the question of alignment of code like:
458 // const char* const* v1;
459 // float const* v2;
460 // SomeVeryLongType const& v3;
Paul Hoad5bcf99b2019-03-01 09:09:54 +0000461 AlignTokens(
462 Style,
463 [](Change const &C) {
464 // tok::kw_operator is necessary for aligning operator overload
465 // definitions.
466 return C.Tok->is(TT_StartOfName) ||
467 C.Tok->is(TT_FunctionDeclarationName) ||
468 C.Tok->is(tok::kw_operator);
469 },
470 Changes, /*StartAt=*/0);
Daniel Jaspere12597c2015-10-01 10:06:54 +0000471}
472
Manuel Klimek4fe43002013-05-22 12:51:29 +0000473void WhitespaceManager::alignTrailingComments() {
474 unsigned MinColumn = 0;
475 unsigned MaxColumn = UINT_MAX;
476 unsigned StartOfSequence = 0;
477 bool BreakBeforeNext = false;
478 unsigned Newlines = 0;
479 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000480 if (Changes[i].StartOfBlockComment)
481 continue;
482 Newlines += Changes[i].NewlinesBefore;
483 if (!Changes[i].IsTrailingComment)
484 continue;
485
Manuel Klimek4fe43002013-05-22 12:51:29 +0000486 unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
Krasimir Georgiev4a9c2602017-08-23 07:18:36 +0000487 unsigned ChangeMaxColumn;
488
489 if (Style.ColumnLimit == 0)
490 ChangeMaxColumn = UINT_MAX;
491 else if (Style.ColumnLimit >= Changes[i].TokenLength)
492 ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
493 else
494 ChangeMaxColumn = ChangeMinColumn;
Daniel Jasper417fc812016-01-09 15:56:53 +0000495
496 // If we don't create a replacement for this change, we have to consider
497 // it to be immovable.
498 if (!Changes[i].CreateReplacement)
499 ChangeMaxColumn = ChangeMinColumn;
500
Daniel Jasper66935022014-04-27 10:03:19 +0000501 if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
502 ChangeMaxColumn -= 2;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000503 // If this comment follows an } in column 0, it probably documents the
504 // closing of a namespace and we don't want to align it.
505 bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000506 Changes[i - 1].Tok->is(tok::r_brace) &&
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000507 Changes[i - 1].StartOfTokenColumn == 0;
508 bool WasAlignedWithStartOfNextLine = false;
509 if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
Daniel Jasper49532102015-01-07 14:00:11 +0000510 unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
511 Changes[i].OriginalWhitespaceRange.getEnd());
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000512 for (unsigned j = i + 1; j != e; ++j) {
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000513 if (Changes[j].Tok->is(tok::comment))
Daniel Jasperbb37a2f2016-02-01 11:20:55 +0000514 continue;
515
516 unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
517 Changes[j].OriginalWhitespaceRange.getEnd());
518 // The start of the next token was previously aligned with the
519 // start of this comment.
520 WasAlignedWithStartOfNextLine =
521 CommentColumn == NextColumn ||
522 CommentColumn == NextColumn + Style.IndentWidth;
523 break;
Daniel Jasper0e93cdb2013-11-08 23:31:14 +0000524 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000525 }
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000526 if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
527 alignTrailingComments(StartOfSequence, i, MinColumn);
528 MinColumn = ChangeMinColumn;
529 MaxColumn = ChangeMinColumn;
530 StartOfSequence = i;
531 } else if (BreakBeforeNext || Newlines > 1 ||
532 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
533 // Break the comment sequence if the previous line did not end
534 // in a trailing comment.
535 (Changes[i].NewlinesBefore == 1 && i > 0 &&
536 !Changes[i - 1].IsTrailingComment) ||
537 WasAlignedWithStartOfNextLine) {
538 alignTrailingComments(StartOfSequence, i, MinColumn);
539 MinColumn = ChangeMinColumn;
540 MaxColumn = ChangeMaxColumn;
541 StartOfSequence = i;
542 } else {
543 MinColumn = std::max(MinColumn, ChangeMinColumn);
544 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
545 }
Paul Hoad5bcf99b2019-03-01 09:09:54 +0000546 BreakBeforeNext = (i == 0) || (Changes[i].NewlinesBefore > 1) ||
547 // Never start a sequence with a comment at the beginning
548 // of the line.
549 (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000550 Newlines = 0;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000551 }
552 alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
553}
554
555void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
556 unsigned Column) {
557 for (unsigned i = Start; i != End; ++i) {
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000558 int Shift = 0;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000559 if (Changes[i].IsTrailingComment) {
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000560 Shift = Column - Changes[i].StartOfTokenColumn;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000561 }
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000562 if (Changes[i].StartOfBlockComment) {
563 Shift = Changes[i].IndentationOffset +
564 Changes[i].StartOfBlockComment->StartOfTokenColumn -
565 Changes[i].StartOfTokenColumn;
566 }
567 assert(Shift >= 0);
568 Changes[i].Spaces += Shift;
Andi-Bogdan Postelnicua9a8fde2016-10-26 07:44:51 +0000569 if (i + 1 != Changes.size())
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000570 Changes[i + 1].PreviousEndOfTokenColumn += Shift;
571 Changes[i].StartOfTokenColumn += Shift;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000572 }
573}
574
575void WhitespaceManager::alignEscapedNewlines() {
Daniel Jasper7fdbb3f2017-05-08 15:08:00 +0000576 if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
577 return;
578
579 bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
580 unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000581 unsigned StartOfMacro = 0;
582 for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
583 Change &C = Changes[i];
584 if (C.NewlinesBefore > 0) {
585 if (C.ContinuesPPDirective) {
Daniel Jaspera49393f2013-08-28 09:07:32 +0000586 MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
Manuel Klimek4fe43002013-05-22 12:51:29 +0000587 } else {
588 alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
Daniel Jasper7fdbb3f2017-05-08 15:08:00 +0000589 MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000590 StartOfMacro = i;
591 }
592 }
593 }
594 alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
595}
596
597void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
598 unsigned Column) {
599 for (unsigned i = Start; i < End; ++i) {
600 Change &C = Changes[i];
601 if (C.NewlinesBefore > 0) {
602 assert(C.ContinuesPPDirective);
603 if (C.PreviousEndOfTokenColumn + 1 > Column)
604 C.EscapedNewlineColumn = 0;
605 else
606 C.EscapedNewlineColumn = Column;
607 }
608 }
609}
610
611void WhitespaceManager::generateChanges() {
612 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
613 const Change &C = Changes[i];
Daniel Jasper47b35ae2015-01-29 10:47:14 +0000614 if (i > 0) {
615 assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() !=
616 C.OriginalWhitespaceRange.getBegin() &&
617 "Generating two replacements for the same location");
618 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000619 if (C.CreateReplacement) {
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000620 std::string ReplacementText = C.PreviousLinePostfix;
621 if (C.ContinuesPPDirective)
Jacob Bandes-Storchd6a7e982017-08-10 00:15:31 +0000622 appendEscapedNewlineText(ReplacementText, C.NewlinesBefore,
623 C.PreviousEndOfTokenColumn,
624 C.EscapedNewlineColumn);
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000625 else
626 appendNewlineText(ReplacementText, C.NewlinesBefore);
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000627 appendIndentText(ReplacementText, C.Tok->IndentLevel,
628 std::max(0, C.Spaces),
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000629 C.StartOfTokenColumn - std::max(0, C.Spaces));
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000630 ReplacementText.append(C.CurrentLinePrefix);
Manuel Klimek4fe43002013-05-22 12:51:29 +0000631 storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
632 }
633 }
634}
635
Manuel Klimek89628f62017-09-20 09:51:03 +0000636void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
Manuel Klimek4fe43002013-05-22 12:51:29 +0000637 unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
638 SourceMgr.getFileOffset(Range.getBegin());
639 // Don't create a replacement, if it does not change anything.
640 if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000641 WhitespaceLength) == Text)
Manuel Klimek4fe43002013-05-22 12:51:29 +0000642 return;
Eric Liu40ef2fb2016-08-01 10:16:37 +0000643 auto Err = Replaces.add(tooling::Replacement(
Manuel Klimek4fe43002013-05-22 12:51:29 +0000644 SourceMgr, CharSourceRange::getCharRange(Range), Text));
Eric Liu40ef2fb2016-08-01 10:16:37 +0000645 // FIXME: better error handling. For now, just print an error message in the
646 // release version.
Piotr Padlewski1ec383c2016-12-23 11:40:44 +0000647 if (Err) {
Eric Liu40ef2fb2016-08-01 10:16:37 +0000648 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
Piotr Padlewski1ec383c2016-12-23 11:40:44 +0000649 assert(false);
650 }
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000651}
652
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000653void WhitespaceManager::appendNewlineText(std::string &Text,
654 unsigned Newlines) {
655 for (unsigned i = 0; i < Newlines; ++i)
656 Text.append(UseCRLF ? "\r\n" : "\n");
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000657}
658
Manuel Klimek89628f62017-09-20 09:51:03 +0000659void WhitespaceManager::appendEscapedNewlineText(
660 std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
661 unsigned EscapedNewlineColumn) {
Alexander Kornienko555efc32013-06-11 16:01:49 +0000662 if (Newlines > 0) {
Jacob Bandes-Storchd6a7e982017-08-10 00:15:31 +0000663 unsigned Spaces =
664 std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
Alexander Kornienko555efc32013-06-11 16:01:49 +0000665 for (unsigned i = 0; i < Newlines; ++i) {
Jacob Bandes-Storchd6a7e982017-08-10 00:15:31 +0000666 Text.append(Spaces, ' ');
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000667 Text.append(UseCRLF ? "\\\r\n" : "\\\n");
Jacob Bandes-Storchd6a7e982017-08-10 00:15:31 +0000668 Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000669 }
670 }
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000671}
672
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000673void WhitespaceManager::appendIndentText(std::string &Text,
674 unsigned IndentLevel, unsigned Spaces,
Alexander Kornienkodb4c21f2013-09-27 09:45:40 +0000675 unsigned WhitespaceStartColumn) {
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000676 switch (Style.UseTab) {
677 case FormatStyle::UT_Never:
Benjamin Kramerddf1cda2015-05-28 19:55:49 +0000678 Text.append(Spaces, ' ');
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000679 break;
680 case FormatStyle::UT_Always: {
Alexander Kornienkodb4c21f2013-09-27 09:45:40 +0000681 unsigned FirstTabWidth =
682 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
Alexander Kornienko027f5f52019-02-15 23:07:43 +0000683 // Insert only spaces when we want to end up before the next tab.
684 if (Spaces < FirstTabWidth || Spaces == 1) {
685 Text.append(Spaces, ' ');
686 break;
Alexander Kornienkodb4c21f2013-09-27 09:45:40 +0000687 }
Alexander Kornienko027f5f52019-02-15 23:07:43 +0000688 // Align to the next tab.
689 Spaces -= FirstTabWidth;
690 Text.append("\t");
691
Benjamin Kramerddf1cda2015-05-28 19:55:49 +0000692 Text.append(Spaces / Style.TabWidth, '\t');
693 Text.append(Spaces % Style.TabWidth, ' ');
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000694 break;
695 }
696 case FormatStyle::UT_ForIndentation:
697 if (WhitespaceStartColumn == 0) {
698 unsigned Indentation = IndentLevel * Style.IndentWidth;
Alexander Kornienko45dc1b22013-09-27 16:40:11 +0000699 // This happens, e.g. when a line in a block comment is indented less than
700 // the first one.
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000701 if (Indentation > Spaces)
702 Indentation = Spaces;
703 unsigned Tabs = Indentation / Style.TabWidth;
Benjamin Kramerddf1cda2015-05-28 19:55:49 +0000704 Text.append(Tabs, '\t');
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000705 Spaces -= Tabs * Style.TabWidth;
706 }
Benjamin Kramerddf1cda2015-05-28 19:55:49 +0000707 Text.append(Spaces, ' ');
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000708 break;
Marianne Mailhot-Sarrasin51fe2792016-04-14 14:52:26 +0000709 case FormatStyle::UT_ForContinuationAndIndentation:
710 if (WhitespaceStartColumn == 0) {
711 unsigned Tabs = Spaces / Style.TabWidth;
712 Text.append(Tabs, '\t');
713 Spaces -= Tabs * Style.TabWidth;
714 }
715 Text.append(Spaces, ' ');
716 break;
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000717 }
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000718}
719
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000720} // namespace format
721} // namespace clang