Alexander Kornienko | 70ce788 | 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 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 21 | bool |
| 22 | WhitespaceManager::Change::IsBeforeInFile::operator()(const Change &C1, |
| 23 | const Change &C2) const { |
| 24 | return SourceMgr.isBeforeInTranslationUnit( |
| 25 | C1.OriginalWhitespaceRange.getBegin(), |
| 26 | C2.OriginalWhitespaceRange.getBegin()); |
| 27 | } |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 28 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 29 | WhitespaceManager::Change::Change( |
| 30 | bool CreateReplacement, const SourceRange &OriginalWhitespaceRange, |
| 31 | unsigned Spaces, unsigned StartOfTokenColumn, unsigned NewlinesBefore, |
| 32 | StringRef PreviousLinePostfix, StringRef CurrentLinePrefix, |
| 33 | tok::TokenKind Kind, bool ContinuesPPDirective) |
| 34 | : CreateReplacement(CreateReplacement), |
| 35 | OriginalWhitespaceRange(OriginalWhitespaceRange), |
| 36 | StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore), |
| 37 | PreviousLinePostfix(PreviousLinePostfix), |
| 38 | CurrentLinePrefix(CurrentLinePrefix), Kind(Kind), |
| 39 | ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces) {} |
| 40 | |
| 41 | void WhitespaceManager::replaceWhitespace(const AnnotatedToken &Tok, |
| 42 | unsigned Newlines, unsigned Spaces, |
| 43 | unsigned StartOfTokenColumn, |
| 44 | bool InPPDirective) { |
| 45 | Changes.push_back(Change( |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 46 | true, Tok.FormatTok.WhitespaceRange, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 47 | Spaces, StartOfTokenColumn, Newlines, "", "", Tok.FormatTok.Tok.getKind(), |
| 48 | InPPDirective && !Tok.FormatTok.IsFirst)); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 49 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 50 | // Align line comments if they are trailing or if they continue other |
| 51 | // trailing comments. |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 52 | // FIXME: Pull this out and generalize so it works the same way in broken |
| 53 | // comments and unbroken comments with trailing whitespace. |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 54 | if (Tok.isTrailingComment()) { |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 55 | SourceLocation TokenEndLoc = Tok.FormatTok.getStartOfNonWhitespace() |
| 56 | .getLocWithOffset(Tok.FormatTok.TokenLength); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 57 | // Remove the comment's trailing whitespace. |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 58 | if (Tok.FormatTok.TrailingWhiteSpaceLength != 0) |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 59 | Replaces.insert(tooling::Replacement( |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 60 | SourceMgr, TokenEndLoc, Tok.FormatTok.TrailingWhiteSpaceLength, "")); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 61 | } |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 64 | void WhitespaceManager::addUntouchableToken(const FormatToken &Tok, |
| 65 | bool InPPDirective) { |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 66 | Changes.push_back( |
| 67 | Change(false, Tok.WhitespaceRange, /*Spaces=*/0, |
| 68 | SourceMgr.getSpellingColumnNumber(Tok.Tok.getLocation()) - 1, |
| 69 | Tok.NewlinesBefore, "", "", Tok.Tok.getKind(), |
| 70 | InPPDirective && !Tok.IsFirst)); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void WhitespaceManager::breakToken(const FormatToken &Tok, unsigned Offset, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 74 | unsigned ReplaceChars, |
| 75 | StringRef PreviousPostfix, |
| 76 | StringRef CurrentPrefix, bool InPPDirective, |
| 77 | unsigned Spaces) { |
| 78 | Changes.push_back(Change( |
| 79 | true, SourceRange(Tok.getStartOfNonWhitespace().getLocWithOffset(Offset), |
| 80 | Tok.getStartOfNonWhitespace().getLocWithOffset( |
| 81 | Offset + ReplaceChars)), |
| 82 | Spaces, Spaces, 1, PreviousPostfix, CurrentPrefix, |
| 83 | // FIXME: Unify token adjustment, so we don't split it between |
| 84 | // BreakableToken and the WhitespaceManager. That would also allow us to |
| 85 | // correctly store a tok::TokenKind instead of rolling our own enum. |
| 86 | tok::unknown, InPPDirective && !Tok.IsFirst)); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void WhitespaceManager::addReplacement(const SourceLocation &SourceLoc, |
| 90 | unsigned ReplaceChars, StringRef Text) { |
| 91 | Replaces.insert( |
| 92 | tooling::Replacement(SourceMgr, SourceLoc, ReplaceChars, Text)); |
| 93 | } |
| 94 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 95 | const tooling::Replacements &WhitespaceManager::generateReplacements() { |
| 96 | if (Changes.empty()) |
| 97 | return Replaces; |
| 98 | |
| 99 | std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr)); |
| 100 | calculateLineBreakInformation(); |
| 101 | alignTrailingComments(); |
| 102 | alignEscapedNewlines(); |
| 103 | generateChanges(); |
| 104 | |
| 105 | return Replaces; |
| 106 | } |
| 107 | |
| 108 | void WhitespaceManager::calculateLineBreakInformation() { |
| 109 | Changes[0].PreviousEndOfTokenColumn = 0; |
| 110 | for (unsigned i = 1, e = Changes.size(); i != e; ++i) { |
| 111 | unsigned OriginalWhitespaceStart = |
| 112 | SourceMgr.getFileOffset(Changes[i].OriginalWhitespaceRange.getBegin()); |
| 113 | unsigned PreviousOriginalWhitespaceEnd = SourceMgr.getFileOffset( |
| 114 | Changes[i - 1].OriginalWhitespaceRange.getEnd()); |
| 115 | Changes[i - 1].TokenLength = |
| 116 | OriginalWhitespaceStart - PreviousOriginalWhitespaceEnd + |
| 117 | Changes[i].PreviousLinePostfix.size() + |
| 118 | Changes[i - 1].CurrentLinePrefix.size(); |
| 119 | |
| 120 | Changes[i].PreviousEndOfTokenColumn = |
| 121 | Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength; |
| 122 | |
| 123 | Changes[i - 1].IsTrailingComment = |
| 124 | (Changes[i].NewlinesBefore > 0 || Changes[i].Kind == tok::eof) && |
| 125 | Changes[i - 1].Kind == tok::comment; |
| 126 | } |
Manuel Klimek | 0cd57b5 | 2013-05-22 14:01:08 +0000 | [diff] [blame] | 127 | // FIXME: The last token is currently not always an eof token; in those |
| 128 | // cases, setting TokenLength of the last token to 0 is wrong. |
| 129 | Changes.back().TokenLength = 0; |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 130 | Changes.back().IsTrailingComment = Changes.back().Kind == tok::comment; |
| 131 | } |
| 132 | |
| 133 | void WhitespaceManager::alignTrailingComments() { |
| 134 | unsigned MinColumn = 0; |
| 135 | unsigned MaxColumn = UINT_MAX; |
| 136 | unsigned StartOfSequence = 0; |
| 137 | bool BreakBeforeNext = false; |
| 138 | unsigned Newlines = 0; |
| 139 | for (unsigned i = 0, e = Changes.size(); i != e; ++i) { |
| 140 | unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn; |
| 141 | // FIXME: Correctly handle ChangeMaxColumn in PP directives. |
| 142 | unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength; |
| 143 | Newlines += Changes[i].NewlinesBefore; |
| 144 | if (Changes[i].IsTrailingComment) { |
Manuel Klimek | ebfb88c | 2013-05-23 11:42:52 +0000 | [diff] [blame] | 145 | bool WasAlignedWithStartOfNextLine = |
| 146 | // A comment on its own line. |
| 147 | Changes[i].NewlinesBefore == 1 && |
| 148 | // Not the last line. |
| 149 | i + 1 != e && |
| 150 | // The start of the next token was previously aligned with |
| 151 | // the start of this comment. |
| 152 | (SourceMgr.getSpellingColumnNumber( |
| 153 | Changes[i].OriginalWhitespaceRange.getEnd()) == |
| 154 | SourceMgr.getSpellingColumnNumber( |
| 155 | Changes[i + 1].OriginalWhitespaceRange.getEnd())) && |
| 156 | // Which is not a comment itself. |
| 157 | Changes[i + 1].Kind != tok::comment; |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 158 | if (BreakBeforeNext || Newlines > 1 || |
| 159 | (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) || |
| 160 | // Break the comment sequence if the previous line did not end |
| 161 | // in a trailing comment. |
| 162 | (Changes[i].NewlinesBefore == 1 && i > 0 && |
Manuel Klimek | ebfb88c | 2013-05-23 11:42:52 +0000 | [diff] [blame] | 163 | !Changes[i - 1].IsTrailingComment) || |
| 164 | WasAlignedWithStartOfNextLine) { |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 165 | alignTrailingComments(StartOfSequence, i, MinColumn); |
| 166 | MinColumn = ChangeMinColumn; |
| 167 | MaxColumn = ChangeMaxColumn; |
| 168 | StartOfSequence = i; |
| 169 | } else { |
| 170 | MinColumn = std::max(MinColumn, ChangeMinColumn); |
| 171 | MaxColumn = std::min(MaxColumn, ChangeMaxColumn); |
| 172 | } |
Manuel Klimek | 854ca79 | 2013-05-23 20:46:07 +0000 | [diff] [blame^] | 173 | BreakBeforeNext = |
| 174 | (i == 0) || (Changes[i].NewlinesBefore > 1) || |
| 175 | // Never start a sequence with a comment at the beginning of |
| 176 | // the line. |
| 177 | (Changes[i].NewlinesBefore == 1 && StartOfSequence == i); |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 178 | Newlines = 0; |
| 179 | } |
| 180 | } |
| 181 | alignTrailingComments(StartOfSequence, Changes.size(), MinColumn); |
| 182 | } |
| 183 | |
| 184 | void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End, |
| 185 | unsigned Column) { |
| 186 | for (unsigned i = Start; i != End; ++i) { |
| 187 | if (Changes[i].IsTrailingComment) { |
| 188 | assert(Column >= Changes[i].StartOfTokenColumn); |
| 189 | Changes[i].Spaces += Column - Changes[i].StartOfTokenColumn; |
| 190 | Changes[i].StartOfTokenColumn = Column; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void WhitespaceManager::alignEscapedNewlines() { |
| 196 | unsigned MaxEndOfLine = 0; |
| 197 | unsigned StartOfMacro = 0; |
| 198 | for (unsigned i = 1, e = Changes.size(); i < e; ++i) { |
| 199 | Change &C = Changes[i]; |
| 200 | if (C.NewlinesBefore > 0) { |
| 201 | if (C.ContinuesPPDirective) { |
| 202 | if (Style.AlignEscapedNewlinesLeft) |
| 203 | MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine); |
| 204 | else |
| 205 | MaxEndOfLine = Style.ColumnLimit; |
| 206 | } else { |
| 207 | alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine); |
| 208 | MaxEndOfLine = 0; |
| 209 | StartOfMacro = i; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine); |
| 214 | } |
| 215 | |
| 216 | void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End, |
| 217 | unsigned Column) { |
| 218 | for (unsigned i = Start; i < End; ++i) { |
| 219 | Change &C = Changes[i]; |
| 220 | if (C.NewlinesBefore > 0) { |
| 221 | assert(C.ContinuesPPDirective); |
| 222 | if (C.PreviousEndOfTokenColumn + 1 > Column) |
| 223 | C.EscapedNewlineColumn = 0; |
| 224 | else |
| 225 | C.EscapedNewlineColumn = Column; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void WhitespaceManager::generateChanges() { |
| 231 | for (unsigned i = 0, e = Changes.size(); i != e; ++i) { |
| 232 | const Change &C = Changes[i]; |
| 233 | if (C.CreateReplacement) { |
| 234 | std::string ReplacementText = |
| 235 | C.PreviousLinePostfix + |
| 236 | (C.ContinuesPPDirective |
| 237 | ? getNewLineText(C.NewlinesBefore, C.Spaces, |
| 238 | C.PreviousEndOfTokenColumn, |
| 239 | C.EscapedNewlineColumn) |
| 240 | : getNewLineText(C.NewlinesBefore, C.Spaces)) + |
| 241 | C.CurrentLinePrefix; |
| 242 | storeReplacement(C.OriginalWhitespaceRange, ReplacementText); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void WhitespaceManager::storeReplacement(const SourceRange &Range, |
| 248 | StringRef Text) { |
| 249 | unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) - |
| 250 | SourceMgr.getFileOffset(Range.getBegin()); |
| 251 | // Don't create a replacement, if it does not change anything. |
| 252 | if (StringRef(SourceMgr.getCharacterData(Range.getBegin()), |
| 253 | WhitespaceLength) == |
| 254 | Text) |
| 255 | return; |
| 256 | Replaces.insert(tooling::Replacement( |
| 257 | SourceMgr, CharSourceRange::getCharRange(Range), Text)); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 260 | std::string WhitespaceManager::getNewLineText(unsigned NewLines, |
| 261 | unsigned Spaces) { |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 262 | return std::string(NewLines, '\n') + getIndentText(Spaces); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | std::string WhitespaceManager::getNewLineText(unsigned NewLines, |
| 266 | unsigned Spaces, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 267 | unsigned PreviousEndOfTokenColumn, |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 268 | unsigned EscapedNewlineColumn) { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 269 | std::string NewLineText; |
| 270 | if (NewLines > 0) { |
| 271 | unsigned Offset = |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 272 | std::min<int>(EscapedNewlineColumn - 1, PreviousEndOfTokenColumn); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 273 | for (unsigned i = 0; i < NewLines; ++i) { |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 274 | NewLineText += std::string(EscapedNewlineColumn - Offset - 1, ' '); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 275 | NewLineText += "\\\n"; |
| 276 | Offset = 0; |
| 277 | } |
| 278 | } |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 279 | return NewLineText + getIndentText(Spaces); |
| 280 | } |
| 281 | |
| 282 | std::string WhitespaceManager::getIndentText(unsigned Spaces) { |
Manuel Klimek | 967d9e9 | 2013-05-13 12:53:04 +0000 | [diff] [blame] | 283 | if (!Style.UseTab) |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 284 | return std::string(Spaces, ' '); |
Manuel Klimek | 967d9e9 | 2013-05-13 12:53:04 +0000 | [diff] [blame] | 285 | |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 286 | return std::string(Spaces / Style.IndentWidth, '\t') + |
| 287 | std::string(Spaces % Style.IndentWidth, ' '); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 290 | } // namespace format |
| 291 | } // namespace clang |