Daniel Jasper | bac016b | 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 | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "UnwrappedLineParser.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "clang/Lex/Lexer.h" |
| 23 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Daniel Jasper | bac016b | 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 | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 31 | enum TokenType { TT_Unknown, TT_TemplateOpener, TT_TemplateCloser, |
| 32 | TT_BinaryOperator, TT_UnaryOperator, TT_OverloadedOperator, |
| 33 | TT_PointerOrReference, TT_ConditionalExpr, TT_LineComment, |
| 34 | TT_BlockComment }; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 35 | |
| 36 | TokenType Type; |
| 37 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 38 | bool SpaceRequiredBefore; |
| 39 | bool CanBreakBefore; |
| 40 | bool MustBreakBefore; |
| 41 | }; |
| 42 | |
| 43 | using llvm::MutableArrayRef; |
| 44 | |
| 45 | FormatStyle getLLVMStyle() { |
| 46 | FormatStyle LLVMStyle; |
| 47 | LLVMStyle.ColumnLimit = 80; |
| 48 | LLVMStyle.MaxEmptyLinesToKeep = 1; |
| 49 | LLVMStyle.PointerAndReferenceBindToType = false; |
| 50 | LLVMStyle.AccessModifierOffset = -2; |
| 51 | LLVMStyle.SplitTemplateClosingGreater = true; |
| 52 | return LLVMStyle; |
| 53 | } |
| 54 | |
| 55 | FormatStyle getGoogleStyle() { |
| 56 | FormatStyle GoogleStyle; |
| 57 | GoogleStyle.ColumnLimit = 80; |
| 58 | GoogleStyle.MaxEmptyLinesToKeep = 1; |
| 59 | GoogleStyle.PointerAndReferenceBindToType = true; |
| 60 | GoogleStyle.AccessModifierOffset = -1; |
| 61 | GoogleStyle.SplitTemplateClosingGreater = false; |
| 62 | return GoogleStyle; |
| 63 | } |
| 64 | |
| 65 | struct OptimizationParameters { |
| 66 | unsigned PenaltyExtraLine; |
| 67 | unsigned PenaltyIndentLevel; |
| 68 | }; |
| 69 | |
| 70 | class UnwrappedLineFormatter { |
| 71 | public: |
| 72 | UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr, |
| 73 | const UnwrappedLine &Line, |
| 74 | const std::vector<TokenAnnotation> &Annotations, |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 75 | tooling::Replacements &Replaces, bool StructuralError) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 76 | : Style(Style), |
| 77 | SourceMgr(SourceMgr), |
| 78 | Line(Line), |
| 79 | Annotations(Annotations), |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 80 | Replaces(Replaces), |
| 81 | StructuralError(StructuralError) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 82 | Parameters.PenaltyExtraLine = 100; |
| 83 | Parameters.PenaltyIndentLevel = 5; |
| 84 | } |
| 85 | |
| 86 | void format() { |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 87 | unsigned Indent = formatFirstToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 88 | count = 0; |
| 89 | IndentState State; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 90 | State.Column = Indent + Line.Tokens[0].Tok.getLength(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 91 | State.CtorInitializerOnNewLine = false; |
| 92 | State.InCtorInitializer = false; |
| 93 | State.ConsumedTokens = 1; |
| 94 | |
| 95 | //State.UsedIndent.push_back(Line.Level * 2); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 96 | State.Indent.push_back(Indent + 4); |
| 97 | State.LastSpace.push_back(Indent); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 98 | |
| 99 | // Start iterating at 1 as we have correctly formatted of Token #0 above. |
| 100 | for (unsigned i = 1, n = Line.Tokens.size(); i != n; ++i) { |
| 101 | unsigned NoBreak = calcPenalty(State, false, UINT_MAX); |
| 102 | unsigned Break = calcPenalty(State, true, NoBreak); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 103 | addTokenToState(Break < NoBreak, false, State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | /// \brief The current state when indenting a unwrapped line. |
| 109 | /// |
| 110 | /// As the indenting tries different combinations this is copied by value. |
| 111 | struct IndentState { |
| 112 | /// \brief The number of used columns in the current line. |
| 113 | unsigned Column; |
| 114 | |
| 115 | /// \brief The number of tokens already consumed. |
| 116 | unsigned ConsumedTokens; |
| 117 | |
| 118 | /// \brief The position to which a specific parenthesis level needs to be |
| 119 | /// indented. |
| 120 | std::vector<unsigned> Indent; |
| 121 | |
| 122 | std::vector<unsigned> LastSpace; |
| 123 | |
| 124 | bool CtorInitializerOnNewLine; |
| 125 | bool InCtorInitializer; |
| 126 | |
| 127 | /// \brief Comparison operator to be able to used \c IndentState in \c map. |
| 128 | bool operator<(const IndentState &Other) const { |
| 129 | if (Other.ConsumedTokens != ConsumedTokens) |
| 130 | return Other.ConsumedTokens > ConsumedTokens; |
| 131 | if (Other.Column != Column) |
| 132 | return Other.Column > Column; |
| 133 | if (Other.Indent.size() != Indent.size()) |
| 134 | return Other.Indent.size() > Indent.size(); |
| 135 | for (int i = 0, e = Indent.size(); i != e; ++i) { |
| 136 | if (Other.Indent[i] != Indent[i]) |
| 137 | return Other.Indent[i] > Indent[i]; |
| 138 | } |
| 139 | if (Other.LastSpace.size() != LastSpace.size()) |
| 140 | return Other.LastSpace.size() > LastSpace.size(); |
| 141 | for (int i = 0, e = LastSpace.size(); i != e; ++i) { |
| 142 | if (Other.LastSpace[i] != LastSpace[i]) |
| 143 | return Other.LastSpace[i] > LastSpace[i]; |
| 144 | } |
| 145 | return false; |
| 146 | } |
| 147 | }; |
| 148 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 149 | /// \brief Appends the next token to \p State and updates information |
| 150 | /// necessary for indentation. |
| 151 | /// |
| 152 | /// Puts the token on the current line if \p Newline is \c true and adds a |
| 153 | /// line break and necessary indentation otherwise. |
| 154 | /// |
| 155 | /// If \p DryRun is \c false, also creates and stores the required |
| 156 | /// \c Replacement. |
| 157 | void addTokenToState(bool Newline, bool DryRun, IndentState &State) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 158 | unsigned Index = State.ConsumedTokens; |
| 159 | const FormatToken &Current = Line.Tokens[Index]; |
| 160 | const FormatToken &Previous = Line.Tokens[Index - 1]; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 161 | unsigned ParenLevel = State.Indent.size() - 1; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 162 | |
| 163 | if (Newline) { |
| 164 | if (Current.Tok.is(tok::string_literal) && |
| 165 | Previous.Tok.is(tok::string_literal)) |
| 166 | State.Column = State.Column - Previous.Tok.getLength(); |
| 167 | else if (Previous.Tok.is(tok::equal) && ParenLevel != 0) |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 168 | // Indent and extra 4 spaces after '=' as it continues an expression. |
| 169 | // Don't do that on the top level, as we already indent 4 there. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 170 | State.Column = State.Indent[ParenLevel] + 4; |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 171 | else |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 172 | State.Column = State.Indent[ParenLevel]; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 173 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 174 | if (!DryRun) |
| 175 | replaceWhitespace(Current, 1, State.Column); |
| 176 | |
| 177 | State.Column += Current.Tok.getLength(); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 178 | State.LastSpace[ParenLevel] = State.Indent[ParenLevel]; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 179 | if (Current.Tok.is(tok::colon) && |
| 180 | Annotations[Index].Type != TokenAnnotation::TT_ConditionalExpr) { |
| 181 | State.Indent[ParenLevel] += 2; |
| 182 | State.CtorInitializerOnNewLine = true; |
| 183 | State.InCtorInitializer = true; |
| 184 | } |
| 185 | } else { |
| 186 | unsigned Spaces = Annotations[Index].SpaceRequiredBefore ? 1 : 0; |
| 187 | if (Annotations[Index].Type == TokenAnnotation::TT_LineComment) |
| 188 | Spaces = 2; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 189 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 190 | if (!DryRun) |
| 191 | replaceWhitespace(Current, 0, Spaces); |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 192 | |
| 193 | if (Previous.Tok.is(tok::l_paren) || |
| 194 | Annotations[Index - 1].Type == TokenAnnotation::TT_TemplateOpener) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 195 | State.Indent[ParenLevel] = State.Column; |
| 196 | if (Current.Tok.is(tok::colon)) { |
| 197 | State.Indent[ParenLevel] = State.Column + 3; |
| 198 | State.InCtorInitializer = true; |
| 199 | } |
| 200 | // Top-level spaces are exempt as that mostly leads to better results. |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 201 | if (Spaces > 0 && ParenLevel != 0) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 202 | State.LastSpace[ParenLevel] = State.Column + Spaces; |
| 203 | State.Column += Current.Tok.getLength() + Spaces; |
| 204 | } |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 205 | moveStateToNextToken(State); |
| 206 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 207 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 208 | /// \brief Mark the next token as consumed in \p State and modify its stacks |
| 209 | /// accordingly. |
| 210 | void moveStateToNextToken(IndentState &State) { |
| 211 | unsigned Index = State.ConsumedTokens; |
| 212 | const FormatToken &Current = Line.Tokens[Index]; |
| 213 | |
| 214 | // If we encounter an opening (, [ or <, we add a level to our stacks to |
| 215 | // prepare for the following tokens. |
| 216 | if (Current.Tok.is(tok::l_paren) || Current.Tok.is(tok::l_square) || |
| 217 | Annotations[Index].Type == TokenAnnotation::TT_TemplateOpener) { |
| 218 | State.Indent.push_back(4 + State.LastSpace.back()); |
| 219 | State.LastSpace.push_back(State.LastSpace.back()); |
| 220 | } |
| 221 | |
| 222 | // If we encounter a closing ), ] or >, we can remove a level from our |
| 223 | // stacks. |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 224 | if (Current.Tok.is(tok::r_paren) || Current.Tok.is(tok::r_square) || |
| 225 | Annotations[Index].Type == TokenAnnotation::TT_TemplateCloser) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 226 | State.Indent.pop_back(); |
| 227 | State.LastSpace.pop_back(); |
| 228 | } |
| 229 | |
| 230 | ++State.ConsumedTokens; |
| 231 | } |
| 232 | |
| 233 | typedef std::map<IndentState, unsigned> StateMap; |
| 234 | StateMap Memory; |
| 235 | |
| 236 | unsigned splitPenalty(const FormatToken &Token) { |
| 237 | if (Token.Tok.is(tok::semi)) |
| 238 | return 0; |
| 239 | if (Token.Tok.is(tok::comma)) |
| 240 | return 1; |
| 241 | if (Token.Tok.is(tok::equal) || Token.Tok.is(tok::l_paren) || |
| 242 | Token.Tok.is(tok::pipepipe) || Token.Tok.is(tok::ampamp)) |
| 243 | return 2; |
| 244 | return 3; |
| 245 | } |
| 246 | |
| 247 | /// \brief Calculate the number of lines needed to format the remaining part |
| 248 | /// of the unwrapped line. |
| 249 | /// |
| 250 | /// Assumes the formatting so far has led to |
| 251 | /// the \c IndentState \p State. If \p NewLine is set, a new line will be |
| 252 | /// added after the previous token. |
| 253 | /// |
| 254 | /// \param StopAt is used for optimization. If we can determine that we'll |
| 255 | /// definitely need at least \p StopAt additional lines, we already know of a |
| 256 | /// better solution. |
| 257 | unsigned calcPenalty(IndentState State, bool NewLine, unsigned StopAt) { |
| 258 | // We are at the end of the unwrapped line, so we don't need any more lines. |
| 259 | if (State.ConsumedTokens >= Line.Tokens.size()) |
| 260 | return 0; |
| 261 | |
| 262 | if (!NewLine && Annotations[State.ConsumedTokens].MustBreakBefore) |
| 263 | return UINT_MAX; |
| 264 | if (NewLine && !Annotations[State.ConsumedTokens].CanBreakBefore) |
| 265 | return UINT_MAX; |
| 266 | |
| 267 | if (State.ConsumedTokens > 0 && !NewLine && |
| 268 | State.CtorInitializerOnNewLine && |
| 269 | Line.Tokens[State.ConsumedTokens - 1].Tok.is(tok::comma)) |
| 270 | return UINT_MAX; |
| 271 | |
| 272 | if (NewLine && State.InCtorInitializer && !State.CtorInitializerOnNewLine) |
| 273 | return UINT_MAX; |
| 274 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 275 | addTokenToState(NewLine, true, State); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 276 | |
| 277 | // Exceeding column limit is bad. |
| 278 | if (State.Column > Style.ColumnLimit) |
| 279 | return UINT_MAX; |
| 280 | |
| 281 | unsigned CurrentPenalty = 0; |
| 282 | if (NewLine) { |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 283 | CurrentPenalty += Parameters.PenaltyIndentLevel * State.Indent.size() + |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 284 | Parameters.PenaltyExtraLine + |
| 285 | splitPenalty(Line.Tokens[State.ConsumedTokens - 2]); |
| 286 | } |
| 287 | |
| 288 | if (StopAt <= CurrentPenalty) |
| 289 | return UINT_MAX; |
| 290 | StopAt -= CurrentPenalty; |
| 291 | |
| 292 | // Has this state already been examined? |
| 293 | StateMap::iterator I = Memory.find(State); |
| 294 | if (I != Memory.end()) |
| 295 | return I->second; |
| 296 | ++count; |
| 297 | |
| 298 | unsigned NoBreak = calcPenalty(State, false, StopAt); |
| 299 | unsigned WithBreak = calcPenalty(State, true, std::min(StopAt, NoBreak)); |
| 300 | unsigned Result = std::min(NoBreak, WithBreak); |
| 301 | if (Result != UINT_MAX) |
| 302 | Result += CurrentPenalty; |
| 303 | Memory[State] = Result; |
| 304 | assert(Memory.find(State) != Memory.end()); |
| 305 | return Result; |
| 306 | } |
| 307 | |
| 308 | /// \brief Replaces the whitespace in front of \p Tok. Only call once for |
| 309 | /// each \c FormatToken. |
| 310 | void replaceWhitespace(const FormatToken &Tok, unsigned NewLines, |
| 311 | unsigned Spaces) { |
| 312 | Replaces.insert(tooling::Replacement( |
| 313 | SourceMgr, Tok.WhiteSpaceStart, Tok.WhiteSpaceLength, |
| 314 | std::string(NewLines, '\n') + std::string(Spaces, ' '))); |
| 315 | } |
| 316 | |
| 317 | /// \brief Add a new line and the required indent before the first Token |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame^] | 318 | /// of the \c UnwrappedLine if there was no structural parsing error. |
| 319 | /// Returns the indent level of the \c UnwrappedLine. |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 320 | unsigned formatFirstToken() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 321 | const FormatToken &Token = Line.Tokens[0]; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 322 | if (!Token.WhiteSpaceStart.isValid() || StructuralError) |
| 323 | return SourceMgr.getSpellingColumnNumber(Token.Tok.getLocation()) - 1; |
| 324 | |
| 325 | unsigned Newlines = |
| 326 | std::min(Token.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); |
| 327 | unsigned Offset = SourceMgr.getFileOffset(Token.WhiteSpaceStart); |
| 328 | if (Newlines == 0 && Offset != 0) |
| 329 | Newlines = 1; |
| 330 | unsigned Indent = Line.Level * 2; |
| 331 | if (Token.Tok.is(tok::kw_public) || Token.Tok.is(tok::kw_protected) || |
| 332 | Token.Tok.is(tok::kw_private)) |
| 333 | Indent += Style.AccessModifierOffset; |
| 334 | replaceWhitespace(Token, Newlines, Indent); |
| 335 | return Indent; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | FormatStyle Style; |
| 339 | SourceManager &SourceMgr; |
| 340 | const UnwrappedLine &Line; |
| 341 | const std::vector<TokenAnnotation> &Annotations; |
| 342 | tooling::Replacements &Replaces; |
| 343 | unsigned int count; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 344 | bool StructuralError; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 345 | |
| 346 | OptimizationParameters Parameters; |
| 347 | }; |
| 348 | |
| 349 | /// \brief Determines extra information about the tokens comprising an |
| 350 | /// \c UnwrappedLine. |
| 351 | class TokenAnnotator { |
| 352 | public: |
| 353 | TokenAnnotator(const UnwrappedLine &Line, const FormatStyle &Style, |
| 354 | SourceManager &SourceMgr) |
| 355 | : Line(Line), |
| 356 | Style(Style), |
| 357 | SourceMgr(SourceMgr) { |
| 358 | } |
| 359 | |
| 360 | /// \brief A parser that gathers additional information about tokens. |
| 361 | /// |
| 362 | /// The \c TokenAnnotator tries to matches parenthesis and square brakets and |
| 363 | /// store a parenthesis levels. It also tries to resolve matching "<" and ">" |
| 364 | /// into template parameter lists. |
| 365 | class AnnotatingParser { |
| 366 | public: |
Manuel Klimek | 0be4b36 | 2012-12-03 20:55:42 +0000 | [diff] [blame] | 367 | AnnotatingParser(const SmallVector<FormatToken, 16> &Tokens, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 368 | std::vector<TokenAnnotation> &Annotations) |
Manuel Klimek | 0be4b36 | 2012-12-03 20:55:42 +0000 | [diff] [blame] | 369 | : Tokens(Tokens), |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 370 | Annotations(Annotations), |
| 371 | Index(0) { |
| 372 | } |
| 373 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 374 | bool parseAngle() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 375 | while (Index < Tokens.size()) { |
| 376 | if (Tokens[Index].Tok.is(tok::greater)) { |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 377 | Annotations[Index].Type = TokenAnnotation::TT_TemplateCloser; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 378 | next(); |
| 379 | return true; |
| 380 | } |
| 381 | if (Tokens[Index].Tok.is(tok::r_paren) || |
| 382 | Tokens[Index].Tok.is(tok::r_square)) |
| 383 | return false; |
| 384 | if (Tokens[Index].Tok.is(tok::pipepipe) || |
| 385 | Tokens[Index].Tok.is(tok::ampamp) || |
| 386 | Tokens[Index].Tok.is(tok::question) || |
| 387 | Tokens[Index].Tok.is(tok::colon)) |
| 388 | return false; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 389 | consumeToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 390 | } |
| 391 | return false; |
| 392 | } |
| 393 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 394 | bool parseParens() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 395 | while (Index < Tokens.size()) { |
| 396 | if (Tokens[Index].Tok.is(tok::r_paren)) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 397 | next(); |
| 398 | return true; |
| 399 | } |
| 400 | if (Tokens[Index].Tok.is(tok::r_square)) |
| 401 | return false; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 402 | consumeToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 403 | } |
| 404 | return false; |
| 405 | } |
| 406 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 407 | bool parseSquare() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 408 | while (Index < Tokens.size()) { |
| 409 | if (Tokens[Index].Tok.is(tok::r_square)) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 410 | next(); |
| 411 | return true; |
| 412 | } |
| 413 | if (Tokens[Index].Tok.is(tok::r_paren)) |
| 414 | return false; |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 415 | consumeToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 416 | } |
| 417 | return false; |
| 418 | } |
| 419 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 420 | bool parseConditional() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 421 | while (Index < Tokens.size()) { |
| 422 | if (Tokens[Index].Tok.is(tok::colon)) { |
| 423 | Annotations[Index].Type = TokenAnnotation::TT_ConditionalExpr; |
| 424 | next(); |
| 425 | return true; |
| 426 | } |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 427 | consumeToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 428 | } |
| 429 | return false; |
| 430 | } |
| 431 | |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 432 | void consumeToken() { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 433 | unsigned CurrentIndex = Index; |
| 434 | next(); |
| 435 | switch (Tokens[CurrentIndex].Tok.getKind()) { |
| 436 | case tok::l_paren: |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 437 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 438 | break; |
| 439 | case tok::l_square: |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 440 | parseSquare(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 441 | break; |
| 442 | case tok::less: |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 443 | if (parseAngle()) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 444 | Annotations[CurrentIndex].Type = TokenAnnotation::TT_TemplateOpener; |
| 445 | else { |
| 446 | Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator; |
| 447 | Index = CurrentIndex + 1; |
| 448 | } |
| 449 | break; |
| 450 | case tok::greater: |
| 451 | Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator; |
| 452 | break; |
| 453 | case tok::kw_operator: |
| 454 | if (!Tokens[Index].Tok.is(tok::l_paren)) |
| 455 | Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator; |
| 456 | next(); |
| 457 | break; |
| 458 | case tok::question: |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 459 | parseConditional(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 460 | break; |
| 461 | default: |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | void parseLine() { |
| 467 | while (Index < Tokens.size()) { |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 468 | consumeToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| 472 | void next() { |
| 473 | ++Index; |
| 474 | } |
| 475 | |
| 476 | private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 477 | const SmallVector<FormatToken, 16> &Tokens; |
| 478 | std::vector<TokenAnnotation> &Annotations; |
| 479 | unsigned Index; |
| 480 | }; |
| 481 | |
| 482 | void annotate() { |
| 483 | Annotations.clear(); |
| 484 | for (int i = 0, e = Line.Tokens.size(); i != e; ++i) { |
| 485 | Annotations.push_back(TokenAnnotation()); |
| 486 | } |
| 487 | |
Manuel Klimek | 0be4b36 | 2012-12-03 20:55:42 +0000 | [diff] [blame] | 488 | AnnotatingParser Parser(Line.Tokens, Annotations); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 489 | Parser.parseLine(); |
| 490 | |
| 491 | determineTokenTypes(); |
| 492 | |
| 493 | for (int i = 1, e = Line.Tokens.size(); i != e; ++i) { |
| 494 | TokenAnnotation &Annotation = Annotations[i]; |
| 495 | |
| 496 | Annotation.CanBreakBefore = |
| 497 | canBreakBetween(Line.Tokens[i - 1], Line.Tokens[i]); |
| 498 | |
| 499 | if (Line.Tokens[i].Tok.is(tok::colon)) { |
| 500 | if (Line.Tokens[0].Tok.is(tok::kw_case) || i == e - 1) { |
| 501 | Annotation.SpaceRequiredBefore = false; |
| 502 | } else { |
| 503 | Annotation.SpaceRequiredBefore = TokenAnnotation::TT_ConditionalExpr; |
| 504 | } |
| 505 | } else if (Annotations[i - 1].Type == TokenAnnotation::TT_UnaryOperator) { |
| 506 | Annotation.SpaceRequiredBefore = false; |
| 507 | } else if (Annotation.Type == TokenAnnotation::TT_UnaryOperator) { |
| 508 | Annotation.SpaceRequiredBefore = |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 509 | Line.Tokens[i - 1].Tok.isNot(tok::l_paren) && |
| 510 | Line.Tokens[i - 1].Tok.isNot(tok::l_square); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 511 | } else if (Line.Tokens[i - 1].Tok.is(tok::greater) && |
| 512 | Line.Tokens[i].Tok.is(tok::greater)) { |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 513 | if (Annotation.Type == TokenAnnotation::TT_TemplateCloser && |
Daniel Jasper | 2040915 | 2012-12-04 14:54:30 +0000 | [diff] [blame] | 514 | Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser) |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 515 | Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 516 | else |
| 517 | Annotation.SpaceRequiredBefore = false; |
| 518 | } else if ( |
| 519 | Annotation.Type == TokenAnnotation::TT_BinaryOperator || |
| 520 | Annotations[i - 1].Type == TokenAnnotation::TT_BinaryOperator) { |
| 521 | Annotation.SpaceRequiredBefore = true; |
| 522 | } else if ( |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 523 | Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser && |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 524 | Line.Tokens[i].Tok.is(tok::l_paren)) { |
| 525 | Annotation.SpaceRequiredBefore = false; |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 526 | } else if (Line.Tokens[i].Tok.is(tok::less) && |
| 527 | Line.Tokens[0].Tok.is(tok::hash)) { |
| 528 | Annotation.SpaceRequiredBefore = true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 529 | } else { |
| 530 | Annotation.SpaceRequiredBefore = |
| 531 | spaceRequiredBetween(Line.Tokens[i - 1].Tok, Line.Tokens[i].Tok); |
| 532 | } |
| 533 | |
| 534 | if (Annotations[i - 1].Type == TokenAnnotation::TT_LineComment || |
| 535 | (Line.Tokens[i].Tok.is(tok::string_literal) && |
| 536 | Line.Tokens[i - 1].Tok.is(tok::string_literal))) { |
| 537 | Annotation.MustBreakBefore = true; |
| 538 | } |
| 539 | |
| 540 | if (Annotation.MustBreakBefore) |
| 541 | Annotation.CanBreakBefore = true; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | const std::vector<TokenAnnotation> &getAnnotations() { |
| 546 | return Annotations; |
| 547 | } |
| 548 | |
| 549 | private: |
| 550 | void determineTokenTypes() { |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 551 | bool EqualEncountered = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 552 | for (int i = 0, e = Line.Tokens.size(); i != e; ++i) { |
| 553 | TokenAnnotation &Annotation = Annotations[i]; |
| 554 | const FormatToken &Tok = Line.Tokens[i]; |
| 555 | |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 556 | if (Tok.Tok.is(tok::equal)) |
| 557 | EqualEncountered = true; |
| 558 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 559 | if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp)) |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 560 | Annotation.Type = determineStarAmpUsage(i, EqualEncountered); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 561 | else if (isUnaryOperator(i)) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 562 | Annotation.Type = TokenAnnotation::TT_UnaryOperator; |
| 563 | else if (isBinaryOperator(Line.Tokens[i])) |
| 564 | Annotation.Type = TokenAnnotation::TT_BinaryOperator; |
| 565 | else if (Tok.Tok.is(tok::comment)) { |
| 566 | StringRef Data(SourceMgr.getCharacterData(Tok.Tok.getLocation()), |
| 567 | Tok.Tok.getLength()); |
| 568 | if (Data.startswith("//")) |
| 569 | Annotation.Type = TokenAnnotation::TT_LineComment; |
| 570 | else |
| 571 | Annotation.Type = TokenAnnotation::TT_BlockComment; |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 576 | bool isUnaryOperator(unsigned Index) { |
| 577 | const Token &Tok = Line.Tokens[Index].Tok; |
| 578 | if (Tok.isNot(tok::minus) && Tok.isNot(tok::plus)) |
| 579 | return false; |
| 580 | const Token &PreviousTok = Line.Tokens[Index - 1].Tok; |
| 581 | if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) || |
| 582 | PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square)) |
| 583 | return true; |
| 584 | return Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator; |
| 585 | } |
| 586 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 587 | bool isBinaryOperator(const FormatToken &Tok) { |
| 588 | switch (Tok.Tok.getKind()) { |
| 589 | case tok::equal: |
| 590 | case tok::equalequal: |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 591 | case tok::exclaimequal: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 592 | case tok::star: |
| 593 | //case tok::amp: |
| 594 | case tok::plus: |
| 595 | case tok::slash: |
| 596 | case tok::minus: |
| 597 | case tok::ampamp: |
| 598 | case tok::pipe: |
| 599 | case tok::pipepipe: |
| 600 | case tok::percent: |
| 601 | return true; |
| 602 | default: |
| 603 | return false; |
| 604 | } |
| 605 | } |
| 606 | |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 607 | TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index, |
| 608 | bool EqualEncountered) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 609 | if (Index == Annotations.size()) |
| 610 | return TokenAnnotation::TT_Unknown; |
| 611 | |
| 612 | if (Index == 0 || Line.Tokens[Index - 1].Tok.is(tok::l_paren) || |
| 613 | Line.Tokens[Index - 1].Tok.is(tok::comma) || |
| 614 | Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator) |
| 615 | return TokenAnnotation::TT_UnaryOperator; |
| 616 | |
| 617 | if (Line.Tokens[Index - 1].Tok.isLiteral() || |
| 618 | Line.Tokens[Index + 1].Tok.isLiteral()) |
| 619 | return TokenAnnotation::TT_BinaryOperator; |
| 620 | |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 621 | // It is very unlikely that we are going to find a pointer or reference type |
| 622 | // definition on the RHS of an assignment. |
| 623 | if (EqualEncountered) |
| 624 | return TokenAnnotation::TT_BinaryOperator; |
| 625 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 626 | return TokenAnnotation::TT_PointerOrReference; |
| 627 | } |
| 628 | |
| 629 | bool isIfForOrWhile(Token Tok) { |
| 630 | return Tok.is(tok::kw_if) || Tok.is(tok::kw_for) || Tok.is(tok::kw_while); |
| 631 | } |
| 632 | |
| 633 | bool spaceRequiredBetween(Token Left, Token Right) { |
| 634 | if (Left.is(tok::kw_template) && Right.is(tok::less)) |
| 635 | return true; |
| 636 | if (Left.is(tok::arrow) || Right.is(tok::arrow)) |
| 637 | return false; |
| 638 | if (Left.is(tok::exclaim) || Left.is(tok::tilde)) |
| 639 | return false; |
| 640 | if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less)) |
| 641 | return false; |
| 642 | if (Left.is(tok::amp) || Left.is(tok::star)) |
| 643 | return Right.isLiteral() || Style.PointerAndReferenceBindToType; |
| 644 | if (Right.is(tok::star) && Left.is(tok::l_paren)) |
| 645 | return false; |
| 646 | if (Right.is(tok::amp) || Right.is(tok::star)) |
| 647 | return Left.isLiteral() || !Style.PointerAndReferenceBindToType; |
| 648 | if (Left.is(tok::l_square) || Right.is(tok::l_square) || |
| 649 | Right.is(tok::r_square)) |
| 650 | return false; |
| 651 | if (Left.is(tok::coloncolon) || Right.is(tok::coloncolon)) |
| 652 | return false; |
| 653 | if (Left.is(tok::period) || Right.is(tok::period)) |
| 654 | return false; |
| 655 | if (Left.is(tok::colon) || Right.is(tok::colon)) |
| 656 | return true; |
| 657 | if ((Left.is(tok::plusplus) && Right.isAnyIdentifier()) || |
| 658 | (Left.isAnyIdentifier() && Right.is(tok::plusplus)) || |
| 659 | (Left.is(tok::minusminus) && Right.isAnyIdentifier()) || |
| 660 | (Left.isAnyIdentifier() && Right.is(tok::minusminus))) |
| 661 | return false; |
| 662 | if (Left.is(tok::l_paren)) |
| 663 | return false; |
| 664 | if (Left.is(tok::hash)) |
| 665 | return false; |
| 666 | if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma)) |
| 667 | return false; |
| 668 | if (Right.is(tok::l_paren)) { |
| 669 | return !Left.isAnyIdentifier() || isIfForOrWhile(Left); |
| 670 | } |
| 671 | return true; |
| 672 | } |
| 673 | |
| 674 | bool canBreakBetween(const FormatToken &Left, const FormatToken &Right) { |
| 675 | if (Right.Tok.is(tok::r_paren)) |
| 676 | return false; |
| 677 | if (isBinaryOperator(Left)) |
| 678 | return true; |
| 679 | return Right.Tok.is(tok::colon) || Left.Tok.is(tok::comma) || Left.Tok.is( |
| 680 | tok::semi) || Left.Tok.is(tok::equal) || Left.Tok.is(tok::ampamp) || |
| 681 | (Left.Tok.is(tok::l_paren) && !Right.Tok.is(tok::r_paren)); |
| 682 | } |
| 683 | |
| 684 | const UnwrappedLine &Line; |
| 685 | FormatStyle Style; |
| 686 | SourceManager &SourceMgr; |
| 687 | std::vector<TokenAnnotation> Annotations; |
| 688 | }; |
| 689 | |
| 690 | class Formatter : public UnwrappedLineConsumer { |
| 691 | public: |
| 692 | Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, |
| 693 | const std::vector<CharSourceRange> &Ranges) |
| 694 | : Style(Style), |
| 695 | Lex(Lex), |
| 696 | SourceMgr(SourceMgr), |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 697 | Ranges(Ranges), |
| 698 | StructuralError(false) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 701 | virtual ~Formatter() { |
| 702 | } |
| 703 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 704 | tooling::Replacements format() { |
| 705 | UnwrappedLineParser Parser(Lex, SourceMgr, *this); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 706 | StructuralError = Parser.parse(); |
| 707 | for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(), |
| 708 | E = UnwrappedLines.end(); |
| 709 | I != E; ++I) |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame^] | 710 | formatUnwrappedLine(*I); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 711 | return Replaces; |
| 712 | } |
| 713 | |
| 714 | private: |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame^] | 715 | virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) { |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 716 | UnwrappedLines.push_back(TheLine); |
| 717 | } |
| 718 | |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame^] | 719 | void formatUnwrappedLine(const UnwrappedLine &TheLine) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 720 | if (TheLine.Tokens.size() == 0) |
| 721 | return; |
| 722 | |
| 723 | CharSourceRange LineRange = |
| 724 | CharSourceRange::getTokenRange(TheLine.Tokens.front().Tok.getLocation(), |
| 725 | TheLine.Tokens.back().Tok.getLocation()); |
| 726 | |
| 727 | for (unsigned i = 0, e = Ranges.size(); i != e; ++i) { |
| 728 | if (SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(), |
| 729 | Ranges[i].getBegin()) || |
| 730 | SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(), |
| 731 | LineRange.getBegin())) |
| 732 | continue; |
| 733 | |
| 734 | TokenAnnotator Annotator(TheLine, Style, SourceMgr); |
| 735 | Annotator.annotate(); |
| 736 | UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 737 | Annotator.getAnnotations(), Replaces, |
| 738 | StructuralError); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 739 | Formatter.format(); |
| 740 | return; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | FormatStyle Style; |
| 745 | Lexer &Lex; |
| 746 | SourceManager &SourceMgr; |
| 747 | tooling::Replacements Replaces; |
| 748 | std::vector<CharSourceRange> Ranges; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 749 | std::vector<UnwrappedLine> UnwrappedLines; |
| 750 | bool StructuralError; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 751 | }; |
| 752 | |
| 753 | tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, |
| 754 | SourceManager &SourceMgr, |
| 755 | std::vector<CharSourceRange> Ranges) { |
| 756 | Formatter formatter(Style, Lex, SourceMgr, Ranges); |
| 757 | return formatter.format(); |
| 758 | } |
| 759 | |
| 760 | } // namespace format |
| 761 | } // namespace clang |