blob: 69e558571f903b8b7b07b701dc22edc7c1d6a8e4 [file] [log] [blame]
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001//===--- TokenAnnotator.cpp - Format C++ code -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file implements a token annotator, i.e. creates
12/// \c AnnotatedTokens out of \c FormatTokens with required extra information.
13///
14//===----------------------------------------------------------------------===//
15
16#include "TokenAnnotator.h"
17#include "clang/Basic/SourceManager.h"
Daniel Jasperbf71ba22013-04-08 20:33:42 +000018#include "llvm/Support/Debug.h"
Daniel Jasper32d28ee2013-01-29 21:01:14 +000019
20namespace clang {
21namespace format {
22
Craig Topper14e66492013-07-01 04:03:19 +000023namespace {
24
Daniel Jasper32d28ee2013-01-29 21:01:14 +000025/// \brief A parser that gathers additional information about tokens.
26///
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +000027/// The \c TokenAnnotator tries to match parenthesis and square brakets and
Daniel Jasper32d28ee2013-01-29 21:01:14 +000028/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
29/// into template parameter lists.
30class AnnotatingParser {
31public:
Daniel Jasperd4a03db2013-08-22 15:00:41 +000032 AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
33 IdentifierInfo &Ident_in)
34 : Style(Style), Line(Line), CurrentToken(Line.First),
Daniel Jasper59875ac2013-11-07 17:43:07 +000035 KeywordVirtualFound(false), AutoFound(false), Ident_in(Ident_in) {
Nico Weber27268772013-06-26 00:30:14 +000036 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
Daniel Jasper32d28ee2013-01-29 21:01:14 +000037 }
38
Nico Weber95e8e462013-02-12 16:17:07 +000039private:
Daniel Jasper32d28ee2013-01-29 21:01:14 +000040 bool parseAngle() {
41 if (CurrentToken == NULL)
42 return false;
Daniel Jasper923ebef2013-03-14 13:45:21 +000043 ScopedContextCreator ContextCreator(*this, tok::less, 10);
Manuel Klimekb3987012013-05-29 14:47:47 +000044 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper4e778092013-02-06 10:05:46 +000045 Contexts.back().IsExpression = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000046 while (CurrentToken != NULL) {
47 if (CurrentToken->is(tok::greater)) {
48 Left->MatchingParen = CurrentToken;
49 CurrentToken->MatchingParen = Left;
50 CurrentToken->Type = TT_TemplateCloser;
51 next();
52 return true;
53 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +000054 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
Daniel Jasper5d823e32013-05-15 13:46:48 +000055 tok::question, tok::colon))
56 return false;
Daniel Jasper0348be02013-06-01 18:56:00 +000057 // If a && or || is found and interpreted as a binary operator, this set
Daniel Jasper15f33f02013-06-03 16:16:41 +000058 // of angles is likely part of something like "a < b && c > d". If the
Daniel Jasper0348be02013-06-01 18:56:00 +000059 // angles are inside an expression, the ||/&& might also be a binary
60 // operator that was misinterpreted because we are parsing template
61 // parameters.
62 // FIXME: This is getting out of hand, write a decent parser.
Manuel Klimekb3987012013-05-29 14:47:47 +000063 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
Daniel Jasper0348be02013-06-01 18:56:00 +000064 (CurrentToken->Previous->Type == TT_BinaryOperator ||
65 Contexts[Contexts.size() - 2].IsExpression) &&
Manuel Klimekb3987012013-05-29 14:47:47 +000066 Line.First->isNot(tok::kw_template))
Daniel Jasper32d28ee2013-01-29 21:01:14 +000067 return false;
Daniel Jasper9fc56f22013-02-14 15:01:34 +000068 updateParameterCount(Left, CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000069 if (!consumeToken())
70 return false;
71 }
72 return false;
73 }
74
75 bool parseParens(bool LookForDecls = false) {
76 if (CurrentToken == NULL)
77 return false;
Daniel Jasper923ebef2013-03-14 13:45:21 +000078 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
Daniel Jasper4e778092013-02-06 10:05:46 +000079
80 // FIXME: This is a bit of a hack. Do better.
81 Contexts.back().ColonIsForRangeExpr =
82 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
83
Daniel Jasper32d28ee2013-01-29 21:01:14 +000084 bool StartsObjCMethodExpr = false;
Manuel Klimekb3987012013-05-29 14:47:47 +000085 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000086 if (CurrentToken->is(tok::caret)) {
87 // ^( starts a block.
88 Left->Type = TT_ObjCBlockLParen;
Manuel Klimekb3987012013-05-29 14:47:47 +000089 } else if (FormatToken *MaybeSel = Left->Previous) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000090 // @selector( starts a selector.
Manuel Klimekb3987012013-05-29 14:47:47 +000091 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
92 MaybeSel->Previous->is(tok::at)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000093 StartsObjCMethodExpr = true;
94 }
95 }
96
Daniel Jasperb644dd62013-08-13 09:09:09 +000097 if (Left->Previous && Left->Previous->isOneOf(tok::kw_static_assert,
Daniel Jasper363193b2013-10-20 18:15:30 +000098 tok::kw_if, tok::kw_while)) {
99 // static_assert, if and while usually contain expressions.
Daniel Jasperb7000ca2013-08-01 17:58:23 +0000100 Contexts.back().IsExpression = true;
Daniel Jasper363193b2013-10-20 18:15:30 +0000101 } else if (Left->Previous && Left->Previous->is(tok::r_square) &&
102 Left->Previous->MatchingParen &&
103 Left->Previous->MatchingParen->Type == TT_LambdaLSquare) {
104 // This is a parameter list of a lambda expression.
105 Contexts.back().IsExpression = false;
106 }
Daniel Jasperb7000ca2013-08-01 17:58:23 +0000107
Daniel Jasper4e778092013-02-06 10:05:46 +0000108 if (StartsObjCMethodExpr) {
109 Contexts.back().ColonIsObjCMethodExpr = true;
110 Left->Type = TT_ObjCMethodExpr;
111 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000112
Daniel Jasper431f5912013-05-28 08:33:00 +0000113 bool MightBeFunctionType = CurrentToken->is(tok::star);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000114 bool HasMultipleLines = false;
115 bool HasMultipleParametersOnALine = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000116 while (CurrentToken != NULL) {
117 // LookForDecls is set when "if (" has been seen. Check for
118 // 'identifier' '*' 'identifier' followed by not '=' -- this
119 // '*' has to be a binary operator but determineStarAmpUsage() will
120 // categorize it as an unary operator, so set the right type here.
Manuel Klimekb3987012013-05-29 14:47:47 +0000121 if (LookForDecls && CurrentToken->Next) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000122 FormatToken *Prev = CurrentToken->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000123 if (Prev) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000124 FormatToken *PrevPrev = Prev->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000125 FormatToken *Next = CurrentToken->Next;
126 if (PrevPrev && PrevPrev->is(tok::identifier) &&
127 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
128 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
129 Prev->Type = TT_BinaryOperator;
130 LookForDecls = false;
131 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000132 }
133 }
134
Daniel Jasper53352602013-08-12 12:16:34 +0000135 if (CurrentToken->Previous->Type == TT_PointerOrReference &&
136 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
137 tok::coloncolon))
138 MightBeFunctionType = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000139 if (CurrentToken->is(tok::r_paren)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000140 if (MightBeFunctionType && CurrentToken->Next &&
Daniel Jaspere7d3bff2013-07-16 11:37:21 +0000141 (CurrentToken->Next->is(tok::l_paren) ||
142 (CurrentToken->Next->is(tok::l_square) &&
143 !Contexts.back().IsExpression)))
Daniel Jasper431f5912013-05-28 08:33:00 +0000144 Left->Type = TT_FunctionTypeLParen;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000145 Left->MatchingParen = CurrentToken;
146 CurrentToken->MatchingParen = Left;
147
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000148 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000149 CurrentToken->Type = TT_ObjCMethodExpr;
150 if (Contexts.back().FirstObjCSelectorName != NULL) {
151 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
152 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000153 }
154 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000155
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000156 if (!HasMultipleLines)
157 Left->PackingKind = PPK_Inconclusive;
158 else if (HasMultipleParametersOnALine)
159 Left->PackingKind = PPK_BinPacked;
160 else
161 Left->PackingKind = PPK_OnePerLine;
162
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000163 next();
164 return true;
165 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000166 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000167 return false;
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000168 updateParameterCount(Left, CurrentToken);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000169 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
170 !CurrentToken->Next->HasUnescapedNewline &&
171 !CurrentToken->Next->isTrailingComment())
172 HasMultipleParametersOnALine = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000173 if (!consumeToken())
174 return false;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000175 if (CurrentToken && CurrentToken->HasUnescapedNewline)
176 HasMultipleLines = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000177 }
178 return false;
179 }
180
181 bool parseSquare() {
182 if (!CurrentToken)
183 return false;
184
Alexander Kornienkod71b15b2013-06-17 13:19:53 +0000185 // A '[' could be an index subscript (after an identifier or after
Nico Weber051860e2013-02-10 02:08:05 +0000186 // ')' or ']'), it could be the start of an Objective-C method
187 // expression, or it could the the start of an Objective-C array literal.
Manuel Klimekb3987012013-05-29 14:47:47 +0000188 FormatToken *Left = CurrentToken->Previous;
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000189 FormatToken *Parent = Left->getPreviousNonComment();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000190 bool StartsObjCMethodExpr =
Daniel Jasper567dcf92013-09-05 09:29:45 +0000191 Contexts.back().CanBeExpression && Left->Type != TT_LambdaLSquare &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000192 (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
193 tok::kw_return, tok::kw_throw) ||
Daniel Jasperac3223e2013-04-10 09:49:49 +0000194 Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000195 Parent->Type == TT_CastRParen ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000196 getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
Daniel Jasper923ebef2013-03-14 13:45:21 +0000197 ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
Daniel Jasper6f21a982013-03-13 07:49:51 +0000198 Contexts.back().IsExpression = true;
Daniel Jasper8f54d882013-10-26 17:00:22 +0000199 bool ColonFound = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000200
Daniel Jasper4e778092013-02-06 10:05:46 +0000201 if (StartsObjCMethodExpr) {
202 Contexts.back().ColonIsObjCMethodExpr = true;
203 Left->Type = TT_ObjCMethodExpr;
Daniel Jaspera07aa662013-10-22 15:30:28 +0000204 } else if (Parent && Parent->is(tok::at)) {
205 Left->Type = TT_ArrayInitializerLSquare;
206 } else if (Left->Type == TT_Unknown) {
207 Left->Type = TT_ArraySubscriptLSquare;
Daniel Jasper4e778092013-02-06 10:05:46 +0000208 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000209
210 while (CurrentToken != NULL) {
211 if (CurrentToken->is(tok::r_square)) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000212 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
213 Left->Type == TT_ObjCMethodExpr) {
Nico Webere8a97982013-02-06 06:20:11 +0000214 // An ObjC method call is rarely followed by an open parenthesis.
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000215 // FIXME: Do we incorrectly label ":" with this?
216 StartsObjCMethodExpr = false;
217 Left->Type = TT_Unknown;
218 }
Daniel Jasper01786732013-02-04 07:21:18 +0000219 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000220 CurrentToken->Type = TT_ObjCMethodExpr;
Nico Webere8a97982013-02-06 06:20:11 +0000221 // determineStarAmpUsage() thinks that '*' '[' is allocating an
222 // array of pointers, but if '[' starts a selector then '*' is a
223 // binary operator.
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +0000224 if (Parent != NULL && Parent->Type == TT_PointerOrReference)
Nico Weber4ed7f3e2013-02-06 16:54:35 +0000225 Parent->Type = TT_BinaryOperator;
Daniel Jasper01786732013-02-04 07:21:18 +0000226 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000227 Left->MatchingParen = CurrentToken;
228 CurrentToken->MatchingParen = Left;
Daniel Jasper4e778092013-02-06 10:05:46 +0000229 if (Contexts.back().FirstObjCSelectorName != NULL)
230 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
231 Contexts.back().LongestObjCSelectorName;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000232 next();
233 return true;
234 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000235 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000236 return false;
Daniel Jasper8f54d882013-10-26 17:00:22 +0000237 if (CurrentToken->is(tok::colon))
238 ColonFound = true;
Daniel Jaspera07aa662013-10-22 15:30:28 +0000239 if (CurrentToken->is(tok::comma) &&
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000240 (Left->Type == TT_ArraySubscriptLSquare ||
Daniel Jasper8f54d882013-10-26 17:00:22 +0000241 (Left->Type == TT_ObjCMethodExpr && !ColonFound)))
Daniel Jaspera07aa662013-10-22 15:30:28 +0000242 Left->Type = TT_ArrayInitializerLSquare;
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000243 updateParameterCount(Left, CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000244 if (!consumeToken())
245 return false;
246 }
247 return false;
248 }
249
250 bool parseBrace() {
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000251 if (CurrentToken != NULL) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000252 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000253 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
254 Contexts.back().ColonIsDictLiteral = true;
Nico Weberf2ff8122013-05-26 05:39:26 +0000255
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000256 while (CurrentToken != NULL) {
257 if (CurrentToken->is(tok::r_brace)) {
258 Left->MatchingParen = CurrentToken;
259 CurrentToken->MatchingParen = Left;
260 next();
261 return true;
262 }
263 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
264 return false;
265 updateParameterCount(Left, CurrentToken);
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000266 if (CurrentToken->is(tok::colon))
267 Left->Type = TT_DictLiteral;
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000268 if (!consumeToken())
269 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000270 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000271 }
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000272 // No closing "}" found, this probably starts a definition.
273 Line.StartsDefinition = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000274 return true;
275 }
Daniel Jasperc4615b72013-02-20 12:56:39 +0000276
Manuel Klimekb3987012013-05-29 14:47:47 +0000277 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000278 if (Current->is(tok::comma)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000279 ++Left->ParameterCount;
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000280 if (!Left->Role)
281 Left->Role.reset(new CommaSeparatedList(Style));
282 Left->Role->CommaFound(Current);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000283 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000284 Left->ParameterCount = 1;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000285 }
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000286 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000287
288 bool parseConditional() {
289 while (CurrentToken != NULL) {
290 if (CurrentToken->is(tok::colon)) {
291 CurrentToken->Type = TT_ConditionalExpr;
292 next();
293 return true;
294 }
295 if (!consumeToken())
296 return false;
297 }
298 return false;
299 }
300
301 bool parseTemplateDeclaration() {
302 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
303 CurrentToken->Type = TT_TemplateOpener;
304 next();
305 if (!parseAngle())
306 return false;
Daniel Jasper34511fb2013-02-19 17:14:38 +0000307 if (CurrentToken != NULL)
Manuel Klimekb3987012013-05-29 14:47:47 +0000308 CurrentToken->Previous->ClosesTemplateDeclaration = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000309 return true;
310 }
311 return false;
312 }
313
314 bool consumeToken() {
Manuel Klimekb3987012013-05-29 14:47:47 +0000315 FormatToken *Tok = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000316 next();
Manuel Klimekb3987012013-05-29 14:47:47 +0000317 switch (Tok->Tok.getKind()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000318 case tok::plus:
319 case tok::minus:
Manuel Klimekb3987012013-05-29 14:47:47 +0000320 if (Tok->Previous == NULL && Line.MustBeDeclaration)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000321 Tok->Type = TT_ObjCMethodSpecifier;
322 break;
323 case tok::colon:
Manuel Klimekb3987012013-05-29 14:47:47 +0000324 if (Tok->Previous == NULL)
Daniel Jaspercf6d76a2013-03-18 12:50:26 +0000325 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000326 // Colons from ?: are handled in parseConditional().
Manuel Klimekb3987012013-05-29 14:47:47 +0000327 if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000328 Tok->Type = TT_CtorInitializerColon;
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000329 } else if (Contexts.back().ColonIsDictLiteral) {
330 Tok->Type = TT_DictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000331 } else if (Contexts.back().ColonIsObjCMethodExpr ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000332 Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000333 Tok->Type = TT_ObjCMethodExpr;
Manuel Klimekb3987012013-05-29 14:47:47 +0000334 Tok->Previous->Type = TT_ObjCSelectorName;
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000335 if (Tok->Previous->ColumnWidth >
Alexander Kornienko00895102013-06-05 14:09:10 +0000336 Contexts.back().LongestObjCSelectorName) {
Alexander Kornienko01fe9f92013-10-10 13:36:20 +0000337 Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
Alexander Kornienko00895102013-06-05 14:09:10 +0000338 }
Daniel Jasper4e778092013-02-06 10:05:46 +0000339 if (Contexts.back().FirstObjCSelectorName == NULL)
Manuel Klimekb3987012013-05-29 14:47:47 +0000340 Contexts.back().FirstObjCSelectorName = Tok->Previous;
Daniel Jasper4e778092013-02-06 10:05:46 +0000341 } else if (Contexts.back().ColonIsForRangeExpr) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000342 Tok->Type = TT_RangeBasedForLoopColon;
Alexander Kornienko01fe9f92013-10-10 13:36:20 +0000343 } else if (CurrentToken != NULL &&
344 CurrentToken->is(tok::numeric_constant)) {
345 Tok->Type = TT_BitFieldColon;
Daniel Jaspercea014b2013-10-08 16:24:07 +0000346 } else if (Contexts.size() == 1 && Line.First->isNot(tok::kw_enum)) {
Daniel Jasper6cabab42013-02-14 08:42:54 +0000347 Tok->Type = TT_InheritanceColon;
Daniel Jasper923ebef2013-03-14 13:45:21 +0000348 } else if (Contexts.back().ContextKind == tok::l_paren) {
349 Tok->Type = TT_InlineASMColon;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000350 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000351 break;
352 case tok::kw_if:
353 case tok::kw_while:
354 if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
355 next();
Nico Weber27268772013-06-26 00:30:14 +0000356 if (!parseParens(/*LookForDecls=*/true))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000357 return false;
358 }
359 break;
360 case tok::kw_for:
Daniel Jasper4e778092013-02-06 10:05:46 +0000361 Contexts.back().ColonIsForRangeExpr = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000362 next();
363 if (!parseParens())
364 return false;
365 break;
366 case tok::l_paren:
367 if (!parseParens())
368 return false;
Daniel Jasper59875ac2013-11-07 17:43:07 +0000369 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
370 !Contexts.back().IsExpression)
Daniel Jasper3c08a812013-02-24 18:54:32 +0000371 Line.MightBeFunctionDecl = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000372 break;
373 case tok::l_square:
374 if (!parseSquare())
375 return false;
376 break;
377 case tok::l_brace:
378 if (!parseBrace())
379 return false;
380 break;
381 case tok::less:
Daniel Jasper0236dd02013-07-30 22:37:19 +0000382 if (Tok->Previous && !Tok->Previous->Tok.isLiteral() && parseAngle())
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000383 Tok->Type = TT_TemplateOpener;
384 else {
385 Tok->Type = TT_BinaryOperator;
386 CurrentToken = Tok;
387 next();
388 }
389 break;
390 case tok::r_paren:
391 case tok::r_square:
392 return false;
393 case tok::r_brace:
394 // Lines can start with '}'.
Manuel Klimekb3987012013-05-29 14:47:47 +0000395 if (Tok->Previous != NULL)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000396 return false;
397 break;
398 case tok::greater:
399 Tok->Type = TT_BinaryOperator;
400 break;
401 case tok::kw_operator:
Daniel Jasper174f60f2013-09-02 09:20:39 +0000402 while (CurrentToken &&
403 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000404 if (CurrentToken->isOneOf(tok::star, tok::amp))
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000405 CurrentToken->Type = TT_PointerOrReference;
406 consumeToken();
Daniel Jasper174f60f2013-09-02 09:20:39 +0000407 if (CurrentToken && CurrentToken->Previous->Type == TT_BinaryOperator)
Daniel Jasperc476ea92013-08-28 07:27:35 +0000408 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000409 }
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000410 if (CurrentToken) {
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000411 CurrentToken->Type = TT_OverloadedOperatorLParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000412 if (CurrentToken->Previous->Type == TT_BinaryOperator)
413 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000414 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000415 break;
416 case tok::question:
417 parseConditional();
418 break;
419 case tok::kw_template:
420 parseTemplateDeclaration();
421 break;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000422 case tok::identifier:
Manuel Klimekb3987012013-05-29 14:47:47 +0000423 if (Line.First->is(tok::kw_for) &&
424 Tok->Tok.getIdentifierInfo() == &Ident_in)
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000425 Tok->Type = TT_ObjCForIn;
426 break;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000427 case tok::comma:
428 if (Contexts.back().FirstStartOfName)
429 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000430 if (Contexts.back().InCtorInitializer)
431 Tok->Type = TT_CtorInitializerComma;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000432 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000433 default:
434 break;
435 }
436 return true;
437 }
438
439 void parseIncludeDirective() {
440 next();
441 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
442 next();
443 while (CurrentToken != NULL) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000444 if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000445 CurrentToken->Type = TT_ImplicitStringLiteral;
446 next();
447 }
448 } else {
449 while (CurrentToken != NULL) {
Daniel Jasper3a204412013-02-23 07:46:38 +0000450 if (CurrentToken->is(tok::string_literal))
451 // Mark these string literals as "implicit" literals, too, so that
452 // they are not split or line-wrapped.
453 CurrentToken->Type = TT_ImplicitStringLiteral;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000454 next();
455 }
456 }
457 }
458
459 void parseWarningOrError() {
460 next();
461 // We still want to format the whitespace left of the first token of the
462 // warning or error.
463 next();
464 while (CurrentToken != NULL) {
465 CurrentToken->Type = TT_ImplicitStringLiteral;
466 next();
467 }
468 }
469
470 void parsePreprocessorDirective() {
471 next();
472 if (CurrentToken == NULL)
473 return;
Alexander Kornienkob18c2582013-10-11 21:43:05 +0000474 if (CurrentToken->Tok.is(tok::numeric_constant)) {
475 CurrentToken->SpacesRequiredBefore = 1;
476 return;
477 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000478 // Hashes in the middle of a line can lead to any strange token
479 // sequence.
Manuel Klimekb3987012013-05-29 14:47:47 +0000480 if (CurrentToken->Tok.getIdentifierInfo() == NULL)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000481 return;
Manuel Klimekb3987012013-05-29 14:47:47 +0000482 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000483 case tok::pp_include:
484 case tok::pp_import:
485 parseIncludeDirective();
486 break;
487 case tok::pp_error:
488 case tok::pp_warning:
489 parseWarningOrError();
490 break;
Daniel Jasperaae7bad2013-04-23 13:54:04 +0000491 case tok::pp_if:
492 case tok::pp_elif:
493 parseLine();
494 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000495 default:
496 break;
497 }
Daniel Jasper5b7e7b02013-02-05 09:34:14 +0000498 while (CurrentToken != NULL)
499 next();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000500 }
501
Nico Weber95e8e462013-02-12 16:17:07 +0000502public:
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000503 LineType parseLine() {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000504 if (CurrentToken->is(tok::hash)) {
505 parsePreprocessorDirective();
506 return LT_PreprocessorDirective;
507 }
508 while (CurrentToken != NULL) {
509 if (CurrentToken->is(tok::kw_virtual))
510 KeywordVirtualFound = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000511 if (!consumeToken())
512 return LT_Invalid;
513 }
514 if (KeywordVirtualFound)
515 return LT_VirtualFunctionDecl;
516
Manuel Klimekb3987012013-05-29 14:47:47 +0000517 if (Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000518 if (Contexts.back().FirstObjCSelectorName != NULL)
519 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
520 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000521 return LT_ObjCMethodDecl;
522 }
523
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000524 return LT_Other;
525 }
526
Nico Weber95e8e462013-02-12 16:17:07 +0000527private:
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000528 void next() {
Daniel Jasper01786732013-02-04 07:21:18 +0000529 if (CurrentToken != NULL) {
530 determineTokenType(*CurrentToken);
Daniel Jasper4e778092013-02-06 10:05:46 +0000531 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
Daniel Jasper01786732013-02-04 07:21:18 +0000532 }
533
Manuel Klimekb3987012013-05-29 14:47:47 +0000534 if (CurrentToken != NULL)
535 CurrentToken = CurrentToken->Next;
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000536
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000537 if (CurrentToken != NULL) {
538 // Reset token type in case we have already looked at it and then
539 // recovered from an error (e.g. failure to find the matching >).
540 if (CurrentToken->Type != TT_LambdaLSquare &&
541 CurrentToken->Type != TT_ImplicitStringLiteral)
542 CurrentToken->Type = TT_Unknown;
543 if (CurrentToken->Role)
544 CurrentToken->Role.reset(NULL);
545 CurrentToken->FakeLParens.clear();
546 CurrentToken->FakeRParens = 0;
547 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000548 }
549
Daniel Jasper4e778092013-02-06 10:05:46 +0000550 /// \brief A struct to hold information valid in a specific context, e.g.
551 /// a pair of parenthesis.
552 struct Context {
Daniel Jasper923ebef2013-03-14 13:45:21 +0000553 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
554 bool IsExpression)
555 : ContextKind(ContextKind), BindingStrength(BindingStrength),
556 LongestObjCSelectorName(0), ColonIsForRangeExpr(false),
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000557 ColonIsDictLiteral(false), ColonIsObjCMethodExpr(false),
Nico Weberf2ff8122013-05-26 05:39:26 +0000558 FirstObjCSelectorName(NULL), FirstStartOfName(NULL),
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000559 IsExpression(IsExpression), CanBeExpression(true),
560 InCtorInitializer(false) {}
Daniel Jasper01786732013-02-04 07:21:18 +0000561
Daniel Jasper923ebef2013-03-14 13:45:21 +0000562 tok::TokenKind ContextKind;
Daniel Jasper4e778092013-02-06 10:05:46 +0000563 unsigned BindingStrength;
564 unsigned LongestObjCSelectorName;
565 bool ColonIsForRangeExpr;
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000566 bool ColonIsDictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000567 bool ColonIsObjCMethodExpr;
Manuel Klimekb3987012013-05-29 14:47:47 +0000568 FormatToken *FirstObjCSelectorName;
569 FormatToken *FirstStartOfName;
Daniel Jasper4e778092013-02-06 10:05:46 +0000570 bool IsExpression;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000571 bool CanBeExpression;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000572 bool InCtorInitializer;
Daniel Jasper4e778092013-02-06 10:05:46 +0000573 };
574
575 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
576 /// of each instance.
577 struct ScopedContextCreator {
578 AnnotatingParser &P;
579
Daniel Jasper923ebef2013-03-14 13:45:21 +0000580 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
581 unsigned Increase)
582 : P(P) {
Daniel Jasper2a409b62013-07-08 14:34:09 +0000583 P.Contexts.push_back(Context(ContextKind,
584 P.Contexts.back().BindingStrength + Increase,
585 P.Contexts.back().IsExpression));
Daniel Jasper4e778092013-02-06 10:05:46 +0000586 }
587
588 ~ScopedContextCreator() { P.Contexts.pop_back(); }
589 };
Daniel Jasper01786732013-02-04 07:21:18 +0000590
Manuel Klimekb3987012013-05-29 14:47:47 +0000591 void determineTokenType(FormatToken &Current) {
592 if (Current.getPrecedence() == prec::Assignment &&
Daniel Jasper53352602013-08-12 12:16:34 +0000593 !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
Manuel Klimekb3987012013-05-29 14:47:47 +0000594 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000595 Contexts.back().IsExpression = true;
Manuel Klimekb3987012013-05-29 14:47:47 +0000596 for (FormatToken *Previous = Current.Previous;
Daniel Jasper7e274002013-09-11 20:37:10 +0000597 Previous && !Previous->isOneOf(tok::comma, tok::semi);
Manuel Klimekb3987012013-05-29 14:47:47 +0000598 Previous = Previous->Previous) {
Daniel Jasper9c65b062013-02-27 11:43:50 +0000599 if (Previous->is(tok::r_square))
600 Previous = Previous->MatchingParen;
Daniel Jasper01786732013-02-04 07:21:18 +0000601 if (Previous->Type == TT_BinaryOperator &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000602 Previous->isOneOf(tok::star, tok::amp)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000603 Previous->Type = TT_PointerOrReference;
604 }
Daniel Jasper01786732013-02-04 07:21:18 +0000605 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000606 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw) ||
Nico Weber95e8e462013-02-12 16:17:07 +0000607 (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
Daniel Jasper378d93d2013-05-13 07:14:40 +0000608 !Line.InPPDirective &&
Manuel Klimek2530fd52013-08-12 03:51:17 +0000609 (!Current.Previous ||
Daniel Jasper53352602013-08-12 12:16:34 +0000610 !Current.Previous->isOneOf(tok::kw_for, tok::kw_catch)))) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000611 Contexts.back().IsExpression = true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000612 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000613 for (FormatToken *Previous = Current.Previous;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000614 Previous && Previous->isOneOf(tok::star, tok::amp);
Manuel Klimekb3987012013-05-29 14:47:47 +0000615 Previous = Previous->Previous)
Nico Weber95e8e462013-02-12 16:17:07 +0000616 Previous->Type = TT_PointerOrReference;
Manuel Klimekb3987012013-05-29 14:47:47 +0000617 } else if (Current.Previous &&
618 Current.Previous->Type == TT_CtorInitializerColon) {
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000619 Contexts.back().IsExpression = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000620 Contexts.back().InCtorInitializer = true;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000621 } else if (Current.is(tok::kw_new)) {
622 Contexts.back().CanBeExpression = false;
Daniel Jasper16a69ef2013-05-03 14:41:24 +0000623 } else if (Current.is(tok::semi)) {
624 // This should be the condition or increment in a for-loop.
625 Contexts.back().IsExpression = true;
Nico Weber95e8e462013-02-12 16:17:07 +0000626 }
Daniel Jasper01786732013-02-04 07:21:18 +0000627
628 if (Current.Type == TT_Unknown) {
Daniel Jasper6b3ff8c2013-09-27 08:29:16 +0000629 // Line.MightBeFunctionDecl can only be true after the parentheses of a
630 // function declaration have been found. In this case, 'Current' is a
631 // trailing token of this declaration and thus cannot be a name.
632 if (isStartOfName(Current) && !Line.MightBeFunctionDecl) {
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000633 Contexts.back().FirstStartOfName = &Current;
Daniel Jasper3c08a812013-02-24 18:54:32 +0000634 Current.Type = TT_StartOfName;
Daniel Jasper2ca37412013-07-09 14:36:48 +0000635 } else if (Current.is(tok::kw_auto)) {
636 AutoFound = true;
Daniel Jasper3262f4c2013-07-11 14:33:06 +0000637 } else if (Current.is(tok::arrow) && AutoFound &&
638 Line.MustBeDeclaration) {
Daniel Jasper2ca37412013-07-09 14:36:48 +0000639 Current.Type = TT_TrailingReturnArrow;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000640 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000641 Current.Type =
Daniel Jasperd6104f62013-07-05 13:30:40 +0000642 determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
643 Contexts.back().IsExpression);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000644 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000645 Current.Type = determinePlusMinusCaretUsage(Current);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000646 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000647 Current.Type = determineIncrementUsage(Current);
648 } else if (Current.is(tok::exclaim)) {
649 Current.Type = TT_UnaryOperator;
Manuel Klimek31e44f72013-09-04 08:20:47 +0000650 } else if (Current.isBinaryOperator() &&
651 (!Current.Previous ||
652 Current.Previous->isNot(tok::l_square))) {
Daniel Jasper01786732013-02-04 07:21:18 +0000653 Current.Type = TT_BinaryOperator;
654 } else if (Current.is(tok::comment)) {
Alexander Kornienko00895102013-06-05 14:09:10 +0000655 if (Current.TokenText.startswith("//"))
Daniel Jasper01786732013-02-04 07:21:18 +0000656 Current.Type = TT_LineComment;
657 else
658 Current.Type = TT_BlockComment;
Nico Weber37d69312013-02-13 04:13:13 +0000659 } else if (Current.is(tok::r_paren)) {
Daniel Jasperb8b42952013-05-31 16:14:28 +0000660 FormatToken *LeftOfParens = NULL;
661 if (Current.MatchingParen)
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000662 LeftOfParens = Current.MatchingParen->getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000663 bool IsCast = false;
664 bool ParensAreEmpty = Current.Previous == Current.MatchingParen;
665 bool ParensAreType = !Current.Previous ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000666 Current.Previous->Type == TT_PointerOrReference ||
Daniel Jasperb8b42952013-05-31 16:14:28 +0000667 Current.Previous->Type == TT_TemplateCloser ||
668 isSimpleTypeSpecifier(*Current.Previous);
Nico Weber37d69312013-02-13 04:13:13 +0000669 bool ParensCouldEndDecl =
Manuel Klimekb3987012013-05-29 14:47:47 +0000670 Current.Next &&
671 Current.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
Daniel Jasper6a365aa2013-03-13 17:13:53 +0000672 bool IsSizeOfOrAlignOf =
Daniel Jasperb8b42952013-05-31 16:14:28 +0000673 LeftOfParens &&
674 LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
675 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
Daniel Jasper0c368782013-07-15 15:04:42 +0000676 (Contexts.back().IsExpression ||
677 (Current.Next && Current.Next->isBinaryOperator())))
Daniel Jasperb8b42952013-05-31 16:14:28 +0000678 IsCast = true;
Daniel Jasper2a409b62013-07-08 14:34:09 +0000679 if (Current.Next && Current.Next->isNot(tok::string_literal) &&
Daniel Jasperb8b42952013-05-31 16:14:28 +0000680 (Current.Next->Tok.isLiteral() ||
681 Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
682 IsCast = true;
683 // If there is an identifier after the (), it is likely a cast, unless
684 // there is also an identifier before the ().
Daniel Jasperff1a2e52013-06-06 08:20:20 +0000685 if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL ||
686 LeftOfParens->is(tok::kw_return)) &&
Daniel Jasper526df0f2013-07-08 14:58:01 +0000687 LeftOfParens->Type != TT_OverloadedOperator &&
Nico Weber465e8612013-06-25 00:55:57 +0000688 LeftOfParens->Type != TT_TemplateCloser && Current.Next &&
689 Current.Next->is(tok::identifier))
Daniel Jasperb8b42952013-05-31 16:14:28 +0000690 IsCast = true;
691 if (IsCast && !ParensAreEmpty)
Nico Weber37d69312013-02-13 04:13:13 +0000692 Current.Type = TT_CastRParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000693 } else if (Current.is(tok::at) && Current.Next) {
694 switch (Current.Next->Tok.getObjCKeywordID()) {
Daniel Jasper01786732013-02-04 07:21:18 +0000695 case tok::objc_interface:
696 case tok::objc_implementation:
697 case tok::objc_protocol:
698 Current.Type = TT_ObjCDecl;
699 break;
700 case tok::objc_property:
701 Current.Type = TT_ObjCProperty;
702 break;
703 default:
704 break;
705 }
Daniel Jasper5ad390d2013-05-28 11:30:49 +0000706 } else if (Current.is(tok::period)) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000707 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
Daniel Jasper5ad390d2013-05-28 11:30:49 +0000708 if (PreviousNoComment &&
709 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
710 Current.Type = TT_DesignatedInitializerPeriod;
Daniel Jasper01786732013-02-04 07:21:18 +0000711 }
712 }
713 }
714
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000715 /// \brief Take a guess at whether \p Tok starts a name of a function or
716 /// variable declaration.
717 ///
718 /// This is a heuristic based on whether \p Tok is an identifier following
719 /// something that is likely a type.
720 bool isStartOfName(const FormatToken &Tok) {
721 if (Tok.isNot(tok::identifier) || Tok.Previous == NULL)
722 return false;
723
724 // Skip "const" as it does not have an influence on whether this is a name.
725 FormatToken *PreviousNotConst = Tok.Previous;
726 while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const))
727 PreviousNotConst = PreviousNotConst->Previous;
728
729 if (PreviousNotConst == NULL)
730 return false;
731
Daniel Jasper2a409b62013-07-08 14:34:09 +0000732 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
733 PreviousNotConst->Previous &&
734 PreviousNotConst->Previous->is(tok::hash);
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000735
Daniel Jasper92495a82013-08-19 10:16:18 +0000736 if (PreviousNotConst->Type == TT_TemplateCloser)
737 return PreviousNotConst && PreviousNotConst->MatchingParen &&
738 PreviousNotConst->MatchingParen->Previous &&
739 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
740
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000741 return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
742 PreviousNotConst->Type == TT_PointerOrReference ||
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000743 isSimpleTypeSpecifier(*PreviousNotConst);
744 }
745
Daniel Jasper01786732013-02-04 07:21:18 +0000746 /// \brief Return the type of the given token assuming it is * or &.
Manuel Klimekb3987012013-05-29 14:47:47 +0000747 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000748 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasper01786732013-02-04 07:21:18 +0000749 if (PrevToken == NULL)
750 return TT_UnaryOperator;
751
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000752 const FormatToken *NextToken = Tok.getNextNonComment();
Daniel Jasper01786732013-02-04 07:21:18 +0000753 if (NextToken == NULL)
754 return TT_Unknown;
755
Daniel Jasper431f5912013-05-28 08:33:00 +0000756 if (PrevToken->is(tok::coloncolon) ||
757 (PrevToken->is(tok::l_paren) && !IsExpression))
Daniel Jasper8a5d7cd2013-03-01 17:13:29 +0000758 return TT_PointerOrReference;
759
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000760 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
Daniel Jasperd3cf17b2013-03-14 10:50:25 +0000761 tok::comma, tok::semi, tok::kw_return, tok::colon,
Daniel Jasper65da8e92013-09-21 17:31:51 +0000762 tok::equal, tok::kw_delete, tok::kw_sizeof) ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000763 PrevToken->Type == TT_BinaryOperator ||
Daniel Jasper01786732013-02-04 07:21:18 +0000764 PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
765 return TT_UnaryOperator;
766
Nico Webere8a97982013-02-06 06:20:11 +0000767 if (NextToken->is(tok::l_square))
768 return TT_PointerOrReference;
769
Daniel Jasperdb8afe42013-09-10 10:26:38 +0000770 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
771 PrevToken->MatchingParen->Previous &&
772 PrevToken->MatchingParen->Previous->is(tok::kw_typeof))
773 return TT_PointerOrReference;
774
Manuel Klimekb3987012013-05-29 14:47:47 +0000775 if (PrevToken->Tok.isLiteral() ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000776 PrevToken->isOneOf(tok::r_paren, tok::r_square) ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000777 NextToken->Tok.isLiteral() || NextToken->isUnaryOperator())
Daniel Jasper01786732013-02-04 07:21:18 +0000778 return TT_BinaryOperator;
779
Daniel Jasper01786732013-02-04 07:21:18 +0000780 // It is very unlikely that we are going to find a pointer or reference type
781 // definition on the RHS of an assignment.
782 if (IsExpression)
783 return TT_BinaryOperator;
784
785 return TT_PointerOrReference;
786 }
787
Manuel Klimekb3987012013-05-29 14:47:47 +0000788 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000789 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000790 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper01786732013-02-04 07:21:18 +0000791 return TT_UnaryOperator;
792
793 // Use heuristics to recognize unary operators.
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000794 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
795 tok::question, tok::colon, tok::kw_return,
796 tok::kw_case, tok::at, tok::l_brace))
Daniel Jasper01786732013-02-04 07:21:18 +0000797 return TT_UnaryOperator;
798
Nico Weberee0feec2013-02-05 16:21:00 +0000799 // There can't be two consecutive binary operators.
Daniel Jasper01786732013-02-04 07:21:18 +0000800 if (PrevToken->Type == TT_BinaryOperator)
801 return TT_UnaryOperator;
802
803 // Fall back to marking the token as binary operator.
804 return TT_BinaryOperator;
805 }
806
807 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
Manuel Klimekb3987012013-05-29 14:47:47 +0000808 TokenType determineIncrementUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000809 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000810 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper01786732013-02-04 07:21:18 +0000811 return TT_UnaryOperator;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000812 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
Daniel Jasper01786732013-02-04 07:21:18 +0000813 return TT_TrailingUnaryOperator;
814
815 return TT_UnaryOperator;
816 }
Daniel Jasper4e778092013-02-06 10:05:46 +0000817
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000818 // FIXME: This is copy&pasted from Sema. Put it in a common place and remove
819 // duplication.
820 /// \brief Determine whether the token kind starts a simple-type-specifier.
Manuel Klimekb3987012013-05-29 14:47:47 +0000821 bool isSimpleTypeSpecifier(const FormatToken &Tok) const {
822 switch (Tok.Tok.getKind()) {
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000823 case tok::kw_short:
824 case tok::kw_long:
825 case tok::kw___int64:
826 case tok::kw___int128:
827 case tok::kw_signed:
828 case tok::kw_unsigned:
829 case tok::kw_void:
830 case tok::kw_char:
831 case tok::kw_int:
832 case tok::kw_half:
833 case tok::kw_float:
834 case tok::kw_double:
835 case tok::kw_wchar_t:
836 case tok::kw_bool:
837 case tok::kw___underlying_type:
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000838 case tok::annot_typename:
839 case tok::kw_char16_t:
840 case tok::kw_char32_t:
841 case tok::kw_typeof:
842 case tok::kw_decltype:
Alexander Kornienko00895102013-06-05 14:09:10 +0000843 return true;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000844 default:
Alexander Kornienko00895102013-06-05 14:09:10 +0000845 return false;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000846 }
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000847 }
848
Daniel Jasper4e778092013-02-06 10:05:46 +0000849 SmallVector<Context, 8> Contexts;
850
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000851 const FormatStyle &Style;
Daniel Jasper4e778092013-02-06 10:05:46 +0000852 AnnotatedLine &Line;
Manuel Klimekb3987012013-05-29 14:47:47 +0000853 FormatToken *CurrentToken;
Daniel Jasper4e778092013-02-06 10:05:46 +0000854 bool KeywordVirtualFound;
Daniel Jasper2ca37412013-07-09 14:36:48 +0000855 bool AutoFound;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000856 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000857};
858
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000859static int PrecedenceUnaryOperator = prec::PointerToMember + 1;
860static int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
861
Daniel Jasper29f123b2013-02-08 15:28:42 +0000862/// \brief Parses binary expressions by inserting fake parenthesis based on
863/// operator precedence.
864class ExpressionParser {
865public:
Daniel Jasper9acb8b42013-06-06 09:11:58 +0000866 ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {
867 // Skip leading "}", e.g. in "} else if (...) {".
868 if (Current->is(tok::r_brace))
869 next();
870 }
Daniel Jasper29f123b2013-02-08 15:28:42 +0000871
872 /// \brief Parse expressions with the given operatore precedence.
Daniel Jasper237d4c12013-02-23 21:01:55 +0000873 void parse(int Precedence = 0) {
Daniel Jasper966e6d32013-11-07 19:23:49 +0000874 // Skip 'return' and ObjC selector colons as they are not part of a binary
875 // expression.
876 while (Current &&
877 (Current->is(tok::kw_return) ||
878 (Current->is(tok::colon) && Current->Type == TT_ObjCMethodExpr)))
Daniel Jasperf78bf4a2013-09-30 08:29:03 +0000879 next();
880
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000881 if (Current == NULL || Precedence > PrecedenceArrowAndPeriod)
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000882 return;
883
Daniel Jasperc01897c2013-05-31 14:56:12 +0000884 // Conditional expressions need to be parsed separately for proper nesting.
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000885 if (Precedence == prec::Conditional) {
Daniel Jasperc01897c2013-05-31 14:56:12 +0000886 parseConditionalExpr();
887 return;
888 }
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000889
890 // Parse unary operators, which all have a higher precedence than binary
891 // operators.
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000892 if (Precedence == PrecedenceUnaryOperator) {
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000893 parseUnaryOperator();
Daniel Jasper29f123b2013-02-08 15:28:42 +0000894 return;
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000895 }
Daniel Jasper29f123b2013-02-08 15:28:42 +0000896
Manuel Klimekb3987012013-05-29 14:47:47 +0000897 FormatToken *Start = Current;
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000898 FormatToken *LatestOperator = NULL;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000899
Daniel Jasper237d4c12013-02-23 21:01:55 +0000900 while (Current) {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000901 // Consume operators with higher precedence.
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000902 parse(Precedence + 1);
Daniel Jasper29f123b2013-02-08 15:28:42 +0000903
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000904 int CurrentPrecedence = getCurrentPrecedence();
905
906 if (Current && Current->Type == TT_ObjCSelectorName &&
907 Precedence == CurrentPrecedence)
908 Start = Current;
Daniel Jasper237d4c12013-02-23 21:01:55 +0000909
Daniel Jasper29f123b2013-02-08 15:28:42 +0000910 // At the end of the line or when an operator with higher precedence is
911 // found, insert fake parenthesis and return.
Daniel Jasperac3223e2013-04-10 09:49:49 +0000912 if (Current == NULL || Current->closesScope() ||
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000913 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) {
914 if (LatestOperator) {
915 if (Precedence == PrecedenceArrowAndPeriod) {
916 LatestOperator->LastInChainOfCalls = true;
917 // Call expressions don't have a binary operator precedence.
918 addFakeParenthesis(Start, prec::Unknown);
919 } else {
920 addFakeParenthesis(Start, prec::Level(Precedence));
921 }
922 }
Daniel Jasper29f123b2013-02-08 15:28:42 +0000923 return;
924 }
925
926 // Consume scopes: (), [], <> and {}
Daniel Jasperac3223e2013-04-10 09:49:49 +0000927 if (Current->opensScope()) {
928 while (Current && !Current->closesScope()) {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000929 next();
930 parse();
931 }
932 next();
933 } else {
934 // Operator found.
Daniel Jasper237d4c12013-02-23 21:01:55 +0000935 if (CurrentPrecedence == Precedence)
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000936 LatestOperator = Current;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000937
938 next();
939 }
940 }
941 }
942
943private:
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000944 /// \brief Gets the precedence (+1) of the given token for binary operators
945 /// and other tokens that we treat like binary operators.
946 int getCurrentPrecedence() {
947 if (Current) {
948 if (Current->Type == TT_ConditionalExpr)
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000949 return prec::Conditional;
Daniel Jasper966e6d32013-11-07 19:23:49 +0000950 else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon ||
951 Current->Type == TT_ObjCSelectorName)
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000952 return 0;
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000953 else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000954 return Current->getPrecedence();
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000955 else if (Current->isOneOf(tok::period, tok::arrow))
956 return PrecedenceArrowAndPeriod;
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000957 }
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000958 return -1;
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000959 }
960
Daniel Jasperc01897c2013-05-31 14:56:12 +0000961 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
962 Start->FakeLParens.push_back(Precedence);
Daniel Jasperdb4813a2013-09-06 08:08:14 +0000963 if (Precedence > prec::Unknown)
964 Start->StartsBinaryExpression = true;
965 if (Current) {
Daniel Jasperc01897c2013-05-31 14:56:12 +0000966 ++Current->Previous->FakeRParens;
Daniel Jasperdb4813a2013-09-06 08:08:14 +0000967 if (Precedence > prec::Unknown)
968 Current->Previous->EndsBinaryExpression = true;
969 }
Daniel Jasperc01897c2013-05-31 14:56:12 +0000970 }
971
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000972 /// \brief Parse unary operator expressions and surround them with fake
973 /// parentheses if appropriate.
974 void parseUnaryOperator() {
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000975 if (Current == NULL || Current->Type != TT_UnaryOperator) {
976 parse(PrecedenceArrowAndPeriod);
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000977 return;
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000978 }
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000979
980 FormatToken *Start = Current;
981 next();
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000982 parseUnaryOperator();
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000983
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000984 // The actual precedence doesn't matter.
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000985 addFakeParenthesis(Start, prec::Unknown);
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000986 }
987
Daniel Jasperc01897c2013-05-31 14:56:12 +0000988 void parseConditionalExpr() {
989 FormatToken *Start = Current;
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000990 parse(prec::LogicalOr);
Daniel Jasperc01897c2013-05-31 14:56:12 +0000991 if (!Current || !Current->is(tok::question))
992 return;
993 next();
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000994 parse(prec::LogicalOr);
Daniel Jasperc01897c2013-05-31 14:56:12 +0000995 if (!Current || Current->Type != TT_ConditionalExpr)
996 return;
997 next();
998 parseConditionalExpr();
999 addFakeParenthesis(Start, prec::Conditional);
1000 }
1001
Daniel Jasper29f123b2013-02-08 15:28:42 +00001002 void next() {
Alexander Kornienkod71b15b2013-06-17 13:19:53 +00001003 if (Current)
1004 Current = Current->Next;
1005 while (Current && Current->isTrailingComment())
Manuel Klimekb3987012013-05-29 14:47:47 +00001006 Current = Current->Next;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001007 }
1008
Manuel Klimekb3987012013-05-29 14:47:47 +00001009 FormatToken *Current;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001010};
1011
Craig Topper14e66492013-07-01 04:03:19 +00001012} // end anonymous namespace
1013
Daniel Jasperb77d7412013-09-06 07:54:20 +00001014void
1015TokenAnnotator::setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines) {
Daniel Jasperb77d7412013-09-06 07:54:20 +00001016 const AnnotatedLine *NextNonCommentLine = NULL;
Daniel Jasper2a80ad62013-11-05 19:10:03 +00001017 for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
1018 E = Lines.rend();
1019 I != E; ++I) {
1020 if (NextNonCommentLine && (*I)->First->is(tok::comment) &&
1021 (*I)->First->Next == NULL)
1022 (*I)->Level = NextNonCommentLine->Level;
Daniel Jasperb77d7412013-09-06 07:54:20 +00001023 else
Daniel Jasper2a80ad62013-11-05 19:10:03 +00001024 NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : NULL;
1025
1026 setCommentLineLevels((*I)->Children);
Daniel Jasperb77d7412013-09-06 07:54:20 +00001027 }
1028}
1029
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001030void TokenAnnotator::annotate(AnnotatedLine &Line) {
Daniel Jasperb77d7412013-09-06 07:54:20 +00001031 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1032 E = Line.Children.end();
Daniel Jasper567dcf92013-09-05 09:29:45 +00001033 I != E; ++I) {
1034 annotate(**I);
1035 }
Daniel Jasperd4a03db2013-08-22 15:00:41 +00001036 AnnotatingParser Parser(Style, Line, Ident_in);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001037 Line.Type = Parser.parseLine();
1038 if (Line.Type == LT_Invalid)
1039 return;
1040
Daniel Jasper29f123b2013-02-08 15:28:42 +00001041 ExpressionParser ExprParser(Line);
1042 ExprParser.parse();
1043
Manuel Klimekb3987012013-05-29 14:47:47 +00001044 if (Line.First->Type == TT_ObjCMethodSpecifier)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001045 Line.Type = LT_ObjCMethodDecl;
Manuel Klimekb3987012013-05-29 14:47:47 +00001046 else if (Line.First->Type == TT_ObjCDecl)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001047 Line.Type = LT_ObjCDecl;
Manuel Klimekb3987012013-05-29 14:47:47 +00001048 else if (Line.First->Type == TT_ObjCProperty)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001049 Line.Type = LT_ObjCProperty;
1050
Manuel Klimekb3987012013-05-29 14:47:47 +00001051 Line.First->SpacesRequiredBefore = 1;
1052 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001053}
1054
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001055void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001056 Line.First->TotalLength =
1057 Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth;
Manuel Klimekb3987012013-05-29 14:47:47 +00001058 if (!Line.First->Next)
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001059 return;
Manuel Klimekb3987012013-05-29 14:47:47 +00001060 FormatToken *Current = Line.First->Next;
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001061 bool InFunctionDecl = Line.MightBeFunctionDecl;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001062 while (Current != NULL) {
Daniel Jasper729a7432013-02-11 12:36:37 +00001063 if (Current->Type == TT_LineComment)
1064 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
Alexander Kornienkob18c2582013-10-11 21:43:05 +00001065 else if (Current->SpacesRequiredBefore == 0 &&
1066 spaceRequiredBefore(Line, *Current))
1067 Current->SpacesRequiredBefore = 1;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001068
Daniel Jasperebaa1712013-09-17 09:52:48 +00001069 Current->MustBreakBefore =
1070 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
1071
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001072 Current->CanBreakBefore =
1073 Current->MustBreakBefore || canBreakBefore(Line, *Current);
Daniel Jasper567dcf92013-09-05 09:29:45 +00001074 if (Current->MustBreakBefore || !Current->Children.empty() ||
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001075 Current->IsMultiline)
Manuel Klimekb3987012013-05-29 14:47:47 +00001076 Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001077 else
Daniel Jasper2a409b62013-07-08 14:34:09 +00001078 Current->TotalLength = Current->Previous->TotalLength +
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001079 Current->ColumnWidth +
Daniel Jasper2a409b62013-07-08 14:34:09 +00001080 Current->SpacesRequiredBefore;
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001081
1082 if (Current->Type == TT_CtorInitializerColon)
1083 InFunctionDecl = false;
1084
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001085 // FIXME: Only calculate this if CanBreakBefore is true once static
1086 // initializers etc. are sorted out.
1087 // FIXME: Move magic numbers to a better place.
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001088 Current->SplitPenalty = 20 * Current->BindingStrength +
1089 splitPenalty(Line, *Current, InFunctionDecl);
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001090
Manuel Klimekb3987012013-05-29 14:47:47 +00001091 Current = Current->Next;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001092 }
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001093
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001094 calculateUnbreakableTailLengths(Line);
Daniel Jasperd4a03db2013-08-22 15:00:41 +00001095 for (Current = Line.First; Current != NULL; Current = Current->Next) {
1096 if (Current->Role)
1097 Current->Role->precomputeFormattingInfos(Current);
1098 }
1099
Daniel Jasper567dcf92013-09-05 09:29:45 +00001100 DEBUG({ printDebugInfo(Line); });
1101
Daniel Jasperb77d7412013-09-06 07:54:20 +00001102 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1103 E = Line.Children.end();
Daniel Jasper567dcf92013-09-05 09:29:45 +00001104 I != E; ++I) {
1105 calculateFormattingInformation(**I);
1106 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001107}
1108
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001109void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1110 unsigned UnbreakableTailLength = 0;
Manuel Klimekb3987012013-05-29 14:47:47 +00001111 FormatToken *Current = Line.Last;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001112 while (Current != NULL) {
1113 Current->UnbreakableTailLength = UnbreakableTailLength;
1114 if (Current->CanBreakBefore ||
1115 Current->isOneOf(tok::comment, tok::string_literal)) {
1116 UnbreakableTailLength = 0;
1117 } else {
1118 UnbreakableTailLength +=
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001119 Current->ColumnWidth + Current->SpacesRequiredBefore;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001120 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001121 Current = Current->Previous;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001122 }
1123}
1124
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001125unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001126 const FormatToken &Tok,
1127 bool InFunctionDecl) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001128 const FormatToken &Left = *Tok.Previous;
1129 const FormatToken &Right = Tok;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001130
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001131 if (Left.is(tok::semi))
1132 return 0;
1133 if (Left.is(tok::comma))
1134 return 1;
Daniel Jasper011c35d2013-07-12 11:19:37 +00001135 if (Right.is(tok::l_square))
1136 return 150;
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001137
Daniel Jasper6561f6a2013-07-09 07:43:55 +00001138 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001139 if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001140 return 3;
Daniel Jasperc18cff32013-07-11 12:34:23 +00001141 if (Left.Type == TT_StartOfName)
1142 return 20;
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001143 if (InFunctionDecl && Right.BindingStrength == 1)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001144 // FIXME: Clean up hack of using BindingStrength to find top-level names.
1145 return Style.PenaltyReturnTypeOnItsOwnLine;
Daniel Jasper92495a82013-08-19 10:16:18 +00001146 return 200;
Daniel Jasper3c08a812013-02-24 18:54:32 +00001147 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001148 if (Left.is(tok::equal) && Right.is(tok::l_brace))
1149 return 150;
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001150 if (Left.Type == TT_CastRParen)
1151 return 100;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001152 if (Left.is(tok::coloncolon))
1153 return 500;
Daniel Jasper6b119d62013-04-05 17:22:09 +00001154 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1155 return 5000;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001156
Daniel Jasper6cabab42013-02-14 08:42:54 +00001157 if (Left.Type == TT_RangeBasedForLoopColon ||
1158 Left.Type == TT_InheritanceColon)
Daniel Jasper84a1a632013-02-26 13:18:08 +00001159 return 2;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001160
Daniel Jasperd3fef0f2013-08-27 14:24:43 +00001161 if (Right.isMemberAccess()) {
Daniel Jasper2f0a0202013-09-06 08:54:24 +00001162 if (Left.isOneOf(tok::r_paren, tok::r_square) && Left.MatchingParen &&
1163 Left.MatchingParen->ParameterCount > 0)
Daniel Jasper518ee342013-02-26 13:59:14 +00001164 return 20; // Should be smaller than breaking at a nested comma.
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001165 return 150;
1166 }
1167
Daniel Jasper20a0f8c2013-07-11 21:02:56 +00001168 // Breaking before a trailing 'const' or not-function-like annotation is bad.
Daniel Jasperaa9e7b12013-08-01 13:46:58 +00001169 if (Left.is(tok::r_paren) && Line.Type != LT_ObjCProperty &&
Daniel Jasper20a0f8c2013-07-11 21:02:56 +00001170 (Right.is(tok::kw_const) || (Right.is(tok::identifier) && Right.Next &&
1171 Right.Next->isNot(tok::l_paren))))
Daniel Jasper53eb05a2013-10-18 16:34:40 +00001172 return 100;
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001173
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001174 // In for-loops, prefer breaking at ',' and ';'.
Manuel Klimekb3987012013-05-29 14:47:47 +00001175 if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
Daniel Jasper7d812812013-02-21 15:00:29 +00001176 return 4;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001177
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001178 // In Objective-C method expressions, prefer breaking before "param:" over
1179 // breaking after it.
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001180 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001181 return 0;
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001182 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001183 return 20;
1184
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001185 if (Left.is(tok::l_paren) && InFunctionDecl)
Daniel Jasper1407bee2013-04-11 14:29:13 +00001186 return 100;
Daniel Jasperac3223e2013-04-10 09:49:49 +00001187 if (Left.opensScope())
Daniel Jasper47066e42013-10-25 14:29:37 +00001188 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
1189 : 19;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001190
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001191 if (Right.is(tok::lessless)) {
1192 if (Left.is(tok::string_literal)) {
Alexander Kornienko00895102013-06-05 14:09:10 +00001193 StringRef Content = Left.TokenText;
Daniel Jasper20376982013-09-29 12:02:57 +00001194 if (Content.startswith("\""))
1195 Content = Content.drop_front(1);
1196 if (Content.endswith("\""))
1197 Content = Content.drop_back(1);
1198 Content = Content.trim();
Daniel Jasperbfa1edd2013-03-14 14:00:17 +00001199 if (Content.size() > 1 &&
1200 (Content.back() == ':' || Content.back() == '='))
Daniel Jasper9637dda2013-07-15 14:33:14 +00001201 return 25;
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001202 }
Daniel Jasper0c368782013-07-15 15:04:42 +00001203 return 1; // Breaking at a << is really cheap.
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001204 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001205 if (Left.Type == TT_ConditionalExpr)
Daniel Jasper518ee342013-02-26 13:59:14 +00001206 return prec::Conditional;
Manuel Klimekb3987012013-05-29 14:47:47 +00001207 prec::Level Level = Left.getPrecedence();
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001208
1209 if (Level != prec::Unknown)
1210 return Level;
Daniel Jasper24849712013-03-01 16:48:32 +00001211
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001212 return 3;
1213}
1214
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001215bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001216 const FormatToken &Left,
1217 const FormatToken &Right) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001218 if (Right.is(tok::hashhash))
1219 return Left.is(tok::hash);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001220 if (Left.isOneOf(tok::hashhash, tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001221 return Right.is(tok::hash);
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001222 if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1223 return Style.SpaceInEmptyParentheses;
1224 if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
Daniel Jasper34f3d052013-08-21 08:39:01 +00001225 return (Right.Type == TT_CastRParen ||
1226 (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen))
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001227 ? Style.SpacesInCStyleCastParentheses
1228 : Style.SpacesInParentheses;
Daniel Jasperd8ee5c12013-10-29 14:52:02 +00001229 if (Style.SpacesInAngles &&
1230 ((Left.Type == TT_TemplateOpener) != (Right.Type == TT_TemplateCloser)))
1231 return true;
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001232 if (Right.isOneOf(tok::semi, tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001233 return false;
1234 if (Right.is(tok::less) &&
1235 (Left.is(tok::kw_template) ||
1236 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1237 return true;
1238 if (Left.is(tok::arrow) || Right.is(tok::arrow))
1239 return false;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001240 if (Left.isOneOf(tok::exclaim, tok::tilde))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001241 return false;
1242 if (Left.is(tok::at) &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001243 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1244 tok::numeric_constant, tok::l_paren, tok::l_brace,
1245 tok::kw_true, tok::kw_false))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001246 return false;
1247 if (Left.is(tok::coloncolon))
1248 return false;
1249 if (Right.is(tok::coloncolon))
Daniel Jasper78a4e612013-10-12 05:16:06 +00001250 return (Left.is(tok::less) && Style.Standard == FormatStyle::LS_Cpp03) ||
1251 !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren,
1252 tok::r_paren, tok::less);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001253 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001254 return false;
Daniel Jasperc47d7f12013-07-01 09:47:25 +00001255 if (Right.is(tok::ellipsis))
Daniel Jasperb3c887d2013-10-20 16:56:16 +00001256 return Left.Tok.isLiteral();
Manuel Klimek31e44f72013-09-04 08:20:47 +00001257 if (Left.is(tok::l_square) && Right.is(tok::amp))
1258 return false;
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001259 if (Right.Type == TT_PointerOrReference)
Manuel Klimekb3987012013-05-29 14:47:47 +00001260 return Left.Tok.isLiteral() ||
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001261 ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
1262 !Style.PointerBindsToType);
Daniel Jasper3ff4a2f2013-05-28 15:27:10 +00001263 if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
Daniel Jasper395228f2013-05-08 14:58:20 +00001264 (Left.Type != TT_PointerOrReference || Style.PointerBindsToType))
1265 return true;
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001266 if (Left.Type == TT_PointerOrReference)
Daniel Jasper3a1847e2013-07-01 09:34:09 +00001267 return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
Daniel Jasper9322aae2013-03-20 09:53:18 +00001268 ((Right.Type != TT_PointerOrReference) &&
Daniel Jasper81d2d382013-04-01 17:13:26 +00001269 Right.isNot(tok::l_paren) && Style.PointerBindsToType &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001270 Left.Previous &&
1271 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001272 if (Right.is(tok::star) && Left.is(tok::l_paren))
1273 return false;
Nico Weber051860e2013-02-10 02:08:05 +00001274 if (Left.is(tok::l_square))
Daniel Jaspera07aa662013-10-22 15:30:28 +00001275 return Left.Type == TT_ArrayInitializerLSquare &&
1276 Right.isNot(tok::r_square);
Nico Weber051860e2013-02-10 02:08:05 +00001277 if (Right.is(tok::r_square))
Daniel Jaspera07aa662013-10-22 15:30:28 +00001278 return Right.MatchingParen &&
1279 Right.MatchingParen->Type == TT_ArrayInitializerLSquare;
Daniel Jasperec172262013-08-30 10:36:58 +00001280 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr &&
Daniel Jasper567dcf92013-09-05 09:29:45 +00001281 Right.Type != TT_LambdaLSquare && Left.isNot(tok::numeric_constant))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001282 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001283 if (Left.is(tok::colon))
1284 return Left.Type != TT_ObjCMethodExpr;
1285 if (Right.is(tok::colon))
Daniel Jasperab3ce592013-08-01 22:05:00 +00001286 return Right.Type != TT_ObjCMethodExpr && !Left.is(tok::question);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001287 if (Right.is(tok::l_paren)) {
Daniel Jaspere0fa4c52013-07-17 20:25:02 +00001288 if (Left.is(tok::r_paren) && Left.MatchingParen &&
1289 Left.MatchingParen->Previous &&
1290 Left.MatchingParen->Previous->is(tok::kw___attribute))
1291 return true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001292 return Line.Type == LT_ObjCDecl ||
Daniel Jasper34f3d052013-08-21 08:39:01 +00001293 Left.isOneOf(tok::kw_return, tok::kw_new, tok::kw_delete,
1294 tok::semi) ||
1295 (Style.SpaceAfterControlStatementKeyword &&
1296 Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
1297 tok::kw_catch));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001298 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001299 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001300 return false;
1301 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
Daniel Jasper567dcf92013-09-05 09:29:45 +00001302 return !Left.Children.empty(); // No spaces in "{}".
Daniel Jasper2424eef2013-05-23 10:15:45 +00001303 if (Left.is(tok::l_brace) || Right.is(tok::r_brace))
Daniel Jasperb5dc3f42013-07-16 18:22:10 +00001304 return !Style.Cpp11BracedListStyle;
Daniel Jasper1bee0732013-05-23 18:05:18 +00001305 if (Right.Type == TT_UnaryOperator)
1306 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1307 (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
Daniel Jasperce933562013-05-23 21:35:49 +00001308 if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) &&
Manuel Klimek31e44f72013-09-04 08:20:47 +00001309 Right.is(tok::l_brace) && Right.getNextNonComment() &&
1310 Right.BlockKind != BK_Block)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001311 return false;
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001312 if (Left.is(tok::period) || Right.is(tok::period))
1313 return false;
Nico Weber861576b2013-06-26 00:15:19 +00001314 if (Left.Type == TT_BlockComment && Left.TokenText.endswith("=*/"))
1315 return false;
Alexander Kornienkodaa07e92013-09-10 13:41:43 +00001316 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
1317 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001318 return true;
1319}
1320
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001321bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001322 const FormatToken &Tok) {
1323 if (Tok.Tok.getIdentifierInfo() && Tok.Previous->Tok.getIdentifierInfo())
Daniel Jasper2b4c9242013-02-11 08:01:18 +00001324 return true; // Never ever merge two identifiers.
Daniel Jasper1d82b1a2013-10-11 19:45:02 +00001325 if (Tok.Previous->Type == TT_ImplicitStringLiteral)
1326 return Tok.WhitespaceRange.getBegin() != Tok.WhitespaceRange.getEnd();
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001327 if (Line.Type == LT_ObjCMethodDecl) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001328 if (Tok.Previous->Type == TT_ObjCMethodSpecifier)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001329 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001330 if (Tok.Previous->is(tok::r_paren) && Tok.is(tok::identifier))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001331 // Don't space between ')' and <id>
1332 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001333 }
1334 if (Line.Type == LT_ObjCProperty &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001335 (Tok.is(tok::equal) || Tok.Previous->is(tok::equal)))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001336 return false;
1337
Daniel Jasper2ca37412013-07-09 14:36:48 +00001338 if (Tok.Type == TT_TrailingReturnArrow ||
1339 Tok.Previous->Type == TT_TrailingReturnArrow)
1340 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001341 if (Tok.Previous->is(tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001342 return true;
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001343 if (Tok.is(tok::comma))
1344 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001345 if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
1346 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001347 if (Tok.Previous->Tok.is(tok::kw_operator))
Daniel Jasper52af9442013-10-29 12:24:23 +00001348 return Tok.is(tok::coloncolon);
Daniel Jasper2b4c9242013-02-11 08:01:18 +00001349 if (Tok.Type == TT_OverloadedOperatorLParen)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001350 return false;
1351 if (Tok.is(tok::colon))
Manuel Klimekb3987012013-05-29 14:47:47 +00001352 return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Daniel Jasperab3ce592013-08-01 22:05:00 +00001353 Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr &&
1354 !Tok.Previous->is(tok::question);
Manuel Klimekb3987012013-05-29 14:47:47 +00001355 if (Tok.Previous->Type == TT_UnaryOperator ||
1356 Tok.Previous->Type == TT_CastRParen)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001357 return false;
Manuel Klimekb3987012013-05-29 14:47:47 +00001358 if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) {
Daniel Jasper29f123b2013-02-08 15:28:42 +00001359 return Tok.Type == TT_TemplateCloser &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001360 Tok.Previous->Type == TT_TemplateCloser &&
Daniel Jasperd8ee5c12013-10-29 14:52:02 +00001361 (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001362 }
Alexander Kornienko54a38bd2013-03-20 16:41:56 +00001363 if (Tok.isOneOf(tok::arrowstar, tok::periodstar) ||
Manuel Klimekb3987012013-05-29 14:47:47 +00001364 Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar))
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001365 return false;
Daniel Jasper9b4de852013-09-25 15:15:02 +00001366 if (!Style.SpaceBeforeAssignmentOperators &&
1367 Tok.getPrecedence() == prec::Assignment)
1368 return false;
Daniel Jasper1dc6f742013-08-07 16:29:23 +00001369 if ((Tok.Type == TT_BinaryOperator && !Tok.Previous->is(tok::l_paren)) ||
1370 Tok.Previous->Type == TT_BinaryOperator)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001371 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001372 if (Tok.Previous->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001373 return false;
Daniel Jaspera4dd9822013-08-28 08:24:04 +00001374 if (Tok.is(tok::less) && Tok.Previous->isNot(tok::l_paren) &&
1375 Line.First->is(tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001376 return true;
1377 if (Tok.Type == TT_TrailingUnaryOperator)
1378 return false;
Manuel Klimekb3987012013-05-29 14:47:47 +00001379 return spaceRequiredBetween(Line, *Tok.Previous, Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001380}
1381
Daniel Jasperebaa1712013-09-17 09:52:48 +00001382bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
1383 const FormatToken &Right) {
1384 if (Right.is(tok::comment)) {
1385 return Right.NewlinesBefore > 0;
1386 } else if (Right.Previous->isTrailingComment() ||
1387 (Right.is(tok::string_literal) &&
1388 Right.Previous->is(tok::string_literal))) {
1389 return true;
1390 } else if (Right.Previous->IsUnterminatedLiteral) {
1391 return true;
1392 } else if (Right.is(tok::lessless) && Right.Next &&
1393 Right.Previous->is(tok::string_literal) &&
1394 Right.Next->is(tok::string_literal)) {
1395 return true;
1396 } else if (Right.Previous->ClosesTemplateDeclaration &&
1397 Right.Previous->MatchingParen &&
1398 Right.Previous->MatchingParen->BindingStrength == 1 &&
1399 Style.AlwaysBreakTemplateDeclarations) {
1400 // FIXME: Fix horrible hack of using BindingStrength to find top-level <>.
1401 return true;
1402 } else if (Right.Type == TT_CtorInitializerComma &&
Daniel Jasper19ccb122013-10-08 05:11:18 +00001403 Style.BreakConstructorInitializersBeforeComma &&
1404 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
Daniel Jasperebaa1712013-09-17 09:52:48 +00001405 return true;
1406 } else if (Right.Previous->BlockKind == BK_Block &&
Alexander Kornienko01fe9f92013-10-10 13:36:20 +00001407 Right.Previous->isNot(tok::r_brace) && Right.isNot(tok::r_brace)) {
Daniel Jasperebaa1712013-09-17 09:52:48 +00001408 return true;
1409 } else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) {
1410 return Style.BreakBeforeBraces == FormatStyle::BS_Allman;
1411 }
1412 return false;
1413}
1414
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001415bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001416 const FormatToken &Right) {
1417 const FormatToken &Left = *Right.Previous;
Daniel Jasper6561f6a2013-07-09 07:43:55 +00001418 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001419 return true;
Nico Weberf2ff8122013-05-26 05:39:26 +00001420 if (Right.is(tok::colon) &&
Daniel Jasper3c6aea72013-10-24 10:31:50 +00001421 (Right.Type == TT_DictLiteral || Right.Type == TT_ObjCMethodExpr))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001422 return false;
Nico Weberf2ff8122013-05-26 05:39:26 +00001423 if (Left.is(tok::colon) &&
Daniel Jasper3c6aea72013-10-24 10:31:50 +00001424 (Left.Type == TT_DictLiteral || Left.Type == TT_ObjCMethodExpr))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001425 return true;
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001426 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001427 return true;
Daniel Jasperaa9e7b12013-08-01 13:46:58 +00001428 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
1429 return true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001430 if (Left.ClosesTemplateDeclaration)
1431 return true;
Daniel Jasperab3ce592013-08-01 22:05:00 +00001432 if ((Right.Type == TT_ConditionalExpr &&
1433 !(Right.is(tok::colon) && Left.is(tok::question))) ||
1434 Right.is(tok::question))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001435 return true;
Daniel Jasper6cabab42013-02-14 08:42:54 +00001436 if (Right.Type == TT_RangeBasedForLoopColon ||
Daniel Jasperc476ea92013-08-28 07:27:35 +00001437 Right.Type == TT_OverloadedOperatorLParen ||
1438 Right.Type == TT_OverloadedOperator)
Daniel Jasper6cabab42013-02-14 08:42:54 +00001439 return false;
Daniel Jasperc194c952013-05-06 06:45:09 +00001440 if (Left.Type == TT_RangeBasedForLoopColon)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001441 return true;
Daniel Jasper7d812812013-02-21 15:00:29 +00001442 if (Right.Type == TT_RangeBasedForLoopColon)
1443 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001444 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
1445 Left.Type == TT_UnaryOperator || Left.Type == TT_ConditionalExpr ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001446 Left.isOneOf(tok::question, tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001447 return false;
1448 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
1449 return false;
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001450 if (Left.Previous) {
1451 if (Left.is(tok::l_paren) && Right.is(tok::l_paren) &&
1452 Left.Previous->is(tok::kw___attribute))
1453 return false;
Daniel Jasper2ca37412013-07-09 14:36:48 +00001454 if (Left.is(tok::l_paren) && (Left.Previous->Type == TT_BinaryOperator ||
Daniel Jasper5e2169f2013-07-18 14:46:07 +00001455 Left.Previous->Type == TT_CastRParen))
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001456 return false;
1457 }
Daniel Jasper84379572013-10-30 13:54:53 +00001458 if (Right.Type == TT_ImplicitStringLiteral)
1459 return false;
Daniel Jasper65d2c382013-06-06 16:08:57 +00001460 if (Right.isTrailingComment())
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001461 // We rely on MustBreakBefore being set correctly here as we should not
1462 // change the "binding" behavior of a comment.
1463 return false;
1464
Daniel Jasper567dcf92013-09-05 09:29:45 +00001465 if (Right.is(tok::r_paren) || Right.Type == TT_TemplateCloser)
1466 return false;
1467
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001468 // We only break before r_brace if there was a corresponding break before
1469 // the l_brace, which is tracked by BreakBeforeClosingBrace.
Daniel Jasper567dcf92013-09-05 09:29:45 +00001470 if (Right.is(tok::r_brace))
1471 return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001472
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001473 // Allow breaking after a trailing 'const', e.g. after a method declaration,
1474 // unless it is follow by ';', '{' or '='.
Manuel Klimekb3987012013-05-29 14:47:47 +00001475 if (Left.is(tok::kw_const) && Left.Previous != NULL &&
1476 Left.Previous->is(tok::r_paren))
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001477 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001478
Daniel Jasper8ef19a22013-03-14 09:50:46 +00001479 if (Right.is(tok::kw___attribute))
1480 return true;
1481
Daniel Jasper3a204412013-02-23 07:46:38 +00001482 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
1483 return true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001484
1485 if (Left.Type == TT_CtorInitializerComma &&
1486 Style.BreakConstructorInitializersBeforeComma)
1487 return false;
Daniel Jasper19ccb122013-10-08 05:11:18 +00001488 if (Right.Type == TT_CtorInitializerComma &&
1489 Style.BreakConstructorInitializersBeforeComma)
1490 return true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001491 if (Right.isBinaryOperator() && Style.BreakBeforeBinaryOperators)
1492 return true;
Daniel Jasper26356cc2013-09-17 08:15:46 +00001493 if (Left.is(tok::greater) && Right.is(tok::greater) &&
1494 Left.Type != TT_TemplateCloser)
1495 return false;
Daniel Jaspera07aa662013-10-22 15:30:28 +00001496 if (Left.Type == TT_ArrayInitializerLSquare)
1497 return true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001498 return (Left.isBinaryOperator() && Left.isNot(tok::lessless) &&
1499 !Style.BreakBeforeBinaryOperators) ||
Daniel Jasper6b119d62013-04-05 17:22:09 +00001500 Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
1501 tok::kw_class, tok::kw_struct) ||
Daniel Jaspera07aa662013-10-22 15:30:28 +00001502 Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon,
1503 tok::l_square, tok::at) ||
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001504 (Left.is(tok::r_paren) &&
Daniel Jaspere033e872013-05-21 09:16:31 +00001505 Right.isOneOf(tok::identifier, tok::kw_const, tok::kw___attribute)) ||
Daniel Jaspera07aa662013-10-22 15:30:28 +00001506 (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001507}
1508
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001509void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
1510 llvm::errs() << "AnnotatedTokens:\n";
Manuel Klimekb3987012013-05-29 14:47:47 +00001511 const FormatToken *Tok = Line.First;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001512 while (Tok) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001513 llvm::errs() << " M=" << Tok->MustBreakBefore
Daniel Jasperc7bd68f2013-07-10 14:02:49 +00001514 << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
1515 << " S=" << Tok->SpacesRequiredBefore
1516 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
Manuel Klimekae76f7f2013-10-11 21:25:45 +00001517 << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
1518 << " FakeLParens=";
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001519 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
1520 llvm::errs() << Tok->FakeLParens[i] << "/";
1521 llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
Manuel Klimekae76f7f2013-10-11 21:25:45 +00001522 if (Tok->Next == NULL)
1523 assert(Tok == Line.Last);
Manuel Klimekb3987012013-05-29 14:47:47 +00001524 Tok = Tok->Next;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001525 }
1526 llvm::errs() << "----\n";
1527}
1528
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001529} // namespace format
1530} // namespace clang