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