blob: 56a1dbac76d60ddbd66ca6e8fa497a8214be6134 [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:
Alexander Kornienko00895102013-06-05 14:09:10 +000032 AnnotatingParser(AnnotatedLine &Line, IdentifierInfo &Ident_in)
33 : Line(Line), CurrentToken(Line.First), KeywordVirtualFound(false),
Daniel Jasper2ca37412013-07-09 14:36:48 +000034 NameFound(false), AutoFound(false), Ident_in(Ident_in) {
Nico Weber27268772013-06-26 00:30:14 +000035 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
Daniel Jasper32d28ee2013-01-29 21:01:14 +000036 }
37
Nico Weber95e8e462013-02-12 16:17:07 +000038private:
Daniel Jasper32d28ee2013-01-29 21:01:14 +000039 bool parseAngle() {
40 if (CurrentToken == NULL)
41 return false;
Daniel Jasper923ebef2013-03-14 13:45:21 +000042 ScopedContextCreator ContextCreator(*this, tok::less, 10);
Manuel Klimekb3987012013-05-29 14:47:47 +000043 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper4e778092013-02-06 10:05:46 +000044 Contexts.back().IsExpression = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000045 while (CurrentToken != NULL) {
46 if (CurrentToken->is(tok::greater)) {
47 Left->MatchingParen = CurrentToken;
48 CurrentToken->MatchingParen = Left;
49 CurrentToken->Type = TT_TemplateCloser;
50 next();
51 return true;
52 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +000053 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
Daniel Jasper5d823e32013-05-15 13:46:48 +000054 tok::question, tok::colon))
55 return false;
Daniel Jasper0348be02013-06-01 18:56:00 +000056 // If a && or || is found and interpreted as a binary operator, this set
Daniel Jasper15f33f02013-06-03 16:16:41 +000057 // of angles is likely part of something like "a < b && c > d". If the
Daniel Jasper0348be02013-06-01 18:56:00 +000058 // angles are inside an expression, the ||/&& might also be a binary
59 // operator that was misinterpreted because we are parsing template
60 // parameters.
61 // FIXME: This is getting out of hand, write a decent parser.
Manuel Klimekb3987012013-05-29 14:47:47 +000062 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
Daniel Jasper0348be02013-06-01 18:56:00 +000063 (CurrentToken->Previous->Type == TT_BinaryOperator ||
64 Contexts[Contexts.size() - 2].IsExpression) &&
Manuel Klimekb3987012013-05-29 14:47:47 +000065 Line.First->isNot(tok::kw_template))
Daniel Jasper32d28ee2013-01-29 21:01:14 +000066 return false;
Daniel Jasper9fc56f22013-02-14 15:01:34 +000067 updateParameterCount(Left, CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000068 if (!consumeToken())
69 return false;
70 }
71 return false;
72 }
73
74 bool parseParens(bool LookForDecls = false) {
75 if (CurrentToken == NULL)
76 return false;
Daniel Jasper923ebef2013-03-14 13:45:21 +000077 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
Daniel Jasper4e778092013-02-06 10:05:46 +000078
79 // FIXME: This is a bit of a hack. Do better.
80 Contexts.back().ColonIsForRangeExpr =
81 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
82
Daniel Jasper32d28ee2013-01-29 21:01:14 +000083 bool StartsObjCMethodExpr = false;
Manuel Klimekb3987012013-05-29 14:47:47 +000084 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000085 if (CurrentToken->is(tok::caret)) {
86 // ^( starts a block.
87 Left->Type = TT_ObjCBlockLParen;
Manuel Klimekb3987012013-05-29 14:47:47 +000088 } else if (FormatToken *MaybeSel = Left->Previous) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000089 // @selector( starts a selector.
Manuel Klimekb3987012013-05-29 14:47:47 +000090 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
91 MaybeSel->Previous->is(tok::at)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000092 StartsObjCMethodExpr = true;
93 }
94 }
95
Daniel Jasperb7000ca2013-08-01 17:58:23 +000096 if (Left->Previous && Left->Previous->is(tok::kw_static_assert))
97 Contexts.back().IsExpression = true;
98
Daniel Jasper4e778092013-02-06 10:05:46 +000099 if (StartsObjCMethodExpr) {
100 Contexts.back().ColonIsObjCMethodExpr = true;
101 Left->Type = TT_ObjCMethodExpr;
102 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000103
Daniel Jasper431f5912013-05-28 08:33:00 +0000104 bool MightBeFunctionType = CurrentToken->is(tok::star);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000105 bool HasMultipleLines = false;
106 bool HasMultipleParametersOnALine = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000107 while (CurrentToken != NULL) {
108 // LookForDecls is set when "if (" has been seen. Check for
109 // 'identifier' '*' 'identifier' followed by not '=' -- this
110 // '*' has to be a binary operator but determineStarAmpUsage() will
111 // categorize it as an unary operator, so set the right type here.
Manuel Klimekb3987012013-05-29 14:47:47 +0000112 if (LookForDecls && CurrentToken->Next) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000113 FormatToken *Prev = CurrentToken->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000114 if (Prev) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000115 FormatToken *PrevPrev = Prev->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000116 FormatToken *Next = CurrentToken->Next;
117 if (PrevPrev && PrevPrev->is(tok::identifier) &&
118 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
119 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
120 Prev->Type = TT_BinaryOperator;
121 LookForDecls = false;
122 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000123 }
124 }
125
Daniel Jasper53352602013-08-12 12:16:34 +0000126 if (CurrentToken->Previous->Type == TT_PointerOrReference &&
127 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
128 tok::coloncolon))
129 MightBeFunctionType = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000130 if (CurrentToken->is(tok::r_paren)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000131 if (MightBeFunctionType && CurrentToken->Next &&
Daniel Jaspere7d3bff2013-07-16 11:37:21 +0000132 (CurrentToken->Next->is(tok::l_paren) ||
133 (CurrentToken->Next->is(tok::l_square) &&
134 !Contexts.back().IsExpression)))
Daniel Jasper431f5912013-05-28 08:33:00 +0000135 Left->Type = TT_FunctionTypeLParen;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000136 Left->MatchingParen = CurrentToken;
137 CurrentToken->MatchingParen = Left;
138
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000139 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000140 CurrentToken->Type = TT_ObjCMethodExpr;
141 if (Contexts.back().FirstObjCSelectorName != NULL) {
142 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
143 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000144 }
145 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000146
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000147 if (!HasMultipleLines)
148 Left->PackingKind = PPK_Inconclusive;
149 else if (HasMultipleParametersOnALine)
150 Left->PackingKind = PPK_BinPacked;
151 else
152 Left->PackingKind = PPK_OnePerLine;
153
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000154 next();
155 return true;
156 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000157 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000158 return false;
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000159 updateParameterCount(Left, CurrentToken);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000160 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
161 !CurrentToken->Next->HasUnescapedNewline &&
162 !CurrentToken->Next->isTrailingComment())
163 HasMultipleParametersOnALine = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000164 if (!consumeToken())
165 return false;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000166 if (CurrentToken && CurrentToken->HasUnescapedNewline)
167 HasMultipleLines = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000168 }
169 return false;
170 }
171
172 bool parseSquare() {
173 if (!CurrentToken)
174 return false;
175
Alexander Kornienkod71b15b2013-06-17 13:19:53 +0000176 // A '[' could be an index subscript (after an identifier or after
Nico Weber051860e2013-02-10 02:08:05 +0000177 // ')' or ']'), it could be the start of an Objective-C method
178 // expression, or it could the the start of an Objective-C array literal.
Manuel Klimekb3987012013-05-29 14:47:47 +0000179 FormatToken *Left = CurrentToken->Previous;
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000180 FormatToken *Parent = Left->getPreviousNonComment();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000181 bool StartsObjCMethodExpr =
Daniel Jasper6f21a982013-03-13 07:49:51 +0000182 Contexts.back().CanBeExpression &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000183 (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
184 tok::kw_return, tok::kw_throw) ||
Daniel Jasperac3223e2013-04-10 09:49:49 +0000185 Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000186 Parent->Type == TT_CastRParen ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000187 getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
Daniel Jasper923ebef2013-03-14 13:45:21 +0000188 ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
Daniel Jasper6f21a982013-03-13 07:49:51 +0000189 Contexts.back().IsExpression = true;
Nico Weber051860e2013-02-10 02:08:05 +0000190 bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000191
Daniel Jasper4e778092013-02-06 10:05:46 +0000192 if (StartsObjCMethodExpr) {
193 Contexts.back().ColonIsObjCMethodExpr = true;
194 Left->Type = TT_ObjCMethodExpr;
Nico Weber051860e2013-02-10 02:08:05 +0000195 } else if (StartsObjCArrayLiteral) {
196 Left->Type = TT_ObjCArrayLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000197 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000198
199 while (CurrentToken != NULL) {
200 if (CurrentToken->is(tok::r_square)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000201 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren)) {
Nico Webere8a97982013-02-06 06:20:11 +0000202 // An ObjC method call is rarely followed by an open parenthesis.
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000203 // FIXME: Do we incorrectly label ":" with this?
204 StartsObjCMethodExpr = false;
205 Left->Type = TT_Unknown;
206 }
Daniel Jasper01786732013-02-04 07:21:18 +0000207 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000208 CurrentToken->Type = TT_ObjCMethodExpr;
Nico Webere8a97982013-02-06 06:20:11 +0000209 // determineStarAmpUsage() thinks that '*' '[' is allocating an
210 // array of pointers, but if '[' starts a selector then '*' is a
211 // binary operator.
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +0000212 if (Parent != NULL && Parent->Type == TT_PointerOrReference)
Nico Weber4ed7f3e2013-02-06 16:54:35 +0000213 Parent->Type = TT_BinaryOperator;
Nico Weber051860e2013-02-10 02:08:05 +0000214 } else if (StartsObjCArrayLiteral) {
215 CurrentToken->Type = TT_ObjCArrayLiteral;
Daniel Jasper01786732013-02-04 07:21:18 +0000216 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000217 Left->MatchingParen = CurrentToken;
218 CurrentToken->MatchingParen = Left;
Daniel Jasper4e778092013-02-06 10:05:46 +0000219 if (Contexts.back().FirstObjCSelectorName != NULL)
220 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
221 Contexts.back().LongestObjCSelectorName;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000222 next();
223 return true;
224 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000225 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000226 return false;
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000227 updateParameterCount(Left, CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000228 if (!consumeToken())
229 return false;
230 }
231 return false;
232 }
233
234 bool parseBrace() {
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000235 if (CurrentToken != NULL) {
236 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
Manuel Klimekb3987012013-05-29 14:47:47 +0000237 FormatToken *Left = CurrentToken->Previous;
Nico Weberf2ff8122013-05-26 05:39:26 +0000238
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000239 FormatToken *Parent = Left->getPreviousNonComment();
Nico Weberf2ff8122013-05-26 05:39:26 +0000240 bool StartsObjCDictLiteral = Parent && Parent->is(tok::at);
241 if (StartsObjCDictLiteral) {
242 Contexts.back().ColonIsObjCDictLiteral = true;
243 Left->Type = TT_ObjCDictLiteral;
244 }
245
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000246 while (CurrentToken != NULL) {
247 if (CurrentToken->is(tok::r_brace)) {
Nico Weberf2ff8122013-05-26 05:39:26 +0000248 if (StartsObjCDictLiteral)
249 CurrentToken->Type = TT_ObjCDictLiteral;
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000250 Left->MatchingParen = CurrentToken;
251 CurrentToken->MatchingParen = Left;
252 next();
253 return true;
254 }
255 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
256 return false;
257 updateParameterCount(Left, CurrentToken);
258 if (!consumeToken())
259 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000260 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000261 }
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000262 // No closing "}" found, this probably starts a definition.
263 Line.StartsDefinition = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000264 return true;
265 }
Daniel Jasperc4615b72013-02-20 12:56:39 +0000266
Manuel Klimekb3987012013-05-29 14:47:47 +0000267 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000268 if (Current->is(tok::comma)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000269 ++Left->ParameterCount;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000270 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000271 Left->ParameterCount = 1;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000272 }
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000273 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000274
275 bool parseConditional() {
276 while (CurrentToken != NULL) {
277 if (CurrentToken->is(tok::colon)) {
278 CurrentToken->Type = TT_ConditionalExpr;
279 next();
280 return true;
281 }
282 if (!consumeToken())
283 return false;
284 }
285 return false;
286 }
287
288 bool parseTemplateDeclaration() {
289 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
290 CurrentToken->Type = TT_TemplateOpener;
291 next();
292 if (!parseAngle())
293 return false;
Daniel Jasper34511fb2013-02-19 17:14:38 +0000294 if (CurrentToken != NULL)
Manuel Klimekb3987012013-05-29 14:47:47 +0000295 CurrentToken->Previous->ClosesTemplateDeclaration = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000296 return true;
297 }
298 return false;
299 }
300
301 bool consumeToken() {
Manuel Klimekb3987012013-05-29 14:47:47 +0000302 FormatToken *Tok = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000303 next();
Manuel Klimekb3987012013-05-29 14:47:47 +0000304 switch (Tok->Tok.getKind()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000305 case tok::plus:
306 case tok::minus:
Manuel Klimekb3987012013-05-29 14:47:47 +0000307 if (Tok->Previous == NULL && Line.MustBeDeclaration)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000308 Tok->Type = TT_ObjCMethodSpecifier;
309 break;
310 case tok::colon:
Manuel Klimekb3987012013-05-29 14:47:47 +0000311 if (Tok->Previous == NULL)
Daniel Jaspercf6d76a2013-03-18 12:50:26 +0000312 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000313 // Colons from ?: are handled in parseConditional().
Manuel Klimekb3987012013-05-29 14:47:47 +0000314 if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000315 Tok->Type = TT_CtorInitializerColon;
Nico Weberf2ff8122013-05-26 05:39:26 +0000316 } else if (Contexts.back().ColonIsObjCDictLiteral) {
317 Tok->Type = TT_ObjCDictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000318 } else if (Contexts.back().ColonIsObjCMethodExpr ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000319 Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000320 Tok->Type = TT_ObjCMethodExpr;
Manuel Klimekb3987012013-05-29 14:47:47 +0000321 Tok->Previous->Type = TT_ObjCSelectorName;
Alexander Kornienko00895102013-06-05 14:09:10 +0000322 if (Tok->Previous->CodePointCount >
323 Contexts.back().LongestObjCSelectorName) {
324 Contexts.back().LongestObjCSelectorName =
325 Tok->Previous->CodePointCount;
326 }
Daniel Jasper4e778092013-02-06 10:05:46 +0000327 if (Contexts.back().FirstObjCSelectorName == NULL)
Manuel Klimekb3987012013-05-29 14:47:47 +0000328 Contexts.back().FirstObjCSelectorName = Tok->Previous;
Daniel Jasper4e778092013-02-06 10:05:46 +0000329 } else if (Contexts.back().ColonIsForRangeExpr) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000330 Tok->Type = TT_RangeBasedForLoopColon;
Daniel Jasper6cabab42013-02-14 08:42:54 +0000331 } else if (Contexts.size() == 1) {
332 Tok->Type = TT_InheritanceColon;
Daniel Jasper923ebef2013-03-14 13:45:21 +0000333 } else if (Contexts.back().ContextKind == tok::l_paren) {
334 Tok->Type = TT_InlineASMColon;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000335 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000336 break;
337 case tok::kw_if:
338 case tok::kw_while:
339 if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
340 next();
Nico Weber27268772013-06-26 00:30:14 +0000341 if (!parseParens(/*LookForDecls=*/true))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000342 return false;
343 }
344 break;
345 case tok::kw_for:
Daniel Jasper4e778092013-02-06 10:05:46 +0000346 Contexts.back().ColonIsForRangeExpr = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000347 next();
348 if (!parseParens())
349 return false;
350 break;
351 case tok::l_paren:
352 if (!parseParens())
353 return false;
Daniel Jasper1407bee2013-04-11 14:29:13 +0000354 if (Line.MustBeDeclaration && NameFound && !Contexts.back().IsExpression)
Daniel Jasper3c08a812013-02-24 18:54:32 +0000355 Line.MightBeFunctionDecl = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000356 break;
357 case tok::l_square:
358 if (!parseSquare())
359 return false;
360 break;
361 case tok::l_brace:
362 if (!parseBrace())
363 return false;
364 break;
365 case tok::less:
Daniel Jasper0236dd02013-07-30 22:37:19 +0000366 if (Tok->Previous && !Tok->Previous->Tok.isLiteral() && parseAngle())
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000367 Tok->Type = TT_TemplateOpener;
368 else {
369 Tok->Type = TT_BinaryOperator;
370 CurrentToken = Tok;
371 next();
372 }
373 break;
374 case tok::r_paren:
375 case tok::r_square:
376 return false;
377 case tok::r_brace:
378 // Lines can start with '}'.
Manuel Klimekb3987012013-05-29 14:47:47 +0000379 if (Tok->Previous != NULL)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000380 return false;
381 break;
382 case tok::greater:
383 Tok->Type = TT_BinaryOperator;
384 break;
385 case tok::kw_operator:
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000386 while (CurrentToken && CurrentToken->isNot(tok::l_paren)) {
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000387 if (CurrentToken->isOneOf(tok::star, tok::amp))
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000388 CurrentToken->Type = TT_PointerOrReference;
389 consumeToken();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000390 }
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000391 if (CurrentToken) {
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000392 CurrentToken->Type = TT_OverloadedOperatorLParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000393 if (CurrentToken->Previous->Type == TT_BinaryOperator)
394 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000395 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000396 break;
397 case tok::question:
398 parseConditional();
399 break;
400 case tok::kw_template:
401 parseTemplateDeclaration();
402 break;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000403 case tok::identifier:
Manuel Klimekb3987012013-05-29 14:47:47 +0000404 if (Line.First->is(tok::kw_for) &&
405 Tok->Tok.getIdentifierInfo() == &Ident_in)
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000406 Tok->Type = TT_ObjCForIn;
407 break;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000408 case tok::comma:
409 if (Contexts.back().FirstStartOfName)
410 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000411 if (Contexts.back().InCtorInitializer)
412 Tok->Type = TT_CtorInitializerComma;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000413 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000414 default:
415 break;
416 }
417 return true;
418 }
419
420 void parseIncludeDirective() {
421 next();
422 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
423 next();
424 while (CurrentToken != NULL) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000425 if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000426 CurrentToken->Type = TT_ImplicitStringLiteral;
427 next();
428 }
429 } else {
430 while (CurrentToken != NULL) {
Daniel Jasper3a204412013-02-23 07:46:38 +0000431 if (CurrentToken->is(tok::string_literal))
432 // Mark these string literals as "implicit" literals, too, so that
433 // they are not split or line-wrapped.
434 CurrentToken->Type = TT_ImplicitStringLiteral;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000435 next();
436 }
437 }
438 }
439
440 void parseWarningOrError() {
441 next();
442 // We still want to format the whitespace left of the first token of the
443 // warning or error.
444 next();
445 while (CurrentToken != NULL) {
446 CurrentToken->Type = TT_ImplicitStringLiteral;
447 next();
448 }
449 }
450
451 void parsePreprocessorDirective() {
452 next();
453 if (CurrentToken == NULL)
454 return;
455 // Hashes in the middle of a line can lead to any strange token
456 // sequence.
Manuel Klimekb3987012013-05-29 14:47:47 +0000457 if (CurrentToken->Tok.getIdentifierInfo() == NULL)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000458 return;
Manuel Klimekb3987012013-05-29 14:47:47 +0000459 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000460 case tok::pp_include:
461 case tok::pp_import:
462 parseIncludeDirective();
463 break;
464 case tok::pp_error:
465 case tok::pp_warning:
466 parseWarningOrError();
467 break;
Daniel Jasperaae7bad2013-04-23 13:54:04 +0000468 case tok::pp_if:
469 case tok::pp_elif:
470 parseLine();
471 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000472 default:
473 break;
474 }
Daniel Jasper5b7e7b02013-02-05 09:34:14 +0000475 while (CurrentToken != NULL)
476 next();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000477 }
478
Nico Weber95e8e462013-02-12 16:17:07 +0000479public:
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000480 LineType parseLine() {
481 int PeriodsAndArrows = 0;
Manuel Klimekb3987012013-05-29 14:47:47 +0000482 FormatToken *LastPeriodOrArrow = NULL;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000483 bool CanBeBuilderTypeStmt = true;
484 if (CurrentToken->is(tok::hash)) {
485 parsePreprocessorDirective();
486 return LT_PreprocessorDirective;
487 }
488 while (CurrentToken != NULL) {
489 if (CurrentToken->is(tok::kw_virtual))
490 KeywordVirtualFound = true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000491 if (CurrentToken->isOneOf(tok::period, tok::arrow)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000492 ++PeriodsAndArrows;
Daniel Jasper24849712013-03-01 16:48:32 +0000493 LastPeriodOrArrow = CurrentToken;
494 }
Manuel Klimekb3987012013-05-29 14:47:47 +0000495 FormatToken *TheToken = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000496 if (!consumeToken())
497 return LT_Invalid;
Manuel Klimekb3987012013-05-29 14:47:47 +0000498 if (TheToken->getPrecedence() > prec::Assignment &&
Daniel Jasper82282dc2013-02-18 13:52:06 +0000499 TheToken->Type == TT_BinaryOperator)
Daniel Jasper4a544e52013-02-15 20:33:06 +0000500 CanBeBuilderTypeStmt = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000501 }
502 if (KeywordVirtualFound)
503 return LT_VirtualFunctionDecl;
504
505 // Assume a builder-type call if there are 2 or more "." and "->".
Daniel Jasper24849712013-03-01 16:48:32 +0000506 if (PeriodsAndArrows >= 2 && CanBeBuilderTypeStmt) {
507 LastPeriodOrArrow->LastInChainOfCalls = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000508 return LT_BuilderTypeCall;
Daniel Jasper24849712013-03-01 16:48:32 +0000509 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000510
Manuel Klimekb3987012013-05-29 14:47:47 +0000511 if (Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000512 if (Contexts.back().FirstObjCSelectorName != NULL)
513 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
514 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000515 return LT_ObjCMethodDecl;
516 }
517
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000518 return LT_Other;
519 }
520
Nico Weber95e8e462013-02-12 16:17:07 +0000521private:
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000522 void next() {
Daniel Jasper01786732013-02-04 07:21:18 +0000523 if (CurrentToken != NULL) {
524 determineTokenType(*CurrentToken);
Daniel Jasper4e778092013-02-06 10:05:46 +0000525 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
Daniel Jasper01786732013-02-04 07:21:18 +0000526 }
527
Manuel Klimekb3987012013-05-29 14:47:47 +0000528 if (CurrentToken != NULL)
529 CurrentToken = CurrentToken->Next;
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000530
531 // Reset token type in case we have already looked at it and then recovered
532 // from an error (e.g. failure to find the matching >).
533 if (CurrentToken != NULL)
534 CurrentToken->Type = TT_Unknown;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000535 }
536
Daniel Jasper4e778092013-02-06 10:05:46 +0000537 /// \brief A struct to hold information valid in a specific context, e.g.
538 /// a pair of parenthesis.
539 struct Context {
Daniel Jasper923ebef2013-03-14 13:45:21 +0000540 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
541 bool IsExpression)
542 : ContextKind(ContextKind), BindingStrength(BindingStrength),
543 LongestObjCSelectorName(0), ColonIsForRangeExpr(false),
Nico Weberf2ff8122013-05-26 05:39:26 +0000544 ColonIsObjCDictLiteral(false), ColonIsObjCMethodExpr(false),
545 FirstObjCSelectorName(NULL), FirstStartOfName(NULL),
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000546 IsExpression(IsExpression), CanBeExpression(true),
547 InCtorInitializer(false) {}
Daniel Jasper01786732013-02-04 07:21:18 +0000548
Daniel Jasper923ebef2013-03-14 13:45:21 +0000549 tok::TokenKind ContextKind;
Daniel Jasper4e778092013-02-06 10:05:46 +0000550 unsigned BindingStrength;
551 unsigned LongestObjCSelectorName;
552 bool ColonIsForRangeExpr;
Nico Weberf2ff8122013-05-26 05:39:26 +0000553 bool ColonIsObjCDictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000554 bool ColonIsObjCMethodExpr;
Manuel Klimekb3987012013-05-29 14:47:47 +0000555 FormatToken *FirstObjCSelectorName;
556 FormatToken *FirstStartOfName;
Daniel Jasper4e778092013-02-06 10:05:46 +0000557 bool IsExpression;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000558 bool CanBeExpression;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000559 bool InCtorInitializer;
Daniel Jasper4e778092013-02-06 10:05:46 +0000560 };
561
562 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
563 /// of each instance.
564 struct ScopedContextCreator {
565 AnnotatingParser &P;
566
Daniel Jasper923ebef2013-03-14 13:45:21 +0000567 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
568 unsigned Increase)
569 : P(P) {
Daniel Jasper2a409b62013-07-08 14:34:09 +0000570 P.Contexts.push_back(Context(ContextKind,
571 P.Contexts.back().BindingStrength + Increase,
572 P.Contexts.back().IsExpression));
Daniel Jasper4e778092013-02-06 10:05:46 +0000573 }
574
575 ~ScopedContextCreator() { P.Contexts.pop_back(); }
576 };
Daniel Jasper01786732013-02-04 07:21:18 +0000577
Manuel Klimekb3987012013-05-29 14:47:47 +0000578 void determineTokenType(FormatToken &Current) {
579 if (Current.getPrecedence() == prec::Assignment &&
Daniel Jasper53352602013-08-12 12:16:34 +0000580 !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
Manuel Klimekb3987012013-05-29 14:47:47 +0000581 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000582 Contexts.back().IsExpression = true;
Manuel Klimekb3987012013-05-29 14:47:47 +0000583 for (FormatToken *Previous = Current.Previous;
Nico Weber95e8e462013-02-12 16:17:07 +0000584 Previous && Previous->isNot(tok::comma);
Manuel Klimekb3987012013-05-29 14:47:47 +0000585 Previous = Previous->Previous) {
Daniel Jasper9c65b062013-02-27 11:43:50 +0000586 if (Previous->is(tok::r_square))
587 Previous = Previous->MatchingParen;
Daniel Jasper01786732013-02-04 07:21:18 +0000588 if (Previous->Type == TT_BinaryOperator &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000589 Previous->isOneOf(tok::star, tok::amp)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000590 Previous->Type = TT_PointerOrReference;
591 }
Daniel Jasper01786732013-02-04 07:21:18 +0000592 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000593 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw) ||
Nico Weber95e8e462013-02-12 16:17:07 +0000594 (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
Daniel Jasper378d93d2013-05-13 07:14:40 +0000595 !Line.InPPDirective &&
Manuel Klimek2530fd52013-08-12 03:51:17 +0000596 (!Current.Previous ||
Daniel Jasper53352602013-08-12 12:16:34 +0000597 !Current.Previous->isOneOf(tok::kw_for, tok::kw_catch)))) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000598 Contexts.back().IsExpression = true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000599 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000600 for (FormatToken *Previous = Current.Previous;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000601 Previous && Previous->isOneOf(tok::star, tok::amp);
Manuel Klimekb3987012013-05-29 14:47:47 +0000602 Previous = Previous->Previous)
Nico Weber95e8e462013-02-12 16:17:07 +0000603 Previous->Type = TT_PointerOrReference;
Manuel Klimekb3987012013-05-29 14:47:47 +0000604 } else if (Current.Previous &&
605 Current.Previous->Type == TT_CtorInitializerColon) {
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000606 Contexts.back().IsExpression = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000607 Contexts.back().InCtorInitializer = true;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000608 } else if (Current.is(tok::kw_new)) {
609 Contexts.back().CanBeExpression = false;
Daniel Jasper16a69ef2013-05-03 14:41:24 +0000610 } else if (Current.is(tok::semi)) {
611 // This should be the condition or increment in a for-loop.
612 Contexts.back().IsExpression = true;
Nico Weber95e8e462013-02-12 16:17:07 +0000613 }
Daniel Jasper01786732013-02-04 07:21:18 +0000614
615 if (Current.Type == TT_Unknown) {
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000616 if (isStartOfName(Current)) {
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000617 Contexts.back().FirstStartOfName = &Current;
Daniel Jasper3c08a812013-02-24 18:54:32 +0000618 Current.Type = TT_StartOfName;
Daniel Jasper1407bee2013-04-11 14:29:13 +0000619 NameFound = true;
Daniel Jasper2ca37412013-07-09 14:36:48 +0000620 } else if (Current.is(tok::kw_auto)) {
621 AutoFound = true;
Daniel Jasper3262f4c2013-07-11 14:33:06 +0000622 } else if (Current.is(tok::arrow) && AutoFound &&
623 Line.MustBeDeclaration) {
Daniel Jasper2ca37412013-07-09 14:36:48 +0000624 Current.Type = TT_TrailingReturnArrow;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000625 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000626 Current.Type =
Daniel Jasperd6104f62013-07-05 13:30:40 +0000627 determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
628 Contexts.back().IsExpression);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000629 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000630 Current.Type = determinePlusMinusCaretUsage(Current);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000631 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000632 Current.Type = determineIncrementUsage(Current);
633 } else if (Current.is(tok::exclaim)) {
634 Current.Type = TT_UnaryOperator;
Daniel Jasperac3223e2013-04-10 09:49:49 +0000635 } else if (Current.isBinaryOperator()) {
Daniel Jasper01786732013-02-04 07:21:18 +0000636 Current.Type = TT_BinaryOperator;
637 } else if (Current.is(tok::comment)) {
Alexander Kornienko00895102013-06-05 14:09:10 +0000638 if (Current.TokenText.startswith("//"))
Daniel Jasper01786732013-02-04 07:21:18 +0000639 Current.Type = TT_LineComment;
640 else
641 Current.Type = TT_BlockComment;
Nico Weber37d69312013-02-13 04:13:13 +0000642 } else if (Current.is(tok::r_paren)) {
Daniel Jasperb8b42952013-05-31 16:14:28 +0000643 FormatToken *LeftOfParens = NULL;
644 if (Current.MatchingParen)
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000645 LeftOfParens = Current.MatchingParen->getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000646 bool IsCast = false;
647 bool ParensAreEmpty = Current.Previous == Current.MatchingParen;
648 bool ParensAreType = !Current.Previous ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000649 Current.Previous->Type == TT_PointerOrReference ||
Daniel Jasperb8b42952013-05-31 16:14:28 +0000650 Current.Previous->Type == TT_TemplateCloser ||
651 isSimpleTypeSpecifier(*Current.Previous);
Nico Weber37d69312013-02-13 04:13:13 +0000652 bool ParensCouldEndDecl =
Manuel Klimekb3987012013-05-29 14:47:47 +0000653 Current.Next &&
654 Current.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
Daniel Jasper6a365aa2013-03-13 17:13:53 +0000655 bool IsSizeOfOrAlignOf =
Daniel Jasperb8b42952013-05-31 16:14:28 +0000656 LeftOfParens &&
657 LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
658 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
Daniel Jasper0c368782013-07-15 15:04:42 +0000659 (Contexts.back().IsExpression ||
660 (Current.Next && Current.Next->isBinaryOperator())))
Daniel Jasperb8b42952013-05-31 16:14:28 +0000661 IsCast = true;
Daniel Jasper2a409b62013-07-08 14:34:09 +0000662 if (Current.Next && Current.Next->isNot(tok::string_literal) &&
Daniel Jasperb8b42952013-05-31 16:14:28 +0000663 (Current.Next->Tok.isLiteral() ||
664 Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
665 IsCast = true;
666 // If there is an identifier after the (), it is likely a cast, unless
667 // there is also an identifier before the ().
Daniel Jasperff1a2e52013-06-06 08:20:20 +0000668 if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL ||
669 LeftOfParens->is(tok::kw_return)) &&
Daniel Jasper526df0f2013-07-08 14:58:01 +0000670 LeftOfParens->Type != TT_OverloadedOperator &&
Nico Weber465e8612013-06-25 00:55:57 +0000671 LeftOfParens->Type != TT_TemplateCloser && Current.Next &&
672 Current.Next->is(tok::identifier))
Daniel Jasperb8b42952013-05-31 16:14:28 +0000673 IsCast = true;
674 if (IsCast && !ParensAreEmpty)
Nico Weber37d69312013-02-13 04:13:13 +0000675 Current.Type = TT_CastRParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000676 } else if (Current.is(tok::at) && Current.Next) {
677 switch (Current.Next->Tok.getObjCKeywordID()) {
Daniel Jasper01786732013-02-04 07:21:18 +0000678 case tok::objc_interface:
679 case tok::objc_implementation:
680 case tok::objc_protocol:
681 Current.Type = TT_ObjCDecl;
682 break;
683 case tok::objc_property:
684 Current.Type = TT_ObjCProperty;
685 break;
686 default:
687 break;
688 }
Daniel Jasper5ad390d2013-05-28 11:30:49 +0000689 } else if (Current.is(tok::period)) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000690 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
Daniel Jasper5ad390d2013-05-28 11:30:49 +0000691 if (PreviousNoComment &&
692 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
693 Current.Type = TT_DesignatedInitializerPeriod;
Daniel Jasper01786732013-02-04 07:21:18 +0000694 }
695 }
696 }
697
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000698 /// \brief Take a guess at whether \p Tok starts a name of a function or
699 /// variable declaration.
700 ///
701 /// This is a heuristic based on whether \p Tok is an identifier following
702 /// something that is likely a type.
703 bool isStartOfName(const FormatToken &Tok) {
704 if (Tok.isNot(tok::identifier) || Tok.Previous == NULL)
705 return false;
706
707 // Skip "const" as it does not have an influence on whether this is a name.
708 FormatToken *PreviousNotConst = Tok.Previous;
709 while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const))
710 PreviousNotConst = PreviousNotConst->Previous;
711
712 if (PreviousNotConst == NULL)
713 return false;
714
Daniel Jasper2a409b62013-07-08 14:34:09 +0000715 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
716 PreviousNotConst->Previous &&
717 PreviousNotConst->Previous->is(tok::hash);
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000718
719 return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
720 PreviousNotConst->Type == TT_PointerOrReference ||
721 PreviousNotConst->Type == TT_TemplateCloser ||
722 isSimpleTypeSpecifier(*PreviousNotConst);
723 }
724
Daniel Jasper01786732013-02-04 07:21:18 +0000725 /// \brief Return the type of the given token assuming it is * or &.
Manuel Klimekb3987012013-05-29 14:47:47 +0000726 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000727 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasper01786732013-02-04 07:21:18 +0000728 if (PrevToken == NULL)
729 return TT_UnaryOperator;
730
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000731 const FormatToken *NextToken = Tok.getNextNonComment();
Daniel Jasper01786732013-02-04 07:21:18 +0000732 if (NextToken == NULL)
733 return TT_Unknown;
734
Daniel Jasper431f5912013-05-28 08:33:00 +0000735 if (PrevToken->is(tok::coloncolon) ||
736 (PrevToken->is(tok::l_paren) && !IsExpression))
Daniel Jasper8a5d7cd2013-03-01 17:13:29 +0000737 return TT_PointerOrReference;
738
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000739 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
Daniel Jasperd3cf17b2013-03-14 10:50:25 +0000740 tok::comma, tok::semi, tok::kw_return, tok::colon,
Daniel Jasperdbef71e2013-05-07 14:17:18 +0000741 tok::equal, tok::kw_delete) ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000742 PrevToken->Type == TT_BinaryOperator ||
Daniel Jasper01786732013-02-04 07:21:18 +0000743 PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
744 return TT_UnaryOperator;
745
Nico Webere8a97982013-02-06 06:20:11 +0000746 if (NextToken->is(tok::l_square))
747 return TT_PointerOrReference;
748
Manuel Klimekb3987012013-05-29 14:47:47 +0000749 if (PrevToken->Tok.isLiteral() ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000750 PrevToken->isOneOf(tok::r_paren, tok::r_square) ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000751 NextToken->Tok.isLiteral() || NextToken->isUnaryOperator())
Daniel Jasper01786732013-02-04 07:21:18 +0000752 return TT_BinaryOperator;
753
Daniel Jasper01786732013-02-04 07:21:18 +0000754 // It is very unlikely that we are going to find a pointer or reference type
755 // definition on the RHS of an assignment.
756 if (IsExpression)
757 return TT_BinaryOperator;
758
759 return TT_PointerOrReference;
760 }
761
Manuel Klimekb3987012013-05-29 14:47:47 +0000762 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000763 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000764 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper01786732013-02-04 07:21:18 +0000765 return TT_UnaryOperator;
766
767 // Use heuristics to recognize unary operators.
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000768 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
769 tok::question, tok::colon, tok::kw_return,
770 tok::kw_case, tok::at, tok::l_brace))
Daniel Jasper01786732013-02-04 07:21:18 +0000771 return TT_UnaryOperator;
772
Nico Weberee0feec2013-02-05 16:21:00 +0000773 // There can't be two consecutive binary operators.
Daniel Jasper01786732013-02-04 07:21:18 +0000774 if (PrevToken->Type == TT_BinaryOperator)
775 return TT_UnaryOperator;
776
777 // Fall back to marking the token as binary operator.
778 return TT_BinaryOperator;
779 }
780
781 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
Manuel Klimekb3987012013-05-29 14:47:47 +0000782 TokenType determineIncrementUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000783 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000784 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper01786732013-02-04 07:21:18 +0000785 return TT_UnaryOperator;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000786 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
Daniel Jasper01786732013-02-04 07:21:18 +0000787 return TT_TrailingUnaryOperator;
788
789 return TT_UnaryOperator;
790 }
Daniel Jasper4e778092013-02-06 10:05:46 +0000791
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000792 // FIXME: This is copy&pasted from Sema. Put it in a common place and remove
793 // duplication.
794 /// \brief Determine whether the token kind starts a simple-type-specifier.
Manuel Klimekb3987012013-05-29 14:47:47 +0000795 bool isSimpleTypeSpecifier(const FormatToken &Tok) const {
796 switch (Tok.Tok.getKind()) {
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000797 case tok::kw_short:
798 case tok::kw_long:
799 case tok::kw___int64:
800 case tok::kw___int128:
801 case tok::kw_signed:
802 case tok::kw_unsigned:
803 case tok::kw_void:
804 case tok::kw_char:
805 case tok::kw_int:
806 case tok::kw_half:
807 case tok::kw_float:
808 case tok::kw_double:
809 case tok::kw_wchar_t:
810 case tok::kw_bool:
811 case tok::kw___underlying_type:
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000812 case tok::annot_typename:
813 case tok::kw_char16_t:
814 case tok::kw_char32_t:
815 case tok::kw_typeof:
816 case tok::kw_decltype:
Alexander Kornienko00895102013-06-05 14:09:10 +0000817 return true;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000818 default:
Alexander Kornienko00895102013-06-05 14:09:10 +0000819 return false;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000820 }
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000821 }
822
Daniel Jasper4e778092013-02-06 10:05:46 +0000823 SmallVector<Context, 8> Contexts;
824
Daniel Jasper4e778092013-02-06 10:05:46 +0000825 AnnotatedLine &Line;
Manuel Klimekb3987012013-05-29 14:47:47 +0000826 FormatToken *CurrentToken;
Daniel Jasper4e778092013-02-06 10:05:46 +0000827 bool KeywordVirtualFound;
Daniel Jasper1407bee2013-04-11 14:29:13 +0000828 bool NameFound;
Daniel Jasper2ca37412013-07-09 14:36:48 +0000829 bool AutoFound;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000830 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000831};
832
Daniel Jasper29f123b2013-02-08 15:28:42 +0000833/// \brief Parses binary expressions by inserting fake parenthesis based on
834/// operator precedence.
835class ExpressionParser {
836public:
Daniel Jasper9acb8b42013-06-06 09:11:58 +0000837 ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {
838 // Skip leading "}", e.g. in "} else if (...) {".
839 if (Current->is(tok::r_brace))
840 next();
841 }
Daniel Jasper29f123b2013-02-08 15:28:42 +0000842
843 /// \brief Parse expressions with the given operatore precedence.
Daniel Jasper237d4c12013-02-23 21:01:55 +0000844 void parse(int Precedence = 0) {
Daniel Jasperc01897c2013-05-31 14:56:12 +0000845 // Conditional expressions need to be parsed separately for proper nesting.
846 if (Precedence == prec::Conditional + 1) {
847 parseConditionalExpr();
848 return;
849 }
Daniel Jasper29f123b2013-02-08 15:28:42 +0000850 if (Precedence > prec::PointerToMember || Current == NULL)
851 return;
852
Manuel Klimekb3987012013-05-29 14:47:47 +0000853 FormatToken *Start = Current;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000854 bool OperatorFound = false;
855
Daniel Jasper237d4c12013-02-23 21:01:55 +0000856 while (Current) {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000857 // Consume operators with higher precedence.
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000858 parse(Precedence + 1);
Daniel Jasper29f123b2013-02-08 15:28:42 +0000859
Daniel Jasper237d4c12013-02-23 21:01:55 +0000860 int CurrentPrecedence = 0;
861 if (Current) {
862 if (Current->Type == TT_ConditionalExpr)
Daniel Jasperb8b42952013-05-31 16:14:28 +0000863 CurrentPrecedence = 1 + (int)prec::Conditional;
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000864 else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon)
Daniel Jasper237d4c12013-02-23 21:01:55 +0000865 CurrentPrecedence = 1;
866 else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
Daniel Jasperb8b42952013-05-31 16:14:28 +0000867 CurrentPrecedence = 1 + (int)Current->getPrecedence();
Daniel Jasper1eaa9972013-08-01 23:13:03 +0000868 else if (Current->Type == TT_ObjCSelectorName) {
869 CurrentPrecedence = 1 + (int)prec::Assignment;
870 if (Precedence == CurrentPrecedence)
871 Start = Current;
872 }
Daniel Jasper237d4c12013-02-23 21:01:55 +0000873 }
874
Daniel Jasper29f123b2013-02-08 15:28:42 +0000875 // At the end of the line or when an operator with higher precedence is
876 // found, insert fake parenthesis and return.
Daniel Jasperac3223e2013-04-10 09:49:49 +0000877 if (Current == NULL || Current->closesScope() ||
Daniel Jasper237d4c12013-02-23 21:01:55 +0000878 (CurrentPrecedence != 0 && CurrentPrecedence < Precedence)) {
Daniel Jasperc01897c2013-05-31 14:56:12 +0000879 if (OperatorFound)
880 addFakeParenthesis(Start, prec::Level(Precedence - 1));
Daniel Jasper29f123b2013-02-08 15:28:42 +0000881 return;
882 }
883
884 // Consume scopes: (), [], <> and {}
Daniel Jasperac3223e2013-04-10 09:49:49 +0000885 if (Current->opensScope()) {
886 while (Current && !Current->closesScope()) {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000887 next();
888 parse();
889 }
890 next();
891 } else {
892 // Operator found.
Daniel Jasper237d4c12013-02-23 21:01:55 +0000893 if (CurrentPrecedence == Precedence)
Daniel Jasper29f123b2013-02-08 15:28:42 +0000894 OperatorFound = true;
895
896 next();
897 }
898 }
899 }
900
901private:
Daniel Jasperc01897c2013-05-31 14:56:12 +0000902 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
903 Start->FakeLParens.push_back(Precedence);
904 if (Current)
905 ++Current->Previous->FakeRParens;
906 }
907
908 void parseConditionalExpr() {
909 FormatToken *Start = Current;
910 parse(prec::LogicalOr + 1);
911 if (!Current || !Current->is(tok::question))
912 return;
913 next();
914 parse(prec::LogicalOr + 1);
915 if (!Current || Current->Type != TT_ConditionalExpr)
916 return;
917 next();
918 parseConditionalExpr();
919 addFakeParenthesis(Start, prec::Conditional);
920 }
921
Daniel Jasper29f123b2013-02-08 15:28:42 +0000922 void next() {
Alexander Kornienkod71b15b2013-06-17 13:19:53 +0000923 if (Current)
924 Current = Current->Next;
925 while (Current && Current->isTrailingComment())
Manuel Klimekb3987012013-05-29 14:47:47 +0000926 Current = Current->Next;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000927 }
928
Manuel Klimekb3987012013-05-29 14:47:47 +0000929 FormatToken *Current;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000930};
931
Craig Topper14e66492013-07-01 04:03:19 +0000932} // end anonymous namespace
933
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000934void TokenAnnotator::annotate(AnnotatedLine &Line) {
Alexander Kornienko00895102013-06-05 14:09:10 +0000935 AnnotatingParser Parser(Line, Ident_in);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000936 Line.Type = Parser.parseLine();
937 if (Line.Type == LT_Invalid)
938 return;
939
Daniel Jasper29f123b2013-02-08 15:28:42 +0000940 ExpressionParser ExprParser(Line);
941 ExprParser.parse();
942
Manuel Klimekb3987012013-05-29 14:47:47 +0000943 if (Line.First->Type == TT_ObjCMethodSpecifier)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000944 Line.Type = LT_ObjCMethodDecl;
Manuel Klimekb3987012013-05-29 14:47:47 +0000945 else if (Line.First->Type == TT_ObjCDecl)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000946 Line.Type = LT_ObjCDecl;
Manuel Klimekb3987012013-05-29 14:47:47 +0000947 else if (Line.First->Type == TT_ObjCProperty)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000948 Line.Type = LT_ObjCProperty;
949
Manuel Klimekb3987012013-05-29 14:47:47 +0000950 Line.First->SpacesRequiredBefore = 1;
951 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000952}
953
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000954void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
Alexander Kornienko00895102013-06-05 14:09:10 +0000955 Line.First->TotalLength = Line.First->CodePointCount;
Manuel Klimekb3987012013-05-29 14:47:47 +0000956 if (!Line.First->Next)
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000957 return;
Manuel Klimekb3987012013-05-29 14:47:47 +0000958 FormatToken *Current = Line.First->Next;
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000959 while (Current != NULL) {
Daniel Jasper729a7432013-02-11 12:36:37 +0000960 if (Current->Type == TT_LineComment)
961 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
962 else
963 Current->SpacesRequiredBefore =
964 spaceRequiredBefore(Line, *Current) ? 1 : 0;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000965
Daniel Jasper561211d2013-07-16 20:28:33 +0000966 if (Current->is(tok::comment)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000967 Current->MustBreakBefore = Current->NewlinesBefore > 0;
968 } else if (Current->Previous->isTrailingComment() ||
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000969 (Current->is(tok::string_literal) &&
Manuel Klimekb3987012013-05-29 14:47:47 +0000970 Current->Previous->is(tok::string_literal))) {
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000971 Current->MustBreakBefore = true;
Daniel Jasper561211d2013-07-16 20:28:33 +0000972 } else if (Current->Previous->IsUnterminatedLiteral) {
973 Current->MustBreakBefore = true;
Manuel Klimekb3987012013-05-29 14:47:47 +0000974 } else if (Current->is(tok::lessless) && Current->Next &&
975 Current->Previous->is(tok::string_literal) &&
976 Current->Next->is(tok::string_literal)) {
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000977 Current->MustBreakBefore = true;
Manuel Klimekb3987012013-05-29 14:47:47 +0000978 } else if (Current->Previous->ClosesTemplateDeclaration &&
Daniel Jasperbbc87762013-05-29 12:07:31 +0000979 Style.AlwaysBreakTemplateDeclarations) {
980 Current->MustBreakBefore = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000981 } else if (Current->Type == TT_CtorInitializerComma &&
982 Style.BreakConstructorInitializersBeforeComma) {
983 Current->MustBreakBefore = true;
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000984 }
985 Current->CanBreakBefore =
986 Current->MustBreakBefore || canBreakBefore(Line, *Current);
Daniel Jasper215c57f2013-07-17 15:38:19 +0000987 if (Current->MustBreakBefore ||
988 (Current->is(tok::string_literal) &&
989 Current->TokenText.find("\\\n") != StringRef::npos))
Manuel Klimekb3987012013-05-29 14:47:47 +0000990 Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit;
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000991 else
Daniel Jasper2a409b62013-07-08 14:34:09 +0000992 Current->TotalLength = Current->Previous->TotalLength +
993 Current->CodePointCount +
994 Current->SpacesRequiredBefore;
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000995 // FIXME: Only calculate this if CanBreakBefore is true once static
996 // initializers etc. are sorted out.
997 // FIXME: Move magic numbers to a better place.
998 Current->SplitPenalty =
999 20 * Current->BindingStrength + splitPenalty(Line, *Current);
1000
Manuel Klimekb3987012013-05-29 14:47:47 +00001001 Current = Current->Next;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001002 }
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001003
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001004 calculateUnbreakableTailLengths(Line);
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001005 DEBUG({
1006 printDebugInfo(Line);
1007 });
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001008}
1009
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001010void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1011 unsigned UnbreakableTailLength = 0;
Manuel Klimekb3987012013-05-29 14:47:47 +00001012 FormatToken *Current = Line.Last;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001013 while (Current != NULL) {
1014 Current->UnbreakableTailLength = UnbreakableTailLength;
1015 if (Current->CanBreakBefore ||
1016 Current->isOneOf(tok::comment, tok::string_literal)) {
1017 UnbreakableTailLength = 0;
1018 } else {
1019 UnbreakableTailLength +=
Alexander Kornienko00895102013-06-05 14:09:10 +00001020 Current->CodePointCount + Current->SpacesRequiredBefore;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001021 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001022 Current = Current->Previous;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001023 }
1024}
1025
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001026unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001027 const FormatToken &Tok) {
1028 const FormatToken &Left = *Tok.Previous;
1029 const FormatToken &Right = Tok;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001030
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001031 if (Left.is(tok::semi))
1032 return 0;
1033 if (Left.is(tok::comma))
1034 return 1;
Daniel Jasper011c35d2013-07-12 11:19:37 +00001035 if (Right.is(tok::l_square))
1036 return 150;
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001037
Daniel Jasper6561f6a2013-07-09 07:43:55 +00001038 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001039 if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001040 return 3;
Daniel Jasperc18cff32013-07-11 12:34:23 +00001041 if (Left.Type == TT_StartOfName)
1042 return 20;
Daniel Jasper3c08a812013-02-24 18:54:32 +00001043 else if (Line.MightBeFunctionDecl && Right.BindingStrength == 1)
1044 // FIXME: Clean up hack of using BindingStrength to find top-level names.
1045 return Style.PenaltyReturnTypeOnItsOwnLine;
1046 else
Daniel Jasper1407bee2013-04-11 14:29:13 +00001047 return 200;
Daniel Jasper3c08a812013-02-24 18:54:32 +00001048 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001049 if (Left.is(tok::equal) && Right.is(tok::l_brace))
1050 return 150;
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001051 if (Left.Type == TT_CastRParen)
1052 return 100;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001053 if (Left.is(tok::coloncolon))
1054 return 500;
Daniel Jasper6b119d62013-04-05 17:22:09 +00001055 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1056 return 5000;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001057
Daniel Jasper6cabab42013-02-14 08:42:54 +00001058 if (Left.Type == TT_RangeBasedForLoopColon ||
1059 Left.Type == TT_InheritanceColon)
Daniel Jasper84a1a632013-02-26 13:18:08 +00001060 return 2;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001061
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001062 if (Right.isOneOf(tok::arrow, tok::period) &&
1063 Right.Type != TT_DesignatedInitializerPeriod) {
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001064 if (Left.isOneOf(tok::r_paren, tok::r_square) && Left.MatchingParen &&
1065 Left.MatchingParen->ParameterCount > 0)
Daniel Jasper518ee342013-02-26 13:59:14 +00001066 return 20; // Should be smaller than breaking at a nested comma.
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001067 return 150;
1068 }
1069
Daniel Jasper20a0f8c2013-07-11 21:02:56 +00001070 // Breaking before a trailing 'const' or not-function-like annotation is bad.
Daniel Jasperaa9e7b12013-08-01 13:46:58 +00001071 if (Left.is(tok::r_paren) && Line.Type != LT_ObjCProperty &&
Daniel Jasper20a0f8c2013-07-11 21:02:56 +00001072 (Right.is(tok::kw_const) || (Right.is(tok::identifier) && Right.Next &&
1073 Right.Next->isNot(tok::l_paren))))
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001074 return 150;
1075
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001076 // In for-loops, prefer breaking at ',' and ';'.
Manuel Klimekb3987012013-05-29 14:47:47 +00001077 if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
Daniel Jasper7d812812013-02-21 15:00:29 +00001078 return 4;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001079
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001080 // In Objective-C method expressions, prefer breaking before "param:" over
1081 // breaking after it.
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001082 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001083 return 0;
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001084 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001085 return 20;
1086
Daniel Jasper1407bee2013-04-11 14:29:13 +00001087 if (Left.is(tok::l_paren) && Line.MightBeFunctionDecl)
1088 return 100;
Daniel Jasperac3223e2013-04-10 09:49:49 +00001089 if (Left.opensScope())
Daniel Jasper64f09282013-03-20 13:53:11 +00001090 return Left.ParameterCount > 1 ? prec::Comma : 20;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001091
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001092 if (Right.is(tok::lessless)) {
1093 if (Left.is(tok::string_literal)) {
Alexander Kornienko00895102013-06-05 14:09:10 +00001094 StringRef Content = Left.TokenText;
Daniel Jasperbfa1edd2013-03-14 14:00:17 +00001095 Content = Content.drop_back(1).drop_front(1).trim();
1096 if (Content.size() > 1 &&
1097 (Content.back() == ':' || Content.back() == '='))
Daniel Jasper9637dda2013-07-15 14:33:14 +00001098 return 25;
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001099 }
Daniel Jasper0c368782013-07-15 15:04:42 +00001100 return 1; // Breaking at a << is really cheap.
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001101 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001102 if (Left.Type == TT_ConditionalExpr)
Daniel Jasper518ee342013-02-26 13:59:14 +00001103 return prec::Conditional;
Manuel Klimekb3987012013-05-29 14:47:47 +00001104 prec::Level Level = Left.getPrecedence();
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001105
1106 if (Level != prec::Unknown)
1107 return Level;
Daniel Jasper24849712013-03-01 16:48:32 +00001108
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001109 return 3;
1110}
1111
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001112bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001113 const FormatToken &Left,
1114 const FormatToken &Right) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001115 if (Right.is(tok::hashhash))
1116 return Left.is(tok::hash);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001117 if (Left.isOneOf(tok::hashhash, tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001118 return Right.is(tok::hash);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001119 if (Right.isOneOf(tok::r_paren, tok::semi, tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001120 return false;
1121 if (Right.is(tok::less) &&
1122 (Left.is(tok::kw_template) ||
1123 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1124 return true;
1125 if (Left.is(tok::arrow) || Right.is(tok::arrow))
1126 return false;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001127 if (Left.isOneOf(tok::exclaim, tok::tilde))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001128 return false;
1129 if (Left.is(tok::at) &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001130 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1131 tok::numeric_constant, tok::l_paren, tok::l_brace,
1132 tok::kw_true, tok::kw_false))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001133 return false;
1134 if (Left.is(tok::coloncolon))
1135 return false;
1136 if (Right.is(tok::coloncolon))
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001137 return !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren);
1138 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001139 return false;
Daniel Jasperc47d7f12013-07-01 09:47:25 +00001140 if (Right.is(tok::ellipsis))
1141 return false;
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001142 if (Right.Type == TT_PointerOrReference)
Manuel Klimekb3987012013-05-29 14:47:47 +00001143 return Left.Tok.isLiteral() ||
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001144 ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
1145 !Style.PointerBindsToType);
Daniel Jasper3ff4a2f2013-05-28 15:27:10 +00001146 if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
Daniel Jasper395228f2013-05-08 14:58:20 +00001147 (Left.Type != TT_PointerOrReference || Style.PointerBindsToType))
1148 return true;
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001149 if (Left.Type == TT_PointerOrReference)
Daniel Jasper3a1847e2013-07-01 09:34:09 +00001150 return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
Daniel Jasper9322aae2013-03-20 09:53:18 +00001151 ((Right.Type != TT_PointerOrReference) &&
Daniel Jasper81d2d382013-04-01 17:13:26 +00001152 Right.isNot(tok::l_paren) && Style.PointerBindsToType &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001153 Left.Previous &&
1154 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001155 if (Right.is(tok::star) && Left.is(tok::l_paren))
1156 return false;
Nico Weber051860e2013-02-10 02:08:05 +00001157 if (Left.is(tok::l_square))
1158 return Left.Type == TT_ObjCArrayLiteral && Right.isNot(tok::r_square);
1159 if (Right.is(tok::r_square))
1160 return Right.Type == TT_ObjCArrayLiteral;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001161 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr)
1162 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001163 if (Left.is(tok::colon))
1164 return Left.Type != TT_ObjCMethodExpr;
1165 if (Right.is(tok::colon))
Daniel Jasperab3ce592013-08-01 22:05:00 +00001166 return Right.Type != TT_ObjCMethodExpr && !Left.is(tok::question);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001167 if (Left.is(tok::l_paren))
1168 return false;
1169 if (Right.is(tok::l_paren)) {
Daniel Jaspere0fa4c52013-07-17 20:25:02 +00001170 if (Left.is(tok::r_paren) && Left.MatchingParen &&
1171 Left.MatchingParen->Previous &&
1172 Left.MatchingParen->Previous->is(tok::kw___attribute))
1173 return true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001174 return Line.Type == LT_ObjCDecl ||
1175 Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
1176 tok::kw_return, tok::kw_catch, tok::kw_new,
Daniel Jasper454cb702013-05-03 14:50:50 +00001177 tok::kw_delete, tok::semi);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001178 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001179 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001180 return false;
1181 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
Daniel Jasper2424eef2013-05-23 10:15:45 +00001182 return false; // No spaces in "{}".
1183 if (Left.is(tok::l_brace) || Right.is(tok::r_brace))
Daniel Jasperb5dc3f42013-07-16 18:22:10 +00001184 return !Style.Cpp11BracedListStyle;
Daniel Jasper1bee0732013-05-23 18:05:18 +00001185 if (Right.Type == TT_UnaryOperator)
1186 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1187 (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
Daniel Jasperce933562013-05-23 21:35:49 +00001188 if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) &&
Alexander Kornienko0bdc6432013-07-04 14:47:51 +00001189 Right.is(tok::l_brace) && Right.getNextNonComment())
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001190 return false;
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001191 if (Left.is(tok::period) || Right.is(tok::period))
1192 return false;
Nico Weber861576b2013-06-26 00:15:19 +00001193 if (Left.Type == TT_BlockComment && Left.TokenText.endswith("=*/"))
1194 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001195 return true;
1196}
1197
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001198bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001199 const FormatToken &Tok) {
1200 if (Tok.Tok.getIdentifierInfo() && Tok.Previous->Tok.getIdentifierInfo())
Daniel Jasper2b4c9242013-02-11 08:01:18 +00001201 return true; // Never ever merge two identifiers.
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001202 if (Line.Type == LT_ObjCMethodDecl) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001203 if (Tok.Previous->Type == TT_ObjCMethodSpecifier)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001204 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001205 if (Tok.Previous->is(tok::r_paren) && Tok.is(tok::identifier))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001206 // Don't space between ')' and <id>
1207 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001208 }
1209 if (Line.Type == LT_ObjCProperty &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001210 (Tok.is(tok::equal) || Tok.Previous->is(tok::equal)))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001211 return false;
1212
Daniel Jasper2ca37412013-07-09 14:36:48 +00001213 if (Tok.Type == TT_TrailingReturnArrow ||
1214 Tok.Previous->Type == TT_TrailingReturnArrow)
1215 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001216 if (Tok.Previous->is(tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001217 return true;
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001218 if (Tok.is(tok::comma))
1219 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001220 if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
1221 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001222 if (Tok.Previous->Tok.is(tok::kw_operator))
Daniel Jasper2b4c9242013-02-11 08:01:18 +00001223 return false;
1224 if (Tok.Type == TT_OverloadedOperatorLParen)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001225 return false;
1226 if (Tok.is(tok::colon))
Manuel Klimekb3987012013-05-29 14:47:47 +00001227 return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Daniel Jasperab3ce592013-08-01 22:05:00 +00001228 Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr &&
1229 !Tok.Previous->is(tok::question);
Manuel Klimekb3987012013-05-29 14:47:47 +00001230 if (Tok.Previous->Type == TT_UnaryOperator ||
1231 Tok.Previous->Type == TT_CastRParen)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001232 return false;
Manuel Klimekb3987012013-05-29 14:47:47 +00001233 if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) {
Daniel Jasper29f123b2013-02-08 15:28:42 +00001234 return Tok.Type == TT_TemplateCloser &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001235 Tok.Previous->Type == TT_TemplateCloser &&
Daniel Jasper29f123b2013-02-08 15:28:42 +00001236 Style.Standard != FormatStyle::LS_Cpp11;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001237 }
Alexander Kornienko54a38bd2013-03-20 16:41:56 +00001238 if (Tok.isOneOf(tok::arrowstar, tok::periodstar) ||
Manuel Klimekb3987012013-05-29 14:47:47 +00001239 Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar))
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001240 return false;
Daniel Jasper1dc6f742013-08-07 16:29:23 +00001241 if ((Tok.Type == TT_BinaryOperator && !Tok.Previous->is(tok::l_paren)) ||
1242 Tok.Previous->Type == TT_BinaryOperator)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001243 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001244 if (Tok.Previous->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001245 return false;
Manuel Klimekb3987012013-05-29 14:47:47 +00001246 if (Tok.is(tok::less) && Line.First->is(tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001247 return true;
1248 if (Tok.Type == TT_TrailingUnaryOperator)
1249 return false;
Manuel Klimekb3987012013-05-29 14:47:47 +00001250 return spaceRequiredBetween(Line, *Tok.Previous, Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001251}
1252
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001253bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001254 const FormatToken &Right) {
1255 const FormatToken &Left = *Right.Previous;
Daniel Jasper6561f6a2013-07-09 07:43:55 +00001256 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001257 return true;
Nico Weberf2ff8122013-05-26 05:39:26 +00001258 if (Right.is(tok::colon) &&
1259 (Right.Type == TT_ObjCDictLiteral || Right.Type == TT_ObjCMethodExpr))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001260 return false;
Nico Weberf2ff8122013-05-26 05:39:26 +00001261 if (Left.is(tok::colon) &&
1262 (Left.Type == TT_ObjCDictLiteral || Left.Type == TT_ObjCMethodExpr))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001263 return true;
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001264 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001265 return true;
Daniel Jasperaa9e7b12013-08-01 13:46:58 +00001266 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
1267 return true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001268 if (Left.ClosesTemplateDeclaration)
1269 return true;
Daniel Jasperab3ce592013-08-01 22:05:00 +00001270 if ((Right.Type == TT_ConditionalExpr &&
1271 !(Right.is(tok::colon) && Left.is(tok::question))) ||
1272 Right.is(tok::question))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001273 return true;
Daniel Jasper6cabab42013-02-14 08:42:54 +00001274 if (Right.Type == TT_RangeBasedForLoopColon ||
Daniel Jasper27b91cc2013-04-05 17:21:59 +00001275 Right.Type == TT_OverloadedOperatorLParen)
Daniel Jasper6cabab42013-02-14 08:42:54 +00001276 return false;
Daniel Jasperc194c952013-05-06 06:45:09 +00001277 if (Left.Type == TT_RangeBasedForLoopColon)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001278 return true;
Daniel Jasper7d812812013-02-21 15:00:29 +00001279 if (Right.Type == TT_RangeBasedForLoopColon)
1280 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001281 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
1282 Left.Type == TT_UnaryOperator || Left.Type == TT_ConditionalExpr ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001283 Left.isOneOf(tok::question, tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001284 return false;
1285 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
1286 return false;
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001287 if (Left.Previous) {
1288 if (Left.is(tok::l_paren) && Right.is(tok::l_paren) &&
1289 Left.Previous->is(tok::kw___attribute))
1290 return false;
Daniel Jasper2ca37412013-07-09 14:36:48 +00001291 if (Left.is(tok::l_paren) && (Left.Previous->Type == TT_BinaryOperator ||
Daniel Jasper5e2169f2013-07-18 14:46:07 +00001292 Left.Previous->Type == TT_CastRParen))
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001293 return false;
1294 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001295
Daniel Jasper65d2c382013-06-06 16:08:57 +00001296 if (Right.isTrailingComment())
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001297 // We rely on MustBreakBefore being set correctly here as we should not
1298 // change the "binding" behavior of a comment.
1299 return false;
1300
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001301 // We only break before r_brace if there was a corresponding break before
1302 // the l_brace, which is tracked by BreakBeforeClosingBrace.
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001303 if (Right.isOneOf(tok::r_brace, tok::r_paren) ||
1304 Right.Type == TT_TemplateCloser)
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001305 return false;
1306
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001307 // Allow breaking after a trailing 'const', e.g. after a method declaration,
1308 // unless it is follow by ';', '{' or '='.
Manuel Klimekb3987012013-05-29 14:47:47 +00001309 if (Left.is(tok::kw_const) && Left.Previous != NULL &&
1310 Left.Previous->is(tok::r_paren))
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001311 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001312
Daniel Jasper8ef19a22013-03-14 09:50:46 +00001313 if (Right.is(tok::kw___attribute))
1314 return true;
1315
Daniel Jasper3a204412013-02-23 07:46:38 +00001316 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
1317 return true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001318
1319 if (Left.Type == TT_CtorInitializerComma &&
1320 Style.BreakConstructorInitializersBeforeComma)
1321 return false;
1322 if (Right.isBinaryOperator() && Style.BreakBeforeBinaryOperators)
1323 return true;
1324 return (Left.isBinaryOperator() && Left.isNot(tok::lessless) &&
1325 !Style.BreakBeforeBinaryOperators) ||
Daniel Jasper6b119d62013-04-05 17:22:09 +00001326 Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
1327 tok::kw_class, tok::kw_struct) ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001328 Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon) ||
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001329 (Left.is(tok::r_paren) &&
Daniel Jaspere033e872013-05-21 09:16:31 +00001330 Right.isOneOf(tok::identifier, tok::kw_const, tok::kw___attribute)) ||
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001331 (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
Daniel Jasper011c35d2013-07-12 11:19:37 +00001332 Right.is(tok::l_square);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001333}
1334
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001335void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
1336 llvm::errs() << "AnnotatedTokens:\n";
Manuel Klimekb3987012013-05-29 14:47:47 +00001337 const FormatToken *Tok = Line.First;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001338 while (Tok) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001339 llvm::errs() << " M=" << Tok->MustBreakBefore
Daniel Jasperc7bd68f2013-07-10 14:02:49 +00001340 << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
1341 << " S=" << Tok->SpacesRequiredBefore
1342 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
1343 << " PPK=" << Tok->PackingKind << " FakeLParens=";
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001344 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
1345 llvm::errs() << Tok->FakeLParens[i] << "/";
1346 llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
Manuel Klimekb3987012013-05-29 14:47:47 +00001347 Tok = Tok->Next;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001348 }
1349 llvm::errs() << "----\n";
1350}
1351
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001352} // namespace format
1353} // namespace clang