blob: a91c847452155356e8aa0080e804f8595aca93e8 [file] [log] [blame]
Daniel Jasper7a6d09b2013-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 Jasper6bee6822013-04-08 20:33:42 +000018#include "llvm/Support/Debug.h"
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000019
20namespace clang {
21namespace format {
22
Craig Topper318ed7c2013-07-01 04:03:19 +000023namespace {
24
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000025/// \brief A parser that gathers additional information about tokens.
26///
Alexander Kornienkoa5151272013-03-12 16:28:18 +000027/// The \c TokenAnnotator tries to match parenthesis and square brakets and
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000028/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
29/// into template parameter lists.
30class AnnotatingParser {
31public:
Daniel Jasper8de9ed02013-08-22 15:00:41 +000032 AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
33 IdentifierInfo &Ident_in)
34 : Style(Style), Line(Line), CurrentToken(Line.First),
Daniel Jasperbc5cb4e2013-11-07 17:43:07 +000035 KeywordVirtualFound(false), AutoFound(false), Ident_in(Ident_in) {
Nico Weber9096fc02013-06-26 00:30:14 +000036 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
Manuel Klimek819788d2014-03-18 11:22:45 +000037 resetTokenMetadata(CurrentToken);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000038 }
39
Nico Weber44449172013-02-12 16:17:07 +000040private:
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000041 bool parseAngle() {
42 if (CurrentToken == NULL)
43 return false;
Daniel Jasper40aacf42013-03-14 13:45:21 +000044 ScopedContextCreator ContextCreator(*this, tok::less, 10);
Manuel Klimek6e6310e2013-05-29 14:47:47 +000045 FormatToken *Left = CurrentToken->Previous;
Daniel Jasperc697ad22013-02-06 10:05:46 +000046 Contexts.back().IsExpression = false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000047 while (CurrentToken != NULL) {
48 if (CurrentToken->is(tok::greater)) {
49 Left->MatchingParen = CurrentToken;
50 CurrentToken->MatchingParen = Left;
51 CurrentToken->Type = TT_TemplateCloser;
52 next();
53 return true;
54 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +000055 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
Daniel Jasper6f05e592013-05-15 13:46:48 +000056 tok::question, tok::colon))
57 return false;
Daniel Jasperd5893912013-06-01 18:56:00 +000058 // If a && or || is found and interpreted as a binary operator, this set
Daniel Jasper1027c6e2013-06-03 16:16:41 +000059 // of angles is likely part of something like "a < b && c > d". If the
Daniel Jasperd5893912013-06-01 18:56:00 +000060 // angles are inside an expression, the ||/&& might also be a binary
61 // operator that was misinterpreted because we are parsing template
62 // parameters.
63 // FIXME: This is getting out of hand, write a decent parser.
Manuel Klimek6e6310e2013-05-29 14:47:47 +000064 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
Daniel Jasperd5893912013-06-01 18:56:00 +000065 (CurrentToken->Previous->Type == TT_BinaryOperator ||
66 Contexts[Contexts.size() - 2].IsExpression) &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +000067 Line.First->isNot(tok::kw_template))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000068 return false;
Daniel Jaspere11095a2013-02-14 15:01:34 +000069 updateParameterCount(Left, CurrentToken);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000070 if (!consumeToken())
71 return false;
72 }
73 return false;
74 }
75
76 bool parseParens(bool LookForDecls = false) {
77 if (CurrentToken == NULL)
78 return false;
Daniel Jaspera225bce2014-01-16 19:14:34 +000079 bool AfterCaret = Contexts.back().CaretFound;
80 Contexts.back().CaretFound = false;
81
Daniel Jasper40aacf42013-03-14 13:45:21 +000082 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
Daniel Jasperc697ad22013-02-06 10:05:46 +000083
84 // FIXME: This is a bit of a hack. Do better.
85 Contexts.back().ColonIsForRangeExpr =
86 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
87
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000088 bool StartsObjCMethodExpr = false;
Manuel Klimek6e6310e2013-05-29 14:47:47 +000089 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000090 if (CurrentToken->is(tok::caret)) {
Daniel Jasperb88b25f2013-12-23 07:29:06 +000091 // (^ can start a block type.
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000092 Left->Type = TT_ObjCBlockLParen;
Manuel Klimek6e6310e2013-05-29 14:47:47 +000093 } else if (FormatToken *MaybeSel = Left->Previous) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000094 // @selector( starts a selector.
Manuel Klimek6e6310e2013-05-29 14:47:47 +000095 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
96 MaybeSel->Previous->is(tok::at)) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000097 StartsObjCMethodExpr = true;
98 }
99 }
100
Daniel Jasper29a98cf2013-08-13 09:09:09 +0000101 if (Left->Previous && Left->Previous->isOneOf(tok::kw_static_assert,
Daniel Jasperd46e07e2013-10-20 18:15:30 +0000102 tok::kw_if, tok::kw_while)) {
103 // static_assert, if and while usually contain expressions.
Daniel Jasper8b1c6352013-08-01 17:58:23 +0000104 Contexts.back().IsExpression = true;
Daniel Jasperd46e07e2013-10-20 18:15:30 +0000105 } else if (Left->Previous && Left->Previous->is(tok::r_square) &&
106 Left->Previous->MatchingParen &&
107 Left->Previous->MatchingParen->Type == TT_LambdaLSquare) {
108 // This is a parameter list of a lambda expression.
109 Contexts.back().IsExpression = false;
Daniel Jaspera225bce2014-01-16 19:14:34 +0000110 } else if (AfterCaret) {
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000111 // This is the parameter list of an ObjC block.
112 Contexts.back().IsExpression = false;
Daniel Jasper559b63c2014-01-28 20:13:43 +0000113 } else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
114 Left->Type = TT_AttributeParen;
Daniel Jasperd46e07e2013-10-20 18:15:30 +0000115 }
Daniel Jasper8b1c6352013-08-01 17:58:23 +0000116
Daniel Jasperc697ad22013-02-06 10:05:46 +0000117 if (StartsObjCMethodExpr) {
118 Contexts.back().ColonIsObjCMethodExpr = true;
119 Left->Type = TT_ObjCMethodExpr;
120 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000121
Daniel Jasper37194282013-05-28 08:33:00 +0000122 bool MightBeFunctionType = CurrentToken->is(tok::star);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000123 bool HasMultipleLines = false;
124 bool HasMultipleParametersOnALine = false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000125 while (CurrentToken != NULL) {
126 // LookForDecls is set when "if (" has been seen. Check for
127 // 'identifier' '*' 'identifier' followed by not '=' -- this
128 // '*' has to be a binary operator but determineStarAmpUsage() will
129 // categorize it as an unary operator, so set the right type here.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000130 if (LookForDecls && CurrentToken->Next) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000131 FormatToken *Prev = CurrentToken->getPreviousNonComment();
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000132 if (Prev) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000133 FormatToken *PrevPrev = Prev->getPreviousNonComment();
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000134 FormatToken *Next = CurrentToken->Next;
135 if (PrevPrev && PrevPrev->is(tok::identifier) &&
136 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
137 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
138 Prev->Type = TT_BinaryOperator;
139 LookForDecls = false;
140 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000141 }
142 }
143
Daniel Jasper59036852013-08-12 12:16:34 +0000144 if (CurrentToken->Previous->Type == TT_PointerOrReference &&
145 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
146 tok::coloncolon))
147 MightBeFunctionType = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000148 if (CurrentToken->is(tok::r_paren)) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000149 if (MightBeFunctionType && CurrentToken->Next &&
Daniel Jasper655d96a2013-07-16 11:37:21 +0000150 (CurrentToken->Next->is(tok::l_paren) ||
151 (CurrentToken->Next->is(tok::l_square) &&
152 !Contexts.back().IsExpression)))
Daniel Jasper37194282013-05-28 08:33:00 +0000153 Left->Type = TT_FunctionTypeLParen;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000154 Left->MatchingParen = CurrentToken;
155 CurrentToken->MatchingParen = Left;
156
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000157 if (StartsObjCMethodExpr) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000158 CurrentToken->Type = TT_ObjCMethodExpr;
159 if (Contexts.back().FirstObjCSelectorName != NULL) {
160 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
161 Contexts.back().LongestObjCSelectorName;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000162 }
163 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000164
Daniel Jasper559b63c2014-01-28 20:13:43 +0000165 if (Left->Type == TT_AttributeParen)
166 CurrentToken->Type = TT_AttributeParen;
167
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000168 if (!HasMultipleLines)
169 Left->PackingKind = PPK_Inconclusive;
170 else if (HasMultipleParametersOnALine)
171 Left->PackingKind = PPK_BinPacked;
172 else
173 Left->PackingKind = PPK_OnePerLine;
174
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000175 next();
176 return true;
177 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000178 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000179 return false;
Daniel Jasper31745732014-01-19 07:46:32 +0000180 else if (CurrentToken->is(tok::l_brace))
181 Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen
Daniel Jaspere11095a2013-02-14 15:01:34 +0000182 updateParameterCount(Left, CurrentToken);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000183 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
184 !CurrentToken->Next->HasUnescapedNewline &&
185 !CurrentToken->Next->isTrailingComment())
186 HasMultipleParametersOnALine = true;
Daniel Jasperc580af92014-03-11 09:29:46 +0000187 if (CurrentToken->is(tok::kw_const) ||
188 CurrentToken->isSimpleTypeSpecifier())
189 Contexts.back().IsExpression = false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000190 if (!consumeToken())
191 return false;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000192 if (CurrentToken && CurrentToken->HasUnescapedNewline)
193 HasMultipleLines = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000194 }
195 return false;
196 }
197
198 bool parseSquare() {
199 if (!CurrentToken)
200 return false;
201
Alexander Kornienkoafaa8f52013-06-17 13:19:53 +0000202 // A '[' could be an index subscript (after an identifier or after
Nico Weber2a726b62013-02-10 02:08:05 +0000203 // ')' or ']'), it could be the start of an Objective-C method
204 // expression, or it could the the start of an Objective-C array literal.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000205 FormatToken *Left = CurrentToken->Previous;
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000206 FormatToken *Parent = Left->getPreviousNonComment();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000207 bool StartsObjCMethodExpr =
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000208 Contexts.back().CanBeExpression && Left->Type != TT_LambdaLSquare &&
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000209 (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
210 tok::kw_return, tok::kw_throw) ||
Daniel Jasperc04baae2013-04-10 09:49:49 +0000211 Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn ||
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000212 Parent->Type == TT_CastRParen ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000213 getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
Daniel Jasper40aacf42013-03-14 13:45:21 +0000214 ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
Daniel Jasper97b89482013-03-13 07:49:51 +0000215 Contexts.back().IsExpression = true;
Daniel Jaspera1ea4cb2013-10-26 17:00:22 +0000216 bool ColonFound = false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000217
Daniel Jasperc697ad22013-02-06 10:05:46 +0000218 if (StartsObjCMethodExpr) {
219 Contexts.back().ColonIsObjCMethodExpr = true;
220 Left->Type = TT_ObjCMethodExpr;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000221 } else if (Parent && Parent->is(tok::at)) {
222 Left->Type = TT_ArrayInitializerLSquare;
223 } else if (Left->Type == TT_Unknown) {
224 Left->Type = TT_ArraySubscriptLSquare;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000225 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000226
227 while (CurrentToken != NULL) {
228 if (CurrentToken->is(tok::r_square)) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000229 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
230 Left->Type == TT_ObjCMethodExpr) {
Nico Weber5d2624e2013-02-06 06:20:11 +0000231 // An ObjC method call is rarely followed by an open parenthesis.
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000232 // FIXME: Do we incorrectly label ":" with this?
233 StartsObjCMethodExpr = false;
234 Left->Type = TT_Unknown;
235 }
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000236 if (StartsObjCMethodExpr) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000237 CurrentToken->Type = TT_ObjCMethodExpr;
Nico Weber5d2624e2013-02-06 06:20:11 +0000238 // determineStarAmpUsage() thinks that '*' '[' is allocating an
239 // array of pointers, but if '[' starts a selector then '*' is a
240 // binary operator.
Alexander Kornienkoa5151272013-03-12 16:28:18 +0000241 if (Parent != NULL && Parent->Type == TT_PointerOrReference)
Nico Weberac9bde22013-02-06 16:54:35 +0000242 Parent->Type = TT_BinaryOperator;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000243 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000244 Left->MatchingParen = CurrentToken;
245 CurrentToken->MatchingParen = Left;
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000246 if (Contexts.back().FirstObjCSelectorName != NULL) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000247 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
248 Contexts.back().LongestObjCSelectorName;
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000249 if (Contexts.back().NumBlockParameters > 1)
250 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
251 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000252 next();
253 return true;
254 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000255 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000256 return false;
Daniel Jaspera1ea4cb2013-10-26 17:00:22 +0000257 if (CurrentToken->is(tok::colon))
258 ColonFound = true;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000259 if (CurrentToken->is(tok::comma) &&
Daniel Jaspera0e9be22014-01-28 18:51:11 +0000260 Style.Language != FormatStyle::LK_Proto &&
Daniel Jasperb596fb22013-10-24 10:31:50 +0000261 (Left->Type == TT_ArraySubscriptLSquare ||
Daniel Jaspera1ea4cb2013-10-26 17:00:22 +0000262 (Left->Type == TT_ObjCMethodExpr && !ColonFound)))
Daniel Jasper1db6c382013-10-22 15:30:28 +0000263 Left->Type = TT_ArrayInitializerLSquare;
Daniel Jaspere11095a2013-02-14 15:01:34 +0000264 updateParameterCount(Left, CurrentToken);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000265 if (!consumeToken())
266 return false;
267 }
268 return false;
269 }
270
271 bool parseBrace() {
Daniel Jasper8e357692013-05-06 08:27:33 +0000272 if (CurrentToken != NULL) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000273 FormatToken *Left = CurrentToken->Previous;
Daniel Jasperb596fb22013-10-24 10:31:50 +0000274 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
275 Contexts.back().ColonIsDictLiteral = true;
Nico Weberced7d412013-05-26 05:39:26 +0000276
Daniel Jasper8e357692013-05-06 08:27:33 +0000277 while (CurrentToken != NULL) {
278 if (CurrentToken->is(tok::r_brace)) {
279 Left->MatchingParen = CurrentToken;
280 CurrentToken->MatchingParen = Left;
281 next();
282 return true;
283 }
284 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
285 return false;
286 updateParameterCount(Left, CurrentToken);
Daniel Jasperf24301d2014-01-29 18:52:43 +0000287 if (CurrentToken->is(tok::colon) &&
288 Style.Language != FormatStyle::LK_Proto)
Daniel Jasperb596fb22013-10-24 10:31:50 +0000289 Left->Type = TT_DictLiteral;
Daniel Jasper8e357692013-05-06 08:27:33 +0000290 if (!consumeToken())
291 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000292 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000293 }
Daniel Jasper8e357692013-05-06 08:27:33 +0000294 // No closing "}" found, this probably starts a definition.
295 Line.StartsDefinition = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000296 return true;
297 }
Daniel Jasperdc7d5812013-02-20 12:56:39 +0000298
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000299 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000300 if (Current->is(tok::comma)) {
Daniel Jaspere11095a2013-02-14 15:01:34 +0000301 ++Left->ParameterCount;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000302 if (!Left->Role)
303 Left->Role.reset(new CommaSeparatedList(Style));
304 Left->Role->CommaFound(Current);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000305 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
Daniel Jaspere11095a2013-02-14 15:01:34 +0000306 Left->ParameterCount = 1;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000307 }
Daniel Jaspere11095a2013-02-14 15:01:34 +0000308 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000309
310 bool parseConditional() {
311 while (CurrentToken != NULL) {
312 if (CurrentToken->is(tok::colon)) {
313 CurrentToken->Type = TT_ConditionalExpr;
314 next();
315 return true;
316 }
317 if (!consumeToken())
318 return false;
319 }
320 return false;
321 }
322
323 bool parseTemplateDeclaration() {
324 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
325 CurrentToken->Type = TT_TemplateOpener;
326 next();
327 if (!parseAngle())
328 return false;
Daniel Jasper00475962013-02-19 17:14:38 +0000329 if (CurrentToken != NULL)
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000330 CurrentToken->Previous->ClosesTemplateDeclaration = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000331 return true;
332 }
333 return false;
334 }
335
336 bool consumeToken() {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000337 FormatToken *Tok = CurrentToken;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000338 next();
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000339 switch (Tok->Tok.getKind()) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000340 case tok::plus:
341 case tok::minus:
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000342 if (Tok->Previous == NULL && Line.MustBeDeclaration)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000343 Tok->Type = TT_ObjCMethodSpecifier;
344 break;
345 case tok::colon:
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000346 if (Tok->Previous == NULL)
Daniel Jasper850677d2013-03-18 12:50:26 +0000347 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000348 // Colons from ?: are handled in parseConditional().
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000349 if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000350 Tok->Type = TT_CtorInitializerColon;
Daniel Jasperb596fb22013-10-24 10:31:50 +0000351 } else if (Contexts.back().ColonIsDictLiteral) {
352 Tok->Type = TT_DictLiteral;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000353 } else if (Contexts.back().ColonIsObjCMethodExpr ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000354 Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000355 Tok->Type = TT_ObjCMethodExpr;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000356 Tok->Previous->Type = TT_ObjCSelectorName;
Alexander Kornienko39856b72013-09-10 09:38:25 +0000357 if (Tok->Previous->ColumnWidth >
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000358 Contexts.back().LongestObjCSelectorName) {
Alexander Kornienko60d1b042013-10-10 13:36:20 +0000359 Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000360 }
Daniel Jasperc697ad22013-02-06 10:05:46 +0000361 if (Contexts.back().FirstObjCSelectorName == NULL)
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000362 Contexts.back().FirstObjCSelectorName = Tok->Previous;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000363 } else if (Contexts.back().ColonIsForRangeExpr) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000364 Tok->Type = TT_RangeBasedForLoopColon;
Alexander Kornienko60d1b042013-10-10 13:36:20 +0000365 } else if (CurrentToken != NULL &&
366 CurrentToken->is(tok::numeric_constant)) {
367 Tok->Type = TT_BitFieldColon;
Daniel Jasperf9a5e402013-10-08 16:24:07 +0000368 } else if (Contexts.size() == 1 && Line.First->isNot(tok::kw_enum)) {
Daniel Jaspereead02b2013-02-14 08:42:54 +0000369 Tok->Type = TT_InheritanceColon;
Daniel Jasper40aacf42013-03-14 13:45:21 +0000370 } else if (Contexts.back().ContextKind == tok::l_paren) {
371 Tok->Type = TT_InlineASMColon;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000372 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000373 break;
374 case tok::kw_if:
375 case tok::kw_while:
376 if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
377 next();
Nico Weber9096fc02013-06-26 00:30:14 +0000378 if (!parseParens(/*LookForDecls=*/true))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000379 return false;
380 }
381 break;
382 case tok::kw_for:
Daniel Jasperc697ad22013-02-06 10:05:46 +0000383 Contexts.back().ColonIsForRangeExpr = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000384 next();
385 if (!parseParens())
386 return false;
387 break;
388 case tok::l_paren:
389 if (!parseParens())
390 return false;
Daniel Jasperbc5cb4e2013-11-07 17:43:07 +0000391 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
Daniel Jasper43e6a282013-12-16 15:01:54 +0000392 !Contexts.back().IsExpression && Line.First->Type != TT_ObjCProperty)
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000393 Line.MightBeFunctionDecl = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000394 break;
395 case tok::l_square:
396 if (!parseSquare())
397 return false;
398 break;
399 case tok::l_brace:
400 if (!parseBrace())
401 return false;
402 break;
403 case tok::less:
Daniel Jasper62c0ac02013-07-30 22:37:19 +0000404 if (Tok->Previous && !Tok->Previous->Tok.isLiteral() && parseAngle())
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000405 Tok->Type = TT_TemplateOpener;
406 else {
407 Tok->Type = TT_BinaryOperator;
408 CurrentToken = Tok;
409 next();
410 }
411 break;
412 case tok::r_paren:
413 case tok::r_square:
414 return false;
415 case tok::r_brace:
416 // Lines can start with '}'.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000417 if (Tok->Previous != NULL)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000418 return false;
419 break;
420 case tok::greater:
421 Tok->Type = TT_BinaryOperator;
422 break;
423 case tok::kw_operator:
Daniel Jasper42401c82013-09-02 09:20:39 +0000424 while (CurrentToken &&
425 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000426 if (CurrentToken->isOneOf(tok::star, tok::amp))
Daniel Jasper35d2dc72013-02-11 08:01:18 +0000427 CurrentToken->Type = TT_PointerOrReference;
428 consumeToken();
Daniel Jasper42401c82013-09-02 09:20:39 +0000429 if (CurrentToken && CurrentToken->Previous->Type == TT_BinaryOperator)
Daniel Jasperd215b8b2013-08-28 07:27:35 +0000430 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000431 }
Daniel Jasper8f9624b2013-05-10 07:59:58 +0000432 if (CurrentToken) {
Daniel Jasper35d2dc72013-02-11 08:01:18 +0000433 CurrentToken->Type = TT_OverloadedOperatorLParen;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000434 if (CurrentToken->Previous->Type == TT_BinaryOperator)
435 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper8f9624b2013-05-10 07:59:58 +0000436 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000437 break;
438 case tok::question:
439 parseConditional();
440 break;
441 case tok::kw_template:
442 parseTemplateDeclaration();
443 break;
Nico Weber29f9dea2013-02-11 15:32:15 +0000444 case tok::identifier:
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000445 if (Line.First->is(tok::kw_for) &&
446 Tok->Tok.getIdentifierInfo() == &Ident_in)
Nico Weber29f9dea2013-02-11 15:32:15 +0000447 Tok->Type = TT_ObjCForIn;
448 break;
Daniel Jaspera628c982013-04-03 13:36:17 +0000449 case tok::comma:
450 if (Contexts.back().FirstStartOfName)
451 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000452 if (Contexts.back().InCtorInitializer)
453 Tok->Type = TT_CtorInitializerComma;
Daniel Jaspera628c982013-04-03 13:36:17 +0000454 break;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000455 default:
456 break;
457 }
458 return true;
459 }
460
461 void parseIncludeDirective() {
462 next();
463 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
464 next();
465 while (CurrentToken != NULL) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000466 if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000467 CurrentToken->Type = TT_ImplicitStringLiteral;
468 next();
469 }
470 } else {
471 while (CurrentToken != NULL) {
Daniel Jasperaf5ba0e2013-02-23 07:46:38 +0000472 if (CurrentToken->is(tok::string_literal))
473 // Mark these string literals as "implicit" literals, too, so that
474 // they are not split or line-wrapped.
475 CurrentToken->Type = TT_ImplicitStringLiteral;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000476 next();
477 }
478 }
479 }
480
481 void parseWarningOrError() {
482 next();
483 // We still want to format the whitespace left of the first token of the
484 // warning or error.
485 next();
486 while (CurrentToken != NULL) {
487 CurrentToken->Type = TT_ImplicitStringLiteral;
488 next();
489 }
490 }
491
Daniel Jasperdc32c1b2014-01-09 13:56:49 +0000492 void parsePragma() {
493 next(); // Consume "pragma".
494 if (CurrentToken && CurrentToken->TokenText == "mark") {
495 next(); // Consume "mark".
496 next(); // Consume first token (so we fix leading whitespace).
497 while (CurrentToken != NULL) {
498 CurrentToken->Type = TT_ImplicitStringLiteral;
499 next();
500 }
501 }
502 }
503
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000504 void parsePreprocessorDirective() {
505 next();
506 if (CurrentToken == NULL)
507 return;
Alexander Kornienko384b40b2013-10-11 21:43:05 +0000508 if (CurrentToken->Tok.is(tok::numeric_constant)) {
509 CurrentToken->SpacesRequiredBefore = 1;
510 return;
511 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000512 // Hashes in the middle of a line can lead to any strange token
513 // sequence.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000514 if (CurrentToken->Tok.getIdentifierInfo() == NULL)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000515 return;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000516 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000517 case tok::pp_include:
518 case tok::pp_import:
519 parseIncludeDirective();
520 break;
521 case tok::pp_error:
522 case tok::pp_warning:
523 parseWarningOrError();
524 break;
Daniel Jasperdc32c1b2014-01-09 13:56:49 +0000525 case tok::pp_pragma:
526 parsePragma();
527 break;
Daniel Jasper4431aa92013-04-23 13:54:04 +0000528 case tok::pp_if:
529 case tok::pp_elif:
Daniel Jasper7cfde412014-01-21 08:56:09 +0000530 Contexts.back().IsExpression = true;
Daniel Jasper4431aa92013-04-23 13:54:04 +0000531 parseLine();
532 break;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000533 default:
534 break;
535 }
Daniel Jaspera885dbe2013-02-05 09:34:14 +0000536 while (CurrentToken != NULL)
537 next();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000538 }
539
Nico Weber44449172013-02-12 16:17:07 +0000540public:
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000541 LineType parseLine() {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000542 if (CurrentToken->is(tok::hash)) {
543 parsePreprocessorDirective();
544 return LT_PreprocessorDirective;
545 }
Daniel Jasper7cfde412014-01-21 08:56:09 +0000546
Daniel Jasper47ef6dd2014-01-17 16:21:39 +0000547 // Directly allow to 'import <string-literal>' to support protocol buffer
548 // definitions (code.google.com/p/protobuf) or missing "#" (either way we
549 // should not break the line).
550 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
551 if (Info && Info->getPPKeywordID() == tok::pp_import &&
552 CurrentToken->Next && CurrentToken->Next->is(tok::string_literal))
553 parseIncludeDirective();
554
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000555 while (CurrentToken != NULL) {
556 if (CurrentToken->is(tok::kw_virtual))
557 KeywordVirtualFound = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000558 if (!consumeToken())
559 return LT_Invalid;
560 }
561 if (KeywordVirtualFound)
562 return LT_VirtualFunctionDecl;
563
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000564 if (Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000565 if (Contexts.back().FirstObjCSelectorName != NULL)
566 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
567 Contexts.back().LongestObjCSelectorName;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000568 return LT_ObjCMethodDecl;
569 }
570
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000571 return LT_Other;
572 }
573
Nico Weber44449172013-02-12 16:17:07 +0000574private:
Manuel Klimek819788d2014-03-18 11:22:45 +0000575 void resetTokenMetadata(FormatToken *Token) {
576 if (Token == nullptr) return;
577
578 // Reset token type in case we have already looked at it and then
579 // recovered from an error (e.g. failure to find the matching >).
580 if (CurrentToken->Type != TT_LambdaLSquare &&
581 CurrentToken->Type != TT_FunctionLBrace &&
582 CurrentToken->Type != TT_ImplicitStringLiteral &&
583 CurrentToken->Type != TT_TrailingReturnArrow)
584 CurrentToken->Type = TT_Unknown;
585 if (CurrentToken->Role)
586 CurrentToken->Role.reset(NULL);
587 CurrentToken->FakeLParens.clear();
588 CurrentToken->FakeRParens = 0;
589 }
590
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000591 void next() {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000592 if (CurrentToken != NULL) {
593 determineTokenType(*CurrentToken);
Daniel Jasperc697ad22013-02-06 10:05:46 +0000594 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
Daniel Jasper63af7c42013-12-09 14:40:19 +0000595 CurrentToken->NestingLevel = Contexts.size() - 1;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000596 }
597
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000598 if (CurrentToken != NULL)
599 CurrentToken = CurrentToken->Next;
Daniel Jasper5065bc42013-02-18 12:44:35 +0000600
Manuel Klimek819788d2014-03-18 11:22:45 +0000601 resetTokenMetadata(CurrentToken);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000602 }
603
Daniel Jasperc697ad22013-02-06 10:05:46 +0000604 /// \brief A struct to hold information valid in a specific context, e.g.
605 /// a pair of parenthesis.
606 struct Context {
Daniel Jasper40aacf42013-03-14 13:45:21 +0000607 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
608 bool IsExpression)
609 : ContextKind(ContextKind), BindingStrength(BindingStrength),
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000610 LongestObjCSelectorName(0), NumBlockParameters(0),
611 ColonIsForRangeExpr(false), ColonIsDictLiteral(false),
612 ColonIsObjCMethodExpr(false), FirstObjCSelectorName(NULL),
613 FirstStartOfName(NULL), IsExpression(IsExpression),
Daniel Jaspera225bce2014-01-16 19:14:34 +0000614 CanBeExpression(true), InCtorInitializer(false), CaretFound(false) {}
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000615
Daniel Jasper40aacf42013-03-14 13:45:21 +0000616 tok::TokenKind ContextKind;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000617 unsigned BindingStrength;
618 unsigned LongestObjCSelectorName;
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000619 unsigned NumBlockParameters;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000620 bool ColonIsForRangeExpr;
Daniel Jasperb596fb22013-10-24 10:31:50 +0000621 bool ColonIsDictLiteral;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000622 bool ColonIsObjCMethodExpr;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000623 FormatToken *FirstObjCSelectorName;
624 FormatToken *FirstStartOfName;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000625 bool IsExpression;
Daniel Jasper97b89482013-03-13 07:49:51 +0000626 bool CanBeExpression;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000627 bool InCtorInitializer;
Daniel Jaspera225bce2014-01-16 19:14:34 +0000628 bool CaretFound;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000629 };
630
631 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
632 /// of each instance.
633 struct ScopedContextCreator {
634 AnnotatingParser &P;
635
Daniel Jasper40aacf42013-03-14 13:45:21 +0000636 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
637 unsigned Increase)
638 : P(P) {
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000639 P.Contexts.push_back(Context(ContextKind,
640 P.Contexts.back().BindingStrength + Increase,
641 P.Contexts.back().IsExpression));
Daniel Jasperc697ad22013-02-06 10:05:46 +0000642 }
643
644 ~ScopedContextCreator() { P.Contexts.pop_back(); }
645 };
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000646
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000647 void determineTokenType(FormatToken &Current) {
648 if (Current.getPrecedence() == prec::Assignment &&
Daniel Jasper59036852013-08-12 12:16:34 +0000649 !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000650 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000651 Contexts.back().IsExpression = true;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000652 for (FormatToken *Previous = Current.Previous;
Daniel Jasper5ca9b712013-09-11 20:37:10 +0000653 Previous && !Previous->isOneOf(tok::comma, tok::semi);
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000654 Previous = Previous->Previous) {
Daniel Jasper8e559272013-02-27 11:43:50 +0000655 if (Previous->is(tok::r_square))
656 Previous = Previous->MatchingParen;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000657 if (Previous->Type == TT_BinaryOperator &&
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000658 Previous->isOneOf(tok::star, tok::amp)) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000659 Previous->Type = TT_PointerOrReference;
660 }
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000661 }
Daniel Jasper3682fcd2013-12-16 08:36:18 +0000662 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000663 Contexts.back().IsExpression = true;
Daniel Jasper3682fcd2013-12-16 08:36:18 +0000664 } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
665 !Line.InPPDirective) {
666 bool ParametersOfFunctionType =
667 Current.Previous && Current.Previous->is(tok::r_paren) &&
668 Current.Previous->MatchingParen &&
669 Current.Previous->MatchingParen->Type == TT_FunctionTypeLParen;
670 bool IsForOrCatch = Current.Previous &&
671 Current.Previous->isOneOf(tok::kw_for, tok::kw_catch);
672 Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000673 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000674 for (FormatToken *Previous = Current.Previous;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000675 Previous && Previous->isOneOf(tok::star, tok::amp);
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000676 Previous = Previous->Previous)
Nico Weber44449172013-02-12 16:17:07 +0000677 Previous->Type = TT_PointerOrReference;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000678 } else if (Current.Previous &&
679 Current.Previous->Type == TT_CtorInitializerColon) {
Daniel Jasper5065bc42013-02-18 12:44:35 +0000680 Contexts.back().IsExpression = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000681 Contexts.back().InCtorInitializer = true;
Daniel Jasper97b89482013-03-13 07:49:51 +0000682 } else if (Current.is(tok::kw_new)) {
683 Contexts.back().CanBeExpression = false;
Daniel Jaspera98da3d2013-11-07 19:56:07 +0000684 } else if (Current.is(tok::semi) || Current.is(tok::exclaim)) {
Daniel Jasperc37de302013-05-03 14:41:24 +0000685 // This should be the condition or increment in a for-loop.
686 Contexts.back().IsExpression = true;
Nico Weber44449172013-02-12 16:17:07 +0000687 }
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000688
689 if (Current.Type == TT_Unknown) {
Daniel Jasperf3167902013-09-27 08:29:16 +0000690 // Line.MightBeFunctionDecl can only be true after the parentheses of a
691 // function declaration have been found. In this case, 'Current' is a
692 // trailing token of this declaration and thus cannot be a name.
693 if (isStartOfName(Current) && !Line.MightBeFunctionDecl) {
Daniel Jaspera628c982013-04-03 13:36:17 +0000694 Contexts.back().FirstStartOfName = &Current;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000695 Current.Type = TT_StartOfName;
Daniel Jasper6cdec7c2013-07-09 14:36:48 +0000696 } else if (Current.is(tok::kw_auto)) {
697 AutoFound = true;
Daniel Jaspera3501d42013-07-11 14:33:06 +0000698 } else if (Current.is(tok::arrow) && AutoFound &&
699 Line.MustBeDeclaration) {
Daniel Jasper6cdec7c2013-07-09 14:36:48 +0000700 Current.Type = TT_TrailingReturnArrow;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000701 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000702 Current.Type =
Daniel Jasper6f9c8d22013-07-05 13:30:40 +0000703 determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
704 Contexts.back().IsExpression);
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000705 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000706 Current.Type = determinePlusMinusCaretUsage(Current);
Daniel Jaspera225bce2014-01-16 19:14:34 +0000707 if (Current.Type == TT_UnaryOperator) {
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000708 ++Contexts.back().NumBlockParameters;
Daniel Jaspera225bce2014-01-16 19:14:34 +0000709 if (Current.is(tok::caret))
710 Contexts.back().CaretFound = true;
711 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000712 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000713 Current.Type = determineIncrementUsage(Current);
714 } else if (Current.is(tok::exclaim)) {
715 Current.Type = TT_UnaryOperator;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000716 } else if (Current.isBinaryOperator() &&
717 (!Current.Previous ||
718 Current.Previous->isNot(tok::l_square))) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000719 Current.Type = TT_BinaryOperator;
720 } else if (Current.is(tok::comment)) {
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000721 if (Current.TokenText.startswith("//"))
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000722 Current.Type = TT_LineComment;
723 else
724 Current.Type = TT_BlockComment;
Nico Weberc6fe2162013-02-13 04:13:13 +0000725 } else if (Current.is(tok::r_paren)) {
Daniel Jasperda6f2252013-05-31 16:14:28 +0000726 FormatToken *LeftOfParens = NULL;
727 if (Current.MatchingParen)
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000728 LeftOfParens = Current.MatchingParen->getPreviousNonComment();
Daniel Jasperda6f2252013-05-31 16:14:28 +0000729 bool IsCast = false;
730 bool ParensAreEmpty = Current.Previous == Current.MatchingParen;
731 bool ParensAreType = !Current.Previous ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000732 Current.Previous->Type == TT_PointerOrReference ||
Daniel Jasperda6f2252013-05-31 16:14:28 +0000733 Current.Previous->Type == TT_TemplateCloser ||
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000734 Current.Previous->isSimpleTypeSpecifier();
Nico Weberc6fe2162013-02-13 04:13:13 +0000735 bool ParensCouldEndDecl =
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000736 Current.Next &&
737 Current.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
Daniel Jasper8a68b952013-03-13 17:13:53 +0000738 bool IsSizeOfOrAlignOf =
Daniel Jasperda6f2252013-05-31 16:14:28 +0000739 LeftOfParens &&
740 LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
741 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
Daniel Jasperc580af92014-03-11 09:29:46 +0000742 ((Contexts.size() > 1 &&
743 Contexts[Contexts.size() - 2].IsExpression) ||
Daniel Jasper49a94482013-07-15 15:04:42 +0000744 (Current.Next && Current.Next->isBinaryOperator())))
Daniel Jasperda6f2252013-05-31 16:14:28 +0000745 IsCast = true;
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000746 if (Current.Next && Current.Next->isNot(tok::string_literal) &&
Daniel Jasperda6f2252013-05-31 16:14:28 +0000747 (Current.Next->Tok.isLiteral() ||
748 Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
749 IsCast = true;
750 // If there is an identifier after the (), it is likely a cast, unless
751 // there is also an identifier before the ().
Daniel Jasper05866372013-06-06 08:20:20 +0000752 if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL ||
753 LeftOfParens->is(tok::kw_return)) &&
Daniel Jasper6a09df72013-07-08 14:58:01 +0000754 LeftOfParens->Type != TT_OverloadedOperator &&
Daniel Jasperb1c19f82014-01-10 07:44:53 +0000755 LeftOfParens->isNot(tok::at) &&
Nico Weberec9e4102013-06-25 00:55:57 +0000756 LeftOfParens->Type != TT_TemplateCloser && Current.Next &&
757 Current.Next->is(tok::identifier))
Daniel Jasperda6f2252013-05-31 16:14:28 +0000758 IsCast = true;
759 if (IsCast && !ParensAreEmpty)
Nico Weberc6fe2162013-02-13 04:13:13 +0000760 Current.Type = TT_CastRParen;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000761 } else if (Current.is(tok::at) && Current.Next) {
762 switch (Current.Next->Tok.getObjCKeywordID()) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000763 case tok::objc_interface:
764 case tok::objc_implementation:
765 case tok::objc_protocol:
766 Current.Type = TT_ObjCDecl;
767 break;
768 case tok::objc_property:
769 Current.Type = TT_ObjCProperty;
770 break;
771 default:
772 break;
773 }
Daniel Jasperbca4bbe2013-05-28 11:30:49 +0000774 } else if (Current.is(tok::period)) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000775 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
Daniel Jasperbca4bbe2013-05-28 11:30:49 +0000776 if (PreviousNoComment &&
777 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
778 Current.Type = TT_DesignatedInitializerPeriod;
Daniel Jasper43e6a282013-12-16 15:01:54 +0000779 } else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
780 Line.MightBeFunctionDecl && Contexts.size() == 1) {
781 // Line.MightBeFunctionDecl can only be true after the parentheses of a
782 // function declaration have been found.
783 Current.Type = TT_TrailingAnnotation;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000784 }
785 }
786 }
787
Daniel Jasperdba1c552013-07-02 09:47:29 +0000788 /// \brief Take a guess at whether \p Tok starts a name of a function or
789 /// variable declaration.
790 ///
791 /// This is a heuristic based on whether \p Tok is an identifier following
792 /// something that is likely a type.
793 bool isStartOfName(const FormatToken &Tok) {
794 if (Tok.isNot(tok::identifier) || Tok.Previous == NULL)
795 return false;
796
797 // Skip "const" as it does not have an influence on whether this is a name.
798 FormatToken *PreviousNotConst = Tok.Previous;
799 while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const))
800 PreviousNotConst = PreviousNotConst->Previous;
801
802 if (PreviousNotConst == NULL)
803 return false;
804
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000805 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
806 PreviousNotConst->Previous &&
807 PreviousNotConst->Previous->is(tok::hash);
Daniel Jasperdba1c552013-07-02 09:47:29 +0000808
Daniel Jasper53643062013-08-19 10:16:18 +0000809 if (PreviousNotConst->Type == TT_TemplateCloser)
810 return PreviousNotConst && PreviousNotConst->MatchingParen &&
811 PreviousNotConst->MatchingParen->Previous &&
812 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
813
Daniel Jasperdba1c552013-07-02 09:47:29 +0000814 return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
815 PreviousNotConst->Type == TT_PointerOrReference ||
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000816 PreviousNotConst->isSimpleTypeSpecifier();
Daniel Jasperdba1c552013-07-02 09:47:29 +0000817 }
818
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000819 /// \brief Return the type of the given token assuming it is * or &.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000820 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000821 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000822 if (PrevToken == NULL)
823 return TT_UnaryOperator;
824
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000825 const FormatToken *NextToken = Tok.getNextNonComment();
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000826 if (NextToken == NULL)
827 return TT_Unknown;
828
Daniel Jasper37194282013-05-28 08:33:00 +0000829 if (PrevToken->is(tok::coloncolon) ||
830 (PrevToken->is(tok::l_paren) && !IsExpression))
Daniel Jasper8eb371b2013-03-01 17:13:29 +0000831 return TT_PointerOrReference;
832
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000833 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
Daniel Jasperae907642013-03-14 10:50:25 +0000834 tok::comma, tok::semi, tok::kw_return, tok::colon,
Daniel Jasperdf620b22013-09-21 17:31:51 +0000835 tok::equal, tok::kw_delete, tok::kw_sizeof) ||
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000836 PrevToken->Type == TT_BinaryOperator ||
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000837 PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
838 return TT_UnaryOperator;
839
Nico Weber5d2624e2013-02-06 06:20:11 +0000840 if (NextToken->is(tok::l_square))
841 return TT_PointerOrReference;
842
Daniel Jasper71665cd2013-09-10 10:26:38 +0000843 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
844 PrevToken->MatchingParen->Previous &&
845 PrevToken->MatchingParen->Previous->is(tok::kw_typeof))
846 return TT_PointerOrReference;
847
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000848 if (PrevToken->Tok.isLiteral() ||
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000849 PrevToken->isOneOf(tok::r_paren, tok::r_square) ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000850 NextToken->Tok.isLiteral() || NextToken->isUnaryOperator())
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000851 return TT_BinaryOperator;
852
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000853 // It is very unlikely that we are going to find a pointer or reference type
854 // definition on the RHS of an assignment.
855 if (IsExpression)
856 return TT_BinaryOperator;
857
858 return TT_PointerOrReference;
859 }
860
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000861 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000862 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperda6f2252013-05-31 16:14:28 +0000863 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000864 return TT_UnaryOperator;
865
866 // Use heuristics to recognize unary operators.
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000867 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
868 tok::question, tok::colon, tok::kw_return,
869 tok::kw_case, tok::at, tok::l_brace))
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000870 return TT_UnaryOperator;
871
Nico Weberb76de882013-02-05 16:21:00 +0000872 // There can't be two consecutive binary operators.
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000873 if (PrevToken->Type == TT_BinaryOperator)
874 return TT_UnaryOperator;
875
876 // Fall back to marking the token as binary operator.
877 return TT_BinaryOperator;
878 }
879
880 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000881 TokenType determineIncrementUsage(const FormatToken &Tok) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000882 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperda6f2252013-05-31 16:14:28 +0000883 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000884 return TT_UnaryOperator;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000885 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000886 return TT_TrailingUnaryOperator;
887
888 return TT_UnaryOperator;
889 }
Daniel Jasperc697ad22013-02-06 10:05:46 +0000890
Daniel Jaspera628c982013-04-03 13:36:17 +0000891
Daniel Jasperc697ad22013-02-06 10:05:46 +0000892 SmallVector<Context, 8> Contexts;
893
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000894 const FormatStyle &Style;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000895 AnnotatedLine &Line;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000896 FormatToken *CurrentToken;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000897 bool KeywordVirtualFound;
Daniel Jasper6cdec7c2013-07-09 14:36:48 +0000898 bool AutoFound;
Nico Weber29f9dea2013-02-11 15:32:15 +0000899 IdentifierInfo &Ident_in;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000900};
901
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000902static int PrecedenceUnaryOperator = prec::PointerToMember + 1;
903static int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
904
Daniel Jasper400adc62013-02-08 15:28:42 +0000905/// \brief Parses binary expressions by inserting fake parenthesis based on
906/// operator precedence.
907class ExpressionParser {
908public:
Daniel Jasper6dcecb62013-06-06 09:11:58 +0000909 ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {
910 // Skip leading "}", e.g. in "} else if (...) {".
911 if (Current->is(tok::r_brace))
912 next();
913 }
Daniel Jasper400adc62013-02-08 15:28:42 +0000914
915 /// \brief Parse expressions with the given operatore precedence.
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000916 void parse(int Precedence = 0) {
Daniel Jasperf48b5ab2013-11-07 19:23:49 +0000917 // Skip 'return' and ObjC selector colons as they are not part of a binary
918 // expression.
919 while (Current &&
920 (Current->is(tok::kw_return) ||
921 (Current->is(tok::colon) && Current->Type == TT_ObjCMethodExpr)))
Daniel Jaspereabede62013-09-30 08:29:03 +0000922 next();
923
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000924 if (Current == NULL || Precedence > PrecedenceArrowAndPeriod)
Daniel Jasper0649d362013-08-23 15:14:03 +0000925 return;
926
Daniel Jasper2c611c02013-05-31 14:56:12 +0000927 // Conditional expressions need to be parsed separately for proper nesting.
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000928 if (Precedence == prec::Conditional) {
Daniel Jasper2c611c02013-05-31 14:56:12 +0000929 parseConditionalExpr();
930 return;
931 }
Daniel Jasper0649d362013-08-23 15:14:03 +0000932
933 // Parse unary operators, which all have a higher precedence than binary
934 // operators.
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000935 if (Precedence == PrecedenceUnaryOperator) {
Daniel Jasper0649d362013-08-23 15:14:03 +0000936 parseUnaryOperator();
Daniel Jasper400adc62013-02-08 15:28:42 +0000937 return;
Daniel Jasper0649d362013-08-23 15:14:03 +0000938 }
Daniel Jasper400adc62013-02-08 15:28:42 +0000939
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000940 FormatToken *Start = Current;
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000941 FormatToken *LatestOperator = NULL;
Daniel Jasper400adc62013-02-08 15:28:42 +0000942
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000943 while (Current) {
Daniel Jasper400adc62013-02-08 15:28:42 +0000944 // Consume operators with higher precedence.
Daniel Jasper6bee6822013-04-08 20:33:42 +0000945 parse(Precedence + 1);
Daniel Jasper400adc62013-02-08 15:28:42 +0000946
Daniel Jasper0649d362013-08-23 15:14:03 +0000947 int CurrentPrecedence = getCurrentPrecedence();
948
949 if (Current && Current->Type == TT_ObjCSelectorName &&
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000950 Precedence == CurrentPrecedence) {
951 if (LatestOperator)
952 addFakeParenthesis(Start, prec::Level(Precedence));
Daniel Jasper0649d362013-08-23 15:14:03 +0000953 Start = Current;
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000954 }
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000955
Daniel Jasper400adc62013-02-08 15:28:42 +0000956 // At the end of the line or when an operator with higher precedence is
957 // found, insert fake parenthesis and return.
Daniel Jasperc04baae2013-04-10 09:49:49 +0000958 if (Current == NULL || Current->closesScope() ||
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000959 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) {
960 if (LatestOperator) {
961 if (Precedence == PrecedenceArrowAndPeriod) {
962 LatestOperator->LastInChainOfCalls = true;
963 // Call expressions don't have a binary operator precedence.
964 addFakeParenthesis(Start, prec::Unknown);
965 } else {
966 addFakeParenthesis(Start, prec::Level(Precedence));
967 }
968 }
Daniel Jasper400adc62013-02-08 15:28:42 +0000969 return;
970 }
971
972 // Consume scopes: (), [], <> and {}
Daniel Jasperc04baae2013-04-10 09:49:49 +0000973 if (Current->opensScope()) {
974 while (Current && !Current->closesScope()) {
Daniel Jasper400adc62013-02-08 15:28:42 +0000975 next();
976 parse();
977 }
978 next();
979 } else {
980 // Operator found.
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000981 if (CurrentPrecedence == Precedence)
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000982 LatestOperator = Current;
Daniel Jasper400adc62013-02-08 15:28:42 +0000983
984 next();
985 }
986 }
987 }
988
989private:
Daniel Jasper0649d362013-08-23 15:14:03 +0000990 /// \brief Gets the precedence (+1) of the given token for binary operators
991 /// and other tokens that we treat like binary operators.
992 int getCurrentPrecedence() {
993 if (Current) {
994 if (Current->Type == TT_ConditionalExpr)
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000995 return prec::Conditional;
Daniel Jasperf48b5ab2013-11-07 19:23:49 +0000996 else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon ||
997 Current->Type == TT_ObjCSelectorName)
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000998 return 0;
Daniel Jasper9cc3e972014-02-07 10:09:46 +0000999 else if (Current->Type == TT_RangeBasedForLoopColon)
1000 return prec::Comma;
Daniel Jasper0649d362013-08-23 15:14:03 +00001001 else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001002 return Current->getPrecedence();
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001003 else if (Current->isOneOf(tok::period, tok::arrow))
1004 return PrecedenceArrowAndPeriod;
Daniel Jasper0649d362013-08-23 15:14:03 +00001005 }
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001006 return -1;
Daniel Jasper0649d362013-08-23 15:14:03 +00001007 }
1008
Daniel Jasper2c611c02013-05-31 14:56:12 +00001009 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
1010 Start->FakeLParens.push_back(Precedence);
Daniel Jasper562ecd42013-09-06 08:08:14 +00001011 if (Precedence > prec::Unknown)
1012 Start->StartsBinaryExpression = true;
1013 if (Current) {
Daniel Jasper2c611c02013-05-31 14:56:12 +00001014 ++Current->Previous->FakeRParens;
Daniel Jasper562ecd42013-09-06 08:08:14 +00001015 if (Precedence > prec::Unknown)
1016 Current->Previous->EndsBinaryExpression = true;
1017 }
Daniel Jasper2c611c02013-05-31 14:56:12 +00001018 }
1019
Daniel Jasper0649d362013-08-23 15:14:03 +00001020 /// \brief Parse unary operator expressions and surround them with fake
1021 /// parentheses if appropriate.
1022 void parseUnaryOperator() {
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001023 if (Current == NULL || Current->Type != TT_UnaryOperator) {
1024 parse(PrecedenceArrowAndPeriod);
Daniel Jasper0649d362013-08-23 15:14:03 +00001025 return;
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001026 }
Daniel Jasper0649d362013-08-23 15:14:03 +00001027
1028 FormatToken *Start = Current;
1029 next();
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001030 parseUnaryOperator();
Daniel Jasper0649d362013-08-23 15:14:03 +00001031
Daniel Jasper0649d362013-08-23 15:14:03 +00001032 // The actual precedence doesn't matter.
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001033 addFakeParenthesis(Start, prec::Unknown);
Daniel Jasper0649d362013-08-23 15:14:03 +00001034 }
1035
Daniel Jasper2c611c02013-05-31 14:56:12 +00001036 void parseConditionalExpr() {
1037 FormatToken *Start = Current;
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001038 parse(prec::LogicalOr);
Daniel Jasper2c611c02013-05-31 14:56:12 +00001039 if (!Current || !Current->is(tok::question))
1040 return;
1041 next();
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001042 parse(prec::LogicalOr);
Daniel Jasper2c611c02013-05-31 14:56:12 +00001043 if (!Current || Current->Type != TT_ConditionalExpr)
1044 return;
1045 next();
1046 parseConditionalExpr();
1047 addFakeParenthesis(Start, prec::Conditional);
1048 }
1049
Daniel Jasper400adc62013-02-08 15:28:42 +00001050 void next() {
Alexander Kornienkoafaa8f52013-06-17 13:19:53 +00001051 if (Current)
1052 Current = Current->Next;
1053 while (Current && Current->isTrailingComment())
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001054 Current = Current->Next;
Daniel Jasper400adc62013-02-08 15:28:42 +00001055 }
1056
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001057 FormatToken *Current;
Daniel Jasper400adc62013-02-08 15:28:42 +00001058};
1059
Craig Topper318ed7c2013-07-01 04:03:19 +00001060} // end anonymous namespace
1061
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001062void
1063TokenAnnotator::setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines) {
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001064 const AnnotatedLine *NextNonCommentLine = NULL;
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001065 for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
1066 E = Lines.rend();
1067 I != E; ++I) {
1068 if (NextNonCommentLine && (*I)->First->is(tok::comment) &&
1069 (*I)->First->Next == NULL)
1070 (*I)->Level = NextNonCommentLine->Level;
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001071 else
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001072 NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : NULL;
1073
1074 setCommentLineLevels((*I)->Children);
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001075 }
1076}
1077
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001078void TokenAnnotator::annotate(AnnotatedLine &Line) {
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001079 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1080 E = Line.Children.end();
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001081 I != E; ++I) {
1082 annotate(**I);
1083 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +00001084 AnnotatingParser Parser(Style, Line, Ident_in);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001085 Line.Type = Parser.parseLine();
1086 if (Line.Type == LT_Invalid)
1087 return;
1088
Daniel Jasper400adc62013-02-08 15:28:42 +00001089 ExpressionParser ExprParser(Line);
1090 ExprParser.parse();
1091
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001092 if (Line.First->Type == TT_ObjCMethodSpecifier)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001093 Line.Type = LT_ObjCMethodDecl;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001094 else if (Line.First->Type == TT_ObjCDecl)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001095 Line.Type = LT_ObjCDecl;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001096 else if (Line.First->Type == TT_ObjCProperty)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001097 Line.Type = LT_ObjCProperty;
1098
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001099 Line.First->SpacesRequiredBefore = 1;
1100 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001101}
1102
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001103void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
Alexander Kornienko39856b72013-09-10 09:38:25 +00001104 Line.First->TotalLength =
1105 Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001106 if (!Line.First->Next)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001107 return;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001108 FormatToken *Current = Line.First->Next;
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001109 bool InFunctionDecl = Line.MightBeFunctionDecl;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001110 while (Current != NULL) {
Daniel Jasper5a611392013-12-19 21:41:37 +00001111 if (Current->Type == TT_LineComment) {
Daniel Jasper84a12e12014-03-10 15:06:25 +00001112 if (Current->Previous->BlockKind == BK_BracedInit &&
1113 Current->Previous->opensScope())
Daniel Jasper5a611392013-12-19 21:41:37 +00001114 Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1;
1115 else
1116 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001117 } else if (Current->SpacesRequiredBefore == 0 &&
Daniel Jasper5a611392013-12-19 21:41:37 +00001118 spaceRequiredBefore(Line, *Current)) {
Alexander Kornienko384b40b2013-10-11 21:43:05 +00001119 Current->SpacesRequiredBefore = 1;
Daniel Jasper5a611392013-12-19 21:41:37 +00001120 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001121
Daniel Jasperfb81b092013-09-17 09:52:48 +00001122 Current->MustBreakBefore =
1123 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
1124
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001125 Current->CanBreakBefore =
1126 Current->MustBreakBefore || canBreakBefore(Line, *Current);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001127 if (Current->MustBreakBefore || !Current->Children.empty() ||
Alexander Kornienko39856b72013-09-10 09:38:25 +00001128 Current->IsMultiline)
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001129 Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001130 else
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +00001131 Current->TotalLength = Current->Previous->TotalLength +
Alexander Kornienko39856b72013-09-10 09:38:25 +00001132 Current->ColumnWidth +
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +00001133 Current->SpacesRequiredBefore;
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001134
1135 if (Current->Type == TT_CtorInitializerColon)
1136 InFunctionDecl = false;
1137
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001138 // FIXME: Only calculate this if CanBreakBefore is true once static
1139 // initializers etc. are sorted out.
1140 // FIXME: Move magic numbers to a better place.
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001141 Current->SplitPenalty = 20 * Current->BindingStrength +
1142 splitPenalty(Line, *Current, InFunctionDecl);
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001143
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001144 Current = Current->Next;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001145 }
Daniel Jasper6bee6822013-04-08 20:33:42 +00001146
Manuel Klimek4fe43002013-05-22 12:51:29 +00001147 calculateUnbreakableTailLengths(Line);
Daniel Jasper8de9ed02013-08-22 15:00:41 +00001148 for (Current = Line.First; Current != NULL; Current = Current->Next) {
1149 if (Current->Role)
1150 Current->Role->precomputeFormattingInfos(Current);
1151 }
1152
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001153 DEBUG({ printDebugInfo(Line); });
1154
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001155 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1156 E = Line.Children.end();
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001157 I != E; ++I) {
1158 calculateFormattingInformation(**I);
1159 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001160}
1161
Manuel Klimek4fe43002013-05-22 12:51:29 +00001162void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1163 unsigned UnbreakableTailLength = 0;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001164 FormatToken *Current = Line.Last;
Manuel Klimek4fe43002013-05-22 12:51:29 +00001165 while (Current != NULL) {
1166 Current->UnbreakableTailLength = UnbreakableTailLength;
1167 if (Current->CanBreakBefore ||
1168 Current->isOneOf(tok::comment, tok::string_literal)) {
1169 UnbreakableTailLength = 0;
1170 } else {
1171 UnbreakableTailLength +=
Alexander Kornienko39856b72013-09-10 09:38:25 +00001172 Current->ColumnWidth + Current->SpacesRequiredBefore;
Manuel Klimek4fe43002013-05-22 12:51:29 +00001173 }
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001174 Current = Current->Previous;
Manuel Klimek4fe43002013-05-22 12:51:29 +00001175 }
1176}
1177
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001178unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001179 const FormatToken &Tok,
1180 bool InFunctionDecl) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001181 const FormatToken &Left = *Tok.Previous;
1182 const FormatToken &Right = Tok;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001183
Daniel Jasperbca4bbe2013-05-28 11:30:49 +00001184 if (Left.is(tok::semi))
1185 return 0;
1186 if (Left.is(tok::comma))
1187 return 1;
Daniel Jasper7052ce62014-01-19 09:04:08 +00001188 if (Right.is(tok::l_square)) {
1189 if (Style.Language == FormatStyle::LK_Proto)
1190 return 1;
1191 if (Right.Type != TT_ObjCMethodExpr)
1192 return 250;
1193 }
Daniel Jasper6331da02013-07-09 07:43:55 +00001194 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001195 if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
Daniel Jasper26d1b1d2013-02-24 18:54:32 +00001196 return 3;
Daniel Jasper40db06a2013-07-11 12:34:23 +00001197 if (Left.Type == TT_StartOfName)
1198 return 20;
Daniel Jasper63af7c42013-12-09 14:40:19 +00001199 if (InFunctionDecl && Right.NestingLevel == 0)
Daniel Jasper26d1b1d2013-02-24 18:54:32 +00001200 return Style.PenaltyReturnTypeOnItsOwnLine;
Daniel Jasper53643062013-08-19 10:16:18 +00001201 return 200;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +00001202 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001203 if (Left.is(tok::equal) && Right.is(tok::l_brace))
1204 return 150;
Daniel Jasper1bc1b502013-07-05 07:58:34 +00001205 if (Left.Type == TT_CastRParen)
1206 return 100;
Daniel Jasper215d6c82014-01-22 08:04:52 +00001207 if (Left.is(tok::coloncolon) ||
1208 (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001209 return 500;
Daniel Jasper83193602013-04-05 17:22:09 +00001210 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1211 return 5000;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001212
Daniel Jaspereead02b2013-02-14 08:42:54 +00001213 if (Left.Type == TT_RangeBasedForLoopColon ||
1214 Left.Type == TT_InheritanceColon)
Daniel Jasper16b35622013-02-26 13:18:08 +00001215 return 2;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001216
Daniel Jasper4c6e0052013-08-27 14:24:43 +00001217 if (Right.isMemberAccess()) {
Daniel Jasper4d7a97a2014-01-10 08:40:17 +00001218 if (Left.is(tok::r_paren) && Left.MatchingParen &&
Daniel Jasper36c28ce2013-09-06 08:54:24 +00001219 Left.MatchingParen->ParameterCount > 0)
Daniel Jasper70bc8742013-02-26 13:59:14 +00001220 return 20; // Should be smaller than breaking at a nested comma.
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001221 return 150;
1222 }
1223
Daniel Jasper43e6a282013-12-16 15:01:54 +00001224 if (Right.Type == TT_TrailingAnnotation && Right.Next &&
1225 Right.Next->isNot(tok::l_paren)) {
Daniel Jasper5550de62014-02-17 07:57:46 +00001226 // Generally, breaking before a trailing annotation is bad unless it is
1227 // function-like. It seems to be especially preferable to keep standard
1228 // annotations (i.e. "const", "final" and "override") on the same line.
Daniel Jasper43e6a282013-12-16 15:01:54 +00001229 // Use a slightly higher penalty after ")" so that annotations like
1230 // "const override" are kept together.
Daniel Jasper5550de62014-02-17 07:57:46 +00001231 bool is_standard_annotation = Right.is(tok::kw_const) ||
1232 Right.TokenText == "override" ||
1233 Right.TokenText == "final";
1234 return (Left.is(tok::r_paren) ? 100 : 120) +
1235 (is_standard_annotation ? 50 : 0);
Daniel Jasper43e6a282013-12-16 15:01:54 +00001236 }
Daniel Jasper13c37b32013-05-22 08:28:26 +00001237
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001238 // In for-loops, prefer breaking at ',' and ';'.
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001239 if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
Daniel Jasper37905f72013-02-21 15:00:29 +00001240 return 4;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001241
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001242 // In Objective-C method expressions, prefer breaking before "param:" over
1243 // breaking after it.
Daniel Jasper1ac3e052013-02-05 10:07:47 +00001244 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001245 return 0;
Daniel Jasper1ac3e052013-02-05 10:07:47 +00001246 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
Daniel Jasper4bf0d802013-11-23 14:27:27 +00001247 return Line.MightBeFunctionDecl ? 50 : 500;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001248
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001249 if (Left.is(tok::l_paren) && InFunctionDecl)
Daniel Jasper6728fc12013-04-11 14:29:13 +00001250 return 100;
Daniel Jasper126153a2013-12-27 06:39:56 +00001251 if (Left.is(tok::equal) && InFunctionDecl)
1252 return 110;
Daniel Jasperc04baae2013-04-10 09:49:49 +00001253 if (Left.opensScope())
Daniel Jasper33b909c2013-10-25 14:29:37 +00001254 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
1255 : 19;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001256
Daniel Jasperba9ddb62013-02-06 21:04:05 +00001257 if (Right.is(tok::lessless)) {
1258 if (Left.is(tok::string_literal)) {
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001259 StringRef Content = Left.TokenText;
Daniel Jasper0b1f76b2013-09-29 12:02:57 +00001260 if (Content.startswith("\""))
1261 Content = Content.drop_front(1);
1262 if (Content.endswith("\""))
1263 Content = Content.drop_back(1);
1264 Content = Content.trim();
Daniel Jasperf38a0ac2013-03-14 14:00:17 +00001265 if (Content.size() > 1 &&
1266 (Content.back() == ':' || Content.back() == '='))
Daniel Jasperfa21c072013-07-15 14:33:14 +00001267 return 25;
Daniel Jasperba9ddb62013-02-06 21:04:05 +00001268 }
Daniel Jasper49a94482013-07-15 15:04:42 +00001269 return 1; // Breaking at a << is really cheap.
Daniel Jasperba9ddb62013-02-06 21:04:05 +00001270 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001271 if (Left.Type == TT_ConditionalExpr)
Daniel Jasper70bc8742013-02-26 13:59:14 +00001272 return prec::Conditional;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001273 prec::Level Level = Left.getPrecedence();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001274
1275 if (Level != prec::Unknown)
1276 return Level;
Daniel Jasperf9a84b52013-03-01 16:48:32 +00001277
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001278 return 3;
1279}
1280
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001281bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001282 const FormatToken &Left,
1283 const FormatToken &Right) {
Daniel Jasper7052ce62014-01-19 09:04:08 +00001284 if (Style.Language == FormatStyle::LK_Proto) {
Daniel Jasper929b1db2014-01-20 16:47:22 +00001285 if (Right.is(tok::l_paren) &&
1286 (Left.TokenText == "returns" || Left.TokenText == "option"))
Daniel Jasper7052ce62014-01-19 09:04:08 +00001287 return true;
1288 }
Daniel Jaspere9beea22014-01-28 15:20:33 +00001289 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
1290 Left.Tok.getObjCKeywordID() == tok::objc_property)
1291 return true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001292 if (Right.is(tok::hashhash))
1293 return Left.is(tok::hash);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001294 if (Left.isOneOf(tok::hashhash, tok::hash))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001295 return Right.is(tok::hash);
Daniel Jasperb55acad2013-08-20 12:36:34 +00001296 if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1297 return Style.SpaceInEmptyParentheses;
1298 if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
Daniel Jasperf110e202013-08-21 08:39:01 +00001299 return (Right.Type == TT_CastRParen ||
1300 (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen))
Daniel Jasperb55acad2013-08-20 12:36:34 +00001301 ? Style.SpacesInCStyleCastParentheses
1302 : Style.SpacesInParentheses;
Daniel Jasperdd978ae2013-10-29 14:52:02 +00001303 if (Style.SpacesInAngles &&
1304 ((Left.Type == TT_TemplateOpener) != (Right.Type == TT_TemplateCloser)))
1305 return true;
Daniel Jasperb55acad2013-08-20 12:36:34 +00001306 if (Right.isOneOf(tok::semi, tok::comma))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001307 return false;
1308 if (Right.is(tok::less) &&
1309 (Left.is(tok::kw_template) ||
1310 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1311 return true;
1312 if (Left.is(tok::arrow) || Right.is(tok::arrow))
1313 return false;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001314 if (Left.isOneOf(tok::exclaim, tok::tilde))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001315 return false;
1316 if (Left.is(tok::at) &&
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001317 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1318 tok::numeric_constant, tok::l_paren, tok::l_brace,
1319 tok::kw_true, tok::kw_false))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001320 return false;
1321 if (Left.is(tok::coloncolon))
1322 return false;
Daniel Jasper7620b662014-01-08 15:41:13 +00001323 if (Right.is(tok::coloncolon) && Left.isNot(tok::l_brace))
Daniel Jasperfba84ff2013-10-12 05:16:06 +00001324 return (Left.is(tok::less) && Style.Standard == FormatStyle::LS_Cpp03) ||
1325 !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren,
1326 tok::r_paren, tok::less);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001327 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001328 return false;
Daniel Jasperbafa6b72013-07-01 09:47:25 +00001329 if (Right.is(tok::ellipsis))
Daniel Jasper2d0cd492013-10-20 16:56:16 +00001330 return Left.Tok.isLiteral();
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001331 if (Left.is(tok::l_square) && Right.is(tok::amp))
1332 return false;
Alexander Kornienkoa5151272013-03-12 16:28:18 +00001333 if (Right.Type == TT_PointerOrReference)
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001334 return Left.Tok.isLiteral() ||
Alexander Kornienkoa5151272013-03-12 16:28:18 +00001335 ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
1336 !Style.PointerBindsToType);
Daniel Jasper4d03d3b2013-05-28 15:27:10 +00001337 if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
Daniel Jaspercfda5172013-05-08 14:58:20 +00001338 (Left.Type != TT_PointerOrReference || Style.PointerBindsToType))
1339 return true;
Alexander Kornienkoa5151272013-03-12 16:28:18 +00001340 if (Left.Type == TT_PointerOrReference)
Daniel Jasper022612d2013-07-01 09:34:09 +00001341 return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
Daniel Jasperb8914dd2013-03-20 09:53:18 +00001342 ((Right.Type != TT_PointerOrReference) &&
Daniel Jasper6e42b1e2013-04-01 17:13:26 +00001343 Right.isNot(tok::l_paren) && Style.PointerBindsToType &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001344 Left.Previous &&
1345 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001346 if (Right.is(tok::star) && Left.is(tok::l_paren))
1347 return false;
Nico Weber2a726b62013-02-10 02:08:05 +00001348 if (Left.is(tok::l_square))
Daniel Jasper1db6c382013-10-22 15:30:28 +00001349 return Left.Type == TT_ArrayInitializerLSquare &&
Daniel Jasperb2e10a52014-01-15 15:09:08 +00001350 Style.SpacesInContainerLiterals && Right.isNot(tok::r_square);
Nico Weber2a726b62013-02-10 02:08:05 +00001351 if (Right.is(tok::r_square))
Daniel Jasperb2e10a52014-01-15 15:09:08 +00001352 return Right.MatchingParen && Style.SpacesInContainerLiterals &&
Daniel Jasper1db6c382013-10-22 15:30:28 +00001353 Right.MatchingParen->Type == TT_ArrayInitializerLSquare;
Daniel Jasper8ddfa842013-08-30 10:36:58 +00001354 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr &&
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001355 Right.Type != TT_LambdaLSquare && Left.isNot(tok::numeric_constant))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001356 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001357 if (Left.is(tok::colon))
1358 return Left.Type != TT_ObjCMethodExpr;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001359 if (Right.is(tok::l_paren)) {
Daniel Jasper559b63c2014-01-28 20:13:43 +00001360 if (Left.is(tok::r_paren) && Left.Type == TT_AttributeParen)
Daniel Jasperee6d6502013-07-17 20:25:02 +00001361 return true;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001362 return Line.Type == LT_ObjCDecl ||
Daniel Jasperf110e202013-08-21 08:39:01 +00001363 Left.isOneOf(tok::kw_return, tok::kw_new, tok::kw_delete,
1364 tok::semi) ||
Alexander Kornienkofdca83d2013-12-10 10:18:34 +00001365 (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
Daniel Jasperf110e202013-08-21 08:39:01 +00001366 Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
Alexander Kornienkofdca83d2013-12-10 10:18:34 +00001367 tok::kw_catch)) ||
1368 (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
1369 Left.isOneOf(tok::identifier, tok::kw___attribute) &&
1370 Line.Type != LT_PreprocessorDirective);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001371 }
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001372 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001373 return false;
1374 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001375 return !Left.Children.empty(); // No spaces in "{}".
Daniel Jasper1a148b42014-01-05 13:23:23 +00001376 if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) ||
1377 (Right.is(tok::r_brace) && Right.MatchingParen &&
1378 Right.MatchingParen->BlockKind != BK_Block))
Daniel Jasper6ab54682013-07-16 18:22:10 +00001379 return !Style.Cpp11BracedListStyle;
Daniel Jasper40bc7462013-11-23 14:51:47 +00001380 if (Left.Type == TT_BlockComment && Left.TokenText.endswith("=*/"))
1381 return false;
Daniel Jasper5bd0b9e2013-05-23 18:05:18 +00001382 if (Right.Type == TT_UnaryOperator)
1383 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1384 (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
Daniel Jasperf632f692013-05-23 21:35:49 +00001385 if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) &&
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001386 Right.is(tok::l_brace) && Right.getNextNonComment() &&
1387 Right.BlockKind != BK_Block)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001388 return false;
Daniel Jasperbca4bbe2013-05-28 11:30:49 +00001389 if (Left.is(tok::period) || Right.is(tok::period))
1390 return false;
Alexander Kornienkod8d47fa2013-09-10 13:41:43 +00001391 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
1392 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001393 return true;
1394}
1395
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001396bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001397 const FormatToken &Tok) {
1398 if (Tok.Tok.getIdentifierInfo() && Tok.Previous->Tok.getIdentifierInfo())
Daniel Jasper35d2dc72013-02-11 08:01:18 +00001399 return true; // Never ever merge two identifiers.
Daniel Jasper877615c2013-10-11 19:45:02 +00001400 if (Tok.Previous->Type == TT_ImplicitStringLiteral)
1401 return Tok.WhitespaceRange.getBegin() != Tok.WhitespaceRange.getEnd();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001402 if (Line.Type == LT_ObjCMethodDecl) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001403 if (Tok.Previous->Type == TT_ObjCMethodSpecifier)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001404 return true;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001405 if (Tok.Previous->is(tok::r_paren) && Tok.is(tok::identifier))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001406 // Don't space between ')' and <id>
1407 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001408 }
1409 if (Line.Type == LT_ObjCProperty &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001410 (Tok.is(tok::equal) || Tok.Previous->is(tok::equal)))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001411 return false;
1412
Daniel Jasper6cdec7c2013-07-09 14:36:48 +00001413 if (Tok.Type == TT_TrailingReturnArrow ||
1414 Tok.Previous->Type == TT_TrailingReturnArrow)
1415 return true;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001416 if (Tok.Previous->is(tok::comma))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001417 return true;
Daniel Jasperff6c3a92013-02-28 13:40:17 +00001418 if (Tok.is(tok::comma))
1419 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001420 if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
1421 return true;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001422 if (Tok.Previous->Tok.is(tok::kw_operator))
Daniel Jasperedc5f092013-10-29 12:24:23 +00001423 return Tok.is(tok::coloncolon);
Daniel Jasper35d2dc72013-02-11 08:01:18 +00001424 if (Tok.Type == TT_OverloadedOperatorLParen)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001425 return false;
1426 if (Tok.is(tok::colon))
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001427 return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Daniel Jasperb1ae7342013-08-01 22:05:00 +00001428 Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr &&
Daniel Jasperb2e10a52014-01-15 15:09:08 +00001429 !Tok.Previous->is(tok::question) &&
1430 (Tok.Type != TT_DictLiteral || Style.SpacesInContainerLiterals);
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001431 if (Tok.Previous->Type == TT_UnaryOperator ||
1432 Tok.Previous->Type == TT_CastRParen)
Daniel Jasperff974ab2014-01-25 09:16:02 +00001433 return Tok.Type == TT_BinaryOperator;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001434 if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) {
Daniel Jasper400adc62013-02-08 15:28:42 +00001435 return Tok.Type == TT_TemplateCloser &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001436 Tok.Previous->Type == TT_TemplateCloser &&
Daniel Jasperdd978ae2013-10-29 14:52:02 +00001437 (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001438 }
Alexander Kornienko674be0a2013-03-20 16:41:56 +00001439 if (Tok.isOneOf(tok::arrowstar, tok::periodstar) ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001440 Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar))
Daniel Jasperff6c3a92013-02-28 13:40:17 +00001441 return false;
Daniel Jasperd94bff32013-09-25 15:15:02 +00001442 if (!Style.SpaceBeforeAssignmentOperators &&
1443 Tok.getPrecedence() == prec::Assignment)
1444 return false;
Daniel Jasper9613c812013-08-07 16:29:23 +00001445 if ((Tok.Type == TT_BinaryOperator && !Tok.Previous->is(tok::l_paren)) ||
1446 Tok.Previous->Type == TT_BinaryOperator)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001447 return true;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001448 if (Tok.Previous->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001449 return false;
Daniel Jaspered8f1c62013-08-28 08:24:04 +00001450 if (Tok.is(tok::less) && Tok.Previous->isNot(tok::l_paren) &&
1451 Line.First->is(tok::hash))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001452 return true;
1453 if (Tok.Type == TT_TrailingUnaryOperator)
1454 return false;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001455 return spaceRequiredBetween(Line, *Tok.Previous, Tok);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001456}
1457
Daniel Jasperfb81b092013-09-17 09:52:48 +00001458bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
1459 const FormatToken &Right) {
1460 if (Right.is(tok::comment)) {
Daniel Jasper5a611392013-12-19 21:41:37 +00001461 return Right.Previous->BlockKind != BK_BracedInit &&
Daniel Jasperf6c7c182014-01-13 14:10:04 +00001462 Right.Previous->Type != TT_CtorInitializerColon &&
Daniel Jasper5a611392013-12-19 21:41:37 +00001463 Right.NewlinesBefore > 0;
Daniel Jasperfb81b092013-09-17 09:52:48 +00001464 } else if (Right.Previous->isTrailingComment() ||
Daniel Jasper04b6a082013-12-20 06:22:01 +00001465 (Right.isStringLiteral() && Right.Previous->isStringLiteral())) {
Daniel Jasperfb81b092013-09-17 09:52:48 +00001466 return true;
1467 } else if (Right.Previous->IsUnterminatedLiteral) {
1468 return true;
1469 } else if (Right.is(tok::lessless) && Right.Next &&
1470 Right.Previous->is(tok::string_literal) &&
1471 Right.Next->is(tok::string_literal)) {
1472 return true;
1473 } else if (Right.Previous->ClosesTemplateDeclaration &&
1474 Right.Previous->MatchingParen &&
Daniel Jasper63af7c42013-12-09 14:40:19 +00001475 Right.Previous->MatchingParen->NestingLevel == 0 &&
Daniel Jasperfb81b092013-09-17 09:52:48 +00001476 Style.AlwaysBreakTemplateDeclarations) {
Daniel Jasperfb81b092013-09-17 09:52:48 +00001477 return true;
Alexander Kornienkoa594ba82013-12-16 14:35:51 +00001478 } else if ((Right.Type == TT_CtorInitializerComma ||
1479 Right.Type == TT_CtorInitializerColon) &&
Daniel Jasperec01cd62013-10-08 05:11:18 +00001480 Style.BreakConstructorInitializersBeforeComma &&
1481 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
Daniel Jasperfb81b092013-09-17 09:52:48 +00001482 return true;
Daniel Jasperfb81b092013-09-17 09:52:48 +00001483 } else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001484 return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1485 Style.BreakBeforeBraces == FormatStyle::BS_GNU;
Daniel Jasperc39b56f2013-12-16 07:23:08 +00001486 } else if (Right.is(tok::string_literal) &&
1487 Right.TokenText.startswith("R\"")) {
1488 // Raw string literals are special wrt. line breaks. The author has made a
1489 // deliberate choice and might have aligned the contents of the string
1490 // literal accordingly. Thus, we try keep existing line breaks.
1491 return Right.NewlinesBefore > 0;
Daniel Jasper6e58fee2014-01-29 18:43:40 +00001492 } else if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 &&
Daniel Jasper7052ce62014-01-19 09:04:08 +00001493 Style.Language == FormatStyle::LK_Proto) {
1494 // Don't enums onto single lines in protocol buffers.
1495 return true;
Daniel Jasperfb81b092013-09-17 09:52:48 +00001496 }
1497 return false;
1498}
1499
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001500bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001501 const FormatToken &Right) {
1502 const FormatToken &Left = *Right.Previous;
Daniel Jasper4bf0d802013-11-23 14:27:27 +00001503 if (Left.is(tok::at))
1504 return false;
Daniel Jasper6331da02013-07-09 07:43:55 +00001505 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001506 return true;
Daniel Jasper165b29e2013-11-08 00:57:11 +00001507 if (Right.isTrailingComment())
1508 // We rely on MustBreakBefore being set correctly here as we should not
1509 // change the "binding" behavior of a comment.
Daniel Jasper5a611392013-12-19 21:41:37 +00001510 // The first comment in a braced lists is always interpreted as belonging to
1511 // the first list element. Otherwise, it should be placed outside of the
1512 // list.
1513 return Left.BlockKind == BK_BracedInit;
Daniel Jasper165b29e2013-11-08 00:57:11 +00001514 if (Left.is(tok::question) && Right.is(tok::colon))
1515 return false;
1516 if (Right.Type == TT_ConditionalExpr || Right.is(tok::question))
1517 return Style.BreakBeforeTernaryOperators;
1518 if (Left.Type == TT_ConditionalExpr || Left.is(tok::question))
1519 return !Style.BreakBeforeTernaryOperators;
Nico Weberced7d412013-05-26 05:39:26 +00001520 if (Right.is(tok::colon) &&
Daniel Jasperb596fb22013-10-24 10:31:50 +00001521 (Right.Type == TT_DictLiteral || Right.Type == TT_ObjCMethodExpr))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001522 return false;
Daniel Jasper3a122c02014-02-14 18:22:40 +00001523 if (Right.Type == TT_InheritanceColon)
1524 return true;
Nico Weberced7d412013-05-26 05:39:26 +00001525 if (Left.is(tok::colon) &&
Daniel Jasperb596fb22013-10-24 10:31:50 +00001526 (Left.Type == TT_DictLiteral || Left.Type == TT_ObjCMethodExpr))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001527 return true;
Daniel Jasper1ac3e052013-02-05 10:07:47 +00001528 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001529 return true;
Daniel Jasper9688ff12013-08-01 13:46:58 +00001530 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
1531 return true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001532 if (Left.ClosesTemplateDeclaration)
1533 return true;
Daniel Jaspereead02b2013-02-14 08:42:54 +00001534 if (Right.Type == TT_RangeBasedForLoopColon ||
Daniel Jasperd215b8b2013-08-28 07:27:35 +00001535 Right.Type == TT_OverloadedOperatorLParen ||
1536 Right.Type == TT_OverloadedOperator)
Daniel Jaspereead02b2013-02-14 08:42:54 +00001537 return false;
Daniel Jaspera61aefb2013-05-06 06:45:09 +00001538 if (Left.Type == TT_RangeBasedForLoopColon)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001539 return true;
Daniel Jasper37905f72013-02-21 15:00:29 +00001540 if (Right.Type == TT_RangeBasedForLoopColon)
1541 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001542 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
Daniel Jasper165b29e2013-11-08 00:57:11 +00001543 Left.Type == TT_UnaryOperator || Left.is(tok::kw_operator))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001544 return false;
1545 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
1546 return false;
Daniel Jasper559b63c2014-01-28 20:13:43 +00001547 if (Left.is(tok::l_paren) && Left.Type == TT_AttributeParen)
1548 return false;
1549 if (Left.is(tok::l_paren) && Left.Previous &&
1550 (Left.Previous->Type == TT_BinaryOperator ||
1551 Left.Previous->Type == TT_CastRParen))
1552 return false;
Daniel Jasper98857842013-10-30 13:54:53 +00001553 if (Right.Type == TT_ImplicitStringLiteral)
1554 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001555
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001556 if (Right.is(tok::r_paren) || Right.Type == TT_TemplateCloser)
1557 return false;
1558
Daniel Jasper13c37b32013-05-22 08:28:26 +00001559 // We only break before r_brace if there was a corresponding break before
1560 // the l_brace, which is tracked by BreakBeforeClosingBrace.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001561 if (Right.is(tok::r_brace))
1562 return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
Daniel Jasper13c37b32013-05-22 08:28:26 +00001563
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001564 // Allow breaking after a trailing 'const', e.g. after a method declaration,
1565 // unless it is follow by ';', '{' or '='.
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001566 if (Left.is(tok::kw_const) && Left.Previous != NULL &&
1567 Left.Previous->is(tok::r_paren))
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001568 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001569
Daniel Jasperbf4755b2013-03-14 09:50:46 +00001570 if (Right.is(tok::kw___attribute))
1571 return true;
1572
Daniel Jasperaf5ba0e2013-02-23 07:46:38 +00001573 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
1574 return true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +00001575
1576 if (Left.Type == TT_CtorInitializerComma &&
1577 Style.BreakConstructorInitializersBeforeComma)
1578 return false;
Daniel Jasperec01cd62013-10-08 05:11:18 +00001579 if (Right.Type == TT_CtorInitializerComma &&
1580 Style.BreakConstructorInitializersBeforeComma)
1581 return true;
Daniel Jasper446d1cd2013-11-23 14:45:49 +00001582 if (Right.Type == TT_BinaryOperator && Style.BreakBeforeBinaryOperators)
Daniel Jaspere33d4af2013-07-26 16:56:36 +00001583 return true;
Daniel Jasper0de8efa2013-09-17 08:15:46 +00001584 if (Left.is(tok::greater) && Right.is(tok::greater) &&
1585 Left.Type != TT_TemplateCloser)
1586 return false;
Daniel Jasper1db6c382013-10-22 15:30:28 +00001587 if (Left.Type == TT_ArrayInitializerLSquare)
1588 return true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +00001589 return (Left.isBinaryOperator() && Left.isNot(tok::lessless) &&
1590 !Style.BreakBeforeBinaryOperators) ||
Daniel Jasper83193602013-04-05 17:22:09 +00001591 Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
1592 tok::kw_class, tok::kw_struct) ||
Daniel Jasper1db6c382013-10-22 15:30:28 +00001593 Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon,
1594 tok::l_square, tok::at) ||
Daniel Jasper1bc1b502013-07-05 07:58:34 +00001595 (Left.is(tok::r_paren) &&
Daniel Jasper559b63c2014-01-28 20:13:43 +00001596 Right.isOneOf(tok::identifier, tok::kw_const)) ||
Daniel Jasper1db6c382013-10-22 15:30:28 +00001597 (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001598}
1599
Daniel Jasper6bee6822013-04-08 20:33:42 +00001600void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
1601 llvm::errs() << "AnnotatedTokens:\n";
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001602 const FormatToken *Tok = Line.First;
Daniel Jasper6bee6822013-04-08 20:33:42 +00001603 while (Tok) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001604 llvm::errs() << " M=" << Tok->MustBreakBefore
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001605 << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
1606 << " S=" << Tok->SpacesRequiredBefore
1607 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
Manuel Klimek71814b42013-10-11 21:25:45 +00001608 << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
1609 << " FakeLParens=";
Daniel Jasper6bee6822013-04-08 20:33:42 +00001610 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
1611 llvm::errs() << Tok->FakeLParens[i] << "/";
1612 llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
Manuel Klimek71814b42013-10-11 21:25:45 +00001613 if (Tok->Next == NULL)
1614 assert(Tok == Line.Last);
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001615 Tok = Tok->Next;
Daniel Jasper6bee6822013-04-08 20:33:42 +00001616 }
1617 llvm::errs() << "----\n";
1618}
1619
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001620} // namespace format
1621} // namespace clang