Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1 | //===--- BreakableToken.h - 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 Declares BreakableToken, BreakableStringLiteral, and |
| 12 | /// BreakableBlockComment classes, that contain token type-specific logic to |
| 13 | /// break long lines in tokens. |
| 14 | /// |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 17 | #ifndef LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H |
| 18 | #define LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 19 | |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 20 | #include "Encoding.h" |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 21 | #include "TokenAnnotator.h" |
| 22 | #include "WhitespaceManager.h" |
| 23 | #include <utility> |
| 24 | |
| 25 | namespace clang { |
| 26 | namespace format { |
| 27 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 28 | struct FormatStyle; |
| 29 | |
| 30 | /// \brief Base class for strategies on how to break tokens. |
| 31 | /// |
| 32 | /// FIXME: The interface seems set in stone, so we might want to just pull the |
| 33 | /// strategy into the class, instead of controlling it from the outside. |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 34 | class BreakableToken { |
| 35 | public: |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 36 | /// \brief Contains starting character index and length of split. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 37 | typedef std::pair<StringRef::size_type, unsigned> Split; |
| 38 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 39 | virtual ~BreakableToken() {} |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 40 | |
| 41 | /// \brief Returns the number of lines in this token in the original code. |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 42 | virtual unsigned getLineCount() const = 0; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 43 | |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 44 | /// \brief Returns the number of columns required to format the piece of line |
| 45 | /// at \p LineIndex, from byte offset \p Offset with length \p Length. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 46 | /// |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 47 | /// Note that previous breaks are not taken into account. \p Offset is always |
| 48 | /// specified from the start of the (original) line. |
| 49 | /// \p Length can be set to StringRef::npos, which means "to the end of line". |
| 50 | virtual unsigned |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 51 | getLineLengthAfterSplit(unsigned LineIndex, unsigned Offset, |
| 52 | StringRef::size_type Length) const = 0; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 53 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 54 | /// \brief Returns a range (offset, length) at which to break the line at |
| 55 | /// \p LineIndex, if previously broken at \p TailOffset. If possible, do not |
| 56 | /// violate \p ColumnLimit. |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 57 | virtual Split getSplit(unsigned LineIndex, unsigned TailOffset, |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 58 | unsigned ColumnLimit) const = 0; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 59 | |
| 60 | /// \brief Emits the previously retrieved \p Split via \p Whitespaces. |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 61 | virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split, |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 62 | WhitespaceManager &Whitespaces) = 0; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 63 | |
Alexander Kornienko | 875395f | 2013-11-12 17:50:13 +0000 | [diff] [blame] | 64 | /// \brief Replaces the whitespace range described by \p Split with a single |
| 65 | /// space. |
| 66 | virtual void replaceWhitespace(unsigned LineIndex, unsigned TailOffset, |
| 67 | Split Split, |
| 68 | WhitespaceManager &Whitespaces) = 0; |
| 69 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 70 | /// \brief Replaces the whitespace between \p LineIndex-1 and \p LineIndex. |
| 71 | virtual void replaceWhitespaceBefore(unsigned LineIndex, |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 72 | WhitespaceManager &Whitespaces) {} |
| 73 | |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 74 | protected: |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 75 | BreakableToken(const FormatToken &Tok, unsigned IndentLevel, |
| 76 | bool InPPDirective, encoding::Encoding Encoding, |
| 77 | const FormatStyle &Style) |
| 78 | : Tok(Tok), IndentLevel(IndentLevel), InPPDirective(InPPDirective), |
| 79 | Encoding(Encoding), Style(Style) {} |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 80 | |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 81 | const FormatToken &Tok; |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 82 | const unsigned IndentLevel; |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 83 | const bool InPPDirective; |
| 84 | const encoding::Encoding Encoding; |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 85 | const FormatStyle &Style; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 88 | /// \brief Base class for single line tokens that can be broken. |
| 89 | /// |
| 90 | /// \c getSplit() needs to be implemented by child classes. |
| 91 | class BreakableSingleLineToken : public BreakableToken { |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 92 | public: |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 93 | unsigned getLineCount() const override; |
| 94 | unsigned getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset, |
| 95 | StringRef::size_type Length) const override; |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 96 | |
| 97 | protected: |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 98 | BreakableSingleLineToken(const FormatToken &Tok, unsigned IndentLevel, |
| 99 | unsigned StartColumn, StringRef Prefix, |
| 100 | StringRef Postfix, bool InPPDirective, |
| 101 | encoding::Encoding Encoding, |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 102 | const FormatStyle &Style); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 103 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 104 | // The column in which the token starts. |
| 105 | unsigned StartColumn; |
| 106 | // The prefix a line needs after a break in the token. |
| 107 | StringRef Prefix; |
| 108 | // The postfix a line needs before introducing a break. |
| 109 | StringRef Postfix; |
| 110 | // The token text excluding the prefix and postfix. |
| 111 | StringRef Line; |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 114 | class BreakableStringLiteral : public BreakableSingleLineToken { |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 115 | public: |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 116 | /// \brief Creates a breakable token for a single line string literal. |
| 117 | /// |
| 118 | /// \p StartColumn specifies the column in which the token will start |
| 119 | /// after formatting. |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 120 | BreakableStringLiteral(const FormatToken &Tok, unsigned IndentLevel, |
| 121 | unsigned StartColumn, StringRef Prefix, |
| 122 | StringRef Postfix, bool InPPDirective, |
| 123 | encoding::Encoding Encoding, const FormatStyle &Style); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 124 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 125 | Split getSplit(unsigned LineIndex, unsigned TailOffset, |
| 126 | unsigned ColumnLimit) const override; |
| 127 | void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 128 | WhitespaceManager &Whitespaces) override; |
| 129 | void replaceWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 130 | WhitespaceManager &Whitespaces) override {} |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 131 | }; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 132 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 133 | class BreakableLineComment : public BreakableSingleLineToken { |
| 134 | public: |
| 135 | /// \brief Creates a breakable token for a line comment. |
| 136 | /// |
| 137 | /// \p StartColumn specifies the column in which the comment will start |
| 138 | /// after formatting. |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 139 | BreakableLineComment(const FormatToken &Token, unsigned IndentLevel, |
| 140 | unsigned StartColumn, bool InPPDirective, |
| 141 | encoding::Encoding Encoding, const FormatStyle &Style); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 142 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 143 | Split getSplit(unsigned LineIndex, unsigned TailOffset, |
| 144 | unsigned ColumnLimit) const override; |
| 145 | void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 146 | WhitespaceManager &Whitespaces) override; |
| 147 | void replaceWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 148 | WhitespaceManager &Whitespaces) override; |
| 149 | void replaceWhitespaceBefore(unsigned LineIndex, |
| 150 | WhitespaceManager &Whitespaces) override; |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | // The prefix without an additional space if one was added. |
| 154 | StringRef OriginalPrefix; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | class BreakableBlockComment : public BreakableToken { |
| 158 | public: |
| 159 | /// \brief Creates a breakable token for a block comment. |
| 160 | /// |
| 161 | /// \p StartColumn specifies the column in which the comment will start |
| 162 | /// after formatting, while \p OriginalStartColumn specifies in which |
| 163 | /// column the comment started before formatting. |
| 164 | /// If the comment starts a line after formatting, set \p FirstInLine to true. |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 165 | BreakableBlockComment(const FormatToken &Token, unsigned IndentLevel, |
| 166 | unsigned StartColumn, unsigned OriginaStartColumn, |
| 167 | bool FirstInLine, bool InPPDirective, |
| 168 | encoding::Encoding Encoding, const FormatStyle &Style); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 169 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 170 | unsigned getLineCount() const override; |
| 171 | unsigned getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset, |
| 172 | StringRef::size_type Length) const override; |
| 173 | Split getSplit(unsigned LineIndex, unsigned TailOffset, |
| 174 | unsigned ColumnLimit) const override; |
| 175 | void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 176 | WhitespaceManager &Whitespaces) override; |
| 177 | void replaceWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 178 | WhitespaceManager &Whitespaces) override; |
| 179 | void replaceWhitespaceBefore(unsigned LineIndex, |
| 180 | WhitespaceManager &Whitespaces) override; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 181 | |
| 182 | private: |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 183 | // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex], |
| 184 | // so that all whitespace between the lines is accounted to Lines[LineIndex] |
| 185 | // as leading whitespace: |
| 186 | // - Lines[LineIndex] points to the text after that whitespace |
| 187 | // - Lines[LineIndex-1] shrinks by its trailing whitespace |
| 188 | // - LeadingWhitespace[LineIndex] is updated with the complete whitespace |
| 189 | // between the end of the text of Lines[LineIndex-1] and Lines[LineIndex] |
| 190 | // |
| 191 | // Sets StartOfLineColumn to the intended column in which the text at |
| 192 | // Lines[LineIndex] starts (note that the decoration, if present, is not |
| 193 | // considered part of the text). |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 194 | void adjustWhitespace(unsigned LineIndex, int IndentDelta); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 195 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 196 | // Returns the column at which the text in line LineIndex starts, when broken |
| 197 | // at TailOffset. Note that the decoration (if present) is not considered part |
| 198 | // of the text. |
| 199 | unsigned getContentStartColumn(unsigned LineIndex, unsigned TailOffset) const; |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 200 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 201 | // Contains the text of the lines of the block comment, excluding the leading |
| 202 | // /* in the first line and trailing */ in the last line, and excluding all |
| 203 | // trailing whitespace between the lines. Note that the decoration (if |
| 204 | // present) is also not considered part of the text. |
| 205 | SmallVector<StringRef, 16> Lines; |
| 206 | |
| 207 | // LeadingWhitespace[i] is the number of characters regarded as whitespace in |
| 208 | // front of Lines[i]. Note that this can include "* " sequences, which we |
| 209 | // regard as whitespace when all lines have a "*" prefix. |
| 210 | SmallVector<unsigned, 16> LeadingWhitespace; |
| 211 | |
| 212 | // StartOfLineColumn[i] is the target column at which Line[i] should be. |
| 213 | // Note that this excludes a leading "* " or "*" in case all lines have |
| 214 | // a "*" prefix. |
Alexander Kornienko | 00691cf | 2015-01-12 13:11:12 +0000 | [diff] [blame] | 215 | // The first line's target column is always positive. The remaining lines' |
| 216 | // target columns are relative to the first line to allow correct indentation |
| 217 | // of comments in \c WhitespaceManager. Thus they can be negative as well (in |
| 218 | // case the first line needs to be unindented more than there's actual |
| 219 | // whitespace in another line). |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 220 | SmallVector<int, 16> StartOfLineColumn; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 221 | |
| 222 | // The column at which the text of a broken line should start. |
| 223 | // Note that an optional decoration would go before that column. |
| 224 | // IndentAtLineBreak is a uniform position for all lines in a block comment, |
| 225 | // regardless of their relative position. |
| 226 | // FIXME: Revisit the decision to do this; the main reason was to support |
| 227 | // patterns like |
| 228 | // /**************//** |
| 229 | // * Comment |
| 230 | // We could also support such patterns by special casing the first line |
| 231 | // instead. |
| 232 | unsigned IndentAtLineBreak; |
| 233 | |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 234 | // This is to distinguish between the case when the last line was empty and |
| 235 | // the case when it started with a decoration ("*" or "* "). |
| 236 | bool LastLineNeedsDecoration; |
| 237 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 238 | // Either "* " if all lines begin with a "*", or empty. |
| 239 | StringRef Decoration; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | } // namespace format |
| 243 | } // namespace clang |
| 244 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 245 | #endif |