blob: a2f599142df87109caee4d9c83c81ad970f2658b [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
Manuel Klimek4fe43002013-05-22 12:51:29 +000028WhitespaceManager::Change::Change(
29 bool CreateReplacement, const SourceRange &OriginalWhitespaceRange,
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +000030 unsigned IndentLevel, int Spaces, unsigned StartOfTokenColumn,
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +000031 unsigned NewlinesBefore, StringRef PreviousLinePostfix,
32 StringRef CurrentLinePrefix, tok::TokenKind Kind, bool ContinuesPPDirective)
Manuel Klimek4fe43002013-05-22 12:51:29 +000033 : CreateReplacement(CreateReplacement),
34 OriginalWhitespaceRange(OriginalWhitespaceRange),
35 StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
36 PreviousLinePostfix(PreviousLinePostfix),
37 CurrentLinePrefix(CurrentLinePrefix), Kind(Kind),
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +000038 ContinuesPPDirective(ContinuesPPDirective), IndentLevel(IndentLevel),
39 Spaces(Spaces) {}
Manuel Klimek4fe43002013-05-22 12:51:29 +000040
Manuel Klimek71814b42013-10-11 21:25:45 +000041void WhitespaceManager::reset() {
42 Changes.clear();
43 Replaces.clear();
44}
45
46void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +000047 unsigned IndentLevel, 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;
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +000053 Changes.push_back(Change(true, Tok.WhitespaceRange, IndentLevel, Spaces,
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +000054 StartOfTokenColumn, Newlines, "", "",
55 Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst));
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;
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +000062 Changes.push_back(Change(false, Tok.WhitespaceRange, /*IndentLevel=*/0,
63 /*Spaces=*/0, Tok.OriginalColumn, Tok.NewlinesBefore,
64 "", "", Tok.Tok.getKind(),
65 InPPDirective && !Tok.IsFirst));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000066}
67
Alexander Kornienko555efc32013-06-11 16:01:49 +000068void WhitespaceManager::replaceWhitespaceInToken(
69 const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
70 StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +000071 unsigned Newlines, unsigned IndentLevel, int Spaces) {
Manuel Klimek71814b42013-10-11 21:25:45 +000072 if (Tok.Finalized)
73 return;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +000074 SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
Manuel Klimek4fe43002013-05-22 12:51:29 +000075 Changes.push_back(Change(
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +000076 true, SourceRange(Start, Start.getLocWithOffset(ReplaceChars)),
77 IndentLevel, Spaces, std::max(0, Spaces), Newlines, PreviousPostfix,
78 CurrentPrefix,
Alexander Kornienko4d26b6e2013-06-17 12:59:44 +000079 // If we don't add a newline this change doesn't start a comment. Thus,
80 // when we align line comments, we don't need to treat this change as one.
81 // FIXME: We still need to take this change in account to properly
82 // calculate the new length of the comment and to calculate the changes
83 // for which to do the alignment when aligning comments.
Daniel Jaspera98b7b02014-11-25 10:05:17 +000084 Tok.is(TT_LineComment) && Newlines > 0 ? tok::comment : tok::unknown,
Alexander Kornienko4d26b6e2013-06-17 12:59:44 +000085 InPPDirective && !Tok.IsFirst));
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
92 std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr));
93 calculateLineBreakInformation();
94 alignTrailingComments();
95 alignEscapedNewlines();
96 generateChanges();
97
98 return Replaces;
99}
100
101void WhitespaceManager::calculateLineBreakInformation() {
102 Changes[0].PreviousEndOfTokenColumn = 0;
103 for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
104 unsigned OriginalWhitespaceStart =
105 SourceMgr.getFileOffset(Changes[i].OriginalWhitespaceRange.getBegin());
106 unsigned PreviousOriginalWhitespaceEnd = SourceMgr.getFileOffset(
107 Changes[i - 1].OriginalWhitespaceRange.getEnd());
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000108 Changes[i - 1].TokenLength = OriginalWhitespaceStart -
109 PreviousOriginalWhitespaceEnd +
110 Changes[i].PreviousLinePostfix.size() +
111 Changes[i - 1].CurrentLinePrefix.size();
Manuel Klimek4fe43002013-05-22 12:51:29 +0000112
113 Changes[i].PreviousEndOfTokenColumn =
114 Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
115
116 Changes[i - 1].IsTrailingComment =
117 (Changes[i].NewlinesBefore > 0 || Changes[i].Kind == tok::eof) &&
118 Changes[i - 1].Kind == tok::comment;
119 }
Manuel Klimek05c67892013-05-22 14:01:08 +0000120 // FIXME: The last token is currently not always an eof token; in those
121 // cases, setting TokenLength of the last token to 0 is wrong.
122 Changes.back().TokenLength = 0;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000123 Changes.back().IsTrailingComment = Changes.back().Kind == tok::comment;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000124
125 const WhitespaceManager::Change *LastBlockComment = nullptr;
126 for (auto &Change : Changes) {
127 Change.StartOfBlockComment = nullptr;
128 Change.IndentationOffset = 0;
129 if (Change.Kind == tok::comment) {
130 LastBlockComment = &Change;
131 } else if (Change.Kind == tok::unknown) {
132 if ((Change.StartOfBlockComment = LastBlockComment))
133 Change.IndentationOffset =
134 Change.StartOfTokenColumn -
135 Change.StartOfBlockComment->StartOfTokenColumn;
136 } else {
137 LastBlockComment = nullptr;
138 }
139 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000140}
141
142void WhitespaceManager::alignTrailingComments() {
143 unsigned MinColumn = 0;
144 unsigned MaxColumn = UINT_MAX;
145 unsigned StartOfSequence = 0;
146 bool BreakBeforeNext = false;
147 unsigned Newlines = 0;
148 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000149 if (Changes[i].StartOfBlockComment)
150 continue;
151 Newlines += Changes[i].NewlinesBefore;
152 if (!Changes[i].IsTrailingComment)
153 continue;
154
Manuel Klimek4fe43002013-05-22 12:51:29 +0000155 unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000156 unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
Daniel Jasper66935022014-04-27 10:03:19 +0000157 if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
158 ChangeMaxColumn -= 2;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000159 // If this comment follows an } in column 0, it probably documents the
160 // closing of a namespace and we don't want to align it.
161 bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
162 Changes[i - 1].Kind == tok::r_brace &&
163 Changes[i - 1].StartOfTokenColumn == 0;
164 bool WasAlignedWithStartOfNextLine = false;
165 if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
166 for (unsigned j = i + 1; j != e; ++j) {
167 if (Changes[j].Kind != tok::comment) { // Skip over comments.
168 // The start of the next token was previously aligned with the
169 // start of this comment.
170 WasAlignedWithStartOfNextLine =
171 (SourceMgr.getSpellingColumnNumber(
172 Changes[i].OriginalWhitespaceRange.getEnd()) ==
173 SourceMgr.getSpellingColumnNumber(
174 Changes[j].OriginalWhitespaceRange.getEnd()));
175 break;
Daniel Jasper0e93cdb2013-11-08 23:31:14 +0000176 }
177 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000178 }
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000179 if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
180 alignTrailingComments(StartOfSequence, i, MinColumn);
181 MinColumn = ChangeMinColumn;
182 MaxColumn = ChangeMinColumn;
183 StartOfSequence = i;
184 } else if (BreakBeforeNext || Newlines > 1 ||
185 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
186 // Break the comment sequence if the previous line did not end
187 // in a trailing comment.
188 (Changes[i].NewlinesBefore == 1 && i > 0 &&
189 !Changes[i - 1].IsTrailingComment) ||
190 WasAlignedWithStartOfNextLine) {
191 alignTrailingComments(StartOfSequence, i, MinColumn);
192 MinColumn = ChangeMinColumn;
193 MaxColumn = ChangeMaxColumn;
194 StartOfSequence = i;
195 } else {
196 MinColumn = std::max(MinColumn, ChangeMinColumn);
197 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
198 }
199 BreakBeforeNext =
200 (i == 0) || (Changes[i].NewlinesBefore > 1) ||
201 // Never start a sequence with a comment at the beginning of
202 // the line.
203 (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
204 Newlines = 0;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000205 }
206 alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
207}
208
209void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
210 unsigned Column) {
211 for (unsigned i = Start; i != End; ++i) {
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000212 int Shift = 0;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000213 if (Changes[i].IsTrailingComment) {
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000214 Shift = Column - Changes[i].StartOfTokenColumn;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000215 }
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000216 if (Changes[i].StartOfBlockComment) {
217 Shift = Changes[i].IndentationOffset +
218 Changes[i].StartOfBlockComment->StartOfTokenColumn -
219 Changes[i].StartOfTokenColumn;
220 }
221 assert(Shift >= 0);
222 Changes[i].Spaces += Shift;
223 if (i + 1 != End)
224 Changes[i + 1].PreviousEndOfTokenColumn += Shift;
225 Changes[i].StartOfTokenColumn += Shift;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000226 }
227}
228
229void WhitespaceManager::alignEscapedNewlines() {
Daniel Jaspera49393f2013-08-28 09:07:32 +0000230 unsigned MaxEndOfLine =
231 Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000232 unsigned StartOfMacro = 0;
233 for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
234 Change &C = Changes[i];
235 if (C.NewlinesBefore > 0) {
236 if (C.ContinuesPPDirective) {
Daniel Jaspera49393f2013-08-28 09:07:32 +0000237 MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
Manuel Klimek4fe43002013-05-22 12:51:29 +0000238 } else {
239 alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
Daniel Jaspera49393f2013-08-28 09:07:32 +0000240 MaxEndOfLine = Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000241 StartOfMacro = i;
242 }
243 }
244 }
245 alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
246}
247
248void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
249 unsigned Column) {
250 for (unsigned i = Start; i < End; ++i) {
251 Change &C = Changes[i];
252 if (C.NewlinesBefore > 0) {
253 assert(C.ContinuesPPDirective);
254 if (C.PreviousEndOfTokenColumn + 1 > Column)
255 C.EscapedNewlineColumn = 0;
256 else
257 C.EscapedNewlineColumn = Column;
258 }
259 }
260}
261
262void WhitespaceManager::generateChanges() {
263 for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
264 const Change &C = Changes[i];
265 if (C.CreateReplacement) {
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000266 std::string ReplacementText = C.PreviousLinePostfix;
267 if (C.ContinuesPPDirective)
268 appendNewlineText(ReplacementText, C.NewlinesBefore,
269 C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn);
270 else
271 appendNewlineText(ReplacementText, C.NewlinesBefore);
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000272 appendIndentText(ReplacementText, C.IndentLevel, std::max(0, C.Spaces),
273 C.StartOfTokenColumn - std::max(0, C.Spaces));
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000274 ReplacementText.append(C.CurrentLinePrefix);
Manuel Klimek4fe43002013-05-22 12:51:29 +0000275 storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
276 }
277 }
278}
279
280void WhitespaceManager::storeReplacement(const SourceRange &Range,
281 StringRef Text) {
282 unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
283 SourceMgr.getFileOffset(Range.getBegin());
284 // Don't create a replacement, if it does not change anything.
285 if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000286 WhitespaceLength) == Text)
Manuel Klimek4fe43002013-05-22 12:51:29 +0000287 return;
288 Replaces.insert(tooling::Replacement(
289 SourceMgr, CharSourceRange::getCharRange(Range), Text));
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000290}
291
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000292void WhitespaceManager::appendNewlineText(std::string &Text,
293 unsigned Newlines) {
294 for (unsigned i = 0; i < Newlines; ++i)
295 Text.append(UseCRLF ? "\r\n" : "\n");
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000296}
297
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000298void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines,
299 unsigned PreviousEndOfTokenColumn,
300 unsigned EscapedNewlineColumn) {
Alexander Kornienko555efc32013-06-11 16:01:49 +0000301 if (Newlines > 0) {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000302 unsigned Offset =
Manuel Klimek4fe43002013-05-22 12:51:29 +0000303 std::min<int>(EscapedNewlineColumn - 1, PreviousEndOfTokenColumn);
Alexander Kornienko555efc32013-06-11 16:01:49 +0000304 for (unsigned i = 0; i < Newlines; ++i) {
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000305 Text.append(std::string(EscapedNewlineColumn - Offset - 1, ' '));
306 Text.append(UseCRLF ? "\\\r\n" : "\\\n");
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000307 Offset = 0;
308 }
309 }
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000310}
311
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000312void WhitespaceManager::appendIndentText(std::string &Text,
313 unsigned IndentLevel, unsigned Spaces,
Alexander Kornienkodb4c21f2013-09-27 09:45:40 +0000314 unsigned WhitespaceStartColumn) {
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000315 switch (Style.UseTab) {
316 case FormatStyle::UT_Never:
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000317 Text.append(std::string(Spaces, ' '));
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000318 break;
319 case FormatStyle::UT_Always: {
Alexander Kornienkodb4c21f2013-09-27 09:45:40 +0000320 unsigned FirstTabWidth =
321 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
322 // Indent with tabs only when there's at least one full tab.
323 if (FirstTabWidth + Style.TabWidth <= Spaces) {
324 Spaces -= FirstTabWidth;
325 Text.append("\t");
326 }
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000327 Text.append(std::string(Spaces / Style.TabWidth, '\t'));
328 Text.append(std::string(Spaces % Style.TabWidth, ' '));
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000329 break;
330 }
331 case FormatStyle::UT_ForIndentation:
332 if (WhitespaceStartColumn == 0) {
333 unsigned Indentation = IndentLevel * Style.IndentWidth;
Alexander Kornienko45dc1b22013-09-27 16:40:11 +0000334 // This happens, e.g. when a line in a block comment is indented less than
335 // the first one.
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000336 if (Indentation > Spaces)
337 Indentation = Spaces;
338 unsigned Tabs = Indentation / Style.TabWidth;
339 Text.append(std::string(Tabs, '\t'));
340 Spaces -= Tabs * Style.TabWidth;
341 }
342 Text.append(std::string(Spaces, ' '));
343 break;
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000344 }
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000345}
346
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000347} // namespace format
348} // namespace clang