Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===--- Format.cpp - Format C++ code -------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file implements functions declared in Format.h. This will be |
| 12 | /// split into separate files as we go. |
| 13 | /// |
| 14 | /// This is EXPERIMENTAL code under heavy development. It is not in a state yet, |
| 15 | /// where it can be used to format real code. |
| 16 | /// |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "clang/Format/Format.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "UnwrappedLineParser.h" |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "clang/Lex/Lexer.h" |
| 23 | |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 26 | namespace clang { |
| 27 | namespace format { |
| 28 | |
| 29 | // FIXME: Move somewhere sane. |
| 30 | struct TokenAnnotation { |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 31 | enum TokenType { |
| 32 | TT_Unknown, |
| 33 | TT_TemplateOpener, |
| 34 | TT_TemplateCloser, |
| 35 | TT_BinaryOperator, |
| 36 | TT_UnaryOperator, |
| 37 | TT_OverloadedOperator, |
| 38 | TT_PointerOrReference, |
| 39 | TT_ConditionalExpr, |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 40 | TT_CtorInitializerColon, |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 41 | TT_LineComment, |
| 42 | TT_BlockComment |
| 43 | }; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 44 | |
| 45 | TokenType Type; |
| 46 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 47 | bool SpaceRequiredBefore; |
| 48 | bool CanBreakBefore; |
| 49 | bool MustBreakBefore; |
| 50 | }; |
| 51 | |
| 52 | using llvm::MutableArrayRef; |
| 53 | |
| 54 | FormatStyle getLLVMStyle() { |
| 55 | FormatStyle LLVMStyle; |
| 56 | LLVMStyle.ColumnLimit = 80; |
| 57 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
| 58 | LLVMStyle.PointerAndReferenceBindToType = false; |
| 59 | LLVMStyle.AccessModifierOffset = -2; |
| 60 | LLVMStyle.SplitTemplateClosingGreater = true; |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 61 | LLVMStyle.IndentCaseLabels = false; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 62 | return LLVMStyle; |
| 63 | } |
| 64 | |
| 65 | FormatStyle getGoogleStyle() { |
| 66 | FormatStyle GoogleStyle; |
| 67 | GoogleStyle.ColumnLimit = 80; |
| 68 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
| 69 | GoogleStyle.PointerAndReferenceBindToType = true; |
| 70 | GoogleStyle.AccessModifierOffset = -1; |
| 71 | GoogleStyle.SplitTemplateClosingGreater = false; |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 72 | GoogleStyle.IndentCaseLabels = true; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 73 | return GoogleStyle; |
| 74 | } |
| 75 | |
| 76 | struct OptimizationParameters { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 77 | unsigned PenaltyIndentLevel; |
| 78 | }; |
| 79 | |
| 80 | class UnwrappedLineFormatter { |
| 81 | public: |
| 82 | UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, |
| 83 | const UnwrappedLine &Line, |
| 84 | const std::vector<TokenAnnotation> &Annotations, |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 85 | tooling::Replacements &Replaces, bool StructuralError) |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 86 | : Style(Style), SourceMgr(SourceMgr), Line(Line), |
| 87 | Annotations(Annotations), Replaces(Replaces), |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 88 | StructuralError(StructuralError) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 89 | Parameters.PenaltyIndentLevel = 5; |
| 90 | } |
| 91 | |
| 92 | void format() { |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 93 | // Format first token and initialize indent. |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 94 | unsigned Indent = formatFirstToken(); |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 95 | |
| 96 | // Initialize state dependent on indent. |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 97 | IndentState State; |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 98 | State.Column = Indent; |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 99 | State.ConsumedTokens = 0; |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 100 | State.Indent.push_back(Indent + 4); |
| 101 | State.LastSpace.push_back(Indent); |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 102 | State.FirstLessLess.push_back(0); |
| 103 | |
| 104 | // The first token has already been indented and thus consumed. |
| 105 | moveStateToNextToken(State); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 106 | |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 107 | // Check whether the UnwrappedLine can be put onto a single line. If so, |
| 108 | // this is bound to be the optimal solution (by definition) and we don't |
| 109 | // need to analyze the entire solution space. |
| 110 | unsigned Columns = State.Column; |
| 111 | bool FitsOnALine = true; |
| 112 | for (unsigned i = 1, n = Line.Tokens.size(); i != n; ++i) { |
| 113 | Columns += (Annotations[i].SpaceRequiredBefore ? 1 : 0) + |
| 114 | Line.Tokens[i].Tok.getLength(); |
| 115 | // A special case for the colon of a constructor initializer as this only |
| 116 | // needs to be put on a new line if the line needs to be split. |
| 117 | if (Columns > Style.ColumnLimit || |
| 118 | (Annotations[i].MustBreakBefore && |
| 119 | Annotations[i].Type != TokenAnnotation::TT_CtorInitializerColon)) { |
| 120 | FitsOnALine = false; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 125 | // Start iterating at 1 as we have correctly formatted of Token #0 above. |
| 126 | for (unsigned i = 1, n = Line.Tokens.size(); i != n; ++i) { |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 127 | if (FitsOnALine) { |
| 128 | addTokenToState(false, false, State); |
| 129 | } else { |
| 130 | unsigned NoBreak = calcPenalty(State, false, UINT_MAX); |
| 131 | unsigned Break = calcPenalty(State, true, NoBreak); |
| 132 | addTokenToState(Break < NoBreak, false, State); |
| 133 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | /// \brief The current state when indenting a unwrapped line. |
| 139 | /// |
| 140 | /// As the indenting tries different combinations this is copied by value. |
| 141 | struct IndentState { |
| 142 | /// \brief The number of used columns in the current line. |
| 143 | unsigned Column; |
| 144 | |
| 145 | /// \brief The number of tokens already consumed. |
| 146 | unsigned ConsumedTokens; |
| 147 | |
| 148 | /// \brief The position to which a specific parenthesis level needs to be |
| 149 | /// indented. |
| 150 | std::vector<unsigned> Indent; |
| 151 | |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 152 | /// \brief The position of the last space on each level. |
| 153 | /// |
| 154 | /// Used e.g. to break like: |
| 155 | /// functionCall(Parameter, otherCall( |
| 156 | /// OtherParameter)); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 157 | std::vector<unsigned> LastSpace; |
| 158 | |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 159 | /// \brief The position the first "<<" operator encountered on each level. |
| 160 | /// |
| 161 | /// Used to align "<<" operators. 0 if no such operator has been encountered |
| 162 | /// on a level. |
| 163 | std::vector<unsigned> FirstLessLess; |
| 164 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 165 | /// \brief Comparison operator to be able to used \c IndentState in \c map. |
| 166 | bool operator<(const IndentState &Other) const { |
| 167 | if (Other.ConsumedTokens != ConsumedTokens) |
| 168 | return Other.ConsumedTokens > ConsumedTokens; |
| 169 | if (Other.Column != Column) |
| 170 | return Other.Column > Column; |
| 171 | if (Other.Indent.size() != Indent.size()) |
| 172 | return Other.Indent.size() > Indent.size(); |
| 173 | for (int i = 0, e = Indent.size(); i != e; ++i) { |
| 174 | if (Other.Indent[i] != Indent[i]) |
| 175 | return Other.Indent[i] > Indent[i]; |
| 176 | } |
| 177 | if (Other.LastSpace.size() != LastSpace.size()) |
| 178 | return Other.LastSpace.size() > LastSpace.size(); |
| 179 | for (int i = 0, e = LastSpace.size(); i != e; ++i) { |
| 180 | if (Other.LastSpace[i] != LastSpace[i]) |
| 181 | return Other.LastSpace[i] > LastSpace[i]; |
| 182 | } |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 183 | if (Other.FirstLessLess.size() != FirstLessLess.size()) |
| 184 | return Other.FirstLessLess.size() > FirstLessLess.size(); |
| 185 | for (int i = 0, e = FirstLessLess.size(); i != e; ++i) { |
| 186 | if (Other.FirstLessLess[i] != FirstLessLess[i]) |
| 187 | return Other.FirstLessLess[i] > FirstLessLess[i]; |
| 188 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | }; |
| 192 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 193 | /// \brief Appends the next token to \p State and updates information |
| 194 | /// necessary for indentation. |
| 195 | /// |
| 196 | /// Puts the token on the current line if \p Newline is \c true and adds a |
| 197 | /// line break and necessary indentation otherwise. |
| 198 | /// |
| 199 | /// If \p DryRun is \c false, also creates and stores the required |
| 200 | /// \c Replacement. |
| 201 | void addTokenToState(bool Newline, bool DryRun, IndentState &State) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 202 | unsigned Index = State.ConsumedTokens; |
| 203 | const FormatToken &Current = Line.Tokens[Index]; |
| 204 | const FormatToken &Previous = Line.Tokens[Index - 1]; |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 205 | unsigned ParenLevel = State.Indent.size() - 1; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 206 | |
| 207 | if (Newline) { |
| 208 | if (Current.Tok.is(tok::string_literal) && |
| 209 | Previous.Tok.is(tok::string_literal)) |
| 210 | State.Column = State.Column - Previous.Tok.getLength(); |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 211 | else if (Current.Tok.is(tok::lessless) && |
| 212 | State.FirstLessLess[ParenLevel] != 0) |
| 213 | State.Column = State.FirstLessLess[ParenLevel]; |
Daniel Jasper | 5485d0c | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 214 | else if (ParenLevel != 0 && |
| 215 | (Previous.Tok.is(tok::equal) || Current.Tok.is(tok::arrow) || |
| 216 | Current.Tok.is(tok::period))) |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 217 | // Indent and extra 4 spaces after '=' as it continues an expression. |
| 218 | // Don't do that on the top level, as we already indent 4 there. |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 219 | State.Column = State.Indent[ParenLevel] + 4; |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 220 | else |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 221 | State.Column = State.Indent[ParenLevel]; |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 222 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 223 | if (!DryRun) |
| 224 | replaceWhitespace(Current, 1, State.Column); |
| 225 | |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 226 | State.LastSpace[ParenLevel] = State.Indent[ParenLevel]; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 227 | if (Current.Tok.is(tok::colon) && |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 228 | Annotations[Index].Type != TokenAnnotation::TT_ConditionalExpr) |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 229 | State.Indent[ParenLevel] += 2; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 230 | } else { |
| 231 | unsigned Spaces = Annotations[Index].SpaceRequiredBefore ? 1 : 0; |
| 232 | if (Annotations[Index].Type == TokenAnnotation::TT_LineComment) |
| 233 | Spaces = 2; |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 234 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 235 | if (!DryRun) |
| 236 | replaceWhitespace(Current, 0, Spaces); |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 237 | |
| 238 | if (Previous.Tok.is(tok::l_paren) || |
| 239 | Annotations[Index - 1].Type == TokenAnnotation::TT_TemplateOpener) |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 240 | State.Indent[ParenLevel] = State.Column; |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 241 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 242 | // Top-level spaces are exempt as that mostly leads to better results. |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 243 | State.Column += Spaces; |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 244 | if (Spaces > 0 && ParenLevel != 0) |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 245 | State.LastSpace[ParenLevel] = State.Column; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 246 | } |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 247 | moveStateToNextToken(State); |
| 248 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 249 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 250 | /// \brief Mark the next token as consumed in \p State and modify its stacks |
| 251 | /// accordingly. |
| 252 | void moveStateToNextToken(IndentState &State) { |
| 253 | unsigned Index = State.ConsumedTokens; |
| 254 | const FormatToken &Current = Line.Tokens[Index]; |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 255 | unsigned ParenLevel = State.Indent.size() - 1; |
| 256 | |
| 257 | if (Current.Tok.is(tok::lessless) && State.FirstLessLess[ParenLevel] == 0) |
| 258 | State.FirstLessLess[ParenLevel] = State.Column; |
| 259 | |
| 260 | State.Column += Current.Tok.getLength(); |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 261 | |
| 262 | // If we encounter an opening (, [ or <, we add a level to our stacks to |
| 263 | // prepare for the following tokens. |
| 264 | if (Current.Tok.is(tok::l_paren) || Current.Tok.is(tok::l_square) || |
| 265 | Annotations[Index].Type == TokenAnnotation::TT_TemplateOpener) { |
| 266 | State.Indent.push_back(4 + State.LastSpace.back()); |
| 267 | State.LastSpace.push_back(State.LastSpace.back()); |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 268 | State.FirstLessLess.push_back(0); |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // If we encounter a closing ), ] or >, we can remove a level from our |
| 272 | // stacks. |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 273 | if (Current.Tok.is(tok::r_paren) || Current.Tok.is(tok::r_square) || |
| 274 | Annotations[Index].Type == TokenAnnotation::TT_TemplateCloser) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 275 | State.Indent.pop_back(); |
| 276 | State.LastSpace.pop_back(); |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 277 | State.FirstLessLess.pop_back(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | ++State.ConsumedTokens; |
| 281 | } |
| 282 | |
Daniel Jasper | 5485d0c | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 283 | /// \brief Calculate the panelty for splitting after the token at \p Index. |
| 284 | unsigned splitPenalty(unsigned Index) { |
| 285 | assert(Index < Line.Tokens.size() && |
| 286 | "Tried to calculate penalty for splitting after the last token"); |
| 287 | const FormatToken &Left = Line.Tokens[Index]; |
| 288 | const FormatToken &Right = Line.Tokens[Index + 1]; |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 289 | if (Left.Tok.is(tok::semi) || Left.Tok.is(tok::comma)) |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 290 | return 0; |
Daniel Jasper | 5485d0c | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 291 | if (Left.Tok.is(tok::equal) || Left.Tok.is(tok::l_paren) || |
| 292 | Left.Tok.is(tok::pipepipe) || Left.Tok.is(tok::ampamp)) |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 293 | return 2; |
Daniel Jasper | 5485d0c | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 294 | |
| 295 | if (Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period)) |
| 296 | return 200; |
| 297 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 298 | return 3; |
| 299 | } |
| 300 | |
| 301 | /// \brief Calculate the number of lines needed to format the remaining part |
| 302 | /// of the unwrapped line. |
| 303 | /// |
| 304 | /// Assumes the formatting so far has led to |
| 305 | /// the \c IndentState \p State. If \p NewLine is set, a new line will be |
| 306 | /// added after the previous token. |
| 307 | /// |
| 308 | /// \param StopAt is used for optimization. If we can determine that we'll |
| 309 | /// definitely need at least \p StopAt additional lines, we already know of a |
| 310 | /// better solution. |
| 311 | unsigned calcPenalty(IndentState State, bool NewLine, unsigned StopAt) { |
| 312 | // We are at the end of the unwrapped line, so we don't need any more lines. |
| 313 | if (State.ConsumedTokens >= Line.Tokens.size()) |
| 314 | return 0; |
| 315 | |
| 316 | if (!NewLine && Annotations[State.ConsumedTokens].MustBreakBefore) |
| 317 | return UINT_MAX; |
| 318 | if (NewLine && !Annotations[State.ConsumedTokens].CanBreakBefore) |
| 319 | return UINT_MAX; |
| 320 | |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 321 | unsigned CurrentPenalty = 0; |
| 322 | if (NewLine) { |
| 323 | CurrentPenalty += Parameters.PenaltyIndentLevel * State.Indent.size() + |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 324 | splitPenalty(State.ConsumedTokens - 1); |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 327 | addTokenToState(NewLine, true, State); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 328 | |
| 329 | // Exceeding column limit is bad. |
| 330 | if (State.Column > Style.ColumnLimit) |
| 331 | return UINT_MAX; |
| 332 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 333 | if (StopAt <= CurrentPenalty) |
| 334 | return UINT_MAX; |
| 335 | StopAt -= CurrentPenalty; |
| 336 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 337 | StateMap::iterator I = Memory.find(State); |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 338 | if (I != Memory.end()) { |
| 339 | // If this state has already been examined, we can safely return the |
| 340 | // previous result if we |
| 341 | // - have not hit the optimatization (and thus returned UINT_MAX) OR |
| 342 | // - are now computing for a smaller or equal StopAt. |
| 343 | unsigned SavedResult = I->second.first; |
| 344 | unsigned SavedStopAt = I->second.second; |
Daniel Jasper | 5485d0c | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 345 | if (SavedResult != UINT_MAX) |
| 346 | return SavedResult + CurrentPenalty; |
| 347 | else if (StopAt <= SavedStopAt) |
| 348 | return UINT_MAX; |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 349 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 350 | |
| 351 | unsigned NoBreak = calcPenalty(State, false, StopAt); |
| 352 | unsigned WithBreak = calcPenalty(State, true, std::min(StopAt, NoBreak)); |
| 353 | unsigned Result = std::min(NoBreak, WithBreak); |
Daniel Jasper | 5485d0c | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 354 | |
| 355 | // We have to store 'Result' without adding 'CurrentPenalty' as the latter |
| 356 | // can depend on 'NewLine'. |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 357 | Memory[State] = std::pair<unsigned, unsigned>(Result, StopAt); |
Daniel Jasper | 5485d0c | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 358 | |
| 359 | return Result == UINT_MAX ? UINT_MAX : Result + CurrentPenalty; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /// \brief Replaces the whitespace in front of \p Tok. Only call once for |
| 363 | /// each \c FormatToken. |
| 364 | void replaceWhitespace(const FormatToken &Tok, unsigned NewLines, |
| 365 | unsigned Spaces) { |
| 366 | Replaces.insert(tooling::Replacement( |
| 367 | SourceMgr, Tok.WhiteSpaceStart, Tok.WhiteSpaceLength, |
| 368 | std::string(NewLines, '\n') + std::string(Spaces, ' '))); |
| 369 | } |
| 370 | |
| 371 | /// \brief Add a new line and the required indent before the first Token |
Alexander Kornienko | bc09a7e | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 372 | /// of the \c UnwrappedLine if there was no structural parsing error. |
| 373 | /// Returns the indent level of the \c UnwrappedLine. |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 374 | unsigned formatFirstToken() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 375 | const FormatToken &Token = Line.Tokens[0]; |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 376 | if (!Token.WhiteSpaceStart.isValid() || StructuralError) |
| 377 | return SourceMgr.getSpellingColumnNumber(Token.Tok.getLocation()) - 1; |
| 378 | |
| 379 | unsigned Newlines = |
| 380 | std::min(Token.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); |
| 381 | unsigned Offset = SourceMgr.getFileOffset(Token.WhiteSpaceStart); |
| 382 | if (Newlines == 0 && Offset != 0) |
| 383 | Newlines = 1; |
| 384 | unsigned Indent = Line.Level * 2; |
Alexander Kornienko | 2ca766f | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 385 | if ((Token.Tok.is(tok::kw_public) || Token.Tok.is(tok::kw_protected) || |
| 386 | Token.Tok.is(tok::kw_private)) && |
| 387 | static_cast<int>(Indent) + Style.AccessModifierOffset >= 0) |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 388 | Indent += Style.AccessModifierOffset; |
| 389 | replaceWhitespace(Token, Newlines, Indent); |
| 390 | return Indent; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | FormatStyle Style; |
| 394 | SourceManager &SourceMgr; |
| 395 | const UnwrappedLine &Line; |
| 396 | const std::vector<TokenAnnotation> &Annotations; |
| 397 | tooling::Replacements &Replaces; |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 398 | bool StructuralError; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 399 | |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 400 | // A map from an indent state to a pair (Result, Used-StopAt). |
| 401 | typedef std::map<IndentState, std::pair<unsigned, unsigned> > StateMap; |
| 402 | StateMap Memory; |
| 403 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 404 | OptimizationParameters Parameters; |
| 405 | }; |
| 406 | |
| 407 | /// \brief Determines extra information about the tokens comprising an |
| 408 | /// \c UnwrappedLine. |
| 409 | class TokenAnnotator { |
| 410 | public: |
| 411 | TokenAnnotator(const UnwrappedLine &Line, const FormatStyle &Style, |
| 412 | SourceManager &SourceMgr) |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 413 | : Line(Line), Style(Style), SourceMgr(SourceMgr) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /// \brief A parser that gathers additional information about tokens. |
| 417 | /// |
| 418 | /// The \c TokenAnnotator tries to matches parenthesis and square brakets and |
| 419 | /// store a parenthesis levels. It also tries to resolve matching "<" and ">" |
| 420 | /// into template parameter lists. |
| 421 | class AnnotatingParser { |
| 422 | public: |
Manuel Klimek | 6a5619d | 2012-12-03 20:55:42 +0000 | [diff] [blame] | 423 | AnnotatingParser(const SmallVector<FormatToken, 16> &Tokens, |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 424 | std::vector<TokenAnnotation> &Annotations) |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 425 | : Tokens(Tokens), Annotations(Annotations), Index(0) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 428 | bool parseAngle() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 429 | while (Index < Tokens.size()) { |
| 430 | if (Tokens[Index].Tok.is(tok::greater)) { |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 431 | Annotations[Index].Type = TokenAnnotation::TT_TemplateCloser; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 432 | next(); |
| 433 | return true; |
| 434 | } |
| 435 | if (Tokens[Index].Tok.is(tok::r_paren) || |
| 436 | Tokens[Index].Tok.is(tok::r_square)) |
| 437 | return false; |
| 438 | if (Tokens[Index].Tok.is(tok::pipepipe) || |
| 439 | Tokens[Index].Tok.is(tok::ampamp) || |
| 440 | Tokens[Index].Tok.is(tok::question) || |
| 441 | Tokens[Index].Tok.is(tok::colon)) |
| 442 | return false; |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 443 | consumeToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 444 | } |
| 445 | return false; |
| 446 | } |
| 447 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 448 | bool parseParens() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 449 | while (Index < Tokens.size()) { |
| 450 | if (Tokens[Index].Tok.is(tok::r_paren)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 451 | next(); |
| 452 | return true; |
| 453 | } |
| 454 | if (Tokens[Index].Tok.is(tok::r_square)) |
| 455 | return false; |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 456 | consumeToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 457 | } |
| 458 | return false; |
| 459 | } |
| 460 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 461 | bool parseSquare() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 462 | while (Index < Tokens.size()) { |
| 463 | if (Tokens[Index].Tok.is(tok::r_square)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 464 | next(); |
| 465 | return true; |
| 466 | } |
| 467 | if (Tokens[Index].Tok.is(tok::r_paren)) |
| 468 | return false; |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 469 | consumeToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 470 | } |
| 471 | return false; |
| 472 | } |
| 473 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 474 | bool parseConditional() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 475 | while (Index < Tokens.size()) { |
| 476 | if (Tokens[Index].Tok.is(tok::colon)) { |
| 477 | Annotations[Index].Type = TokenAnnotation::TT_ConditionalExpr; |
| 478 | next(); |
| 479 | return true; |
| 480 | } |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 481 | consumeToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 482 | } |
| 483 | return false; |
| 484 | } |
| 485 | |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 486 | void consumeToken() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 487 | unsigned CurrentIndex = Index; |
| 488 | next(); |
| 489 | switch (Tokens[CurrentIndex].Tok.getKind()) { |
| 490 | case tok::l_paren: |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 491 | parseParens(); |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 492 | if (Index < Tokens.size() && Tokens[Index].Tok.is(tok::colon)) { |
| 493 | Annotations[Index].Type = TokenAnnotation::TT_CtorInitializerColon; |
| 494 | next(); |
| 495 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 496 | break; |
| 497 | case tok::l_square: |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 498 | parseSquare(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 499 | break; |
| 500 | case tok::less: |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 501 | if (parseAngle()) |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 502 | Annotations[CurrentIndex].Type = TokenAnnotation::TT_TemplateOpener; |
| 503 | else { |
| 504 | Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator; |
| 505 | Index = CurrentIndex + 1; |
| 506 | } |
| 507 | break; |
| 508 | case tok::greater: |
| 509 | Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator; |
| 510 | break; |
| 511 | case tok::kw_operator: |
| 512 | if (!Tokens[Index].Tok.is(tok::l_paren)) |
| 513 | Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator; |
| 514 | next(); |
| 515 | break; |
| 516 | case tok::question: |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 517 | parseConditional(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 518 | break; |
| 519 | default: |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | void parseLine() { |
| 525 | while (Index < Tokens.size()) { |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 526 | consumeToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | void next() { |
| 531 | ++Index; |
| 532 | } |
| 533 | |
| 534 | private: |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 535 | const SmallVector<FormatToken, 16> &Tokens; |
| 536 | std::vector<TokenAnnotation> &Annotations; |
| 537 | unsigned Index; |
| 538 | }; |
| 539 | |
| 540 | void annotate() { |
| 541 | Annotations.clear(); |
| 542 | for (int i = 0, e = Line.Tokens.size(); i != e; ++i) { |
| 543 | Annotations.push_back(TokenAnnotation()); |
| 544 | } |
| 545 | |
Manuel Klimek | 6a5619d | 2012-12-03 20:55:42 +0000 | [diff] [blame] | 546 | AnnotatingParser Parser(Line.Tokens, Annotations); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 547 | Parser.parseLine(); |
| 548 | |
| 549 | determineTokenTypes(); |
| 550 | |
| 551 | for (int i = 1, e = Line.Tokens.size(); i != e; ++i) { |
| 552 | TokenAnnotation &Annotation = Annotations[i]; |
| 553 | |
| 554 | Annotation.CanBreakBefore = |
| 555 | canBreakBetween(Line.Tokens[i - 1], Line.Tokens[i]); |
| 556 | |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 557 | if (Annotation.Type == TokenAnnotation::TT_CtorInitializerColon) { |
| 558 | Annotation.MustBreakBefore = true; |
| 559 | Annotation.SpaceRequiredBefore = true; |
| 560 | } else if (Line.Tokens[i].Tok.is(tok::colon)) { |
Daniel Jasper | 55b6b64 | 2012-12-05 16:24:48 +0000 | [diff] [blame] | 561 | Annotation.SpaceRequiredBefore = |
| 562 | Line.Tokens[0].Tok.isNot(tok::kw_case) && i != e - 1; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 563 | } else if (Annotations[i - 1].Type == TokenAnnotation::TT_UnaryOperator) { |
| 564 | Annotation.SpaceRequiredBefore = false; |
| 565 | } else if (Annotation.Type == TokenAnnotation::TT_UnaryOperator) { |
| 566 | Annotation.SpaceRequiredBefore = |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 567 | Line.Tokens[i - 1].Tok.isNot(tok::l_paren) && |
| 568 | Line.Tokens[i - 1].Tok.isNot(tok::l_square); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 569 | } else if (Line.Tokens[i - 1].Tok.is(tok::greater) && |
| 570 | Line.Tokens[i].Tok.is(tok::greater)) { |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 571 | if (Annotation.Type == TokenAnnotation::TT_TemplateCloser && |
Daniel Jasper | 6021c4a | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 572 | Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser) |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 573 | Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 574 | else |
| 575 | Annotation.SpaceRequiredBefore = false; |
| 576 | } else if ( |
| 577 | Annotation.Type == TokenAnnotation::TT_BinaryOperator || |
| 578 | Annotations[i - 1].Type == TokenAnnotation::TT_BinaryOperator) { |
| 579 | Annotation.SpaceRequiredBefore = true; |
| 580 | } else if ( |
Daniel Jasper | 9b15547 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 581 | Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser && |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 582 | Line.Tokens[i].Tok.is(tok::l_paren)) { |
| 583 | Annotation.SpaceRequiredBefore = false; |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 584 | } else if (Line.Tokens[i].Tok.is(tok::less) && |
| 585 | Line.Tokens[0].Tok.is(tok::hash)) { |
| 586 | Annotation.SpaceRequiredBefore = true; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 587 | } else { |
| 588 | Annotation.SpaceRequiredBefore = |
| 589 | spaceRequiredBetween(Line.Tokens[i - 1].Tok, Line.Tokens[i].Tok); |
| 590 | } |
| 591 | |
| 592 | if (Annotations[i - 1].Type == TokenAnnotation::TT_LineComment || |
| 593 | (Line.Tokens[i].Tok.is(tok::string_literal) && |
| 594 | Line.Tokens[i - 1].Tok.is(tok::string_literal))) { |
| 595 | Annotation.MustBreakBefore = true; |
| 596 | } |
| 597 | |
| 598 | if (Annotation.MustBreakBefore) |
| 599 | Annotation.CanBreakBefore = true; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | const std::vector<TokenAnnotation> &getAnnotations() { |
| 604 | return Annotations; |
| 605 | } |
| 606 | |
| 607 | private: |
| 608 | void determineTokenTypes() { |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 609 | bool AssignmentEncountered = false; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 610 | for (int i = 0, e = Line.Tokens.size(); i != e; ++i) { |
| 611 | TokenAnnotation &Annotation = Annotations[i]; |
| 612 | const FormatToken &Tok = Line.Tokens[i]; |
| 613 | |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 614 | if (Tok.Tok.is(tok::equal) || Tok.Tok.is(tok::plusequal) || |
| 615 | Tok.Tok.is(tok::minusequal) || Tok.Tok.is(tok::starequal) || |
| 616 | Tok.Tok.is(tok::slashequal)) |
| 617 | AssignmentEncountered = true; |
Daniel Jasper | 426702d | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 618 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 619 | if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp)) |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 620 | Annotation.Type = determineStarAmpUsage(i, AssignmentEncountered); |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 621 | else if (isUnaryOperator(i)) |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 622 | Annotation.Type = TokenAnnotation::TT_UnaryOperator; |
| 623 | else if (isBinaryOperator(Line.Tokens[i])) |
| 624 | Annotation.Type = TokenAnnotation::TT_BinaryOperator; |
| 625 | else if (Tok.Tok.is(tok::comment)) { |
| 626 | StringRef Data(SourceMgr.getCharacterData(Tok.Tok.getLocation()), |
| 627 | Tok.Tok.getLength()); |
| 628 | if (Data.startswith("//")) |
| 629 | Annotation.Type = TokenAnnotation::TT_LineComment; |
| 630 | else |
| 631 | Annotation.Type = TokenAnnotation::TT_BlockComment; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 636 | bool isUnaryOperator(unsigned Index) { |
| 637 | const Token &Tok = Line.Tokens[Index].Tok; |
Daniel Jasper | 26333c3 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 638 | |
| 639 | // '++', '--' and '!' are always unary operators. |
| 640 | if (Tok.is(tok::minusminus) || Tok.is(tok::plusplus) || |
| 641 | Tok.is(tok::exclaim)) |
| 642 | return true; |
| 643 | |
| 644 | // The other possible unary operators are '+' and '-' as we |
| 645 | // determine the usage of '*' and '&' in determineStarAmpUsage(). |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 646 | if (Tok.isNot(tok::minus) && Tok.isNot(tok::plus)) |
| 647 | return false; |
Daniel Jasper | 26333c3 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 648 | |
| 649 | // Use heuristics to recognize unary operators. |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 650 | const Token &PreviousTok = Line.Tokens[Index - 1].Tok; |
| 651 | if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) || |
| 652 | PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square)) |
| 653 | return true; |
Daniel Jasper | 26333c3 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 654 | |
| 655 | // Fall back to marking the token as binary operator. |
Daniel Jasper | 8b52971 | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 656 | return Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator; |
| 657 | } |
| 658 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 659 | bool isBinaryOperator(const FormatToken &Tok) { |
| 660 | switch (Tok.Tok.getKind()) { |
| 661 | case tok::equal: |
| 662 | case tok::equalequal: |
Daniel Jasper | 426702d | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 663 | case tok::exclaimequal: |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 664 | case tok::star: |
| 665 | //case tok::amp: |
| 666 | case tok::plus: |
| 667 | case tok::slash: |
| 668 | case tok::minus: |
| 669 | case tok::ampamp: |
| 670 | case tok::pipe: |
| 671 | case tok::pipepipe: |
| 672 | case tok::percent: |
| 673 | return true; |
| 674 | default: |
| 675 | return false; |
| 676 | } |
| 677 | } |
| 678 | |
Daniel Jasper | 426702d | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 679 | TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index, |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 680 | bool AssignmentEncountered) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 681 | if (Index == Annotations.size()) |
| 682 | return TokenAnnotation::TT_Unknown; |
| 683 | |
| 684 | if (Index == 0 || Line.Tokens[Index - 1].Tok.is(tok::l_paren) || |
| 685 | Line.Tokens[Index - 1].Tok.is(tok::comma) || |
| 686 | Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator) |
| 687 | return TokenAnnotation::TT_UnaryOperator; |
| 688 | |
| 689 | if (Line.Tokens[Index - 1].Tok.isLiteral() || |
| 690 | Line.Tokens[Index + 1].Tok.isLiteral()) |
| 691 | return TokenAnnotation::TT_BinaryOperator; |
| 692 | |
Daniel Jasper | 426702d | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 693 | // It is very unlikely that we are going to find a pointer or reference type |
| 694 | // definition on the RHS of an assignment. |
Daniel Jasper | aa1c920 | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 695 | if (AssignmentEncountered) |
Daniel Jasper | 426702d | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 696 | return TokenAnnotation::TT_BinaryOperator; |
| 697 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 698 | return TokenAnnotation::TT_PointerOrReference; |
| 699 | } |
| 700 | |
| 701 | bool isIfForOrWhile(Token Tok) { |
| 702 | return Tok.is(tok::kw_if) || Tok.is(tok::kw_for) || Tok.is(tok::kw_while); |
| 703 | } |
| 704 | |
| 705 | bool spaceRequiredBetween(Token Left, Token Right) { |
Daniel Jasper | a439686 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 706 | if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma)) |
| 707 | return false; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 708 | if (Left.is(tok::kw_template) && Right.is(tok::less)) |
| 709 | return true; |
| 710 | if (Left.is(tok::arrow) || Right.is(tok::arrow)) |
| 711 | return false; |
| 712 | if (Left.is(tok::exclaim) || Left.is(tok::tilde)) |
| 713 | return false; |
| 714 | if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less)) |
| 715 | return false; |
Daniel Jasper | 2723403 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 716 | if (Right.is(tok::amp) || Right.is(tok::star)) |
| 717 | return Left.isLiteral() || |
| 718 | (Left.isNot(tok::star) && Left.isNot(tok::amp) && |
| 719 | !Style.PointerAndReferenceBindToType); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 720 | if (Left.is(tok::amp) || Left.is(tok::star)) |
| 721 | return Right.isLiteral() || Style.PointerAndReferenceBindToType; |
| 722 | if (Right.is(tok::star) && Left.is(tok::l_paren)) |
| 723 | return false; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 724 | if (Left.is(tok::l_square) || Right.is(tok::l_square) || |
| 725 | Right.is(tok::r_square)) |
| 726 | return false; |
Daniel Jasper | 2723403 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 727 | if (Left.is(tok::coloncolon) || |
| 728 | (Right.is(tok::coloncolon) && |
| 729 | (Left.is(tok::identifier) || Left.is(tok::greater)))) |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 730 | return false; |
| 731 | if (Left.is(tok::period) || Right.is(tok::period)) |
| 732 | return false; |
| 733 | if (Left.is(tok::colon) || Right.is(tok::colon)) |
| 734 | return true; |
| 735 | if ((Left.is(tok::plusplus) && Right.isAnyIdentifier()) || |
| 736 | (Left.isAnyIdentifier() && Right.is(tok::plusplus)) || |
| 737 | (Left.is(tok::minusminus) && Right.isAnyIdentifier()) || |
| 738 | (Left.isAnyIdentifier() && Right.is(tok::minusminus))) |
| 739 | return false; |
| 740 | if (Left.is(tok::l_paren)) |
| 741 | return false; |
| 742 | if (Left.is(tok::hash)) |
| 743 | return false; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 744 | if (Right.is(tok::l_paren)) { |
| 745 | return !Left.isAnyIdentifier() || isIfForOrWhile(Left); |
| 746 | } |
| 747 | return true; |
| 748 | } |
| 749 | |
| 750 | bool canBreakBetween(const FormatToken &Left, const FormatToken &Right) { |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 751 | if (Right.Tok.is(tok::r_paren) || Right.Tok.is(tok::l_brace) || |
| 752 | Right.Tok.is(tok::comment) || Right.Tok.is(tok::greater)) |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 753 | return false; |
Daniel Jasper | 5485d0c | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 754 | if (isBinaryOperator(Left) || Right.Tok.is(tok::lessless) || |
| 755 | Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period)) |
Daniel Jasper | e9de260 | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 756 | return true; |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 757 | return Right.Tok.is(tok::colon) || Left.Tok.is(tok::comma) || Left.Tok.is( |
| 758 | tok::semi) || Left.Tok.is(tok::equal) || Left.Tok.is(tok::ampamp) || |
| 759 | Left.Tok.is(tok::pipepipe) || Left.Tok.is(tok::l_brace) || |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 760 | (Left.Tok.is(tok::l_paren) && !Right.Tok.is(tok::r_paren)); |
| 761 | } |
| 762 | |
| 763 | const UnwrappedLine &Line; |
| 764 | FormatStyle Style; |
| 765 | SourceManager &SourceMgr; |
| 766 | std::vector<TokenAnnotation> Annotations; |
| 767 | }; |
| 768 | |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 769 | class LexerBasedFormatTokenSource : public FormatTokenSource { |
| 770 | public: |
| 771 | LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr) |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 772 | : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr), |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 773 | IdentTable(Lex.getLangOpts()) { |
| 774 | Lex.SetKeepWhitespaceMode(true); |
| 775 | } |
| 776 | |
| 777 | virtual FormatToken getNextToken() { |
| 778 | if (GreaterStashed) { |
| 779 | FormatTok.NewlinesBefore = 0; |
| 780 | FormatTok.WhiteSpaceStart = |
| 781 | FormatTok.Tok.getLocation().getLocWithOffset(1); |
| 782 | FormatTok.WhiteSpaceLength = 0; |
| 783 | GreaterStashed = false; |
| 784 | return FormatTok; |
| 785 | } |
| 786 | |
| 787 | FormatTok = FormatToken(); |
| 788 | Lex.LexFromRawLexer(FormatTok.Tok); |
| 789 | FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation(); |
| 790 | |
| 791 | // Consume and record whitespace until we find a significant token. |
| 792 | while (FormatTok.Tok.is(tok::unknown)) { |
| 793 | FormatTok.NewlinesBefore += tokenText(FormatTok.Tok).count('\n'); |
| 794 | FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength(); |
| 795 | |
| 796 | if (FormatTok.Tok.is(tok::eof)) |
| 797 | return FormatTok; |
| 798 | Lex.LexFromRawLexer(FormatTok.Tok); |
| 799 | } |
| 800 | |
| 801 | if (FormatTok.Tok.is(tok::raw_identifier)) { |
| 802 | const IdentifierInfo &Info = IdentTable.get(tokenText(FormatTok.Tok)); |
| 803 | FormatTok.Tok.setKind(Info.getTokenID()); |
| 804 | } |
| 805 | |
| 806 | if (FormatTok.Tok.is(tok::greatergreater)) { |
| 807 | FormatTok.Tok.setKind(tok::greater); |
| 808 | GreaterStashed = true; |
| 809 | } |
| 810 | |
| 811 | return FormatTok; |
| 812 | } |
| 813 | |
| 814 | private: |
| 815 | FormatToken FormatTok; |
| 816 | bool GreaterStashed; |
| 817 | Lexer &Lex; |
| 818 | SourceManager &SourceMgr; |
| 819 | IdentifierTable IdentTable; |
| 820 | |
| 821 | /// Returns the text of \c FormatTok. |
| 822 | StringRef tokenText(Token &Tok) { |
| 823 | return StringRef(SourceMgr.getCharacterData(Tok.getLocation()), |
| 824 | Tok.getLength()); |
| 825 | } |
| 826 | }; |
| 827 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 828 | class Formatter : public UnwrappedLineConsumer { |
| 829 | public: |
| 830 | Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, |
| 831 | const std::vector<CharSourceRange> &Ranges) |
Daniel Jasper | 2af6bbe | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 832 | : Style(Style), Lex(Lex), SourceMgr(SourceMgr), Ranges(Ranges), |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 833 | StructuralError(false) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Daniel Jasper | 61bd3a1 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 836 | virtual ~Formatter() { |
| 837 | } |
| 838 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 839 | tooling::Replacements format() { |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 840 | LexerBasedFormatTokenSource Tokens(Lex, SourceMgr); |
| 841 | UnwrappedLineParser Parser(Style, Tokens, *this); |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 842 | StructuralError = Parser.parse(); |
| 843 | for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(), |
| 844 | E = UnwrappedLines.end(); |
| 845 | I != E; ++I) |
Alexander Kornienko | bc09a7e | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 846 | formatUnwrappedLine(*I); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 847 | return Replaces; |
| 848 | } |
| 849 | |
| 850 | private: |
Alexander Kornienko | bc09a7e | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 851 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 852 | UnwrappedLines.push_back(TheLine); |
| 853 | } |
| 854 | |
Alexander Kornienko | bc09a7e | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 855 | void formatUnwrappedLine(const UnwrappedLine &TheLine) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 856 | if (TheLine.Tokens.size() == 0) |
| 857 | return; |
| 858 | |
| 859 | CharSourceRange LineRange = |
| 860 | CharSourceRange::getTokenRange(TheLine.Tokens.front().Tok.getLocation(), |
| 861 | TheLine.Tokens.back().Tok.getLocation()); |
| 862 | |
| 863 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 864 | if (SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(), |
| 865 | Ranges[i].getBegin()) || |
| 866 | SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 867 | LineRange.getBegin())) |
| 868 | continue; |
| 869 | |
| 870 | TokenAnnotator Annotator(TheLine, Style, SourceMgr); |
| 871 | Annotator.annotate(); |
| 872 | UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 873 | Annotator.getAnnotations(), Replaces, |
| 874 | StructuralError); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 875 | Formatter.format(); |
| 876 | return; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | FormatStyle Style; |
| 881 | Lexer &Lex; |
| 882 | SourceManager &SourceMgr; |
| 883 | tooling::Replacements Replaces; |
| 884 | std::vector<CharSourceRange> Ranges; |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 885 | std::vector<UnwrappedLine> UnwrappedLines; |
| 886 | bool StructuralError; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 887 | }; |
| 888 | |
| 889 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 890 | SourceManager &SourceMgr, |
| 891 | std::vector<CharSourceRange> Ranges) { |
| 892 | Formatter formatter(Style, Lex, SourceMgr, Ranges); |
| 893 | return formatter.format(); |
| 894 | } |
| 895 | |
| 896 | } // namespace format |
| 897 | } // namespace clang |