Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1 | //===--- BreakableToken.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 Contains implementation of BreakableToken class and classes derived |
| 12 | /// from it. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "format-token-breaker" |
| 17 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 18 | #include "BreakableToken.h" |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 19 | #include "clang/Format/Format.h" |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | |
| 24 | namespace clang { |
| 25 | namespace format { |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 26 | namespace { |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 27 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 28 | // FIXME: Move helper string functions to where it makes sense. |
| 29 | |
| 30 | unsigned getOctalLength(StringRef Text) { |
| 31 | unsigned I = 1; |
| 32 | while (I < Text.size() && I < 4 && (Text[I] >= '0' && Text[I] <= '7')) { |
| 33 | ++I; |
| 34 | } |
| 35 | return I; |
| 36 | } |
| 37 | |
| 38 | unsigned getHexLength(StringRef Text) { |
| 39 | unsigned I = 2; // Point after '\x'. |
| 40 | while (I < Text.size() && ((Text[I] >= '0' && Text[I] <= '9') || |
| 41 | (Text[I] >= 'a' && Text[I] <= 'f') || |
| 42 | (Text[I] >= 'A' && Text[I] <= 'F'))) { |
| 43 | ++I; |
| 44 | } |
| 45 | return I; |
| 46 | } |
| 47 | |
| 48 | unsigned getEscapeSequenceLength(StringRef Text) { |
| 49 | assert(Text[0] == '\\'); |
| 50 | if (Text.size() < 2) |
| 51 | return 1; |
| 52 | |
| 53 | switch (Text[1]) { |
| 54 | case 'u': |
| 55 | return 6; |
| 56 | case 'U': |
| 57 | return 10; |
| 58 | case 'x': |
| 59 | return getHexLength(Text); |
| 60 | default: |
| 61 | if (Text[1] >= '0' && Text[1] <= '7') |
| 62 | return getOctalLength(Text); |
| 63 | return 2; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | StringRef::size_type getStartOfCharacter(StringRef Text, |
| 68 | StringRef::size_type Offset) { |
| 69 | StringRef::size_type NextEscape = Text.find('\\'); |
| 70 | while (NextEscape != StringRef::npos && NextEscape < Offset) { |
| 71 | StringRef::size_type SequenceLength = |
| 72 | getEscapeSequenceLength(Text.substr(NextEscape)); |
| 73 | if (Offset < NextEscape + SequenceLength) |
| 74 | return NextEscape; |
| 75 | NextEscape = Text.find('\\', NextEscape + SequenceLength); |
| 76 | } |
| 77 | return Offset; |
| 78 | } |
| 79 | |
| 80 | BreakableToken::Split getCommentSplit(StringRef Text, |
| 81 | unsigned ContentStartColumn, |
| 82 | unsigned ColumnLimit) { |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 83 | if (ColumnLimit <= ContentStartColumn + 1) |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 84 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 85 | |
| 86 | unsigned MaxSplit = ColumnLimit - ContentStartColumn + 1; |
| 87 | StringRef::size_type SpaceOffset = Text.rfind(' ', MaxSplit); |
| 88 | if (SpaceOffset == StringRef::npos || |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 89 | // Don't break at leading whitespace. |
| 90 | Text.find_last_not_of(' ', SpaceOffset) == StringRef::npos) |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 91 | SpaceOffset = Text.find(' ', MaxSplit); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 92 | if (SpaceOffset != StringRef::npos && SpaceOffset != 0) { |
| 93 | StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(); |
| 94 | StringRef AfterCut = Text.substr(SpaceOffset).ltrim(); |
| 95 | return BreakableToken::Split(BeforeCut.size(), |
| 96 | AfterCut.begin() - BeforeCut.end()); |
| 97 | } |
| 98 | return BreakableToken::Split(StringRef::npos, 0); |
| 99 | } |
| 100 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 101 | BreakableToken::Split getStringSplit(StringRef Text, |
| 102 | unsigned ContentStartColumn, |
| 103 | unsigned ColumnLimit) { |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 104 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 105 | if (ColumnLimit <= ContentStartColumn) |
| 106 | return BreakableToken::Split(StringRef::npos, 0); |
| 107 | unsigned MaxSplit = ColumnLimit - ContentStartColumn; |
| 108 | // FIXME: Reduce unit test case. |
| 109 | if (Text.empty()) |
| 110 | return BreakableToken::Split(StringRef::npos, 0); |
| 111 | MaxSplit = std::min<unsigned>(MaxSplit, Text.size() - 1); |
| 112 | StringRef::size_type SpaceOffset = Text.rfind(' ', MaxSplit); |
| 113 | if (SpaceOffset != StringRef::npos && SpaceOffset != 0) |
| 114 | return BreakableToken::Split(SpaceOffset + 1, 0); |
| 115 | StringRef::size_type SlashOffset = Text.rfind('/', MaxSplit); |
| 116 | if (SlashOffset != StringRef::npos && SlashOffset != 0) |
| 117 | return BreakableToken::Split(SlashOffset + 1, 0); |
| 118 | StringRef::size_type SplitPoint = getStartOfCharacter(Text, MaxSplit); |
| 119 | if (SplitPoint == StringRef::npos || SplitPoint == 0) |
| 120 | return BreakableToken::Split(StringRef::npos, 0); |
| 121 | return BreakableToken::Split(SplitPoint, 0); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 124 | } // namespace |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 125 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 126 | unsigned BreakableSingleLineToken::getLineCount() const { return 1; } |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 127 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 128 | unsigned |
| 129 | BreakableSingleLineToken::getLineLengthAfterSplit(unsigned LineIndex, |
| 130 | unsigned TailOffset) const { |
| 131 | return StartColumn + Prefix.size() + Postfix.size() + Line.size() - |
| 132 | TailOffset; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 135 | void BreakableSingleLineToken::insertBreak(unsigned LineIndex, |
| 136 | unsigned TailOffset, Split Split, |
| 137 | bool InPPDirective, |
| 138 | WhitespaceManager &Whitespaces) { |
| 139 | Whitespaces.breakToken(Tok, Prefix.size() + TailOffset + Split.first, |
| 140 | Split.second, Postfix, Prefix, InPPDirective, |
| 141 | StartColumn); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 144 | BreakableSingleLineToken::BreakableSingleLineToken(const FormatToken &Tok, |
| 145 | unsigned StartColumn, |
| 146 | StringRef Prefix, |
| 147 | StringRef Postfix) |
| 148 | : BreakableToken(Tok), StartColumn(StartColumn), Prefix(Prefix), |
| 149 | Postfix(Postfix) { |
| 150 | assert(Tok.TokenText.startswith(Prefix) && Tok.TokenText.endswith(Postfix)); |
| 151 | Line = Tok.TokenText.substr( |
| 152 | Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size()); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 155 | BreakableStringLiteral::BreakableStringLiteral(const FormatToken &Tok, |
| 156 | unsigned StartColumn) |
| 157 | : BreakableSingleLineToken(Tok, StartColumn, "\"", "\"") {} |
| 158 | |
| 159 | BreakableToken::Split |
| 160 | BreakableStringLiteral::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 161 | unsigned ColumnLimit) const { |
| 162 | return getStringSplit(Line.substr(TailOffset), StartColumn + 2, ColumnLimit); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 165 | static StringRef getLineCommentPrefix(StringRef Comment) { |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 166 | const char *KnownPrefixes[] = { "/// ", "///", "// ", "//" }; |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 167 | for (size_t i = 0, e = llvm::array_lengthof(KnownPrefixes); i != e; ++i) |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 168 | if (Comment.startswith(KnownPrefixes[i])) |
| 169 | return KnownPrefixes[i]; |
| 170 | return ""; |
| 171 | } |
| 172 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 173 | BreakableLineComment::BreakableLineComment(const FormatToken &Token, |
| 174 | unsigned StartColumn) |
| 175 | : BreakableSingleLineToken(Token, StartColumn, |
| 176 | getLineCommentPrefix(Token.TokenText), "") {} |
| 177 | |
| 178 | BreakableToken::Split |
| 179 | BreakableLineComment::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 180 | unsigned ColumnLimit) const { |
| 181 | return getCommentSplit(Line.substr(TailOffset), StartColumn + Prefix.size(), |
| 182 | ColumnLimit); |
| 183 | } |
| 184 | |
| 185 | BreakableBlockComment::BreakableBlockComment(const FormatStyle &Style, |
| 186 | const FormatToken &Token, |
| 187 | unsigned StartColumn, |
| 188 | unsigned OriginalStartColumn, |
| 189 | bool FirstInLine) |
| 190 | : BreakableToken(Token) { |
| 191 | StringRef TokenText(Token.TokenText); |
| 192 | assert(TokenText.startswith("/*") && TokenText.endswith("*/")); |
| 193 | TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n"); |
| 194 | |
| 195 | int IndentDelta = StartColumn - OriginalStartColumn; |
| 196 | bool NeedsStar = true; |
| 197 | LeadingWhitespace.resize(Lines.size()); |
| 198 | StartOfLineColumn.resize(Lines.size()); |
| 199 | if (Lines.size() == 1 && !FirstInLine) { |
| 200 | // Comments for which FirstInLine is false can start on arbitrary column, |
| 201 | // and available horizontal space can be too small to align consecutive |
| 202 | // lines with the first one. |
| 203 | // FIXME: We could, probably, align them to current indentation level, but |
| 204 | // now we just wrap them without stars. |
| 205 | NeedsStar = false; |
| 206 | } |
| 207 | StartOfLineColumn[0] = StartColumn + 2; |
| 208 | for (size_t i = 1; i < Lines.size(); ++i) { |
| 209 | adjustWhitespace(Style, i, IndentDelta); |
| 210 | if (Lines[i].empty()) |
| 211 | // If the last line is empty, the closing "*/" will have a star. |
| 212 | NeedsStar = NeedsStar && i + 1 == Lines.size(); |
| 213 | else |
| 214 | NeedsStar = NeedsStar && Lines[i][0] == '*'; |
| 215 | } |
| 216 | Decoration = NeedsStar ? "* " : ""; |
| 217 | IndentAtLineBreak = StartOfLineColumn[0] + 1; |
| 218 | for (size_t i = 1; i < Lines.size(); ++i) { |
| 219 | if (Lines[i].empty()) { |
| 220 | if (!NeedsStar && i + 1 != Lines.size()) |
| 221 | // For all but the last line (which always ends in */), set the |
| 222 | // start column to 0 if they're empty, so we do not insert |
| 223 | // trailing whitespace anywhere. |
| 224 | StartOfLineColumn[i] = 0; |
| 225 | continue; |
| 226 | } |
| 227 | if (NeedsStar) { |
| 228 | // The first line already excludes the star. |
| 229 | // For all other lines, adjust the line to exclude the star and |
| 230 | // (optionally) the first whitespace. |
| 231 | int Offset = Lines[i].startswith("* ") ? 2 : 1; |
| 232 | StartOfLineColumn[i] += Offset; |
| 233 | Lines[i] = Lines[i].substr(Offset); |
| 234 | LeadingWhitespace[i] += Offset; |
| 235 | } |
| 236 | IndentAtLineBreak = std::min<int>(IndentAtLineBreak, StartOfLineColumn[i]); |
| 237 | } |
| 238 | DEBUG({ |
| 239 | for (size_t i = 0; i < Lines.size(); ++i) { |
| 240 | llvm::dbgs() << i << " |" << Lines[i] << "| " << LeadingWhitespace[i] |
| 241 | << "\n"; |
| 242 | } |
| 243 | }); |
| 244 | } |
| 245 | |
| 246 | void BreakableBlockComment::adjustWhitespace(const FormatStyle &Style, |
| 247 | unsigned LineIndex, |
| 248 | int IndentDelta) { |
| 249 | // Calculate the end of the non-whitespace text in the previous line. |
| 250 | size_t EndOfPreviousLine = Lines[LineIndex - 1].find_last_not_of(" \\\t"); |
| 251 | if (EndOfPreviousLine == StringRef::npos) |
| 252 | EndOfPreviousLine = 0; |
| 253 | else |
| 254 | ++EndOfPreviousLine; |
| 255 | // Calculate the start of the non-whitespace text in the current line. |
| 256 | size_t StartOfLine = Lines[LineIndex].find_first_not_of(" \t"); |
| 257 | if (StartOfLine == StringRef::npos) |
| 258 | StartOfLine = Lines[LineIndex].size(); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 259 | |
| 260 | // Adjust Lines to only contain relevant text. |
| 261 | Lines[LineIndex - 1] = Lines[LineIndex - 1].substr(0, EndOfPreviousLine); |
| 262 | Lines[LineIndex] = Lines[LineIndex].substr(StartOfLine); |
| 263 | // Adjust LeadingWhitespace to account all whitespace between the lines |
| 264 | // to the current line. |
| 265 | LeadingWhitespace[LineIndex] = |
| 266 | Lines[LineIndex].begin() - Lines[LineIndex - 1].end(); |
Manuel Klimek | d63312b | 2013-05-28 10:01:59 +0000 | [diff] [blame] | 267 | |
| 268 | // FIXME: We currently count tabs as 1 character. To solve this, we need to |
| 269 | // get the correct indentation width of the start of the comment, which |
| 270 | // requires correct counting of the tab expansions before the comment, and |
| 271 | // a configurable tab width. Since the current implementation only breaks |
| 272 | // if leading tabs are intermixed with spaces, that is not a high priority. |
| 273 | |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 274 | // Adjust the start column uniformly accross all lines. |
Manuel Klimek | d63312b | 2013-05-28 10:01:59 +0000 | [diff] [blame] | 275 | StartOfLineColumn[LineIndex] = std::max<int>(0, StartOfLine + IndentDelta); |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | unsigned BreakableBlockComment::getLineCount() const { return Lines.size(); } |
| 279 | |
| 280 | unsigned |
| 281 | BreakableBlockComment::getLineLengthAfterSplit(unsigned LineIndex, |
| 282 | unsigned TailOffset) const { |
| 283 | return getContentStartColumn(LineIndex, TailOffset) + |
| 284 | (Lines[LineIndex].size() - TailOffset) + |
| 285 | // The last line gets a "*/" postfix. |
| 286 | (LineIndex + 1 == Lines.size() ? 2 : 0); |
| 287 | } |
| 288 | |
| 289 | BreakableToken::Split |
| 290 | BreakableBlockComment::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 291 | unsigned ColumnLimit) const { |
| 292 | return getCommentSplit(Lines[LineIndex].substr(TailOffset), |
| 293 | getContentStartColumn(LineIndex, TailOffset), |
| 294 | ColumnLimit); |
| 295 | } |
| 296 | |
| 297 | void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset, |
| 298 | Split Split, bool InPPDirective, |
| 299 | WhitespaceManager &Whitespaces) { |
| 300 | StringRef Text = Lines[LineIndex].substr(TailOffset); |
| 301 | StringRef Prefix = Decoration; |
| 302 | if (LineIndex + 1 == Lines.size() && |
| 303 | Text.size() == Split.first + Split.second) { |
| 304 | // For the last line we need to break before "*/", but not to add "* ". |
| 305 | Prefix = ""; |
| 306 | } |
| 307 | |
| 308 | unsigned BreakOffsetInToken = |
| 309 | Text.data() - Tok.TokenText.data() + Split.first; |
| 310 | unsigned CharsToRemove = Split.second; |
| 311 | Whitespaces.breakToken(Tok, BreakOffsetInToken, CharsToRemove, "", Prefix, |
| 312 | InPPDirective, IndentAtLineBreak - Decoration.size()); |
| 313 | } |
| 314 | |
| 315 | void |
| 316 | BreakableBlockComment::replaceWhitespaceBefore(unsigned LineIndex, |
| 317 | unsigned InPPDirective, |
| 318 | WhitespaceManager &Whitespaces) { |
| 319 | if (LineIndex == 0) |
| 320 | return; |
| 321 | StringRef Prefix = Decoration; |
Manuel Klimek | c5cc4bf | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 322 | if (Lines[LineIndex].empty()) { |
| 323 | if (LineIndex + 1 == Lines.size()) { |
| 324 | // If the last line is empty, we don't need a prefix, as the */ will line |
| 325 | // up with the decoration (if it exists). |
| 326 | Prefix = ""; |
| 327 | } else if (!Decoration.empty()) { |
| 328 | // For other empty lines, if we do have a decoration, adapt it to not |
| 329 | // contain a trailing whitespace. |
| 330 | Prefix = Prefix.substr(0, 1); |
| 331 | } |
| 332 | } |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 333 | |
| 334 | unsigned WhitespaceOffsetInToken = |
| 335 | Lines[LineIndex].data() - Tok.TokenText.data() - |
| 336 | LeadingWhitespace[LineIndex]; |
| 337 | Whitespaces.breakToken( |
| 338 | Tok, WhitespaceOffsetInToken, LeadingWhitespace[LineIndex], "", Prefix, |
| 339 | InPPDirective, StartOfLineColumn[LineIndex] - Prefix.size()); |
| 340 | } |
| 341 | |
| 342 | unsigned |
| 343 | BreakableBlockComment::getContentStartColumn(unsigned LineIndex, |
| 344 | unsigned TailOffset) const { |
| 345 | // If we break, we always break at the predefined indent. |
| 346 | if (TailOffset != 0) |
| 347 | return IndentAtLineBreak; |
| 348 | return StartOfLineColumn[LineIndex]; |
| 349 | } |
| 350 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 351 | } // namespace format |
| 352 | } // namespace clang |