blob: 46af06fb41946a6767dccaa57e1d149457aeb50d [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- 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 Carruth3a022472012-12-04 09:13:33 +000020#include "UnwrappedLineParser.h"
Daniel Jasperf7935112012-12-03 18:12:45 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Lex/Lexer.h"
23
Daniel Jasper8b529712012-12-04 13:02:32 +000024#include <string>
25
Daniel Jasperf7935112012-12-03 18:12:45 +000026namespace clang {
27namespace format {
28
29// FIXME: Move somewhere sane.
30struct TokenAnnotation {
Daniel Jasperaa1c9202012-12-05 14:57:28 +000031 enum TokenType {
32 TT_Unknown,
33 TT_TemplateOpener,
34 TT_TemplateCloser,
35 TT_BinaryOperator,
36 TT_UnaryOperator,
37 TT_OverloadedOperator,
38 TT_PointerOrReference,
39 TT_ConditionalExpr,
40 TT_LineComment,
41 TT_BlockComment
42 };
Daniel Jasperf7935112012-12-03 18:12:45 +000043
44 TokenType Type;
45
Daniel Jasperf7935112012-12-03 18:12:45 +000046 bool SpaceRequiredBefore;
47 bool CanBreakBefore;
48 bool MustBreakBefore;
49};
50
51using llvm::MutableArrayRef;
52
53FormatStyle getLLVMStyle() {
54 FormatStyle LLVMStyle;
55 LLVMStyle.ColumnLimit = 80;
56 LLVMStyle.MaxEmptyLinesToKeep = 1;
57 LLVMStyle.PointerAndReferenceBindToType = false;
58 LLVMStyle.AccessModifierOffset = -2;
59 LLVMStyle.SplitTemplateClosingGreater = true;
Alexander Kornienko578fdd82012-12-06 18:03:27 +000060 LLVMStyle.IndentCaseLabels = false;
Daniel Jasperf7935112012-12-03 18:12:45 +000061 return LLVMStyle;
62}
63
64FormatStyle getGoogleStyle() {
65 FormatStyle GoogleStyle;
66 GoogleStyle.ColumnLimit = 80;
67 GoogleStyle.MaxEmptyLinesToKeep = 1;
68 GoogleStyle.PointerAndReferenceBindToType = true;
69 GoogleStyle.AccessModifierOffset = -1;
70 GoogleStyle.SplitTemplateClosingGreater = false;
Alexander Kornienko578fdd82012-12-06 18:03:27 +000071 GoogleStyle.IndentCaseLabels = true;
Daniel Jasperf7935112012-12-03 18:12:45 +000072 return GoogleStyle;
73}
74
75struct OptimizationParameters {
76 unsigned PenaltyExtraLine;
77 unsigned PenaltyIndentLevel;
78};
79
80class UnwrappedLineFormatter {
81public:
82 UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
83 const UnwrappedLine &Line,
84 const std::vector<TokenAnnotation> &Annotations,
Alexander Kornienko870f9eb2012-12-04 17:27:50 +000085 tooling::Replacements &Replaces, bool StructuralError)
Daniel Jasperf7935112012-12-03 18:12:45 +000086 : Style(Style),
87 SourceMgr(SourceMgr),
88 Line(Line),
89 Annotations(Annotations),
Alexander Kornienko870f9eb2012-12-04 17:27:50 +000090 Replaces(Replaces),
91 StructuralError(StructuralError) {
Daniel Jasperf7935112012-12-03 18:12:45 +000092 Parameters.PenaltyExtraLine = 100;
93 Parameters.PenaltyIndentLevel = 5;
94 }
95
96 void format() {
Daniel Jaspere9de2602012-12-06 09:56:08 +000097 // Format first token and initialize indent.
Alexander Kornienko870f9eb2012-12-04 17:27:50 +000098 unsigned Indent = formatFirstToken();
Daniel Jaspere9de2602012-12-06 09:56:08 +000099
100 // Initialize state dependent on indent.
Daniel Jasperf7935112012-12-03 18:12:45 +0000101 IndentState State;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000102 State.Column = Indent;
Daniel Jasperf7935112012-12-03 18:12:45 +0000103 State.CtorInitializerOnNewLine = false;
104 State.InCtorInitializer = false;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000105 State.ConsumedTokens = 0;
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000106 State.Indent.push_back(Indent + 4);
107 State.LastSpace.push_back(Indent);
Daniel Jaspere9de2602012-12-06 09:56:08 +0000108 State.FirstLessLess.push_back(0);
109
110 // The first token has already been indented and thus consumed.
111 moveStateToNextToken(State);
Daniel Jasperf7935112012-12-03 18:12:45 +0000112
113 // Start iterating at 1 as we have correctly formatted of Token #0 above.
114 for (unsigned i = 1, n = Line.Tokens.size(); i != n; ++i) {
115 unsigned NoBreak = calcPenalty(State, false, UINT_MAX);
116 unsigned Break = calcPenalty(State, true, NoBreak);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000117 addTokenToState(Break < NoBreak, false, State);
Daniel Jasperf7935112012-12-03 18:12:45 +0000118 }
119 }
120
121private:
122 /// \brief The current state when indenting a unwrapped line.
123 ///
124 /// As the indenting tries different combinations this is copied by value.
125 struct IndentState {
126 /// \brief The number of used columns in the current line.
127 unsigned Column;
128
129 /// \brief The number of tokens already consumed.
130 unsigned ConsumedTokens;
131
132 /// \brief The position to which a specific parenthesis level needs to be
133 /// indented.
134 std::vector<unsigned> Indent;
135
Daniel Jaspere9de2602012-12-06 09:56:08 +0000136 /// \brief The position of the last space on each level.
137 ///
138 /// Used e.g. to break like:
139 /// functionCall(Parameter, otherCall(
140 /// OtherParameter));
Daniel Jasperf7935112012-12-03 18:12:45 +0000141 std::vector<unsigned> LastSpace;
142
Daniel Jaspere9de2602012-12-06 09:56:08 +0000143 /// \brief The position the first "<<" operator encountered on each level.
144 ///
145 /// Used to align "<<" operators. 0 if no such operator has been encountered
146 /// on a level.
147 std::vector<unsigned> FirstLessLess;
148
Daniel Jasperf7935112012-12-03 18:12:45 +0000149 bool CtorInitializerOnNewLine;
150 bool InCtorInitializer;
151
152 /// \brief Comparison operator to be able to used \c IndentState in \c map.
153 bool operator<(const IndentState &Other) const {
154 if (Other.ConsumedTokens != ConsumedTokens)
155 return Other.ConsumedTokens > ConsumedTokens;
156 if (Other.Column != Column)
157 return Other.Column > Column;
158 if (Other.Indent.size() != Indent.size())
159 return Other.Indent.size() > Indent.size();
160 for (int i = 0, e = Indent.size(); i != e; ++i) {
161 if (Other.Indent[i] != Indent[i])
162 return Other.Indent[i] > Indent[i];
163 }
164 if (Other.LastSpace.size() != LastSpace.size())
165 return Other.LastSpace.size() > LastSpace.size();
166 for (int i = 0, e = LastSpace.size(); i != e; ++i) {
167 if (Other.LastSpace[i] != LastSpace[i])
168 return Other.LastSpace[i] > LastSpace[i];
169 }
Daniel Jaspere9de2602012-12-06 09:56:08 +0000170 if (Other.FirstLessLess.size() != FirstLessLess.size())
171 return Other.FirstLessLess.size() > FirstLessLess.size();
172 for (int i = 0, e = FirstLessLess.size(); i != e; ++i) {
173 if (Other.FirstLessLess[i] != FirstLessLess[i])
174 return Other.FirstLessLess[i] > FirstLessLess[i];
175 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000176 return false;
177 }
178 };
179
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000180 /// \brief Appends the next token to \p State and updates information
181 /// necessary for indentation.
182 ///
183 /// Puts the token on the current line if \p Newline is \c true and adds a
184 /// line break and necessary indentation otherwise.
185 ///
186 /// If \p DryRun is \c false, also creates and stores the required
187 /// \c Replacement.
188 void addTokenToState(bool Newline, bool DryRun, IndentState &State) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000189 unsigned Index = State.ConsumedTokens;
190 const FormatToken &Current = Line.Tokens[Index];
191 const FormatToken &Previous = Line.Tokens[Index - 1];
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000192 unsigned ParenLevel = State.Indent.size() - 1;
Daniel Jasperf7935112012-12-03 18:12:45 +0000193
194 if (Newline) {
195 if (Current.Tok.is(tok::string_literal) &&
196 Previous.Tok.is(tok::string_literal))
197 State.Column = State.Column - Previous.Tok.getLength();
Daniel Jaspere9de2602012-12-06 09:56:08 +0000198 else if (Current.Tok.is(tok::lessless) &&
199 State.FirstLessLess[ParenLevel] != 0)
200 State.Column = State.FirstLessLess[ParenLevel];
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000201 else if (ParenLevel != 0 &&
202 (Previous.Tok.is(tok::equal) || Current.Tok.is(tok::arrow) ||
203 Current.Tok.is(tok::period)))
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000204 // Indent and extra 4 spaces after '=' as it continues an expression.
205 // Don't do that on the top level, as we already indent 4 there.
Daniel Jasperf7935112012-12-03 18:12:45 +0000206 State.Column = State.Indent[ParenLevel] + 4;
Daniel Jasper9b155472012-12-04 10:50:12 +0000207 else
Daniel Jasperf7935112012-12-03 18:12:45 +0000208 State.Column = State.Indent[ParenLevel];
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000209
Daniel Jasperf7935112012-12-03 18:12:45 +0000210 if (!DryRun)
211 replaceWhitespace(Current, 1, State.Column);
212
Daniel Jasper9b155472012-12-04 10:50:12 +0000213 State.LastSpace[ParenLevel] = State.Indent[ParenLevel];
Daniel Jasperf7935112012-12-03 18:12:45 +0000214 if (Current.Tok.is(tok::colon) &&
215 Annotations[Index].Type != TokenAnnotation::TT_ConditionalExpr) {
216 State.Indent[ParenLevel] += 2;
217 State.CtorInitializerOnNewLine = true;
218 State.InCtorInitializer = true;
219 }
220 } else {
221 unsigned Spaces = Annotations[Index].SpaceRequiredBefore ? 1 : 0;
222 if (Annotations[Index].Type == TokenAnnotation::TT_LineComment)
223 Spaces = 2;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000224
Daniel Jasperf7935112012-12-03 18:12:45 +0000225 if (!DryRun)
226 replaceWhitespace(Current, 0, Spaces);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000227
228 if (Previous.Tok.is(tok::l_paren) ||
229 Annotations[Index - 1].Type == TokenAnnotation::TT_TemplateOpener)
Daniel Jasperf7935112012-12-03 18:12:45 +0000230 State.Indent[ParenLevel] = State.Column;
231 if (Current.Tok.is(tok::colon)) {
232 State.Indent[ParenLevel] = State.Column + 3;
233 State.InCtorInitializer = true;
234 }
235 // Top-level spaces are exempt as that mostly leads to better results.
Daniel Jaspere9de2602012-12-06 09:56:08 +0000236 State.Column += Spaces;
Daniel Jasper9b155472012-12-04 10:50:12 +0000237 if (Spaces > 0 && ParenLevel != 0)
Daniel Jaspere9de2602012-12-06 09:56:08 +0000238 State.LastSpace[ParenLevel] = State.Column;
Daniel Jasperf7935112012-12-03 18:12:45 +0000239 }
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000240 moveStateToNextToken(State);
241 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000242
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000243 /// \brief Mark the next token as consumed in \p State and modify its stacks
244 /// accordingly.
245 void moveStateToNextToken(IndentState &State) {
246 unsigned Index = State.ConsumedTokens;
247 const FormatToken &Current = Line.Tokens[Index];
Daniel Jaspere9de2602012-12-06 09:56:08 +0000248 unsigned ParenLevel = State.Indent.size() - 1;
249
250 if (Current.Tok.is(tok::lessless) && State.FirstLessLess[ParenLevel] == 0)
251 State.FirstLessLess[ParenLevel] = State.Column;
252
253 State.Column += Current.Tok.getLength();
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000254
255 // If we encounter an opening (, [ or <, we add a level to our stacks to
256 // prepare for the following tokens.
257 if (Current.Tok.is(tok::l_paren) || Current.Tok.is(tok::l_square) ||
258 Annotations[Index].Type == TokenAnnotation::TT_TemplateOpener) {
259 State.Indent.push_back(4 + State.LastSpace.back());
260 State.LastSpace.push_back(State.LastSpace.back());
Daniel Jaspere9de2602012-12-06 09:56:08 +0000261 State.FirstLessLess.push_back(0);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000262 }
263
264 // If we encounter a closing ), ] or >, we can remove a level from our
265 // stacks.
Daniel Jasper9b155472012-12-04 10:50:12 +0000266 if (Current.Tok.is(tok::r_paren) || Current.Tok.is(tok::r_square) ||
267 Annotations[Index].Type == TokenAnnotation::TT_TemplateCloser) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000268 State.Indent.pop_back();
269 State.LastSpace.pop_back();
Daniel Jaspere9de2602012-12-06 09:56:08 +0000270 State.FirstLessLess.pop_back();
Daniel Jasperf7935112012-12-03 18:12:45 +0000271 }
272
273 ++State.ConsumedTokens;
274 }
275
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000276 /// \brief Calculate the panelty for splitting after the token at \p Index.
277 unsigned splitPenalty(unsigned Index) {
278 assert(Index < Line.Tokens.size() &&
279 "Tried to calculate penalty for splitting after the last token");
280 const FormatToken &Left = Line.Tokens[Index];
281 const FormatToken &Right = Line.Tokens[Index + 1];
282 if (Left.Tok.is(tok::semi))
Daniel Jasperf7935112012-12-03 18:12:45 +0000283 return 0;
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000284 if (Left.Tok.is(tok::comma))
Daniel Jasperf7935112012-12-03 18:12:45 +0000285 return 1;
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000286 if (Left.Tok.is(tok::equal) || Left.Tok.is(tok::l_paren) ||
287 Left.Tok.is(tok::pipepipe) || Left.Tok.is(tok::ampamp))
Daniel Jasperf7935112012-12-03 18:12:45 +0000288 return 2;
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000289
290 if (Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period))
291 return 200;
292
Daniel Jasperf7935112012-12-03 18:12:45 +0000293 return 3;
294 }
295
296 /// \brief Calculate the number of lines needed to format the remaining part
297 /// of the unwrapped line.
298 ///
299 /// Assumes the formatting so far has led to
300 /// the \c IndentState \p State. If \p NewLine is set, a new line will be
301 /// added after the previous token.
302 ///
303 /// \param StopAt is used for optimization. If we can determine that we'll
304 /// definitely need at least \p StopAt additional lines, we already know of a
305 /// better solution.
306 unsigned calcPenalty(IndentState State, bool NewLine, unsigned StopAt) {
307 // We are at the end of the unwrapped line, so we don't need any more lines.
308 if (State.ConsumedTokens >= Line.Tokens.size())
309 return 0;
310
311 if (!NewLine && Annotations[State.ConsumedTokens].MustBreakBefore)
312 return UINT_MAX;
313 if (NewLine && !Annotations[State.ConsumedTokens].CanBreakBefore)
314 return UINT_MAX;
315
316 if (State.ConsumedTokens > 0 && !NewLine &&
317 State.CtorInitializerOnNewLine &&
318 Line.Tokens[State.ConsumedTokens - 1].Tok.is(tok::comma))
319 return UINT_MAX;
320
321 if (NewLine && State.InCtorInitializer && !State.CtorInitializerOnNewLine)
322 return UINT_MAX;
323
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000324 unsigned CurrentPenalty = 0;
325 if (NewLine) {
326 CurrentPenalty += Parameters.PenaltyIndentLevel * State.Indent.size() +
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000327 Parameters.PenaltyExtraLine + splitPenalty(State.ConsumedTokens - 1);
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000328 }
329
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000330 addTokenToState(NewLine, true, State);
Daniel Jasperf7935112012-12-03 18:12:45 +0000331
332 // Exceeding column limit is bad.
333 if (State.Column > Style.ColumnLimit)
334 return UINT_MAX;
335
Daniel Jasperf7935112012-12-03 18:12:45 +0000336 if (StopAt <= CurrentPenalty)
337 return UINT_MAX;
338 StopAt -= CurrentPenalty;
339
Daniel Jasperf7935112012-12-03 18:12:45 +0000340 StateMap::iterator I = Memory.find(State);
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000341 if (I != Memory.end()) {
342 // If this state has already been examined, we can safely return the
343 // previous result if we
344 // - have not hit the optimatization (and thus returned UINT_MAX) OR
345 // - are now computing for a smaller or equal StopAt.
346 unsigned SavedResult = I->second.first;
347 unsigned SavedStopAt = I->second.second;
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000348 if (SavedResult != UINT_MAX)
349 return SavedResult + CurrentPenalty;
350 else if (StopAt <= SavedStopAt)
351 return UINT_MAX;
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000352 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000353
354 unsigned NoBreak = calcPenalty(State, false, StopAt);
355 unsigned WithBreak = calcPenalty(State, true, std::min(StopAt, NoBreak));
356 unsigned Result = std::min(NoBreak, WithBreak);
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000357
358 // We have to store 'Result' without adding 'CurrentPenalty' as the latter
359 // can depend on 'NewLine'.
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000360 Memory[State] = std::pair<unsigned, unsigned>(Result, StopAt);
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000361
362 return Result == UINT_MAX ? UINT_MAX : Result + CurrentPenalty;
Daniel Jasperf7935112012-12-03 18:12:45 +0000363 }
364
365 /// \brief Replaces the whitespace in front of \p Tok. Only call once for
366 /// each \c FormatToken.
367 void replaceWhitespace(const FormatToken &Tok, unsigned NewLines,
368 unsigned Spaces) {
369 Replaces.insert(tooling::Replacement(
370 SourceMgr, Tok.WhiteSpaceStart, Tok.WhiteSpaceLength,
371 std::string(NewLines, '\n') + std::string(Spaces, ' ')));
372 }
373
374 /// \brief Add a new line and the required indent before the first Token
Alexander Kornienkobc09a7e2012-12-05 13:56:52 +0000375 /// of the \c UnwrappedLine if there was no structural parsing error.
376 /// Returns the indent level of the \c UnwrappedLine.
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000377 unsigned formatFirstToken() {
Daniel Jasperf7935112012-12-03 18:12:45 +0000378 const FormatToken &Token = Line.Tokens[0];
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000379 if (!Token.WhiteSpaceStart.isValid() || StructuralError)
380 return SourceMgr.getSpellingColumnNumber(Token.Tok.getLocation()) - 1;
381
382 unsigned Newlines =
383 std::min(Token.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
384 unsigned Offset = SourceMgr.getFileOffset(Token.WhiteSpaceStart);
385 if (Newlines == 0 && Offset != 0)
386 Newlines = 1;
387 unsigned Indent = Line.Level * 2;
Alexander Kornienko2ca766f2012-12-10 16:34:48 +0000388 if ((Token.Tok.is(tok::kw_public) || Token.Tok.is(tok::kw_protected) ||
389 Token.Tok.is(tok::kw_private)) &&
390 static_cast<int>(Indent) + Style.AccessModifierOffset >= 0)
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000391 Indent += Style.AccessModifierOffset;
392 replaceWhitespace(Token, Newlines, Indent);
393 return Indent;
Daniel Jasperf7935112012-12-03 18:12:45 +0000394 }
395
396 FormatStyle Style;
397 SourceManager &SourceMgr;
398 const UnwrappedLine &Line;
399 const std::vector<TokenAnnotation> &Annotations;
400 tooling::Replacements &Replaces;
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000401 bool StructuralError;
Daniel Jasperf7935112012-12-03 18:12:45 +0000402
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000403 // A map from an indent state to a pair (Result, Used-StopAt).
404 typedef std::map<IndentState, std::pair<unsigned, unsigned> > StateMap;
405 StateMap Memory;
406
Daniel Jasperf7935112012-12-03 18:12:45 +0000407 OptimizationParameters Parameters;
408};
409
410/// \brief Determines extra information about the tokens comprising an
411/// \c UnwrappedLine.
412class TokenAnnotator {
413public:
414 TokenAnnotator(const UnwrappedLine &Line, const FormatStyle &Style,
415 SourceManager &SourceMgr)
416 : Line(Line),
417 Style(Style),
418 SourceMgr(SourceMgr) {
419 }
420
421 /// \brief A parser that gathers additional information about tokens.
422 ///
423 /// The \c TokenAnnotator tries to matches parenthesis and square brakets and
424 /// store a parenthesis levels. It also tries to resolve matching "<" and ">"
425 /// into template parameter lists.
426 class AnnotatingParser {
427 public:
Manuel Klimek6a5619d2012-12-03 20:55:42 +0000428 AnnotatingParser(const SmallVector<FormatToken, 16> &Tokens,
Daniel Jasperf7935112012-12-03 18:12:45 +0000429 std::vector<TokenAnnotation> &Annotations)
Manuel Klimek6a5619d2012-12-03 20:55:42 +0000430 : Tokens(Tokens),
Daniel Jasperf7935112012-12-03 18:12:45 +0000431 Annotations(Annotations),
432 Index(0) {
433 }
434
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000435 bool parseAngle() {
Daniel Jasperf7935112012-12-03 18:12:45 +0000436 while (Index < Tokens.size()) {
437 if (Tokens[Index].Tok.is(tok::greater)) {
Daniel Jasper9b155472012-12-04 10:50:12 +0000438 Annotations[Index].Type = TokenAnnotation::TT_TemplateCloser;
Daniel Jasperf7935112012-12-03 18:12:45 +0000439 next();
440 return true;
441 }
442 if (Tokens[Index].Tok.is(tok::r_paren) ||
443 Tokens[Index].Tok.is(tok::r_square))
444 return false;
445 if (Tokens[Index].Tok.is(tok::pipepipe) ||
446 Tokens[Index].Tok.is(tok::ampamp) ||
447 Tokens[Index].Tok.is(tok::question) ||
448 Tokens[Index].Tok.is(tok::colon))
449 return false;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000450 consumeToken();
Daniel Jasperf7935112012-12-03 18:12:45 +0000451 }
452 return false;
453 }
454
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000455 bool parseParens() {
Daniel Jasperf7935112012-12-03 18:12:45 +0000456 while (Index < Tokens.size()) {
457 if (Tokens[Index].Tok.is(tok::r_paren)) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000458 next();
459 return true;
460 }
461 if (Tokens[Index].Tok.is(tok::r_square))
462 return false;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000463 consumeToken();
Daniel Jasperf7935112012-12-03 18:12:45 +0000464 }
465 return false;
466 }
467
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000468 bool parseSquare() {
Daniel Jasperf7935112012-12-03 18:12:45 +0000469 while (Index < Tokens.size()) {
470 if (Tokens[Index].Tok.is(tok::r_square)) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000471 next();
472 return true;
473 }
474 if (Tokens[Index].Tok.is(tok::r_paren))
475 return false;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000476 consumeToken();
Daniel Jasperf7935112012-12-03 18:12:45 +0000477 }
478 return false;
479 }
480
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000481 bool parseConditional() {
Daniel Jasperf7935112012-12-03 18:12:45 +0000482 while (Index < Tokens.size()) {
483 if (Tokens[Index].Tok.is(tok::colon)) {
484 Annotations[Index].Type = TokenAnnotation::TT_ConditionalExpr;
485 next();
486 return true;
487 }
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000488 consumeToken();
Daniel Jasperf7935112012-12-03 18:12:45 +0000489 }
490 return false;
491 }
492
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000493 void consumeToken() {
Daniel Jasperf7935112012-12-03 18:12:45 +0000494 unsigned CurrentIndex = Index;
495 next();
496 switch (Tokens[CurrentIndex].Tok.getKind()) {
497 case tok::l_paren:
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000498 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +0000499 break;
500 case tok::l_square:
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000501 parseSquare();
Daniel Jasperf7935112012-12-03 18:12:45 +0000502 break;
503 case tok::less:
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000504 if (parseAngle())
Daniel Jasperf7935112012-12-03 18:12:45 +0000505 Annotations[CurrentIndex].Type = TokenAnnotation::TT_TemplateOpener;
506 else {
507 Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator;
508 Index = CurrentIndex + 1;
509 }
510 break;
511 case tok::greater:
512 Annotations[CurrentIndex].Type = TokenAnnotation::TT_BinaryOperator;
513 break;
514 case tok::kw_operator:
515 if (!Tokens[Index].Tok.is(tok::l_paren))
516 Annotations[Index].Type = TokenAnnotation::TT_OverloadedOperator;
517 next();
518 break;
519 case tok::question:
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000520 parseConditional();
Daniel Jasperf7935112012-12-03 18:12:45 +0000521 break;
522 default:
523 break;
524 }
525 }
526
527 void parseLine() {
528 while (Index < Tokens.size()) {
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000529 consumeToken();
Daniel Jasperf7935112012-12-03 18:12:45 +0000530 }
531 }
532
533 void next() {
534 ++Index;
535 }
536
537 private:
Daniel Jasperf7935112012-12-03 18:12:45 +0000538 const SmallVector<FormatToken, 16> &Tokens;
539 std::vector<TokenAnnotation> &Annotations;
540 unsigned Index;
541 };
542
543 void annotate() {
544 Annotations.clear();
545 for (int i = 0, e = Line.Tokens.size(); i != e; ++i) {
546 Annotations.push_back(TokenAnnotation());
547 }
548
Manuel Klimek6a5619d2012-12-03 20:55:42 +0000549 AnnotatingParser Parser(Line.Tokens, Annotations);
Daniel Jasperf7935112012-12-03 18:12:45 +0000550 Parser.parseLine();
551
552 determineTokenTypes();
553
554 for (int i = 1, e = Line.Tokens.size(); i != e; ++i) {
555 TokenAnnotation &Annotation = Annotations[i];
556
557 Annotation.CanBreakBefore =
558 canBreakBetween(Line.Tokens[i - 1], Line.Tokens[i]);
559
560 if (Line.Tokens[i].Tok.is(tok::colon)) {
Daniel Jasper55b6b642012-12-05 16:24:48 +0000561 Annotation.SpaceRequiredBefore =
562 Line.Tokens[0].Tok.isNot(tok::kw_case) && i != e - 1;
Daniel Jasperf7935112012-12-03 18:12:45 +0000563 } else if (Annotations[i - 1].Type == TokenAnnotation::TT_UnaryOperator) {
564 Annotation.SpaceRequiredBefore = false;
565 } else if (Annotation.Type == TokenAnnotation::TT_UnaryOperator) {
566 Annotation.SpaceRequiredBefore =
Daniel Jasper8b529712012-12-04 13:02:32 +0000567 Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&
568 Line.Tokens[i - 1].Tok.isNot(tok::l_square);
Daniel Jasperf7935112012-12-03 18:12:45 +0000569 } else if (Line.Tokens[i - 1].Tok.is(tok::greater) &&
570 Line.Tokens[i].Tok.is(tok::greater)) {
Daniel Jasper8b529712012-12-04 13:02:32 +0000571 if (Annotation.Type == TokenAnnotation::TT_TemplateCloser &&
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000572 Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser)
Daniel Jasper9b155472012-12-04 10:50:12 +0000573 Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater;
Daniel Jasperf7935112012-12-03 18:12:45 +0000574 else
575 Annotation.SpaceRequiredBefore = false;
576 } else if (
577 Annotation.Type == TokenAnnotation::TT_BinaryOperator ||
578 Annotations[i - 1].Type == TokenAnnotation::TT_BinaryOperator) {
579 Annotation.SpaceRequiredBefore = true;
580 } else if (
Daniel Jasper9b155472012-12-04 10:50:12 +0000581 Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser &&
Daniel Jasperf7935112012-12-03 18:12:45 +0000582 Line.Tokens[i].Tok.is(tok::l_paren)) {
583 Annotation.SpaceRequiredBefore = false;
Daniel Jasper8b529712012-12-04 13:02:32 +0000584 } else if (Line.Tokens[i].Tok.is(tok::less) &&
585 Line.Tokens[0].Tok.is(tok::hash)) {
586 Annotation.SpaceRequiredBefore = true;
Daniel Jasperf7935112012-12-03 18:12:45 +0000587 } else {
588 Annotation.SpaceRequiredBefore =
589 spaceRequiredBetween(Line.Tokens[i - 1].Tok, Line.Tokens[i].Tok);
590 }
591
592 if (Annotations[i - 1].Type == TokenAnnotation::TT_LineComment ||
593 (Line.Tokens[i].Tok.is(tok::string_literal) &&
594 Line.Tokens[i - 1].Tok.is(tok::string_literal))) {
595 Annotation.MustBreakBefore = true;
596 }
597
598 if (Annotation.MustBreakBefore)
599 Annotation.CanBreakBefore = true;
600 }
601 }
602
603 const std::vector<TokenAnnotation> &getAnnotations() {
604 return Annotations;
605 }
606
607private:
608 void determineTokenTypes() {
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000609 bool AssignmentEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000610 for (int i = 0, e = Line.Tokens.size(); i != e; ++i) {
611 TokenAnnotation &Annotation = Annotations[i];
612 const FormatToken &Tok = Line.Tokens[i];
613
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000614 if (Tok.Tok.is(tok::equal) || Tok.Tok.is(tok::plusequal) ||
615 Tok.Tok.is(tok::minusequal) || Tok.Tok.is(tok::starequal) ||
616 Tok.Tok.is(tok::slashequal))
617 AssignmentEncountered = true;
Daniel Jasper426702d2012-12-05 07:51:39 +0000618
Daniel Jasperf7935112012-12-03 18:12:45 +0000619 if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp))
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000620 Annotation.Type = determineStarAmpUsage(i, AssignmentEncountered);
Daniel Jasper8b529712012-12-04 13:02:32 +0000621 else if (isUnaryOperator(i))
Daniel Jasperf7935112012-12-03 18:12:45 +0000622 Annotation.Type = TokenAnnotation::TT_UnaryOperator;
623 else if (isBinaryOperator(Line.Tokens[i]))
624 Annotation.Type = TokenAnnotation::TT_BinaryOperator;
625 else if (Tok.Tok.is(tok::comment)) {
626 StringRef Data(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
627 Tok.Tok.getLength());
628 if (Data.startswith("//"))
629 Annotation.Type = TokenAnnotation::TT_LineComment;
630 else
631 Annotation.Type = TokenAnnotation::TT_BlockComment;
632 }
633 }
634 }
635
Daniel Jasper8b529712012-12-04 13:02:32 +0000636 bool isUnaryOperator(unsigned Index) {
637 const Token &Tok = Line.Tokens[Index].Tok;
Daniel Jasper26333c32012-12-06 13:16:39 +0000638
639 // '++', '--' and '!' are always unary operators.
640 if (Tok.is(tok::minusminus) || Tok.is(tok::plusplus) ||
641 Tok.is(tok::exclaim))
642 return true;
643
644 // The other possible unary operators are '+' and '-' as we
645 // determine the usage of '*' and '&' in determineStarAmpUsage().
Daniel Jasper8b529712012-12-04 13:02:32 +0000646 if (Tok.isNot(tok::minus) && Tok.isNot(tok::plus))
647 return false;
Daniel Jasper26333c32012-12-06 13:16:39 +0000648
649 // Use heuristics to recognize unary operators.
Daniel Jasper8b529712012-12-04 13:02:32 +0000650 const Token &PreviousTok = Line.Tokens[Index - 1].Tok;
651 if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) ||
652 PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square))
653 return true;
Daniel Jasper26333c32012-12-06 13:16:39 +0000654
655 // Fall back to marking the token as binary operator.
Daniel Jasper8b529712012-12-04 13:02:32 +0000656 return Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator;
657 }
658
Daniel Jasperf7935112012-12-03 18:12:45 +0000659 bool isBinaryOperator(const FormatToken &Tok) {
660 switch (Tok.Tok.getKind()) {
661 case tok::equal:
662 case tok::equalequal:
Daniel Jasper426702d2012-12-05 07:51:39 +0000663 case tok::exclaimequal:
Daniel Jasperf7935112012-12-03 18:12:45 +0000664 case tok::star:
665 //case tok::amp:
666 case tok::plus:
667 case tok::slash:
668 case tok::minus:
669 case tok::ampamp:
670 case tok::pipe:
671 case tok::pipepipe:
672 case tok::percent:
673 return true;
674 default:
675 return false;
676 }
677 }
678
Daniel Jasper426702d2012-12-05 07:51:39 +0000679 TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index,
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000680 bool AssignmentEncountered) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000681 if (Index == Annotations.size())
682 return TokenAnnotation::TT_Unknown;
683
684 if (Index == 0 || Line.Tokens[Index - 1].Tok.is(tok::l_paren) ||
685 Line.Tokens[Index - 1].Tok.is(tok::comma) ||
686 Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator)
687 return TokenAnnotation::TT_UnaryOperator;
688
689 if (Line.Tokens[Index - 1].Tok.isLiteral() ||
690 Line.Tokens[Index + 1].Tok.isLiteral())
691 return TokenAnnotation::TT_BinaryOperator;
692
Daniel Jasper426702d2012-12-05 07:51:39 +0000693 // It is very unlikely that we are going to find a pointer or reference type
694 // definition on the RHS of an assignment.
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000695 if (AssignmentEncountered)
Daniel Jasper426702d2012-12-05 07:51:39 +0000696 return TokenAnnotation::TT_BinaryOperator;
697
Daniel Jasperf7935112012-12-03 18:12:45 +0000698 return TokenAnnotation::TT_PointerOrReference;
699 }
700
701 bool isIfForOrWhile(Token Tok) {
702 return Tok.is(tok::kw_if) || Tok.is(tok::kw_for) || Tok.is(tok::kw_while);
703 }
704
705 bool spaceRequiredBetween(Token Left, Token Right) {
Daniel Jaspera4396862012-12-10 18:59:13 +0000706 if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma))
707 return false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000708 if (Left.is(tok::kw_template) && Right.is(tok::less))
709 return true;
710 if (Left.is(tok::arrow) || Right.is(tok::arrow))
711 return false;
712 if (Left.is(tok::exclaim) || Left.is(tok::tilde))
713 return false;
714 if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less))
715 return false;
Daniel Jasper27234032012-12-07 09:52:15 +0000716 if (Right.is(tok::amp) || Right.is(tok::star))
717 return Left.isLiteral() ||
718 (Left.isNot(tok::star) && Left.isNot(tok::amp) &&
719 !Style.PointerAndReferenceBindToType);
Daniel Jasperf7935112012-12-03 18:12:45 +0000720 if (Left.is(tok::amp) || Left.is(tok::star))
721 return Right.isLiteral() || Style.PointerAndReferenceBindToType;
722 if (Right.is(tok::star) && Left.is(tok::l_paren))
723 return false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000724 if (Left.is(tok::l_square) || Right.is(tok::l_square) ||
725 Right.is(tok::r_square))
726 return false;
Daniel Jasper27234032012-12-07 09:52:15 +0000727 if (Left.is(tok::coloncolon) ||
728 (Right.is(tok::coloncolon) &&
729 (Left.is(tok::identifier) || Left.is(tok::greater))))
Daniel Jasperf7935112012-12-03 18:12:45 +0000730 return false;
731 if (Left.is(tok::period) || Right.is(tok::period))
732 return false;
733 if (Left.is(tok::colon) || Right.is(tok::colon))
734 return true;
735 if ((Left.is(tok::plusplus) && Right.isAnyIdentifier()) ||
736 (Left.isAnyIdentifier() && Right.is(tok::plusplus)) ||
737 (Left.is(tok::minusminus) && Right.isAnyIdentifier()) ||
738 (Left.isAnyIdentifier() && Right.is(tok::minusminus)))
739 return false;
740 if (Left.is(tok::l_paren))
741 return false;
742 if (Left.is(tok::hash))
743 return false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000744 if (Right.is(tok::l_paren)) {
745 return !Left.isAnyIdentifier() || isIfForOrWhile(Left);
746 }
747 return true;
748 }
749
750 bool canBreakBetween(const FormatToken &Left, const FormatToken &Right) {
Daniel Jaspere25509f2012-12-17 11:29:41 +0000751 if (Right.Tok.is(tok::r_paren) || Right.Tok.is(tok::l_brace) ||
752 Right.Tok.is(tok::comment) || Right.Tok.is(tok::greater))
Daniel Jasperf7935112012-12-03 18:12:45 +0000753 return false;
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000754 if (isBinaryOperator(Left) || Right.Tok.is(tok::lessless) ||
755 Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period))
Daniel Jaspere9de2602012-12-06 09:56:08 +0000756 return true;
Daniel Jaspere25509f2012-12-17 11:29:41 +0000757 return Right.Tok.is(tok::colon) || Left.Tok.is(tok::comma) || Left.Tok.is(
758 tok::semi) || Left.Tok.is(tok::equal) || Left.Tok.is(tok::ampamp) ||
759 Left.Tok.is(tok::pipepipe) || Left.Tok.is(tok::l_brace) ||
Daniel Jasperf7935112012-12-03 18:12:45 +0000760 (Left.Tok.is(tok::l_paren) && !Right.Tok.is(tok::r_paren));
761 }
762
763 const UnwrappedLine &Line;
764 FormatStyle Style;
765 SourceManager &SourceMgr;
766 std::vector<TokenAnnotation> Annotations;
767};
768
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000769class LexerBasedFormatTokenSource : public FormatTokenSource {
770public:
771 LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr)
772 : GreaterStashed(false),
773 Lex(Lex),
774 SourceMgr(SourceMgr),
775 IdentTable(Lex.getLangOpts()) {
776 Lex.SetKeepWhitespaceMode(true);
777 }
778
779 virtual FormatToken getNextToken() {
780 if (GreaterStashed) {
781 FormatTok.NewlinesBefore = 0;
782 FormatTok.WhiteSpaceStart =
783 FormatTok.Tok.getLocation().getLocWithOffset(1);
784 FormatTok.WhiteSpaceLength = 0;
785 GreaterStashed = false;
786 return FormatTok;
787 }
788
789 FormatTok = FormatToken();
790 Lex.LexFromRawLexer(FormatTok.Tok);
791 FormatTok.WhiteSpaceStart = FormatTok.Tok.getLocation();
792
793 // Consume and record whitespace until we find a significant token.
794 while (FormatTok.Tok.is(tok::unknown)) {
795 FormatTok.NewlinesBefore += tokenText(FormatTok.Tok).count('\n');
796 FormatTok.WhiteSpaceLength += FormatTok.Tok.getLength();
797
798 if (FormatTok.Tok.is(tok::eof))
799 return FormatTok;
800 Lex.LexFromRawLexer(FormatTok.Tok);
801 }
802
803 if (FormatTok.Tok.is(tok::raw_identifier)) {
804 const IdentifierInfo &Info = IdentTable.get(tokenText(FormatTok.Tok));
805 FormatTok.Tok.setKind(Info.getTokenID());
806 }
807
808 if (FormatTok.Tok.is(tok::greatergreater)) {
809 FormatTok.Tok.setKind(tok::greater);
810 GreaterStashed = true;
811 }
812
813 return FormatTok;
814 }
815
816private:
817 FormatToken FormatTok;
818 bool GreaterStashed;
819 Lexer &Lex;
820 SourceManager &SourceMgr;
821 IdentifierTable IdentTable;
822
823 /// Returns the text of \c FormatTok.
824 StringRef tokenText(Token &Tok) {
825 return StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
826 Tok.getLength());
827 }
828};
829
Daniel Jasperf7935112012-12-03 18:12:45 +0000830class Formatter : public UnwrappedLineConsumer {
831public:
832 Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
833 const std::vector<CharSourceRange> &Ranges)
834 : Style(Style),
835 Lex(Lex),
836 SourceMgr(SourceMgr),
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000837 Ranges(Ranges),
838 StructuralError(false) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000839 }
840
Daniel Jasper61bd3a12012-12-04 21:05:31 +0000841 virtual ~Formatter() {
842 }
843
Daniel Jasperf7935112012-12-03 18:12:45 +0000844 tooling::Replacements format() {
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000845 LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
846 UnwrappedLineParser Parser(Style, Tokens, *this);
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000847 StructuralError = Parser.parse();
848 for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),
849 E = UnwrappedLines.end();
850 I != E; ++I)
Alexander Kornienkobc09a7e2012-12-05 13:56:52 +0000851 formatUnwrappedLine(*I);
Daniel Jasperf7935112012-12-03 18:12:45 +0000852 return Replaces;
853 }
854
855private:
Alexander Kornienkobc09a7e2012-12-05 13:56:52 +0000856 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000857 UnwrappedLines.push_back(TheLine);
858 }
859
Alexander Kornienkobc09a7e2012-12-05 13:56:52 +0000860 void formatUnwrappedLine(const UnwrappedLine &TheLine) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000861 if (TheLine.Tokens.size() == 0)
862 return;
863
864 CharSourceRange LineRange =
865 CharSourceRange::getTokenRange(TheLine.Tokens.front().Tok.getLocation(),
866 TheLine.Tokens.back().Tok.getLocation());
867
868 for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
869 if (SourceMgr.isBeforeInTranslationUnit(LineRange.getEnd(),
870 Ranges[i].getBegin()) ||
871 SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
872 LineRange.getBegin()))
873 continue;
874
875 TokenAnnotator Annotator(TheLine, Style, SourceMgr);
876 Annotator.annotate();
877 UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine,
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000878 Annotator.getAnnotations(), Replaces,
879 StructuralError);
Daniel Jasperf7935112012-12-03 18:12:45 +0000880 Formatter.format();
881 return;
882 }
883 }
884
885 FormatStyle Style;
886 Lexer &Lex;
887 SourceManager &SourceMgr;
888 tooling::Replacements Replaces;
889 std::vector<CharSourceRange> Ranges;
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000890 std::vector<UnwrappedLine> UnwrappedLines;
891 bool StructuralError;
Daniel Jasperf7935112012-12-03 18:12:45 +0000892};
893
894tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
895 SourceManager &SourceMgr,
896 std::vector<CharSourceRange> Ranges) {
897 Formatter formatter(Style, Lex, SourceMgr, Ranges);
898 return formatter.format();
899}
900
901} // namespace format
902} // namespace clang