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 | |
| 17 | #ifndef LLVM_CLANG_FORMAT_BREAKABLETOKEN_H |
| 18 | #define LLVM_CLANG_FORMAT_BREAKABLETOKEN_H |
| 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 | |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +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 |
| 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, |
| 62 | bool InPPDirective, |
| 63 | WhitespaceManager &Whitespaces) = 0; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 64 | |
| 65 | /// \brief Replaces the whitespace between \p LineIndex-1 and \p LineIndex. |
| 66 | virtual void replaceWhitespaceBefore(unsigned LineIndex, |
| 67 | unsigned InPPDirective, |
| 68 | WhitespaceManager &Whitespaces) {} |
| 69 | |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 70 | protected: |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 71 | BreakableToken(const FormatToken &Tok, encoding::Encoding Encoding) |
| 72 | : Tok(Tok), Encoding(Encoding) {} |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 73 | |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 74 | const FormatToken &Tok; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 75 | encoding::Encoding Encoding; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 78 | /// \brief Base class for single line tokens that can be broken. |
| 79 | /// |
| 80 | /// \c getSplit() needs to be implemented by child classes. |
| 81 | class BreakableSingleLineToken : public BreakableToken { |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 82 | public: |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 83 | virtual unsigned getLineCount() const; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 84 | virtual unsigned getLineLengthAfterSplit(unsigned LineIndex, |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 85 | unsigned TailOffset, |
| 86 | StringRef::size_type Length) const; |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 87 | |
| 88 | protected: |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 89 | BreakableSingleLineToken(const FormatToken &Tok, unsigned StartColumn, |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 90 | StringRef Prefix, StringRef Postfix, |
| 91 | encoding::Encoding Encoding); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 92 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 93 | // The column in which the token starts. |
| 94 | unsigned StartColumn; |
| 95 | // The prefix a line needs after a break in the token. |
| 96 | StringRef Prefix; |
| 97 | // The postfix a line needs before introducing a break. |
| 98 | StringRef Postfix; |
| 99 | // The token text excluding the prefix and postfix. |
| 100 | StringRef Line; |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 103 | class BreakableStringLiteral : public BreakableSingleLineToken { |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 104 | public: |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 105 | /// \brief Creates a breakable token for a single line string literal. |
| 106 | /// |
| 107 | /// \p StartColumn specifies the column in which the token will start |
| 108 | /// after formatting. |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 109 | BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn, |
| 110 | encoding::Encoding Encoding); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 111 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 112 | virtual Split getSplit(unsigned LineIndex, unsigned TailOffset, |
| 113 | unsigned ColumnLimit) const; |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame^] | 114 | virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 115 | bool InPPDirective, |
| 116 | WhitespaceManager &Whitespaces); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 117 | }; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 118 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 119 | class BreakableLineComment : public BreakableSingleLineToken { |
| 120 | public: |
| 121 | /// \brief Creates a breakable token for a line comment. |
| 122 | /// |
| 123 | /// \p StartColumn specifies the column in which the comment will start |
| 124 | /// after formatting. |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 125 | BreakableLineComment(const FormatToken &Token, unsigned StartColumn, |
| 126 | encoding::Encoding Encoding); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 127 | |
| 128 | virtual Split getSplit(unsigned LineIndex, unsigned TailOffset, |
| 129 | unsigned ColumnLimit) const; |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame^] | 130 | virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 131 | bool InPPDirective, WhitespaceManager &Whitespaces); |
| 132 | virtual void replaceWhitespaceBefore(unsigned LineIndex, |
| 133 | unsigned InPPDirective, |
| 134 | WhitespaceManager &Whitespaces); |
| 135 | |
| 136 | private: |
| 137 | // The prefix without an additional space if one was added. |
| 138 | StringRef OriginalPrefix; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | class BreakableBlockComment : public BreakableToken { |
| 142 | public: |
| 143 | /// \brief Creates a breakable token for a block comment. |
| 144 | /// |
| 145 | /// \p StartColumn specifies the column in which the comment will start |
| 146 | /// after formatting, while \p OriginalStartColumn specifies in which |
| 147 | /// column the comment started before formatting. |
| 148 | /// If the comment starts a line after formatting, set \p FirstInLine to true. |
| 149 | BreakableBlockComment(const FormatStyle &Style, const FormatToken &Token, |
| 150 | unsigned StartColumn, unsigned OriginaStartColumn, |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 151 | bool FirstInLine, encoding::Encoding Encoding); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 152 | |
| 153 | virtual unsigned getLineCount() const; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 154 | virtual unsigned getLineLengthAfterSplit(unsigned LineIndex, |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 155 | unsigned TailOffset, |
| 156 | StringRef::size_type Length) const; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 157 | virtual Split getSplit(unsigned LineIndex, unsigned TailOffset, |
| 158 | unsigned ColumnLimit) const; |
| 159 | virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split, |
| 160 | bool InPPDirective, WhitespaceManager &Whitespaces); |
| 161 | virtual void replaceWhitespaceBefore(unsigned LineIndex, |
| 162 | unsigned InPPDirective, |
| 163 | WhitespaceManager &Whitespaces); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 164 | |
| 165 | private: |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 166 | // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex], |
| 167 | // so that all whitespace between the lines is accounted to Lines[LineIndex] |
| 168 | // as leading whitespace: |
| 169 | // - Lines[LineIndex] points to the text after that whitespace |
| 170 | // - Lines[LineIndex-1] shrinks by its trailing whitespace |
| 171 | // - LeadingWhitespace[LineIndex] is updated with the complete whitespace |
| 172 | // between the end of the text of Lines[LineIndex-1] and Lines[LineIndex] |
| 173 | // |
| 174 | // Sets StartOfLineColumn to the intended column in which the text at |
| 175 | // Lines[LineIndex] starts (note that the decoration, if present, is not |
| 176 | // considered part of the text). |
| 177 | void adjustWhitespace(const FormatStyle &Style, unsigned LineIndex, |
| 178 | int IndentDelta); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 179 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 180 | // Returns the column at which the text in line LineIndex starts, when broken |
| 181 | // at TailOffset. Note that the decoration (if present) is not considered part |
| 182 | // of the text. |
| 183 | unsigned getContentStartColumn(unsigned LineIndex, unsigned TailOffset) const; |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 184 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 185 | // Contains the text of the lines of the block comment, excluding the leading |
| 186 | // /* in the first line and trailing */ in the last line, and excluding all |
| 187 | // trailing whitespace between the lines. Note that the decoration (if |
| 188 | // present) is also not considered part of the text. |
| 189 | SmallVector<StringRef, 16> Lines; |
| 190 | |
| 191 | // LeadingWhitespace[i] is the number of characters regarded as whitespace in |
| 192 | // front of Lines[i]. Note that this can include "* " sequences, which we |
| 193 | // regard as whitespace when all lines have a "*" prefix. |
| 194 | SmallVector<unsigned, 16> LeadingWhitespace; |
| 195 | |
| 196 | // StartOfLineColumn[i] is the target column at which Line[i] should be. |
| 197 | // Note that this excludes a leading "* " or "*" in case all lines have |
| 198 | // a "*" prefix. |
| 199 | SmallVector<unsigned, 16> StartOfLineColumn; |
| 200 | |
| 201 | // The column at which the text of a broken line should start. |
| 202 | // Note that an optional decoration would go before that column. |
| 203 | // IndentAtLineBreak is a uniform position for all lines in a block comment, |
| 204 | // regardless of their relative position. |
| 205 | // FIXME: Revisit the decision to do this; the main reason was to support |
| 206 | // patterns like |
| 207 | // /**************//** |
| 208 | // * Comment |
| 209 | // We could also support such patterns by special casing the first line |
| 210 | // instead. |
| 211 | unsigned IndentAtLineBreak; |
| 212 | |
| 213 | // Either "* " if all lines begin with a "*", or empty. |
| 214 | StringRef Decoration; |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | } // namespace format |
| 218 | } // namespace clang |
| 219 | |
| 220 | #endif // LLVM_CLANG_FORMAT_BREAKABLETOKEN_H |