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