blob: afcc8b83a53e6118aa69b40dfe441160f9e6c302 [file] [log] [blame]
Alexander Kornienko70ce7882013-04-15 14:28:00 +00001//===--- 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 Kornienko00895102013-06-05 14:09:10 +000020#include "Encoding.h"
Alexander Kornienko70ce7882013-04-15 14:28:00 +000021#include "TokenAnnotator.h"
22#include "WhitespaceManager.h"
23#include <utility>
24
25namespace clang {
26namespace format {
27
Manuel Klimekde008c02013-05-27 15:23:34 +000028struct 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 Kornienko70ce7882013-04-15 14:28:00 +000034class BreakableToken {
35public:
Alexander Kornienko2785b9a2013-06-07 16:02:52 +000036 /// \brief Contains starting character index and length of split.
Manuel Klimekde008c02013-05-27 15:23:34 +000037 typedef std::pair<StringRef::size_type, unsigned> Split;
38
Alexander Kornienko70ce7882013-04-15 14:28:00 +000039 virtual ~BreakableToken() {}
Manuel Klimekde008c02013-05-27 15:23:34 +000040
41 /// \brief Returns the number of lines in this token in the original code.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000042 virtual unsigned getLineCount() const = 0;
Manuel Klimekde008c02013-05-27 15:23:34 +000043
Alexander Kornienko2785b9a2013-06-07 16:02:52 +000044 /// \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 Klimekde008c02013-05-27 15:23:34 +000046 ///
Alexander Kornienko2785b9a2013-06-07 16:02:52 +000047 /// 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 Kornienko70ce7882013-04-15 14:28:00 +000053
Manuel Klimekde008c02013-05-27 15:23:34 +000054 /// \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 Kornienko70ce7882013-04-15 14:28:00 +000057 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
Alexander Kornienko919398b2013-04-17 17:34:05 +000058 unsigned ColumnLimit) const = 0;
Manuel Klimekde008c02013-05-27 15:23:34 +000059
60 /// \brief Emits the previously retrieved \p Split via \p Whitespaces.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000061 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
Alexander Kornienko70ce7882013-04-15 14:28:00 +000062 WhitespaceManager &Whitespaces) = 0;
Manuel Klimekde008c02013-05-27 15:23:34 +000063
64 /// \brief Replaces the whitespace between \p LineIndex-1 and \p LineIndex.
65 virtual void replaceWhitespaceBefore(unsigned LineIndex,
Manuel Klimekde008c02013-05-27 15:23:34 +000066 WhitespaceManager &Whitespaces) {}
67
Alexander Kornienko919398b2013-04-17 17:34:05 +000068protected:
Alexander Kornienko16a0ec62013-06-14 11:46:10 +000069 BreakableToken(const FormatToken &Tok, bool InPPDirective,
70 encoding::Encoding Encoding)
71 : Tok(Tok), InPPDirective(InPPDirective), Encoding(Encoding) {}
Manuel Klimekde008c02013-05-27 15:23:34 +000072
Alexander Kornienko919398b2013-04-17 17:34:05 +000073 const FormatToken &Tok;
Alexander Kornienko16a0ec62013-06-14 11:46:10 +000074 const bool InPPDirective;
75 const encoding::Encoding Encoding;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000076};
77
Manuel Klimekde008c02013-05-27 15:23:34 +000078/// \brief Base class for single line tokens that can be broken.
79///
80/// \c getSplit() needs to be implemented by child classes.
81class BreakableSingleLineToken : public BreakableToken {
Alexander Kornienko70ce7882013-04-15 14:28:00 +000082public:
Manuel Klimekde008c02013-05-27 15:23:34 +000083 virtual unsigned getLineCount() const;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000084 virtual unsigned getLineLengthAfterSplit(unsigned LineIndex,
Alexander Kornienko2785b9a2013-06-07 16:02:52 +000085 unsigned TailOffset,
86 StringRef::size_type Length) const;
Alexander Kornienko919398b2013-04-17 17:34:05 +000087
88protected:
Manuel Klimekde008c02013-05-27 15:23:34 +000089 BreakableSingleLineToken(const FormatToken &Tok, unsigned StartColumn,
Alexander Kornienko00895102013-06-05 14:09:10 +000090 StringRef Prefix, StringRef Postfix,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +000091 bool InPPDirective, encoding::Encoding Encoding);
Alexander Kornienko919398b2013-04-17 17:34:05 +000092
Manuel Klimekde008c02013-05-27 15:23:34 +000093 // 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 Kornienko919398b2013-04-17 17:34:05 +0000101};
102
Manuel Klimekde008c02013-05-27 15:23:34 +0000103class BreakableStringLiteral : public BreakableSingleLineToken {
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000104public:
Manuel Klimekde008c02013-05-27 15:23:34 +0000105 /// \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 Kornienko00895102013-06-05 14:09:10 +0000109 BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000110 bool InPPDirective, encoding::Encoding Encoding);
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000111
Manuel Klimekde008c02013-05-27 15:23:34 +0000112 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
113 unsigned ColumnLimit) const;
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000114 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000115 WhitespaceManager &Whitespaces);
Manuel Klimekde008c02013-05-27 15:23:34 +0000116};
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000117
Manuel Klimekde008c02013-05-27 15:23:34 +0000118class BreakableLineComment : public BreakableSingleLineToken {
119public:
120 /// \brief Creates a breakable token for a line comment.
121 ///
122 /// \p StartColumn specifies the column in which the comment will start
123 /// after formatting.
Alexander Kornienko00895102013-06-05 14:09:10 +0000124 BreakableLineComment(const FormatToken &Token, unsigned StartColumn,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000125 bool InPPDirective, encoding::Encoding Encoding);
Manuel Klimekde008c02013-05-27 15:23:34 +0000126
127 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
128 unsigned ColumnLimit) const;
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000129 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000130 WhitespaceManager &Whitespaces);
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000131 virtual void replaceWhitespaceBefore(unsigned LineIndex,
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000132 WhitespaceManager &Whitespaces);
133
134private:
135 // The prefix without an additional space if one was added.
136 StringRef OriginalPrefix;
Manuel Klimekde008c02013-05-27 15:23:34 +0000137};
138
139class BreakableBlockComment : public BreakableToken {
140public:
141 /// \brief Creates a breakable token for a block comment.
142 ///
143 /// \p StartColumn specifies the column in which the comment will start
144 /// after formatting, while \p OriginalStartColumn specifies in which
145 /// column the comment started before formatting.
146 /// If the comment starts a line after formatting, set \p FirstInLine to true.
147 BreakableBlockComment(const FormatStyle &Style, const FormatToken &Token,
148 unsigned StartColumn, unsigned OriginaStartColumn,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000149 bool FirstInLine, bool InPPDirective,
150 encoding::Encoding Encoding);
Manuel Klimekde008c02013-05-27 15:23:34 +0000151
152 virtual unsigned getLineCount() const;
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000153 virtual unsigned getLineLengthAfterSplit(unsigned LineIndex,
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000154 unsigned TailOffset,
155 StringRef::size_type Length) const;
Manuel Klimekde008c02013-05-27 15:23:34 +0000156 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
157 unsigned ColumnLimit) const;
158 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000159 WhitespaceManager &Whitespaces);
Manuel Klimekde008c02013-05-27 15:23:34 +0000160 virtual void replaceWhitespaceBefore(unsigned LineIndex,
Manuel Klimekde008c02013-05-27 15:23:34 +0000161 WhitespaceManager &Whitespaces);
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000162
163private:
Manuel Klimekde008c02013-05-27 15:23:34 +0000164 // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex],
165 // so that all whitespace between the lines is accounted to Lines[LineIndex]
166 // as leading whitespace:
167 // - Lines[LineIndex] points to the text after that whitespace
168 // - Lines[LineIndex-1] shrinks by its trailing whitespace
169 // - LeadingWhitespace[LineIndex] is updated with the complete whitespace
170 // between the end of the text of Lines[LineIndex-1] and Lines[LineIndex]
171 //
172 // Sets StartOfLineColumn to the intended column in which the text at
173 // Lines[LineIndex] starts (note that the decoration, if present, is not
174 // considered part of the text).
175 void adjustWhitespace(const FormatStyle &Style, unsigned LineIndex,
176 int IndentDelta);
Alexander Kornienko919398b2013-04-17 17:34:05 +0000177
Manuel Klimekde008c02013-05-27 15:23:34 +0000178 // Returns the column at which the text in line LineIndex starts, when broken
179 // at TailOffset. Note that the decoration (if present) is not considered part
180 // of the text.
181 unsigned getContentStartColumn(unsigned LineIndex, unsigned TailOffset) const;
Alexander Kornienko919398b2013-04-17 17:34:05 +0000182
Manuel Klimekde008c02013-05-27 15:23:34 +0000183 // Contains the text of the lines of the block comment, excluding the leading
184 // /* in the first line and trailing */ in the last line, and excluding all
185 // trailing whitespace between the lines. Note that the decoration (if
186 // present) is also not considered part of the text.
187 SmallVector<StringRef, 16> Lines;
188
189 // LeadingWhitespace[i] is the number of characters regarded as whitespace in
190 // front of Lines[i]. Note that this can include "* " sequences, which we
191 // regard as whitespace when all lines have a "*" prefix.
192 SmallVector<unsigned, 16> LeadingWhitespace;
193
194 // StartOfLineColumn[i] is the target column at which Line[i] should be.
195 // Note that this excludes a leading "* " or "*" in case all lines have
196 // a "*" prefix.
197 SmallVector<unsigned, 16> StartOfLineColumn;
198
199 // The column at which the text of a broken line should start.
200 // Note that an optional decoration would go before that column.
201 // IndentAtLineBreak is a uniform position for all lines in a block comment,
202 // regardless of their relative position.
203 // FIXME: Revisit the decision to do this; the main reason was to support
204 // patterns like
205 // /**************//**
206 // * Comment
207 // We could also support such patterns by special casing the first line
208 // instead.
209 unsigned IndentAtLineBreak;
210
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000211 // This is to distinguish between the case when the last line was empty and
212 // the case when it started with a decoration ("*" or "* ").
213 bool LastLineNeedsDecoration;
214
Manuel Klimekde008c02013-05-27 15:23:34 +0000215 // Either "* " if all lines begin with a "*", or empty.
216 StringRef Decoration;
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000217};
218
219} // namespace format
220} // namespace clang
221
222#endif // LLVM_CLANG_FORMAT_BREAKABLETOKEN_H