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