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