blob: a88c22f4e96a7c27960375f2dee59bf17544a352 [file] [log] [blame]
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00001//===--- WhitespaceManager.cpp - Format C++ code --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000011/// This file implements WhitespaceManager class.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000012///
13//===----------------------------------------------------------------------===//
14
15#include "WhitespaceManager.h"
16#include "llvm/ADT/STLExtras.h"
17
18namespace clang {
19namespace format {
20
Daniel Jasperb05a81d2014-05-09 13:11:16 +000021bool WhitespaceManager::Change::IsBeforeInFile::
22operator()(const Change &C1, const Change &C2) const {
Manuel Klimek4fe43002013-05-22 12:51:29 +000023 return SourceMgr.isBeforeInTranslationUnit(
24 C1.OriginalWhitespaceRange.getBegin(),
25 C2.OriginalWhitespaceRange.getBegin());
26}
Daniel Jasper6fe2f002013-04-25 08:56:26 +000027
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000028WhitespaceManager::Change::Change(const FormatToken &Tok,
29 bool CreateReplacement,
30 SourceRange OriginalWhitespaceRange,
31 int Spaces, unsigned StartOfTokenColumn,
32 unsigned NewlinesBefore,
33 StringRef PreviousLinePostfix,
34 StringRef CurrentLinePrefix,
35 bool ContinuesPPDirective, bool IsInsideToken)
36 : Tok(&Tok), CreateReplacement(CreateReplacement),
Manuel Klimek4fe43002013-05-22 12:51:29 +000037 OriginalWhitespaceRange(OriginalWhitespaceRange),
38 StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
39 PreviousLinePostfix(PreviousLinePostfix),
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000040 CurrentLinePrefix(CurrentLinePrefix),
41 ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces),
42 IsInsideToken(IsInsideToken), IsTrailingComment(false), TokenLength(0),
43 PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0),
Manuel Klimek2d293402015-03-03 14:21:48 +000044 StartOfBlockComment(nullptr), IndentationOffset(0) {}
Manuel Klimek4fe43002013-05-22 12:51:29 +000045
Manuel Klimek71814b42013-10-11 21:25:45 +000046void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000047 unsigned Spaces,
Manuel Klimek4fe43002013-05-22 12:51:29 +000048 unsigned StartOfTokenColumn,
49 bool InPPDirective) {
Manuel Klimek71814b42013-10-11 21:25:45 +000050 if (Tok.Finalized)
51 return;
52 Tok.Decision = (Newlines > 0) ? FD_Break : FD_Continue;
Nikola Smiljanic92b397f2017-03-23 02:51:25 +000053 Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange,
54 Spaces, StartOfTokenColumn, Newlines, "", "",
55 InPPDirective && !Tok.IsFirst,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000056 /*IsInsideToken=*/false));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000057}
58
Manuel Klimek4fe43002013-05-22 12:51:29 +000059void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
60 bool InPPDirective) {
Manuel Klimek71814b42013-10-11 21:25:45 +000061 if (Tok.Finalized)
62 return;
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000063 Changes.push_back(Change(Tok, /*CreateReplacement=*/false,
64 Tok.WhitespaceRange, /*Spaces=*/0,
65 Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
66 InPPDirective && !Tok.IsFirst,
67 /*IsInsideToken=*/false));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000068}
69
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000070llvm::Error
71WhitespaceManager::addReplacement(const tooling::Replacement &Replacement) {
72 return Replaces.add(Replacement);
73}
74
Alexander Kornienko555efc32013-06-11 16:01:49 +000075void WhitespaceManager::replaceWhitespaceInToken(
76 const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
77 StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000078 unsigned Newlines, int Spaces) {
Manuel Klimek71814b42013-10-11 21:25:45 +000079 if (Tok.Finalized)
80 return;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +000081 SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000082 Changes.push_back(
83 Change(Tok, /*CreateReplacement=*/true,
84 SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
85 std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
86 InPPDirective && !Tok.IsFirst, /*IsInsideToken=*/true));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000087}
88
Manuel Klimek4fe43002013-05-22 12:51:29 +000089const tooling::Replacements &WhitespaceManager::generateReplacements() {
90 if (Changes.empty())
91 return Replaces;
92
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +000093 llvm::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr));
Manuel Klimek4fe43002013-05-22 12:51:29 +000094 calculateLineBreakInformation();
Daniel Jaspere12597c2015-10-01 10:06:54 +000095 alignConsecutiveDeclarations();
Daniel Jaspera44991332015-04-29 13:06:49 +000096 alignConsecutiveAssignments();
Manuel Klimek4fe43002013-05-22 12:51:29 +000097 alignTrailingComments();
98 alignEscapedNewlines();
99 generateChanges();
100
101 return Replaces;
102}
103
104void WhitespaceManager::calculateLineBreakInformation() {
105 Changes[0].PreviousEndOfTokenColumn = 0;
Benjamin Kramerdab50462016-01-11 16:27:16 +0000106 Change *LastOutsideTokenChange = &Changes[0];
Manuel Klimek4fe43002013-05-22 12:51:29 +0000107 for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
Krasimir Georgieve1518822017-06-07 14:05:06 +0000108 SourceLocation OriginalWhitespaceStart =
109 Changes[i].OriginalWhitespaceRange.getBegin();
110 SourceLocation PreviousOriginalWhitespaceEnd =
111 Changes[i - 1].OriginalWhitespaceRange.getEnd();
112 unsigned OriginalWhitespaceStartOffset =
113 SourceMgr.getFileOffset(OriginalWhitespaceStart);
114 unsigned PreviousOriginalWhitespaceEndOffset =
115 SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
116 assert(PreviousOriginalWhitespaceEndOffset <=
117 OriginalWhitespaceStartOffset);
118 const char *const PreviousOriginalWhitespaceEndData =
119 SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
120 StringRef Text(PreviousOriginalWhitespaceEndData,
121 SourceMgr.getCharacterData(OriginalWhitespaceStart) -
122 PreviousOriginalWhitespaceEndData);
123 // Usually consecutive changes would occur in consecutive tokens. This is
124 // not the case however when analyzing some preprocessor runs of the
125 // annotated lines. For example, in this code:
126 //
127 // #if A // line 1
128 // int i = 1;
129 // #else B // line 2
130 // int i = 2;
131 // #endif // line 3
132 //
133 // one of the runs will produce the sequence of lines marked with line 1, 2
134 // and 3. So the two consecutive whitespace changes just before '// line 2'
135 // and before '#endif // line 3' span multiple lines and tokens:
136 //
137 // #else B{change X}[// line 2
138 // int i = 2;
139 // ]{change Y}#endif // line 3
140 //
141 // For this reason, if the text between consecutive changes spans multiple
142 // newlines, the token length must be adjusted to the end of the original
143 // line of the token.
144 auto NewlinePos = Text.find_first_of('\n');
145 if (NewlinePos == StringRef::npos) {
146 Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
147 PreviousOriginalWhitespaceEndOffset +
148 Changes[i].PreviousLinePostfix.size() +
149 Changes[i - 1].CurrentLinePrefix.size();
150 } else {
151 Changes[i - 1].TokenLength =
152 NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
153 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000154
Benjamin Kramerdab50462016-01-11 16:27:16 +0000155 // If there are multiple changes in this token, sum up all the changes until
156 // the end of the line.
Krasimir Georgiev59ed77b2017-06-04 19:27:02 +0000157 if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0)
Benjamin Kramerdab50462016-01-11 16:27:16 +0000158 LastOutsideTokenChange->TokenLength +=
159 Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
160 else
161 LastOutsideTokenChange = &Changes[i - 1];
162
Manuel Klimek4fe43002013-05-22 12:51:29 +0000163 Changes[i].PreviousEndOfTokenColumn =
164 Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
165
166 Changes[i - 1].IsTrailingComment =
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000167 (Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
168 (Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
169 Changes[i - 1].Tok->is(tok::comment) &&
Krasimir Georgiev91834222017-01-25 13:58:58 +0000170 // FIXME: This is a dirty hack. The problem is that
171 // BreakableLineCommentSection does comment reflow changes and here is
172 // the aligning of trailing comments. Consider the case where we reflow
173 // the second line up in this example:
Manuel Klimek89628f62017-09-20 09:51:03 +0000174 //
Krasimir Georgiev91834222017-01-25 13:58:58 +0000175 // // line 1
176 // // line 2
Manuel Klimek89628f62017-09-20 09:51:03 +0000177 //
Krasimir Georgiev91834222017-01-25 13:58:58 +0000178 // That amounts to 2 changes by BreakableLineCommentSection:
179 // - the first, delimited by (), for the whitespace between the tokens,
180 // - and second, delimited by [], for the whitespace at the beginning
181 // of the second token:
Manuel Klimek89628f62017-09-20 09:51:03 +0000182 //
Krasimir Georgiev91834222017-01-25 13:58:58 +0000183 // // line 1(
184 // )[// ]line 2
185 //
186 // So in the end we have two changes like this:
187 //
188 // // line1()[ ]line 2
189 //
190 // Note that the OriginalWhitespaceStart of the second change is the
191 // same as the PreviousOriginalWhitespaceEnd of the first change.
192 // In this case, the below check ensures that the second change doesn't
193 // get treated as a trailing comment change here, since this might
194 // trigger additional whitespace to be wrongly inserted before "line 2"
195 // by the comment aligner here.
196 //
197 // For a proper solution we need a mechanism to say to WhitespaceManager
198 // that a particular change breaks the current sequence of trailing
199 // comments.
200 OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000201 }
Manuel Klimek05c67892013-05-22 14:01:08 +0000202 // FIXME: The last token is currently not always an eof token; in those
203 // cases, setting TokenLength of the last token to 0 is wrong.
204 Changes.back().TokenLength = 0;
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000205 Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000206
207 const WhitespaceManager::Change *LastBlockComment = nullptr;
208 for (auto &Change : Changes) {
Benjamin Kramerdab50462016-01-11 16:27:16 +0000209 // Reset the IsTrailingComment flag for changes inside of trailing comments
Krasimir Georgievd105b722017-02-03 10:18:25 +0000210 // so they don't get realigned later. Comment line breaks however still need
211 // to be aligned.
212 if (Change.IsInsideToken && Change.NewlinesBefore == 0)
Benjamin Kramerdab50462016-01-11 16:27:16 +0000213 Change.IsTrailingComment = false;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000214 Change.StartOfBlockComment = nullptr;
215 Change.IndentationOffset = 0;
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000216 if (Change.Tok->is(tok::comment)) {
217 if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken)
218 LastBlockComment = &Change;
219 else {
220 if ((Change.StartOfBlockComment = LastBlockComment))
221 Change.IndentationOffset =
222 Change.StartOfTokenColumn -
223 Change.StartOfBlockComment->StartOfTokenColumn;
224 }
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000225 } else {
226 LastBlockComment = nullptr;
227 }
228 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000229}
230
Daniel Jasperec90e512015-12-01 12:00:43 +0000231// Align a single sequence of tokens, see AlignTokens below.
232template <typename F>
233static void
234AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches,
235 SmallVector<WhitespaceManager::Change, 16> &Changes) {
236 bool FoundMatchOnLine = false;
237 int Shift = 0;
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000238
239 // ScopeStack keeps track of the current scope depth. It contains indices of
240 // the first token on each scope.
241 // We only run the "Matches" function on tokens from the outer-most scope.
242 // However, we do need to pay special attention to one class of tokens
243 // that are not in the outer-most scope, and that is function parameters
244 // which are split across multiple lines, as illustrated by this example:
245 // double a(int x);
246 // int b(int y,
247 // double z);
248 // In the above example, we need to take special care to ensure that
249 // 'double z' is indented along with it's owning function 'b'.
250 SmallVector<unsigned, 16> ScopeStack;
251
Daniel Jasperec90e512015-12-01 12:00:43 +0000252 for (unsigned i = Start; i != End; ++i) {
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000253 if (ScopeStack.size() != 0 &&
Daniel Jasper4917af62017-08-25 19:14:53 +0000254 Changes[i].indentAndNestingLevel() <
255 Changes[ScopeStack.back()].indentAndNestingLevel())
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000256 ScopeStack.pop_back();
257
Ilya Biryukovf16a6fa2018-08-01 15:32:56 +0000258 // Compare current token to previous non-comment token to ensure whether
259 // it is in a deeper scope or not.
260 unsigned PreviousNonComment = i - 1;
261 while (PreviousNonComment > Start &&
262 Changes[PreviousNonComment].Tok->is(tok::comment))
263 PreviousNonComment--;
Daniel Jasper4917af62017-08-25 19:14:53 +0000264 if (i != Start && Changes[i].indentAndNestingLevel() >
Ilya Biryukovf16a6fa2018-08-01 15:32:56 +0000265 Changes[PreviousNonComment].indentAndNestingLevel())
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000266 ScopeStack.push_back(i);
267
268 bool InsideNestedScope = ScopeStack.size() != 0;
269
270 if (Changes[i].NewlinesBefore > 0 && !InsideNestedScope) {
Daniel Jasperec90e512015-12-01 12:00:43 +0000271 Shift = 0;
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000272 FoundMatchOnLine = false;
Daniel Jasperec90e512015-12-01 12:00:43 +0000273 }
274
275 // If this is the first matching token to be aligned, remember by how many
276 // spaces it has to be shifted, so the rest of the changes on the line are
277 // shifted by the same amount
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000278 if (!FoundMatchOnLine && !InsideNestedScope && Matches(Changes[i])) {
Daniel Jasperec90e512015-12-01 12:00:43 +0000279 FoundMatchOnLine = true;
280 Shift = Column - Changes[i].StartOfTokenColumn;
281 Changes[i].Spaces += Shift;
282 }
283
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000284 // This is for function parameters that are split across multiple lines,
285 // as mentioned in the ScopeStack comment.
286 if (InsideNestedScope && Changes[i].NewlinesBefore > 0) {
287 unsigned ScopeStart = ScopeStack.back();
288 if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) ||
289 (ScopeStart > Start + 1 &&
290 Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)))
291 Changes[i].Spaces += Shift;
292 }
293
Daniel Jasperec90e512015-12-01 12:00:43 +0000294 assert(Shift >= 0);
295 Changes[i].StartOfTokenColumn += Shift;
296 if (i + 1 != Changes.size())
297 Changes[i + 1].PreviousEndOfTokenColumn += Shift;
298 }
299}
300
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000301// Walk through a subset of the changes, starting at StartAt, and find
302// sequences of matching tokens to align. To do so, keep track of the lines and
303// whether or not a matching token was found on a line. If a matching token is
304// found, extend the current sequence. If the current line cannot be part of a
305// sequence, e.g. because there is an empty line before it or it contains only
306// non-matching tokens, finalize the previous sequence.
307// The value returned is the token on which we stopped, either because we
308// exhausted all items inside Changes, or because we hit a scope level higher
309// than our initial scope.
310// This function is recursive. Each invocation processes only the scope level
311// equal to the initial level, which is the level of Changes[StartAt].
312// If we encounter a scope level greater than the initial level, then we call
313// ourselves recursively, thereby avoiding the pollution of the current state
314// with the alignment requirements of the nested sub-level. This recursive
315// behavior is necessary for aligning function prototypes that have one or more
316// arguments.
317// If this function encounters a scope level less than the initial level,
318// it returns the current position.
319// There is a non-obvious subtlety in the recursive behavior: Even though we
320// defer processing of nested levels to recursive invocations of this
321// function, when it comes time to align a sequence of tokens, we run the
322// alignment on the entire sequence, including the nested levels.
323// When doing so, most of the nested tokens are skipped, because their
324// alignment was already handled by the recursive invocations of this function.
325// However, the special exception is that we do NOT skip function parameters
326// that are split across multiple lines. See the test case in FormatTest.cpp
327// that mentions "split function parameter alignment" for an example of this.
Daniel Jasperec90e512015-12-01 12:00:43 +0000328template <typename F>
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000329static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
330 SmallVector<WhitespaceManager::Change, 16> &Changes,
331 unsigned StartAt) {
Daniel Jasperec90e512015-12-01 12:00:43 +0000332 unsigned MinColumn = 0;
333 unsigned MaxColumn = UINT_MAX;
334
335 // Line number of the start and the end of the current token sequence.
336 unsigned StartOfSequence = 0;
337 unsigned EndOfSequence = 0;
338
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000339 // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
340 // abort when we hit any token in a higher scope than the starting one.
Daniel Jasper4917af62017-08-25 19:14:53 +0000341 auto IndentAndNestingLevel = StartAt < Changes.size()
342 ? Changes[StartAt].indentAndNestingLevel()
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000343 : std::pair<unsigned, unsigned>(0, 0);
Daniel Jasperec90e512015-12-01 12:00:43 +0000344
345 // Keep track of the number of commas before the matching tokens, we will only
346 // align a sequence of matching tokens if they are preceded by the same number
347 // of commas.
348 unsigned CommasBeforeLastMatch = 0;
349 unsigned CommasBeforeMatch = 0;
350
351 // Whether a matching token has been found on the current line.
352 bool FoundMatchOnLine = false;
353
354 // Aligns a sequence of matching tokens, on the MinColumn column.
355 //
356 // Sequences start from the first matching token to align, and end at the
357 // first token of the first line that doesn't need to be aligned.
358 //
359 // We need to adjust the StartOfTokenColumn of each Change that is on a line
360 // containing any matching token to be aligned and located after such token.
361 auto AlignCurrentSequence = [&] {
362 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
363 AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
364 Changes);
365 MinColumn = 0;
366 MaxColumn = UINT_MAX;
367 StartOfSequence = 0;
368 EndOfSequence = 0;
369 };
370
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000371 unsigned i = StartAt;
372 for (unsigned e = Changes.size(); i != e; ++i) {
Daniel Jasper4917af62017-08-25 19:14:53 +0000373 if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000374 break;
375
Daniel Jasperec90e512015-12-01 12:00:43 +0000376 if (Changes[i].NewlinesBefore != 0) {
377 CommasBeforeMatch = 0;
378 EndOfSequence = i;
379 // If there is a blank line, or if the last line didn't contain any
380 // matching token, the sequence ends here.
381 if (Changes[i].NewlinesBefore > 1 || !FoundMatchOnLine)
382 AlignCurrentSequence();
383
384 FoundMatchOnLine = false;
385 }
386
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000387 if (Changes[i].Tok->is(tok::comma)) {
Daniel Jasperec90e512015-12-01 12:00:43 +0000388 ++CommasBeforeMatch;
Daniel Jasper4917af62017-08-25 19:14:53 +0000389 } else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000390 // Call AlignTokens recursively, skipping over this scope block.
391 unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
392 i = StoppedAt - 1;
393 continue;
Daniel Jasperec90e512015-12-01 12:00:43 +0000394 }
395
396 if (!Matches(Changes[i]))
397 continue;
398
399 // If there is more than one matching token per line, or if the number of
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000400 // preceding commas, do not match anymore, end the sequence.
401 if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
Daniel Jasperec90e512015-12-01 12:00:43 +0000402 AlignCurrentSequence();
403
404 CommasBeforeLastMatch = CommasBeforeMatch;
Daniel Jasperec90e512015-12-01 12:00:43 +0000405 FoundMatchOnLine = true;
406
407 if (StartOfSequence == 0)
408 StartOfSequence = i;
409
410 unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
411 int LineLengthAfter = -Changes[i].Spaces;
412 for (unsigned j = i; j != e && Changes[j].NewlinesBefore == 0; ++j)
413 LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
414 unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
415
416 // If we are restricted by the maximum column width, end the sequence.
417 if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
418 CommasBeforeLastMatch != CommasBeforeMatch) {
419 AlignCurrentSequence();
420 StartOfSequence = i;
421 }
422
423 MinColumn = std::max(MinColumn, ChangeMinColumn);
424 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
425 }
426
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000427 EndOfSequence = i;
Daniel Jasperec90e512015-12-01 12:00:43 +0000428 AlignCurrentSequence();
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000429 return i;
Daniel Jasperec90e512015-12-01 12:00:43 +0000430}
431
Daniel Jaspera44991332015-04-29 13:06:49 +0000432void WhitespaceManager::alignConsecutiveAssignments() {
433 if (!Style.AlignConsecutiveAssignments)
434 return;
435
Daniel Jasperec90e512015-12-01 12:00:43 +0000436 AlignTokens(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
Daniel Jasperec90e512015-12-01 12:00:43 +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
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000446 return C.Tok->is(tok::equal);
Daniel Jasperec90e512015-12-01 12:00:43 +0000447 },
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000448 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;
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000461 AlignTokens(Style,
462 [](Change const &C) {
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000463 // tok::kw_operator is necessary for aligning operator overload
464 // definitions.
465 return C.Tok->is(TT_StartOfName) ||
466 C.Tok->is(TT_FunctionDeclarationName) ||
467 C.Tok->is(tok::kw_operator);
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000468 },
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000469 Changes, /*StartAt=*/0);
Daniel Jaspere12597c2015-10-01 10:06:54 +0000470}
471
Manuel Klimek4fe43002013-05-22 12:51:29 +0000472void WhitespaceManager::alignTrailingComments() {
473 unsigned MinColumn = 0;
474 unsigned MaxColumn = UINT_MAX;
475 unsigned StartOfSequence = 0;
476 bool BreakBeforeNext = false;
477 unsigned Newlines = 0;
478 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000479 if (Changes[i].StartOfBlockComment)
480 continue;
481 Newlines += Changes[i].NewlinesBefore;
482 if (!Changes[i].IsTrailingComment)
483 continue;
484
Manuel Klimek4fe43002013-05-22 12:51:29 +0000485 unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
Krasimir Georgiev4a9c2602017-08-23 07:18:36 +0000486 unsigned ChangeMaxColumn;
487
488 if (Style.ColumnLimit == 0)
489 ChangeMaxColumn = UINT_MAX;
490 else if (Style.ColumnLimit >= Changes[i].TokenLength)
491 ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
492 else
493 ChangeMaxColumn = ChangeMinColumn;
Daniel Jasper417fc812016-01-09 15:56:53 +0000494
495 // If we don't create a replacement for this change, we have to consider
496 // it to be immovable.
497 if (!Changes[i].CreateReplacement)
498 ChangeMaxColumn = ChangeMinColumn;
499
Daniel Jasper66935022014-04-27 10:03:19 +0000500 if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
501 ChangeMaxColumn -= 2;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000502 // If this comment follows an } in column 0, it probably documents the
503 // closing of a namespace and we don't want to align it.
504 bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000505 Changes[i - 1].Tok->is(tok::r_brace) &&
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000506 Changes[i - 1].StartOfTokenColumn == 0;
507 bool WasAlignedWithStartOfNextLine = false;
508 if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
Daniel Jasper49532102015-01-07 14:00:11 +0000509 unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
510 Changes[i].OriginalWhitespaceRange.getEnd());
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000511 for (unsigned j = i + 1; j != e; ++j) {
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000512 if (Changes[j].Tok->is(tok::comment))
Daniel Jasperbb37a2f2016-02-01 11:20:55 +0000513 continue;
514
515 unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
516 Changes[j].OriginalWhitespaceRange.getEnd());
517 // The start of the next token was previously aligned with the
518 // start of this comment.
519 WasAlignedWithStartOfNextLine =
520 CommentColumn == NextColumn ||
521 CommentColumn == NextColumn + Style.IndentWidth;
522 break;
Daniel Jasper0e93cdb2013-11-08 23:31:14 +0000523 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000524 }
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000525 if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
526 alignTrailingComments(StartOfSequence, i, MinColumn);
527 MinColumn = ChangeMinColumn;
528 MaxColumn = ChangeMinColumn;
529 StartOfSequence = i;
530 } else if (BreakBeforeNext || Newlines > 1 ||
531 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
532 // Break the comment sequence if the previous line did not end
533 // in a trailing comment.
534 (Changes[i].NewlinesBefore == 1 && i > 0 &&
535 !Changes[i - 1].IsTrailingComment) ||
536 WasAlignedWithStartOfNextLine) {
537 alignTrailingComments(StartOfSequence, i, MinColumn);
538 MinColumn = ChangeMinColumn;
539 MaxColumn = ChangeMaxColumn;
540 StartOfSequence = i;
541 } else {
542 MinColumn = std::max(MinColumn, ChangeMinColumn);
543 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
544 }
545 BreakBeforeNext =
546 (i == 0) || (Changes[i].NewlinesBefore > 1) ||
547 // Never start a sequence with a comment at the beginning of
548 // the line.
549 (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
550 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;
683 // Indent with tabs only when there's at least one full tab.
684 if (FirstTabWidth + Style.TabWidth <= Spaces) {
685 Spaces -= FirstTabWidth;
686 Text.append("\t");
687 }
Benjamin Kramerddf1cda2015-05-28 19:55:49 +0000688 Text.append(Spaces / Style.TabWidth, '\t');
689 Text.append(Spaces % Style.TabWidth, ' ');
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000690 break;
691 }
692 case FormatStyle::UT_ForIndentation:
693 if (WhitespaceStartColumn == 0) {
694 unsigned Indentation = IndentLevel * Style.IndentWidth;
Alexander Kornienko45dc1b22013-09-27 16:40:11 +0000695 // This happens, e.g. when a line in a block comment is indented less than
696 // the first one.
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000697 if (Indentation > Spaces)
698 Indentation = Spaces;
699 unsigned Tabs = Indentation / Style.TabWidth;
Benjamin Kramerddf1cda2015-05-28 19:55:49 +0000700 Text.append(Tabs, '\t');
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000701 Spaces -= Tabs * Style.TabWidth;
702 }
Benjamin Kramerddf1cda2015-05-28 19:55:49 +0000703 Text.append(Spaces, ' ');
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000704 break;
Marianne Mailhot-Sarrasin51fe2792016-04-14 14:52:26 +0000705 case FormatStyle::UT_ForContinuationAndIndentation:
706 if (WhitespaceStartColumn == 0) {
707 unsigned Tabs = Spaces / Style.TabWidth;
708 Text.append(Tabs, '\t');
709 Spaces -= Tabs * Style.TabWidth;
710 }
711 Text.append(Spaces, ' ');
712 break;
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000713 }
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000714}
715
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000716} // namespace format
717} // namespace clang