blob: 4b4fd13145fbc120aa59b84cf66d5c3d81a9998a [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
11/// \brief This file implements WhitespaceManager class.
12///
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
Alexander Kornienko555efc32013-06-11 16:01:49 +000070void WhitespaceManager::replaceWhitespaceInToken(
71 const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
72 StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000073 unsigned Newlines, int Spaces) {
Manuel Klimek71814b42013-10-11 21:25:45 +000074 if (Tok.Finalized)
75 return;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +000076 SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000077 Changes.push_back(
78 Change(Tok, /*CreateReplacement=*/true,
79 SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
80 std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
81 InPPDirective && !Tok.IsFirst, /*IsInsideToken=*/true));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000082}
83
Manuel Klimek4fe43002013-05-22 12:51:29 +000084const tooling::Replacements &WhitespaceManager::generateReplacements() {
85 if (Changes.empty())
86 return Replaces;
87
88 std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr));
89 calculateLineBreakInformation();
Daniel Jaspere12597c2015-10-01 10:06:54 +000090 alignConsecutiveDeclarations();
Daniel Jaspera44991332015-04-29 13:06:49 +000091 alignConsecutiveAssignments();
Manuel Klimek4fe43002013-05-22 12:51:29 +000092 alignTrailingComments();
93 alignEscapedNewlines();
94 generateChanges();
95
96 return Replaces;
97}
98
99void WhitespaceManager::calculateLineBreakInformation() {
100 Changes[0].PreviousEndOfTokenColumn = 0;
Benjamin Kramerdab50462016-01-11 16:27:16 +0000101 Change *LastOutsideTokenChange = &Changes[0];
Manuel Klimek4fe43002013-05-22 12:51:29 +0000102 for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
Krasimir Georgieve1518822017-06-07 14:05:06 +0000103 SourceLocation OriginalWhitespaceStart =
104 Changes[i].OriginalWhitespaceRange.getBegin();
105 SourceLocation PreviousOriginalWhitespaceEnd =
106 Changes[i - 1].OriginalWhitespaceRange.getEnd();
107 unsigned OriginalWhitespaceStartOffset =
108 SourceMgr.getFileOffset(OriginalWhitespaceStart);
109 unsigned PreviousOriginalWhitespaceEndOffset =
110 SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
111 assert(PreviousOriginalWhitespaceEndOffset <=
112 OriginalWhitespaceStartOffset);
113 const char *const PreviousOriginalWhitespaceEndData =
114 SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
115 StringRef Text(PreviousOriginalWhitespaceEndData,
116 SourceMgr.getCharacterData(OriginalWhitespaceStart) -
117 PreviousOriginalWhitespaceEndData);
118 // Usually consecutive changes would occur in consecutive tokens. This is
119 // not the case however when analyzing some preprocessor runs of the
120 // annotated lines. For example, in this code:
121 //
122 // #if A // line 1
123 // int i = 1;
124 // #else B // line 2
125 // int i = 2;
126 // #endif // line 3
127 //
128 // one of the runs will produce the sequence of lines marked with line 1, 2
129 // and 3. So the two consecutive whitespace changes just before '// line 2'
130 // and before '#endif // line 3' span multiple lines and tokens:
131 //
132 // #else B{change X}[// line 2
133 // int i = 2;
134 // ]{change Y}#endif // line 3
135 //
136 // For this reason, if the text between consecutive changes spans multiple
137 // newlines, the token length must be adjusted to the end of the original
138 // line of the token.
139 auto NewlinePos = Text.find_first_of('\n');
140 if (NewlinePos == StringRef::npos) {
141 Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
142 PreviousOriginalWhitespaceEndOffset +
143 Changes[i].PreviousLinePostfix.size() +
144 Changes[i - 1].CurrentLinePrefix.size();
145 } else {
146 Changes[i - 1].TokenLength =
147 NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
148 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000149
Benjamin Kramerdab50462016-01-11 16:27:16 +0000150 // If there are multiple changes in this token, sum up all the changes until
151 // the end of the line.
Krasimir Georgiev59ed77b2017-06-04 19:27:02 +0000152 if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0)
Benjamin Kramerdab50462016-01-11 16:27:16 +0000153 LastOutsideTokenChange->TokenLength +=
154 Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
155 else
156 LastOutsideTokenChange = &Changes[i - 1];
157
Manuel Klimek4fe43002013-05-22 12:51:29 +0000158 Changes[i].PreviousEndOfTokenColumn =
159 Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
160
161 Changes[i - 1].IsTrailingComment =
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000162 (Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
163 (Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
164 Changes[i - 1].Tok->is(tok::comment) &&
Krasimir Georgiev91834222017-01-25 13:58:58 +0000165 // FIXME: This is a dirty hack. The problem is that
166 // BreakableLineCommentSection does comment reflow changes and here is
167 // the aligning of trailing comments. Consider the case where we reflow
168 // the second line up in this example:
169 //
170 // // line 1
171 // // line 2
172 //
173 // That amounts to 2 changes by BreakableLineCommentSection:
174 // - the first, delimited by (), for the whitespace between the tokens,
175 // - and second, delimited by [], for the whitespace at the beginning
176 // of the second token:
177 //
178 // // line 1(
179 // )[// ]line 2
180 //
181 // So in the end we have two changes like this:
182 //
183 // // line1()[ ]line 2
184 //
185 // Note that the OriginalWhitespaceStart of the second change is the
186 // same as the PreviousOriginalWhitespaceEnd of the first change.
187 // In this case, the below check ensures that the second change doesn't
188 // get treated as a trailing comment change here, since this might
189 // trigger additional whitespace to be wrongly inserted before "line 2"
190 // by the comment aligner here.
191 //
192 // For a proper solution we need a mechanism to say to WhitespaceManager
193 // that a particular change breaks the current sequence of trailing
194 // comments.
195 OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000196 }
Manuel Klimek05c67892013-05-22 14:01:08 +0000197 // FIXME: The last token is currently not always an eof token; in those
198 // cases, setting TokenLength of the last token to 0 is wrong.
199 Changes.back().TokenLength = 0;
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000200 Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000201
202 const WhitespaceManager::Change *LastBlockComment = nullptr;
203 for (auto &Change : Changes) {
Benjamin Kramerdab50462016-01-11 16:27:16 +0000204 // Reset the IsTrailingComment flag for changes inside of trailing comments
Krasimir Georgievd105b722017-02-03 10:18:25 +0000205 // so they don't get realigned later. Comment line breaks however still need
206 // to be aligned.
207 if (Change.IsInsideToken && Change.NewlinesBefore == 0)
Benjamin Kramerdab50462016-01-11 16:27:16 +0000208 Change.IsTrailingComment = false;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000209 Change.StartOfBlockComment = nullptr;
210 Change.IndentationOffset = 0;
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000211 if (Change.Tok->is(tok::comment)) {
212 if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken)
213 LastBlockComment = &Change;
214 else {
215 if ((Change.StartOfBlockComment = LastBlockComment))
216 Change.IndentationOffset =
217 Change.StartOfTokenColumn -
218 Change.StartOfBlockComment->StartOfTokenColumn;
219 }
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000220 } else {
221 LastBlockComment = nullptr;
222 }
223 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000224}
225
Daniel Jasperec90e512015-12-01 12:00:43 +0000226// Align a single sequence of tokens, see AlignTokens below.
227template <typename F>
228static void
229AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches,
230 SmallVector<WhitespaceManager::Change, 16> &Changes) {
231 bool FoundMatchOnLine = false;
232 int Shift = 0;
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000233
234 // ScopeStack keeps track of the current scope depth. It contains indices of
235 // the first token on each scope.
236 // We only run the "Matches" function on tokens from the outer-most scope.
237 // However, we do need to pay special attention to one class of tokens
238 // that are not in the outer-most scope, and that is function parameters
239 // which are split across multiple lines, as illustrated by this example:
240 // double a(int x);
241 // int b(int y,
242 // double z);
243 // In the above example, we need to take special care to ensure that
244 // 'double z' is indented along with it's owning function 'b'.
245 SmallVector<unsigned, 16> ScopeStack;
246
Daniel Jasperec90e512015-12-01 12:00:43 +0000247 for (unsigned i = Start; i != End; ++i) {
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000248 if (ScopeStack.size() != 0 &&
249 Changes[i].nestingAndIndentLevel() <
250 Changes[ScopeStack.back()].nestingAndIndentLevel())
251 ScopeStack.pop_back();
252
253 if (i != Start && Changes[i].nestingAndIndentLevel() >
254 Changes[i - 1].nestingAndIndentLevel())
255 ScopeStack.push_back(i);
256
257 bool InsideNestedScope = ScopeStack.size() != 0;
258
259 if (Changes[i].NewlinesBefore > 0 && !InsideNestedScope) {
Daniel Jasperec90e512015-12-01 12:00:43 +0000260 Shift = 0;
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000261 FoundMatchOnLine = false;
Daniel Jasperec90e512015-12-01 12:00:43 +0000262 }
263
264 // If this is the first matching token to be aligned, remember by how many
265 // spaces it has to be shifted, so the rest of the changes on the line are
266 // shifted by the same amount
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000267 if (!FoundMatchOnLine && !InsideNestedScope && Matches(Changes[i])) {
Daniel Jasperec90e512015-12-01 12:00:43 +0000268 FoundMatchOnLine = true;
269 Shift = Column - Changes[i].StartOfTokenColumn;
270 Changes[i].Spaces += Shift;
271 }
272
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000273 // This is for function parameters that are split across multiple lines,
274 // as mentioned in the ScopeStack comment.
275 if (InsideNestedScope && Changes[i].NewlinesBefore > 0) {
276 unsigned ScopeStart = ScopeStack.back();
277 if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) ||
278 (ScopeStart > Start + 1 &&
279 Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)))
280 Changes[i].Spaces += Shift;
281 }
282
Daniel Jasperec90e512015-12-01 12:00:43 +0000283 assert(Shift >= 0);
284 Changes[i].StartOfTokenColumn += Shift;
285 if (i + 1 != Changes.size())
286 Changes[i + 1].PreviousEndOfTokenColumn += Shift;
287 }
288}
289
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000290// Walk through a subset of the changes, starting at StartAt, and find
291// sequences of matching tokens to align. To do so, keep track of the lines and
292// whether or not a matching token was found on a line. If a matching token is
293// found, extend the current sequence. If the current line cannot be part of a
294// sequence, e.g. because there is an empty line before it or it contains only
295// non-matching tokens, finalize the previous sequence.
296// The value returned is the token on which we stopped, either because we
297// exhausted all items inside Changes, or because we hit a scope level higher
298// than our initial scope.
299// This function is recursive. Each invocation processes only the scope level
300// equal to the initial level, which is the level of Changes[StartAt].
301// If we encounter a scope level greater than the initial level, then we call
302// ourselves recursively, thereby avoiding the pollution of the current state
303// with the alignment requirements of the nested sub-level. This recursive
304// behavior is necessary for aligning function prototypes that have one or more
305// arguments.
306// If this function encounters a scope level less than the initial level,
307// it returns the current position.
308// There is a non-obvious subtlety in the recursive behavior: Even though we
309// defer processing of nested levels to recursive invocations of this
310// function, when it comes time to align a sequence of tokens, we run the
311// alignment on the entire sequence, including the nested levels.
312// When doing so, most of the nested tokens are skipped, because their
313// alignment was already handled by the recursive invocations of this function.
314// However, the special exception is that we do NOT skip function parameters
315// that are split across multiple lines. See the test case in FormatTest.cpp
316// that mentions "split function parameter alignment" for an example of this.
Daniel Jasperec90e512015-12-01 12:00:43 +0000317template <typename F>
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000318static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
319 SmallVector<WhitespaceManager::Change, 16> &Changes,
320 unsigned StartAt) {
Daniel Jasperec90e512015-12-01 12:00:43 +0000321 unsigned MinColumn = 0;
322 unsigned MaxColumn = UINT_MAX;
323
324 // Line number of the start and the end of the current token sequence.
325 unsigned StartOfSequence = 0;
326 unsigned EndOfSequence = 0;
327
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000328 // Measure the scope level (i.e. depth of (), [], {}) of the first token, and
329 // abort when we hit any token in a higher scope than the starting one.
330 auto NestingAndIndentLevel = StartAt < Changes.size()
331 ? Changes[StartAt].nestingAndIndentLevel()
332 : std::pair<unsigned, unsigned>(0, 0);
Daniel Jasperec90e512015-12-01 12:00:43 +0000333
334 // Keep track of the number of commas before the matching tokens, we will only
335 // align a sequence of matching tokens if they are preceded by the same number
336 // of commas.
337 unsigned CommasBeforeLastMatch = 0;
338 unsigned CommasBeforeMatch = 0;
339
340 // Whether a matching token has been found on the current line.
341 bool FoundMatchOnLine = false;
342
343 // Aligns a sequence of matching tokens, on the MinColumn column.
344 //
345 // Sequences start from the first matching token to align, and end at the
346 // first token of the first line that doesn't need to be aligned.
347 //
348 // We need to adjust the StartOfTokenColumn of each Change that is on a line
349 // containing any matching token to be aligned and located after such token.
350 auto AlignCurrentSequence = [&] {
351 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
352 AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
353 Changes);
354 MinColumn = 0;
355 MaxColumn = UINT_MAX;
356 StartOfSequence = 0;
357 EndOfSequence = 0;
358 };
359
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000360 unsigned i = StartAt;
361 for (unsigned e = Changes.size(); i != e; ++i) {
362 if (Changes[i].nestingAndIndentLevel() < NestingAndIndentLevel)
363 break;
364
Daniel Jasperec90e512015-12-01 12:00:43 +0000365 if (Changes[i].NewlinesBefore != 0) {
366 CommasBeforeMatch = 0;
367 EndOfSequence = i;
368 // If there is a blank line, or if the last line didn't contain any
369 // matching token, the sequence ends here.
370 if (Changes[i].NewlinesBefore > 1 || !FoundMatchOnLine)
371 AlignCurrentSequence();
372
373 FoundMatchOnLine = false;
374 }
375
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000376 if (Changes[i].Tok->is(tok::comma)) {
Daniel Jasperec90e512015-12-01 12:00:43 +0000377 ++CommasBeforeMatch;
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000378 } else if (Changes[i].nestingAndIndentLevel() > NestingAndIndentLevel) {
379 // Call AlignTokens recursively, skipping over this scope block.
380 unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i);
381 i = StoppedAt - 1;
382 continue;
Daniel Jasperec90e512015-12-01 12:00:43 +0000383 }
384
385 if (!Matches(Changes[i]))
386 continue;
387
388 // If there is more than one matching token per line, or if the number of
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000389 // preceding commas, do not match anymore, end the sequence.
390 if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
Daniel Jasperec90e512015-12-01 12:00:43 +0000391 AlignCurrentSequence();
392
393 CommasBeforeLastMatch = CommasBeforeMatch;
Daniel Jasperec90e512015-12-01 12:00:43 +0000394 FoundMatchOnLine = true;
395
396 if (StartOfSequence == 0)
397 StartOfSequence = i;
398
399 unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
400 int LineLengthAfter = -Changes[i].Spaces;
401 for (unsigned j = i; j != e && Changes[j].NewlinesBefore == 0; ++j)
402 LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
403 unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
404
405 // If we are restricted by the maximum column width, end the sequence.
406 if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
407 CommasBeforeLastMatch != CommasBeforeMatch) {
408 AlignCurrentSequence();
409 StartOfSequence = i;
410 }
411
412 MinColumn = std::max(MinColumn, ChangeMinColumn);
413 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
414 }
415
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000416 EndOfSequence = i;
Daniel Jasperec90e512015-12-01 12:00:43 +0000417 AlignCurrentSequence();
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000418 return i;
Daniel Jasperec90e512015-12-01 12:00:43 +0000419}
420
Daniel Jaspera44991332015-04-29 13:06:49 +0000421void WhitespaceManager::alignConsecutiveAssignments() {
422 if (!Style.AlignConsecutiveAssignments)
423 return;
424
Daniel Jasperec90e512015-12-01 12:00:43 +0000425 AlignTokens(Style,
426 [&](const Change &C) {
427 // Do not align on equal signs that are first on a line.
428 if (C.NewlinesBefore > 0)
429 return false;
Daniel Jaspera44991332015-04-29 13:06:49 +0000430
Daniel Jasperec90e512015-12-01 12:00:43 +0000431 // Do not align on equal signs that are last on a line.
432 if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
433 return false;
Daniel Jaspera44991332015-04-29 13:06:49 +0000434
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000435 return C.Tok->is(tok::equal);
Daniel Jasperec90e512015-12-01 12:00:43 +0000436 },
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000437 Changes, /*StartAt=*/0);
Daniel Jaspera44991332015-04-29 13:06:49 +0000438}
439
Daniel Jaspere12597c2015-10-01 10:06:54 +0000440void WhitespaceManager::alignConsecutiveDeclarations() {
441 if (!Style.AlignConsecutiveDeclarations)
442 return;
443
Daniel Jasperec90e512015-12-01 12:00:43 +0000444 // FIXME: Currently we don't handle properly the PointerAlignment: Right
445 // The * and & are not aligned and are left dangling. Something has to be done
446 // about it, but it raises the question of alignment of code like:
447 // const char* const* v1;
448 // float const* v2;
449 // SomeVeryLongType const& v3;
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000450 AlignTokens(Style,
451 [](Change const &C) {
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000452 // tok::kw_operator is necessary for aligning operator overload
453 // definitions.
454 return C.Tok->is(TT_StartOfName) ||
455 C.Tok->is(TT_FunctionDeclarationName) ||
456 C.Tok->is(tok::kw_operator);
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000457 },
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000458 Changes, /*StartAt=*/0);
Daniel Jaspere12597c2015-10-01 10:06:54 +0000459}
460
Manuel Klimek4fe43002013-05-22 12:51:29 +0000461void WhitespaceManager::alignTrailingComments() {
462 unsigned MinColumn = 0;
463 unsigned MaxColumn = UINT_MAX;
464 unsigned StartOfSequence = 0;
465 bool BreakBeforeNext = false;
466 unsigned Newlines = 0;
467 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000468 if (Changes[i].StartOfBlockComment)
469 continue;
470 Newlines += Changes[i].NewlinesBefore;
471 if (!Changes[i].IsTrailingComment)
472 continue;
473
Manuel Klimek4fe43002013-05-22 12:51:29 +0000474 unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
Krasimir Georgiev59ed77b2017-06-04 19:27:02 +0000475 unsigned ChangeMaxColumn = Style.ColumnLimit >= Changes[i].TokenLength
476 ? Style.ColumnLimit - Changes[i].TokenLength
477 : ChangeMinColumn;
Daniel Jasper417fc812016-01-09 15:56:53 +0000478
479 // If we don't create a replacement for this change, we have to consider
480 // it to be immovable.
481 if (!Changes[i].CreateReplacement)
482 ChangeMaxColumn = ChangeMinColumn;
483
Daniel Jasper66935022014-04-27 10:03:19 +0000484 if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
485 ChangeMaxColumn -= 2;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000486 // If this comment follows an } in column 0, it probably documents the
487 // closing of a namespace and we don't want to align it.
488 bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000489 Changes[i - 1].Tok->is(tok::r_brace) &&
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000490 Changes[i - 1].StartOfTokenColumn == 0;
491 bool WasAlignedWithStartOfNextLine = false;
492 if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
Daniel Jasper49532102015-01-07 14:00:11 +0000493 unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
494 Changes[i].OriginalWhitespaceRange.getEnd());
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000495 for (unsigned j = i + 1; j != e; ++j) {
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000496 if (Changes[j].Tok->is(tok::comment))
Daniel Jasperbb37a2f2016-02-01 11:20:55 +0000497 continue;
498
499 unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
500 Changes[j].OriginalWhitespaceRange.getEnd());
501 // The start of the next token was previously aligned with the
502 // start of this comment.
503 WasAlignedWithStartOfNextLine =
504 CommentColumn == NextColumn ||
505 CommentColumn == NextColumn + Style.IndentWidth;
506 break;
Daniel Jasper0e93cdb2013-11-08 23:31:14 +0000507 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000508 }
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000509 if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
510 alignTrailingComments(StartOfSequence, i, MinColumn);
511 MinColumn = ChangeMinColumn;
512 MaxColumn = ChangeMinColumn;
513 StartOfSequence = i;
514 } else if (BreakBeforeNext || Newlines > 1 ||
515 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
516 // Break the comment sequence if the previous line did not end
517 // in a trailing comment.
518 (Changes[i].NewlinesBefore == 1 && i > 0 &&
519 !Changes[i - 1].IsTrailingComment) ||
520 WasAlignedWithStartOfNextLine) {
521 alignTrailingComments(StartOfSequence, i, MinColumn);
522 MinColumn = ChangeMinColumn;
523 MaxColumn = ChangeMaxColumn;
524 StartOfSequence = i;
525 } else {
526 MinColumn = std::max(MinColumn, ChangeMinColumn);
527 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
528 }
529 BreakBeforeNext =
530 (i == 0) || (Changes[i].NewlinesBefore > 1) ||
531 // Never start a sequence with a comment at the beginning of
532 // the line.
533 (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
534 Newlines = 0;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000535 }
536 alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
537}
538
539void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
540 unsigned Column) {
541 for (unsigned i = Start; i != End; ++i) {
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000542 int Shift = 0;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000543 if (Changes[i].IsTrailingComment) {
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000544 Shift = Column - Changes[i].StartOfTokenColumn;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000545 }
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000546 if (Changes[i].StartOfBlockComment) {
547 Shift = Changes[i].IndentationOffset +
548 Changes[i].StartOfBlockComment->StartOfTokenColumn -
549 Changes[i].StartOfTokenColumn;
550 }
551 assert(Shift >= 0);
552 Changes[i].Spaces += Shift;
Andi-Bogdan Postelnicua9a8fde2016-10-26 07:44:51 +0000553 if (i + 1 != Changes.size())
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000554 Changes[i + 1].PreviousEndOfTokenColumn += Shift;
555 Changes[i].StartOfTokenColumn += Shift;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000556 }
557}
558
559void WhitespaceManager::alignEscapedNewlines() {
Daniel Jasper7fdbb3f2017-05-08 15:08:00 +0000560 if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
561 return;
562
563 bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
564 unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000565 unsigned StartOfMacro = 0;
566 for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
567 Change &C = Changes[i];
568 if (C.NewlinesBefore > 0) {
569 if (C.ContinuesPPDirective) {
Daniel Jaspera49393f2013-08-28 09:07:32 +0000570 MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
Manuel Klimek4fe43002013-05-22 12:51:29 +0000571 } else {
572 alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
Daniel Jasper7fdbb3f2017-05-08 15:08:00 +0000573 MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000574 StartOfMacro = i;
575 }
576 }
577 }
578 alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
579}
580
581void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
582 unsigned Column) {
583 for (unsigned i = Start; i < End; ++i) {
584 Change &C = Changes[i];
585 if (C.NewlinesBefore > 0) {
586 assert(C.ContinuesPPDirective);
587 if (C.PreviousEndOfTokenColumn + 1 > Column)
588 C.EscapedNewlineColumn = 0;
589 else
590 C.EscapedNewlineColumn = Column;
591 }
592 }
593}
594
595void WhitespaceManager::generateChanges() {
596 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
597 const Change &C = Changes[i];
Daniel Jasper47b35ae2015-01-29 10:47:14 +0000598 if (i > 0) {
599 assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() !=
600 C.OriginalWhitespaceRange.getBegin() &&
601 "Generating two replacements for the same location");
602 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000603 if (C.CreateReplacement) {
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000604 std::string ReplacementText = C.PreviousLinePostfix;
605 if (C.ContinuesPPDirective)
606 appendNewlineText(ReplacementText, C.NewlinesBefore,
607 C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn);
608 else
609 appendNewlineText(ReplacementText, C.NewlinesBefore);
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000610 appendIndentText(ReplacementText, C.Tok->IndentLevel,
611 std::max(0, C.Spaces),
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000612 C.StartOfTokenColumn - std::max(0, C.Spaces));
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000613 ReplacementText.append(C.CurrentLinePrefix);
Manuel Klimek4fe43002013-05-22 12:51:29 +0000614 storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
615 }
616 }
617}
618
Craig Toppere335f252015-10-04 04:53:55 +0000619void WhitespaceManager::storeReplacement(SourceRange Range,
Manuel Klimek4fe43002013-05-22 12:51:29 +0000620 StringRef Text) {
621 unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
622 SourceMgr.getFileOffset(Range.getBegin());
623 // Don't create a replacement, if it does not change anything.
624 if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000625 WhitespaceLength) == Text)
Manuel Klimek4fe43002013-05-22 12:51:29 +0000626 return;
Eric Liu40ef2fb2016-08-01 10:16:37 +0000627 auto Err = Replaces.add(tooling::Replacement(
Manuel Klimek4fe43002013-05-22 12:51:29 +0000628 SourceMgr, CharSourceRange::getCharRange(Range), Text));
Eric Liu40ef2fb2016-08-01 10:16:37 +0000629 // FIXME: better error handling. For now, just print an error message in the
630 // release version.
Piotr Padlewski1ec383c2016-12-23 11:40:44 +0000631 if (Err) {
Eric Liu40ef2fb2016-08-01 10:16:37 +0000632 llvm::errs() << llvm::toString(std::move(Err)) << "\n";
Piotr Padlewski1ec383c2016-12-23 11:40:44 +0000633 assert(false);
634 }
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000635}
636
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000637void WhitespaceManager::appendNewlineText(std::string &Text,
638 unsigned Newlines) {
639 for (unsigned i = 0; i < Newlines; ++i)
640 Text.append(UseCRLF ? "\r\n" : "\n");
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000641}
642
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000643void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines,
644 unsigned PreviousEndOfTokenColumn,
645 unsigned EscapedNewlineColumn) {
Alexander Kornienko555efc32013-06-11 16:01:49 +0000646 if (Newlines > 0) {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000647 unsigned Offset =
Daniel Jasper7fdbb3f2017-05-08 15:08:00 +0000648 std::min<int>(EscapedNewlineColumn - 2, PreviousEndOfTokenColumn);
Alexander Kornienko555efc32013-06-11 16:01:49 +0000649 for (unsigned i = 0; i < Newlines; ++i) {
Benjamin Kramerddf1cda2015-05-28 19:55:49 +0000650 Text.append(EscapedNewlineColumn - Offset - 1, ' ');
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000651 Text.append(UseCRLF ? "\\\r\n" : "\\\n");
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000652 Offset = 0;
653 }
654 }
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000655}
656
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000657void WhitespaceManager::appendIndentText(std::string &Text,
658 unsigned IndentLevel, unsigned Spaces,
Alexander Kornienkodb4c21f2013-09-27 09:45:40 +0000659 unsigned WhitespaceStartColumn) {
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000660 switch (Style.UseTab) {
661 case FormatStyle::UT_Never:
Benjamin Kramerddf1cda2015-05-28 19:55:49 +0000662 Text.append(Spaces, ' ');
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000663 break;
664 case FormatStyle::UT_Always: {
Alexander Kornienkodb4c21f2013-09-27 09:45:40 +0000665 unsigned FirstTabWidth =
666 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
667 // Indent with tabs only when there's at least one full tab.
668 if (FirstTabWidth + Style.TabWidth <= Spaces) {
669 Spaces -= FirstTabWidth;
670 Text.append("\t");
671 }
Benjamin Kramerddf1cda2015-05-28 19:55:49 +0000672 Text.append(Spaces / Style.TabWidth, '\t');
673 Text.append(Spaces % Style.TabWidth, ' ');
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000674 break;
675 }
676 case FormatStyle::UT_ForIndentation:
677 if (WhitespaceStartColumn == 0) {
678 unsigned Indentation = IndentLevel * Style.IndentWidth;
Alexander Kornienko45dc1b22013-09-27 16:40:11 +0000679 // This happens, e.g. when a line in a block comment is indented less than
680 // the first one.
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000681 if (Indentation > Spaces)
682 Indentation = Spaces;
683 unsigned Tabs = Indentation / Style.TabWidth;
Benjamin Kramerddf1cda2015-05-28 19:55:49 +0000684 Text.append(Tabs, '\t');
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000685 Spaces -= Tabs * Style.TabWidth;
686 }
Benjamin Kramerddf1cda2015-05-28 19:55:49 +0000687 Text.append(Spaces, ' ');
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000688 break;
Marianne Mailhot-Sarrasin51fe2792016-04-14 14:52:26 +0000689 case FormatStyle::UT_ForContinuationAndIndentation:
690 if (WhitespaceStartColumn == 0) {
691 unsigned Tabs = Spaces / Style.TabWidth;
692 Text.append(Tabs, '\t');
693 Spaces -= Tabs * Style.TabWidth;
694 }
695 Text.append(Spaces, ' ');
696 break;
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000697 }
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000698}
699
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000700} // namespace format
701} // namespace clang