blob: fde89db864b18e65e45d0dd909227493a6c29e2c [file] [log] [blame]
Daniel Jasperde0328a2013-08-16 11:20:30 +00001//===--- ContinuationIndenter.h - Format C++ code ---------------*- C++ -*-===//
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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000011/// This file implements an indenter that manages the indentation of
Daniel Jasperde0328a2013-08-16 11:20:30 +000012/// continuations.
13///
14//===----------------------------------------------------------------------===//
15
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000016#ifndef LLVM_CLANG_LIB_FORMAT_CONTINUATIONINDENTER_H
17#define LLVM_CLANG_LIB_FORMAT_CONTINUATIONINDENTER_H
Daniel Jasperde0328a2013-08-16 11:20:30 +000018
19#include "Encoding.h"
Daniel Jasperd0ec0d62014-11-04 12:41:02 +000020#include "FormatToken.h"
Daniel Jasperde0328a2013-08-16 11:20:30 +000021#include "clang/Format/Format.h"
Alexander Kornienkoce9161a2014-01-02 15:13:14 +000022#include "llvm/Support/Regex.h"
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000023#include <map>
24#include <tuple>
Daniel Jasperde0328a2013-08-16 11:20:30 +000025
26namespace clang {
27class SourceManager;
28
29namespace format {
30
31class AnnotatedLine;
Manuel Klimek45ab5592017-11-14 09:19:53 +000032class BreakableToken;
Daniel Jasperde0328a2013-08-16 11:20:30 +000033struct FormatToken;
34struct LineState;
35struct ParenState;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000036struct RawStringFormatStyleManager;
Daniel Jasperde0328a2013-08-16 11:20:30 +000037class WhitespaceManager;
38
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000039struct RawStringFormatStyleManager {
40 llvm::StringMap<FormatStyle> DelimiterStyle;
Krasimir Georgiev2537e222018-01-17 16:17:26 +000041 llvm::StringMap<FormatStyle> EnclosingFunctionStyle;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000042
43 RawStringFormatStyleManager(const FormatStyle &CodeStyle);
44
Krasimir Georgiev2537e222018-01-17 16:17:26 +000045 llvm::Optional<FormatStyle> getDelimiterStyle(StringRef Delimiter) const;
46
47 llvm::Optional<FormatStyle>
48 getEnclosingFunctionStyle(StringRef EnclosingFunction) const;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000049};
50
Daniel Jasperde0328a2013-08-16 11:20:30 +000051class ContinuationIndenter {
52public:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000053 /// Constructs a \c ContinuationIndenter to format \p Line starting in
Daniel Jasperde0328a2013-08-16 11:20:30 +000054 /// column \p FirstIndent.
Daniel Jasperd0ec0d62014-11-04 12:41:02 +000055 ContinuationIndenter(const FormatStyle &Style,
56 const AdditionalKeywords &Keywords,
Eric Liu635423e2016-04-28 07:52:03 +000057 const SourceManager &SourceMgr,
58 WhitespaceManager &Whitespaces,
Daniel Jasperde0328a2013-08-16 11:20:30 +000059 encoding::Encoding Encoding,
60 bool BinPackInconclusiveFunctions);
61
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000062 /// Get the initial state, i.e. the state after placing \p Line's
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000063 /// first token at \p FirstIndent. When reformatting a fragment of code, as in
64 /// the case of formatting inside raw string literals, \p FirstStartColumn is
65 /// the column at which the state of the parent formatter is.
66 LineState getInitialState(unsigned FirstIndent, unsigned FirstStartColumn,
67 const AnnotatedLine *Line, bool DryRun);
Daniel Jasperde0328a2013-08-16 11:20:30 +000068
69 // FIXME: canBreak and mustBreak aren't strictly indentation-related. Find a
70 // better home.
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000071 /// Returns \c true, if a line break after \p State is allowed.
Daniel Jasperde0328a2013-08-16 11:20:30 +000072 bool canBreak(const LineState &State);
73
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000074 /// Returns \c true, if a line break after \p State is mandatory.
Daniel Jasperde0328a2013-08-16 11:20:30 +000075 bool mustBreak(const LineState &State);
76
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000077 /// Appends the next token to \p State and updates information
Daniel Jasperde0328a2013-08-16 11:20:30 +000078 /// necessary for indentation.
79 ///
80 /// Puts the token on the current line if \p Newline is \c false and adds a
81 /// line break and necessary indentation otherwise.
82 ///
83 /// If \p DryRun is \c false, also creates and stores the required
84 /// \c Replacement.
Daniel Jasper8de9ed02013-08-22 15:00:41 +000085 unsigned addTokenToState(LineState &State, bool Newline, bool DryRun,
86 unsigned ExtraSpaces = 0);
Daniel Jasperde0328a2013-08-16 11:20:30 +000087
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000088 /// Get the column limit for this line. This is the style's column
Daniel Jasperde0328a2013-08-16 11:20:30 +000089 /// limit, potentially reduced for preprocessor definitions.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000090 unsigned getColumnLimit(const LineState &State) const;
Daniel Jasperde0328a2013-08-16 11:20:30 +000091
92private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000093 /// Mark the next token as consumed in \p State and modify its stacks
Daniel Jasperde0328a2013-08-16 11:20:30 +000094 /// accordingly.
95 unsigned moveStateToNextToken(LineState &State, bool DryRun, bool Newline);
96
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000097 /// Update 'State' according to the next token's fake left parentheses.
Daniel Jasper60553be2014-05-26 13:10:39 +000098 void moveStatePastFakeLParens(LineState &State, bool Newline);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000099 /// Update 'State' according to the next token's fake r_parens.
Daniel Jasper60553be2014-05-26 13:10:39 +0000100 void moveStatePastFakeRParens(LineState &State);
101
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000102 /// Update 'State' according to the next token being one of "(<{[".
Daniel Jasper60553be2014-05-26 13:10:39 +0000103 void moveStatePastScopeOpener(LineState &State, bool Newline);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000104 /// Update 'State' according to the next token being one of ")>}]".
Daniel Jasper60553be2014-05-26 13:10:39 +0000105 void moveStatePastScopeCloser(LineState &State);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000106 /// Update 'State' with the next token opening a nested block.
Daniel Jasper60553be2014-05-26 13:10:39 +0000107 void moveStateToNewBlock(LineState &State);
108
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000109 /// Reformats a raw string literal.
Fangrui Song6907ce22018-07-30 19:24:48 +0000110 ///
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000111 /// \returns An extra penalty induced by reformatting the token.
112 unsigned reformatRawStringLiteral(const FormatToken &Current,
Manuel Klimek45ab5592017-11-14 09:19:53 +0000113 LineState &State,
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000114 const FormatStyle &RawStringStyle,
115 bool DryRun);
116
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000117 /// If the current token is at the end of the current line, handle
Manuel Klimek45ab5592017-11-14 09:19:53 +0000118 /// the transition to the next line.
119 unsigned handleEndOfLine(const FormatToken &Current, LineState &State,
120 bool DryRun, bool AllowBreak);
121
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000122 /// If \p Current is a raw string that is configured to be reformatted,
Manuel Klimek45ab5592017-11-14 09:19:53 +0000123 /// return the style to be used.
124 llvm::Optional<FormatStyle> getRawStringStyle(const FormatToken &Current,
125 const LineState &State);
126
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000127 /// If the current token sticks out over the end of the line, break
Daniel Jasperde0328a2013-08-16 11:20:30 +0000128 /// it if possible.
129 ///
Manuel Klimek0b58c322017-12-01 13:28:08 +0000130 /// \returns A pair (penalty, exceeded), where penalty is the extra penalty
131 /// when tokens are broken or lines exceed the column limit, and exceeded
132 /// indicates whether the algorithm purposefully left lines exceeding the
133 /// column limit.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000134 ///
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000135 /// The returned penalty will cover the cost of the additional line breaks
136 /// and column limit violation in all lines except for the last one. The
137 /// penalty for the column limit violation in the last line (and in single
138 /// line tokens) is handled in \c addNextStateToQueue.
Manuel Klimek0b58c322017-12-01 13:28:08 +0000139 ///
140 /// \p Strict indicates whether reflowing is allowed to leave characters
141 /// protruding the column limit; if true, lines will be split strictly within
142 /// the column limit where possible; if false, words are allowed to protrude
143 /// over the column limit as long as the penalty is less than the penalty
144 /// of a break.
145 std::pair<unsigned, bool> breakProtrudingToken(const FormatToken &Current,
146 LineState &State,
147 bool AllowBreak, bool DryRun,
148 bool Strict);
Manuel Klimek45ab5592017-11-14 09:19:53 +0000149
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000150 /// Returns the \c BreakableToken starting at \p Current, or nullptr
Manuel Klimek45ab5592017-11-14 09:19:53 +0000151 /// if the current token cannot be broken.
152 std::unique_ptr<BreakableToken>
153 createBreakableToken(const FormatToken &Current, LineState &State,
154 bool AllowBreak);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000155
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000156 /// Appends the next token to \p State and updates information
Alexander Kornienko1f803962013-10-01 14:41:18 +0000157 /// necessary for indentation.
158 ///
159 /// Puts the token on the current line.
160 ///
161 /// If \p DryRun is \c false, also creates and stores the required
162 /// \c Replacement.
Daniel Jasper48437ce2013-11-20 14:54:39 +0000163 void addTokenOnCurrentLine(LineState &State, bool DryRun,
164 unsigned ExtraSpaces);
Alexander Kornienko1f803962013-10-01 14:41:18 +0000165
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000166 /// Appends the next token to \p State and updates information
Alexander Kornienko1f803962013-10-01 14:41:18 +0000167 /// necessary for indentation.
168 ///
169 /// Adds a line break and necessary indentation.
170 ///
171 /// If \p DryRun is \c false, also creates and stores the required
172 /// \c Replacement.
173 unsigned addTokenOnNewLine(LineState &State, bool DryRun);
174
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000175 /// Calculate the new column for a line wrap before the next token.
Daniel Jasper9f388d02014-03-27 14:33:30 +0000176 unsigned getNewLineColumn(const LineState &State);
177
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000178 /// Adds a multiline token to the \p State.
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000179 ///
180 /// \returns Extra penalty for the first line of the literal: last line is
181 /// handled in \c addNextStateToQueue, and the penalty for other lines doesn't
182 /// matter, as we don't change them.
Alexander Kornienko917f9e02013-09-10 12:29:48 +0000183 unsigned addMultilineToken(const FormatToken &Current, LineState &State);
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000184
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000185 /// Returns \c true if the next token starts a multiline string
Daniel Jasperf438cb72013-08-23 11:57:34 +0000186 /// literal.
187 ///
188 /// This includes implicitly concatenated strings, strings that will be broken
189 /// by clang-format and string literals with escaped newlines.
Daniel Jasperc39b56f2013-12-16 07:23:08 +0000190 bool nextIsMultilineString(const LineState &State);
Daniel Jasperf438cb72013-08-23 11:57:34 +0000191
Daniel Jasperde0328a2013-08-16 11:20:30 +0000192 FormatStyle Style;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000193 const AdditionalKeywords &Keywords;
Eric Liu635423e2016-04-28 07:52:03 +0000194 const SourceManager &SourceMgr;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000195 WhitespaceManager &Whitespaces;
196 encoding::Encoding Encoding;
197 bool BinPackInconclusiveFunctions;
Alexander Kornienkoce9161a2014-01-02 15:13:14 +0000198 llvm::Regex CommentPragmasRegex;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000199 const RawStringFormatStyleManager RawStringFormats;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000200};
201
202struct ParenState {
Krasimir Georgiev8b4bc76a232018-05-09 09:02:11 +0000203 ParenState(const FormatToken *Tok, unsigned Indent, unsigned LastSpace,
204 bool AvoidBinPacking, bool NoLineBreak)
205 : Tok(Tok), Indent(Indent), LastSpace(LastSpace),
206 NestedBlockIndent(Indent), BreakBeforeClosingBrace(false),
207 AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
208 NoLineBreak(NoLineBreak), NoLineBreakInOperand(false),
209 LastOperatorWrapped(true), ContainsLineBreak(false),
210 ContainsUnwrappedBuilder(false), AlignColons(true),
211 ObjCSelectorNameFound(false), HasMultipleNestedBlocks(false),
212 NestedBlockInlined(false), IsInsideObjCArrayLiteral(false) {}
213
214 /// \brief The token opening this parenthesis level, or nullptr if this level
215 /// is opened by fake parenthesis.
216 ///
217 /// Not considered for memoization as it will always have the same value at
218 /// the same token.
219 const FormatToken *Tok;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000220
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000221 /// The position to which a specific parenthesis level needs to be
Daniel Jasperde0328a2013-08-16 11:20:30 +0000222 /// indented.
223 unsigned Indent;
224
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000225 /// The position of the last space on each level.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000226 ///
227 /// Used e.g. to break like:
228 /// functionCall(Parameter, otherCall(
229 /// OtherParameter));
230 unsigned LastSpace;
231
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000232 /// If a block relative to this parenthesis level gets wrapped, indent
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000233 /// it this much.
234 unsigned NestedBlockIndent;
235
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000236 /// The position the first "<<" operator encountered on each level.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000237 ///
238 /// Used to align "<<" operators. 0 if no such operator has been encountered
239 /// on a level.
Benjamin Kramer133aa202015-06-12 14:39:08 +0000240 unsigned FirstLessLess = 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000241
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000242 /// The column of a \c ? in a conditional expression;
Benjamin Kramer133aa202015-06-12 14:39:08 +0000243 unsigned QuestionColumn = 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000244
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000245 /// The position of the colon in an ObjC method declaration/call.
Benjamin Kramer133aa202015-06-12 14:39:08 +0000246 unsigned ColonPos = 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000247
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000248 /// The start of the most recent function in a builder-type call.
Benjamin Kramer133aa202015-06-12 14:39:08 +0000249 unsigned StartOfFunctionCall = 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000250
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000251 /// Contains the start of array subscript expressions, so that they
Daniel Jasperde0328a2013-08-16 11:20:30 +0000252 /// can be aligned.
Benjamin Kramer133aa202015-06-12 14:39:08 +0000253 unsigned StartOfArraySubscripts = 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000254
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000255 /// If a nested name specifier was broken over multiple lines, this
Daniel Jasperde0328a2013-08-16 11:20:30 +0000256 /// contains the start column of the second line. Otherwise 0.
Benjamin Kramer133aa202015-06-12 14:39:08 +0000257 unsigned NestedNameSpecifierContinuation = 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000258
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000259 /// If a call expression was broken over multiple lines, this
Daniel Jasperde0328a2013-08-16 11:20:30 +0000260 /// contains the start column of the second line. Otherwise 0.
Benjamin Kramer133aa202015-06-12 14:39:08 +0000261 unsigned CallContinuation = 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000262
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000263 /// The column of the first variable name in a variable declaration.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000264 ///
265 /// Used to align further variables if necessary.
Benjamin Kramer133aa202015-06-12 14:39:08 +0000266 unsigned VariablePos = 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000267
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000268 /// Whether a newline needs to be inserted before the block's closing
Benjamin Kramer8ad86fc2015-06-12 13:07:03 +0000269 /// brace.
270 ///
271 /// We only want to insert a newline before the closing brace if there also
272 /// was a newline after the beginning left brace.
273 bool BreakBeforeClosingBrace : 1;
274
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000275 /// Avoid bin packing, i.e. multiple parameters/elements on multiple
Benjamin Kramer8ad86fc2015-06-12 13:07:03 +0000276 /// lines, in this context.
277 bool AvoidBinPacking : 1;
278
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000279 /// Break after the next comma (or all the commas in this context if
Benjamin Kramer8ad86fc2015-06-12 13:07:03 +0000280 /// \c AvoidBinPacking is \c true).
281 bool BreakBeforeParameter : 1;
282
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000283 /// Line breaking in this context would break a formatting rule.
Benjamin Kramer8ad86fc2015-06-12 13:07:03 +0000284 bool NoLineBreak : 1;
285
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000286 /// Same as \c NoLineBreak, but is restricted until the end of the
Daniel Jasper240527c2017-01-16 13:13:15 +0000287 /// operand (including the next ",").
288 bool NoLineBreakInOperand : 1;
289
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000290 /// True if the last binary operator on this level was wrapped to the
Benjamin Kramer8ad86fc2015-06-12 13:07:03 +0000291 /// next line.
292 bool LastOperatorWrapped : 1;
293
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000294 /// \c true if this \c ParenState already contains a line-break.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000295 ///
296 /// The first line break in a certain \c ParenState causes extra penalty so
297 /// that clang-format prefers similar breaks, i.e. breaks in the same
298 /// parenthesis.
Benjamin Kramer8ad86fc2015-06-12 13:07:03 +0000299 bool ContainsLineBreak : 1;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000300
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000301 /// \c true if this \c ParenState contains multiple segments of a
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000302 /// builder-type call on one line.
Benjamin Kramer8ad86fc2015-06-12 13:07:03 +0000303 bool ContainsUnwrappedBuilder : 1;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000304
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000305 /// \c true if the colons of the curren ObjC method expression should
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000306 /// be aligned.
307 ///
308 /// Not considered for memoization as it will always have the same value at
309 /// the same token.
Benjamin Kramer8ad86fc2015-06-12 13:07:03 +0000310 bool AlignColons : 1;
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000311
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000312 /// \c true if at least one selector name was found in the current
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000313 /// ObjC method expression.
314 ///
315 /// Not considered for memoization as it will always have the same value at
316 /// the same token.
Benjamin Kramer8ad86fc2015-06-12 13:07:03 +0000317 bool ObjCSelectorNameFound : 1;
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000318
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000319 /// \c true if there are multiple nested blocks inside these parens.
Daniel Jasper3ae6f5a2014-04-09 12:08:39 +0000320 ///
321 /// Not considered for memoization as it will always have the same value at
322 /// the same token.
Benjamin Kramer8ad86fc2015-06-12 13:07:03 +0000323 bool HasMultipleNestedBlocks : 1;
Daniel Jasper3ae6f5a2014-04-09 12:08:39 +0000324
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000325 /// The start of a nested block (e.g. lambda introducer in C++ or
Jacek Olesiak022765d2018-02-06 12:12:00 +0000326 /// "function" in JavaScript) is not wrapped to a new line.
Benjamin Kramer8ad86fc2015-06-12 13:07:03 +0000327 bool NestedBlockInlined : 1;
Daniel Jasperb16b9692014-05-21 12:51:23 +0000328
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000329 /// \c true if the current \c ParenState represents an Objective-C
Ben Hamilton09051f22018-02-08 16:07:25 +0000330 /// array literal.
331 bool IsInsideObjCArrayLiteral : 1;
332
Daniel Jasperde0328a2013-08-16 11:20:30 +0000333 bool operator<(const ParenState &Other) const {
334 if (Indent != Other.Indent)
335 return Indent < Other.Indent;
336 if (LastSpace != Other.LastSpace)
337 return LastSpace < Other.LastSpace;
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000338 if (NestedBlockIndent != Other.NestedBlockIndent)
339 return NestedBlockIndent < Other.NestedBlockIndent;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000340 if (FirstLessLess != Other.FirstLessLess)
341 return FirstLessLess < Other.FirstLessLess;
342 if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
343 return BreakBeforeClosingBrace;
344 if (QuestionColumn != Other.QuestionColumn)
345 return QuestionColumn < Other.QuestionColumn;
346 if (AvoidBinPacking != Other.AvoidBinPacking)
347 return AvoidBinPacking;
348 if (BreakBeforeParameter != Other.BreakBeforeParameter)
349 return BreakBeforeParameter;
350 if (NoLineBreak != Other.NoLineBreak)
351 return NoLineBreak;
Daniel Jasperc0d606a2014-04-14 11:08:45 +0000352 if (LastOperatorWrapped != Other.LastOperatorWrapped)
353 return LastOperatorWrapped;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000354 if (ColonPos != Other.ColonPos)
355 return ColonPos < Other.ColonPos;
356 if (StartOfFunctionCall != Other.StartOfFunctionCall)
357 return StartOfFunctionCall < Other.StartOfFunctionCall;
358 if (StartOfArraySubscripts != Other.StartOfArraySubscripts)
359 return StartOfArraySubscripts < Other.StartOfArraySubscripts;
360 if (CallContinuation != Other.CallContinuation)
361 return CallContinuation < Other.CallContinuation;
362 if (VariablePos != Other.VariablePos)
363 return VariablePos < Other.VariablePos;
364 if (ContainsLineBreak != Other.ContainsLineBreak)
Daniel Jasperc83d7692015-06-09 11:39:22 +0000365 return ContainsLineBreak;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000366 if (ContainsUnwrappedBuilder != Other.ContainsUnwrappedBuilder)
Daniel Jasperc83d7692015-06-09 11:39:22 +0000367 return ContainsUnwrappedBuilder;
Daniel Jasper4b444492014-11-21 13:38:53 +0000368 if (NestedBlockInlined != Other.NestedBlockInlined)
Daniel Jasperc83d7692015-06-09 11:39:22 +0000369 return NestedBlockInlined;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000370 return false;
371 }
372};
373
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000374/// The current state when indenting a unwrapped line.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000375///
376/// As the indenting tries different combinations this is copied by value.
377struct LineState {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000378 /// The number of used columns in the current line.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000379 unsigned Column;
380
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000381 /// The token that needs to be next formatted.
Manuel Klimek71814b42013-10-11 21:25:45 +0000382 FormatToken *NextToken;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000383
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000384 /// \c true if this line contains a continued for-loop section.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000385 bool LineContainsContinuedForLoopSection;
386
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000387 /// \c true if \p NextToken should not continue this line.
Krasimir Georgiev35599fd2017-10-16 09:08:53 +0000388 bool NoContinuation;
389
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000390 /// The \c NestingLevel at the start of this line.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000391 unsigned StartOfLineLevel;
392
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000393 /// The lowest \c NestingLevel on the current line.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000394 unsigned LowestLevelOnLine;
395
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000396 /// The start column of the string literal, if we're in a string
Daniel Jasperde0328a2013-08-16 11:20:30 +0000397 /// literal sequence, 0 otherwise.
398 unsigned StartOfStringLiteral;
399
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000400 /// A stack keeping track of properties applying to parenthesis
Daniel Jasperde0328a2013-08-16 11:20:30 +0000401 /// levels.
402 std::vector<ParenState> Stack;
403
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000404 /// Ignore the stack of \c ParenStates for state comparison.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000405 ///
406 /// In long and deeply nested unwrapped lines, the current algorithm can
407 /// be insufficient for finding the best formatting with a reasonable amount
408 /// of time and memory. Setting this flag will effectively lead to the
409 /// algorithm not analyzing some combinations. However, these combinations
410 /// rarely contain the optimal solution: In short, accepting a higher
411 /// penalty early would need to lead to different values in the \c
412 /// ParenState stack (in an otherwise identical state) and these different
413 /// values would need to lead to a significant amount of avoided penalty
414 /// later.
415 ///
416 /// FIXME: Come up with a better algorithm instead.
417 bool IgnoreStackForComparison;
418
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000419 /// The indent of the first token.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000420 unsigned FirstIndent;
421
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000422 /// The line that is being formatted.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000423 ///
424 /// Does not need to be considered for memoization because it doesn't change.
425 const AnnotatedLine *Line;
426
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000427 /// Comparison operator to be able to used \c LineState in \c map.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000428 bool operator<(const LineState &Other) const {
429 if (NextToken != Other.NextToken)
430 return NextToken < Other.NextToken;
431 if (Column != Other.Column)
432 return Column < Other.Column;
433 if (LineContainsContinuedForLoopSection !=
434 Other.LineContainsContinuedForLoopSection)
435 return LineContainsContinuedForLoopSection;
Krasimir Georgiev35599fd2017-10-16 09:08:53 +0000436 if (NoContinuation != Other.NoContinuation)
437 return NoContinuation;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000438 if (StartOfLineLevel != Other.StartOfLineLevel)
439 return StartOfLineLevel < Other.StartOfLineLevel;
440 if (LowestLevelOnLine != Other.LowestLevelOnLine)
441 return LowestLevelOnLine < Other.LowestLevelOnLine;
442 if (StartOfStringLiteral != Other.StartOfStringLiteral)
443 return StartOfStringLiteral < Other.StartOfStringLiteral;
444 if (IgnoreStackForComparison || Other.IgnoreStackForComparison)
445 return false;
446 return Stack < Other.Stack;
447 }
448};
449
450} // end namespace format
451} // end namespace clang
452
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000453#endif