blob: 641ebd704a4cbb82765c99e3d4518fd6405366bd [file] [log] [blame]
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001//===--- TokenAnnotator.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 a token annotator, i.e. creates
12/// \c AnnotatedTokens out of \c FormatTokens with required extra information.
13///
14//===----------------------------------------------------------------------===//
15
16#include "TokenAnnotator.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Lex/Lexer.h"
19
20namespace clang {
21namespace format {
22
Nico Weberee0feec2013-02-05 16:21:00 +000023static bool isUnaryOperator(const AnnotatedToken &Tok) {
24 switch (Tok.FormatTok.Tok.getKind()) {
25 case tok::plus:
26 case tok::plusplus:
27 case tok::minus:
28 case tok::minusminus:
29 case tok::exclaim:
30 case tok::tilde:
31 case tok::kw_sizeof:
32 case tok::kw_alignof:
33 return true;
34 default:
35 return false;
36 }
37}
38
Daniel Jasper32d28ee2013-01-29 21:01:14 +000039static bool isBinaryOperator(const AnnotatedToken &Tok) {
40 // Comma is a binary operator, but does not behave as such wrt. formatting.
41 return getPrecedence(Tok) > prec::Comma;
42}
43
Daniel Jasper01786732013-02-04 07:21:18 +000044// Returns the previous token ignoring comments.
Nico Weber4ed7f3e2013-02-06 16:54:35 +000045static AnnotatedToken *getPreviousToken(AnnotatedToken &Tok) {
46 AnnotatedToken *PrevToken = Tok.Parent;
Daniel Jasper01786732013-02-04 07:21:18 +000047 while (PrevToken != NULL && PrevToken->is(tok::comment))
48 PrevToken = PrevToken->Parent;
49 return PrevToken;
50}
Nico Weber4ed7f3e2013-02-06 16:54:35 +000051static const AnnotatedToken *getPreviousToken(const AnnotatedToken &Tok) {
52 return getPreviousToken(const_cast<AnnotatedToken &>(Tok));
53}
Daniel Jasper01786732013-02-04 07:21:18 +000054
Daniel Jasper29f123b2013-02-08 15:28:42 +000055static bool isTrailingComment(AnnotatedToken *Tok) {
56 return Tok != NULL && Tok->is(tok::comment) &&
57 (Tok->Children.empty() ||
58 Tok->Children[0].FormatTok.NewlinesBefore > 0);
59}
60
Daniel Jasper01786732013-02-04 07:21:18 +000061// Returns the next token ignoring comments.
62static const AnnotatedToken *getNextToken(const AnnotatedToken &Tok) {
63 if (Tok.Children.empty())
64 return NULL;
65 const AnnotatedToken *NextToken = &Tok.Children[0];
66 while (NextToken->is(tok::comment)) {
67 if (NextToken->Children.empty())
68 return NULL;
69 NextToken = &NextToken->Children[0];
70 }
71 return NextToken;
72}
73
Daniel Jasper32d28ee2013-01-29 21:01:14 +000074/// \brief A parser that gathers additional information about tokens.
75///
76/// The \c TokenAnnotator tries to matches parenthesis and square brakets and
77/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
78/// into template parameter lists.
79class AnnotatingParser {
80public:
Daniel Jasper01786732013-02-04 07:21:18 +000081 AnnotatingParser(SourceManager &SourceMgr, Lexer &Lex, AnnotatedLine &Line)
82 : SourceMgr(SourceMgr), Lex(Lex), Line(Line), CurrentToken(&Line.First),
Daniel Jasper4e778092013-02-06 10:05:46 +000083 KeywordVirtualFound(false) {
84 Contexts.push_back(Context(1, /*IsExpression=*/ false));
85 Contexts.back().LookForFunctionName = Line.MustBeDeclaration;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000086 }
87
Daniel Jasper32d28ee2013-01-29 21:01:14 +000088 bool parseAngle() {
89 if (CurrentToken == NULL)
90 return false;
Daniel Jasper4e778092013-02-06 10:05:46 +000091 ScopedContextCreator ContextCreator(*this, 10);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000092 AnnotatedToken *Left = CurrentToken->Parent;
Daniel Jasper4e778092013-02-06 10:05:46 +000093 Contexts.back().IsExpression = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000094 while (CurrentToken != NULL) {
95 if (CurrentToken->is(tok::greater)) {
96 Left->MatchingParen = CurrentToken;
97 CurrentToken->MatchingParen = Left;
98 CurrentToken->Type = TT_TemplateCloser;
99 next();
100 return true;
101 }
102 if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_square) ||
103 CurrentToken->is(tok::r_brace))
104 return false;
105 if (CurrentToken->is(tok::pipepipe) || CurrentToken->is(tok::ampamp) ||
106 CurrentToken->is(tok::question) || CurrentToken->is(tok::colon))
107 return false;
108 if (CurrentToken->is(tok::comma))
109 ++Left->ParameterCount;
110 if (!consumeToken())
111 return false;
112 }
113 return false;
114 }
115
116 bool parseParens(bool LookForDecls = false) {
117 if (CurrentToken == NULL)
118 return false;
Daniel Jasper4e778092013-02-06 10:05:46 +0000119 ScopedContextCreator ContextCreator(*this, 1);
120
121 // FIXME: This is a bit of a hack. Do better.
122 Contexts.back().ColonIsForRangeExpr =
123 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
124
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000125 bool StartsObjCMethodExpr = false;
126 AnnotatedToken *Left = CurrentToken->Parent;
127 if (CurrentToken->is(tok::caret)) {
128 // ^( starts a block.
129 Left->Type = TT_ObjCBlockLParen;
130 } else if (AnnotatedToken *MaybeSel = Left->Parent) {
131 // @selector( starts a selector.
132 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Parent &&
133 MaybeSel->Parent->is(tok::at)) {
134 StartsObjCMethodExpr = true;
135 }
136 }
137
Daniel Jasper4e778092013-02-06 10:05:46 +0000138 if (StartsObjCMethodExpr) {
139 Contexts.back().ColonIsObjCMethodExpr = true;
140 Left->Type = TT_ObjCMethodExpr;
141 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000142
143 while (CurrentToken != NULL) {
144 // LookForDecls is set when "if (" has been seen. Check for
145 // 'identifier' '*' 'identifier' followed by not '=' -- this
146 // '*' has to be a binary operator but determineStarAmpUsage() will
147 // categorize it as an unary operator, so set the right type here.
148 if (LookForDecls && !CurrentToken->Children.empty()) {
149 AnnotatedToken &Prev = *CurrentToken->Parent;
150 AnnotatedToken &Next = CurrentToken->Children[0];
151 if (Prev.Parent->is(tok::identifier) &&
152 (Prev.is(tok::star) || Prev.is(tok::amp)) &&
153 CurrentToken->is(tok::identifier) && Next.isNot(tok::equal)) {
154 Prev.Type = TT_BinaryOperator;
155 LookForDecls = false;
156 }
157 }
158
159 if (CurrentToken->is(tok::r_paren)) {
160 Left->MatchingParen = CurrentToken;
161 CurrentToken->MatchingParen = Left;
162
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000163 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000164 CurrentToken->Type = TT_ObjCMethodExpr;
165 if (Contexts.back().FirstObjCSelectorName != NULL) {
166 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
167 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000168 }
169 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000170
171 next();
172 return true;
173 }
174 if (CurrentToken->is(tok::r_square) || CurrentToken->is(tok::r_brace))
175 return false;
176 if (CurrentToken->is(tok::comma))
177 ++Left->ParameterCount;
178 if (!consumeToken())
179 return false;
180 }
181 return false;
182 }
183
184 bool parseSquare() {
185 if (!CurrentToken)
186 return false;
Daniel Jasper4e778092013-02-06 10:05:46 +0000187 ScopedContextCreator ContextCreator(*this, 10);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000188
189 // A '[' could be an index subscript (after an indentifier or after
Nico Weber051860e2013-02-10 02:08:05 +0000190 // ')' or ']'), it could be the start of an Objective-C method
191 // expression, or it could the the start of an Objective-C array literal.
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000192 AnnotatedToken *Left = CurrentToken->Parent;
Nico Weber4ed7f3e2013-02-06 16:54:35 +0000193 AnnotatedToken *Parent = getPreviousToken(*Left);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000194 bool StartsObjCMethodExpr =
Nico Weber4ed7f3e2013-02-06 16:54:35 +0000195 !Parent || Parent->is(tok::colon) || Parent->is(tok::l_square) ||
196 Parent->is(tok::l_paren) || Parent->is(tok::kw_return) ||
197 Parent->is(tok::kw_throw) || isUnaryOperator(*Parent) ||
198 getBinOpPrecedence(Parent->FormatTok.Tok.getKind(), true, true) >
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000199 prec::Unknown;
Nico Weber051860e2013-02-10 02:08:05 +0000200 bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000201
Daniel Jasper4e778092013-02-06 10:05:46 +0000202 if (StartsObjCMethodExpr) {
203 Contexts.back().ColonIsObjCMethodExpr = true;
204 Left->Type = TT_ObjCMethodExpr;
Nico Weber051860e2013-02-10 02:08:05 +0000205 } else if (StartsObjCArrayLiteral) {
206 Left->Type = TT_ObjCArrayLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000207 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000208
209 while (CurrentToken != NULL) {
210 if (CurrentToken->is(tok::r_square)) {
211 if (!CurrentToken->Children.empty() &&
212 CurrentToken->Children[0].is(tok::l_paren)) {
Nico Webere8a97982013-02-06 06:20:11 +0000213 // An ObjC method call is rarely followed by an open parenthesis.
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000214 // FIXME: Do we incorrectly label ":" with this?
215 StartsObjCMethodExpr = false;
216 Left->Type = TT_Unknown;
217 }
Daniel Jasper01786732013-02-04 07:21:18 +0000218 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000219 CurrentToken->Type = TT_ObjCMethodExpr;
Nico Webere8a97982013-02-06 06:20:11 +0000220 // determineStarAmpUsage() thinks that '*' '[' is allocating an
221 // array of pointers, but if '[' starts a selector then '*' is a
222 // binary operator.
Nico Weber4ed7f3e2013-02-06 16:54:35 +0000223 if (Parent != NULL &&
224 (Parent->is(tok::star) || Parent->is(tok::amp)) &&
225 Parent->Type == TT_PointerOrReference)
226 Parent->Type = TT_BinaryOperator;
Nico Weber051860e2013-02-10 02:08:05 +0000227 } else if (StartsObjCArrayLiteral) {
228 CurrentToken->Type = TT_ObjCArrayLiteral;
Daniel Jasper01786732013-02-04 07:21:18 +0000229 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000230 Left->MatchingParen = CurrentToken;
231 CurrentToken->MatchingParen = Left;
Daniel Jasper4e778092013-02-06 10:05:46 +0000232 if (Contexts.back().FirstObjCSelectorName != NULL)
233 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
234 Contexts.back().LongestObjCSelectorName;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000235 next();
236 return true;
237 }
238 if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_brace))
239 return false;
240 if (CurrentToken->is(tok::comma))
241 ++Left->ParameterCount;
242 if (!consumeToken())
243 return false;
244 }
245 return false;
246 }
247
248 bool parseBrace() {
249 // Lines are fine to end with '{'.
250 if (CurrentToken == NULL)
251 return true;
Daniel Jasper4e778092013-02-06 10:05:46 +0000252 ScopedContextCreator ContextCreator(*this, 1);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000253 AnnotatedToken *Left = CurrentToken->Parent;
254 while (CurrentToken != NULL) {
255 if (CurrentToken->is(tok::r_brace)) {
256 Left->MatchingParen = CurrentToken;
257 CurrentToken->MatchingParen = Left;
258 next();
259 return true;
260 }
261 if (CurrentToken->is(tok::r_paren) || CurrentToken->is(tok::r_square))
262 return false;
Daniel Jasperf343cab2013-01-31 14:59:26 +0000263 if (CurrentToken->is(tok::comma))
264 ++Left->ParameterCount;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000265 if (!consumeToken())
266 return false;
267 }
268 return true;
269 }
270
271 bool parseConditional() {
272 while (CurrentToken != NULL) {
273 if (CurrentToken->is(tok::colon)) {
274 CurrentToken->Type = TT_ConditionalExpr;
275 next();
276 return true;
277 }
278 if (!consumeToken())
279 return false;
280 }
281 return false;
282 }
283
284 bool parseTemplateDeclaration() {
285 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
286 CurrentToken->Type = TT_TemplateOpener;
287 next();
288 if (!parseAngle())
289 return false;
290 CurrentToken->Parent->ClosesTemplateDeclaration = true;
291 return true;
292 }
293 return false;
294 }
295
296 bool consumeToken() {
297 AnnotatedToken *Tok = CurrentToken;
298 next();
299 switch (Tok->FormatTok.Tok.getKind()) {
300 case tok::plus:
301 case tok::minus:
302 // At the start of the line, +/- specific ObjectiveC method
303 // declarations.
304 if (Tok->Parent == NULL)
305 Tok->Type = TT_ObjCMethodSpecifier;
306 break;
307 case tok::colon:
308 // Colons from ?: are handled in parseConditional().
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000309 if (Tok->Parent->is(tok::r_paren)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000310 Tok->Type = TT_CtorInitializerColon;
Daniel Jasper4e778092013-02-06 10:05:46 +0000311 } else if (Contexts.back().ColonIsObjCMethodExpr ||
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000312 Line.First.Type == TT_ObjCMethodSpecifier) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000313 Tok->Type = TT_ObjCMethodExpr;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000314 Tok->Parent->Type = TT_ObjCSelectorName;
Daniel Jasper4e778092013-02-06 10:05:46 +0000315 if (Tok->Parent->FormatTok.TokenLength >
316 Contexts.back().LongestObjCSelectorName)
317 Contexts.back().LongestObjCSelectorName =
318 Tok->Parent->FormatTok.TokenLength;
319 if (Contexts.back().FirstObjCSelectorName == NULL)
320 Contexts.back().FirstObjCSelectorName = Tok->Parent;
321 } else if (Contexts.back().ColonIsForRangeExpr) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000322 Tok->Type = TT_RangeBasedForLoopColon;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000323 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000324 break;
325 case tok::kw_if:
326 case tok::kw_while:
327 if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
328 next();
329 if (!parseParens(/*LookForDecls=*/ true))
330 return false;
331 }
332 break;
333 case tok::kw_for:
Daniel Jasper4e778092013-02-06 10:05:46 +0000334 Contexts.back().ColonIsForRangeExpr = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000335 next();
336 if (!parseParens())
337 return false;
338 break;
339 case tok::l_paren:
340 if (!parseParens())
341 return false;
342 break;
343 case tok::l_square:
344 if (!parseSquare())
345 return false;
346 break;
347 case tok::l_brace:
348 if (!parseBrace())
349 return false;
350 break;
351 case tok::less:
352 if (parseAngle())
353 Tok->Type = TT_TemplateOpener;
354 else {
355 Tok->Type = TT_BinaryOperator;
356 CurrentToken = Tok;
357 next();
358 }
359 break;
360 case tok::r_paren:
361 case tok::r_square:
362 return false;
363 case tok::r_brace:
364 // Lines can start with '}'.
365 if (Tok->Parent != NULL)
366 return false;
367 break;
368 case tok::greater:
369 Tok->Type = TT_BinaryOperator;
370 break;
371 case tok::kw_operator:
372 if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
373 CurrentToken->Type = TT_OverloadedOperator;
374 next();
375 if (CurrentToken != NULL && CurrentToken->is(tok::r_paren)) {
376 CurrentToken->Type = TT_OverloadedOperator;
377 next();
378 }
379 } else {
380 while (CurrentToken != NULL && CurrentToken->isNot(tok::l_paren)) {
381 CurrentToken->Type = TT_OverloadedOperator;
382 next();
383 }
384 }
385 break;
386 case tok::question:
387 parseConditional();
388 break;
389 case tok::kw_template:
390 parseTemplateDeclaration();
391 break;
392 default:
393 break;
394 }
395 return true;
396 }
397
398 void parseIncludeDirective() {
399 next();
400 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
401 next();
402 while (CurrentToken != NULL) {
403 if (CurrentToken->isNot(tok::comment) ||
404 !CurrentToken->Children.empty())
405 CurrentToken->Type = TT_ImplicitStringLiteral;
406 next();
407 }
408 } else {
409 while (CurrentToken != NULL) {
410 next();
411 }
412 }
413 }
414
415 void parseWarningOrError() {
416 next();
417 // We still want to format the whitespace left of the first token of the
418 // warning or error.
419 next();
420 while (CurrentToken != NULL) {
421 CurrentToken->Type = TT_ImplicitStringLiteral;
422 next();
423 }
424 }
425
426 void parsePreprocessorDirective() {
427 next();
428 if (CurrentToken == NULL)
429 return;
430 // Hashes in the middle of a line can lead to any strange token
431 // sequence.
432 if (CurrentToken->FormatTok.Tok.getIdentifierInfo() == NULL)
433 return;
434 switch (CurrentToken->FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) {
435 case tok::pp_include:
436 case tok::pp_import:
437 parseIncludeDirective();
438 break;
439 case tok::pp_error:
440 case tok::pp_warning:
441 parseWarningOrError();
442 break;
443 default:
444 break;
445 }
Daniel Jasper5b7e7b02013-02-05 09:34:14 +0000446 while (CurrentToken != NULL)
447 next();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000448 }
449
450 LineType parseLine() {
451 int PeriodsAndArrows = 0;
452 bool CanBeBuilderTypeStmt = true;
453 if (CurrentToken->is(tok::hash)) {
454 parsePreprocessorDirective();
455 return LT_PreprocessorDirective;
456 }
457 while (CurrentToken != NULL) {
458 if (CurrentToken->is(tok::kw_virtual))
459 KeywordVirtualFound = true;
460 if (CurrentToken->is(tok::period) || CurrentToken->is(tok::arrow))
461 ++PeriodsAndArrows;
462 if (getPrecedence(*CurrentToken) > prec::Assignment &&
463 CurrentToken->isNot(tok::less) && CurrentToken->isNot(tok::greater))
464 CanBeBuilderTypeStmt = false;
465 if (!consumeToken())
466 return LT_Invalid;
467 }
468 if (KeywordVirtualFound)
469 return LT_VirtualFunctionDecl;
470
471 // Assume a builder-type call if there are 2 or more "." and "->".
472 if (PeriodsAndArrows >= 2 && CanBeBuilderTypeStmt)
473 return LT_BuilderTypeCall;
474
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000475 if (Line.First.Type == TT_ObjCMethodSpecifier) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000476 if (Contexts.back().FirstObjCSelectorName != NULL)
477 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
478 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000479 return LT_ObjCMethodDecl;
480 }
481
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000482 return LT_Other;
483 }
484
485 void next() {
Daniel Jasper01786732013-02-04 07:21:18 +0000486 if (CurrentToken != NULL) {
487 determineTokenType(*CurrentToken);
Daniel Jasper4e778092013-02-06 10:05:46 +0000488 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
Daniel Jasper01786732013-02-04 07:21:18 +0000489 }
490
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000491 if (CurrentToken != NULL && !CurrentToken->Children.empty())
492 CurrentToken = &CurrentToken->Children[0];
493 else
494 CurrentToken = NULL;
495 }
496
497private:
Daniel Jasper4e778092013-02-06 10:05:46 +0000498 /// \brief A struct to hold information valid in a specific context, e.g.
499 /// a pair of parenthesis.
500 struct Context {
501 Context(unsigned BindingStrength, bool IsExpression)
502 : BindingStrength(BindingStrength), LongestObjCSelectorName(0),
503 ColonIsForRangeExpr(false), ColonIsObjCMethodExpr(false),
504 FirstObjCSelectorName(NULL), IsExpression(IsExpression),
505 LookForFunctionName(false) {
506 }
Daniel Jasper01786732013-02-04 07:21:18 +0000507
Daniel Jasper4e778092013-02-06 10:05:46 +0000508 unsigned BindingStrength;
509 unsigned LongestObjCSelectorName;
510 bool ColonIsForRangeExpr;
511 bool ColonIsObjCMethodExpr;
512 AnnotatedToken *FirstObjCSelectorName;
513 bool IsExpression;
514 bool LookForFunctionName;
515 };
516
517 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
518 /// of each instance.
519 struct ScopedContextCreator {
520 AnnotatingParser &P;
521
522 ScopedContextCreator(AnnotatingParser &P, unsigned Increase)
523 : P(P) {
524 P.Contexts.push_back(Context(
525 P.Contexts.back().BindingStrength + Increase,
526 P.Contexts.back().IsExpression));
527 }
528
529 ~ScopedContextCreator() { P.Contexts.pop_back(); }
530 };
Daniel Jasper01786732013-02-04 07:21:18 +0000531
532 void determineTokenType(AnnotatedToken &Current) {
533 if (getPrecedence(Current) == prec::Assignment) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000534 Contexts.back().IsExpression = true;
Daniel Jasper01786732013-02-04 07:21:18 +0000535 AnnotatedToken *Previous = Current.Parent;
Daniel Jasper6b5ba8b2013-02-06 10:57:42 +0000536 while (Previous != NULL && Previous->isNot(tok::comma)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000537 if (Previous->Type == TT_BinaryOperator &&
538 (Previous->is(tok::star) || Previous->is(tok::amp))) {
539 Previous->Type = TT_PointerOrReference;
540 }
541 Previous = Previous->Parent;
542 }
543 }
544 if (Current.is(tok::kw_return) || Current.is(tok::kw_throw) ||
545 (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
546 (Current.Parent == NULL || Current.Parent->isNot(tok::kw_for))))
Daniel Jasper4e778092013-02-06 10:05:46 +0000547 Contexts.back().IsExpression = true;
Daniel Jasper01786732013-02-04 07:21:18 +0000548
549 if (Current.Type == TT_Unknown) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000550 if (Contexts.back().LookForFunctionName && Current.is(tok::l_paren)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000551 findFunctionName(&Current);
Daniel Jasper4e778092013-02-06 10:05:46 +0000552 Contexts.back().LookForFunctionName = false;
Daniel Jasper01786732013-02-04 07:21:18 +0000553 } else if (Current.is(tok::star) || Current.is(tok::amp)) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000554 Current.Type =
555 determineStarAmpUsage(Current, Contexts.back().IsExpression);
Daniel Jasper01786732013-02-04 07:21:18 +0000556 } else if (Current.is(tok::minus) || Current.is(tok::plus) ||
557 Current.is(tok::caret)) {
558 Current.Type = determinePlusMinusCaretUsage(Current);
559 } else if (Current.is(tok::minusminus) || Current.is(tok::plusplus)) {
560 Current.Type = determineIncrementUsage(Current);
561 } else if (Current.is(tok::exclaim)) {
562 Current.Type = TT_UnaryOperator;
563 } else if (isBinaryOperator(Current)) {
564 Current.Type = TT_BinaryOperator;
565 } else if (Current.is(tok::comment)) {
566 std::string Data(Lexer::getSpelling(Current.FormatTok.Tok, SourceMgr,
567 Lex.getLangOpts()));
568 if (StringRef(Data).startswith("//"))
569 Current.Type = TT_LineComment;
570 else
571 Current.Type = TT_BlockComment;
572 } else if (Current.is(tok::r_paren) &&
573 (Current.Parent->Type == TT_PointerOrReference ||
574 Current.Parent->Type == TT_TemplateCloser) &&
575 (Current.Children.empty() ||
576 (Current.Children[0].isNot(tok::equal) &&
577 Current.Children[0].isNot(tok::semi) &&
578 Current.Children[0].isNot(tok::l_brace)))) {
579 // FIXME: We need to get smarter and understand more cases of casts.
580 Current.Type = TT_CastRParen;
581 } else if (Current.is(tok::at) && Current.Children.size()) {
582 switch (Current.Children[0].FormatTok.Tok.getObjCKeywordID()) {
583 case tok::objc_interface:
584 case tok::objc_implementation:
585 case tok::objc_protocol:
586 Current.Type = TT_ObjCDecl;
587 break;
588 case tok::objc_property:
589 Current.Type = TT_ObjCProperty;
590 break;
591 default:
592 break;
593 }
594 }
595 }
596 }
597
598 /// \brief Starting from \p Current, this searches backwards for an
599 /// identifier which could be the start of a function name and marks it.
600 void findFunctionName(AnnotatedToken *Current) {
601 AnnotatedToken *Parent = Current->Parent;
602 while (Parent != NULL && Parent->Parent != NULL) {
603 if (Parent->is(tok::identifier) &&
604 (Parent->Parent->is(tok::identifier) ||
605 Parent->Parent->Type == TT_PointerOrReference ||
606 Parent->Parent->Type == TT_TemplateCloser)) {
607 Parent->Type = TT_StartOfName;
608 break;
609 }
610 Parent = Parent->Parent;
611 }
612 }
613
614 /// \brief Return the type of the given token assuming it is * or &.
615 TokenType
616 determineStarAmpUsage(const AnnotatedToken &Tok, bool IsExpression) {
617 const AnnotatedToken *PrevToken = getPreviousToken(Tok);
618 if (PrevToken == NULL)
619 return TT_UnaryOperator;
620
621 const AnnotatedToken *NextToken = getNextToken(Tok);
622 if (NextToken == NULL)
623 return TT_Unknown;
624
Daniel Jasper01786732013-02-04 07:21:18 +0000625 if (PrevToken->is(tok::l_paren) || PrevToken->is(tok::l_square) ||
626 PrevToken->is(tok::l_brace) || PrevToken->is(tok::comma) ||
627 PrevToken->is(tok::kw_return) || PrevToken->is(tok::colon) ||
Nico Webere8a97982013-02-06 06:20:11 +0000628 PrevToken->is(tok::equal) || PrevToken->Type == TT_BinaryOperator ||
Daniel Jasper01786732013-02-04 07:21:18 +0000629 PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
630 return TT_UnaryOperator;
631
Nico Webere8a97982013-02-06 06:20:11 +0000632 if (NextToken->is(tok::l_square))
633 return TT_PointerOrReference;
634
Daniel Jasper01786732013-02-04 07:21:18 +0000635 if (PrevToken->FormatTok.Tok.isLiteral() || PrevToken->is(tok::r_paren) ||
636 PrevToken->is(tok::r_square) || NextToken->FormatTok.Tok.isLiteral() ||
Nico Weberee0feec2013-02-05 16:21:00 +0000637 isUnaryOperator(*NextToken) || NextToken->is(tok::l_paren) ||
638 NextToken->is(tok::l_square))
Daniel Jasper01786732013-02-04 07:21:18 +0000639 return TT_BinaryOperator;
640
641 if (NextToken->is(tok::comma) || NextToken->is(tok::r_paren) ||
642 NextToken->is(tok::greater))
643 return TT_PointerOrReference;
644
645 // It is very unlikely that we are going to find a pointer or reference type
646 // definition on the RHS of an assignment.
647 if (IsExpression)
648 return TT_BinaryOperator;
649
650 return TT_PointerOrReference;
651 }
652
653 TokenType determinePlusMinusCaretUsage(const AnnotatedToken &Tok) {
654 const AnnotatedToken *PrevToken = getPreviousToken(Tok);
655 if (PrevToken == NULL)
656 return TT_UnaryOperator;
657
658 // Use heuristics to recognize unary operators.
659 if (PrevToken->is(tok::equal) || PrevToken->is(tok::l_paren) ||
660 PrevToken->is(tok::comma) || PrevToken->is(tok::l_square) ||
661 PrevToken->is(tok::question) || PrevToken->is(tok::colon) ||
662 PrevToken->is(tok::kw_return) || PrevToken->is(tok::kw_case) ||
663 PrevToken->is(tok::at) || PrevToken->is(tok::l_brace))
664 return TT_UnaryOperator;
665
Nico Weberee0feec2013-02-05 16:21:00 +0000666 // There can't be two consecutive binary operators.
Daniel Jasper01786732013-02-04 07:21:18 +0000667 if (PrevToken->Type == TT_BinaryOperator)
668 return TT_UnaryOperator;
669
670 // Fall back to marking the token as binary operator.
671 return TT_BinaryOperator;
672 }
673
674 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
675 TokenType determineIncrementUsage(const AnnotatedToken &Tok) {
676 const AnnotatedToken *PrevToken = getPreviousToken(Tok);
677 if (PrevToken == NULL)
678 return TT_UnaryOperator;
679 if (PrevToken->is(tok::r_paren) || PrevToken->is(tok::r_square) ||
680 PrevToken->is(tok::identifier))
681 return TT_TrailingUnaryOperator;
682
683 return TT_UnaryOperator;
684 }
Daniel Jasper4e778092013-02-06 10:05:46 +0000685
686 SmallVector<Context, 8> Contexts;
687
688 SourceManager &SourceMgr;
689 Lexer &Lex;
690 AnnotatedLine &Line;
691 AnnotatedToken *CurrentToken;
692 bool KeywordVirtualFound;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000693};
694
Daniel Jasper29f123b2013-02-08 15:28:42 +0000695/// \brief Parses binary expressions by inserting fake parenthesis based on
696/// operator precedence.
697class ExpressionParser {
698public:
699 ExpressionParser(AnnotatedLine &Line) : Current(&Line.First) {}
700
701 /// \brief Parse expressions with the given operatore precedence.
702 void parse(unsigned Precedence = prec::Unknown) {
703 if (Precedence > prec::PointerToMember || Current == NULL)
704 return;
705
706 // Skip over "return" until we can properly parse it.
707 if (Current->is(tok::kw_return))
708 next();
709
710 // Eagerly consume trailing comments.
711 while (isTrailingComment(Current)) {
712 next();
713 }
714
715 AnnotatedToken *Start = Current;
716 bool OperatorFound = false;
717
718 while (Current != NULL) {
719 // Consume operators with higher precedence.
720 parse(Precedence + 1);
721
722 // At the end of the line or when an operator with higher precedence is
723 // found, insert fake parenthesis and return.
724 if (Current == NULL || Current->is(tok::semi) || closesScope(*Current) ||
725 ((Current->Type == TT_BinaryOperator || Current->is(tok::comma)) &&
726 getPrecedence(*Current) < Precedence)) {
727 if (OperatorFound) {
728 ++Start->FakeLParens;
729 if (Current != NULL)
Daniel Jasper087387a2013-02-08 16:49:27 +0000730 ++Current->Parent->FakeRParens;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000731 }
732 return;
733 }
734
735 // Consume scopes: (), [], <> and {}
736 if (opensScope(*Current)) {
737 while (Current != NULL && !closesScope(*Current)) {
738 next();
739 parse();
740 }
741 next();
742 } else {
743 // Operator found.
744 if (getPrecedence(*Current) == Precedence)
745 OperatorFound = true;
746
747 next();
748 }
749 }
750 }
751
752private:
753 void next() {
754 if (Current != NULL)
755 Current = Current->Children.empty() ? NULL : &Current->Children[0];
756 }
757
758 bool closesScope(const AnnotatedToken &Tok) {
759 return Current->is(tok::r_paren) || Current->Type == TT_TemplateCloser ||
760 Current->is(tok::r_brace) || Current->is(tok::r_square);
761 }
762
763 bool opensScope(const AnnotatedToken &Tok) {
764 return Current->is(tok::l_paren) || Current->Type == TT_TemplateOpener ||
765 Current->is(tok::l_brace) || Current->is(tok::l_square);
766 }
767
768 AnnotatedToken *Current;
769};
770
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000771void TokenAnnotator::annotate(AnnotatedLine &Line) {
Daniel Jasper01786732013-02-04 07:21:18 +0000772 AnnotatingParser Parser(SourceMgr, Lex, Line);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000773 Line.Type = Parser.parseLine();
774 if (Line.Type == LT_Invalid)
775 return;
776
Daniel Jasper29f123b2013-02-08 15:28:42 +0000777 ExpressionParser ExprParser(Line);
778 ExprParser.parse();
779
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000780 if (Line.First.Type == TT_ObjCMethodSpecifier)
781 Line.Type = LT_ObjCMethodDecl;
782 else if (Line.First.Type == TT_ObjCDecl)
783 Line.Type = LT_ObjCDecl;
784 else if (Line.First.Type == TT_ObjCProperty)
785 Line.Type = LT_ObjCProperty;
786
787 Line.First.SpaceRequiredBefore = true;
788 Line.First.MustBreakBefore = Line.First.FormatTok.MustBreakBefore;
789 Line.First.CanBreakBefore = Line.First.MustBreakBefore;
790
791 Line.First.TotalLength = Line.First.FormatTok.TokenLength;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000792}
793
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000794void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
795 if (Line.First.Children.empty())
796 return;
797 AnnotatedToken *Current = &Line.First.Children[0];
798 while (Current != NULL) {
799 Current->SpaceRequiredBefore = spaceRequiredBefore(Line, *Current);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000800
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000801 if (Current->FormatTok.MustBreakBefore) {
802 Current->MustBreakBefore = true;
803 } else if (Current->Type == TT_LineComment) {
804 Current->MustBreakBefore = Current->FormatTok.NewlinesBefore > 0;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000805 } else if (isTrailingComment(Current->Parent) ||
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000806 (Current->is(tok::string_literal) &&
807 Current->Parent->is(tok::string_literal))) {
808 Current->MustBreakBefore = true;
809 } else if (Current->is(tok::lessless) && !Current->Children.empty() &&
810 Current->Parent->is(tok::string_literal) &&
811 Current->Children[0].is(tok::string_literal)) {
812 Current->MustBreakBefore = true;
813 } else {
814 Current->MustBreakBefore = false;
815 }
816 Current->CanBreakBefore =
817 Current->MustBreakBefore || canBreakBefore(Line, *Current);
818 if (Current->MustBreakBefore)
819 Current->TotalLength = Current->Parent->TotalLength + Style.ColumnLimit;
820 else
821 Current->TotalLength =
822 Current->Parent->TotalLength + Current->FormatTok.TokenLength +
823 (Current->SpaceRequiredBefore ? 1 : 0);
824 // FIXME: Only calculate this if CanBreakBefore is true once static
825 // initializers etc. are sorted out.
826 // FIXME: Move magic numbers to a better place.
827 Current->SplitPenalty =
828 20 * Current->BindingStrength + splitPenalty(Line, *Current);
829
830 Current = Current->Children.empty() ? NULL : &Current->Children[0];
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000831 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000832}
833
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000834unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
835 const AnnotatedToken &Tok) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000836 const AnnotatedToken &Left = *Tok.Parent;
837 const AnnotatedToken &Right = Tok;
838
839 if (Left.is(tok::l_brace) && Right.isNot(tok::l_brace))
840 return 50;
841 if (Left.is(tok::equal) && Right.is(tok::l_brace))
842 return 150;
843 if (Left.is(tok::coloncolon))
844 return 500;
845
846 if (Left.Type == TT_RangeBasedForLoopColon)
847 return 5;
848
849 if (Right.is(tok::arrow) || Right.is(tok::period)) {
850 if (Left.is(tok::r_paren) && Line.Type == LT_BuilderTypeCall)
851 return 5; // Should be smaller than breaking at a nested comma.
852 return 150;
853 }
854
855 // In for-loops, prefer breaking at ',' and ';'.
856 if (Line.First.is(tok::kw_for) &&
857 (Left.isNot(tok::comma) && Left.isNot(tok::semi)))
858 return 20;
859
Daniel Jasper8159d2f2013-02-04 07:30:30 +0000860 if (Left.is(tok::semi))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000861 return 0;
Daniel Jasper8159d2f2013-02-04 07:30:30 +0000862 if (Left.is(tok::comma))
863 return 1;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000864
865 // In Objective-C method expressions, prefer breaking before "param:" over
866 // breaking after it.
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000867 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000868 return 0;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000869 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000870 return 20;
871
Daniel Jasper01786732013-02-04 07:21:18 +0000872 if (Left.is(tok::l_paren) || Left.is(tok::l_square) ||
873 Left.Type == TT_TemplateOpener)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000874 return 20;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000875
Daniel Jasper4e8a7b42013-02-06 21:04:05 +0000876 if (Right.is(tok::lessless)) {
877 if (Left.is(tok::string_literal)) {
878 char LastChar =
879 StringRef(Left.FormatTok.Tok.getLiteralData(),
880 Left.FormatTok.TokenLength).drop_back(1).rtrim().back();
881 if (LastChar == ':' || LastChar == '=')
882 return 100;
883 }
Daniel Jasper01786732013-02-04 07:21:18 +0000884 return prec::Shift;
Daniel Jasper4e8a7b42013-02-06 21:04:05 +0000885 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000886 if (Left.Type == TT_ConditionalExpr)
887 return prec::Assignment;
888 prec::Level Level = getPrecedence(Left);
889
890 if (Level != prec::Unknown)
891 return Level;
892
893 return 3;
894}
895
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000896bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
897 const AnnotatedToken &Left,
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000898 const AnnotatedToken &Right) {
899 if (Right.is(tok::hashhash))
900 return Left.is(tok::hash);
901 if (Left.is(tok::hashhash) || Left.is(tok::hash))
902 return Right.is(tok::hash);
903 if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma))
904 return false;
905 if (Right.is(tok::less) &&
906 (Left.is(tok::kw_template) ||
907 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
908 return true;
909 if (Left.is(tok::arrow) || Right.is(tok::arrow))
910 return false;
911 if (Left.is(tok::exclaim) || Left.is(tok::tilde))
912 return false;
913 if (Left.is(tok::at) &&
914 (Right.is(tok::identifier) || Right.is(tok::string_literal) ||
915 Right.is(tok::char_constant) || Right.is(tok::numeric_constant) ||
916 Right.is(tok::l_paren) || Right.is(tok::l_brace) ||
917 Right.is(tok::kw_true) || Right.is(tok::kw_false)))
918 return false;
919 if (Left.is(tok::coloncolon))
920 return false;
921 if (Right.is(tok::coloncolon))
Daniel Jasperdaf1a152013-02-07 21:08:36 +0000922 return Left.isNot(tok::identifier) && Left.isNot(tok::greater) &&
923 Left.isNot(tok::l_paren);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000924 if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less))
925 return false;
926 if (Right.is(tok::amp) || Right.is(tok::star))
927 return Left.FormatTok.Tok.isLiteral() ||
928 (Left.isNot(tok::star) && Left.isNot(tok::amp) &&
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000929 !Style.PointerBindsToType);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000930 if (Left.is(tok::amp) || Left.is(tok::star))
Daniel Jasper29f123b2013-02-08 15:28:42 +0000931 return Right.FormatTok.Tok.isLiteral() || Style.PointerBindsToType;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000932 if (Right.is(tok::star) && Left.is(tok::l_paren))
933 return false;
Nico Weber051860e2013-02-10 02:08:05 +0000934 if (Left.is(tok::l_square))
935 return Left.Type == TT_ObjCArrayLiteral && Right.isNot(tok::r_square);
936 if (Right.is(tok::r_square))
937 return Right.Type == TT_ObjCArrayLiteral;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000938 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr)
939 return false;
940 if (Left.is(tok::period) || Right.is(tok::period))
941 return false;
942 if (Left.is(tok::colon))
943 return Left.Type != TT_ObjCMethodExpr;
944 if (Right.is(tok::colon))
945 return Right.Type != TT_ObjCMethodExpr;
946 if (Left.is(tok::l_paren))
947 return false;
948 if (Right.is(tok::l_paren)) {
949 return Line.Type == LT_ObjCDecl || Left.is(tok::kw_if) ||
950 Left.is(tok::kw_for) || Left.is(tok::kw_while) ||
951 Left.is(tok::kw_switch) || Left.is(tok::kw_return) ||
952 Left.is(tok::kw_catch) || Left.is(tok::kw_new) ||
953 Left.is(tok::kw_delete);
954 }
955 if (Left.is(tok::at) &&
956 Right.FormatTok.Tok.getObjCKeywordID() != tok::objc_not_keyword)
957 return false;
958 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
959 return false;
960 return true;
961}
962
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000963bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
964 const AnnotatedToken &Tok) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000965 if (Line.Type == LT_ObjCMethodDecl) {
966 if (Tok.is(tok::identifier) && !Tok.Children.empty() &&
967 Tok.Children[0].is(tok::colon) && Tok.Parent->is(tok::identifier))
968 return true;
969 if (Tok.is(tok::colon))
970 return false;
971 if (Tok.Parent->Type == TT_ObjCMethodSpecifier)
972 return true;
973 if (Tok.Parent->is(tok::r_paren) && Tok.is(tok::identifier))
974 // Don't space between ')' and <id>
975 return false;
976 if (Tok.Parent->is(tok::colon) && Tok.is(tok::l_paren))
977 // Don't space between ':' and '('
978 return false;
979 }
980 if (Line.Type == LT_ObjCProperty &&
981 (Tok.is(tok::equal) || Tok.Parent->is(tok::equal)))
982 return false;
983
984 if (Tok.Parent->is(tok::comma))
985 return true;
986 if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
987 return true;
988 if (Tok.Type == TT_OverloadedOperator)
989 return Tok.is(tok::identifier) || Tok.is(tok::kw_new) ||
990 Tok.is(tok::kw_delete) || Tok.is(tok::kw_bool);
991 if (Tok.Parent->Type == TT_OverloadedOperator)
992 return false;
993 if (Tok.is(tok::colon))
994 return Line.First.isNot(tok::kw_case) && !Tok.Children.empty() &&
995 Tok.Type != TT_ObjCMethodExpr;
996 if (Tok.Parent->Type == TT_UnaryOperator || Tok.Parent->Type == TT_CastRParen)
997 return false;
998 if (Tok.Type == TT_UnaryOperator)
999 return Tok.Parent->isNot(tok::l_paren) &&
1000 Tok.Parent->isNot(tok::l_square) && Tok.Parent->isNot(tok::at) &&
1001 (Tok.Parent->isNot(tok::colon) ||
1002 Tok.Parent->Type != TT_ObjCMethodExpr);
1003 if (Tok.Parent->is(tok::greater) && Tok.is(tok::greater)) {
Daniel Jasper29f123b2013-02-08 15:28:42 +00001004 return Tok.Type == TT_TemplateCloser &&
1005 Tok.Parent->Type == TT_TemplateCloser &&
1006 Style.Standard != FormatStyle::LS_Cpp11;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001007 }
1008 if (Tok.Type == TT_BinaryOperator || Tok.Parent->Type == TT_BinaryOperator)
1009 return true;
1010 if (Tok.Parent->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
1011 return false;
1012 if (Tok.is(tok::less) && Line.First.is(tok::hash))
1013 return true;
1014 if (Tok.Type == TT_TrailingUnaryOperator)
1015 return false;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001016 return spaceRequiredBetween(Line, *Tok.Parent, Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001017}
1018
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001019bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
1020 const AnnotatedToken &Right) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001021 const AnnotatedToken &Left = *Right.Parent;
1022 if (Line.Type == LT_ObjCMethodDecl) {
1023 if (Right.is(tok::identifier) && !Right.Children.empty() &&
1024 Right.Children[0].is(tok::colon) && Left.is(tok::identifier))
1025 return true;
1026 if (Right.is(tok::identifier) && Left.is(tok::l_paren) &&
1027 Left.Parent->is(tok::colon))
1028 // Don't break this identifier as ':' or identifier
1029 // before it will break.
1030 return false;
1031 if (Right.is(tok::colon) && Left.is(tok::identifier) && Left.CanBreakBefore)
1032 // Don't break at ':' if identifier before it can beak.
1033 return false;
1034 }
1035 if (Right.Type == TT_StartOfName && Style.AllowReturnTypeOnItsOwnLine)
1036 return true;
1037 if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr)
1038 return false;
1039 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
1040 return true;
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001041 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001042 return true;
1043 if (Left.ClosesTemplateDeclaration)
1044 return true;
1045 if (Right.Type == TT_ConditionalExpr || Right.is(tok::question))
1046 return true;
1047 if (Left.Type == TT_RangeBasedForLoopColon)
1048 return true;
1049 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
1050 Left.Type == TT_UnaryOperator || Left.Type == TT_ConditionalExpr ||
1051 Left.is(tok::question))
1052 return false;
1053 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
1054 return false;
1055
1056 if (Right.Type == TT_LineComment)
1057 // We rely on MustBreakBefore being set correctly here as we should not
1058 // change the "binding" behavior of a comment.
1059 return false;
1060
1061 // Allow breaking after a trailing 'const', e.g. after a method declaration,
1062 // unless it is follow by ';', '{' or '='.
1063 if (Left.is(tok::kw_const) && Left.Parent != NULL &&
1064 Left.Parent->is(tok::r_paren))
1065 return Right.isNot(tok::l_brace) && Right.isNot(tok::semi) &&
1066 Right.isNot(tok::equal);
1067
1068 // We only break before r_brace if there was a corresponding break before
1069 // the l_brace, which is tracked by BreakBeforeClosingBrace.
1070 if (Right.is(tok::r_brace))
1071 return false;
1072
1073 if (Right.is(tok::r_paren) || Right.is(tok::greater))
1074 return false;
1075 return (isBinaryOperator(Left) && Left.isNot(tok::lessless)) ||
1076 Left.is(tok::comma) || Right.is(tok::lessless) ||
1077 Right.is(tok::arrow) || Right.is(tok::period) ||
1078 Right.is(tok::colon) || Left.is(tok::coloncolon) ||
1079 Left.is(tok::semi) || Left.is(tok::l_brace) ||
1080 (Left.is(tok::r_paren) && Left.Type != TT_CastRParen &&
1081 Right.is(tok::identifier)) ||
1082 (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
1083 (Left.is(tok::l_square) && !Right.is(tok::r_square));
1084}
1085
1086} // namespace format
1087} // namespace clang