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