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; |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 53 | Changes.push_back(Change(Tok, /*CreateReplacement=*/true, Tok.WhitespaceRange, |
| 54 | Spaces, StartOfTokenColumn, Newlines, "", "", |
| 55 | InPPDirective && !Tok.IsFirst, |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 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. |
Krasimir Georgiev | 59ed77b | 2017-06-04 19:27:02 +0000 | [diff] [blame^] | 114 | if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0) |
Benjamin Kramer | dab5046 | 2016-01-11 16:27:16 +0000 | [diff] [blame] | 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 |
Krasimir Georgiev | d105b72 | 2017-02-03 10:18:25 +0000 | [diff] [blame] | 167 | // so they don't get realigned later. Comment line breaks however still need |
| 168 | // to be aligned. |
| 169 | if (Change.IsInsideToken && Change.NewlinesBefore == 0) |
Benjamin Kramer | dab5046 | 2016-01-11 16:27:16 +0000 | [diff] [blame] | 170 | Change.IsTrailingComment = false; |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 171 | Change.StartOfBlockComment = nullptr; |
| 172 | Change.IndentationOffset = 0; |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 173 | if (Change.Tok->is(tok::comment)) { |
| 174 | if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken) |
| 175 | LastBlockComment = &Change; |
| 176 | else { |
| 177 | if ((Change.StartOfBlockComment = LastBlockComment)) |
| 178 | Change.IndentationOffset = |
| 179 | Change.StartOfTokenColumn - |
| 180 | Change.StartOfBlockComment->StartOfTokenColumn; |
| 181 | } |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 182 | } else { |
| 183 | LastBlockComment = nullptr; |
| 184 | } |
| 185 | } |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 188 | // Align a single sequence of tokens, see AlignTokens below. |
| 189 | template <typename F> |
| 190 | static void |
| 191 | AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches, |
| 192 | SmallVector<WhitespaceManager::Change, 16> &Changes) { |
| 193 | bool FoundMatchOnLine = false; |
| 194 | int Shift = 0; |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 195 | |
| 196 | // ScopeStack keeps track of the current scope depth. It contains indices of |
| 197 | // the first token on each scope. |
| 198 | // We only run the "Matches" function on tokens from the outer-most scope. |
| 199 | // However, we do need to pay special attention to one class of tokens |
| 200 | // that are not in the outer-most scope, and that is function parameters |
| 201 | // which are split across multiple lines, as illustrated by this example: |
| 202 | // double a(int x); |
| 203 | // int b(int y, |
| 204 | // double z); |
| 205 | // In the above example, we need to take special care to ensure that |
| 206 | // 'double z' is indented along with it's owning function 'b'. |
| 207 | SmallVector<unsigned, 16> ScopeStack; |
| 208 | |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 209 | for (unsigned i = Start; i != End; ++i) { |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 210 | if (ScopeStack.size() != 0 && |
| 211 | Changes[i].nestingAndIndentLevel() < |
| 212 | Changes[ScopeStack.back()].nestingAndIndentLevel()) |
| 213 | ScopeStack.pop_back(); |
| 214 | |
| 215 | if (i != Start && Changes[i].nestingAndIndentLevel() > |
| 216 | Changes[i - 1].nestingAndIndentLevel()) |
| 217 | ScopeStack.push_back(i); |
| 218 | |
| 219 | bool InsideNestedScope = ScopeStack.size() != 0; |
| 220 | |
| 221 | if (Changes[i].NewlinesBefore > 0 && !InsideNestedScope) { |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 222 | Shift = 0; |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 223 | FoundMatchOnLine = false; |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // If this is the first matching token to be aligned, remember by how many |
| 227 | // spaces it has to be shifted, so the rest of the changes on the line are |
| 228 | // shifted by the same amount |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 229 | if (!FoundMatchOnLine && !InsideNestedScope && Matches(Changes[i])) { |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 230 | FoundMatchOnLine = true; |
| 231 | Shift = Column - Changes[i].StartOfTokenColumn; |
| 232 | Changes[i].Spaces += Shift; |
| 233 | } |
| 234 | |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 235 | // This is for function parameters that are split across multiple lines, |
| 236 | // as mentioned in the ScopeStack comment. |
| 237 | if (InsideNestedScope && Changes[i].NewlinesBefore > 0) { |
| 238 | unsigned ScopeStart = ScopeStack.back(); |
| 239 | if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName) || |
| 240 | (ScopeStart > Start + 1 && |
| 241 | Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName))) |
| 242 | Changes[i].Spaces += Shift; |
| 243 | } |
| 244 | |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 245 | assert(Shift >= 0); |
| 246 | Changes[i].StartOfTokenColumn += Shift; |
| 247 | if (i + 1 != Changes.size()) |
| 248 | Changes[i + 1].PreviousEndOfTokenColumn += Shift; |
| 249 | } |
| 250 | } |
| 251 | |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 252 | // Walk through a subset of the changes, starting at StartAt, and find |
| 253 | // sequences of matching tokens to align. To do so, keep track of the lines and |
| 254 | // whether or not a matching token was found on a line. If a matching token is |
| 255 | // found, extend the current sequence. If the current line cannot be part of a |
| 256 | // sequence, e.g. because there is an empty line before it or it contains only |
| 257 | // non-matching tokens, finalize the previous sequence. |
| 258 | // The value returned is the token on which we stopped, either because we |
| 259 | // exhausted all items inside Changes, or because we hit a scope level higher |
| 260 | // than our initial scope. |
| 261 | // This function is recursive. Each invocation processes only the scope level |
| 262 | // equal to the initial level, which is the level of Changes[StartAt]. |
| 263 | // If we encounter a scope level greater than the initial level, then we call |
| 264 | // ourselves recursively, thereby avoiding the pollution of the current state |
| 265 | // with the alignment requirements of the nested sub-level. This recursive |
| 266 | // behavior is necessary for aligning function prototypes that have one or more |
| 267 | // arguments. |
| 268 | // If this function encounters a scope level less than the initial level, |
| 269 | // it returns the current position. |
| 270 | // There is a non-obvious subtlety in the recursive behavior: Even though we |
| 271 | // defer processing of nested levels to recursive invocations of this |
| 272 | // function, when it comes time to align a sequence of tokens, we run the |
| 273 | // alignment on the entire sequence, including the nested levels. |
| 274 | // When doing so, most of the nested tokens are skipped, because their |
| 275 | // alignment was already handled by the recursive invocations of this function. |
| 276 | // However, the special exception is that we do NOT skip function parameters |
| 277 | // that are split across multiple lines. See the test case in FormatTest.cpp |
| 278 | // that mentions "split function parameter alignment" for an example of this. |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 279 | template <typename F> |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 280 | static unsigned AlignTokens(const FormatStyle &Style, F &&Matches, |
| 281 | SmallVector<WhitespaceManager::Change, 16> &Changes, |
| 282 | unsigned StartAt) { |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 283 | unsigned MinColumn = 0; |
| 284 | unsigned MaxColumn = UINT_MAX; |
| 285 | |
| 286 | // Line number of the start and the end of the current token sequence. |
| 287 | unsigned StartOfSequence = 0; |
| 288 | unsigned EndOfSequence = 0; |
| 289 | |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 290 | // Measure the scope level (i.e. depth of (), [], {}) of the first token, and |
| 291 | // abort when we hit any token in a higher scope than the starting one. |
| 292 | auto NestingAndIndentLevel = StartAt < Changes.size() |
| 293 | ? Changes[StartAt].nestingAndIndentLevel() |
| 294 | : std::pair<unsigned, unsigned>(0, 0); |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 295 | |
| 296 | // Keep track of the number of commas before the matching tokens, we will only |
| 297 | // align a sequence of matching tokens if they are preceded by the same number |
| 298 | // of commas. |
| 299 | unsigned CommasBeforeLastMatch = 0; |
| 300 | unsigned CommasBeforeMatch = 0; |
| 301 | |
| 302 | // Whether a matching token has been found on the current line. |
| 303 | bool FoundMatchOnLine = false; |
| 304 | |
| 305 | // Aligns a sequence of matching tokens, on the MinColumn column. |
| 306 | // |
| 307 | // Sequences start from the first matching token to align, and end at the |
| 308 | // first token of the first line that doesn't need to be aligned. |
| 309 | // |
| 310 | // We need to adjust the StartOfTokenColumn of each Change that is on a line |
| 311 | // containing any matching token to be aligned and located after such token. |
| 312 | auto AlignCurrentSequence = [&] { |
| 313 | if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) |
| 314 | AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches, |
| 315 | Changes); |
| 316 | MinColumn = 0; |
| 317 | MaxColumn = UINT_MAX; |
| 318 | StartOfSequence = 0; |
| 319 | EndOfSequence = 0; |
| 320 | }; |
| 321 | |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 322 | unsigned i = StartAt; |
| 323 | for (unsigned e = Changes.size(); i != e; ++i) { |
| 324 | if (Changes[i].nestingAndIndentLevel() < NestingAndIndentLevel) |
| 325 | break; |
| 326 | |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 327 | if (Changes[i].NewlinesBefore != 0) { |
| 328 | CommasBeforeMatch = 0; |
| 329 | EndOfSequence = i; |
| 330 | // If there is a blank line, or if the last line didn't contain any |
| 331 | // matching token, the sequence ends here. |
| 332 | if (Changes[i].NewlinesBefore > 1 || !FoundMatchOnLine) |
| 333 | AlignCurrentSequence(); |
| 334 | |
| 335 | FoundMatchOnLine = false; |
| 336 | } |
| 337 | |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 338 | if (Changes[i].Tok->is(tok::comma)) { |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 339 | ++CommasBeforeMatch; |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 340 | } else if (Changes[i].nestingAndIndentLevel() > NestingAndIndentLevel) { |
| 341 | // Call AlignTokens recursively, skipping over this scope block. |
| 342 | unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i); |
| 343 | i = StoppedAt - 1; |
| 344 | continue; |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | if (!Matches(Changes[i])) |
| 348 | continue; |
| 349 | |
| 350 | // If there is more than one matching token per line, or if the number of |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 351 | // preceding commas, do not match anymore, end the sequence. |
| 352 | if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch) |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 353 | AlignCurrentSequence(); |
| 354 | |
| 355 | CommasBeforeLastMatch = CommasBeforeMatch; |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 356 | FoundMatchOnLine = true; |
| 357 | |
| 358 | if (StartOfSequence == 0) |
| 359 | StartOfSequence = i; |
| 360 | |
| 361 | unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn; |
| 362 | int LineLengthAfter = -Changes[i].Spaces; |
| 363 | for (unsigned j = i; j != e && Changes[j].NewlinesBefore == 0; ++j) |
| 364 | LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength; |
| 365 | unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter; |
| 366 | |
| 367 | // If we are restricted by the maximum column width, end the sequence. |
| 368 | if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn || |
| 369 | CommasBeforeLastMatch != CommasBeforeMatch) { |
| 370 | AlignCurrentSequence(); |
| 371 | StartOfSequence = i; |
| 372 | } |
| 373 | |
| 374 | MinColumn = std::max(MinColumn, ChangeMinColumn); |
| 375 | MaxColumn = std::min(MaxColumn, ChangeMaxColumn); |
| 376 | } |
| 377 | |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 378 | EndOfSequence = i; |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 379 | AlignCurrentSequence(); |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 380 | return i; |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 383 | void WhitespaceManager::alignConsecutiveAssignments() { |
| 384 | if (!Style.AlignConsecutiveAssignments) |
| 385 | return; |
| 386 | |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 387 | AlignTokens(Style, |
| 388 | [&](const Change &C) { |
| 389 | // Do not align on equal signs that are first on a line. |
| 390 | if (C.NewlinesBefore > 0) |
| 391 | return false; |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 392 | |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 393 | // Do not align on equal signs that are last on a line. |
| 394 | if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0) |
| 395 | return false; |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 396 | |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 397 | return C.Tok->is(tok::equal); |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 398 | }, |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 399 | Changes, /*StartAt=*/0); |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Daniel Jasper | e12597c | 2015-10-01 10:06:54 +0000 | [diff] [blame] | 402 | void WhitespaceManager::alignConsecutiveDeclarations() { |
| 403 | if (!Style.AlignConsecutiveDeclarations) |
| 404 | return; |
| 405 | |
Daniel Jasper | ec90e51 | 2015-12-01 12:00:43 +0000 | [diff] [blame] | 406 | // FIXME: Currently we don't handle properly the PointerAlignment: Right |
| 407 | // The * and & are not aligned and are left dangling. Something has to be done |
| 408 | // about it, but it raises the question of alignment of code like: |
| 409 | // const char* const* v1; |
| 410 | // float const* v2; |
| 411 | // SomeVeryLongType const& v3; |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 412 | AlignTokens(Style, |
| 413 | [](Change const &C) { |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 414 | // tok::kw_operator is necessary for aligning operator overload |
| 415 | // definitions. |
| 416 | return C.Tok->is(TT_StartOfName) || |
| 417 | C.Tok->is(TT_FunctionDeclarationName) || |
| 418 | C.Tok->is(tok::kw_operator); |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 419 | }, |
Nikola Smiljanic | 92b397f | 2017-03-23 02:51:25 +0000 | [diff] [blame] | 420 | Changes, /*StartAt=*/0); |
Daniel Jasper | e12597c | 2015-10-01 10:06:54 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 423 | void WhitespaceManager::alignTrailingComments() { |
| 424 | unsigned MinColumn = 0; |
| 425 | unsigned MaxColumn = UINT_MAX; |
| 426 | unsigned StartOfSequence = 0; |
| 427 | bool BreakBeforeNext = false; |
| 428 | unsigned Newlines = 0; |
| 429 | for (unsigned i = 0, e = Changes.size(); i != e; ++i) { |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 430 | if (Changes[i].StartOfBlockComment) |
| 431 | continue; |
| 432 | Newlines += Changes[i].NewlinesBefore; |
| 433 | if (!Changes[i].IsTrailingComment) |
| 434 | continue; |
| 435 | |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 436 | unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn; |
Krasimir Georgiev | 59ed77b | 2017-06-04 19:27:02 +0000 | [diff] [blame^] | 437 | unsigned ChangeMaxColumn = Style.ColumnLimit >= Changes[i].TokenLength |
| 438 | ? Style.ColumnLimit - Changes[i].TokenLength |
| 439 | : ChangeMinColumn; |
Daniel Jasper | 417fc81 | 2016-01-09 15:56:53 +0000 | [diff] [blame] | 440 | |
| 441 | // If we don't create a replacement for this change, we have to consider |
| 442 | // it to be immovable. |
| 443 | if (!Changes[i].CreateReplacement) |
| 444 | ChangeMaxColumn = ChangeMinColumn; |
| 445 | |
Daniel Jasper | 6693502 | 2014-04-27 10:03:19 +0000 | [diff] [blame] | 446 | if (i + 1 != e && Changes[i + 1].ContinuesPPDirective) |
| 447 | ChangeMaxColumn -= 2; |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 448 | // If this comment follows an } in column 0, it probably documents the |
| 449 | // closing of a namespace and we don't want to align it. |
| 450 | bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 && |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 451 | Changes[i - 1].Tok->is(tok::r_brace) && |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 452 | Changes[i - 1].StartOfTokenColumn == 0; |
| 453 | bool WasAlignedWithStartOfNextLine = false; |
| 454 | if (Changes[i].NewlinesBefore == 1) { // A comment on its own line. |
Daniel Jasper | 4953210 | 2015-01-07 14:00:11 +0000 | [diff] [blame] | 455 | unsigned CommentColumn = SourceMgr.getSpellingColumnNumber( |
| 456 | Changes[i].OriginalWhitespaceRange.getEnd()); |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 457 | for (unsigned j = i + 1; j != e; ++j) { |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 458 | if (Changes[j].Tok->is(tok::comment)) |
Daniel Jasper | bb37a2f | 2016-02-01 11:20:55 +0000 | [diff] [blame] | 459 | continue; |
| 460 | |
| 461 | unsigned NextColumn = SourceMgr.getSpellingColumnNumber( |
| 462 | Changes[j].OriginalWhitespaceRange.getEnd()); |
| 463 | // The start of the next token was previously aligned with the |
| 464 | // start of this comment. |
| 465 | WasAlignedWithStartOfNextLine = |
| 466 | CommentColumn == NextColumn || |
| 467 | CommentColumn == NextColumn + Style.IndentWidth; |
| 468 | break; |
Daniel Jasper | 0e93cdb | 2013-11-08 23:31:14 +0000 | [diff] [blame] | 469 | } |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 470 | } |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 471 | if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) { |
| 472 | alignTrailingComments(StartOfSequence, i, MinColumn); |
| 473 | MinColumn = ChangeMinColumn; |
| 474 | MaxColumn = ChangeMinColumn; |
| 475 | StartOfSequence = i; |
| 476 | } else if (BreakBeforeNext || Newlines > 1 || |
| 477 | (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) || |
| 478 | // Break the comment sequence if the previous line did not end |
| 479 | // in a trailing comment. |
| 480 | (Changes[i].NewlinesBefore == 1 && i > 0 && |
| 481 | !Changes[i - 1].IsTrailingComment) || |
| 482 | WasAlignedWithStartOfNextLine) { |
| 483 | alignTrailingComments(StartOfSequence, i, MinColumn); |
| 484 | MinColumn = ChangeMinColumn; |
| 485 | MaxColumn = ChangeMaxColumn; |
| 486 | StartOfSequence = i; |
| 487 | } else { |
| 488 | MinColumn = std::max(MinColumn, ChangeMinColumn); |
| 489 | MaxColumn = std::min(MaxColumn, ChangeMaxColumn); |
| 490 | } |
| 491 | BreakBeforeNext = |
| 492 | (i == 0) || (Changes[i].NewlinesBefore > 1) || |
| 493 | // Never start a sequence with a comment at the beginning of |
| 494 | // the line. |
| 495 | (Changes[i].NewlinesBefore == 1 && StartOfSequence == i); |
| 496 | Newlines = 0; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 497 | } |
| 498 | alignTrailingComments(StartOfSequence, Changes.size(), MinColumn); |
| 499 | } |
| 500 | |
| 501 | void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End, |
| 502 | unsigned Column) { |
| 503 | for (unsigned i = Start; i != End; ++i) { |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 504 | int Shift = 0; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 505 | if (Changes[i].IsTrailingComment) { |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 506 | Shift = Column - Changes[i].StartOfTokenColumn; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 507 | } |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 508 | if (Changes[i].StartOfBlockComment) { |
| 509 | Shift = Changes[i].IndentationOffset + |
| 510 | Changes[i].StartOfBlockComment->StartOfTokenColumn - |
| 511 | Changes[i].StartOfTokenColumn; |
| 512 | } |
| 513 | assert(Shift >= 0); |
| 514 | Changes[i].Spaces += Shift; |
Andi-Bogdan Postelnicu | a9a8fde | 2016-10-26 07:44:51 +0000 | [diff] [blame] | 515 | if (i + 1 != Changes.size()) |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 516 | Changes[i + 1].PreviousEndOfTokenColumn += Shift; |
| 517 | Changes[i].StartOfTokenColumn += Shift; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
| 521 | void WhitespaceManager::alignEscapedNewlines() { |
Daniel Jasper | 7fdbb3f | 2017-05-08 15:08:00 +0000 | [diff] [blame] | 522 | if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign) |
| 523 | return; |
| 524 | |
| 525 | bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left; |
| 526 | unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 527 | unsigned StartOfMacro = 0; |
| 528 | for (unsigned i = 1, e = Changes.size(); i < e; ++i) { |
| 529 | Change &C = Changes[i]; |
| 530 | if (C.NewlinesBefore > 0) { |
| 531 | if (C.ContinuesPPDirective) { |
Daniel Jasper | a49393f | 2013-08-28 09:07:32 +0000 | [diff] [blame] | 532 | MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine); |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 533 | } else { |
| 534 | alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine); |
Daniel Jasper | 7fdbb3f | 2017-05-08 15:08:00 +0000 | [diff] [blame] | 535 | MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit; |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 536 | StartOfMacro = i; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine); |
| 541 | } |
| 542 | |
| 543 | void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End, |
| 544 | unsigned Column) { |
| 545 | for (unsigned i = Start; i < End; ++i) { |
| 546 | Change &C = Changes[i]; |
| 547 | if (C.NewlinesBefore > 0) { |
| 548 | assert(C.ContinuesPPDirective); |
| 549 | if (C.PreviousEndOfTokenColumn + 1 > Column) |
| 550 | C.EscapedNewlineColumn = 0; |
| 551 | else |
| 552 | C.EscapedNewlineColumn = Column; |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | void WhitespaceManager::generateChanges() { |
| 558 | for (unsigned i = 0, e = Changes.size(); i != e; ++i) { |
| 559 | const Change &C = Changes[i]; |
Daniel Jasper | 47b35ae | 2015-01-29 10:47:14 +0000 | [diff] [blame] | 560 | if (i > 0) { |
| 561 | assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() != |
| 562 | C.OriginalWhitespaceRange.getBegin() && |
| 563 | "Generating two replacements for the same location"); |
| 564 | } |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 565 | if (C.CreateReplacement) { |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 566 | std::string ReplacementText = C.PreviousLinePostfix; |
| 567 | if (C.ContinuesPPDirective) |
| 568 | appendNewlineText(ReplacementText, C.NewlinesBefore, |
| 569 | C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn); |
| 570 | else |
| 571 | appendNewlineText(ReplacementText, C.NewlinesBefore); |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 572 | appendIndentText(ReplacementText, C.Tok->IndentLevel, |
| 573 | std::max(0, C.Spaces), |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 574 | C.StartOfTokenColumn - std::max(0, C.Spaces)); |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 575 | ReplacementText.append(C.CurrentLinePrefix); |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 576 | storeReplacement(C.OriginalWhitespaceRange, ReplacementText); |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
Craig Topper | e335f25 | 2015-10-04 04:53:55 +0000 | [diff] [blame] | 581 | void WhitespaceManager::storeReplacement(SourceRange Range, |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 582 | StringRef Text) { |
| 583 | unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) - |
| 584 | SourceMgr.getFileOffset(Range.getBegin()); |
| 585 | // Don't create a replacement, if it does not change anything. |
| 586 | if (StringRef(SourceMgr.getCharacterData(Range.getBegin()), |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 587 | WhitespaceLength) == Text) |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 588 | return; |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 589 | auto Err = Replaces.add(tooling::Replacement( |
Manuel Klimek | 4fe4300 | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 590 | SourceMgr, CharSourceRange::getCharRange(Range), Text)); |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 591 | // FIXME: better error handling. For now, just print an error message in the |
| 592 | // release version. |
Piotr Padlewski | 1ec383c | 2016-12-23 11:40:44 +0000 | [diff] [blame] | 593 | if (Err) { |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 594 | llvm::errs() << llvm::toString(std::move(Err)) << "\n"; |
Piotr Padlewski | 1ec383c | 2016-12-23 11:40:44 +0000 | [diff] [blame] | 595 | assert(false); |
| 596 | } |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 599 | void WhitespaceManager::appendNewlineText(std::string &Text, |
| 600 | unsigned Newlines) { |
| 601 | for (unsigned i = 0; i < Newlines; ++i) |
| 602 | Text.append(UseCRLF ? "\r\n" : "\n"); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 605 | void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines, |
| 606 | unsigned PreviousEndOfTokenColumn, |
| 607 | unsigned EscapedNewlineColumn) { |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 608 | if (Newlines > 0) { |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 609 | unsigned Offset = |
Daniel Jasper | 7fdbb3f | 2017-05-08 15:08:00 +0000 | [diff] [blame] | 610 | std::min<int>(EscapedNewlineColumn - 2, PreviousEndOfTokenColumn); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 611 | for (unsigned i = 0; i < Newlines; ++i) { |
Benjamin Kramer | ddf1cda | 2015-05-28 19:55:49 +0000 | [diff] [blame] | 612 | Text.append(EscapedNewlineColumn - Offset - 1, ' '); |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 613 | Text.append(UseCRLF ? "\\\r\n" : "\\\n"); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 614 | Offset = 0; |
| 615 | } |
| 616 | } |
Manuel Klimek | b9eae4c | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 619 | void WhitespaceManager::appendIndentText(std::string &Text, |
| 620 | unsigned IndentLevel, unsigned Spaces, |
Alexander Kornienko | db4c21f | 2013-09-27 09:45:40 +0000 | [diff] [blame] | 621 | unsigned WhitespaceStartColumn) { |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 622 | switch (Style.UseTab) { |
| 623 | case FormatStyle::UT_Never: |
Benjamin Kramer | ddf1cda | 2015-05-28 19:55:49 +0000 | [diff] [blame] | 624 | Text.append(Spaces, ' '); |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 625 | break; |
| 626 | case FormatStyle::UT_Always: { |
Alexander Kornienko | db4c21f | 2013-09-27 09:45:40 +0000 | [diff] [blame] | 627 | unsigned FirstTabWidth = |
| 628 | Style.TabWidth - WhitespaceStartColumn % Style.TabWidth; |
| 629 | // Indent with tabs only when there's at least one full tab. |
| 630 | if (FirstTabWidth + Style.TabWidth <= Spaces) { |
| 631 | Spaces -= FirstTabWidth; |
| 632 | Text.append("\t"); |
| 633 | } |
Benjamin Kramer | ddf1cda | 2015-05-28 19:55:49 +0000 | [diff] [blame] | 634 | Text.append(Spaces / Style.TabWidth, '\t'); |
| 635 | Text.append(Spaces % Style.TabWidth, ' '); |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 636 | break; |
| 637 | } |
| 638 | case FormatStyle::UT_ForIndentation: |
| 639 | if (WhitespaceStartColumn == 0) { |
| 640 | unsigned Indentation = IndentLevel * Style.IndentWidth; |
Alexander Kornienko | 45dc1b2 | 2013-09-27 16:40:11 +0000 | [diff] [blame] | 641 | // This happens, e.g. when a line in a block comment is indented less than |
| 642 | // the first one. |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 643 | if (Indentation > Spaces) |
| 644 | Indentation = Spaces; |
| 645 | unsigned Tabs = Indentation / Style.TabWidth; |
Benjamin Kramer | ddf1cda | 2015-05-28 19:55:49 +0000 | [diff] [blame] | 646 | Text.append(Tabs, '\t'); |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 647 | Spaces -= Tabs * Style.TabWidth; |
| 648 | } |
Benjamin Kramer | ddf1cda | 2015-05-28 19:55:49 +0000 | [diff] [blame] | 649 | Text.append(Spaces, ' '); |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 650 | break; |
Marianne Mailhot-Sarrasin | 51fe279 | 2016-04-14 14:52:26 +0000 | [diff] [blame] | 651 | case FormatStyle::UT_ForContinuationAndIndentation: |
| 652 | if (WhitespaceStartColumn == 0) { |
| 653 | unsigned Tabs = Spaces / Style.TabWidth; |
| 654 | Text.append(Tabs, '\t'); |
| 655 | Spaces -= Tabs * Style.TabWidth; |
| 656 | } |
| 657 | Text.append(Spaces, ' '); |
| 658 | break; |
Alexander Kornienko | 9e649af | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 659 | } |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 662 | } // namespace format |
| 663 | } // namespace clang |