blob: d14c1ce5b55a55bce9a24e8b253ee070fc8c3cbc [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"
Daniel Jasperab7654e2012-12-21 10:20:02 +000022#include "clang/Basic/OperatorPrecedence.h"
Daniel Jasperf7935112012-12-03 18:12:45 +000023#include "clang/Lex/Lexer.h"
24
Daniel Jasper8b529712012-12-04 13:02:32 +000025#include <string>
26
Daniel Jasperf7935112012-12-03 18:12:45 +000027namespace clang {
28namespace format {
29
30// FIXME: Move somewhere sane.
31struct TokenAnnotation {
Daniel Jasperaa1c9202012-12-05 14:57:28 +000032 enum TokenType {
33 TT_Unknown,
34 TT_TemplateOpener,
35 TT_TemplateCloser,
36 TT_BinaryOperator,
37 TT_UnaryOperator,
Daniel Jasper8dd40472012-12-21 09:41:31 +000038 TT_TrailingUnaryOperator,
Daniel Jasperaa1c9202012-12-05 14:57:28 +000039 TT_OverloadedOperator,
40 TT_PointerOrReference,
41 TT_ConditionalExpr,
Daniel Jasper2af6bbe2012-12-18 21:05:13 +000042 TT_CtorInitializerColon,
Daniel Jasperaa1c9202012-12-05 14:57:28 +000043 TT_LineComment,
Fariborz Jahanian68a542a2012-12-20 19:54:13 +000044 TT_BlockComment,
45 TT_ObjCMethodSpecifier
Daniel Jasperaa1c9202012-12-05 14:57:28 +000046 };
Daniel Jasperf7935112012-12-03 18:12:45 +000047
48 TokenType Type;
49
Daniel Jasperf7935112012-12-03 18:12:45 +000050 bool SpaceRequiredBefore;
51 bool CanBreakBefore;
52 bool MustBreakBefore;
53};
54
55using llvm::MutableArrayRef;
56
57FormatStyle 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 Kornienko578fdd82012-12-06 18:03:27 +000064 LLVMStyle.IndentCaseLabels = false;
Daniel Jasperf7935112012-12-03 18:12:45 +000065 return LLVMStyle;
66}
67
68FormatStyle 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 Kornienko578fdd82012-12-06 18:03:27 +000075 GoogleStyle.IndentCaseLabels = true;
Daniel Jasperf7935112012-12-03 18:12:45 +000076 return GoogleStyle;
77}
78
79struct OptimizationParameters {
Daniel Jasperf7935112012-12-03 18:12:45 +000080 unsigned PenaltyIndentLevel;
81};
82
83class UnwrappedLineFormatter {
84public:
85 UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
86 const UnwrappedLine &Line,
87 const std::vector<TokenAnnotation> &Annotations,
Alexander Kornienko870f9eb2012-12-04 17:27:50 +000088 tooling::Replacements &Replaces, bool StructuralError)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +000089 : Style(Style), SourceMgr(SourceMgr), Line(Line),
90 Annotations(Annotations), Replaces(Replaces),
Alexander Kornienko870f9eb2012-12-04 17:27:50 +000091 StructuralError(StructuralError) {
Daniel Jasperf7935112012-12-03 18:12:45 +000092 Parameters.PenaltyIndentLevel = 5;
93 }
94
95 void format() {
Daniel Jaspere9de2602012-12-06 09:56:08 +000096 // Format first token and initialize indent.
Alexander Kornienko870f9eb2012-12-04 17:27:50 +000097 unsigned Indent = formatFirstToken();
Daniel Jaspere9de2602012-12-06 09:56:08 +000098
99 // Initialize state dependent on indent.
Daniel Jasperf7935112012-12-03 18:12:45 +0000100 IndentState State;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000101 State.Column = Indent;
Daniel Jaspere9de2602012-12-06 09:56:08 +0000102 State.ConsumedTokens = 0;
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000103 State.Indent.push_back(Indent + 4);
104 State.LastSpace.push_back(Indent);
Daniel Jaspere9de2602012-12-06 09:56:08 +0000105 State.FirstLessLess.push_back(0);
106
107 // The first token has already been indented and thus consumed.
108 moveStateToNextToken(State);
Daniel Jasperf7935112012-12-03 18:12:45 +0000109
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000110 // 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 Jasperf7935112012-12-03 18:12:45 +0000128 // 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 Jasper2af6bbe2012-12-18 21:05:13 +0000130 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 Jasperf7935112012-12-03 18:12:45 +0000137 }
138 }
139
140private:
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 Jaspere9de2602012-12-06 09:56:08 +0000155 /// \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 Jasperf7935112012-12-03 18:12:45 +0000160 std::vector<unsigned> LastSpace;
161
Daniel Jaspere9de2602012-12-06 09:56:08 +0000162 /// \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 Jasperf7935112012-12-03 18:12:45 +0000168 /// \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 Jaspere9de2602012-12-06 09:56:08 +0000186 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 Jasperf7935112012-12-03 18:12:45 +0000192 return false;
193 }
194 };
195
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000196 /// \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 Jasperf7935112012-12-03 18:12:45 +0000205 unsigned Index = State.ConsumedTokens;
206 const FormatToken &Current = Line.Tokens[Index];
207 const FormatToken &Previous = Line.Tokens[Index - 1];
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000208 unsigned ParenLevel = State.Indent.size() - 1;
Daniel Jasperf7935112012-12-03 18:12:45 +0000209
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 Jaspere9de2602012-12-06 09:56:08 +0000214 else if (Current.Tok.is(tok::lessless) &&
215 State.FirstLessLess[ParenLevel] != 0)
216 State.Column = State.FirstLessLess[ParenLevel];
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000217 else if (ParenLevel != 0 &&
218 (Previous.Tok.is(tok::equal) || Current.Tok.is(tok::arrow) ||
219 Current.Tok.is(tok::period)))
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000220 // 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 Jasperf7935112012-12-03 18:12:45 +0000222 State.Column = State.Indent[ParenLevel] + 4;
Daniel Jasper9b155472012-12-04 10:50:12 +0000223 else
Daniel Jasperf7935112012-12-03 18:12:45 +0000224 State.Column = State.Indent[ParenLevel];
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000225
Daniel Jasperf7935112012-12-03 18:12:45 +0000226 if (!DryRun)
227 replaceWhitespace(Current, 1, State.Column);
228
Daniel Jasper9b155472012-12-04 10:50:12 +0000229 State.LastSpace[ParenLevel] = State.Indent[ParenLevel];
Daniel Jasperf7935112012-12-03 18:12:45 +0000230 if (Current.Tok.is(tok::colon) &&
Fariborz Jahanian68a542a2012-12-20 19:54:13 +0000231 Annotations[Index].Type != TokenAnnotation::TT_ConditionalExpr &&
232 Annotations[0].Type != TokenAnnotation::TT_ObjCMethodSpecifier)
Daniel Jasperf7935112012-12-03 18:12:45 +0000233 State.Indent[ParenLevel] += 2;
Daniel Jasperf7935112012-12-03 18:12:45 +0000234 } else {
235 unsigned Spaces = Annotations[Index].SpaceRequiredBefore ? 1 : 0;
236 if (Annotations[Index].Type == TokenAnnotation::TT_LineComment)
237 Spaces = 2;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000238
Daniel Jasperf7935112012-12-03 18:12:45 +0000239 if (!DryRun)
240 replaceWhitespace(Current, 0, Spaces);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000241
242 if (Previous.Tok.is(tok::l_paren) ||
243 Annotations[Index - 1].Type == TokenAnnotation::TT_TemplateOpener)
Daniel Jasperf7935112012-12-03 18:12:45 +0000244 State.Indent[ParenLevel] = State.Column;
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000245
Daniel Jasperf7935112012-12-03 18:12:45 +0000246 // Top-level spaces are exempt as that mostly leads to better results.
Daniel Jaspere9de2602012-12-06 09:56:08 +0000247 State.Column += Spaces;
Daniel Jasper9b155472012-12-04 10:50:12 +0000248 if (Spaces > 0 && ParenLevel != 0)
Daniel Jaspere9de2602012-12-06 09:56:08 +0000249 State.LastSpace[ParenLevel] = State.Column;
Daniel Jasperf7935112012-12-03 18:12:45 +0000250 }
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000251 moveStateToNextToken(State);
252 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000253
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000254 /// \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 Jaspere9de2602012-12-06 09:56:08 +0000259 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 Jasper6021c4a2012-12-04 14:54:30 +0000265
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 Jaspere9de2602012-12-06 09:56:08 +0000272 State.FirstLessLess.push_back(0);
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000273 }
274
275 // If we encounter a closing ), ] or >, we can remove a level from our
276 // stacks.
Daniel Jasper9b155472012-12-04 10:50:12 +0000277 if (Current.Tok.is(tok::r_paren) || Current.Tok.is(tok::r_square) ||
278 Annotations[Index].Type == TokenAnnotation::TT_TemplateCloser) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000279 State.Indent.pop_back();
280 State.LastSpace.pop_back();
Daniel Jaspere9de2602012-12-06 09:56:08 +0000281 State.FirstLessLess.pop_back();
Daniel Jasperf7935112012-12-03 18:12:45 +0000282 }
283
284 ++State.ConsumedTokens;
285 }
286
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000287 /// \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 Jasper2af6bbe2012-12-18 21:05:13 +0000293 if (Left.Tok.is(tok::semi) || Left.Tok.is(tok::comma))
Daniel Jasperf7935112012-12-03 18:12:45 +0000294 return 0;
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000295 if (Left.Tok.is(tok::equal) || Left.Tok.is(tok::l_paren) ||
296 Left.Tok.is(tok::pipepipe) || Left.Tok.is(tok::ampamp))
Daniel Jasperf7935112012-12-03 18:12:45 +0000297 return 2;
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000298
299 if (Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period))
300 return 200;
301
Daniel Jasperf7935112012-12-03 18:12:45 +0000302 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 Jasperaa1c9202012-12-05 14:57:28 +0000325 unsigned CurrentPenalty = 0;
326 if (NewLine) {
327 CurrentPenalty += Parameters.PenaltyIndentLevel * State.Indent.size() +
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000328 splitPenalty(State.ConsumedTokens - 1);
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000329 }
330
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000331 addTokenToState(NewLine, true, State);
Daniel Jasperf7935112012-12-03 18:12:45 +0000332
333 // Exceeding column limit is bad.
334 if (State.Column > Style.ColumnLimit)
335 return UINT_MAX;
336
Daniel Jasperf7935112012-12-03 18:12:45 +0000337 if (StopAt <= CurrentPenalty)
338 return UINT_MAX;
339 StopAt -= CurrentPenalty;
340
Daniel Jasperf7935112012-12-03 18:12:45 +0000341 StateMap::iterator I = Memory.find(State);
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000342 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 Jasper5485d0c2012-12-17 14:34:14 +0000349 if (SavedResult != UINT_MAX)
350 return SavedResult + CurrentPenalty;
351 else if (StopAt <= SavedStopAt)
352 return UINT_MAX;
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000353 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000354
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 Jasper5485d0c2012-12-17 14:34:14 +0000358
359 // We have to store 'Result' without adding 'CurrentPenalty' as the latter
360 // can depend on 'NewLine'.
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000361 Memory[State] = std::pair<unsigned, unsigned>(Result, StopAt);
Daniel Jasper5485d0c2012-12-17 14:34:14 +0000362
363 return Result == UINT_MAX ? UINT_MAX : Result + CurrentPenalty;
Daniel Jasperf7935112012-12-03 18:12:45 +0000364 }
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 Kornienkobc09a7e2012-12-05 13:56:52 +0000376 /// of the \c UnwrappedLine if there was no structural parsing error.
377 /// Returns the indent level of the \c UnwrappedLine.
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000378 unsigned formatFirstToken() {
Daniel Jasperf7935112012-12-03 18:12:45 +0000379 const FormatToken &Token = Line.Tokens[0];
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000380 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 Kornienko2ca766f2012-12-10 16:34:48 +0000389 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 Kornienko870f9eb2012-12-04 17:27:50 +0000392 Indent += Style.AccessModifierOffset;
393 replaceWhitespace(Token, Newlines, Indent);
394 return Indent;
Daniel Jasperf7935112012-12-03 18:12:45 +0000395 }
396
397 FormatStyle Style;
398 SourceManager &SourceMgr;
399 const UnwrappedLine &Line;
400 const std::vector<TokenAnnotation> &Annotations;
401 tooling::Replacements &Replaces;
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000402 bool StructuralError;
Daniel Jasperf7935112012-12-03 18:12:45 +0000403
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000404 // 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 Jasperf7935112012-12-03 18:12:45 +0000408 OptimizationParameters Parameters;
409};
410
411/// \brief Determines extra information about the tokens comprising an
412/// \c UnwrappedLine.
413class TokenAnnotator {
414public:
415 TokenAnnotator(const UnwrappedLine &Line, const FormatStyle &Style,
416 SourceManager &SourceMgr)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000417 : Line(Line), Style(Style), SourceMgr(SourceMgr) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000418 }
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 Klimek6a5619d2012-12-03 20:55:42 +0000427 AnnotatingParser(const SmallVector<FormatToken, 16> &Tokens,
Daniel Jasperf7935112012-12-03 18:12:45 +0000428 std::vector<TokenAnnotation> &Annotations)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000429 : Tokens(Tokens), Annotations(Annotations), Index(0) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000430 }
431
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000432 bool parseAngle() {
Daniel Jasperf7935112012-12-03 18:12:45 +0000433 while (Index < Tokens.size()) {
434 if (Tokens[Index].Tok.is(tok::greater)) {
Daniel Jasper9b155472012-12-04 10:50:12 +0000435 Annotations[Index].Type = TokenAnnotation::TT_TemplateCloser;
Daniel Jasperf7935112012-12-03 18:12:45 +0000436 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 Jasper6021c4a2012-12-04 14:54:30 +0000447 consumeToken();
Daniel Jasperf7935112012-12-03 18:12:45 +0000448 }
449 return false;
450 }
451
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000452 bool parseParens() {
Daniel Jasperf7935112012-12-03 18:12:45 +0000453 while (Index < Tokens.size()) {
454 if (Tokens[Index].Tok.is(tok::r_paren)) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000455 next();
456 return true;
457 }
458 if (Tokens[Index].Tok.is(tok::r_square))
459 return false;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000460 consumeToken();
Daniel Jasperf7935112012-12-03 18:12:45 +0000461 }
462 return false;
463 }
464
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000465 bool parseSquare() {
Daniel Jasperf7935112012-12-03 18:12:45 +0000466 while (Index < Tokens.size()) {
467 if (Tokens[Index].Tok.is(tok::r_square)) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000468 next();
469 return true;
470 }
471 if (Tokens[Index].Tok.is(tok::r_paren))
472 return false;
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000473 consumeToken();
Daniel Jasperf7935112012-12-03 18:12:45 +0000474 }
475 return false;
476 }
477
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000478 bool parseConditional() {
Daniel Jasperf7935112012-12-03 18:12:45 +0000479 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 Jasper6021c4a2012-12-04 14:54:30 +0000485 consumeToken();
Daniel Jasperf7935112012-12-03 18:12:45 +0000486 }
487 return false;
488 }
489
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000490 void consumeToken() {
Daniel Jasperf7935112012-12-03 18:12:45 +0000491 unsigned CurrentIndex = Index;
492 next();
493 switch (Tokens[CurrentIndex].Tok.getKind()) {
494 case tok::l_paren:
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000495 parseParens();
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000496 if (Index < Tokens.size() && Tokens[Index].Tok.is(tok::colon)) {
497 Annotations[Index].Type = TokenAnnotation::TT_CtorInitializerColon;
498 next();
499 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000500 break;
501 case tok::l_square:
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000502 parseSquare();
Daniel Jasperf7935112012-12-03 18:12:45 +0000503 break;
504 case tok::less:
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000505 if (parseAngle())
Daniel Jasperf7935112012-12-03 18:12:45 +0000506 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 Jasper6021c4a2012-12-04 14:54:30 +0000521 parseConditional();
Daniel Jasperf7935112012-12-03 18:12:45 +0000522 break;
523 default:
524 break;
525 }
526 }
527
528 void parseLine() {
529 while (Index < Tokens.size()) {
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000530 consumeToken();
Daniel Jasperf7935112012-12-03 18:12:45 +0000531 }
532 }
533
534 void next() {
535 ++Index;
536 }
537
538 private:
Daniel Jasperf7935112012-12-03 18:12:45 +0000539 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 Klimek6a5619d2012-12-03 20:55:42 +0000550 AnnotatingParser Parser(Line.Tokens, Annotations);
Daniel Jasperf7935112012-12-03 18:12:45 +0000551 Parser.parseLine();
552
553 determineTokenTypes();
Fariborz Jahanian68a542a2012-12-20 19:54:13 +0000554 bool IsObjCMethodDecl =
Daniel Jasper8dd40472012-12-21 09:41:31 +0000555 (Line.Tokens.size() > 0 &&
556 (Annotations[0].Type == TokenAnnotation::TT_ObjCMethodSpecifier));
Daniel Jasperf7935112012-12-03 18:12:45 +0000557 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 Jasper2af6bbe2012-12-18 21:05:13 +0000563 if (Annotation.Type == TokenAnnotation::TT_CtorInitializerColon) {
564 Annotation.MustBreakBefore = true;
565 Annotation.SpaceRequiredBefore = true;
Daniel Jasper8dd40472012-12-21 09:41:31 +0000566 } 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 Jahanian68a542a2012-12-20 19:54:13 +0000569 Annotation.CanBreakBefore = true;
570 Annotation.SpaceRequiredBefore = true;
Daniel Jasper8dd40472012-12-21 09:41:31 +0000571 } 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 Jahanian68a542a2012-12-20 19:54:13 +0000574 // 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 Jasper8dd40472012-12-21 09:41:31 +0000578 Line.Tokens[i - 2].Tok.is(tok::at)) {
Fariborz Jahanian68a542a2012-12-20 19:54:13 +0000579 // Don't put two objc's '@' on the same line. This could happen,
580 // as in, @optinal @property ...
581 Annotation.MustBreakBefore = true;
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000582 } else if (Line.Tokens[i].Tok.is(tok::colon)) {
Daniel Jasper55b6b642012-12-05 16:24:48 +0000583 Annotation.SpaceRequiredBefore =
Fariborz Jahanian68a542a2012-12-20 19:54:13 +0000584 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 Jasper8dd40472012-12-21 09:41:31 +0000587 if (IsObjCMethodDecl && Line.Tokens[i - 1].Tok.is(tok::identifier) &&
588 Annotations[i - 1].CanBreakBefore)
Fariborz Jahanian68a542a2012-12-20 19:54:13 +0000589 Annotation.CanBreakBefore = false;
Daniel Jasper8dd40472012-12-21 09:41:31 +0000590 } else if (
591 Annotations[i - 1].Type == TokenAnnotation::TT_ObjCMethodSpecifier) {
Fariborz Jahanian68a542a2012-12-20 19:54:13 +0000592 Annotation.SpaceRequiredBefore = true;
Daniel Jasper8dd40472012-12-21 09:41:31 +0000593 } else if (Annotations[i - 1].Type == TokenAnnotation::TT_UnaryOperator) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000594 Annotation.SpaceRequiredBefore = false;
595 } else if (Annotation.Type == TokenAnnotation::TT_UnaryOperator) {
596 Annotation.SpaceRequiredBefore =
Daniel Jasper8b529712012-12-04 13:02:32 +0000597 Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&
598 Line.Tokens[i - 1].Tok.isNot(tok::l_square);
Daniel Jasperf7935112012-12-03 18:12:45 +0000599 } else if (Line.Tokens[i - 1].Tok.is(tok::greater) &&
600 Line.Tokens[i].Tok.is(tok::greater)) {
Daniel Jasper8b529712012-12-04 13:02:32 +0000601 if (Annotation.Type == TokenAnnotation::TT_TemplateCloser &&
Daniel Jasper6021c4a2012-12-04 14:54:30 +0000602 Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser)
Daniel Jasper9b155472012-12-04 10:50:12 +0000603 Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater;
Daniel Jasperf7935112012-12-03 18:12:45 +0000604 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 Jasper9b155472012-12-04 10:50:12 +0000611 Annotations[i - 1].Type == TokenAnnotation::TT_TemplateCloser &&
Daniel Jasperf7935112012-12-03 18:12:45 +0000612 Line.Tokens[i].Tok.is(tok::l_paren)) {
613 Annotation.SpaceRequiredBefore = false;
Daniel Jasper8b529712012-12-04 13:02:32 +0000614 } else if (Line.Tokens[i].Tok.is(tok::less) &&
615 Line.Tokens[0].Tok.is(tok::hash)) {
616 Annotation.SpaceRequiredBefore = true;
Daniel Jasper8dd40472012-12-21 09:41:31 +0000617 } else if (IsObjCMethodDecl && Line.Tokens[i - 1].Tok.is(tok::r_paren) &&
618 Line.Tokens[i].Tok.is(tok::identifier)) {
Fariborz Jahanian68a542a2012-12-20 19:54:13 +0000619 // Don't space between ')' and <id>
620 Annotation.SpaceRequiredBefore = false;
Daniel Jasper8dd40472012-12-21 09:41:31 +0000621 } else if (IsObjCMethodDecl && Line.Tokens[i - 1].Tok.is(tok::colon) &&
622 Line.Tokens[i].Tok.is(tok::l_paren)) {
Fariborz Jahanian68a542a2012-12-20 19:54:13 +0000623 // Don't space between ':' and '('
624 Annotation.SpaceRequiredBefore = false;
Daniel Jasper8dd40472012-12-21 09:41:31 +0000625 } else if (Annotation.Type == TokenAnnotation::TT_TrailingUnaryOperator) {
626 Annotation.SpaceRequiredBefore = false;
627 } else {
Daniel Jasperf7935112012-12-03 18:12:45 +0000628 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
647private:
648 void determineTokenTypes() {
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000649 bool AssignmentEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000650 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 Jasperab7654e2012-12-21 10:20:02 +0000654 if (getBinOpPrecedence(Tok.Tok.getKind(), true, true) == prec::Assignment)
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000655 AssignmentEncountered = true;
Daniel Jasper426702d2012-12-05 07:51:39 +0000656
Daniel Jasperab7654e2012-12-21 10:20:02 +0000657 if (Annotation.Type != TokenAnnotation::TT_Unknown)
658 continue;
659
Daniel Jasper8dd40472012-12-21 09:41:31 +0000660 if (Tok.Tok.is(tok::star) || Tok.Tok.is(tok::amp)) {
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000661 Annotation.Type = determineStarAmpUsage(i, AssignmentEncountered);
Daniel Jasper8dd40472012-12-21 09:41:31 +0000662 } 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 Jasperf7935112012-12-03 18:12:45 +0000667 Annotation.Type = TokenAnnotation::TT_UnaryOperator;
Daniel Jasperab7654e2012-12-21 10:20:02 +0000668 } else if (isBinaryOperator(Line.Tokens[i])) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000669 Annotation.Type = TokenAnnotation::TT_BinaryOperator;
Daniel Jasperab7654e2012-12-21 10:20:02 +0000670 } else if (Tok.Tok.is(tok::comment)) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000671 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 Jasperf7935112012-12-03 18:12:45 +0000681 bool isBinaryOperator(const FormatToken &Tok) {
Daniel Jasperab7654e2012-12-21 10:20:02 +0000682 // Comma is a binary operator, but does not behave a such wrt. formatting.
683 return getBinOpPrecedence(Tok.Tok.getKind(), true, true) > prec::Comma;
Daniel Jasperf7935112012-12-03 18:12:45 +0000684 }
685
Daniel Jasper426702d2012-12-05 07:51:39 +0000686 TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index,
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000687 bool AssignmentEncountered) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000688 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 Jasper8dd40472012-12-21 09:41:31 +0000697 Line.Tokens[Index + 1].Tok.isLiteral() ||
698 Line.Tokens[Index + 1].Tok.is(tok::kw_sizeof))
Daniel Jasperf7935112012-12-03 18:12:45 +0000699 return TokenAnnotation::TT_BinaryOperator;
700
Daniel Jasper426702d2012-12-05 07:51:39 +0000701 // It is very unlikely that we are going to find a pointer or reference type
702 // definition on the RHS of an assignment.
Daniel Jasperaa1c9202012-12-05 14:57:28 +0000703 if (AssignmentEncountered)
Daniel Jasper426702d2012-12-05 07:51:39 +0000704 return TokenAnnotation::TT_BinaryOperator;
705
Daniel Jasperf7935112012-12-03 18:12:45 +0000706 return TokenAnnotation::TT_PointerOrReference;
707 }
708
Daniel Jasper8dd40472012-12-21 09:41:31 +0000709 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 Jasperf7935112012-12-03 18:12:45 +0000735 }
736
737 bool spaceRequiredBetween(Token Left, Token Right) {
Daniel Jaspera4396862012-12-10 18:59:13 +0000738 if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma))
739 return false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000740 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 Jahanian68a542a2012-12-20 19:54:13 +0000746 if (Left.is(tok::at) && Right.is(tok::identifier))
747 return false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000748 if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less))
749 return false;
Daniel Jasper27234032012-12-07 09:52:15 +0000750 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 Jasperf7935112012-12-03 18:12:45 +0000754 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 Jasperf7935112012-12-03 18:12:45 +0000758 if (Left.is(tok::l_square) || Right.is(tok::l_square) ||
759 Right.is(tok::r_square))
760 return false;
Daniel Jasper27234032012-12-07 09:52:15 +0000761 if (Left.is(tok::coloncolon) ||
762 (Right.is(tok::coloncolon) &&
763 (Left.is(tok::identifier) || Left.is(tok::greater))))
Daniel Jasperf7935112012-12-03 18:12:45 +0000764 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 Jasperf7935112012-12-03 18:12:45 +0000769 if (Left.is(tok::l_paren))
770 return false;
771 if (Left.is(tok::hash))
772 return false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000773 if (Right.is(tok::l_paren)) {
Daniel Jasper8dd40472012-12-21 09:41:31 +0000774 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 Jasperf7935112012-12-03 18:12:45 +0000778 }
779 return true;
780 }
781
782 bool canBreakBetween(const FormatToken &Left, const FormatToken &Right) {
Daniel Jaspere25509f2012-12-17 11:29:41 +0000783 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 Jasperf7935112012-12-03 18:12:45 +0000785 return false;
Daniel Jasperab7654e2012-12-21 10:20:02 +0000786 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 Jasperf7935112012-12-03 18:12:45 +0000791 (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 Kornienkoe3276842012-12-07 16:15:44 +0000800class LexerBasedFormatTokenSource : public FormatTokenSource {
801public:
802 LexerBasedFormatTokenSource(Lexer &Lex, SourceManager &SourceMgr)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000803 : GreaterStashed(false), Lex(Lex), SourceMgr(SourceMgr),
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000804 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
845private:
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 Jasperf7935112012-12-03 18:12:45 +0000859class Formatter : public UnwrappedLineConsumer {
860public:
861 Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
862 const std::vector<CharSourceRange> &Ranges)
Daniel Jasper2af6bbe2012-12-18 21:05:13 +0000863 : Style(Style), Lex(Lex), SourceMgr(SourceMgr), Ranges(Ranges),
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000864 StructuralError(false) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000865 }
866
Daniel Jasper61bd3a12012-12-04 21:05:31 +0000867 virtual ~Formatter() {
868 }
869
Daniel Jasperf7935112012-12-03 18:12:45 +0000870 tooling::Replacements format() {
Alexander Kornienkoe3276842012-12-07 16:15:44 +0000871 LexerBasedFormatTokenSource Tokens(Lex, SourceMgr);
872 UnwrappedLineParser Parser(Style, Tokens, *this);
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000873 StructuralError = Parser.parse();
874 for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),
875 E = UnwrappedLines.end();
876 I != E; ++I)
Alexander Kornienkobc09a7e2012-12-05 13:56:52 +0000877 formatUnwrappedLine(*I);
Daniel Jasperf7935112012-12-03 18:12:45 +0000878 return Replaces;
879 }
880
881private:
Alexander Kornienkobc09a7e2012-12-05 13:56:52 +0000882 virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000883 UnwrappedLines.push_back(TheLine);
884 }
885
Alexander Kornienkobc09a7e2012-12-05 13:56:52 +0000886 void formatUnwrappedLine(const UnwrappedLine &TheLine) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000887 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 Kornienko870f9eb2012-12-04 17:27:50 +0000904 Annotator.getAnnotations(), Replaces,
905 StructuralError);
Daniel Jasperf7935112012-12-03 18:12:45 +0000906 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 Kornienko870f9eb2012-12-04 17:27:50 +0000916 std::vector<UnwrappedLine> UnwrappedLines;
917 bool StructuralError;
Daniel Jasperf7935112012-12-03 18:12:45 +0000918};
919
920tooling::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