Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1 | //===--- 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 | |
| 18 | namespace clang { |
| 19 | namespace format { |
| 20 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 21 | bool WhitespaceManager::Change::IsBeforeInFile:: |
| 22 | operator()(const Change &C1, const Change &C2) const { |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 23 | return SourceMgr.isBeforeInTranslationUnit( |
| 24 | C1.OriginalWhitespaceRange.getBegin(), |
| 25 | C2.OriginalWhitespaceRange.getBegin()); |
| 26 | } |
Daniel Jasper | 6fe2f00 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 27 | |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 28 | WhitespaceManager::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 Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 37 | OriginalWhitespaceRange(OriginalWhitespaceRange), |
| 38 | StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore), |
| 39 | PreviousLinePostfix(PreviousLinePostfix), |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 40 | CurrentLinePrefix(CurrentLinePrefix), |
| 41 | ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces), |
| 42 | IsInsideToken(IsInsideToken), IsTrailingComment(false), TokenLength(0), |
| 43 | PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0), |
Manuel Klimek | 2d29340 | 2015-03-03 14:21:48 +0000 | [diff] [blame] | 44 | StartOfBlockComment(nullptr), IndentationOffset(0) {} |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 45 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 46 | void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines, |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 47 | unsigned Spaces, |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 48 | unsigned StartOfTokenColumn, |
| 49 | bool InPPDirective) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 50 | if (Tok.Finalized) |
| 51 | return; |
| 52 | Tok.Decision = (Newlines > 0) ? FD_Break : FD_Continue; |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 53 | Changes.push_back(Change(Tok, /*CreateReplacement=*/true, |
| 54 | Tok.WhitespaceRange, Spaces, StartOfTokenColumn, |
| 55 | Newlines, "", "", InPPDirective && !Tok.IsFirst, |
| 56 | /*IsInsideToken=*/false)); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 59 | void WhitespaceManager::addUntouchableToken(const FormatToken &Tok, |
| 60 | bool InPPDirective) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 61 | if (Tok.Finalized) |
| 62 | return; |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 63 | 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 Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 70 | void WhitespaceManager::replaceWhitespaceInToken( |
| 71 | const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars, |
| 72 | StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective, |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 73 | unsigned Newlines, int Spaces) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 74 | if (Tok.Finalized) |
| 75 | return; |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 76 | SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset); |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 77 | 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 Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 84 | const 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 Jasper | e12597c | 2015-10-01 10:06:54 +0000 | [diff] [blame] | 90 | alignConsecutiveDeclarations(); |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 91 | alignConsecutiveAssignments(); |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 92 | alignTrailingComments(); |
| 93 | alignEscapedNewlines(); |
| 94 | generateChanges(); |
| 95 | |
| 96 | return Replaces; |
| 97 | } |
| 98 | |
| 99 | void WhitespaceManager::calculateLineBreakInformation() { |
| 100 | Changes[0].PreviousEndOfTokenColumn = 0; |
Benjamin Kramer | dab5046 | 2016-01-11 16:27:16 +0000 | [diff] [blame] | 101 | Change *LastOutsideTokenChange = &Changes[0]; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 102 | for (unsigned i = 1, e = Changes.size(); i != e; ++i) { |
| 103 | unsigned OriginalWhitespaceStart = |
| 104 | SourceMgr.getFileOffset(Changes[i].OriginalWhitespaceRange.getBegin()); |
| 105 | unsigned PreviousOriginalWhitespaceEnd = SourceMgr.getFileOffset( |
| 106 | Changes[i - 1].OriginalWhitespaceRange.getEnd()); |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 107 | Changes[i - 1].TokenLength = OriginalWhitespaceStart - |
| 108 | PreviousOriginalWhitespaceEnd + |
| 109 | Changes[i].PreviousLinePostfix.size() + |
| 110 | Changes[i - 1].CurrentLinePrefix.size(); |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 111 | |
Benjamin Kramer | dab5046 | 2016-01-11 16:27:16 +0000 | [diff] [blame] | 112 | // If there are multiple changes in this token, sum up all the changes until |
| 113 | // the end of the line. |
| 114 | if (Changes[i - 1].IsInsideToken) |
| 115 | LastOutsideTokenChange->TokenLength += |
| 116 | Changes[i - 1].TokenLength + Changes[i - 1].Spaces; |
| 117 | else |
| 118 | LastOutsideTokenChange = &Changes[i - 1]; |
| 119 | |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 120 | Changes[i].PreviousEndOfTokenColumn = |
| 121 | Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength; |
| 122 | |
| 123 | Changes[i - 1].IsTrailingComment = |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 124 | (Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) || |
| 125 | (Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) && |
| 126 | Changes[i - 1].Tok->is(tok::comment) && |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 127 | // FIXME: This is a dirty hack. The problem is that |
| 128 | // BreakableLineCommentSection does comment reflow changes and here is |
| 129 | // the aligning of trailing comments. Consider the case where we reflow |
| 130 | // the second line up in this example: |
| 131 | // |
| 132 | // // line 1 |
| 133 | // // line 2 |
| 134 | // |
| 135 | // That amounts to 2 changes by BreakableLineCommentSection: |
| 136 | // - the first, delimited by (), for the whitespace between the tokens, |
| 137 | // - and second, delimited by [], for the whitespace at the beginning |
| 138 | // of the second token: |
| 139 | // |
| 140 | // // line 1( |
| 141 | // )[// ]line 2 |
| 142 | // |
| 143 | // So in the end we have two changes like this: |
| 144 | // |
| 145 | // // line1()[ ]line 2 |
| 146 | // |
| 147 | // Note that the OriginalWhitespaceStart of the second change is the |
| 148 | // same as the PreviousOriginalWhitespaceEnd of the first change. |
| 149 | // In this case, the below check ensures that the second change doesn't |
| 150 | // get treated as a trailing comment change here, since this might |
| 151 | // trigger additional whitespace to be wrongly inserted before "line 2" |
| 152 | // by the comment aligner here. |
| 153 | // |
| 154 | // For a proper solution we need a mechanism to say to WhitespaceManager |
| 155 | // that a particular change breaks the current sequence of trailing |
| 156 | // comments. |
| 157 | OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 158 | } |
Manuel Klimek | 05c6789 | 2013-05-22 14:01:08 +0000 | [diff] [blame] | 159 | // FIXME: The last token is currently not always an eof token; in those |
| 160 | // cases, setting TokenLength of the last token to 0 is wrong. |
| 161 | Changes.back().TokenLength = 0; |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 162 | Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment); |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 163 | |
| 164 | const WhitespaceManager::Change *LastBlockComment = nullptr; |
| 165 | for (auto &Change : Changes) { |
Benjamin Kramer | dab5046 | 2016-01-11 16:27:16 +0000 | [diff] [blame] | 166 | // Reset the IsTrailingComment flag for changes inside of trailing comments |
| 167 | // so they don't get realigned later. |
| 168 | if (Change.IsInsideToken) |
| 169 | Change.IsTrailingComment = false; |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 170 | Change.StartOfBlockComment = nullptr; |
| 171 | Change.IndentationOffset = 0; |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 172 | if (Change.Tok->is(tok::comment)) { |
| 173 | if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken) |
| 174 | LastBlockComment = &Change; |
| 175 | else { |
| 176 | if ((Change.StartOfBlockComment = LastBlockComment)) |
| 177 | Change.IndentationOffset = |
| 178 | Change.StartOfTokenColumn - |
| 179 | Change.StartOfBlockComment->StartOfTokenColumn; |
| 180 | } |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 181 | } else { |
| 182 | LastBlockComment = nullptr; |
| 183 | } |
| 184 | } |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 187 | // Align a single sequence of tokens, see AlignTokens below. |
| 188 | template <typename F> |
| 189 | static void |
| 190 | AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches, |
| 191 | SmallVector<WhitespaceManager::Change, 16> &Changes) { |
| 192 | bool FoundMatchOnLine = false; |
| 193 | int Shift = 0; |
| 194 | for (unsigned i = Start; i != End; ++i) { |
| 195 | if (Changes[i].NewlinesBefore > 0) { |
| 196 | FoundMatchOnLine = false; |
| 197 | Shift = 0; |
| 198 | } |
| 199 | |
| 200 | // If this is the first matching token to be aligned, remember by how many |
| 201 | // spaces it has to be shifted, so the rest of the changes on the line are |
| 202 | // shifted by the same amount |
| 203 | if (!FoundMatchOnLine && Matches(Changes[i])) { |
| 204 | FoundMatchOnLine = true; |
| 205 | Shift = Column - Changes[i].StartOfTokenColumn; |
| 206 | Changes[i].Spaces += Shift; |
| 207 | } |
| 208 | |
| 209 | assert(Shift >= 0); |
| 210 | Changes[i].StartOfTokenColumn += Shift; |
| 211 | if (i + 1 != Changes.size()) |
| 212 | Changes[i + 1].PreviousEndOfTokenColumn += Shift; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Walk through all of the changes and find sequences of matching tokens to |
| 217 | // align. To do so, keep track of the lines and whether or not a matching token |
| 218 | // was found on a line. If a matching token is found, extend the current |
| 219 | // sequence. If the current line cannot be part of a sequence, e.g. because |
| 220 | // there is an empty line before it or it contains only non-matching tokens, |
| 221 | // finalize the previous sequence. |
| 222 | template <typename F> |
| 223 | static void AlignTokens(const FormatStyle &Style, F &&Matches, |
| 224 | SmallVector<WhitespaceManager::Change, 16> &Changes) { |
| 225 | unsigned MinColumn = 0; |
| 226 | unsigned MaxColumn = UINT_MAX; |
| 227 | |
| 228 | // Line number of the start and the end of the current token sequence. |
| 229 | unsigned StartOfSequence = 0; |
| 230 | unsigned EndOfSequence = 0; |
| 231 | |
| 232 | // Keep track of the nesting level of matching tokens, i.e. the number of |
| 233 | // surrounding (), [], or {}. We will only align a sequence of matching |
| 234 | // token that share the same scope depth. |
| 235 | // |
| 236 | // FIXME: This could use FormatToken::NestingLevel information, but there is |
| 237 | // an outstanding issue wrt the brace scopes. |
| 238 | unsigned NestingLevelOfLastMatch = 0; |
| 239 | unsigned NestingLevel = 0; |
| 240 | |
| 241 | // Keep track of the number of commas before the matching tokens, we will only |
| 242 | // align a sequence of matching tokens if they are preceded by the same number |
| 243 | // of commas. |
| 244 | unsigned CommasBeforeLastMatch = 0; |
| 245 | unsigned CommasBeforeMatch = 0; |
| 246 | |
| 247 | // Whether a matching token has been found on the current line. |
| 248 | bool FoundMatchOnLine = false; |
| 249 | |
| 250 | // Aligns a sequence of matching tokens, on the MinColumn column. |
| 251 | // |
| 252 | // Sequences start from the first matching token to align, and end at the |
| 253 | // first token of the first line that doesn't need to be aligned. |
| 254 | // |
| 255 | // We need to adjust the StartOfTokenColumn of each Change that is on a line |
| 256 | // containing any matching token to be aligned and located after such token. |
| 257 | auto AlignCurrentSequence = [&] { |
| 258 | if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) |
| 259 | AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches, |
| 260 | Changes); |
| 261 | MinColumn = 0; |
| 262 | MaxColumn = UINT_MAX; |
| 263 | StartOfSequence = 0; |
| 264 | EndOfSequence = 0; |
| 265 | }; |
| 266 | |
| 267 | for (unsigned i = 0, e = Changes.size(); i != e; ++i) { |
| 268 | if (Changes[i].NewlinesBefore != 0) { |
| 269 | CommasBeforeMatch = 0; |
| 270 | EndOfSequence = i; |
| 271 | // If there is a blank line, or if the last line didn't contain any |
| 272 | // matching token, the sequence ends here. |
| 273 | if (Changes[i].NewlinesBefore > 1 || !FoundMatchOnLine) |
| 274 | AlignCurrentSequence(); |
| 275 | |
| 276 | FoundMatchOnLine = false; |
| 277 | } |
| 278 | |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 279 | if (Changes[i].Tok->is(tok::comma)) { |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 280 | ++CommasBeforeMatch; |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 281 | } else if (Changes[i].Tok->isOneOf(tok::r_brace, tok::r_paren, |
| 282 | tok::r_square)) { |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 283 | --NestingLevel; |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 284 | } else if (Changes[i].Tok->isOneOf(tok::l_brace, tok::l_paren, |
| 285 | tok::l_square)) { |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 286 | // We want sequences to skip over child scopes if possible, but not the |
| 287 | // other way around. |
| 288 | NestingLevelOfLastMatch = std::min(NestingLevelOfLastMatch, NestingLevel); |
| 289 | ++NestingLevel; |
| 290 | } |
| 291 | |
| 292 | if (!Matches(Changes[i])) |
| 293 | continue; |
| 294 | |
| 295 | // If there is more than one matching token per line, or if the number of |
| 296 | // preceding commas, or the scope depth, do not match anymore, end the |
| 297 | // sequence. |
| 298 | if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch || |
| 299 | NestingLevel != NestingLevelOfLastMatch) |
| 300 | AlignCurrentSequence(); |
| 301 | |
| 302 | CommasBeforeLastMatch = CommasBeforeMatch; |
| 303 | NestingLevelOfLastMatch = NestingLevel; |
| 304 | FoundMatchOnLine = true; |
| 305 | |
| 306 | if (StartOfSequence == 0) |
| 307 | StartOfSequence = i; |
| 308 | |
| 309 | unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn; |
| 310 | int LineLengthAfter = -Changes[i].Spaces; |
| 311 | for (unsigned j = i; j != e && Changes[j].NewlinesBefore == 0; ++j) |
| 312 | LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength; |
| 313 | unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter; |
| 314 | |
| 315 | // If we are restricted by the maximum column width, end the sequence. |
| 316 | if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn || |
| 317 | CommasBeforeLastMatch != CommasBeforeMatch) { |
| 318 | AlignCurrentSequence(); |
| 319 | StartOfSequence = i; |
| 320 | } |
| 321 | |
| 322 | MinColumn = std::max(MinColumn, ChangeMinColumn); |
| 323 | MaxColumn = std::min(MaxColumn, ChangeMaxColumn); |
| 324 | } |
| 325 | |
| 326 | EndOfSequence = Changes.size(); |
| 327 | AlignCurrentSequence(); |
| 328 | } |
| 329 | |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 330 | void WhitespaceManager::alignConsecutiveAssignments() { |
| 331 | if (!Style.AlignConsecutiveAssignments) |
| 332 | return; |
| 333 | |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 334 | AlignTokens(Style, |
| 335 | [&](const Change &C) { |
| 336 | // Do not align on equal signs that are first on a line. |
| 337 | if (C.NewlinesBefore > 0) |
| 338 | return false; |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 339 | |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 340 | // Do not align on equal signs that are last on a line. |
| 341 | if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0) |
| 342 | return false; |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 343 | |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 344 | return C.Tok->is(tok::equal); |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 345 | }, |
| 346 | Changes); |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Daniel Jasper | e12597c | 2015-10-01 10:06:54 +0000 | [diff] [blame] | 349 | void WhitespaceManager::alignConsecutiveDeclarations() { |
| 350 | if (!Style.AlignConsecutiveDeclarations) |
| 351 | return; |
| 352 | |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 353 | // FIXME: Currently we don't handle properly the PointerAlignment: Right |
| 354 | // The * and & are not aligned and are left dangling. Something has to be done |
| 355 | // about it, but it raises the question of alignment of code like: |
| 356 | // const char* const* v1; |
| 357 | // float const* v2; |
| 358 | // SomeVeryLongType const& v3; |
Daniel Jasper | e12597c | 2015-10-01 10:06:54 +0000 | [diff] [blame] | 359 | |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 360 | AlignTokens(Style, |
| 361 | [](Change const &C) { |
| 362 | return C.Tok->isOneOf(TT_StartOfName, |
| 363 | TT_FunctionDeclarationName); |
| 364 | }, |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 365 | Changes); |
Daniel Jasper | e12597c | 2015-10-01 10:06:54 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 368 | void WhitespaceManager::alignTrailingComments() { |
| 369 | unsigned MinColumn = 0; |
| 370 | unsigned MaxColumn = UINT_MAX; |
| 371 | unsigned StartOfSequence = 0; |
| 372 | bool BreakBeforeNext = false; |
| 373 | unsigned Newlines = 0; |
| 374 | for (unsigned i = 0, e = Changes.size(); i != e; ++i) { |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 375 | if (Changes[i].StartOfBlockComment) |
| 376 | continue; |
| 377 | Newlines += Changes[i].NewlinesBefore; |
| 378 | if (!Changes[i].IsTrailingComment) |
| 379 | continue; |
| 380 | |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 381 | unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 382 | unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength; |
Daniel Jasper | 417fc81 | 2016-01-09 15:56:53 +0000 | [diff] [blame] | 383 | |
| 384 | // If we don't create a replacement for this change, we have to consider |
| 385 | // it to be immovable. |
| 386 | if (!Changes[i].CreateReplacement) |
| 387 | ChangeMaxColumn = ChangeMinColumn; |
| 388 | |
Daniel Jasper | 6693502 | 2014-04-27 10:03:19 +0000 | [diff] [blame] | 389 | if (i + 1 != e && Changes[i + 1].ContinuesPPDirective) |
| 390 | ChangeMaxColumn -= 2; |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 391 | // If this comment follows an } in column 0, it probably documents the |
| 392 | // closing of a namespace and we don't want to align it. |
| 393 | bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 && |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 394 | Changes[i - 1].Tok->is(tok::r_brace) && |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 395 | Changes[i - 1].StartOfTokenColumn == 0; |
| 396 | bool WasAlignedWithStartOfNextLine = false; |
| 397 | if (Changes[i].NewlinesBefore == 1) { // A comment on its own line. |
Daniel Jasper | 4953210 | 2015-01-07 14:00:11 +0000 | [diff] [blame] | 398 | unsigned CommentColumn = SourceMgr.getSpellingColumnNumber( |
| 399 | Changes[i].OriginalWhitespaceRange.getEnd()); |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 400 | for (unsigned j = i + 1; j != e; ++j) { |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 401 | if (Changes[j].Tok->is(tok::comment)) |
Daniel Jasper | bb37a2f | 2016-02-01 11:20:55 +0000 | [diff] [blame] | 402 | continue; |
| 403 | |
| 404 | unsigned NextColumn = SourceMgr.getSpellingColumnNumber( |
| 405 | Changes[j].OriginalWhitespaceRange.getEnd()); |
| 406 | // The start of the next token was previously aligned with the |
| 407 | // start of this comment. |
| 408 | WasAlignedWithStartOfNextLine = |
| 409 | CommentColumn == NextColumn || |
| 410 | CommentColumn == NextColumn + Style.IndentWidth; |
| 411 | break; |
Daniel Jasper | 0e93cdb | 2013-11-08 23:31:14 +0000 | [diff] [blame] | 412 | } |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 413 | } |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 414 | if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) { |
| 415 | alignTrailingComments(StartOfSequence, i, MinColumn); |
| 416 | MinColumn = ChangeMinColumn; |
| 417 | MaxColumn = ChangeMinColumn; |
| 418 | StartOfSequence = i; |
| 419 | } else if (BreakBeforeNext || Newlines > 1 || |
| 420 | (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) || |
| 421 | // Break the comment sequence if the previous line did not end |
| 422 | // in a trailing comment. |
| 423 | (Changes[i].NewlinesBefore == 1 && i > 0 && |
| 424 | !Changes[i - 1].IsTrailingComment) || |
| 425 | WasAlignedWithStartOfNextLine) { |
| 426 | alignTrailingComments(StartOfSequence, i, MinColumn); |
| 427 | MinColumn = ChangeMinColumn; |
| 428 | MaxColumn = ChangeMaxColumn; |
| 429 | StartOfSequence = i; |
| 430 | } else { |
| 431 | MinColumn = std::max(MinColumn, ChangeMinColumn); |
| 432 | MaxColumn = std::min(MaxColumn, ChangeMaxColumn); |
| 433 | } |
| 434 | BreakBeforeNext = |
| 435 | (i == 0) || (Changes[i].NewlinesBefore > 1) || |
| 436 | // Never start a sequence with a comment at the beginning of |
| 437 | // the line. |
| 438 | (Changes[i].NewlinesBefore == 1 && StartOfSequence == i); |
| 439 | Newlines = 0; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 440 | } |
| 441 | alignTrailingComments(StartOfSequence, Changes.size(), MinColumn); |
| 442 | } |
| 443 | |
| 444 | void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End, |
| 445 | unsigned Column) { |
| 446 | for (unsigned i = Start; i != End; ++i) { |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 447 | int Shift = 0; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 448 | if (Changes[i].IsTrailingComment) { |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 449 | Shift = Column - Changes[i].StartOfTokenColumn; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 450 | } |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 451 | if (Changes[i].StartOfBlockComment) { |
| 452 | Shift = Changes[i].IndentationOffset + |
| 453 | Changes[i].StartOfBlockComment->StartOfTokenColumn - |
| 454 | Changes[i].StartOfTokenColumn; |
| 455 | } |
| 456 | assert(Shift >= 0); |
| 457 | Changes[i].Spaces += Shift; |
Andi-Bogdan Postelnicu | a9a8fde | 2016-10-26 07:44:51 +0000 | [diff] [blame] | 458 | if (i + 1 != Changes.size()) |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 459 | Changes[i + 1].PreviousEndOfTokenColumn += Shift; |
| 460 | Changes[i].StartOfTokenColumn += Shift; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | void WhitespaceManager::alignEscapedNewlines() { |
Daniel Jasper | a49393f | 2013-08-28 09:07:32 +0000 | [diff] [blame] | 465 | unsigned MaxEndOfLine = |
| 466 | Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 467 | unsigned StartOfMacro = 0; |
| 468 | for (unsigned i = 1, e = Changes.size(); i < e; ++i) { |
| 469 | Change &C = Changes[i]; |
| 470 | if (C.NewlinesBefore > 0) { |
| 471 | if (C.ContinuesPPDirective) { |
Daniel Jasper | a49393f | 2013-08-28 09:07:32 +0000 | [diff] [blame] | 472 | MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine); |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 473 | } else { |
| 474 | alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine); |
Daniel Jasper | a49393f | 2013-08-28 09:07:32 +0000 | [diff] [blame] | 475 | MaxEndOfLine = Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 476 | StartOfMacro = i; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine); |
| 481 | } |
| 482 | |
| 483 | void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End, |
| 484 | unsigned Column) { |
| 485 | for (unsigned i = Start; i < End; ++i) { |
| 486 | Change &C = Changes[i]; |
| 487 | if (C.NewlinesBefore > 0) { |
| 488 | assert(C.ContinuesPPDirective); |
| 489 | if (C.PreviousEndOfTokenColumn + 1 > Column) |
| 490 | C.EscapedNewlineColumn = 0; |
| 491 | else |
| 492 | C.EscapedNewlineColumn = Column; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | void WhitespaceManager::generateChanges() { |
| 498 | for (unsigned i = 0, e = Changes.size(); i != e; ++i) { |
| 499 | const Change &C = Changes[i]; |
Daniel Jasper | 47b35ae | 2015-01-29 10:47:14 +0000 | [diff] [blame] | 500 | if (i > 0) { |
| 501 | assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() != |
| 502 | C.OriginalWhitespaceRange.getBegin() && |
| 503 | "Generating two replacements for the same location"); |
| 504 | } |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 505 | if (C.CreateReplacement) { |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 506 | std::string ReplacementText = C.PreviousLinePostfix; |
| 507 | if (C.ContinuesPPDirective) |
| 508 | appendNewlineText(ReplacementText, C.NewlinesBefore, |
| 509 | C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn); |
| 510 | else |
| 511 | appendNewlineText(ReplacementText, C.NewlinesBefore); |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame^] | 512 | appendIndentText(ReplacementText, C.Tok->IndentLevel, |
| 513 | std::max(0, C.Spaces), |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 514 | C.StartOfTokenColumn - std::max(0, C.Spaces)); |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 515 | ReplacementText.append(C.CurrentLinePrefix); |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 516 | storeReplacement(C.OriginalWhitespaceRange, ReplacementText); |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
Craig Topper | e335f25 | 2015-10-04 04:53:55 +0000 | [diff] [blame] | 521 | void WhitespaceManager::storeReplacement(SourceRange Range, |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 522 | StringRef Text) { |
| 523 | unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) - |
| 524 | SourceMgr.getFileOffset(Range.getBegin()); |
| 525 | // Don't create a replacement, if it does not change anything. |
| 526 | if (StringRef(SourceMgr.getCharacterData(Range.getBegin()), |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 527 | WhitespaceLength) == Text) |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 528 | return; |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 529 | auto Err = Replaces.add(tooling::Replacement( |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 530 | SourceMgr, CharSourceRange::getCharRange(Range), Text)); |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 531 | // FIXME: better error handling. For now, just print an error message in the |
| 532 | // release version. |
Piotr Padlewski | 1ec383c | 2016-12-23 11:40:44 +0000 | [diff] [blame] | 533 | if (Err) { |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 534 | llvm::errs() << llvm::toString(std::move(Err)) << "\n"; |
Piotr Padlewski | 1ec383c | 2016-12-23 11:40:44 +0000 | [diff] [blame] | 535 | assert(false); |
| 536 | } |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 539 | void WhitespaceManager::appendNewlineText(std::string &Text, |
| 540 | unsigned Newlines) { |
| 541 | for (unsigned i = 0; i < Newlines; ++i) |
| 542 | Text.append(UseCRLF ? "\r\n" : "\n"); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 545 | void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines, |
| 546 | unsigned PreviousEndOfTokenColumn, |
| 547 | unsigned EscapedNewlineColumn) { |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 548 | if (Newlines > 0) { |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 549 | unsigned Offset = |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 550 | std::min<int>(EscapedNewlineColumn - 1, PreviousEndOfTokenColumn); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 551 | for (unsigned i = 0; i < Newlines; ++i) { |
Benjamin Kramer | ddf1cda | 2015-05-28 19:55:49 +0000 | [diff] [blame] | 552 | Text.append(EscapedNewlineColumn - Offset - 1, ' '); |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 553 | Text.append(UseCRLF ? "\\\r\n" : "\\\n"); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 554 | Offset = 0; |
| 555 | } |
| 556 | } |
Manuel Klimek | b9eae4c | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 559 | void WhitespaceManager::appendIndentText(std::string &Text, |
| 560 | unsigned IndentLevel, unsigned Spaces, |
Alexander Kornienko | db4c21f | 2013-09-27 09:45:40 +0000 | [diff] [blame] | 561 | unsigned WhitespaceStartColumn) { |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 562 | switch (Style.UseTab) { |
| 563 | case FormatStyle::UT_Never: |
Benjamin Kramer | ddf1cda | 2015-05-28 19:55:49 +0000 | [diff] [blame] | 564 | Text.append(Spaces, ' '); |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 565 | break; |
| 566 | case FormatStyle::UT_Always: { |
Alexander Kornienko | db4c21f | 2013-09-27 09:45:40 +0000 | [diff] [blame] | 567 | unsigned FirstTabWidth = |
| 568 | Style.TabWidth - WhitespaceStartColumn % Style.TabWidth; |
| 569 | // Indent with tabs only when there's at least one full tab. |
| 570 | if (FirstTabWidth + Style.TabWidth <= Spaces) { |
| 571 | Spaces -= FirstTabWidth; |
| 572 | Text.append("\t"); |
| 573 | } |
Benjamin Kramer | ddf1cda | 2015-05-28 19:55:49 +0000 | [diff] [blame] | 574 | Text.append(Spaces / Style.TabWidth, '\t'); |
| 575 | Text.append(Spaces % Style.TabWidth, ' '); |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 576 | break; |
| 577 | } |
| 578 | case FormatStyle::UT_ForIndentation: |
| 579 | if (WhitespaceStartColumn == 0) { |
| 580 | unsigned Indentation = IndentLevel * Style.IndentWidth; |
Alexander Kornienko | 45dc1b2 | 2013-09-27 16:40:11 +0000 | [diff] [blame] | 581 | // This happens, e.g. when a line in a block comment is indented less than |
| 582 | // the first one. |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 583 | if (Indentation > Spaces) |
| 584 | Indentation = Spaces; |
| 585 | unsigned Tabs = Indentation / Style.TabWidth; |
Benjamin Kramer | ddf1cda | 2015-05-28 19:55:49 +0000 | [diff] [blame] | 586 | Text.append(Tabs, '\t'); |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 587 | Spaces -= Tabs * Style.TabWidth; |
| 588 | } |
Benjamin Kramer | ddf1cda | 2015-05-28 19:55:49 +0000 | [diff] [blame] | 589 | Text.append(Spaces, ' '); |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 590 | break; |
Marianne Mailhot-Sarrasin | 51fe279 | 2016-04-14 14:52:26 +0000 | [diff] [blame] | 591 | case FormatStyle::UT_ForContinuationAndIndentation: |
| 592 | if (WhitespaceStartColumn == 0) { |
| 593 | unsigned Tabs = Spaces / Style.TabWidth; |
| 594 | Text.append(Tabs, '\t'); |
| 595 | Spaces -= Tabs * Style.TabWidth; |
| 596 | } |
| 597 | Text.append(Spaces, ' '); |
| 598 | break; |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 599 | } |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 602 | } // namespace format |
| 603 | } // namespace clang |