blob: 0034235c32d023fbe372264cecd0b5ebc0fdc883 [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"
Daniel Jasperbf71ba22013-04-08 20:33:42 +000018#include "llvm/Support/Debug.h"
Daniel Jasper32d28ee2013-01-29 21:01:14 +000019
20namespace clang {
21namespace format {
22
Craig Topper14e66492013-07-01 04:03:19 +000023namespace {
24
Daniel Jasper32d28ee2013-01-29 21:01:14 +000025/// \brief A parser that gathers additional information about tokens.
26///
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +000027/// The \c TokenAnnotator tries to match parenthesis and square brakets and
Daniel Jasper32d28ee2013-01-29 21:01:14 +000028/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
29/// into template parameter lists.
30class AnnotatingParser {
31public:
Daniel Jasperd4a03db2013-08-22 15:00:41 +000032 AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
33 IdentifierInfo &Ident_in)
34 : Style(Style), Line(Line), CurrentToken(Line.First),
Daniel Jasper59875ac2013-11-07 17:43:07 +000035 KeywordVirtualFound(false), AutoFound(false), Ident_in(Ident_in) {
Nico Weber27268772013-06-26 00:30:14 +000036 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
Stephen Hines651f13c2014-04-23 16:59:28 -070037 resetTokenMetadata(CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000038 }
39
Nico Weber95e8e462013-02-12 16:17:07 +000040private:
Daniel Jasper32d28ee2013-01-29 21:01:14 +000041 bool parseAngle() {
42 if (CurrentToken == NULL)
43 return false;
Daniel Jasper923ebef2013-03-14 13:45:21 +000044 ScopedContextCreator ContextCreator(*this, tok::less, 10);
Manuel Klimekb3987012013-05-29 14:47:47 +000045 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper4e778092013-02-06 10:05:46 +000046 Contexts.back().IsExpression = false;
Stephen Hines651f13c2014-04-23 16:59:28 -070047 // If there's a template keyword before the opening angle bracket, this is a
48 // template parameter, not an argument.
49 Contexts.back().InTemplateArgument =
50 Left->Previous != NULL && Left->Previous->Tok.isNot(tok::kw_template);
51
Daniel Jasper32d28ee2013-01-29 21:01:14 +000052 while (CurrentToken != NULL) {
53 if (CurrentToken->is(tok::greater)) {
54 Left->MatchingParen = CurrentToken;
55 CurrentToken->MatchingParen = Left;
56 CurrentToken->Type = TT_TemplateCloser;
57 next();
58 return true;
59 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +000060 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
Daniel Jasper5d823e32013-05-15 13:46:48 +000061 tok::question, tok::colon))
62 return false;
Daniel Jasper0348be02013-06-01 18:56:00 +000063 // If a && or || is found and interpreted as a binary operator, this set
Daniel Jasper15f33f02013-06-03 16:16:41 +000064 // of angles is likely part of something like "a < b && c > d". If the
Daniel Jasper0348be02013-06-01 18:56:00 +000065 // angles are inside an expression, the ||/&& might also be a binary
66 // operator that was misinterpreted because we are parsing template
67 // parameters.
68 // FIXME: This is getting out of hand, write a decent parser.
Manuel Klimekb3987012013-05-29 14:47:47 +000069 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
Stephen Hines651f13c2014-04-23 16:59:28 -070070 ((CurrentToken->Previous->Type == TT_BinaryOperator &&
71 // Toplevel bool expressions do not make lots of sense;
72 // If we're on the top level, it contains only the base context and
73 // the context for the current opening angle bracket.
74 Contexts.size() > 2) ||
Daniel Jasper0348be02013-06-01 18:56:00 +000075 Contexts[Contexts.size() - 2].IsExpression) &&
Manuel Klimekb3987012013-05-29 14:47:47 +000076 Line.First->isNot(tok::kw_template))
Daniel Jasper32d28ee2013-01-29 21:01:14 +000077 return false;
Daniel Jasper9fc56f22013-02-14 15:01:34 +000078 updateParameterCount(Left, CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000079 if (!consumeToken())
80 return false;
81 }
82 return false;
83 }
84
85 bool parseParens(bool LookForDecls = false) {
86 if (CurrentToken == NULL)
87 return false;
Daniel Jasper923ebef2013-03-14 13:45:21 +000088 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
Daniel Jasper4e778092013-02-06 10:05:46 +000089
90 // FIXME: This is a bit of a hack. Do better.
91 Contexts.back().ColonIsForRangeExpr =
92 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
93
Daniel Jasper32d28ee2013-01-29 21:01:14 +000094 bool StartsObjCMethodExpr = false;
Manuel Klimekb3987012013-05-29 14:47:47 +000095 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000096 if (CurrentToken->is(tok::caret)) {
Stephen Hines651f13c2014-04-23 16:59:28 -070097 // (^ can start a block type.
Daniel Jasper32d28ee2013-01-29 21:01:14 +000098 Left->Type = TT_ObjCBlockLParen;
Manuel Klimekb3987012013-05-29 14:47:47 +000099 } else if (FormatToken *MaybeSel = Left->Previous) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000100 // @selector( starts a selector.
Manuel Klimekb3987012013-05-29 14:47:47 +0000101 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
102 MaybeSel->Previous->is(tok::at)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000103 StartsObjCMethodExpr = true;
104 }
105 }
106
Stephen Hines651f13c2014-04-23 16:59:28 -0700107 if (Left->Previous &&
108 (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_if,
109 tok::kw_while, tok::l_paren, tok::comma) ||
110 Left->Previous->Type == TT_BinaryOperator)) {
Daniel Jasper363193b2013-10-20 18:15:30 +0000111 // static_assert, if and while usually contain expressions.
Daniel Jasperb7000ca2013-08-01 17:58:23 +0000112 Contexts.back().IsExpression = true;
Daniel Jasper363193b2013-10-20 18:15:30 +0000113 } else if (Left->Previous && Left->Previous->is(tok::r_square) &&
114 Left->Previous->MatchingParen &&
115 Left->Previous->MatchingParen->Type == TT_LambdaLSquare) {
116 // This is a parameter list of a lambda expression.
117 Contexts.back().IsExpression = false;
Stephen Hines651f13c2014-04-23 16:59:28 -0700118 } else if (Contexts[Contexts.size() - 2].CaretFound) {
119 // This is the parameter list of an ObjC block.
120 Contexts.back().IsExpression = false;
121 } else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
122 Left->Type = TT_AttributeParen;
123 } else if (Left->Previous && Left->Previous->IsForEachMacro) {
124 // The first argument to a foreach macro is a declaration.
125 Contexts.back().IsForEachMacro = true;
126 Contexts.back().IsExpression = false;
Daniel Jasper363193b2013-10-20 18:15:30 +0000127 }
Daniel Jasperb7000ca2013-08-01 17:58:23 +0000128
Daniel Jasper4e778092013-02-06 10:05:46 +0000129 if (StartsObjCMethodExpr) {
130 Contexts.back().ColonIsObjCMethodExpr = true;
131 Left->Type = TT_ObjCMethodExpr;
132 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000133
Daniel Jasper431f5912013-05-28 08:33:00 +0000134 bool MightBeFunctionType = CurrentToken->is(tok::star);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000135 bool HasMultipleLines = false;
136 bool HasMultipleParametersOnALine = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000137 while (CurrentToken != NULL) {
138 // LookForDecls is set when "if (" has been seen. Check for
139 // 'identifier' '*' 'identifier' followed by not '=' -- this
140 // '*' has to be a binary operator but determineStarAmpUsage() will
141 // categorize it as an unary operator, so set the right type here.
Manuel Klimekb3987012013-05-29 14:47:47 +0000142 if (LookForDecls && CurrentToken->Next) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000143 FormatToken *Prev = CurrentToken->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000144 if (Prev) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000145 FormatToken *PrevPrev = Prev->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000146 FormatToken *Next = CurrentToken->Next;
147 if (PrevPrev && PrevPrev->is(tok::identifier) &&
148 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
149 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
150 Prev->Type = TT_BinaryOperator;
151 LookForDecls = false;
152 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000153 }
154 }
155
Daniel Jasper53352602013-08-12 12:16:34 +0000156 if (CurrentToken->Previous->Type == TT_PointerOrReference &&
157 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
158 tok::coloncolon))
159 MightBeFunctionType = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000160 if (CurrentToken->is(tok::r_paren)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000161 if (MightBeFunctionType && CurrentToken->Next &&
Daniel Jaspere7d3bff2013-07-16 11:37:21 +0000162 (CurrentToken->Next->is(tok::l_paren) ||
163 (CurrentToken->Next->is(tok::l_square) &&
164 !Contexts.back().IsExpression)))
Daniel Jasper431f5912013-05-28 08:33:00 +0000165 Left->Type = TT_FunctionTypeLParen;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000166 Left->MatchingParen = CurrentToken;
167 CurrentToken->MatchingParen = Left;
168
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000169 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000170 CurrentToken->Type = TT_ObjCMethodExpr;
171 if (Contexts.back().FirstObjCSelectorName != NULL) {
172 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
173 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000174 }
175 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000176
Stephen Hines651f13c2014-04-23 16:59:28 -0700177 if (Left->Type == TT_AttributeParen)
178 CurrentToken->Type = TT_AttributeParen;
179
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000180 if (!HasMultipleLines)
181 Left->PackingKind = PPK_Inconclusive;
182 else if (HasMultipleParametersOnALine)
183 Left->PackingKind = PPK_BinPacked;
184 else
185 Left->PackingKind = PPK_OnePerLine;
186
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000187 next();
188 return true;
189 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000190 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000191 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -0700192 else if (CurrentToken->is(tok::l_brace))
193 Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000194 updateParameterCount(Left, CurrentToken);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000195 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
196 !CurrentToken->Next->HasUnescapedNewline &&
197 !CurrentToken->Next->isTrailingComment())
198 HasMultipleParametersOnALine = true;
Stephen Hines651f13c2014-04-23 16:59:28 -0700199 if (CurrentToken->isOneOf(tok::kw_const, tok::kw_auto) ||
200 CurrentToken->isSimpleTypeSpecifier())
201 Contexts.back().IsExpression = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000202 if (!consumeToken())
203 return false;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000204 if (CurrentToken && CurrentToken->HasUnescapedNewline)
205 HasMultipleLines = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000206 }
207 return false;
208 }
209
210 bool parseSquare() {
211 if (!CurrentToken)
212 return false;
213
Alexander Kornienkod71b15b2013-06-17 13:19:53 +0000214 // A '[' could be an index subscript (after an identifier or after
Nico Weber051860e2013-02-10 02:08:05 +0000215 // ')' or ']'), it could be the start of an Objective-C method
216 // expression, or it could the the start of an Objective-C array literal.
Manuel Klimekb3987012013-05-29 14:47:47 +0000217 FormatToken *Left = CurrentToken->Previous;
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000218 FormatToken *Parent = Left->getPreviousNonComment();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000219 bool StartsObjCMethodExpr =
Daniel Jasper567dcf92013-09-05 09:29:45 +0000220 Contexts.back().CanBeExpression && Left->Type != TT_LambdaLSquare &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000221 (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
222 tok::kw_return, tok::kw_throw) ||
Daniel Jasperac3223e2013-04-10 09:49:49 +0000223 Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000224 Parent->Type == TT_CastRParen ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000225 getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
Daniel Jasper923ebef2013-03-14 13:45:21 +0000226 ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
Daniel Jasper6f21a982013-03-13 07:49:51 +0000227 Contexts.back().IsExpression = true;
Daniel Jasper8f54d882013-10-26 17:00:22 +0000228 bool ColonFound = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000229
Daniel Jasper4e778092013-02-06 10:05:46 +0000230 if (StartsObjCMethodExpr) {
231 Contexts.back().ColonIsObjCMethodExpr = true;
232 Left->Type = TT_ObjCMethodExpr;
Daniel Jaspera07aa662013-10-22 15:30:28 +0000233 } else if (Parent && Parent->is(tok::at)) {
234 Left->Type = TT_ArrayInitializerLSquare;
235 } else if (Left->Type == TT_Unknown) {
236 Left->Type = TT_ArraySubscriptLSquare;
Daniel Jasper4e778092013-02-06 10:05:46 +0000237 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000238
239 while (CurrentToken != NULL) {
240 if (CurrentToken->is(tok::r_square)) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000241 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
242 Left->Type == TT_ObjCMethodExpr) {
Nico Webere8a97982013-02-06 06:20:11 +0000243 // An ObjC method call is rarely followed by an open parenthesis.
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000244 // FIXME: Do we incorrectly label ":" with this?
245 StartsObjCMethodExpr = false;
246 Left->Type = TT_Unknown;
247 }
Daniel Jasper01786732013-02-04 07:21:18 +0000248 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000249 CurrentToken->Type = TT_ObjCMethodExpr;
Nico Webere8a97982013-02-06 06:20:11 +0000250 // determineStarAmpUsage() thinks that '*' '[' is allocating an
251 // array of pointers, but if '[' starts a selector then '*' is a
252 // binary operator.
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +0000253 if (Parent != NULL && Parent->Type == TT_PointerOrReference)
Nico Weber4ed7f3e2013-02-06 16:54:35 +0000254 Parent->Type = TT_BinaryOperator;
Daniel Jasper01786732013-02-04 07:21:18 +0000255 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000256 Left->MatchingParen = CurrentToken;
257 CurrentToken->MatchingParen = Left;
Stephen Hines651f13c2014-04-23 16:59:28 -0700258 if (Contexts.back().FirstObjCSelectorName != NULL) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000259 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
260 Contexts.back().LongestObjCSelectorName;
Stephen Hines651f13c2014-04-23 16:59:28 -0700261 if (Contexts.back().NumBlockParameters > 1)
262 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
263 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000264 next();
265 return true;
266 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000267 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000268 return false;
Daniel Jasper8f54d882013-10-26 17:00:22 +0000269 if (CurrentToken->is(tok::colon))
270 ColonFound = true;
Daniel Jaspera07aa662013-10-22 15:30:28 +0000271 if (CurrentToken->is(tok::comma) &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700272 Style.Language != FormatStyle::LK_Proto &&
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000273 (Left->Type == TT_ArraySubscriptLSquare ||
Daniel Jasper8f54d882013-10-26 17:00:22 +0000274 (Left->Type == TT_ObjCMethodExpr && !ColonFound)))
Daniel Jaspera07aa662013-10-22 15:30:28 +0000275 Left->Type = TT_ArrayInitializerLSquare;
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000276 updateParameterCount(Left, CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000277 if (!consumeToken())
278 return false;
279 }
280 return false;
281 }
282
283 bool parseBrace() {
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000284 if (CurrentToken != NULL) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000285 FormatToken *Left = CurrentToken->Previous;
Stephen Hines651f13c2014-04-23 16:59:28 -0700286
287 if (Contexts.back().CaretFound)
288 Left->Type = TT_ObjCBlockLBrace;
289 Contexts.back().CaretFound = false;
290
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000291 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
292 Contexts.back().ColonIsDictLiteral = true;
Nico Weberf2ff8122013-05-26 05:39:26 +0000293
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000294 while (CurrentToken != NULL) {
295 if (CurrentToken->is(tok::r_brace)) {
296 Left->MatchingParen = CurrentToken;
297 CurrentToken->MatchingParen = Left;
298 next();
299 return true;
300 }
301 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
302 return false;
303 updateParameterCount(Left, CurrentToken);
Stephen Hines651f13c2014-04-23 16:59:28 -0700304 if (CurrentToken->is(tok::colon) &&
305 Style.Language != FormatStyle::LK_Proto)
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000306 Left->Type = TT_DictLiteral;
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000307 if (!consumeToken())
308 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000309 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000310 }
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000311 // No closing "}" found, this probably starts a definition.
312 Line.StartsDefinition = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000313 return true;
314 }
Daniel Jasperc4615b72013-02-20 12:56:39 +0000315
Manuel Klimekb3987012013-05-29 14:47:47 +0000316 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000317 if (Current->is(tok::comma)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000318 ++Left->ParameterCount;
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000319 if (!Left->Role)
320 Left->Role.reset(new CommaSeparatedList(Style));
321 Left->Role->CommaFound(Current);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000322 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000323 Left->ParameterCount = 1;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000324 }
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000325 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000326
327 bool parseConditional() {
328 while (CurrentToken != NULL) {
329 if (CurrentToken->is(tok::colon)) {
330 CurrentToken->Type = TT_ConditionalExpr;
331 next();
332 return true;
333 }
334 if (!consumeToken())
335 return false;
336 }
337 return false;
338 }
339
340 bool parseTemplateDeclaration() {
341 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
342 CurrentToken->Type = TT_TemplateOpener;
343 next();
344 if (!parseAngle())
345 return false;
Daniel Jasper34511fb2013-02-19 17:14:38 +0000346 if (CurrentToken != NULL)
Manuel Klimekb3987012013-05-29 14:47:47 +0000347 CurrentToken->Previous->ClosesTemplateDeclaration = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000348 return true;
349 }
350 return false;
351 }
352
353 bool consumeToken() {
Manuel Klimekb3987012013-05-29 14:47:47 +0000354 FormatToken *Tok = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000355 next();
Manuel Klimekb3987012013-05-29 14:47:47 +0000356 switch (Tok->Tok.getKind()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000357 case tok::plus:
358 case tok::minus:
Manuel Klimekb3987012013-05-29 14:47:47 +0000359 if (Tok->Previous == NULL && Line.MustBeDeclaration)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000360 Tok->Type = TT_ObjCMethodSpecifier;
361 break;
362 case tok::colon:
Manuel Klimekb3987012013-05-29 14:47:47 +0000363 if (Tok->Previous == NULL)
Daniel Jaspercf6d76a2013-03-18 12:50:26 +0000364 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000365 // Colons from ?: are handled in parseConditional().
Manuel Klimekb3987012013-05-29 14:47:47 +0000366 if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000367 Tok->Type = TT_CtorInitializerColon;
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000368 } else if (Contexts.back().ColonIsDictLiteral) {
369 Tok->Type = TT_DictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000370 } else if (Contexts.back().ColonIsObjCMethodExpr ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000371 Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000372 Tok->Type = TT_ObjCMethodExpr;
Manuel Klimekb3987012013-05-29 14:47:47 +0000373 Tok->Previous->Type = TT_ObjCSelectorName;
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000374 if (Tok->Previous->ColumnWidth >
Alexander Kornienko00895102013-06-05 14:09:10 +0000375 Contexts.back().LongestObjCSelectorName) {
Alexander Kornienko01fe9f92013-10-10 13:36:20 +0000376 Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
Alexander Kornienko00895102013-06-05 14:09:10 +0000377 }
Daniel Jasper4e778092013-02-06 10:05:46 +0000378 if (Contexts.back().FirstObjCSelectorName == NULL)
Manuel Klimekb3987012013-05-29 14:47:47 +0000379 Contexts.back().FirstObjCSelectorName = Tok->Previous;
Daniel Jasper4e778092013-02-06 10:05:46 +0000380 } else if (Contexts.back().ColonIsForRangeExpr) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000381 Tok->Type = TT_RangeBasedForLoopColon;
Alexander Kornienko01fe9f92013-10-10 13:36:20 +0000382 } else if (CurrentToken != NULL &&
383 CurrentToken->is(tok::numeric_constant)) {
384 Tok->Type = TT_BitFieldColon;
Daniel Jaspercea014b2013-10-08 16:24:07 +0000385 } else if (Contexts.size() == 1 && Line.First->isNot(tok::kw_enum)) {
Daniel Jasper6cabab42013-02-14 08:42:54 +0000386 Tok->Type = TT_InheritanceColon;
Daniel Jasper923ebef2013-03-14 13:45:21 +0000387 } else if (Contexts.back().ContextKind == tok::l_paren) {
388 Tok->Type = TT_InlineASMColon;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000389 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000390 break;
391 case tok::kw_if:
392 case tok::kw_while:
393 if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
394 next();
Nico Weber27268772013-06-26 00:30:14 +0000395 if (!parseParens(/*LookForDecls=*/true))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000396 return false;
397 }
398 break;
399 case tok::kw_for:
Daniel Jasper4e778092013-02-06 10:05:46 +0000400 Contexts.back().ColonIsForRangeExpr = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000401 next();
402 if (!parseParens())
403 return false;
404 break;
405 case tok::l_paren:
406 if (!parseParens())
407 return false;
Daniel Jasper59875ac2013-11-07 17:43:07 +0000408 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700409 !Contexts.back().IsExpression && Line.First->Type != TT_ObjCProperty)
Daniel Jasper3c08a812013-02-24 18:54:32 +0000410 Line.MightBeFunctionDecl = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000411 break;
412 case tok::l_square:
413 if (!parseSquare())
414 return false;
415 break;
416 case tok::l_brace:
417 if (!parseBrace())
418 return false;
419 break;
420 case tok::less:
Daniel Jasper0236dd02013-07-30 22:37:19 +0000421 if (Tok->Previous && !Tok->Previous->Tok.isLiteral() && parseAngle())
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000422 Tok->Type = TT_TemplateOpener;
423 else {
424 Tok->Type = TT_BinaryOperator;
425 CurrentToken = Tok;
426 next();
427 }
428 break;
429 case tok::r_paren:
430 case tok::r_square:
431 return false;
432 case tok::r_brace:
433 // Lines can start with '}'.
Manuel Klimekb3987012013-05-29 14:47:47 +0000434 if (Tok->Previous != NULL)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000435 return false;
436 break;
437 case tok::greater:
438 Tok->Type = TT_BinaryOperator;
439 break;
440 case tok::kw_operator:
Daniel Jasper174f60f2013-09-02 09:20:39 +0000441 while (CurrentToken &&
442 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000443 if (CurrentToken->isOneOf(tok::star, tok::amp))
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000444 CurrentToken->Type = TT_PointerOrReference;
445 consumeToken();
Daniel Jasper174f60f2013-09-02 09:20:39 +0000446 if (CurrentToken && CurrentToken->Previous->Type == TT_BinaryOperator)
Daniel Jasperc476ea92013-08-28 07:27:35 +0000447 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000448 }
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000449 if (CurrentToken) {
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000450 CurrentToken->Type = TT_OverloadedOperatorLParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000451 if (CurrentToken->Previous->Type == TT_BinaryOperator)
452 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000453 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000454 break;
455 case tok::question:
456 parseConditional();
457 break;
458 case tok::kw_template:
459 parseTemplateDeclaration();
460 break;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000461 case tok::identifier:
Manuel Klimekb3987012013-05-29 14:47:47 +0000462 if (Line.First->is(tok::kw_for) &&
463 Tok->Tok.getIdentifierInfo() == &Ident_in)
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000464 Tok->Type = TT_ObjCForIn;
465 break;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000466 case tok::comma:
467 if (Contexts.back().FirstStartOfName)
468 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000469 if (Contexts.back().InCtorInitializer)
470 Tok->Type = TT_CtorInitializerComma;
Stephen Hines651f13c2014-04-23 16:59:28 -0700471 if (Contexts.back().IsForEachMacro)
472 Contexts.back().IsExpression = true;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000473 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000474 default:
475 break;
476 }
477 return true;
478 }
479
480 void parseIncludeDirective() {
481 next();
482 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
483 next();
484 while (CurrentToken != NULL) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000485 if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000486 CurrentToken->Type = TT_ImplicitStringLiteral;
487 next();
488 }
489 } else {
490 while (CurrentToken != NULL) {
Daniel Jasper3a204412013-02-23 07:46:38 +0000491 if (CurrentToken->is(tok::string_literal))
492 // Mark these string literals as "implicit" literals, too, so that
493 // they are not split or line-wrapped.
494 CurrentToken->Type = TT_ImplicitStringLiteral;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000495 next();
496 }
497 }
498 }
499
500 void parseWarningOrError() {
501 next();
502 // We still want to format the whitespace left of the first token of the
503 // warning or error.
504 next();
505 while (CurrentToken != NULL) {
506 CurrentToken->Type = TT_ImplicitStringLiteral;
507 next();
508 }
509 }
510
Stephen Hines651f13c2014-04-23 16:59:28 -0700511 void parsePragma() {
512 next(); // Consume "pragma".
513 if (CurrentToken && CurrentToken->TokenText == "mark") {
514 next(); // Consume "mark".
515 next(); // Consume first token (so we fix leading whitespace).
516 while (CurrentToken != NULL) {
517 CurrentToken->Type = TT_ImplicitStringLiteral;
518 next();
519 }
520 }
521 }
522
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000523 void parsePreprocessorDirective() {
524 next();
525 if (CurrentToken == NULL)
526 return;
Alexander Kornienkob18c2582013-10-11 21:43:05 +0000527 if (CurrentToken->Tok.is(tok::numeric_constant)) {
528 CurrentToken->SpacesRequiredBefore = 1;
529 return;
530 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000531 // Hashes in the middle of a line can lead to any strange token
532 // sequence.
Manuel Klimekb3987012013-05-29 14:47:47 +0000533 if (CurrentToken->Tok.getIdentifierInfo() == NULL)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000534 return;
Manuel Klimekb3987012013-05-29 14:47:47 +0000535 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000536 case tok::pp_include:
537 case tok::pp_import:
538 parseIncludeDirective();
539 break;
540 case tok::pp_error:
541 case tok::pp_warning:
542 parseWarningOrError();
543 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700544 case tok::pp_pragma:
545 parsePragma();
546 break;
Daniel Jasperaae7bad2013-04-23 13:54:04 +0000547 case tok::pp_if:
548 case tok::pp_elif:
Stephen Hines651f13c2014-04-23 16:59:28 -0700549 Contexts.back().IsExpression = true;
Daniel Jasperaae7bad2013-04-23 13:54:04 +0000550 parseLine();
551 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000552 default:
553 break;
554 }
Daniel Jasper5b7e7b02013-02-05 09:34:14 +0000555 while (CurrentToken != NULL)
556 next();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000557 }
558
Nico Weber95e8e462013-02-12 16:17:07 +0000559public:
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000560 LineType parseLine() {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000561 if (CurrentToken->is(tok::hash)) {
562 parsePreprocessorDirective();
563 return LT_PreprocessorDirective;
564 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700565
566 // Directly allow to 'import <string-literal>' to support protocol buffer
567 // definitions (code.google.com/p/protobuf) or missing "#" (either way we
568 // should not break the line).
569 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
570 if (Info && Info->getPPKeywordID() == tok::pp_import &&
571 CurrentToken->Next && CurrentToken->Next->is(tok::string_literal))
572 parseIncludeDirective();
573
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000574 while (CurrentToken != NULL) {
575 if (CurrentToken->is(tok::kw_virtual))
576 KeywordVirtualFound = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000577 if (!consumeToken())
578 return LT_Invalid;
579 }
580 if (KeywordVirtualFound)
581 return LT_VirtualFunctionDecl;
582
Manuel Klimekb3987012013-05-29 14:47:47 +0000583 if (Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000584 if (Contexts.back().FirstObjCSelectorName != NULL)
585 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
586 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000587 return LT_ObjCMethodDecl;
588 }
589
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000590 return LT_Other;
591 }
592
Nico Weber95e8e462013-02-12 16:17:07 +0000593private:
Stephen Hines651f13c2014-04-23 16:59:28 -0700594 void resetTokenMetadata(FormatToken *Token) {
595 if (Token == nullptr) return;
596
597 // Reset token type in case we have already looked at it and then
598 // recovered from an error (e.g. failure to find the matching >).
599 if (CurrentToken->Type != TT_LambdaLSquare &&
600 CurrentToken->Type != TT_FunctionLBrace &&
601 CurrentToken->Type != TT_ImplicitStringLiteral &&
602 CurrentToken->Type != TT_TrailingReturnArrow)
603 CurrentToken->Type = TT_Unknown;
604 if (CurrentToken->Role)
605 CurrentToken->Role.reset(NULL);
606 CurrentToken->FakeLParens.clear();
607 CurrentToken->FakeRParens = 0;
608 }
609
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000610 void next() {
Daniel Jasper01786732013-02-04 07:21:18 +0000611 if (CurrentToken != NULL) {
612 determineTokenType(*CurrentToken);
Daniel Jasper4e778092013-02-06 10:05:46 +0000613 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
Stephen Hines651f13c2014-04-23 16:59:28 -0700614 CurrentToken->NestingLevel = Contexts.size() - 1;
Daniel Jasper01786732013-02-04 07:21:18 +0000615 }
616
Manuel Klimekb3987012013-05-29 14:47:47 +0000617 if (CurrentToken != NULL)
618 CurrentToken = CurrentToken->Next;
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000619
Stephen Hines651f13c2014-04-23 16:59:28 -0700620 resetTokenMetadata(CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000621 }
622
Daniel Jasper4e778092013-02-06 10:05:46 +0000623 /// \brief A struct to hold information valid in a specific context, e.g.
624 /// a pair of parenthesis.
625 struct Context {
Daniel Jasper923ebef2013-03-14 13:45:21 +0000626 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
627 bool IsExpression)
628 : ContextKind(ContextKind), BindingStrength(BindingStrength),
Stephen Hines651f13c2014-04-23 16:59:28 -0700629 LongestObjCSelectorName(0), NumBlockParameters(0),
630 ColonIsForRangeExpr(false), ColonIsDictLiteral(false),
631 ColonIsObjCMethodExpr(false), FirstObjCSelectorName(NULL),
632 FirstStartOfName(NULL), IsExpression(IsExpression),
633 CanBeExpression(true), InTemplateArgument(false),
634 InCtorInitializer(false), CaretFound(false), IsForEachMacro(false) {}
Daniel Jasper01786732013-02-04 07:21:18 +0000635
Daniel Jasper923ebef2013-03-14 13:45:21 +0000636 tok::TokenKind ContextKind;
Daniel Jasper4e778092013-02-06 10:05:46 +0000637 unsigned BindingStrength;
638 unsigned LongestObjCSelectorName;
Stephen Hines651f13c2014-04-23 16:59:28 -0700639 unsigned NumBlockParameters;
Daniel Jasper4e778092013-02-06 10:05:46 +0000640 bool ColonIsForRangeExpr;
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000641 bool ColonIsDictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000642 bool ColonIsObjCMethodExpr;
Manuel Klimekb3987012013-05-29 14:47:47 +0000643 FormatToken *FirstObjCSelectorName;
644 FormatToken *FirstStartOfName;
Daniel Jasper4e778092013-02-06 10:05:46 +0000645 bool IsExpression;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000646 bool CanBeExpression;
Stephen Hines651f13c2014-04-23 16:59:28 -0700647 bool InTemplateArgument;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000648 bool InCtorInitializer;
Stephen Hines651f13c2014-04-23 16:59:28 -0700649 bool CaretFound;
650 bool IsForEachMacro;
Daniel Jasper4e778092013-02-06 10:05:46 +0000651 };
652
653 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
654 /// of each instance.
655 struct ScopedContextCreator {
656 AnnotatingParser &P;
657
Daniel Jasper923ebef2013-03-14 13:45:21 +0000658 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
659 unsigned Increase)
660 : P(P) {
Daniel Jasper2a409b62013-07-08 14:34:09 +0000661 P.Contexts.push_back(Context(ContextKind,
662 P.Contexts.back().BindingStrength + Increase,
663 P.Contexts.back().IsExpression));
Daniel Jasper4e778092013-02-06 10:05:46 +0000664 }
665
666 ~ScopedContextCreator() { P.Contexts.pop_back(); }
667 };
Daniel Jasper01786732013-02-04 07:21:18 +0000668
Manuel Klimekb3987012013-05-29 14:47:47 +0000669 void determineTokenType(FormatToken &Current) {
670 if (Current.getPrecedence() == prec::Assignment &&
Daniel Jasper53352602013-08-12 12:16:34 +0000671 !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
Manuel Klimekb3987012013-05-29 14:47:47 +0000672 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000673 Contexts.back().IsExpression = true;
Manuel Klimekb3987012013-05-29 14:47:47 +0000674 for (FormatToken *Previous = Current.Previous;
Daniel Jasper7e274002013-09-11 20:37:10 +0000675 Previous && !Previous->isOneOf(tok::comma, tok::semi);
Manuel Klimekb3987012013-05-29 14:47:47 +0000676 Previous = Previous->Previous) {
Daniel Jasper9c65b062013-02-27 11:43:50 +0000677 if (Previous->is(tok::r_square))
678 Previous = Previous->MatchingParen;
Daniel Jasper01786732013-02-04 07:21:18 +0000679 if (Previous->Type == TT_BinaryOperator &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000680 Previous->isOneOf(tok::star, tok::amp)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000681 Previous->Type = TT_PointerOrReference;
682 }
Daniel Jasper01786732013-02-04 07:21:18 +0000683 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700684 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000685 Contexts.back().IsExpression = true;
Stephen Hines651f13c2014-04-23 16:59:28 -0700686 } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
687 !Line.InPPDirective) {
688 bool ParametersOfFunctionType =
689 Current.Previous && Current.Previous->is(tok::r_paren) &&
690 Current.Previous->MatchingParen &&
691 Current.Previous->MatchingParen->Type == TT_FunctionTypeLParen;
692 bool IsForOrCatch = Current.Previous &&
693 Current.Previous->isOneOf(tok::kw_for, tok::kw_catch);
694 Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000695 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000696 for (FormatToken *Previous = Current.Previous;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000697 Previous && Previous->isOneOf(tok::star, tok::amp);
Manuel Klimekb3987012013-05-29 14:47:47 +0000698 Previous = Previous->Previous)
Nico Weber95e8e462013-02-12 16:17:07 +0000699 Previous->Type = TT_PointerOrReference;
Manuel Klimekb3987012013-05-29 14:47:47 +0000700 } else if (Current.Previous &&
701 Current.Previous->Type == TT_CtorInitializerColon) {
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000702 Contexts.back().IsExpression = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000703 Contexts.back().InCtorInitializer = true;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000704 } else if (Current.is(tok::kw_new)) {
705 Contexts.back().CanBeExpression = false;
Daniel Jasperf9504aa2013-11-07 19:56:07 +0000706 } else if (Current.is(tok::semi) || Current.is(tok::exclaim)) {
Daniel Jasper16a69ef2013-05-03 14:41:24 +0000707 // This should be the condition or increment in a for-loop.
708 Contexts.back().IsExpression = true;
Nico Weber95e8e462013-02-12 16:17:07 +0000709 }
Daniel Jasper01786732013-02-04 07:21:18 +0000710
711 if (Current.Type == TT_Unknown) {
Daniel Jasper6b3ff8c2013-09-27 08:29:16 +0000712 // Line.MightBeFunctionDecl can only be true after the parentheses of a
713 // function declaration have been found. In this case, 'Current' is a
714 // trailing token of this declaration and thus cannot be a name.
715 if (isStartOfName(Current) && !Line.MightBeFunctionDecl) {
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000716 Contexts.back().FirstStartOfName = &Current;
Daniel Jasper3c08a812013-02-24 18:54:32 +0000717 Current.Type = TT_StartOfName;
Daniel Jasper2ca37412013-07-09 14:36:48 +0000718 } else if (Current.is(tok::kw_auto)) {
719 AutoFound = true;
Daniel Jasper3262f4c2013-07-11 14:33:06 +0000720 } else if (Current.is(tok::arrow) && AutoFound &&
721 Line.MustBeDeclaration) {
Daniel Jasper2ca37412013-07-09 14:36:48 +0000722 Current.Type = TT_TrailingReturnArrow;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000723 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000724 Current.Type =
Daniel Jasperd6104f62013-07-05 13:30:40 +0000725 determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700726 Contexts.back().IsExpression,
727 Contexts.back().InTemplateArgument);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000728 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000729 Current.Type = determinePlusMinusCaretUsage(Current);
Stephen Hines651f13c2014-04-23 16:59:28 -0700730 if (Current.Type == TT_UnaryOperator) {
731 ++Contexts.back().NumBlockParameters;
732 if (Current.is(tok::caret))
733 Contexts.back().CaretFound = true;
734 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000735 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000736 Current.Type = determineIncrementUsage(Current);
737 } else if (Current.is(tok::exclaim)) {
738 Current.Type = TT_UnaryOperator;
Manuel Klimek31e44f72013-09-04 08:20:47 +0000739 } else if (Current.isBinaryOperator() &&
740 (!Current.Previous ||
741 Current.Previous->isNot(tok::l_square))) {
Daniel Jasper01786732013-02-04 07:21:18 +0000742 Current.Type = TT_BinaryOperator;
743 } else if (Current.is(tok::comment)) {
Alexander Kornienko00895102013-06-05 14:09:10 +0000744 if (Current.TokenText.startswith("//"))
Daniel Jasper01786732013-02-04 07:21:18 +0000745 Current.Type = TT_LineComment;
746 else
747 Current.Type = TT_BlockComment;
Nico Weber37d69312013-02-13 04:13:13 +0000748 } else if (Current.is(tok::r_paren)) {
Daniel Jasperb8b42952013-05-31 16:14:28 +0000749 FormatToken *LeftOfParens = NULL;
750 if (Current.MatchingParen)
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000751 LeftOfParens = Current.MatchingParen->getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000752 bool IsCast = false;
753 bool ParensAreEmpty = Current.Previous == Current.MatchingParen;
754 bool ParensAreType = !Current.Previous ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000755 Current.Previous->Type == TT_PointerOrReference ||
Daniel Jasperb8b42952013-05-31 16:14:28 +0000756 Current.Previous->Type == TT_TemplateCloser ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700757 Current.Previous->isSimpleTypeSpecifier();
Nico Weber37d69312013-02-13 04:13:13 +0000758 bool ParensCouldEndDecl =
Manuel Klimekb3987012013-05-29 14:47:47 +0000759 Current.Next &&
760 Current.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
Daniel Jasper6a365aa2013-03-13 17:13:53 +0000761 bool IsSizeOfOrAlignOf =
Daniel Jasperb8b42952013-05-31 16:14:28 +0000762 LeftOfParens &&
763 LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
764 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700765 ((Contexts.size() > 1 &&
766 Contexts[Contexts.size() - 2].IsExpression) ||
Daniel Jasper0c368782013-07-15 15:04:42 +0000767 (Current.Next && Current.Next->isBinaryOperator())))
Daniel Jasperb8b42952013-05-31 16:14:28 +0000768 IsCast = true;
Daniel Jasper2a409b62013-07-08 14:34:09 +0000769 if (Current.Next && Current.Next->isNot(tok::string_literal) &&
Daniel Jasperb8b42952013-05-31 16:14:28 +0000770 (Current.Next->Tok.isLiteral() ||
771 Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
772 IsCast = true;
773 // If there is an identifier after the (), it is likely a cast, unless
774 // there is also an identifier before the ().
Daniel Jasperff1a2e52013-06-06 08:20:20 +0000775 if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL ||
776 LeftOfParens->is(tok::kw_return)) &&
Daniel Jasper526df0f2013-07-08 14:58:01 +0000777 LeftOfParens->Type != TT_OverloadedOperator &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700778 LeftOfParens->isNot(tok::at) &&
Nico Weber465e8612013-06-25 00:55:57 +0000779 LeftOfParens->Type != TT_TemplateCloser && Current.Next &&
780 Current.Next->is(tok::identifier))
Daniel Jasperb8b42952013-05-31 16:14:28 +0000781 IsCast = true;
782 if (IsCast && !ParensAreEmpty)
Nico Weber37d69312013-02-13 04:13:13 +0000783 Current.Type = TT_CastRParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000784 } else if (Current.is(tok::at) && Current.Next) {
785 switch (Current.Next->Tok.getObjCKeywordID()) {
Daniel Jasper01786732013-02-04 07:21:18 +0000786 case tok::objc_interface:
787 case tok::objc_implementation:
788 case tok::objc_protocol:
789 Current.Type = TT_ObjCDecl;
790 break;
791 case tok::objc_property:
792 Current.Type = TT_ObjCProperty;
793 break;
794 default:
795 break;
796 }
Daniel Jasper5ad390d2013-05-28 11:30:49 +0000797 } else if (Current.is(tok::period)) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000798 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
Daniel Jasper5ad390d2013-05-28 11:30:49 +0000799 if (PreviousNoComment &&
800 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
801 Current.Type = TT_DesignatedInitializerPeriod;
Stephen Hines651f13c2014-04-23 16:59:28 -0700802 } else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
803 Line.MightBeFunctionDecl && Contexts.size() == 1) {
804 // Line.MightBeFunctionDecl can only be true after the parentheses of a
805 // function declaration have been found.
806 Current.Type = TT_TrailingAnnotation;
Daniel Jasper01786732013-02-04 07:21:18 +0000807 }
808 }
809 }
810
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000811 /// \brief Take a guess at whether \p Tok starts a name of a function or
812 /// variable declaration.
813 ///
814 /// This is a heuristic based on whether \p Tok is an identifier following
815 /// something that is likely a type.
816 bool isStartOfName(const FormatToken &Tok) {
817 if (Tok.isNot(tok::identifier) || Tok.Previous == NULL)
818 return false;
819
820 // Skip "const" as it does not have an influence on whether this is a name.
821 FormatToken *PreviousNotConst = Tok.Previous;
822 while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const))
823 PreviousNotConst = PreviousNotConst->Previous;
824
825 if (PreviousNotConst == NULL)
826 return false;
827
Daniel Jasper2a409b62013-07-08 14:34:09 +0000828 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
829 PreviousNotConst->Previous &&
830 PreviousNotConst->Previous->is(tok::hash);
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000831
Daniel Jasper92495a82013-08-19 10:16:18 +0000832 if (PreviousNotConst->Type == TT_TemplateCloser)
833 return PreviousNotConst && PreviousNotConst->MatchingParen &&
834 PreviousNotConst->MatchingParen->Previous &&
835 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
836
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000837 return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
838 PreviousNotConst->Type == TT_PointerOrReference ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700839 PreviousNotConst->isSimpleTypeSpecifier();
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000840 }
841
Daniel Jasper01786732013-02-04 07:21:18 +0000842 /// \brief Return the type of the given token assuming it is * or &.
Stephen Hines651f13c2014-04-23 16:59:28 -0700843 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
844 bool InTemplateArgument) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000845 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasper01786732013-02-04 07:21:18 +0000846 if (PrevToken == NULL)
847 return TT_UnaryOperator;
848
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000849 const FormatToken *NextToken = Tok.getNextNonComment();
Daniel Jasper01786732013-02-04 07:21:18 +0000850 if (NextToken == NULL)
851 return TT_Unknown;
852
Daniel Jasper431f5912013-05-28 08:33:00 +0000853 if (PrevToken->is(tok::coloncolon) ||
854 (PrevToken->is(tok::l_paren) && !IsExpression))
Daniel Jasper8a5d7cd2013-03-01 17:13:29 +0000855 return TT_PointerOrReference;
856
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000857 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
Daniel Jasperd3cf17b2013-03-14 10:50:25 +0000858 tok::comma, tok::semi, tok::kw_return, tok::colon,
Daniel Jasper65da8e92013-09-21 17:31:51 +0000859 tok::equal, tok::kw_delete, tok::kw_sizeof) ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000860 PrevToken->Type == TT_BinaryOperator ||
Daniel Jasper01786732013-02-04 07:21:18 +0000861 PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
862 return TT_UnaryOperator;
863
Nico Webere8a97982013-02-06 06:20:11 +0000864 if (NextToken->is(tok::l_square))
865 return TT_PointerOrReference;
866
Daniel Jasperdb8afe42013-09-10 10:26:38 +0000867 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
868 PrevToken->MatchingParen->Previous &&
869 PrevToken->MatchingParen->Previous->is(tok::kw_typeof))
870 return TT_PointerOrReference;
871
Manuel Klimekb3987012013-05-29 14:47:47 +0000872 if (PrevToken->Tok.isLiteral() ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700873 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
874 tok::kw_false) ||
875 NextToken->Tok.isLiteral() ||
876 NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
877 NextToken->isUnaryOperator() ||
878 // If we know we're in a template argument, there are no named
879 // declarations. Thus, having an identifier on the right-hand side
880 // indicates a binary operator.
881 (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
Daniel Jasper01786732013-02-04 07:21:18 +0000882 return TT_BinaryOperator;
883
Daniel Jasper01786732013-02-04 07:21:18 +0000884 // It is very unlikely that we are going to find a pointer or reference type
885 // definition on the RHS of an assignment.
886 if (IsExpression)
887 return TT_BinaryOperator;
888
889 return TT_PointerOrReference;
890 }
891
Manuel Klimekb3987012013-05-29 14:47:47 +0000892 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000893 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000894 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper01786732013-02-04 07:21:18 +0000895 return TT_UnaryOperator;
896
897 // Use heuristics to recognize unary operators.
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000898 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
899 tok::question, tok::colon, tok::kw_return,
900 tok::kw_case, tok::at, tok::l_brace))
Daniel Jasper01786732013-02-04 07:21:18 +0000901 return TT_UnaryOperator;
902
Nico Weberee0feec2013-02-05 16:21:00 +0000903 // There can't be two consecutive binary operators.
Daniel Jasper01786732013-02-04 07:21:18 +0000904 if (PrevToken->Type == TT_BinaryOperator)
905 return TT_UnaryOperator;
906
907 // Fall back to marking the token as binary operator.
908 return TT_BinaryOperator;
909 }
910
911 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
Manuel Klimekb3987012013-05-29 14:47:47 +0000912 TokenType determineIncrementUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000913 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000914 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper01786732013-02-04 07:21:18 +0000915 return TT_UnaryOperator;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000916 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
Daniel Jasper01786732013-02-04 07:21:18 +0000917 return TT_TrailingUnaryOperator;
918
919 return TT_UnaryOperator;
920 }
Daniel Jasper4e778092013-02-06 10:05:46 +0000921
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000922
Daniel Jasper4e778092013-02-06 10:05:46 +0000923 SmallVector<Context, 8> Contexts;
924
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000925 const FormatStyle &Style;
Daniel Jasper4e778092013-02-06 10:05:46 +0000926 AnnotatedLine &Line;
Manuel Klimekb3987012013-05-29 14:47:47 +0000927 FormatToken *CurrentToken;
Daniel Jasper4e778092013-02-06 10:05:46 +0000928 bool KeywordVirtualFound;
Daniel Jasper2ca37412013-07-09 14:36:48 +0000929 bool AutoFound;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000930 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000931};
932
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000933static int PrecedenceUnaryOperator = prec::PointerToMember + 1;
934static int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
935
Daniel Jasper29f123b2013-02-08 15:28:42 +0000936/// \brief Parses binary expressions by inserting fake parenthesis based on
937/// operator precedence.
938class ExpressionParser {
939public:
Daniel Jasper9acb8b42013-06-06 09:11:58 +0000940 ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {
941 // Skip leading "}", e.g. in "} else if (...) {".
942 if (Current->is(tok::r_brace))
943 next();
944 }
Daniel Jasper29f123b2013-02-08 15:28:42 +0000945
946 /// \brief Parse expressions with the given operatore precedence.
Daniel Jasper237d4c12013-02-23 21:01:55 +0000947 void parse(int Precedence = 0) {
Daniel Jasper966e6d32013-11-07 19:23:49 +0000948 // Skip 'return' and ObjC selector colons as they are not part of a binary
949 // expression.
950 while (Current &&
951 (Current->is(tok::kw_return) ||
952 (Current->is(tok::colon) && Current->Type == TT_ObjCMethodExpr)))
Daniel Jasperf78bf4a2013-09-30 08:29:03 +0000953 next();
954
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000955 if (Current == NULL || Precedence > PrecedenceArrowAndPeriod)
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000956 return;
957
Daniel Jasperc01897c2013-05-31 14:56:12 +0000958 // Conditional expressions need to be parsed separately for proper nesting.
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000959 if (Precedence == prec::Conditional) {
Daniel Jasperc01897c2013-05-31 14:56:12 +0000960 parseConditionalExpr();
961 return;
962 }
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000963
964 // Parse unary operators, which all have a higher precedence than binary
965 // operators.
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000966 if (Precedence == PrecedenceUnaryOperator) {
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000967 parseUnaryOperator();
Daniel Jasper29f123b2013-02-08 15:28:42 +0000968 return;
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000969 }
Daniel Jasper29f123b2013-02-08 15:28:42 +0000970
Manuel Klimekb3987012013-05-29 14:47:47 +0000971 FormatToken *Start = Current;
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000972 FormatToken *LatestOperator = NULL;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000973
Daniel Jasper237d4c12013-02-23 21:01:55 +0000974 while (Current) {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000975 // Consume operators with higher precedence.
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000976 parse(Precedence + 1);
Daniel Jasper29f123b2013-02-08 15:28:42 +0000977
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000978 int CurrentPrecedence = getCurrentPrecedence();
979
980 if (Current && Current->Type == TT_ObjCSelectorName &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700981 Precedence == CurrentPrecedence) {
982 if (LatestOperator)
983 addFakeParenthesis(Start, prec::Level(Precedence));
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000984 Start = Current;
Stephen Hines651f13c2014-04-23 16:59:28 -0700985 }
Daniel Jasper237d4c12013-02-23 21:01:55 +0000986
Daniel Jasper29f123b2013-02-08 15:28:42 +0000987 // At the end of the line or when an operator with higher precedence is
988 // found, insert fake parenthesis and return.
Daniel Jasperac3223e2013-04-10 09:49:49 +0000989 if (Current == NULL || Current->closesScope() ||
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000990 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) {
991 if (LatestOperator) {
992 if (Precedence == PrecedenceArrowAndPeriod) {
993 LatestOperator->LastInChainOfCalls = true;
994 // Call expressions don't have a binary operator precedence.
995 addFakeParenthesis(Start, prec::Unknown);
996 } else {
997 addFakeParenthesis(Start, prec::Level(Precedence));
998 }
999 }
Daniel Jasper29f123b2013-02-08 15:28:42 +00001000 return;
1001 }
1002
1003 // Consume scopes: (), [], <> and {}
Daniel Jasperac3223e2013-04-10 09:49:49 +00001004 if (Current->opensScope()) {
1005 while (Current && !Current->closesScope()) {
Daniel Jasper29f123b2013-02-08 15:28:42 +00001006 next();
1007 parse();
1008 }
1009 next();
1010 } else {
1011 // Operator found.
Daniel Jasper237d4c12013-02-23 21:01:55 +00001012 if (CurrentPrecedence == Precedence)
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001013 LatestOperator = Current;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001014
1015 next();
1016 }
1017 }
1018 }
1019
1020private:
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001021 /// \brief Gets the precedence (+1) of the given token for binary operators
1022 /// and other tokens that we treat like binary operators.
1023 int getCurrentPrecedence() {
1024 if (Current) {
1025 if (Current->Type == TT_ConditionalExpr)
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001026 return prec::Conditional;
Daniel Jasper966e6d32013-11-07 19:23:49 +00001027 else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon ||
1028 Current->Type == TT_ObjCSelectorName)
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001029 return 0;
Stephen Hines651f13c2014-04-23 16:59:28 -07001030 else if (Current->Type == TT_RangeBasedForLoopColon)
1031 return prec::Comma;
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001032 else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001033 return Current->getPrecedence();
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001034 else if (Current->isOneOf(tok::period, tok::arrow))
1035 return PrecedenceArrowAndPeriod;
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001036 }
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001037 return -1;
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001038 }
1039
Daniel Jasperc01897c2013-05-31 14:56:12 +00001040 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
1041 Start->FakeLParens.push_back(Precedence);
Daniel Jasperdb4813a2013-09-06 08:08:14 +00001042 if (Precedence > prec::Unknown)
1043 Start->StartsBinaryExpression = true;
1044 if (Current) {
Daniel Jasperc01897c2013-05-31 14:56:12 +00001045 ++Current->Previous->FakeRParens;
Daniel Jasperdb4813a2013-09-06 08:08:14 +00001046 if (Precedence > prec::Unknown)
1047 Current->Previous->EndsBinaryExpression = true;
1048 }
Daniel Jasperc01897c2013-05-31 14:56:12 +00001049 }
1050
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001051 /// \brief Parse unary operator expressions and surround them with fake
1052 /// parentheses if appropriate.
1053 void parseUnaryOperator() {
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001054 if (Current == NULL || Current->Type != TT_UnaryOperator) {
1055 parse(PrecedenceArrowAndPeriod);
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001056 return;
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001057 }
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001058
1059 FormatToken *Start = Current;
1060 next();
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001061 parseUnaryOperator();
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001062
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001063 // The actual precedence doesn't matter.
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001064 addFakeParenthesis(Start, prec::Unknown);
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001065 }
1066
Daniel Jasperc01897c2013-05-31 14:56:12 +00001067 void parseConditionalExpr() {
1068 FormatToken *Start = Current;
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001069 parse(prec::LogicalOr);
Daniel Jasperc01897c2013-05-31 14:56:12 +00001070 if (!Current || !Current->is(tok::question))
1071 return;
1072 next();
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001073 parse(prec::LogicalOr);
Daniel Jasperc01897c2013-05-31 14:56:12 +00001074 if (!Current || Current->Type != TT_ConditionalExpr)
1075 return;
1076 next();
1077 parseConditionalExpr();
1078 addFakeParenthesis(Start, prec::Conditional);
1079 }
1080
Daniel Jasper29f123b2013-02-08 15:28:42 +00001081 void next() {
Alexander Kornienkod71b15b2013-06-17 13:19:53 +00001082 if (Current)
1083 Current = Current->Next;
1084 while (Current && Current->isTrailingComment())
Manuel Klimekb3987012013-05-29 14:47:47 +00001085 Current = Current->Next;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001086 }
1087
Manuel Klimekb3987012013-05-29 14:47:47 +00001088 FormatToken *Current;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001089};
1090
Craig Topper14e66492013-07-01 04:03:19 +00001091} // end anonymous namespace
1092
Daniel Jasperb77d7412013-09-06 07:54:20 +00001093void
1094TokenAnnotator::setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines) {
Daniel Jasperb77d7412013-09-06 07:54:20 +00001095 const AnnotatedLine *NextNonCommentLine = NULL;
Daniel Jasper2a80ad62013-11-05 19:10:03 +00001096 for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
1097 E = Lines.rend();
1098 I != E; ++I) {
1099 if (NextNonCommentLine && (*I)->First->is(tok::comment) &&
1100 (*I)->First->Next == NULL)
1101 (*I)->Level = NextNonCommentLine->Level;
Daniel Jasperb77d7412013-09-06 07:54:20 +00001102 else
Daniel Jasper2a80ad62013-11-05 19:10:03 +00001103 NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : NULL;
1104
1105 setCommentLineLevels((*I)->Children);
Daniel Jasperb77d7412013-09-06 07:54:20 +00001106 }
1107}
1108
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001109void TokenAnnotator::annotate(AnnotatedLine &Line) {
Daniel Jasperb77d7412013-09-06 07:54:20 +00001110 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1111 E = Line.Children.end();
Daniel Jasper567dcf92013-09-05 09:29:45 +00001112 I != E; ++I) {
1113 annotate(**I);
1114 }
Daniel Jasperd4a03db2013-08-22 15:00:41 +00001115 AnnotatingParser Parser(Style, Line, Ident_in);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001116 Line.Type = Parser.parseLine();
1117 if (Line.Type == LT_Invalid)
1118 return;
1119
Daniel Jasper29f123b2013-02-08 15:28:42 +00001120 ExpressionParser ExprParser(Line);
1121 ExprParser.parse();
1122
Manuel Klimekb3987012013-05-29 14:47:47 +00001123 if (Line.First->Type == TT_ObjCMethodSpecifier)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001124 Line.Type = LT_ObjCMethodDecl;
Manuel Klimekb3987012013-05-29 14:47:47 +00001125 else if (Line.First->Type == TT_ObjCDecl)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001126 Line.Type = LT_ObjCDecl;
Manuel Klimekb3987012013-05-29 14:47:47 +00001127 else if (Line.First->Type == TT_ObjCProperty)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001128 Line.Type = LT_ObjCProperty;
1129
Manuel Klimekb3987012013-05-29 14:47:47 +00001130 Line.First->SpacesRequiredBefore = 1;
1131 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001132}
1133
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001134void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001135 Line.First->TotalLength =
1136 Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth;
Manuel Klimekb3987012013-05-29 14:47:47 +00001137 if (!Line.First->Next)
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001138 return;
Manuel Klimekb3987012013-05-29 14:47:47 +00001139 FormatToken *Current = Line.First->Next;
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001140 bool InFunctionDecl = Line.MightBeFunctionDecl;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001141 while (Current != NULL) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001142 if (Current->Type == TT_LineComment) {
1143 if (Current->Previous->BlockKind == BK_BracedInit &&
1144 Current->Previous->opensScope())
1145 Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1;
1146 else
1147 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
1148
1149 // If we find a trailing comment, iterate backwards to determine whether
1150 // it seems to relate to a specific parameter. If so, break before that
1151 // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
1152 // to the previous line in:
1153 // SomeFunction(a,
1154 // b, // comment
1155 // c);
1156 if (!Current->HasUnescapedNewline) {
1157 for (FormatToken *Parameter = Current->Previous; Parameter;
1158 Parameter = Parameter->Previous) {
1159 if (Parameter->isOneOf(tok::comment, tok::r_brace))
1160 break;
1161 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
1162 if (Parameter->Previous->Type != TT_CtorInitializerComma &&
1163 Parameter->HasUnescapedNewline)
1164 Parameter->MustBreakBefore = true;
1165 break;
1166 }
1167 }
1168 }
1169 } else if (Current->SpacesRequiredBefore == 0 &&
1170 spaceRequiredBefore(Line, *Current)) {
Alexander Kornienkob18c2582013-10-11 21:43:05 +00001171 Current->SpacesRequiredBefore = 1;
Stephen Hines651f13c2014-04-23 16:59:28 -07001172 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001173
Daniel Jasperebaa1712013-09-17 09:52:48 +00001174 Current->MustBreakBefore =
1175 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
1176
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001177 Current->CanBreakBefore =
1178 Current->MustBreakBefore || canBreakBefore(Line, *Current);
Daniel Jasper567dcf92013-09-05 09:29:45 +00001179 if (Current->MustBreakBefore || !Current->Children.empty() ||
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001180 Current->IsMultiline)
Manuel Klimekb3987012013-05-29 14:47:47 +00001181 Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001182 else
Daniel Jasper2a409b62013-07-08 14:34:09 +00001183 Current->TotalLength = Current->Previous->TotalLength +
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001184 Current->ColumnWidth +
Daniel Jasper2a409b62013-07-08 14:34:09 +00001185 Current->SpacesRequiredBefore;
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001186
1187 if (Current->Type == TT_CtorInitializerColon)
1188 InFunctionDecl = false;
1189
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001190 // FIXME: Only calculate this if CanBreakBefore is true once static
1191 // initializers etc. are sorted out.
1192 // FIXME: Move magic numbers to a better place.
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001193 Current->SplitPenalty = 20 * Current->BindingStrength +
1194 splitPenalty(Line, *Current, InFunctionDecl);
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001195
Manuel Klimekb3987012013-05-29 14:47:47 +00001196 Current = Current->Next;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001197 }
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001198
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001199 calculateUnbreakableTailLengths(Line);
Daniel Jasperd4a03db2013-08-22 15:00:41 +00001200 for (Current = Line.First; Current != NULL; Current = Current->Next) {
1201 if (Current->Role)
1202 Current->Role->precomputeFormattingInfos(Current);
1203 }
1204
Daniel Jasper567dcf92013-09-05 09:29:45 +00001205 DEBUG({ printDebugInfo(Line); });
1206
Daniel Jasperb77d7412013-09-06 07:54:20 +00001207 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1208 E = Line.Children.end();
Daniel Jasper567dcf92013-09-05 09:29:45 +00001209 I != E; ++I) {
1210 calculateFormattingInformation(**I);
1211 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001212}
1213
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001214void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1215 unsigned UnbreakableTailLength = 0;
Manuel Klimekb3987012013-05-29 14:47:47 +00001216 FormatToken *Current = Line.Last;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001217 while (Current != NULL) {
1218 Current->UnbreakableTailLength = UnbreakableTailLength;
1219 if (Current->CanBreakBefore ||
1220 Current->isOneOf(tok::comment, tok::string_literal)) {
1221 UnbreakableTailLength = 0;
1222 } else {
1223 UnbreakableTailLength +=
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001224 Current->ColumnWidth + Current->SpacesRequiredBefore;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001225 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001226 Current = Current->Previous;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001227 }
1228}
1229
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001230unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001231 const FormatToken &Tok,
1232 bool InFunctionDecl) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001233 const FormatToken &Left = *Tok.Previous;
1234 const FormatToken &Right = Tok;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001235
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001236 if (Left.is(tok::semi))
1237 return 0;
1238 if (Left.is(tok::comma))
1239 return 1;
Stephen Hines651f13c2014-04-23 16:59:28 -07001240 if (Right.is(tok::l_square)) {
1241 if (Style.Language == FormatStyle::LK_Proto)
1242 return 1;
1243 if (Right.Type != TT_ObjCMethodExpr)
1244 return 250;
1245 }
Daniel Jasper6561f6a2013-07-09 07:43:55 +00001246 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001247 if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001248 return 3;
Daniel Jasperc18cff32013-07-11 12:34:23 +00001249 if (Left.Type == TT_StartOfName)
1250 return 20;
Stephen Hines651f13c2014-04-23 16:59:28 -07001251 if (InFunctionDecl && Right.NestingLevel == 0)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001252 return Style.PenaltyReturnTypeOnItsOwnLine;
Daniel Jasper92495a82013-08-19 10:16:18 +00001253 return 200;
Daniel Jasper3c08a812013-02-24 18:54:32 +00001254 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001255 if (Left.is(tok::equal) && Right.is(tok::l_brace))
1256 return 150;
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001257 if (Left.Type == TT_CastRParen)
1258 return 100;
Stephen Hines651f13c2014-04-23 16:59:28 -07001259 if (Left.is(tok::coloncolon) ||
1260 (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001261 return 500;
Daniel Jasper6b119d62013-04-05 17:22:09 +00001262 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1263 return 5000;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001264
Daniel Jasper6cabab42013-02-14 08:42:54 +00001265 if (Left.Type == TT_RangeBasedForLoopColon ||
1266 Left.Type == TT_InheritanceColon)
Daniel Jasper84a1a632013-02-26 13:18:08 +00001267 return 2;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001268
Daniel Jasperd3fef0f2013-08-27 14:24:43 +00001269 if (Right.isMemberAccess()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001270 if (Left.is(tok::r_paren) && Left.MatchingParen &&
Daniel Jasper2f0a0202013-09-06 08:54:24 +00001271 Left.MatchingParen->ParameterCount > 0)
Daniel Jasper518ee342013-02-26 13:59:14 +00001272 return 20; // Should be smaller than breaking at a nested comma.
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001273 return 150;
1274 }
1275
Stephen Hines651f13c2014-04-23 16:59:28 -07001276 if (Right.Type == TT_TrailingAnnotation && Right.Next &&
1277 Right.Next->isNot(tok::l_paren)) {
1278 // Generally, breaking before a trailing annotation is bad unless it is
1279 // function-like. It seems to be especially preferable to keep standard
1280 // annotations (i.e. "const", "final" and "override") on the same line.
1281 // Use a slightly higher penalty after ")" so that annotations like
1282 // "const override" are kept together.
1283 bool is_standard_annotation = Right.is(tok::kw_const) ||
1284 Right.TokenText == "override" ||
1285 Right.TokenText == "final";
1286 return (Left.is(tok::r_paren) ? 100 : 120) +
1287 (is_standard_annotation ? 50 : 0);
1288 }
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001289
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001290 // In for-loops, prefer breaking at ',' and ';'.
Manuel Klimekb3987012013-05-29 14:47:47 +00001291 if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
Daniel Jasper7d812812013-02-21 15:00:29 +00001292 return 4;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001293
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001294 // In Objective-C method expressions, prefer breaking before "param:" over
1295 // breaking after it.
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001296 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001297 return 0;
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001298 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
Stephen Hines651f13c2014-04-23 16:59:28 -07001299 return Line.MightBeFunctionDecl ? 50 : 500;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001300
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001301 if (Left.is(tok::l_paren) && InFunctionDecl)
Daniel Jasper1407bee2013-04-11 14:29:13 +00001302 return 100;
Stephen Hines651f13c2014-04-23 16:59:28 -07001303 if (Left.is(tok::equal) && InFunctionDecl)
1304 return 110;
Daniel Jasperac3223e2013-04-10 09:49:49 +00001305 if (Left.opensScope())
Daniel Jasper47066e42013-10-25 14:29:37 +00001306 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
1307 : 19;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001308
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001309 if (Right.is(tok::lessless)) {
1310 if (Left.is(tok::string_literal)) {
Alexander Kornienko00895102013-06-05 14:09:10 +00001311 StringRef Content = Left.TokenText;
Daniel Jasper20376982013-09-29 12:02:57 +00001312 if (Content.startswith("\""))
1313 Content = Content.drop_front(1);
1314 if (Content.endswith("\""))
1315 Content = Content.drop_back(1);
1316 Content = Content.trim();
Daniel Jasperbfa1edd2013-03-14 14:00:17 +00001317 if (Content.size() > 1 &&
1318 (Content.back() == ':' || Content.back() == '='))
Daniel Jasper9637dda2013-07-15 14:33:14 +00001319 return 25;
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001320 }
Daniel Jasper0c368782013-07-15 15:04:42 +00001321 return 1; // Breaking at a << is really cheap.
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001322 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001323 if (Left.Type == TT_ConditionalExpr)
Daniel Jasper518ee342013-02-26 13:59:14 +00001324 return prec::Conditional;
Manuel Klimekb3987012013-05-29 14:47:47 +00001325 prec::Level Level = Left.getPrecedence();
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001326
1327 if (Level != prec::Unknown)
1328 return Level;
Daniel Jasper24849712013-03-01 16:48:32 +00001329
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001330 return 3;
1331}
1332
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001333bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001334 const FormatToken &Left,
1335 const FormatToken &Right) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001336 if (Style.Language == FormatStyle::LK_Proto) {
1337 if (Right.is(tok::l_paren) &&
1338 (Left.TokenText == "returns" || Left.TokenText == "option"))
1339 return true;
1340 }
1341 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
1342 Left.Tok.getObjCKeywordID() == tok::objc_property)
1343 return true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001344 if (Right.is(tok::hashhash))
1345 return Left.is(tok::hash);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001346 if (Left.isOneOf(tok::hashhash, tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001347 return Right.is(tok::hash);
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001348 if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1349 return Style.SpaceInEmptyParentheses;
1350 if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
Daniel Jasper34f3d052013-08-21 08:39:01 +00001351 return (Right.Type == TT_CastRParen ||
1352 (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen))
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001353 ? Style.SpacesInCStyleCastParentheses
1354 : Style.SpacesInParentheses;
Daniel Jasperd8ee5c12013-10-29 14:52:02 +00001355 if (Style.SpacesInAngles &&
1356 ((Left.Type == TT_TemplateOpener) != (Right.Type == TT_TemplateCloser)))
1357 return true;
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001358 if (Right.isOneOf(tok::semi, tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001359 return false;
1360 if (Right.is(tok::less) &&
1361 (Left.is(tok::kw_template) ||
1362 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1363 return true;
1364 if (Left.is(tok::arrow) || Right.is(tok::arrow))
1365 return false;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001366 if (Left.isOneOf(tok::exclaim, tok::tilde))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001367 return false;
1368 if (Left.is(tok::at) &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001369 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1370 tok::numeric_constant, tok::l_paren, tok::l_brace,
1371 tok::kw_true, tok::kw_false))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001372 return false;
1373 if (Left.is(tok::coloncolon))
1374 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001375 if (Right.is(tok::coloncolon) && Left.isNot(tok::l_brace))
Daniel Jasper78a4e612013-10-12 05:16:06 +00001376 return (Left.is(tok::less) && Style.Standard == FormatStyle::LS_Cpp03) ||
1377 !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren,
1378 tok::r_paren, tok::less);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001379 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001380 return false;
Daniel Jasperc47d7f12013-07-01 09:47:25 +00001381 if (Right.is(tok::ellipsis))
Daniel Jasperb3c887d2013-10-20 16:56:16 +00001382 return Left.Tok.isLiteral();
Manuel Klimek31e44f72013-09-04 08:20:47 +00001383 if (Left.is(tok::l_square) && Right.is(tok::amp))
1384 return false;
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001385 if (Right.Type == TT_PointerOrReference)
Manuel Klimekb3987012013-05-29 14:47:47 +00001386 return Left.Tok.isLiteral() ||
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001387 ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
1388 !Style.PointerBindsToType);
Daniel Jasper3ff4a2f2013-05-28 15:27:10 +00001389 if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
Daniel Jasper395228f2013-05-08 14:58:20 +00001390 (Left.Type != TT_PointerOrReference || Style.PointerBindsToType))
1391 return true;
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001392 if (Left.Type == TT_PointerOrReference)
Daniel Jasper3a1847e2013-07-01 09:34:09 +00001393 return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
Daniel Jasper9322aae2013-03-20 09:53:18 +00001394 ((Right.Type != TT_PointerOrReference) &&
Daniel Jasper81d2d382013-04-01 17:13:26 +00001395 Right.isNot(tok::l_paren) && Style.PointerBindsToType &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001396 Left.Previous &&
1397 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001398 if (Right.is(tok::star) && Left.is(tok::l_paren))
1399 return false;
Nico Weber051860e2013-02-10 02:08:05 +00001400 if (Left.is(tok::l_square))
Daniel Jaspera07aa662013-10-22 15:30:28 +00001401 return Left.Type == TT_ArrayInitializerLSquare &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001402 Style.SpacesInContainerLiterals && Right.isNot(tok::r_square);
Nico Weber051860e2013-02-10 02:08:05 +00001403 if (Right.is(tok::r_square))
Stephen Hines651f13c2014-04-23 16:59:28 -07001404 return Right.MatchingParen && Style.SpacesInContainerLiterals &&
Daniel Jaspera07aa662013-10-22 15:30:28 +00001405 Right.MatchingParen->Type == TT_ArrayInitializerLSquare;
Daniel Jasperec172262013-08-30 10:36:58 +00001406 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr &&
Daniel Jasper567dcf92013-09-05 09:29:45 +00001407 Right.Type != TT_LambdaLSquare && Left.isNot(tok::numeric_constant))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001408 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001409 if (Left.is(tok::colon))
1410 return Left.Type != TT_ObjCMethodExpr;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001411 if (Right.is(tok::l_paren)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001412 if (Left.is(tok::r_paren) && Left.Type == TT_AttributeParen)
Daniel Jaspere0fa4c52013-07-17 20:25:02 +00001413 return true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001414 return Line.Type == LT_ObjCDecl ||
Daniel Jasper34f3d052013-08-21 08:39:01 +00001415 Left.isOneOf(tok::kw_return, tok::kw_new, tok::kw_delete,
1416 tok::semi) ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001417 (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
1418 (Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while,
1419 tok::kw_switch, tok::kw_catch) ||
1420 Left.IsForEachMacro)) ||
1421 (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
1422 Left.isOneOf(tok::identifier, tok::kw___attribute) &&
1423 Line.Type != LT_PreprocessorDirective);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001424 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001425 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001426 return false;
1427 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
Daniel Jasper567dcf92013-09-05 09:29:45 +00001428 return !Left.Children.empty(); // No spaces in "{}".
Stephen Hines651f13c2014-04-23 16:59:28 -07001429 if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) ||
1430 (Right.is(tok::r_brace) && Right.MatchingParen &&
1431 Right.MatchingParen->BlockKind != BK_Block))
Daniel Jasperb5dc3f42013-07-16 18:22:10 +00001432 return !Style.Cpp11BracedListStyle;
Stephen Hines651f13c2014-04-23 16:59:28 -07001433 if (Left.Type == TT_BlockComment && Left.TokenText.endswith("=*/"))
1434 return false;
Daniel Jasper1bee0732013-05-23 18:05:18 +00001435 if (Right.Type == TT_UnaryOperator)
1436 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1437 (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
Daniel Jasperce933562013-05-23 21:35:49 +00001438 if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) &&
Manuel Klimek31e44f72013-09-04 08:20:47 +00001439 Right.is(tok::l_brace) && Right.getNextNonComment() &&
1440 Right.BlockKind != BK_Block)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001441 return false;
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001442 if (Left.is(tok::period) || Right.is(tok::period))
1443 return false;
Alexander Kornienkodaa07e92013-09-10 13:41:43 +00001444 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
1445 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001446 return true;
1447}
1448
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001449bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001450 const FormatToken &Tok) {
1451 if (Tok.Tok.getIdentifierInfo() && Tok.Previous->Tok.getIdentifierInfo())
Daniel Jasper2b4c9242013-02-11 08:01:18 +00001452 return true; // Never ever merge two identifiers.
Daniel Jasper1d82b1a2013-10-11 19:45:02 +00001453 if (Tok.Previous->Type == TT_ImplicitStringLiteral)
1454 return Tok.WhitespaceRange.getBegin() != Tok.WhitespaceRange.getEnd();
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001455 if (Line.Type == LT_ObjCMethodDecl) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001456 if (Tok.Previous->Type == TT_ObjCMethodSpecifier)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001457 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001458 if (Tok.Previous->is(tok::r_paren) && Tok.is(tok::identifier))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001459 // Don't space between ')' and <id>
1460 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001461 }
1462 if (Line.Type == LT_ObjCProperty &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001463 (Tok.is(tok::equal) || Tok.Previous->is(tok::equal)))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001464 return false;
1465
Daniel Jasper2ca37412013-07-09 14:36:48 +00001466 if (Tok.Type == TT_TrailingReturnArrow ||
1467 Tok.Previous->Type == TT_TrailingReturnArrow)
1468 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001469 if (Tok.Previous->is(tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001470 return true;
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001471 if (Tok.is(tok::comma))
1472 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001473 if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
1474 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001475 if (Tok.Previous->Tok.is(tok::kw_operator))
Daniel Jasper52af9442013-10-29 12:24:23 +00001476 return Tok.is(tok::coloncolon);
Daniel Jasper2b4c9242013-02-11 08:01:18 +00001477 if (Tok.Type == TT_OverloadedOperatorLParen)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001478 return false;
1479 if (Tok.is(tok::colon))
Manuel Klimekb3987012013-05-29 14:47:47 +00001480 return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Daniel Jasperab3ce592013-08-01 22:05:00 +00001481 Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001482 !Tok.Previous->is(tok::question) &&
1483 (Tok.Type != TT_DictLiteral || Style.SpacesInContainerLiterals);
Manuel Klimekb3987012013-05-29 14:47:47 +00001484 if (Tok.Previous->Type == TT_UnaryOperator ||
1485 Tok.Previous->Type == TT_CastRParen)
Stephen Hines651f13c2014-04-23 16:59:28 -07001486 return Tok.Type == TT_BinaryOperator;
Manuel Klimekb3987012013-05-29 14:47:47 +00001487 if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) {
Daniel Jasper29f123b2013-02-08 15:28:42 +00001488 return Tok.Type == TT_TemplateCloser &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001489 Tok.Previous->Type == TT_TemplateCloser &&
Daniel Jasperd8ee5c12013-10-29 14:52:02 +00001490 (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001491 }
Alexander Kornienko54a38bd2013-03-20 16:41:56 +00001492 if (Tok.isOneOf(tok::arrowstar, tok::periodstar) ||
Manuel Klimekb3987012013-05-29 14:47:47 +00001493 Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar))
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001494 return false;
Daniel Jasper9b4de852013-09-25 15:15:02 +00001495 if (!Style.SpaceBeforeAssignmentOperators &&
1496 Tok.getPrecedence() == prec::Assignment)
1497 return false;
Daniel Jasper1dc6f742013-08-07 16:29:23 +00001498 if ((Tok.Type == TT_BinaryOperator && !Tok.Previous->is(tok::l_paren)) ||
1499 Tok.Previous->Type == TT_BinaryOperator)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001500 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001501 if (Tok.Previous->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001502 return false;
Daniel Jaspera4dd9822013-08-28 08:24:04 +00001503 if (Tok.is(tok::less) && Tok.Previous->isNot(tok::l_paren) &&
1504 Line.First->is(tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001505 return true;
1506 if (Tok.Type == TT_TrailingUnaryOperator)
1507 return false;
Manuel Klimekb3987012013-05-29 14:47:47 +00001508 return spaceRequiredBetween(Line, *Tok.Previous, Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001509}
1510
Daniel Jasperebaa1712013-09-17 09:52:48 +00001511bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
1512 const FormatToken &Right) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001513 const FormatToken &Left = *Right.Previous;
Daniel Jasperebaa1712013-09-17 09:52:48 +00001514 if (Right.is(tok::comment)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001515 return Right.Previous->BlockKind != BK_BracedInit &&
1516 Right.Previous->Type != TT_CtorInitializerColon &&
1517 Right.NewlinesBefore > 0;
Daniel Jasperebaa1712013-09-17 09:52:48 +00001518 } else if (Right.Previous->isTrailingComment() ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001519 (Right.isStringLiteral() && Right.Previous->isStringLiteral())) {
Daniel Jasperebaa1712013-09-17 09:52:48 +00001520 return true;
1521 } else if (Right.Previous->IsUnterminatedLiteral) {
1522 return true;
1523 } else if (Right.is(tok::lessless) && Right.Next &&
1524 Right.Previous->is(tok::string_literal) &&
1525 Right.Next->is(tok::string_literal)) {
1526 return true;
1527 } else if (Right.Previous->ClosesTemplateDeclaration &&
1528 Right.Previous->MatchingParen &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001529 Right.Previous->MatchingParen->NestingLevel == 0 &&
Daniel Jasperebaa1712013-09-17 09:52:48 +00001530 Style.AlwaysBreakTemplateDeclarations) {
Daniel Jasperebaa1712013-09-17 09:52:48 +00001531 return true;
Stephen Hines651f13c2014-04-23 16:59:28 -07001532 } else if ((Right.Type == TT_CtorInitializerComma ||
1533 Right.Type == TT_CtorInitializerColon) &&
Daniel Jasper19ccb122013-10-08 05:11:18 +00001534 Style.BreakConstructorInitializersBeforeComma &&
1535 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
Daniel Jasperebaa1712013-09-17 09:52:48 +00001536 return true;
Daniel Jasperebaa1712013-09-17 09:52:48 +00001537 } else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001538 return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1539 Style.BreakBeforeBraces == FormatStyle::BS_GNU;
1540 } else if (Right.is(tok::string_literal) &&
1541 Right.TokenText.startswith("R\"")) {
1542 // Raw string literals are special wrt. line breaks. The author has made a
1543 // deliberate choice and might have aligned the contents of the string
1544 // literal accordingly. Thus, we try keep existing line breaks.
1545 return Right.NewlinesBefore > 0;
1546 } else if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 &&
1547 Style.Language == FormatStyle::LK_Proto) {
1548 // Don't enums onto single lines in protocol buffers.
1549 return true;
1550 } else if ((Left.is(tok::l_brace) && Left.MatchingParen &&
1551 Left.MatchingParen->Previous &&
1552 Left.MatchingParen->Previous->is(tok::comma)) ||
1553 (Right.is(tok::r_brace) && Left.is(tok::comma))) {
1554 // If the last token before a '}' is a comma, the intention is to insert a
1555 // line break after it in order to make shuffling around entries easier.
1556 return true;
Daniel Jasperebaa1712013-09-17 09:52:48 +00001557 }
1558 return false;
1559}
1560
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001561bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001562 const FormatToken &Right) {
1563 const FormatToken &Left = *Right.Previous;
Stephen Hines651f13c2014-04-23 16:59:28 -07001564 if (Left.is(tok::at))
1565 return false;
Daniel Jasper6561f6a2013-07-09 07:43:55 +00001566 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001567 return true;
Daniel Jasper1a896a52013-11-08 00:57:11 +00001568 if (Right.isTrailingComment())
1569 // We rely on MustBreakBefore being set correctly here as we should not
1570 // change the "binding" behavior of a comment.
Stephen Hines651f13c2014-04-23 16:59:28 -07001571 // The first comment in a braced lists is always interpreted as belonging to
1572 // the first list element. Otherwise, it should be placed outside of the
1573 // list.
1574 return Left.BlockKind == BK_BracedInit;
Daniel Jasper1a896a52013-11-08 00:57:11 +00001575 if (Left.is(tok::question) && Right.is(tok::colon))
1576 return false;
1577 if (Right.Type == TT_ConditionalExpr || Right.is(tok::question))
1578 return Style.BreakBeforeTernaryOperators;
1579 if (Left.Type == TT_ConditionalExpr || Left.is(tok::question))
1580 return !Style.BreakBeforeTernaryOperators;
Nico Weberf2ff8122013-05-26 05:39:26 +00001581 if (Right.is(tok::colon) &&
Daniel Jasper3c6aea72013-10-24 10:31:50 +00001582 (Right.Type == TT_DictLiteral || Right.Type == TT_ObjCMethodExpr))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001583 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001584 if (Right.Type == TT_InheritanceColon)
1585 return true;
Nico Weberf2ff8122013-05-26 05:39:26 +00001586 if (Left.is(tok::colon) &&
Daniel Jasper3c6aea72013-10-24 10:31:50 +00001587 (Left.Type == TT_DictLiteral || Left.Type == TT_ObjCMethodExpr))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001588 return true;
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001589 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001590 return true;
Daniel Jasperaa9e7b12013-08-01 13:46:58 +00001591 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
1592 return true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001593 if (Left.ClosesTemplateDeclaration)
1594 return true;
Daniel Jasper6cabab42013-02-14 08:42:54 +00001595 if (Right.Type == TT_RangeBasedForLoopColon ||
Daniel Jasperc476ea92013-08-28 07:27:35 +00001596 Right.Type == TT_OverloadedOperatorLParen ||
1597 Right.Type == TT_OverloadedOperator)
Daniel Jasper6cabab42013-02-14 08:42:54 +00001598 return false;
Daniel Jasperc194c952013-05-06 06:45:09 +00001599 if (Left.Type == TT_RangeBasedForLoopColon)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001600 return true;
Daniel Jasper7d812812013-02-21 15:00:29 +00001601 if (Right.Type == TT_RangeBasedForLoopColon)
1602 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001603 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
Daniel Jasper1a896a52013-11-08 00:57:11 +00001604 Left.Type == TT_UnaryOperator || Left.is(tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001605 return false;
1606 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
1607 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001608 if (Left.is(tok::l_paren) && Left.Type == TT_AttributeParen)
1609 return false;
1610 if (Left.is(tok::l_paren) && Left.Previous &&
1611 (Left.Previous->Type == TT_BinaryOperator ||
1612 Left.Previous->Type == TT_CastRParen))
1613 return false;
Daniel Jasper84379572013-10-30 13:54:53 +00001614 if (Right.Type == TT_ImplicitStringLiteral)
1615 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001616
Daniel Jasper567dcf92013-09-05 09:29:45 +00001617 if (Right.is(tok::r_paren) || Right.Type == TT_TemplateCloser)
1618 return false;
1619
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001620 // We only break before r_brace if there was a corresponding break before
1621 // the l_brace, which is tracked by BreakBeforeClosingBrace.
Daniel Jasper567dcf92013-09-05 09:29:45 +00001622 if (Right.is(tok::r_brace))
1623 return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001624
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001625 // Allow breaking after a trailing 'const', e.g. after a method declaration,
1626 // unless it is follow by ';', '{' or '='.
Manuel Klimekb3987012013-05-29 14:47:47 +00001627 if (Left.is(tok::kw_const) && Left.Previous != NULL &&
1628 Left.Previous->is(tok::r_paren))
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001629 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001630
Daniel Jasper8ef19a22013-03-14 09:50:46 +00001631 if (Right.is(tok::kw___attribute))
1632 return true;
1633
Daniel Jasper3a204412013-02-23 07:46:38 +00001634 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
1635 return true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001636
1637 if (Left.Type == TT_CtorInitializerComma &&
1638 Style.BreakConstructorInitializersBeforeComma)
1639 return false;
Daniel Jasper19ccb122013-10-08 05:11:18 +00001640 if (Right.Type == TT_CtorInitializerComma &&
1641 Style.BreakConstructorInitializersBeforeComma)
1642 return true;
Stephen Hines651f13c2014-04-23 16:59:28 -07001643 if (Right.Type == TT_BinaryOperator && Style.BreakBeforeBinaryOperators)
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001644 return true;
Daniel Jasper26356cc2013-09-17 08:15:46 +00001645 if (Left.is(tok::greater) && Right.is(tok::greater) &&
1646 Left.Type != TT_TemplateCloser)
1647 return false;
Daniel Jaspera07aa662013-10-22 15:30:28 +00001648 if (Left.Type == TT_ArrayInitializerLSquare)
1649 return true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001650 return (Left.isBinaryOperator() && Left.isNot(tok::lessless) &&
1651 !Style.BreakBeforeBinaryOperators) ||
Daniel Jasper6b119d62013-04-05 17:22:09 +00001652 Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
1653 tok::kw_class, tok::kw_struct) ||
Daniel Jaspera07aa662013-10-22 15:30:28 +00001654 Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon,
1655 tok::l_square, tok::at) ||
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001656 (Left.is(tok::r_paren) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001657 Right.isOneOf(tok::identifier, tok::kw_const)) ||
Daniel Jaspera07aa662013-10-22 15:30:28 +00001658 (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001659}
1660
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001661void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
1662 llvm::errs() << "AnnotatedTokens:\n";
Manuel Klimekb3987012013-05-29 14:47:47 +00001663 const FormatToken *Tok = Line.First;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001664 while (Tok) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001665 llvm::errs() << " M=" << Tok->MustBreakBefore
Daniel Jasperc7bd68f2013-07-10 14:02:49 +00001666 << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
1667 << " S=" << Tok->SpacesRequiredBefore
1668 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
Manuel Klimekae76f7f2013-10-11 21:25:45 +00001669 << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
1670 << " FakeLParens=";
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001671 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
1672 llvm::errs() << Tok->FakeLParens[i] << "/";
1673 llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
Manuel Klimekae76f7f2013-10-11 21:25:45 +00001674 if (Tok->Next == NULL)
1675 assert(Tok == Line.Last);
Manuel Klimekb3987012013-05-29 14:47:47 +00001676 Tok = Tok->Next;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001677 }
1678 llvm::errs() << "----\n";
1679}
1680
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001681} // namespace format
1682} // namespace clang