blob: ee3ca67d27c2e582c60563337fe721b0d63849fd [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 Jasperb644dd62013-08-13 09:09:09 +000096 if (Left->Previous && Left->Previous->isOneOf(tok::kw_static_assert,
97 tok::kw_if, tok::kw_while))
Daniel Jasperb7000ca2013-08-01 17:58:23 +000098 Contexts.back().IsExpression = true;
99
Daniel Jasper4e778092013-02-06 10:05:46 +0000100 if (StartsObjCMethodExpr) {
101 Contexts.back().ColonIsObjCMethodExpr = true;
102 Left->Type = TT_ObjCMethodExpr;
103 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000104
Daniel Jasper431f5912013-05-28 08:33:00 +0000105 bool MightBeFunctionType = CurrentToken->is(tok::star);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000106 bool HasMultipleLines = false;
107 bool HasMultipleParametersOnALine = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000108 while (CurrentToken != NULL) {
109 // LookForDecls is set when "if (" has been seen. Check for
110 // 'identifier' '*' 'identifier' followed by not '=' -- this
111 // '*' has to be a binary operator but determineStarAmpUsage() will
112 // categorize it as an unary operator, so set the right type here.
Manuel Klimekb3987012013-05-29 14:47:47 +0000113 if (LookForDecls && CurrentToken->Next) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000114 FormatToken *Prev = CurrentToken->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000115 if (Prev) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000116 FormatToken *PrevPrev = Prev->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000117 FormatToken *Next = CurrentToken->Next;
118 if (PrevPrev && PrevPrev->is(tok::identifier) &&
119 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
120 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
121 Prev->Type = TT_BinaryOperator;
122 LookForDecls = false;
123 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000124 }
125 }
126
Daniel Jasper53352602013-08-12 12:16:34 +0000127 if (CurrentToken->Previous->Type == TT_PointerOrReference &&
128 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
129 tok::coloncolon))
130 MightBeFunctionType = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000131 if (CurrentToken->is(tok::r_paren)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000132 if (MightBeFunctionType && CurrentToken->Next &&
Daniel Jaspere7d3bff2013-07-16 11:37:21 +0000133 (CurrentToken->Next->is(tok::l_paren) ||
134 (CurrentToken->Next->is(tok::l_square) &&
135 !Contexts.back().IsExpression)))
Daniel Jasper431f5912013-05-28 08:33:00 +0000136 Left->Type = TT_FunctionTypeLParen;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000137 Left->MatchingParen = CurrentToken;
138 CurrentToken->MatchingParen = Left;
139
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000140 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000141 CurrentToken->Type = TT_ObjCMethodExpr;
142 if (Contexts.back().FirstObjCSelectorName != NULL) {
143 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
144 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000145 }
146 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000147
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000148 if (!HasMultipleLines)
149 Left->PackingKind = PPK_Inconclusive;
150 else if (HasMultipleParametersOnALine)
151 Left->PackingKind = PPK_BinPacked;
152 else
153 Left->PackingKind = PPK_OnePerLine;
154
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000155 next();
156 return true;
157 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000158 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000159 return false;
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000160 updateParameterCount(Left, CurrentToken);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000161 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
162 !CurrentToken->Next->HasUnescapedNewline &&
163 !CurrentToken->Next->isTrailingComment())
164 HasMultipleParametersOnALine = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000165 if (!consumeToken())
166 return false;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000167 if (CurrentToken && CurrentToken->HasUnescapedNewline)
168 HasMultipleLines = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000169 }
170 return false;
171 }
172
173 bool parseSquare() {
174 if (!CurrentToken)
175 return false;
176
Alexander Kornienkod71b15b2013-06-17 13:19:53 +0000177 // A '[' could be an index subscript (after an identifier or after
Nico Weber051860e2013-02-10 02:08:05 +0000178 // ')' or ']'), it could be the start of an Objective-C method
179 // expression, or it could the the start of an Objective-C array literal.
Manuel Klimekb3987012013-05-29 14:47:47 +0000180 FormatToken *Left = CurrentToken->Previous;
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000181 FormatToken *Parent = Left->getPreviousNonComment();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000182 bool StartsObjCMethodExpr =
Daniel Jasper6f21a982013-03-13 07:49:51 +0000183 Contexts.back().CanBeExpression &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000184 (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
185 tok::kw_return, tok::kw_throw) ||
Daniel Jasperac3223e2013-04-10 09:49:49 +0000186 Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000187 Parent->Type == TT_CastRParen ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000188 getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
Daniel Jasper923ebef2013-03-14 13:45:21 +0000189 ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
Daniel Jasper6f21a982013-03-13 07:49:51 +0000190 Contexts.back().IsExpression = true;
Nico Weber051860e2013-02-10 02:08:05 +0000191 bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000192
Daniel Jasper4e778092013-02-06 10:05:46 +0000193 if (StartsObjCMethodExpr) {
194 Contexts.back().ColonIsObjCMethodExpr = true;
195 Left->Type = TT_ObjCMethodExpr;
Nico Weber051860e2013-02-10 02:08:05 +0000196 } else if (StartsObjCArrayLiteral) {
197 Left->Type = TT_ObjCArrayLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000198 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000199
200 while (CurrentToken != NULL) {
201 if (CurrentToken->is(tok::r_square)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000202 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren)) {
Nico Webere8a97982013-02-06 06:20:11 +0000203 // An ObjC method call is rarely followed by an open parenthesis.
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000204 // FIXME: Do we incorrectly label ":" with this?
205 StartsObjCMethodExpr = false;
206 Left->Type = TT_Unknown;
207 }
Daniel Jasper01786732013-02-04 07:21:18 +0000208 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000209 CurrentToken->Type = TT_ObjCMethodExpr;
Nico Webere8a97982013-02-06 06:20:11 +0000210 // determineStarAmpUsage() thinks that '*' '[' is allocating an
211 // array of pointers, but if '[' starts a selector then '*' is a
212 // binary operator.
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +0000213 if (Parent != NULL && Parent->Type == TT_PointerOrReference)
Nico Weber4ed7f3e2013-02-06 16:54:35 +0000214 Parent->Type = TT_BinaryOperator;
Nico Weber051860e2013-02-10 02:08:05 +0000215 } else if (StartsObjCArrayLiteral) {
216 CurrentToken->Type = TT_ObjCArrayLiteral;
Daniel Jasper01786732013-02-04 07:21:18 +0000217 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000218 Left->MatchingParen = CurrentToken;
219 CurrentToken->MatchingParen = Left;
Daniel Jasper4e778092013-02-06 10:05:46 +0000220 if (Contexts.back().FirstObjCSelectorName != NULL)
221 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
222 Contexts.back().LongestObjCSelectorName;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000223 next();
224 return true;
225 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000226 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000227 return false;
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000228 updateParameterCount(Left, CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000229 if (!consumeToken())
230 return false;
231 }
232 return false;
233 }
234
235 bool parseBrace() {
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000236 if (CurrentToken != NULL) {
237 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
Manuel Klimekb3987012013-05-29 14:47:47 +0000238 FormatToken *Left = CurrentToken->Previous;
Nico Weberf2ff8122013-05-26 05:39:26 +0000239
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000240 FormatToken *Parent = Left->getPreviousNonComment();
Nico Weberf2ff8122013-05-26 05:39:26 +0000241 bool StartsObjCDictLiteral = Parent && Parent->is(tok::at);
242 if (StartsObjCDictLiteral) {
243 Contexts.back().ColonIsObjCDictLiteral = true;
244 Left->Type = TT_ObjCDictLiteral;
245 }
246
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000247 while (CurrentToken != NULL) {
248 if (CurrentToken->is(tok::r_brace)) {
Nico Weberf2ff8122013-05-26 05:39:26 +0000249 if (StartsObjCDictLiteral)
250 CurrentToken->Type = TT_ObjCDictLiteral;
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000251 Left->MatchingParen = CurrentToken;
252 CurrentToken->MatchingParen = Left;
253 next();
254 return true;
255 }
256 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
257 return false;
258 updateParameterCount(Left, CurrentToken);
259 if (!consumeToken())
260 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000261 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000262 }
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000263 // No closing "}" found, this probably starts a definition.
264 Line.StartsDefinition = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000265 return true;
266 }
Daniel Jasperc4615b72013-02-20 12:56:39 +0000267
Manuel Klimekb3987012013-05-29 14:47:47 +0000268 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000269 if (Current->is(tok::comma)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000270 ++Left->ParameterCount;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000271 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000272 Left->ParameterCount = 1;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000273 }
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000274 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000275
276 bool parseConditional() {
277 while (CurrentToken != NULL) {
278 if (CurrentToken->is(tok::colon)) {
279 CurrentToken->Type = TT_ConditionalExpr;
280 next();
281 return true;
282 }
283 if (!consumeToken())
284 return false;
285 }
286 return false;
287 }
288
289 bool parseTemplateDeclaration() {
290 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
291 CurrentToken->Type = TT_TemplateOpener;
292 next();
293 if (!parseAngle())
294 return false;
Daniel Jasper34511fb2013-02-19 17:14:38 +0000295 if (CurrentToken != NULL)
Manuel Klimekb3987012013-05-29 14:47:47 +0000296 CurrentToken->Previous->ClosesTemplateDeclaration = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000297 return true;
298 }
299 return false;
300 }
301
302 bool consumeToken() {
Manuel Klimekb3987012013-05-29 14:47:47 +0000303 FormatToken *Tok = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000304 next();
Manuel Klimekb3987012013-05-29 14:47:47 +0000305 switch (Tok->Tok.getKind()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000306 case tok::plus:
307 case tok::minus:
Manuel Klimekb3987012013-05-29 14:47:47 +0000308 if (Tok->Previous == NULL && Line.MustBeDeclaration)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000309 Tok->Type = TT_ObjCMethodSpecifier;
310 break;
311 case tok::colon:
Manuel Klimekb3987012013-05-29 14:47:47 +0000312 if (Tok->Previous == NULL)
Daniel Jaspercf6d76a2013-03-18 12:50:26 +0000313 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000314 // Colons from ?: are handled in parseConditional().
Manuel Klimekb3987012013-05-29 14:47:47 +0000315 if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000316 Tok->Type = TT_CtorInitializerColon;
Nico Weberf2ff8122013-05-26 05:39:26 +0000317 } else if (Contexts.back().ColonIsObjCDictLiteral) {
318 Tok->Type = TT_ObjCDictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000319 } else if (Contexts.back().ColonIsObjCMethodExpr ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000320 Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000321 Tok->Type = TT_ObjCMethodExpr;
Manuel Klimekb3987012013-05-29 14:47:47 +0000322 Tok->Previous->Type = TT_ObjCSelectorName;
Alexander Kornienko00895102013-06-05 14:09:10 +0000323 if (Tok->Previous->CodePointCount >
324 Contexts.back().LongestObjCSelectorName) {
325 Contexts.back().LongestObjCSelectorName =
326 Tok->Previous->CodePointCount;
327 }
Daniel Jasper4e778092013-02-06 10:05:46 +0000328 if (Contexts.back().FirstObjCSelectorName == NULL)
Manuel Klimekb3987012013-05-29 14:47:47 +0000329 Contexts.back().FirstObjCSelectorName = Tok->Previous;
Daniel Jasper4e778092013-02-06 10:05:46 +0000330 } else if (Contexts.back().ColonIsForRangeExpr) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000331 Tok->Type = TT_RangeBasedForLoopColon;
Daniel Jasper6cabab42013-02-14 08:42:54 +0000332 } else if (Contexts.size() == 1) {
333 Tok->Type = TT_InheritanceColon;
Daniel Jasper923ebef2013-03-14 13:45:21 +0000334 } else if (Contexts.back().ContextKind == tok::l_paren) {
335 Tok->Type = TT_InlineASMColon;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000336 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000337 break;
338 case tok::kw_if:
339 case tok::kw_while:
340 if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
341 next();
Nico Weber27268772013-06-26 00:30:14 +0000342 if (!parseParens(/*LookForDecls=*/true))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000343 return false;
344 }
345 break;
346 case tok::kw_for:
Daniel Jasper4e778092013-02-06 10:05:46 +0000347 Contexts.back().ColonIsForRangeExpr = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000348 next();
349 if (!parseParens())
350 return false;
351 break;
352 case tok::l_paren:
353 if (!parseParens())
354 return false;
Daniel Jasper1407bee2013-04-11 14:29:13 +0000355 if (Line.MustBeDeclaration && NameFound && !Contexts.back().IsExpression)
Daniel Jasper3c08a812013-02-24 18:54:32 +0000356 Line.MightBeFunctionDecl = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000357 break;
358 case tok::l_square:
359 if (!parseSquare())
360 return false;
361 break;
362 case tok::l_brace:
363 if (!parseBrace())
364 return false;
365 break;
366 case tok::less:
Daniel Jasper0236dd02013-07-30 22:37:19 +0000367 if (Tok->Previous && !Tok->Previous->Tok.isLiteral() && parseAngle())
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000368 Tok->Type = TT_TemplateOpener;
369 else {
370 Tok->Type = TT_BinaryOperator;
371 CurrentToken = Tok;
372 next();
373 }
374 break;
375 case tok::r_paren:
376 case tok::r_square:
377 return false;
378 case tok::r_brace:
379 // Lines can start with '}'.
Manuel Klimekb3987012013-05-29 14:47:47 +0000380 if (Tok->Previous != NULL)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000381 return false;
382 break;
383 case tok::greater:
384 Tok->Type = TT_BinaryOperator;
385 break;
386 case tok::kw_operator:
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000387 while (CurrentToken && CurrentToken->isNot(tok::l_paren)) {
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000388 if (CurrentToken->isOneOf(tok::star, tok::amp))
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000389 CurrentToken->Type = TT_PointerOrReference;
390 consumeToken();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000391 }
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000392 if (CurrentToken) {
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000393 CurrentToken->Type = TT_OverloadedOperatorLParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000394 if (CurrentToken->Previous->Type == TT_BinaryOperator)
395 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000396 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000397 break;
398 case tok::question:
399 parseConditional();
400 break;
401 case tok::kw_template:
402 parseTemplateDeclaration();
403 break;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000404 case tok::identifier:
Manuel Klimekb3987012013-05-29 14:47:47 +0000405 if (Line.First->is(tok::kw_for) &&
406 Tok->Tok.getIdentifierInfo() == &Ident_in)
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000407 Tok->Type = TT_ObjCForIn;
408 break;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000409 case tok::comma:
410 if (Contexts.back().FirstStartOfName)
411 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000412 if (Contexts.back().InCtorInitializer)
413 Tok->Type = TT_CtorInitializerComma;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000414 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000415 default:
416 break;
417 }
418 return true;
419 }
420
421 void parseIncludeDirective() {
422 next();
423 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
424 next();
425 while (CurrentToken != NULL) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000426 if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000427 CurrentToken->Type = TT_ImplicitStringLiteral;
428 next();
429 }
430 } else {
431 while (CurrentToken != NULL) {
Daniel Jasper3a204412013-02-23 07:46:38 +0000432 if (CurrentToken->is(tok::string_literal))
433 // Mark these string literals as "implicit" literals, too, so that
434 // they are not split or line-wrapped.
435 CurrentToken->Type = TT_ImplicitStringLiteral;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000436 next();
437 }
438 }
439 }
440
441 void parseWarningOrError() {
442 next();
443 // We still want to format the whitespace left of the first token of the
444 // warning or error.
445 next();
446 while (CurrentToken != NULL) {
447 CurrentToken->Type = TT_ImplicitStringLiteral;
448 next();
449 }
450 }
451
452 void parsePreprocessorDirective() {
453 next();
454 if (CurrentToken == NULL)
455 return;
456 // Hashes in the middle of a line can lead to any strange token
457 // sequence.
Manuel Klimekb3987012013-05-29 14:47:47 +0000458 if (CurrentToken->Tok.getIdentifierInfo() == NULL)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000459 return;
Manuel Klimekb3987012013-05-29 14:47:47 +0000460 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000461 case tok::pp_include:
462 case tok::pp_import:
463 parseIncludeDirective();
464 break;
465 case tok::pp_error:
466 case tok::pp_warning:
467 parseWarningOrError();
468 break;
Daniel Jasperaae7bad2013-04-23 13:54:04 +0000469 case tok::pp_if:
470 case tok::pp_elif:
471 parseLine();
472 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000473 default:
474 break;
475 }
Daniel Jasper5b7e7b02013-02-05 09:34:14 +0000476 while (CurrentToken != NULL)
477 next();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000478 }
479
Nico Weber95e8e462013-02-12 16:17:07 +0000480public:
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000481 LineType parseLine() {
482 int PeriodsAndArrows = 0;
Manuel Klimekb3987012013-05-29 14:47:47 +0000483 FormatToken *LastPeriodOrArrow = NULL;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000484 bool CanBeBuilderTypeStmt = true;
485 if (CurrentToken->is(tok::hash)) {
486 parsePreprocessorDirective();
487 return LT_PreprocessorDirective;
488 }
489 while (CurrentToken != NULL) {
490 if (CurrentToken->is(tok::kw_virtual))
491 KeywordVirtualFound = true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000492 if (CurrentToken->isOneOf(tok::period, tok::arrow)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000493 ++PeriodsAndArrows;
Daniel Jasper24849712013-03-01 16:48:32 +0000494 LastPeriodOrArrow = CurrentToken;
495 }
Manuel Klimekb3987012013-05-29 14:47:47 +0000496 FormatToken *TheToken = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000497 if (!consumeToken())
498 return LT_Invalid;
Manuel Klimekb3987012013-05-29 14:47:47 +0000499 if (TheToken->getPrecedence() > prec::Assignment &&
Daniel Jasper82282dc2013-02-18 13:52:06 +0000500 TheToken->Type == TT_BinaryOperator)
Daniel Jasper4a544e52013-02-15 20:33:06 +0000501 CanBeBuilderTypeStmt = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000502 }
503 if (KeywordVirtualFound)
504 return LT_VirtualFunctionDecl;
505
506 // Assume a builder-type call if there are 2 or more "." and "->".
Daniel Jasper24849712013-03-01 16:48:32 +0000507 if (PeriodsAndArrows >= 2 && CanBeBuilderTypeStmt) {
508 LastPeriodOrArrow->LastInChainOfCalls = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000509 return LT_BuilderTypeCall;
Daniel Jasper24849712013-03-01 16:48:32 +0000510 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000511
Manuel Klimekb3987012013-05-29 14:47:47 +0000512 if (Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000513 if (Contexts.back().FirstObjCSelectorName != NULL)
514 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
515 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000516 return LT_ObjCMethodDecl;
517 }
518
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000519 return LT_Other;
520 }
521
Nico Weber95e8e462013-02-12 16:17:07 +0000522private:
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000523 void next() {
Daniel Jasper01786732013-02-04 07:21:18 +0000524 if (CurrentToken != NULL) {
525 determineTokenType(*CurrentToken);
Daniel Jasper4e778092013-02-06 10:05:46 +0000526 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
Daniel Jasper01786732013-02-04 07:21:18 +0000527 }
528
Manuel Klimekb3987012013-05-29 14:47:47 +0000529 if (CurrentToken != NULL)
530 CurrentToken = CurrentToken->Next;
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000531
532 // Reset token type in case we have already looked at it and then recovered
533 // from an error (e.g. failure to find the matching >).
534 if (CurrentToken != NULL)
535 CurrentToken->Type = TT_Unknown;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000536 }
537
Daniel Jasper4e778092013-02-06 10:05:46 +0000538 /// \brief A struct to hold information valid in a specific context, e.g.
539 /// a pair of parenthesis.
540 struct Context {
Daniel Jasper923ebef2013-03-14 13:45:21 +0000541 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
542 bool IsExpression)
543 : ContextKind(ContextKind), BindingStrength(BindingStrength),
544 LongestObjCSelectorName(0), ColonIsForRangeExpr(false),
Nico Weberf2ff8122013-05-26 05:39:26 +0000545 ColonIsObjCDictLiteral(false), ColonIsObjCMethodExpr(false),
546 FirstObjCSelectorName(NULL), FirstStartOfName(NULL),
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000547 IsExpression(IsExpression), CanBeExpression(true),
548 InCtorInitializer(false) {}
Daniel Jasper01786732013-02-04 07:21:18 +0000549
Daniel Jasper923ebef2013-03-14 13:45:21 +0000550 tok::TokenKind ContextKind;
Daniel Jasper4e778092013-02-06 10:05:46 +0000551 unsigned BindingStrength;
552 unsigned LongestObjCSelectorName;
553 bool ColonIsForRangeExpr;
Nico Weberf2ff8122013-05-26 05:39:26 +0000554 bool ColonIsObjCDictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000555 bool ColonIsObjCMethodExpr;
Manuel Klimekb3987012013-05-29 14:47:47 +0000556 FormatToken *FirstObjCSelectorName;
557 FormatToken *FirstStartOfName;
Daniel Jasper4e778092013-02-06 10:05:46 +0000558 bool IsExpression;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000559 bool CanBeExpression;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000560 bool InCtorInitializer;
Daniel Jasper4e778092013-02-06 10:05:46 +0000561 };
562
563 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
564 /// of each instance.
565 struct ScopedContextCreator {
566 AnnotatingParser &P;
567
Daniel Jasper923ebef2013-03-14 13:45:21 +0000568 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
569 unsigned Increase)
570 : P(P) {
Daniel Jasper2a409b62013-07-08 14:34:09 +0000571 P.Contexts.push_back(Context(ContextKind,
572 P.Contexts.back().BindingStrength + Increase,
573 P.Contexts.back().IsExpression));
Daniel Jasper4e778092013-02-06 10:05:46 +0000574 }
575
576 ~ScopedContextCreator() { P.Contexts.pop_back(); }
577 };
Daniel Jasper01786732013-02-04 07:21:18 +0000578
Manuel Klimekb3987012013-05-29 14:47:47 +0000579 void determineTokenType(FormatToken &Current) {
580 if (Current.getPrecedence() == prec::Assignment &&
Daniel Jasper53352602013-08-12 12:16:34 +0000581 !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
Manuel Klimekb3987012013-05-29 14:47:47 +0000582 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000583 Contexts.back().IsExpression = true;
Manuel Klimekb3987012013-05-29 14:47:47 +0000584 for (FormatToken *Previous = Current.Previous;
Nico Weber95e8e462013-02-12 16:17:07 +0000585 Previous && Previous->isNot(tok::comma);
Manuel Klimekb3987012013-05-29 14:47:47 +0000586 Previous = Previous->Previous) {
Daniel Jasper9c65b062013-02-27 11:43:50 +0000587 if (Previous->is(tok::r_square))
588 Previous = Previous->MatchingParen;
Daniel Jasper01786732013-02-04 07:21:18 +0000589 if (Previous->Type == TT_BinaryOperator &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000590 Previous->isOneOf(tok::star, tok::amp)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000591 Previous->Type = TT_PointerOrReference;
592 }
Daniel Jasper01786732013-02-04 07:21:18 +0000593 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000594 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw) ||
Nico Weber95e8e462013-02-12 16:17:07 +0000595 (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
Daniel Jasper378d93d2013-05-13 07:14:40 +0000596 !Line.InPPDirective &&
Manuel Klimek2530fd52013-08-12 03:51:17 +0000597 (!Current.Previous ||
Daniel Jasper53352602013-08-12 12:16:34 +0000598 !Current.Previous->isOneOf(tok::kw_for, tok::kw_catch)))) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000599 Contexts.back().IsExpression = true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000600 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000601 for (FormatToken *Previous = Current.Previous;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000602 Previous && Previous->isOneOf(tok::star, tok::amp);
Manuel Klimekb3987012013-05-29 14:47:47 +0000603 Previous = Previous->Previous)
Nico Weber95e8e462013-02-12 16:17:07 +0000604 Previous->Type = TT_PointerOrReference;
Manuel Klimekb3987012013-05-29 14:47:47 +0000605 } else if (Current.Previous &&
606 Current.Previous->Type == TT_CtorInitializerColon) {
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000607 Contexts.back().IsExpression = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000608 Contexts.back().InCtorInitializer = true;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000609 } else if (Current.is(tok::kw_new)) {
610 Contexts.back().CanBeExpression = false;
Daniel Jasper16a69ef2013-05-03 14:41:24 +0000611 } else if (Current.is(tok::semi)) {
612 // This should be the condition or increment in a for-loop.
613 Contexts.back().IsExpression = true;
Nico Weber95e8e462013-02-12 16:17:07 +0000614 }
Daniel Jasper01786732013-02-04 07:21:18 +0000615
616 if (Current.Type == TT_Unknown) {
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000617 if (isStartOfName(Current)) {
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000618 Contexts.back().FirstStartOfName = &Current;
Daniel Jasper3c08a812013-02-24 18:54:32 +0000619 Current.Type = TT_StartOfName;
Daniel Jasper1407bee2013-04-11 14:29:13 +0000620 NameFound = true;
Daniel Jasper2ca37412013-07-09 14:36:48 +0000621 } else if (Current.is(tok::kw_auto)) {
622 AutoFound = true;
Daniel Jasper3262f4c2013-07-11 14:33:06 +0000623 } else if (Current.is(tok::arrow) && AutoFound &&
624 Line.MustBeDeclaration) {
Daniel Jasper2ca37412013-07-09 14:36:48 +0000625 Current.Type = TT_TrailingReturnArrow;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000626 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000627 Current.Type =
Daniel Jasperd6104f62013-07-05 13:30:40 +0000628 determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
629 Contexts.back().IsExpression);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000630 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000631 Current.Type = determinePlusMinusCaretUsage(Current);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000632 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000633 Current.Type = determineIncrementUsage(Current);
634 } else if (Current.is(tok::exclaim)) {
635 Current.Type = TT_UnaryOperator;
Daniel Jasperac3223e2013-04-10 09:49:49 +0000636 } else if (Current.isBinaryOperator()) {
Daniel Jasper01786732013-02-04 07:21:18 +0000637 Current.Type = TT_BinaryOperator;
638 } else if (Current.is(tok::comment)) {
Alexander Kornienko00895102013-06-05 14:09:10 +0000639 if (Current.TokenText.startswith("//"))
Daniel Jasper01786732013-02-04 07:21:18 +0000640 Current.Type = TT_LineComment;
641 else
642 Current.Type = TT_BlockComment;
Nico Weber37d69312013-02-13 04:13:13 +0000643 } else if (Current.is(tok::r_paren)) {
Daniel Jasperb8b42952013-05-31 16:14:28 +0000644 FormatToken *LeftOfParens = NULL;
645 if (Current.MatchingParen)
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000646 LeftOfParens = Current.MatchingParen->getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000647 bool IsCast = false;
648 bool ParensAreEmpty = Current.Previous == Current.MatchingParen;
649 bool ParensAreType = !Current.Previous ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000650 Current.Previous->Type == TT_PointerOrReference ||
Daniel Jasperb8b42952013-05-31 16:14:28 +0000651 Current.Previous->Type == TT_TemplateCloser ||
652 isSimpleTypeSpecifier(*Current.Previous);
Nico Weber37d69312013-02-13 04:13:13 +0000653 bool ParensCouldEndDecl =
Manuel Klimekb3987012013-05-29 14:47:47 +0000654 Current.Next &&
655 Current.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
Daniel Jasper6a365aa2013-03-13 17:13:53 +0000656 bool IsSizeOfOrAlignOf =
Daniel Jasperb8b42952013-05-31 16:14:28 +0000657 LeftOfParens &&
658 LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
659 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
Daniel Jasper0c368782013-07-15 15:04:42 +0000660 (Contexts.back().IsExpression ||
661 (Current.Next && Current.Next->isBinaryOperator())))
Daniel Jasperb8b42952013-05-31 16:14:28 +0000662 IsCast = true;
Daniel Jasper2a409b62013-07-08 14:34:09 +0000663 if (Current.Next && Current.Next->isNot(tok::string_literal) &&
Daniel Jasperb8b42952013-05-31 16:14:28 +0000664 (Current.Next->Tok.isLiteral() ||
665 Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
666 IsCast = true;
667 // If there is an identifier after the (), it is likely a cast, unless
668 // there is also an identifier before the ().
Daniel Jasperff1a2e52013-06-06 08:20:20 +0000669 if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL ||
670 LeftOfParens->is(tok::kw_return)) &&
Daniel Jasper526df0f2013-07-08 14:58:01 +0000671 LeftOfParens->Type != TT_OverloadedOperator &&
Nico Weber465e8612013-06-25 00:55:57 +0000672 LeftOfParens->Type != TT_TemplateCloser && Current.Next &&
673 Current.Next->is(tok::identifier))
Daniel Jasperb8b42952013-05-31 16:14:28 +0000674 IsCast = true;
675 if (IsCast && !ParensAreEmpty)
Nico Weber37d69312013-02-13 04:13:13 +0000676 Current.Type = TT_CastRParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000677 } else if (Current.is(tok::at) && Current.Next) {
678 switch (Current.Next->Tok.getObjCKeywordID()) {
Daniel Jasper01786732013-02-04 07:21:18 +0000679 case tok::objc_interface:
680 case tok::objc_implementation:
681 case tok::objc_protocol:
682 Current.Type = TT_ObjCDecl;
683 break;
684 case tok::objc_property:
685 Current.Type = TT_ObjCProperty;
686 break;
687 default:
688 break;
689 }
Daniel Jasper5ad390d2013-05-28 11:30:49 +0000690 } else if (Current.is(tok::period)) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000691 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
Daniel Jasper5ad390d2013-05-28 11:30:49 +0000692 if (PreviousNoComment &&
693 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
694 Current.Type = TT_DesignatedInitializerPeriod;
Daniel Jasper01786732013-02-04 07:21:18 +0000695 }
696 }
697 }
698
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000699 /// \brief Take a guess at whether \p Tok starts a name of a function or
700 /// variable declaration.
701 ///
702 /// This is a heuristic based on whether \p Tok is an identifier following
703 /// something that is likely a type.
704 bool isStartOfName(const FormatToken &Tok) {
705 if (Tok.isNot(tok::identifier) || Tok.Previous == NULL)
706 return false;
707
708 // Skip "const" as it does not have an influence on whether this is a name.
709 FormatToken *PreviousNotConst = Tok.Previous;
710 while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const))
711 PreviousNotConst = PreviousNotConst->Previous;
712
713 if (PreviousNotConst == NULL)
714 return false;
715
Daniel Jasper2a409b62013-07-08 14:34:09 +0000716 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
717 PreviousNotConst->Previous &&
718 PreviousNotConst->Previous->is(tok::hash);
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000719
Daniel Jasper92495a82013-08-19 10:16:18 +0000720 if (PreviousNotConst->Type == TT_TemplateCloser)
721 return PreviousNotConst && PreviousNotConst->MatchingParen &&
722 PreviousNotConst->MatchingParen->Previous &&
723 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
724
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000725 return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
726 PreviousNotConst->Type == TT_PointerOrReference ||
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000727 isSimpleTypeSpecifier(*PreviousNotConst);
728 }
729
Daniel Jasper01786732013-02-04 07:21:18 +0000730 /// \brief Return the type of the given token assuming it is * or &.
Manuel Klimekb3987012013-05-29 14:47:47 +0000731 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000732 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasper01786732013-02-04 07:21:18 +0000733 if (PrevToken == NULL)
734 return TT_UnaryOperator;
735
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000736 const FormatToken *NextToken = Tok.getNextNonComment();
Daniel Jasper01786732013-02-04 07:21:18 +0000737 if (NextToken == NULL)
738 return TT_Unknown;
739
Daniel Jasper431f5912013-05-28 08:33:00 +0000740 if (PrevToken->is(tok::coloncolon) ||
741 (PrevToken->is(tok::l_paren) && !IsExpression))
Daniel Jasper8a5d7cd2013-03-01 17:13:29 +0000742 return TT_PointerOrReference;
743
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000744 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
Daniel Jasperd3cf17b2013-03-14 10:50:25 +0000745 tok::comma, tok::semi, tok::kw_return, tok::colon,
Daniel Jasperdbef71e2013-05-07 14:17:18 +0000746 tok::equal, tok::kw_delete) ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000747 PrevToken->Type == TT_BinaryOperator ||
Daniel Jasper01786732013-02-04 07:21:18 +0000748 PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
749 return TT_UnaryOperator;
750
Nico Webere8a97982013-02-06 06:20:11 +0000751 if (NextToken->is(tok::l_square))
752 return TT_PointerOrReference;
753
Manuel Klimekb3987012013-05-29 14:47:47 +0000754 if (PrevToken->Tok.isLiteral() ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000755 PrevToken->isOneOf(tok::r_paren, tok::r_square) ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000756 NextToken->Tok.isLiteral() || NextToken->isUnaryOperator())
Daniel Jasper01786732013-02-04 07:21:18 +0000757 return TT_BinaryOperator;
758
Daniel Jasper01786732013-02-04 07:21:18 +0000759 // It is very unlikely that we are going to find a pointer or reference type
760 // definition on the RHS of an assignment.
761 if (IsExpression)
762 return TT_BinaryOperator;
763
764 return TT_PointerOrReference;
765 }
766
Manuel Klimekb3987012013-05-29 14:47:47 +0000767 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000768 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000769 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper01786732013-02-04 07:21:18 +0000770 return TT_UnaryOperator;
771
772 // Use heuristics to recognize unary operators.
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000773 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
774 tok::question, tok::colon, tok::kw_return,
775 tok::kw_case, tok::at, tok::l_brace))
Daniel Jasper01786732013-02-04 07:21:18 +0000776 return TT_UnaryOperator;
777
Nico Weberee0feec2013-02-05 16:21:00 +0000778 // There can't be two consecutive binary operators.
Daniel Jasper01786732013-02-04 07:21:18 +0000779 if (PrevToken->Type == TT_BinaryOperator)
780 return TT_UnaryOperator;
781
782 // Fall back to marking the token as binary operator.
783 return TT_BinaryOperator;
784 }
785
786 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
Manuel Klimekb3987012013-05-29 14:47:47 +0000787 TokenType determineIncrementUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000788 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000789 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper01786732013-02-04 07:21:18 +0000790 return TT_UnaryOperator;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000791 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
Daniel Jasper01786732013-02-04 07:21:18 +0000792 return TT_TrailingUnaryOperator;
793
794 return TT_UnaryOperator;
795 }
Daniel Jasper4e778092013-02-06 10:05:46 +0000796
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000797 // FIXME: This is copy&pasted from Sema. Put it in a common place and remove
798 // duplication.
799 /// \brief Determine whether the token kind starts a simple-type-specifier.
Manuel Klimekb3987012013-05-29 14:47:47 +0000800 bool isSimpleTypeSpecifier(const FormatToken &Tok) const {
801 switch (Tok.Tok.getKind()) {
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000802 case tok::kw_short:
803 case tok::kw_long:
804 case tok::kw___int64:
805 case tok::kw___int128:
806 case tok::kw_signed:
807 case tok::kw_unsigned:
808 case tok::kw_void:
809 case tok::kw_char:
810 case tok::kw_int:
811 case tok::kw_half:
812 case tok::kw_float:
813 case tok::kw_double:
814 case tok::kw_wchar_t:
815 case tok::kw_bool:
816 case tok::kw___underlying_type:
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000817 case tok::annot_typename:
818 case tok::kw_char16_t:
819 case tok::kw_char32_t:
820 case tok::kw_typeof:
821 case tok::kw_decltype:
Alexander Kornienko00895102013-06-05 14:09:10 +0000822 return true;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000823 default:
Alexander Kornienko00895102013-06-05 14:09:10 +0000824 return false;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000825 }
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000826 }
827
Daniel Jasper4e778092013-02-06 10:05:46 +0000828 SmallVector<Context, 8> Contexts;
829
Daniel Jasper4e778092013-02-06 10:05:46 +0000830 AnnotatedLine &Line;
Manuel Klimekb3987012013-05-29 14:47:47 +0000831 FormatToken *CurrentToken;
Daniel Jasper4e778092013-02-06 10:05:46 +0000832 bool KeywordVirtualFound;
Daniel Jasper1407bee2013-04-11 14:29:13 +0000833 bool NameFound;
Daniel Jasper2ca37412013-07-09 14:36:48 +0000834 bool AutoFound;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000835 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000836};
837
Daniel Jasper29f123b2013-02-08 15:28:42 +0000838/// \brief Parses binary expressions by inserting fake parenthesis based on
839/// operator precedence.
840class ExpressionParser {
841public:
Daniel Jasper9acb8b42013-06-06 09:11:58 +0000842 ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {
843 // Skip leading "}", e.g. in "} else if (...) {".
844 if (Current->is(tok::r_brace))
845 next();
846 }
Daniel Jasper29f123b2013-02-08 15:28:42 +0000847
848 /// \brief Parse expressions with the given operatore precedence.
Daniel Jasper237d4c12013-02-23 21:01:55 +0000849 void parse(int Precedence = 0) {
Daniel Jasperc01897c2013-05-31 14:56:12 +0000850 // Conditional expressions need to be parsed separately for proper nesting.
851 if (Precedence == prec::Conditional + 1) {
852 parseConditionalExpr();
853 return;
854 }
Daniel Jasper29f123b2013-02-08 15:28:42 +0000855 if (Precedence > prec::PointerToMember || Current == NULL)
856 return;
857
Manuel Klimekb3987012013-05-29 14:47:47 +0000858 FormatToken *Start = Current;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000859 bool OperatorFound = false;
860
Daniel Jasper237d4c12013-02-23 21:01:55 +0000861 while (Current) {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000862 // Consume operators with higher precedence.
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000863 parse(Precedence + 1);
Daniel Jasper29f123b2013-02-08 15:28:42 +0000864
Daniel Jasper237d4c12013-02-23 21:01:55 +0000865 int CurrentPrecedence = 0;
866 if (Current) {
867 if (Current->Type == TT_ConditionalExpr)
Daniel Jasperb8b42952013-05-31 16:14:28 +0000868 CurrentPrecedence = 1 + (int)prec::Conditional;
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000869 else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon)
Daniel Jasper237d4c12013-02-23 21:01:55 +0000870 CurrentPrecedence = 1;
871 else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
Daniel Jasperb8b42952013-05-31 16:14:28 +0000872 CurrentPrecedence = 1 + (int)Current->getPrecedence();
Daniel Jasper1eaa9972013-08-01 23:13:03 +0000873 else if (Current->Type == TT_ObjCSelectorName) {
874 CurrentPrecedence = 1 + (int)prec::Assignment;
875 if (Precedence == CurrentPrecedence)
876 Start = Current;
877 }
Daniel Jasper237d4c12013-02-23 21:01:55 +0000878 }
879
Daniel Jasper29f123b2013-02-08 15:28:42 +0000880 // At the end of the line or when an operator with higher precedence is
881 // found, insert fake parenthesis and return.
Daniel Jasperac3223e2013-04-10 09:49:49 +0000882 if (Current == NULL || Current->closesScope() ||
Daniel Jasper237d4c12013-02-23 21:01:55 +0000883 (CurrentPrecedence != 0 && CurrentPrecedence < Precedence)) {
Daniel Jasperc01897c2013-05-31 14:56:12 +0000884 if (OperatorFound)
885 addFakeParenthesis(Start, prec::Level(Precedence - 1));
Daniel Jasper29f123b2013-02-08 15:28:42 +0000886 return;
887 }
888
889 // Consume scopes: (), [], <> and {}
Daniel Jasperac3223e2013-04-10 09:49:49 +0000890 if (Current->opensScope()) {
891 while (Current && !Current->closesScope()) {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000892 next();
893 parse();
894 }
895 next();
896 } else {
897 // Operator found.
Daniel Jasper237d4c12013-02-23 21:01:55 +0000898 if (CurrentPrecedence == Precedence)
Daniel Jasper29f123b2013-02-08 15:28:42 +0000899 OperatorFound = true;
900
901 next();
902 }
903 }
904 }
905
906private:
Daniel Jasperc01897c2013-05-31 14:56:12 +0000907 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
908 Start->FakeLParens.push_back(Precedence);
909 if (Current)
910 ++Current->Previous->FakeRParens;
911 }
912
913 void parseConditionalExpr() {
914 FormatToken *Start = Current;
915 parse(prec::LogicalOr + 1);
916 if (!Current || !Current->is(tok::question))
917 return;
918 next();
919 parse(prec::LogicalOr + 1);
920 if (!Current || Current->Type != TT_ConditionalExpr)
921 return;
922 next();
923 parseConditionalExpr();
924 addFakeParenthesis(Start, prec::Conditional);
925 }
926
Daniel Jasper29f123b2013-02-08 15:28:42 +0000927 void next() {
Alexander Kornienkod71b15b2013-06-17 13:19:53 +0000928 if (Current)
929 Current = Current->Next;
930 while (Current && Current->isTrailingComment())
Manuel Klimekb3987012013-05-29 14:47:47 +0000931 Current = Current->Next;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000932 }
933
Manuel Klimekb3987012013-05-29 14:47:47 +0000934 FormatToken *Current;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000935};
936
Craig Topper14e66492013-07-01 04:03:19 +0000937} // end anonymous namespace
938
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000939void TokenAnnotator::annotate(AnnotatedLine &Line) {
Alexander Kornienko00895102013-06-05 14:09:10 +0000940 AnnotatingParser Parser(Line, Ident_in);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000941 Line.Type = Parser.parseLine();
942 if (Line.Type == LT_Invalid)
943 return;
944
Daniel Jasper29f123b2013-02-08 15:28:42 +0000945 ExpressionParser ExprParser(Line);
946 ExprParser.parse();
947
Manuel Klimekb3987012013-05-29 14:47:47 +0000948 if (Line.First->Type == TT_ObjCMethodSpecifier)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000949 Line.Type = LT_ObjCMethodDecl;
Manuel Klimekb3987012013-05-29 14:47:47 +0000950 else if (Line.First->Type == TT_ObjCDecl)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000951 Line.Type = LT_ObjCDecl;
Manuel Klimekb3987012013-05-29 14:47:47 +0000952 else if (Line.First->Type == TT_ObjCProperty)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000953 Line.Type = LT_ObjCProperty;
954
Manuel Klimekb3987012013-05-29 14:47:47 +0000955 Line.First->SpacesRequiredBefore = 1;
956 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000957}
958
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000959void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
Alexander Kornienko00895102013-06-05 14:09:10 +0000960 Line.First->TotalLength = Line.First->CodePointCount;
Manuel Klimekb3987012013-05-29 14:47:47 +0000961 if (!Line.First->Next)
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000962 return;
Manuel Klimekb3987012013-05-29 14:47:47 +0000963 FormatToken *Current = Line.First->Next;
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000964 while (Current != NULL) {
Daniel Jasper729a7432013-02-11 12:36:37 +0000965 if (Current->Type == TT_LineComment)
966 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
967 else
968 Current->SpacesRequiredBefore =
969 spaceRequiredBefore(Line, *Current) ? 1 : 0;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000970
Daniel Jasper561211d2013-07-16 20:28:33 +0000971 if (Current->is(tok::comment)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000972 Current->MustBreakBefore = Current->NewlinesBefore > 0;
973 } else if (Current->Previous->isTrailingComment() ||
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000974 (Current->is(tok::string_literal) &&
Manuel Klimekb3987012013-05-29 14:47:47 +0000975 Current->Previous->is(tok::string_literal))) {
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000976 Current->MustBreakBefore = true;
Daniel Jasper561211d2013-07-16 20:28:33 +0000977 } else if (Current->Previous->IsUnterminatedLiteral) {
978 Current->MustBreakBefore = true;
Manuel Klimekb3987012013-05-29 14:47:47 +0000979 } else if (Current->is(tok::lessless) && Current->Next &&
980 Current->Previous->is(tok::string_literal) &&
981 Current->Next->is(tok::string_literal)) {
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000982 Current->MustBreakBefore = true;
Manuel Klimekb3987012013-05-29 14:47:47 +0000983 } else if (Current->Previous->ClosesTemplateDeclaration &&
Daniel Jasperbbc87762013-05-29 12:07:31 +0000984 Style.AlwaysBreakTemplateDeclarations) {
985 Current->MustBreakBefore = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000986 } else if (Current->Type == TT_CtorInitializerComma &&
987 Style.BreakConstructorInitializersBeforeComma) {
988 Current->MustBreakBefore = true;
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000989 }
990 Current->CanBreakBefore =
991 Current->MustBreakBefore || canBreakBefore(Line, *Current);
Daniel Jasper215c57f2013-07-17 15:38:19 +0000992 if (Current->MustBreakBefore ||
993 (Current->is(tok::string_literal) &&
994 Current->TokenText.find("\\\n") != StringRef::npos))
Manuel Klimekb3987012013-05-29 14:47:47 +0000995 Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit;
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000996 else
Daniel Jasper2a409b62013-07-08 14:34:09 +0000997 Current->TotalLength = Current->Previous->TotalLength +
998 Current->CodePointCount +
999 Current->SpacesRequiredBefore;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001000 // FIXME: Only calculate this if CanBreakBefore is true once static
1001 // initializers etc. are sorted out.
1002 // FIXME: Move magic numbers to a better place.
1003 Current->SplitPenalty =
1004 20 * Current->BindingStrength + splitPenalty(Line, *Current);
1005
Manuel Klimekb3987012013-05-29 14:47:47 +00001006 Current = Current->Next;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001007 }
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001008
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001009 calculateUnbreakableTailLengths(Line);
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001010 DEBUG({
1011 printDebugInfo(Line);
1012 });
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001013}
1014
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001015void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1016 unsigned UnbreakableTailLength = 0;
Manuel Klimekb3987012013-05-29 14:47:47 +00001017 FormatToken *Current = Line.Last;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001018 while (Current != NULL) {
1019 Current->UnbreakableTailLength = UnbreakableTailLength;
1020 if (Current->CanBreakBefore ||
1021 Current->isOneOf(tok::comment, tok::string_literal)) {
1022 UnbreakableTailLength = 0;
1023 } else {
1024 UnbreakableTailLength +=
Alexander Kornienko00895102013-06-05 14:09:10 +00001025 Current->CodePointCount + Current->SpacesRequiredBefore;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001026 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001027 Current = Current->Previous;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001028 }
1029}
1030
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001031unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001032 const FormatToken &Tok) {
1033 const FormatToken &Left = *Tok.Previous;
1034 const FormatToken &Right = Tok;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001035
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001036 if (Left.is(tok::semi))
1037 return 0;
1038 if (Left.is(tok::comma))
1039 return 1;
Daniel Jasper011c35d2013-07-12 11:19:37 +00001040 if (Right.is(tok::l_square))
1041 return 150;
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001042
Daniel Jasper6561f6a2013-07-09 07:43:55 +00001043 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001044 if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001045 return 3;
Daniel Jasperc18cff32013-07-11 12:34:23 +00001046 if (Left.Type == TT_StartOfName)
1047 return 20;
Daniel Jasper92495a82013-08-19 10:16:18 +00001048 if (Line.MightBeFunctionDecl && Right.BindingStrength == 1)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001049 // FIXME: Clean up hack of using BindingStrength to find top-level names.
1050 return Style.PenaltyReturnTypeOnItsOwnLine;
Daniel Jasper92495a82013-08-19 10:16:18 +00001051 return 200;
Daniel Jasper3c08a812013-02-24 18:54:32 +00001052 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001053 if (Left.is(tok::equal) && Right.is(tok::l_brace))
1054 return 150;
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001055 if (Left.Type == TT_CastRParen)
1056 return 100;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001057 if (Left.is(tok::coloncolon))
1058 return 500;
Daniel Jasper6b119d62013-04-05 17:22:09 +00001059 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1060 return 5000;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001061
Daniel Jasper6cabab42013-02-14 08:42:54 +00001062 if (Left.Type == TT_RangeBasedForLoopColon ||
1063 Left.Type == TT_InheritanceColon)
Daniel Jasper84a1a632013-02-26 13:18:08 +00001064 return 2;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001065
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001066 if (Right.isOneOf(tok::arrow, tok::period) &&
1067 Right.Type != TT_DesignatedInitializerPeriod) {
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001068 if (Left.isOneOf(tok::r_paren, tok::r_square) && Left.MatchingParen &&
1069 Left.MatchingParen->ParameterCount > 0)
Daniel Jasper518ee342013-02-26 13:59:14 +00001070 return 20; // Should be smaller than breaking at a nested comma.
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001071 return 150;
1072 }
1073
Daniel Jasper20a0f8c2013-07-11 21:02:56 +00001074 // Breaking before a trailing 'const' or not-function-like annotation is bad.
Daniel Jasperaa9e7b12013-08-01 13:46:58 +00001075 if (Left.is(tok::r_paren) && Line.Type != LT_ObjCProperty &&
Daniel Jasper20a0f8c2013-07-11 21:02:56 +00001076 (Right.is(tok::kw_const) || (Right.is(tok::identifier) && Right.Next &&
1077 Right.Next->isNot(tok::l_paren))))
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001078 return 150;
1079
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001080 // In for-loops, prefer breaking at ',' and ';'.
Manuel Klimekb3987012013-05-29 14:47:47 +00001081 if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
Daniel Jasper7d812812013-02-21 15:00:29 +00001082 return 4;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001083
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001084 // In Objective-C method expressions, prefer breaking before "param:" over
1085 // breaking after it.
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001086 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001087 return 0;
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001088 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001089 return 20;
1090
Daniel Jasper1407bee2013-04-11 14:29:13 +00001091 if (Left.is(tok::l_paren) && Line.MightBeFunctionDecl)
1092 return 100;
Daniel Jasperac3223e2013-04-10 09:49:49 +00001093 if (Left.opensScope())
Daniel Jaspere60084d2013-08-13 06:50:04 +00001094 return Left.ParameterCount > 1 ? prec::Comma : 19;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001095
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001096 if (Right.is(tok::lessless)) {
1097 if (Left.is(tok::string_literal)) {
Alexander Kornienko00895102013-06-05 14:09:10 +00001098 StringRef Content = Left.TokenText;
Daniel Jasperbfa1edd2013-03-14 14:00:17 +00001099 Content = Content.drop_back(1).drop_front(1).trim();
1100 if (Content.size() > 1 &&
1101 (Content.back() == ':' || Content.back() == '='))
Daniel Jasper9637dda2013-07-15 14:33:14 +00001102 return 25;
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001103 }
Daniel Jasper0c368782013-07-15 15:04:42 +00001104 return 1; // Breaking at a << is really cheap.
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001105 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001106 if (Left.Type == TT_ConditionalExpr)
Daniel Jasper518ee342013-02-26 13:59:14 +00001107 return prec::Conditional;
Manuel Klimekb3987012013-05-29 14:47:47 +00001108 prec::Level Level = Left.getPrecedence();
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001109
1110 if (Level != prec::Unknown)
1111 return Level;
Daniel Jasper24849712013-03-01 16:48:32 +00001112
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001113 return 3;
1114}
1115
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001116bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001117 const FormatToken &Left,
1118 const FormatToken &Right) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001119 if (Right.is(tok::hashhash))
1120 return Left.is(tok::hash);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001121 if (Left.isOneOf(tok::hashhash, tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001122 return Right.is(tok::hash);
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001123 if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1124 return Style.SpaceInEmptyParentheses;
1125 if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
1126 return Right.Type == TT_CastRParen ||
1127 (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen)
1128 ? Style.SpacesInCStyleCastParentheses
1129 : Style.SpacesInParentheses;
1130 if (Right.isOneOf(tok::semi, tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001131 return false;
1132 if (Right.is(tok::less) &&
1133 (Left.is(tok::kw_template) ||
1134 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1135 return true;
1136 if (Left.is(tok::arrow) || Right.is(tok::arrow))
1137 return false;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001138 if (Left.isOneOf(tok::exclaim, tok::tilde))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001139 return false;
1140 if (Left.is(tok::at) &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001141 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1142 tok::numeric_constant, tok::l_paren, tok::l_brace,
1143 tok::kw_true, tok::kw_false))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001144 return false;
1145 if (Left.is(tok::coloncolon))
1146 return false;
1147 if (Right.is(tok::coloncolon))
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001148 return !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren);
1149 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001150 return false;
Daniel Jasperc47d7f12013-07-01 09:47:25 +00001151 if (Right.is(tok::ellipsis))
1152 return false;
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001153 if (Right.Type == TT_PointerOrReference)
Manuel Klimekb3987012013-05-29 14:47:47 +00001154 return Left.Tok.isLiteral() ||
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001155 ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
1156 !Style.PointerBindsToType);
Daniel Jasper3ff4a2f2013-05-28 15:27:10 +00001157 if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
Daniel Jasper395228f2013-05-08 14:58:20 +00001158 (Left.Type != TT_PointerOrReference || Style.PointerBindsToType))
1159 return true;
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001160 if (Left.Type == TT_PointerOrReference)
Daniel Jasper3a1847e2013-07-01 09:34:09 +00001161 return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
Daniel Jasper9322aae2013-03-20 09:53:18 +00001162 ((Right.Type != TT_PointerOrReference) &&
Daniel Jasper81d2d382013-04-01 17:13:26 +00001163 Right.isNot(tok::l_paren) && Style.PointerBindsToType &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001164 Left.Previous &&
1165 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001166 if (Right.is(tok::star) && Left.is(tok::l_paren))
1167 return false;
Nico Weber051860e2013-02-10 02:08:05 +00001168 if (Left.is(tok::l_square))
1169 return Left.Type == TT_ObjCArrayLiteral && Right.isNot(tok::r_square);
1170 if (Right.is(tok::r_square))
1171 return Right.Type == TT_ObjCArrayLiteral;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001172 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr)
1173 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001174 if (Left.is(tok::colon))
1175 return Left.Type != TT_ObjCMethodExpr;
1176 if (Right.is(tok::colon))
Daniel Jasperab3ce592013-08-01 22:05:00 +00001177 return Right.Type != TT_ObjCMethodExpr && !Left.is(tok::question);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001178 if (Right.is(tok::l_paren)) {
Daniel Jaspere0fa4c52013-07-17 20:25:02 +00001179 if (Left.is(tok::r_paren) && Left.MatchingParen &&
1180 Left.MatchingParen->Previous &&
1181 Left.MatchingParen->Previous->is(tok::kw___attribute))
1182 return true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001183 return Line.Type == LT_ObjCDecl ||
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001184 Left.isOneOf(tok::kw_return, tok::kw_new,
1185 tok::kw_delete, tok::semi) ||
1186 (Style.SpaceAfterControlStatementKeyword &&
1187 Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
1188 tok::kw_catch));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001189 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001190 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001191 return false;
1192 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
Daniel Jasper2424eef2013-05-23 10:15:45 +00001193 return false; // No spaces in "{}".
1194 if (Left.is(tok::l_brace) || Right.is(tok::r_brace))
Daniel Jasperb5dc3f42013-07-16 18:22:10 +00001195 return !Style.Cpp11BracedListStyle;
Daniel Jasper1bee0732013-05-23 18:05:18 +00001196 if (Right.Type == TT_UnaryOperator)
1197 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1198 (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
Daniel Jasperce933562013-05-23 21:35:49 +00001199 if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) &&
Alexander Kornienko0bdc6432013-07-04 14:47:51 +00001200 Right.is(tok::l_brace) && Right.getNextNonComment())
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001201 return false;
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001202 if (Left.is(tok::period) || Right.is(tok::period))
1203 return false;
Nico Weber861576b2013-06-26 00:15:19 +00001204 if (Left.Type == TT_BlockComment && Left.TokenText.endswith("=*/"))
1205 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001206 return true;
1207}
1208
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001209bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001210 const FormatToken &Tok) {
1211 if (Tok.Tok.getIdentifierInfo() && Tok.Previous->Tok.getIdentifierInfo())
Daniel Jasper2b4c9242013-02-11 08:01:18 +00001212 return true; // Never ever merge two identifiers.
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001213 if (Line.Type == LT_ObjCMethodDecl) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001214 if (Tok.Previous->Type == TT_ObjCMethodSpecifier)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001215 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001216 if (Tok.Previous->is(tok::r_paren) && Tok.is(tok::identifier))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001217 // Don't space between ')' and <id>
1218 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001219 }
1220 if (Line.Type == LT_ObjCProperty &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001221 (Tok.is(tok::equal) || Tok.Previous->is(tok::equal)))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001222 return false;
1223
Daniel Jasper2ca37412013-07-09 14:36:48 +00001224 if (Tok.Type == TT_TrailingReturnArrow ||
1225 Tok.Previous->Type == TT_TrailingReturnArrow)
1226 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001227 if (Tok.Previous->is(tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001228 return true;
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001229 if (Tok.is(tok::comma))
1230 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001231 if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
1232 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001233 if (Tok.Previous->Tok.is(tok::kw_operator))
Daniel Jasper2b4c9242013-02-11 08:01:18 +00001234 return false;
1235 if (Tok.Type == TT_OverloadedOperatorLParen)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001236 return false;
1237 if (Tok.is(tok::colon))
Manuel Klimekb3987012013-05-29 14:47:47 +00001238 return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Daniel Jasperab3ce592013-08-01 22:05:00 +00001239 Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr &&
1240 !Tok.Previous->is(tok::question);
Manuel Klimekb3987012013-05-29 14:47:47 +00001241 if (Tok.Previous->Type == TT_UnaryOperator ||
1242 Tok.Previous->Type == TT_CastRParen)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001243 return false;
Manuel Klimekb3987012013-05-29 14:47:47 +00001244 if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) {
Daniel Jasper29f123b2013-02-08 15:28:42 +00001245 return Tok.Type == TT_TemplateCloser &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001246 Tok.Previous->Type == TT_TemplateCloser &&
Daniel Jasper29f123b2013-02-08 15:28:42 +00001247 Style.Standard != FormatStyle::LS_Cpp11;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001248 }
Alexander Kornienko54a38bd2013-03-20 16:41:56 +00001249 if (Tok.isOneOf(tok::arrowstar, tok::periodstar) ||
Manuel Klimekb3987012013-05-29 14:47:47 +00001250 Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar))
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001251 return false;
Daniel Jasper1dc6f742013-08-07 16:29:23 +00001252 if ((Tok.Type == TT_BinaryOperator && !Tok.Previous->is(tok::l_paren)) ||
1253 Tok.Previous->Type == TT_BinaryOperator)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001254 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001255 if (Tok.Previous->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001256 return false;
Manuel Klimekb3987012013-05-29 14:47:47 +00001257 if (Tok.is(tok::less) && Line.First->is(tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001258 return true;
1259 if (Tok.Type == TT_TrailingUnaryOperator)
1260 return false;
Manuel Klimekb3987012013-05-29 14:47:47 +00001261 return spaceRequiredBetween(Line, *Tok.Previous, Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001262}
1263
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001264bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001265 const FormatToken &Right) {
1266 const FormatToken &Left = *Right.Previous;
Daniel Jasper6561f6a2013-07-09 07:43:55 +00001267 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001268 return true;
Nico Weberf2ff8122013-05-26 05:39:26 +00001269 if (Right.is(tok::colon) &&
1270 (Right.Type == TT_ObjCDictLiteral || Right.Type == TT_ObjCMethodExpr))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001271 return false;
Nico Weberf2ff8122013-05-26 05:39:26 +00001272 if (Left.is(tok::colon) &&
1273 (Left.Type == TT_ObjCDictLiteral || Left.Type == TT_ObjCMethodExpr))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001274 return true;
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001275 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001276 return true;
Daniel Jasperaa9e7b12013-08-01 13:46:58 +00001277 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
1278 return true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001279 if (Left.ClosesTemplateDeclaration)
1280 return true;
Daniel Jasperab3ce592013-08-01 22:05:00 +00001281 if ((Right.Type == TT_ConditionalExpr &&
1282 !(Right.is(tok::colon) && Left.is(tok::question))) ||
1283 Right.is(tok::question))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001284 return true;
Daniel Jasper6cabab42013-02-14 08:42:54 +00001285 if (Right.Type == TT_RangeBasedForLoopColon ||
Daniel Jasper27b91cc2013-04-05 17:21:59 +00001286 Right.Type == TT_OverloadedOperatorLParen)
Daniel Jasper6cabab42013-02-14 08:42:54 +00001287 return false;
Daniel Jasperc194c952013-05-06 06:45:09 +00001288 if (Left.Type == TT_RangeBasedForLoopColon)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001289 return true;
Daniel Jasper7d812812013-02-21 15:00:29 +00001290 if (Right.Type == TT_RangeBasedForLoopColon)
1291 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001292 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
1293 Left.Type == TT_UnaryOperator || Left.Type == TT_ConditionalExpr ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001294 Left.isOneOf(tok::question, tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001295 return false;
1296 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
1297 return false;
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001298 if (Left.Previous) {
1299 if (Left.is(tok::l_paren) && Right.is(tok::l_paren) &&
1300 Left.Previous->is(tok::kw___attribute))
1301 return false;
Daniel Jasper2ca37412013-07-09 14:36:48 +00001302 if (Left.is(tok::l_paren) && (Left.Previous->Type == TT_BinaryOperator ||
Daniel Jasper5e2169f2013-07-18 14:46:07 +00001303 Left.Previous->Type == TT_CastRParen))
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001304 return false;
1305 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001306
Daniel Jasper65d2c382013-06-06 16:08:57 +00001307 if (Right.isTrailingComment())
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001308 // We rely on MustBreakBefore being set correctly here as we should not
1309 // change the "binding" behavior of a comment.
1310 return false;
1311
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001312 // We only break before r_brace if there was a corresponding break before
1313 // the l_brace, which is tracked by BreakBeforeClosingBrace.
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001314 if (Right.isOneOf(tok::r_brace, tok::r_paren) ||
1315 Right.Type == TT_TemplateCloser)
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001316 return false;
1317
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001318 // Allow breaking after a trailing 'const', e.g. after a method declaration,
1319 // unless it is follow by ';', '{' or '='.
Manuel Klimekb3987012013-05-29 14:47:47 +00001320 if (Left.is(tok::kw_const) && Left.Previous != NULL &&
1321 Left.Previous->is(tok::r_paren))
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001322 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001323
Daniel Jasper8ef19a22013-03-14 09:50:46 +00001324 if (Right.is(tok::kw___attribute))
1325 return true;
1326
Daniel Jasper3a204412013-02-23 07:46:38 +00001327 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
1328 return true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001329
1330 if (Left.Type == TT_CtorInitializerComma &&
1331 Style.BreakConstructorInitializersBeforeComma)
1332 return false;
1333 if (Right.isBinaryOperator() && Style.BreakBeforeBinaryOperators)
1334 return true;
1335 return (Left.isBinaryOperator() && Left.isNot(tok::lessless) &&
1336 !Style.BreakBeforeBinaryOperators) ||
Daniel Jasper6b119d62013-04-05 17:22:09 +00001337 Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
1338 tok::kw_class, tok::kw_struct) ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001339 Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon) ||
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001340 (Left.is(tok::r_paren) &&
Daniel Jaspere033e872013-05-21 09:16:31 +00001341 Right.isOneOf(tok::identifier, tok::kw_const, tok::kw___attribute)) ||
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001342 (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
Daniel Jasper011c35d2013-07-12 11:19:37 +00001343 Right.is(tok::l_square);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001344}
1345
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001346void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
1347 llvm::errs() << "AnnotatedTokens:\n";
Manuel Klimekb3987012013-05-29 14:47:47 +00001348 const FormatToken *Tok = Line.First;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001349 while (Tok) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001350 llvm::errs() << " M=" << Tok->MustBreakBefore
Daniel Jasperc7bd68f2013-07-10 14:02:49 +00001351 << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
1352 << " S=" << Tok->SpacesRequiredBefore
1353 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
1354 << " PPK=" << Tok->PackingKind << " FakeLParens=";
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001355 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
1356 llvm::errs() << Tok->FakeLParens[i] << "/";
1357 llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
Manuel Klimekb3987012013-05-29 14:47:47 +00001358 Tok = Tok->Next;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001359 }
1360 llvm::errs() << "----\n";
1361}
1362
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001363} // namespace format
1364} // namespace clang