annotate clang/lib/Format/ContinuationIndenter.h @ 176:de4ac79aef9d

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 17:13:11 +0900
parents 0572611fdcc8
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- ContinuationIndenter.h - Format C++ code ---------------*- C++ -*-===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 ///
anatofuz
parents:
diff changeset
9 /// \file
anatofuz
parents:
diff changeset
10 /// This file implements an indenter that manages the indentation of
anatofuz
parents:
diff changeset
11 /// continuations.
anatofuz
parents:
diff changeset
12 ///
anatofuz
parents:
diff changeset
13 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
14
anatofuz
parents:
diff changeset
15 #ifndef LLVM_CLANG_LIB_FORMAT_CONTINUATIONINDENTER_H
anatofuz
parents:
diff changeset
16 #define LLVM_CLANG_LIB_FORMAT_CONTINUATIONINDENTER_H
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 #include "Encoding.h"
anatofuz
parents:
diff changeset
19 #include "FormatToken.h"
anatofuz
parents:
diff changeset
20 #include "clang/Format/Format.h"
anatofuz
parents:
diff changeset
21 #include "llvm/Support/Regex.h"
anatofuz
parents:
diff changeset
22 #include <map>
anatofuz
parents:
diff changeset
23 #include <tuple>
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 namespace clang {
anatofuz
parents:
diff changeset
26 class SourceManager;
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 namespace format {
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 class AnnotatedLine;
anatofuz
parents:
diff changeset
31 class BreakableToken;
anatofuz
parents:
diff changeset
32 struct FormatToken;
anatofuz
parents:
diff changeset
33 struct LineState;
anatofuz
parents:
diff changeset
34 struct ParenState;
anatofuz
parents:
diff changeset
35 struct RawStringFormatStyleManager;
anatofuz
parents:
diff changeset
36 class WhitespaceManager;
anatofuz
parents:
diff changeset
37
anatofuz
parents:
diff changeset
38 struct RawStringFormatStyleManager {
anatofuz
parents:
diff changeset
39 llvm::StringMap<FormatStyle> DelimiterStyle;
anatofuz
parents:
diff changeset
40 llvm::StringMap<FormatStyle> EnclosingFunctionStyle;
anatofuz
parents:
diff changeset
41
anatofuz
parents:
diff changeset
42 RawStringFormatStyleManager(const FormatStyle &CodeStyle);
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 llvm::Optional<FormatStyle> getDelimiterStyle(StringRef Delimiter) const;
anatofuz
parents:
diff changeset
45
anatofuz
parents:
diff changeset
46 llvm::Optional<FormatStyle>
anatofuz
parents:
diff changeset
47 getEnclosingFunctionStyle(StringRef EnclosingFunction) const;
anatofuz
parents:
diff changeset
48 };
anatofuz
parents:
diff changeset
49
anatofuz
parents:
diff changeset
50 class ContinuationIndenter {
anatofuz
parents:
diff changeset
51 public:
anatofuz
parents:
diff changeset
52 /// Constructs a \c ContinuationIndenter to format \p Line starting in
anatofuz
parents:
diff changeset
53 /// column \p FirstIndent.
anatofuz
parents:
diff changeset
54 ContinuationIndenter(const FormatStyle &Style,
anatofuz
parents:
diff changeset
55 const AdditionalKeywords &Keywords,
anatofuz
parents:
diff changeset
56 const SourceManager &SourceMgr,
anatofuz
parents:
diff changeset
57 WhitespaceManager &Whitespaces,
anatofuz
parents:
diff changeset
58 encoding::Encoding Encoding,
anatofuz
parents:
diff changeset
59 bool BinPackInconclusiveFunctions);
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61 /// Get the initial state, i.e. the state after placing \p Line's
anatofuz
parents:
diff changeset
62 /// first token at \p FirstIndent. When reformatting a fragment of code, as in
anatofuz
parents:
diff changeset
63 /// the case of formatting inside raw string literals, \p FirstStartColumn is
anatofuz
parents:
diff changeset
64 /// the column at which the state of the parent formatter is.
anatofuz
parents:
diff changeset
65 LineState getInitialState(unsigned FirstIndent, unsigned FirstStartColumn,
anatofuz
parents:
diff changeset
66 const AnnotatedLine *Line, bool DryRun);
anatofuz
parents:
diff changeset
67
anatofuz
parents:
diff changeset
68 // FIXME: canBreak and mustBreak aren't strictly indentation-related. Find a
anatofuz
parents:
diff changeset
69 // better home.
anatofuz
parents:
diff changeset
70 /// Returns \c true, if a line break after \p State is allowed.
anatofuz
parents:
diff changeset
71 bool canBreak(const LineState &State);
anatofuz
parents:
diff changeset
72
anatofuz
parents:
diff changeset
73 /// Returns \c true, if a line break after \p State is mandatory.
anatofuz
parents:
diff changeset
74 bool mustBreak(const LineState &State);
anatofuz
parents:
diff changeset
75
anatofuz
parents:
diff changeset
76 /// Appends the next token to \p State and updates information
anatofuz
parents:
diff changeset
77 /// necessary for indentation.
anatofuz
parents:
diff changeset
78 ///
anatofuz
parents:
diff changeset
79 /// Puts the token on the current line if \p Newline is \c false and adds a
anatofuz
parents:
diff changeset
80 /// line break and necessary indentation otherwise.
anatofuz
parents:
diff changeset
81 ///
anatofuz
parents:
diff changeset
82 /// If \p DryRun is \c false, also creates and stores the required
anatofuz
parents:
diff changeset
83 /// \c Replacement.
anatofuz
parents:
diff changeset
84 unsigned addTokenToState(LineState &State, bool Newline, bool DryRun,
anatofuz
parents:
diff changeset
85 unsigned ExtraSpaces = 0);
anatofuz
parents:
diff changeset
86
anatofuz
parents:
diff changeset
87 /// Get the column limit for this line. This is the style's column
anatofuz
parents:
diff changeset
88 /// limit, potentially reduced for preprocessor definitions.
anatofuz
parents:
diff changeset
89 unsigned getColumnLimit(const LineState &State) const;
anatofuz
parents:
diff changeset
90
anatofuz
parents:
diff changeset
91 private:
anatofuz
parents:
diff changeset
92 /// Mark the next token as consumed in \p State and modify its stacks
anatofuz
parents:
diff changeset
93 /// accordingly.
anatofuz
parents:
diff changeset
94 unsigned moveStateToNextToken(LineState &State, bool DryRun, bool Newline);
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 /// Update 'State' according to the next token's fake left parentheses.
anatofuz
parents:
diff changeset
97 void moveStatePastFakeLParens(LineState &State, bool Newline);
anatofuz
parents:
diff changeset
98 /// Update 'State' according to the next token's fake r_parens.
anatofuz
parents:
diff changeset
99 void moveStatePastFakeRParens(LineState &State);
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 /// Update 'State' according to the next token being one of "(<{[".
anatofuz
parents:
diff changeset
102 void moveStatePastScopeOpener(LineState &State, bool Newline);
anatofuz
parents:
diff changeset
103 /// Update 'State' according to the next token being one of ")>}]".
anatofuz
parents:
diff changeset
104 void moveStatePastScopeCloser(LineState &State);
anatofuz
parents:
diff changeset
105 /// Update 'State' with the next token opening a nested block.
anatofuz
parents:
diff changeset
106 void moveStateToNewBlock(LineState &State);
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 /// Reformats a raw string literal.
anatofuz
parents:
diff changeset
109 ///
anatofuz
parents:
diff changeset
110 /// \returns An extra penalty induced by reformatting the token.
anatofuz
parents:
diff changeset
111 unsigned reformatRawStringLiteral(const FormatToken &Current,
anatofuz
parents:
diff changeset
112 LineState &State,
anatofuz
parents:
diff changeset
113 const FormatStyle &RawStringStyle,
anatofuz
parents:
diff changeset
114 bool DryRun, bool Newline);
anatofuz
parents:
diff changeset
115
anatofuz
parents:
diff changeset
116 /// If the current token is at the end of the current line, handle
anatofuz
parents:
diff changeset
117 /// the transition to the next line.
anatofuz
parents:
diff changeset
118 unsigned handleEndOfLine(const FormatToken &Current, LineState &State,
anatofuz
parents:
diff changeset
119 bool DryRun, bool AllowBreak, bool Newline);
anatofuz
parents:
diff changeset
120
anatofuz
parents:
diff changeset
121 /// If \p Current is a raw string that is configured to be reformatted,
anatofuz
parents:
diff changeset
122 /// return the style to be used.
anatofuz
parents:
diff changeset
123 llvm::Optional<FormatStyle> getRawStringStyle(const FormatToken &Current,
anatofuz
parents:
diff changeset
124 const LineState &State);
anatofuz
parents:
diff changeset
125
anatofuz
parents:
diff changeset
126 /// If the current token sticks out over the end of the line, break
anatofuz
parents:
diff changeset
127 /// it if possible.
anatofuz
parents:
diff changeset
128 ///
anatofuz
parents:
diff changeset
129 /// \returns A pair (penalty, exceeded), where penalty is the extra penalty
anatofuz
parents:
diff changeset
130 /// when tokens are broken or lines exceed the column limit, and exceeded
anatofuz
parents:
diff changeset
131 /// indicates whether the algorithm purposefully left lines exceeding the
anatofuz
parents:
diff changeset
132 /// column limit.
anatofuz
parents:
diff changeset
133 ///
anatofuz
parents:
diff changeset
134 /// The returned penalty will cover the cost of the additional line breaks
anatofuz
parents:
diff changeset
135 /// and column limit violation in all lines except for the last one. The
anatofuz
parents:
diff changeset
136 /// penalty for the column limit violation in the last line (and in single
anatofuz
parents:
diff changeset
137 /// line tokens) is handled in \c addNextStateToQueue.
anatofuz
parents:
diff changeset
138 ///
anatofuz
parents:
diff changeset
139 /// \p Strict indicates whether reflowing is allowed to leave characters
anatofuz
parents:
diff changeset
140 /// protruding the column limit; if true, lines will be split strictly within
anatofuz
parents:
diff changeset
141 /// the column limit where possible; if false, words are allowed to protrude
anatofuz
parents:
diff changeset
142 /// over the column limit as long as the penalty is less than the penalty
anatofuz
parents:
diff changeset
143 /// of a break.
anatofuz
parents:
diff changeset
144 std::pair<unsigned, bool> breakProtrudingToken(const FormatToken &Current,
anatofuz
parents:
diff changeset
145 LineState &State,
anatofuz
parents:
diff changeset
146 bool AllowBreak, bool DryRun,
anatofuz
parents:
diff changeset
147 bool Strict);
anatofuz
parents:
diff changeset
148
anatofuz
parents:
diff changeset
149 /// Returns the \c BreakableToken starting at \p Current, or nullptr
anatofuz
parents:
diff changeset
150 /// if the current token cannot be broken.
anatofuz
parents:
diff changeset
151 std::unique_ptr<BreakableToken>
anatofuz
parents:
diff changeset
152 createBreakableToken(const FormatToken &Current, LineState &State,
anatofuz
parents:
diff changeset
153 bool AllowBreak);
anatofuz
parents:
diff changeset
154
anatofuz
parents:
diff changeset
155 /// Appends the next token to \p State and updates information
anatofuz
parents:
diff changeset
156 /// necessary for indentation.
anatofuz
parents:
diff changeset
157 ///
anatofuz
parents:
diff changeset
158 /// Puts the token on the current line.
anatofuz
parents:
diff changeset
159 ///
anatofuz
parents:
diff changeset
160 /// If \p DryRun is \c false, also creates and stores the required
anatofuz
parents:
diff changeset
161 /// \c Replacement.
anatofuz
parents:
diff changeset
162 void addTokenOnCurrentLine(LineState &State, bool DryRun,
anatofuz
parents:
diff changeset
163 unsigned ExtraSpaces);
anatofuz
parents:
diff changeset
164
anatofuz
parents:
diff changeset
165 /// Appends the next token to \p State and updates information
anatofuz
parents:
diff changeset
166 /// necessary for indentation.
anatofuz
parents:
diff changeset
167 ///
anatofuz
parents:
diff changeset
168 /// Adds a line break and necessary indentation.
anatofuz
parents:
diff changeset
169 ///
anatofuz
parents:
diff changeset
170 /// If \p DryRun is \c false, also creates and stores the required
anatofuz
parents:
diff changeset
171 /// \c Replacement.
anatofuz
parents:
diff changeset
172 unsigned addTokenOnNewLine(LineState &State, bool DryRun);
anatofuz
parents:
diff changeset
173
anatofuz
parents:
diff changeset
174 /// Calculate the new column for a line wrap before the next token.
anatofuz
parents:
diff changeset
175 unsigned getNewLineColumn(const LineState &State);
anatofuz
parents:
diff changeset
176
anatofuz
parents:
diff changeset
177 /// Adds a multiline token to the \p State.
anatofuz
parents:
diff changeset
178 ///
anatofuz
parents:
diff changeset
179 /// \returns Extra penalty for the first line of the literal: last line is
anatofuz
parents:
diff changeset
180 /// handled in \c addNextStateToQueue, and the penalty for other lines doesn't
anatofuz
parents:
diff changeset
181 /// matter, as we don't change them.
anatofuz
parents:
diff changeset
182 unsigned addMultilineToken(const FormatToken &Current, LineState &State);
anatofuz
parents:
diff changeset
183
anatofuz
parents:
diff changeset
184 /// Returns \c true if the next token starts a multiline string
anatofuz
parents:
diff changeset
185 /// literal.
anatofuz
parents:
diff changeset
186 ///
anatofuz
parents:
diff changeset
187 /// This includes implicitly concatenated strings, strings that will be broken
anatofuz
parents:
diff changeset
188 /// by clang-format and string literals with escaped newlines.
anatofuz
parents:
diff changeset
189 bool nextIsMultilineString(const LineState &State);
anatofuz
parents:
diff changeset
190
anatofuz
parents:
diff changeset
191 FormatStyle Style;
anatofuz
parents:
diff changeset
192 const AdditionalKeywords &Keywords;
anatofuz
parents:
diff changeset
193 const SourceManager &SourceMgr;
anatofuz
parents:
diff changeset
194 WhitespaceManager &Whitespaces;
anatofuz
parents:
diff changeset
195 encoding::Encoding Encoding;
anatofuz
parents:
diff changeset
196 bool BinPackInconclusiveFunctions;
anatofuz
parents:
diff changeset
197 llvm::Regex CommentPragmasRegex;
anatofuz
parents:
diff changeset
198 const RawStringFormatStyleManager RawStringFormats;
anatofuz
parents:
diff changeset
199 };
anatofuz
parents:
diff changeset
200
anatofuz
parents:
diff changeset
201 struct ParenState {
anatofuz
parents:
diff changeset
202 ParenState(const FormatToken *Tok, unsigned Indent, unsigned LastSpace,
anatofuz
parents:
diff changeset
203 bool AvoidBinPacking, bool NoLineBreak)
anatofuz
parents:
diff changeset
204 : Tok(Tok), Indent(Indent), LastSpace(LastSpace),
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
205 NestedBlockIndent(Indent), IsAligned(false),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
206 BreakBeforeClosingBrace(false), AvoidBinPacking(AvoidBinPacking),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
207 BreakBeforeParameter(false), NoLineBreak(NoLineBreak),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
208 NoLineBreakInOperand(false), LastOperatorWrapped(true),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
209 ContainsLineBreak(false), ContainsUnwrappedBuilder(false),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
210 AlignColons(true), ObjCSelectorNameFound(false),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
211 HasMultipleNestedBlocks(false), NestedBlockInlined(false),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
212 IsInsideObjCArrayLiteral(false), IsCSharpGenericTypeConstraint(false),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
213 IsChainedConditional(false), IsWrappedConditional(false),
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
214 UnindentOperator(false) {}
150
anatofuz
parents:
diff changeset
215
anatofuz
parents:
diff changeset
216 /// \brief The token opening this parenthesis level, or nullptr if this level
anatofuz
parents:
diff changeset
217 /// is opened by fake parenthesis.
anatofuz
parents:
diff changeset
218 ///
anatofuz
parents:
diff changeset
219 /// Not considered for memoization as it will always have the same value at
anatofuz
parents:
diff changeset
220 /// the same token.
anatofuz
parents:
diff changeset
221 const FormatToken *Tok;
anatofuz
parents:
diff changeset
222
anatofuz
parents:
diff changeset
223 /// The position to which a specific parenthesis level needs to be
anatofuz
parents:
diff changeset
224 /// indented.
anatofuz
parents:
diff changeset
225 unsigned Indent;
anatofuz
parents:
diff changeset
226
anatofuz
parents:
diff changeset
227 /// The position of the last space on each level.
anatofuz
parents:
diff changeset
228 ///
anatofuz
parents:
diff changeset
229 /// Used e.g. to break like:
anatofuz
parents:
diff changeset
230 /// functionCall(Parameter, otherCall(
anatofuz
parents:
diff changeset
231 /// OtherParameter));
anatofuz
parents:
diff changeset
232 unsigned LastSpace;
anatofuz
parents:
diff changeset
233
anatofuz
parents:
diff changeset
234 /// If a block relative to this parenthesis level gets wrapped, indent
anatofuz
parents:
diff changeset
235 /// it this much.
anatofuz
parents:
diff changeset
236 unsigned NestedBlockIndent;
anatofuz
parents:
diff changeset
237
anatofuz
parents:
diff changeset
238 /// The position the first "<<" operator encountered on each level.
anatofuz
parents:
diff changeset
239 ///
anatofuz
parents:
diff changeset
240 /// Used to align "<<" operators. 0 if no such operator has been encountered
anatofuz
parents:
diff changeset
241 /// on a level.
anatofuz
parents:
diff changeset
242 unsigned FirstLessLess = 0;
anatofuz
parents:
diff changeset
243
anatofuz
parents:
diff changeset
244 /// The column of a \c ? in a conditional expression;
anatofuz
parents:
diff changeset
245 unsigned QuestionColumn = 0;
anatofuz
parents:
diff changeset
246
anatofuz
parents:
diff changeset
247 /// The position of the colon in an ObjC method declaration/call.
anatofuz
parents:
diff changeset
248 unsigned ColonPos = 0;
anatofuz
parents:
diff changeset
249
anatofuz
parents:
diff changeset
250 /// The start of the most recent function in a builder-type call.
anatofuz
parents:
diff changeset
251 unsigned StartOfFunctionCall = 0;
anatofuz
parents:
diff changeset
252
anatofuz
parents:
diff changeset
253 /// Contains the start of array subscript expressions, so that they
anatofuz
parents:
diff changeset
254 /// can be aligned.
anatofuz
parents:
diff changeset
255 unsigned StartOfArraySubscripts = 0;
anatofuz
parents:
diff changeset
256
anatofuz
parents:
diff changeset
257 /// If a nested name specifier was broken over multiple lines, this
anatofuz
parents:
diff changeset
258 /// contains the start column of the second line. Otherwise 0.
anatofuz
parents:
diff changeset
259 unsigned NestedNameSpecifierContinuation = 0;
anatofuz
parents:
diff changeset
260
anatofuz
parents:
diff changeset
261 /// If a call expression was broken over multiple lines, this
anatofuz
parents:
diff changeset
262 /// contains the start column of the second line. Otherwise 0.
anatofuz
parents:
diff changeset
263 unsigned CallContinuation = 0;
anatofuz
parents:
diff changeset
264
anatofuz
parents:
diff changeset
265 /// The column of the first variable name in a variable declaration.
anatofuz
parents:
diff changeset
266 ///
anatofuz
parents:
diff changeset
267 /// Used to align further variables if necessary.
anatofuz
parents:
diff changeset
268 unsigned VariablePos = 0;
anatofuz
parents:
diff changeset
269
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
270 /// Whether this block's indentation is used for alignment.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
271 bool IsAligned : 1;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
272
150
anatofuz
parents:
diff changeset
273 /// Whether a newline needs to be inserted before the block's closing
anatofuz
parents:
diff changeset
274 /// brace.
anatofuz
parents:
diff changeset
275 ///
anatofuz
parents:
diff changeset
276 /// We only want to insert a newline before the closing brace if there also
anatofuz
parents:
diff changeset
277 /// was a newline after the beginning left brace.
anatofuz
parents:
diff changeset
278 bool BreakBeforeClosingBrace : 1;
anatofuz
parents:
diff changeset
279
anatofuz
parents:
diff changeset
280 /// Avoid bin packing, i.e. multiple parameters/elements on multiple
anatofuz
parents:
diff changeset
281 /// lines, in this context.
anatofuz
parents:
diff changeset
282 bool AvoidBinPacking : 1;
anatofuz
parents:
diff changeset
283
anatofuz
parents:
diff changeset
284 /// Break after the next comma (or all the commas in this context if
anatofuz
parents:
diff changeset
285 /// \c AvoidBinPacking is \c true).
anatofuz
parents:
diff changeset
286 bool BreakBeforeParameter : 1;
anatofuz
parents:
diff changeset
287
anatofuz
parents:
diff changeset
288 /// Line breaking in this context would break a formatting rule.
anatofuz
parents:
diff changeset
289 bool NoLineBreak : 1;
anatofuz
parents:
diff changeset
290
anatofuz
parents:
diff changeset
291 /// Same as \c NoLineBreak, but is restricted until the end of the
anatofuz
parents:
diff changeset
292 /// operand (including the next ",").
anatofuz
parents:
diff changeset
293 bool NoLineBreakInOperand : 1;
anatofuz
parents:
diff changeset
294
anatofuz
parents:
diff changeset
295 /// True if the last binary operator on this level was wrapped to the
anatofuz
parents:
diff changeset
296 /// next line.
anatofuz
parents:
diff changeset
297 bool LastOperatorWrapped : 1;
anatofuz
parents:
diff changeset
298
anatofuz
parents:
diff changeset
299 /// \c true if this \c ParenState already contains a line-break.
anatofuz
parents:
diff changeset
300 ///
anatofuz
parents:
diff changeset
301 /// The first line break in a certain \c ParenState causes extra penalty so
anatofuz
parents:
diff changeset
302 /// that clang-format prefers similar breaks, i.e. breaks in the same
anatofuz
parents:
diff changeset
303 /// parenthesis.
anatofuz
parents:
diff changeset
304 bool ContainsLineBreak : 1;
anatofuz
parents:
diff changeset
305
anatofuz
parents:
diff changeset
306 /// \c true if this \c ParenState contains multiple segments of a
anatofuz
parents:
diff changeset
307 /// builder-type call on one line.
anatofuz
parents:
diff changeset
308 bool ContainsUnwrappedBuilder : 1;
anatofuz
parents:
diff changeset
309
anatofuz
parents:
diff changeset
310 /// \c true if the colons of the curren ObjC method expression should
anatofuz
parents:
diff changeset
311 /// be aligned.
anatofuz
parents:
diff changeset
312 ///
anatofuz
parents:
diff changeset
313 /// Not considered for memoization as it will always have the same value at
anatofuz
parents:
diff changeset
314 /// the same token.
anatofuz
parents:
diff changeset
315 bool AlignColons : 1;
anatofuz
parents:
diff changeset
316
anatofuz
parents:
diff changeset
317 /// \c true if at least one selector name was found in the current
anatofuz
parents:
diff changeset
318 /// ObjC method expression.
anatofuz
parents:
diff changeset
319 ///
anatofuz
parents:
diff changeset
320 /// Not considered for memoization as it will always have the same value at
anatofuz
parents:
diff changeset
321 /// the same token.
anatofuz
parents:
diff changeset
322 bool ObjCSelectorNameFound : 1;
anatofuz
parents:
diff changeset
323
anatofuz
parents:
diff changeset
324 /// \c true if there are multiple nested blocks inside these parens.
anatofuz
parents:
diff changeset
325 ///
anatofuz
parents:
diff changeset
326 /// Not considered for memoization as it will always have the same value at
anatofuz
parents:
diff changeset
327 /// the same token.
anatofuz
parents:
diff changeset
328 bool HasMultipleNestedBlocks : 1;
anatofuz
parents:
diff changeset
329
anatofuz
parents:
diff changeset
330 /// The start of a nested block (e.g. lambda introducer in C++ or
anatofuz
parents:
diff changeset
331 /// "function" in JavaScript) is not wrapped to a new line.
anatofuz
parents:
diff changeset
332 bool NestedBlockInlined : 1;
anatofuz
parents:
diff changeset
333
anatofuz
parents:
diff changeset
334 /// \c true if the current \c ParenState represents an Objective-C
anatofuz
parents:
diff changeset
335 /// array literal.
anatofuz
parents:
diff changeset
336 bool IsInsideObjCArrayLiteral : 1;
anatofuz
parents:
diff changeset
337
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
338 bool IsCSharpGenericTypeConstraint : 1;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
339
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
340 /// \brief true if the current \c ParenState represents the false branch of
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
341 /// a chained conditional expression (e.g. else-if)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
342 bool IsChainedConditional : 1;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
343
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
344 /// \brief true if there conditionnal was wrapped on the first operator (the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
345 /// question mark)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
346 bool IsWrappedConditional : 1;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
347
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
348 /// \brief Indicates the indent should be reduced by the length of the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
349 /// operator.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
350 bool UnindentOperator : 1;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
351
150
anatofuz
parents:
diff changeset
352 bool operator<(const ParenState &Other) const {
anatofuz
parents:
diff changeset
353 if (Indent != Other.Indent)
anatofuz
parents:
diff changeset
354 return Indent < Other.Indent;
anatofuz
parents:
diff changeset
355 if (LastSpace != Other.LastSpace)
anatofuz
parents:
diff changeset
356 return LastSpace < Other.LastSpace;
anatofuz
parents:
diff changeset
357 if (NestedBlockIndent != Other.NestedBlockIndent)
anatofuz
parents:
diff changeset
358 return NestedBlockIndent < Other.NestedBlockIndent;
anatofuz
parents:
diff changeset
359 if (FirstLessLess != Other.FirstLessLess)
anatofuz
parents:
diff changeset
360 return FirstLessLess < Other.FirstLessLess;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
361 if (IsAligned != Other.IsAligned)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
362 return IsAligned;
150
anatofuz
parents:
diff changeset
363 if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
anatofuz
parents:
diff changeset
364 return BreakBeforeClosingBrace;
anatofuz
parents:
diff changeset
365 if (QuestionColumn != Other.QuestionColumn)
anatofuz
parents:
diff changeset
366 return QuestionColumn < Other.QuestionColumn;
anatofuz
parents:
diff changeset
367 if (AvoidBinPacking != Other.AvoidBinPacking)
anatofuz
parents:
diff changeset
368 return AvoidBinPacking;
anatofuz
parents:
diff changeset
369 if (BreakBeforeParameter != Other.BreakBeforeParameter)
anatofuz
parents:
diff changeset
370 return BreakBeforeParameter;
anatofuz
parents:
diff changeset
371 if (NoLineBreak != Other.NoLineBreak)
anatofuz
parents:
diff changeset
372 return NoLineBreak;
anatofuz
parents:
diff changeset
373 if (LastOperatorWrapped != Other.LastOperatorWrapped)
anatofuz
parents:
diff changeset
374 return LastOperatorWrapped;
anatofuz
parents:
diff changeset
375 if (ColonPos != Other.ColonPos)
anatofuz
parents:
diff changeset
376 return ColonPos < Other.ColonPos;
anatofuz
parents:
diff changeset
377 if (StartOfFunctionCall != Other.StartOfFunctionCall)
anatofuz
parents:
diff changeset
378 return StartOfFunctionCall < Other.StartOfFunctionCall;
anatofuz
parents:
diff changeset
379 if (StartOfArraySubscripts != Other.StartOfArraySubscripts)
anatofuz
parents:
diff changeset
380 return StartOfArraySubscripts < Other.StartOfArraySubscripts;
anatofuz
parents:
diff changeset
381 if (CallContinuation != Other.CallContinuation)
anatofuz
parents:
diff changeset
382 return CallContinuation < Other.CallContinuation;
anatofuz
parents:
diff changeset
383 if (VariablePos != Other.VariablePos)
anatofuz
parents:
diff changeset
384 return VariablePos < Other.VariablePos;
anatofuz
parents:
diff changeset
385 if (ContainsLineBreak != Other.ContainsLineBreak)
anatofuz
parents:
diff changeset
386 return ContainsLineBreak;
anatofuz
parents:
diff changeset
387 if (ContainsUnwrappedBuilder != Other.ContainsUnwrappedBuilder)
anatofuz
parents:
diff changeset
388 return ContainsUnwrappedBuilder;
anatofuz
parents:
diff changeset
389 if (NestedBlockInlined != Other.NestedBlockInlined)
anatofuz
parents:
diff changeset
390 return NestedBlockInlined;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
391 if (IsCSharpGenericTypeConstraint != Other.IsCSharpGenericTypeConstraint)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
392 return IsCSharpGenericTypeConstraint;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
393 if (IsChainedConditional != Other.IsChainedConditional)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
394 return IsChainedConditional;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
395 if (IsWrappedConditional != Other.IsWrappedConditional)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
396 return IsWrappedConditional;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
397 if (UnindentOperator != Other.UnindentOperator)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
398 return UnindentOperator;
150
anatofuz
parents:
diff changeset
399 return false;
anatofuz
parents:
diff changeset
400 }
anatofuz
parents:
diff changeset
401 };
anatofuz
parents:
diff changeset
402
anatofuz
parents:
diff changeset
403 /// The current state when indenting a unwrapped line.
anatofuz
parents:
diff changeset
404 ///
anatofuz
parents:
diff changeset
405 /// As the indenting tries different combinations this is copied by value.
anatofuz
parents:
diff changeset
406 struct LineState {
anatofuz
parents:
diff changeset
407 /// The number of used columns in the current line.
anatofuz
parents:
diff changeset
408 unsigned Column;
anatofuz
parents:
diff changeset
409
anatofuz
parents:
diff changeset
410 /// The token that needs to be next formatted.
anatofuz
parents:
diff changeset
411 FormatToken *NextToken;
anatofuz
parents:
diff changeset
412
anatofuz
parents:
diff changeset
413 /// \c true if this line contains a continued for-loop section.
anatofuz
parents:
diff changeset
414 bool LineContainsContinuedForLoopSection;
anatofuz
parents:
diff changeset
415
anatofuz
parents:
diff changeset
416 /// \c true if \p NextToken should not continue this line.
anatofuz
parents:
diff changeset
417 bool NoContinuation;
anatofuz
parents:
diff changeset
418
anatofuz
parents:
diff changeset
419 /// The \c NestingLevel at the start of this line.
anatofuz
parents:
diff changeset
420 unsigned StartOfLineLevel;
anatofuz
parents:
diff changeset
421
anatofuz
parents:
diff changeset
422 /// The lowest \c NestingLevel on the current line.
anatofuz
parents:
diff changeset
423 unsigned LowestLevelOnLine;
anatofuz
parents:
diff changeset
424
anatofuz
parents:
diff changeset
425 /// The start column of the string literal, if we're in a string
anatofuz
parents:
diff changeset
426 /// literal sequence, 0 otherwise.
anatofuz
parents:
diff changeset
427 unsigned StartOfStringLiteral;
anatofuz
parents:
diff changeset
428
anatofuz
parents:
diff changeset
429 /// A stack keeping track of properties applying to parenthesis
anatofuz
parents:
diff changeset
430 /// levels.
anatofuz
parents:
diff changeset
431 std::vector<ParenState> Stack;
anatofuz
parents:
diff changeset
432
anatofuz
parents:
diff changeset
433 /// Ignore the stack of \c ParenStates for state comparison.
anatofuz
parents:
diff changeset
434 ///
anatofuz
parents:
diff changeset
435 /// In long and deeply nested unwrapped lines, the current algorithm can
anatofuz
parents:
diff changeset
436 /// be insufficient for finding the best formatting with a reasonable amount
anatofuz
parents:
diff changeset
437 /// of time and memory. Setting this flag will effectively lead to the
anatofuz
parents:
diff changeset
438 /// algorithm not analyzing some combinations. However, these combinations
anatofuz
parents:
diff changeset
439 /// rarely contain the optimal solution: In short, accepting a higher
anatofuz
parents:
diff changeset
440 /// penalty early would need to lead to different values in the \c
anatofuz
parents:
diff changeset
441 /// ParenState stack (in an otherwise identical state) and these different
anatofuz
parents:
diff changeset
442 /// values would need to lead to a significant amount of avoided penalty
anatofuz
parents:
diff changeset
443 /// later.
anatofuz
parents:
diff changeset
444 ///
anatofuz
parents:
diff changeset
445 /// FIXME: Come up with a better algorithm instead.
anatofuz
parents:
diff changeset
446 bool IgnoreStackForComparison;
anatofuz
parents:
diff changeset
447
anatofuz
parents:
diff changeset
448 /// The indent of the first token.
anatofuz
parents:
diff changeset
449 unsigned FirstIndent;
anatofuz
parents:
diff changeset
450
anatofuz
parents:
diff changeset
451 /// The line that is being formatted.
anatofuz
parents:
diff changeset
452 ///
anatofuz
parents:
diff changeset
453 /// Does not need to be considered for memoization because it doesn't change.
anatofuz
parents:
diff changeset
454 const AnnotatedLine *Line;
anatofuz
parents:
diff changeset
455
anatofuz
parents:
diff changeset
456 /// Comparison operator to be able to used \c LineState in \c map.
anatofuz
parents:
diff changeset
457 bool operator<(const LineState &Other) const {
anatofuz
parents:
diff changeset
458 if (NextToken != Other.NextToken)
anatofuz
parents:
diff changeset
459 return NextToken < Other.NextToken;
anatofuz
parents:
diff changeset
460 if (Column != Other.Column)
anatofuz
parents:
diff changeset
461 return Column < Other.Column;
anatofuz
parents:
diff changeset
462 if (LineContainsContinuedForLoopSection !=
anatofuz
parents:
diff changeset
463 Other.LineContainsContinuedForLoopSection)
anatofuz
parents:
diff changeset
464 return LineContainsContinuedForLoopSection;
anatofuz
parents:
diff changeset
465 if (NoContinuation != Other.NoContinuation)
anatofuz
parents:
diff changeset
466 return NoContinuation;
anatofuz
parents:
diff changeset
467 if (StartOfLineLevel != Other.StartOfLineLevel)
anatofuz
parents:
diff changeset
468 return StartOfLineLevel < Other.StartOfLineLevel;
anatofuz
parents:
diff changeset
469 if (LowestLevelOnLine != Other.LowestLevelOnLine)
anatofuz
parents:
diff changeset
470 return LowestLevelOnLine < Other.LowestLevelOnLine;
anatofuz
parents:
diff changeset
471 if (StartOfStringLiteral != Other.StartOfStringLiteral)
anatofuz
parents:
diff changeset
472 return StartOfStringLiteral < Other.StartOfStringLiteral;
anatofuz
parents:
diff changeset
473 if (IgnoreStackForComparison || Other.IgnoreStackForComparison)
anatofuz
parents:
diff changeset
474 return false;
anatofuz
parents:
diff changeset
475 return Stack < Other.Stack;
anatofuz
parents:
diff changeset
476 }
anatofuz
parents:
diff changeset
477 };
anatofuz
parents:
diff changeset
478
anatofuz
parents:
diff changeset
479 } // end namespace format
anatofuz
parents:
diff changeset
480 } // end namespace clang
anatofuz
parents:
diff changeset
481
anatofuz
parents:
diff changeset
482 #endif