blob: 7e794a3d2d546f1c47d3b236563852837033216e [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),
35 KeywordVirtualFound(false), NameFound(false), AutoFound(false),
36 Ident_in(Ident_in) {
Nico Weber27268772013-06-26 00:30:14 +000037 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
Daniel Jasper32d28ee2013-01-29 21:01:14 +000038 }
39
Nico Weber95e8e462013-02-12 16:17:07 +000040private:
Daniel Jasper32d28ee2013-01-29 21:01:14 +000041 bool parseAngle() {
42 if (CurrentToken == NULL)
43 return false;
Daniel Jasper923ebef2013-03-14 13:45:21 +000044 ScopedContextCreator ContextCreator(*this, tok::less, 10);
Manuel Klimekb3987012013-05-29 14:47:47 +000045 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper4e778092013-02-06 10:05:46 +000046 Contexts.back().IsExpression = false;
Daniel Jasper32d28ee2013-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 Kornienkoe74de282013-03-13 14:41:29 +000055 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
Daniel Jasper5d823e32013-05-15 13:46:48 +000056 tok::question, tok::colon))
57 return false;
Daniel Jasper0348be02013-06-01 18:56:00 +000058 // If a && or || is found and interpreted as a binary operator, this set
Daniel Jasper15f33f02013-06-03 16:16:41 +000059 // of angles is likely part of something like "a < b && c > d". If the
Daniel Jasper0348be02013-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 Klimekb3987012013-05-29 14:47:47 +000064 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
Daniel Jasper0348be02013-06-01 18:56:00 +000065 (CurrentToken->Previous->Type == TT_BinaryOperator ||
66 Contexts[Contexts.size() - 2].IsExpression) &&
Manuel Klimekb3987012013-05-29 14:47:47 +000067 Line.First->isNot(tok::kw_template))
Daniel Jasper32d28ee2013-01-29 21:01:14 +000068 return false;
Daniel Jasper9fc56f22013-02-14 15:01:34 +000069 updateParameterCount(Left, CurrentToken);
Daniel Jasper32d28ee2013-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 Jasper923ebef2013-03-14 13:45:21 +000079 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
Daniel Jasper4e778092013-02-06 10:05:46 +000080
81 // FIXME: This is a bit of a hack. Do better.
82 Contexts.back().ColonIsForRangeExpr =
83 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
84
Daniel Jasper32d28ee2013-01-29 21:01:14 +000085 bool StartsObjCMethodExpr = false;
Manuel Klimekb3987012013-05-29 14:47:47 +000086 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000087 if (CurrentToken->is(tok::caret)) {
88 // ^( starts a block.
89 Left->Type = TT_ObjCBlockLParen;
Manuel Klimekb3987012013-05-29 14:47:47 +000090 } else if (FormatToken *MaybeSel = Left->Previous) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000091 // @selector( starts a selector.
Manuel Klimekb3987012013-05-29 14:47:47 +000092 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
93 MaybeSel->Previous->is(tok::at)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000094 StartsObjCMethodExpr = true;
95 }
96 }
97
Daniel Jasperb644dd62013-08-13 09:09:09 +000098 if (Left->Previous && Left->Previous->isOneOf(tok::kw_static_assert,
99 tok::kw_if, tok::kw_while))
Daniel Jasperb7000ca2013-08-01 17:58:23 +0000100 Contexts.back().IsExpression = true;
101
Daniel Jasper4e778092013-02-06 10:05:46 +0000102 if (StartsObjCMethodExpr) {
103 Contexts.back().ColonIsObjCMethodExpr = true;
104 Left->Type = TT_ObjCMethodExpr;
105 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000106
Daniel Jasper431f5912013-05-28 08:33:00 +0000107 bool MightBeFunctionType = CurrentToken->is(tok::star);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000108 bool HasMultipleLines = false;
109 bool HasMultipleParametersOnALine = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000110 while (CurrentToken != NULL) {
111 // LookForDecls is set when "if (" has been seen. Check for
112 // 'identifier' '*' 'identifier' followed by not '=' -- this
113 // '*' has to be a binary operator but determineStarAmpUsage() will
114 // categorize it as an unary operator, so set the right type here.
Manuel Klimekb3987012013-05-29 14:47:47 +0000115 if (LookForDecls && CurrentToken->Next) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000116 FormatToken *Prev = CurrentToken->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000117 if (Prev) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000118 FormatToken *PrevPrev = Prev->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000119 FormatToken *Next = CurrentToken->Next;
120 if (PrevPrev && PrevPrev->is(tok::identifier) &&
121 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
122 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
123 Prev->Type = TT_BinaryOperator;
124 LookForDecls = false;
125 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000126 }
127 }
128
Daniel Jasper53352602013-08-12 12:16:34 +0000129 if (CurrentToken->Previous->Type == TT_PointerOrReference &&
130 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
131 tok::coloncolon))
132 MightBeFunctionType = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000133 if (CurrentToken->is(tok::r_paren)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000134 if (MightBeFunctionType && CurrentToken->Next &&
Daniel Jaspere7d3bff2013-07-16 11:37:21 +0000135 (CurrentToken->Next->is(tok::l_paren) ||
136 (CurrentToken->Next->is(tok::l_square) &&
137 !Contexts.back().IsExpression)))
Daniel Jasper431f5912013-05-28 08:33:00 +0000138 Left->Type = TT_FunctionTypeLParen;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000139 Left->MatchingParen = CurrentToken;
140 CurrentToken->MatchingParen = Left;
141
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000142 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000143 CurrentToken->Type = TT_ObjCMethodExpr;
144 if (Contexts.back().FirstObjCSelectorName != NULL) {
145 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
146 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000147 }
148 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000149
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000150 if (!HasMultipleLines)
151 Left->PackingKind = PPK_Inconclusive;
152 else if (HasMultipleParametersOnALine)
153 Left->PackingKind = PPK_BinPacked;
154 else
155 Left->PackingKind = PPK_OnePerLine;
156
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000157 next();
158 return true;
159 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000160 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000161 return false;
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000162 updateParameterCount(Left, CurrentToken);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000163 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
164 !CurrentToken->Next->HasUnescapedNewline &&
165 !CurrentToken->Next->isTrailingComment())
166 HasMultipleParametersOnALine = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000167 if (!consumeToken())
168 return false;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000169 if (CurrentToken && CurrentToken->HasUnescapedNewline)
170 HasMultipleLines = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000171 }
172 return false;
173 }
174
175 bool parseSquare() {
176 if (!CurrentToken)
177 return false;
178
Alexander Kornienkod71b15b2013-06-17 13:19:53 +0000179 // A '[' could be an index subscript (after an identifier or after
Nico Weber051860e2013-02-10 02:08:05 +0000180 // ')' or ']'), it could be the start of an Objective-C method
181 // expression, or it could the the start of an Objective-C array literal.
Manuel Klimekb3987012013-05-29 14:47:47 +0000182 FormatToken *Left = CurrentToken->Previous;
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000183 FormatToken *Parent = Left->getPreviousNonComment();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000184 bool StartsObjCMethodExpr =
Daniel Jasper567dcf92013-09-05 09:29:45 +0000185 Contexts.back().CanBeExpression && Left->Type != TT_LambdaLSquare &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000186 (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
187 tok::kw_return, tok::kw_throw) ||
Daniel Jasperac3223e2013-04-10 09:49:49 +0000188 Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000189 Parent->Type == TT_CastRParen ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000190 getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
Daniel Jasper923ebef2013-03-14 13:45:21 +0000191 ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
Daniel Jasper6f21a982013-03-13 07:49:51 +0000192 Contexts.back().IsExpression = true;
Nico Weber051860e2013-02-10 02:08:05 +0000193 bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000194
Daniel Jasper4e778092013-02-06 10:05:46 +0000195 if (StartsObjCMethodExpr) {
196 Contexts.back().ColonIsObjCMethodExpr = true;
197 Left->Type = TT_ObjCMethodExpr;
Nico Weber051860e2013-02-10 02:08:05 +0000198 } else if (StartsObjCArrayLiteral) {
199 Left->Type = TT_ObjCArrayLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000200 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000201
202 while (CurrentToken != NULL) {
203 if (CurrentToken->is(tok::r_square)) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000204 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
205 Left->Type == TT_ObjCMethodExpr) {
Nico Webere8a97982013-02-06 06:20:11 +0000206 // An ObjC method call is rarely followed by an open parenthesis.
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000207 // FIXME: Do we incorrectly label ":" with this?
208 StartsObjCMethodExpr = false;
209 Left->Type = TT_Unknown;
210 }
Daniel Jasper01786732013-02-04 07:21:18 +0000211 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000212 CurrentToken->Type = TT_ObjCMethodExpr;
Nico Webere8a97982013-02-06 06:20:11 +0000213 // determineStarAmpUsage() thinks that '*' '[' is allocating an
214 // array of pointers, but if '[' starts a selector then '*' is a
215 // binary operator.
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +0000216 if (Parent != NULL && Parent->Type == TT_PointerOrReference)
Nico Weber4ed7f3e2013-02-06 16:54:35 +0000217 Parent->Type = TT_BinaryOperator;
Nico Weber051860e2013-02-10 02:08:05 +0000218 } else if (StartsObjCArrayLiteral) {
219 CurrentToken->Type = TT_ObjCArrayLiteral;
Daniel Jasper01786732013-02-04 07:21:18 +0000220 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000221 Left->MatchingParen = CurrentToken;
222 CurrentToken->MatchingParen = Left;
Daniel Jasper4e778092013-02-06 10:05:46 +0000223 if (Contexts.back().FirstObjCSelectorName != NULL)
224 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
225 Contexts.back().LongestObjCSelectorName;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000226 next();
227 return true;
228 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000229 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000230 return false;
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000231 updateParameterCount(Left, CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000232 if (!consumeToken())
233 return false;
234 }
235 return false;
236 }
237
238 bool parseBrace() {
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000239 if (CurrentToken != NULL) {
240 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
Manuel Klimekb3987012013-05-29 14:47:47 +0000241 FormatToken *Left = CurrentToken->Previous;
Nico Weberf2ff8122013-05-26 05:39:26 +0000242
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000243 FormatToken *Parent = Left->getPreviousNonComment();
Nico Weberf2ff8122013-05-26 05:39:26 +0000244 bool StartsObjCDictLiteral = Parent && Parent->is(tok::at);
245 if (StartsObjCDictLiteral) {
246 Contexts.back().ColonIsObjCDictLiteral = true;
247 Left->Type = TT_ObjCDictLiteral;
248 }
249
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000250 while (CurrentToken != NULL) {
251 if (CurrentToken->is(tok::r_brace)) {
Nico Weberf2ff8122013-05-26 05:39:26 +0000252 if (StartsObjCDictLiteral)
253 CurrentToken->Type = TT_ObjCDictLiteral;
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000254 Left->MatchingParen = CurrentToken;
255 CurrentToken->MatchingParen = Left;
256 next();
257 return true;
258 }
259 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
260 return false;
261 updateParameterCount(Left, CurrentToken);
262 if (!consumeToken())
263 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000264 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000265 }
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000266 // No closing "}" found, this probably starts a definition.
267 Line.StartsDefinition = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000268 return true;
269 }
Daniel Jasperc4615b72013-02-20 12:56:39 +0000270
Manuel Klimekb3987012013-05-29 14:47:47 +0000271 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000272 if (Current->is(tok::comma)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000273 ++Left->ParameterCount;
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000274 if (!Left->Role)
275 Left->Role.reset(new CommaSeparatedList(Style));
276 Left->Role->CommaFound(Current);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000277 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000278 Left->ParameterCount = 1;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000279 }
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000280 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000281
282 bool parseConditional() {
283 while (CurrentToken != NULL) {
284 if (CurrentToken->is(tok::colon)) {
285 CurrentToken->Type = TT_ConditionalExpr;
286 next();
287 return true;
288 }
289 if (!consumeToken())
290 return false;
291 }
292 return false;
293 }
294
295 bool parseTemplateDeclaration() {
296 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
297 CurrentToken->Type = TT_TemplateOpener;
298 next();
299 if (!parseAngle())
300 return false;
Daniel Jasper34511fb2013-02-19 17:14:38 +0000301 if (CurrentToken != NULL)
Manuel Klimekb3987012013-05-29 14:47:47 +0000302 CurrentToken->Previous->ClosesTemplateDeclaration = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000303 return true;
304 }
305 return false;
306 }
307
308 bool consumeToken() {
Manuel Klimekb3987012013-05-29 14:47:47 +0000309 FormatToken *Tok = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000310 next();
Manuel Klimekb3987012013-05-29 14:47:47 +0000311 switch (Tok->Tok.getKind()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000312 case tok::plus:
313 case tok::minus:
Manuel Klimekb3987012013-05-29 14:47:47 +0000314 if (Tok->Previous == NULL && Line.MustBeDeclaration)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000315 Tok->Type = TT_ObjCMethodSpecifier;
316 break;
317 case tok::colon:
Manuel Klimekb3987012013-05-29 14:47:47 +0000318 if (Tok->Previous == NULL)
Daniel Jaspercf6d76a2013-03-18 12:50:26 +0000319 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000320 // Colons from ?: are handled in parseConditional().
Manuel Klimekb3987012013-05-29 14:47:47 +0000321 if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000322 Tok->Type = TT_CtorInitializerColon;
Nico Weberf2ff8122013-05-26 05:39:26 +0000323 } else if (Contexts.back().ColonIsObjCDictLiteral) {
324 Tok->Type = TT_ObjCDictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000325 } else if (Contexts.back().ColonIsObjCMethodExpr ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000326 Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000327 Tok->Type = TT_ObjCMethodExpr;
Manuel Klimekb3987012013-05-29 14:47:47 +0000328 Tok->Previous->Type = TT_ObjCSelectorName;
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000329 if (Tok->Previous->ColumnWidth >
Alexander Kornienko00895102013-06-05 14:09:10 +0000330 Contexts.back().LongestObjCSelectorName) {
Alexander Kornienko01fe9f92013-10-10 13:36:20 +0000331 Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
Alexander Kornienko00895102013-06-05 14:09:10 +0000332 }
Daniel Jasper4e778092013-02-06 10:05:46 +0000333 if (Contexts.back().FirstObjCSelectorName == NULL)
Manuel Klimekb3987012013-05-29 14:47:47 +0000334 Contexts.back().FirstObjCSelectorName = Tok->Previous;
Daniel Jasper4e778092013-02-06 10:05:46 +0000335 } else if (Contexts.back().ColonIsForRangeExpr) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000336 Tok->Type = TT_RangeBasedForLoopColon;
Alexander Kornienko01fe9f92013-10-10 13:36:20 +0000337 } else if (CurrentToken != NULL &&
338 CurrentToken->is(tok::numeric_constant)) {
339 Tok->Type = TT_BitFieldColon;
Daniel Jaspercea014b2013-10-08 16:24:07 +0000340 } else if (Contexts.size() == 1 && Line.First->isNot(tok::kw_enum)) {
Daniel Jasper6cabab42013-02-14 08:42:54 +0000341 Tok->Type = TT_InheritanceColon;
Daniel Jasper923ebef2013-03-14 13:45:21 +0000342 } else if (Contexts.back().ContextKind == tok::l_paren) {
343 Tok->Type = TT_InlineASMColon;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000344 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000345 break;
346 case tok::kw_if:
347 case tok::kw_while:
348 if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
349 next();
Nico Weber27268772013-06-26 00:30:14 +0000350 if (!parseParens(/*LookForDecls=*/true))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000351 return false;
352 }
353 break;
354 case tok::kw_for:
Daniel Jasper4e778092013-02-06 10:05:46 +0000355 Contexts.back().ColonIsForRangeExpr = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000356 next();
357 if (!parseParens())
358 return false;
359 break;
360 case tok::l_paren:
361 if (!parseParens())
362 return false;
Daniel Jasper1407bee2013-04-11 14:29:13 +0000363 if (Line.MustBeDeclaration && NameFound && !Contexts.back().IsExpression)
Daniel Jasper3c08a812013-02-24 18:54:32 +0000364 Line.MightBeFunctionDecl = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000365 break;
366 case tok::l_square:
367 if (!parseSquare())
368 return false;
369 break;
370 case tok::l_brace:
371 if (!parseBrace())
372 return false;
373 break;
374 case tok::less:
Daniel Jasper0236dd02013-07-30 22:37:19 +0000375 if (Tok->Previous && !Tok->Previous->Tok.isLiteral() && parseAngle())
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000376 Tok->Type = TT_TemplateOpener;
377 else {
378 Tok->Type = TT_BinaryOperator;
379 CurrentToken = Tok;
380 next();
381 }
382 break;
383 case tok::r_paren:
384 case tok::r_square:
385 return false;
386 case tok::r_brace:
387 // Lines can start with '}'.
Manuel Klimekb3987012013-05-29 14:47:47 +0000388 if (Tok->Previous != NULL)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000389 return false;
390 break;
391 case tok::greater:
392 Tok->Type = TT_BinaryOperator;
393 break;
394 case tok::kw_operator:
Daniel Jasper174f60f2013-09-02 09:20:39 +0000395 while (CurrentToken &&
396 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000397 if (CurrentToken->isOneOf(tok::star, tok::amp))
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000398 CurrentToken->Type = TT_PointerOrReference;
399 consumeToken();
Daniel Jasper174f60f2013-09-02 09:20:39 +0000400 if (CurrentToken && CurrentToken->Previous->Type == TT_BinaryOperator)
Daniel Jasperc476ea92013-08-28 07:27:35 +0000401 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000402 }
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000403 if (CurrentToken) {
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000404 CurrentToken->Type = TT_OverloadedOperatorLParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000405 if (CurrentToken->Previous->Type == TT_BinaryOperator)
406 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000407 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000408 break;
409 case tok::question:
410 parseConditional();
411 break;
412 case tok::kw_template:
413 parseTemplateDeclaration();
414 break;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000415 case tok::identifier:
Manuel Klimekb3987012013-05-29 14:47:47 +0000416 if (Line.First->is(tok::kw_for) &&
417 Tok->Tok.getIdentifierInfo() == &Ident_in)
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000418 Tok->Type = TT_ObjCForIn;
419 break;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000420 case tok::comma:
421 if (Contexts.back().FirstStartOfName)
422 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000423 if (Contexts.back().InCtorInitializer)
424 Tok->Type = TT_CtorInitializerComma;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000425 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000426 default:
427 break;
428 }
429 return true;
430 }
431
432 void parseIncludeDirective() {
433 next();
434 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
435 next();
436 while (CurrentToken != NULL) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000437 if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000438 CurrentToken->Type = TT_ImplicitStringLiteral;
439 next();
440 }
441 } else {
442 while (CurrentToken != NULL) {
Daniel Jasper3a204412013-02-23 07:46:38 +0000443 if (CurrentToken->is(tok::string_literal))
444 // Mark these string literals as "implicit" literals, too, so that
445 // they are not split or line-wrapped.
446 CurrentToken->Type = TT_ImplicitStringLiteral;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000447 next();
448 }
449 }
450 }
451
452 void parseWarningOrError() {
453 next();
454 // We still want to format the whitespace left of the first token of the
455 // warning or error.
456 next();
457 while (CurrentToken != NULL) {
458 CurrentToken->Type = TT_ImplicitStringLiteral;
459 next();
460 }
461 }
462
463 void parsePreprocessorDirective() {
464 next();
465 if (CurrentToken == NULL)
466 return;
Alexander Kornienkob18c2582013-10-11 21:43:05 +0000467 if (CurrentToken->Tok.is(tok::numeric_constant)) {
468 CurrentToken->SpacesRequiredBefore = 1;
469 return;
470 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000471 // Hashes in the middle of a line can lead to any strange token
472 // sequence.
Manuel Klimekb3987012013-05-29 14:47:47 +0000473 if (CurrentToken->Tok.getIdentifierInfo() == NULL)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000474 return;
Manuel Klimekb3987012013-05-29 14:47:47 +0000475 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000476 case tok::pp_include:
477 case tok::pp_import:
478 parseIncludeDirective();
479 break;
480 case tok::pp_error:
481 case tok::pp_warning:
482 parseWarningOrError();
483 break;
Daniel Jasperaae7bad2013-04-23 13:54:04 +0000484 case tok::pp_if:
485 case tok::pp_elif:
486 parseLine();
487 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000488 default:
489 break;
490 }
Daniel Jasper5b7e7b02013-02-05 09:34:14 +0000491 while (CurrentToken != NULL)
492 next();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000493 }
494
Nico Weber95e8e462013-02-12 16:17:07 +0000495public:
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000496 LineType parseLine() {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000497 if (CurrentToken->is(tok::hash)) {
498 parsePreprocessorDirective();
499 return LT_PreprocessorDirective;
500 }
501 while (CurrentToken != NULL) {
502 if (CurrentToken->is(tok::kw_virtual))
503 KeywordVirtualFound = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000504 if (!consumeToken())
505 return LT_Invalid;
506 }
507 if (KeywordVirtualFound)
508 return LT_VirtualFunctionDecl;
509
Manuel Klimekb3987012013-05-29 14:47:47 +0000510 if (Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000511 if (Contexts.back().FirstObjCSelectorName != NULL)
512 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
513 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000514 return LT_ObjCMethodDecl;
515 }
516
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000517 return LT_Other;
518 }
519
Nico Weber95e8e462013-02-12 16:17:07 +0000520private:
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000521 void next() {
Daniel Jasper01786732013-02-04 07:21:18 +0000522 if (CurrentToken != NULL) {
523 determineTokenType(*CurrentToken);
Daniel Jasper4e778092013-02-06 10:05:46 +0000524 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
Daniel Jasper01786732013-02-04 07:21:18 +0000525 }
526
Manuel Klimekb3987012013-05-29 14:47:47 +0000527 if (CurrentToken != NULL)
528 CurrentToken = CurrentToken->Next;
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000529
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000530 if (CurrentToken != NULL) {
531 // Reset token type in case we have already looked at it and then
532 // recovered from an error (e.g. failure to find the matching >).
533 if (CurrentToken->Type != TT_LambdaLSquare &&
534 CurrentToken->Type != TT_ImplicitStringLiteral)
535 CurrentToken->Type = TT_Unknown;
536 if (CurrentToken->Role)
537 CurrentToken->Role.reset(NULL);
538 CurrentToken->FakeLParens.clear();
539 CurrentToken->FakeRParens = 0;
540 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000541 }
542
Daniel Jasper4e778092013-02-06 10:05:46 +0000543 /// \brief A struct to hold information valid in a specific context, e.g.
544 /// a pair of parenthesis.
545 struct Context {
Daniel Jasper923ebef2013-03-14 13:45:21 +0000546 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
547 bool IsExpression)
548 : ContextKind(ContextKind), BindingStrength(BindingStrength),
549 LongestObjCSelectorName(0), ColonIsForRangeExpr(false),
Nico Weberf2ff8122013-05-26 05:39:26 +0000550 ColonIsObjCDictLiteral(false), ColonIsObjCMethodExpr(false),
551 FirstObjCSelectorName(NULL), FirstStartOfName(NULL),
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000552 IsExpression(IsExpression), CanBeExpression(true),
553 InCtorInitializer(false) {}
Daniel Jasper01786732013-02-04 07:21:18 +0000554
Daniel Jasper923ebef2013-03-14 13:45:21 +0000555 tok::TokenKind ContextKind;
Daniel Jasper4e778092013-02-06 10:05:46 +0000556 unsigned BindingStrength;
557 unsigned LongestObjCSelectorName;
558 bool ColonIsForRangeExpr;
Nico Weberf2ff8122013-05-26 05:39:26 +0000559 bool ColonIsObjCDictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000560 bool ColonIsObjCMethodExpr;
Manuel Klimekb3987012013-05-29 14:47:47 +0000561 FormatToken *FirstObjCSelectorName;
562 FormatToken *FirstStartOfName;
Daniel Jasper4e778092013-02-06 10:05:46 +0000563 bool IsExpression;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000564 bool CanBeExpression;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000565 bool InCtorInitializer;
Daniel Jasper4e778092013-02-06 10:05:46 +0000566 };
567
568 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
569 /// of each instance.
570 struct ScopedContextCreator {
571 AnnotatingParser &P;
572
Daniel Jasper923ebef2013-03-14 13:45:21 +0000573 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
574 unsigned Increase)
575 : P(P) {
Daniel Jasper2a409b62013-07-08 14:34:09 +0000576 P.Contexts.push_back(Context(ContextKind,
577 P.Contexts.back().BindingStrength + Increase,
578 P.Contexts.back().IsExpression));
Daniel Jasper4e778092013-02-06 10:05:46 +0000579 }
580
581 ~ScopedContextCreator() { P.Contexts.pop_back(); }
582 };
Daniel Jasper01786732013-02-04 07:21:18 +0000583
Manuel Klimekb3987012013-05-29 14:47:47 +0000584 void determineTokenType(FormatToken &Current) {
585 if (Current.getPrecedence() == prec::Assignment &&
Daniel Jasper53352602013-08-12 12:16:34 +0000586 !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
Manuel Klimekb3987012013-05-29 14:47:47 +0000587 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000588 Contexts.back().IsExpression = true;
Manuel Klimekb3987012013-05-29 14:47:47 +0000589 for (FormatToken *Previous = Current.Previous;
Daniel Jasper7e274002013-09-11 20:37:10 +0000590 Previous && !Previous->isOneOf(tok::comma, tok::semi);
Manuel Klimekb3987012013-05-29 14:47:47 +0000591 Previous = Previous->Previous) {
Daniel Jasper9c65b062013-02-27 11:43:50 +0000592 if (Previous->is(tok::r_square))
593 Previous = Previous->MatchingParen;
Daniel Jasper01786732013-02-04 07:21:18 +0000594 if (Previous->Type == TT_BinaryOperator &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000595 Previous->isOneOf(tok::star, tok::amp)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000596 Previous->Type = TT_PointerOrReference;
597 }
Daniel Jasper01786732013-02-04 07:21:18 +0000598 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000599 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw) ||
Nico Weber95e8e462013-02-12 16:17:07 +0000600 (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
Daniel Jasper378d93d2013-05-13 07:14:40 +0000601 !Line.InPPDirective &&
Manuel Klimek2530fd52013-08-12 03:51:17 +0000602 (!Current.Previous ||
Daniel Jasper53352602013-08-12 12:16:34 +0000603 !Current.Previous->isOneOf(tok::kw_for, tok::kw_catch)))) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000604 Contexts.back().IsExpression = true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000605 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000606 for (FormatToken *Previous = Current.Previous;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000607 Previous && Previous->isOneOf(tok::star, tok::amp);
Manuel Klimekb3987012013-05-29 14:47:47 +0000608 Previous = Previous->Previous)
Nico Weber95e8e462013-02-12 16:17:07 +0000609 Previous->Type = TT_PointerOrReference;
Manuel Klimekb3987012013-05-29 14:47:47 +0000610 } else if (Current.Previous &&
611 Current.Previous->Type == TT_CtorInitializerColon) {
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000612 Contexts.back().IsExpression = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000613 Contexts.back().InCtorInitializer = true;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000614 } else if (Current.is(tok::kw_new)) {
615 Contexts.back().CanBeExpression = false;
Daniel Jasper16a69ef2013-05-03 14:41:24 +0000616 } else if (Current.is(tok::semi)) {
617 // This should be the condition or increment in a for-loop.
618 Contexts.back().IsExpression = true;
Nico Weber95e8e462013-02-12 16:17:07 +0000619 }
Daniel Jasper01786732013-02-04 07:21:18 +0000620
621 if (Current.Type == TT_Unknown) {
Daniel Jasper6b3ff8c2013-09-27 08:29:16 +0000622 // Line.MightBeFunctionDecl can only be true after the parentheses of a
623 // function declaration have been found. In this case, 'Current' is a
624 // trailing token of this declaration and thus cannot be a name.
625 if (isStartOfName(Current) && !Line.MightBeFunctionDecl) {
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000626 Contexts.back().FirstStartOfName = &Current;
Daniel Jasper3c08a812013-02-24 18:54:32 +0000627 Current.Type = TT_StartOfName;
Daniel Jasper1407bee2013-04-11 14:29:13 +0000628 NameFound = true;
Daniel Jasper2ca37412013-07-09 14:36:48 +0000629 } else if (Current.is(tok::kw_auto)) {
630 AutoFound = true;
Daniel Jasper3262f4c2013-07-11 14:33:06 +0000631 } else if (Current.is(tok::arrow) && AutoFound &&
632 Line.MustBeDeclaration) {
Daniel Jasper2ca37412013-07-09 14:36:48 +0000633 Current.Type = TT_TrailingReturnArrow;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000634 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000635 Current.Type =
Daniel Jasperd6104f62013-07-05 13:30:40 +0000636 determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
637 Contexts.back().IsExpression);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000638 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000639 Current.Type = determinePlusMinusCaretUsage(Current);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000640 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000641 Current.Type = determineIncrementUsage(Current);
642 } else if (Current.is(tok::exclaim)) {
643 Current.Type = TT_UnaryOperator;
Manuel Klimek31e44f72013-09-04 08:20:47 +0000644 } else if (Current.isBinaryOperator() &&
645 (!Current.Previous ||
646 Current.Previous->isNot(tok::l_square))) {
Daniel Jasper01786732013-02-04 07:21:18 +0000647 Current.Type = TT_BinaryOperator;
648 } else if (Current.is(tok::comment)) {
Alexander Kornienko00895102013-06-05 14:09:10 +0000649 if (Current.TokenText.startswith("//"))
Daniel Jasper01786732013-02-04 07:21:18 +0000650 Current.Type = TT_LineComment;
651 else
652 Current.Type = TT_BlockComment;
Nico Weber37d69312013-02-13 04:13:13 +0000653 } else if (Current.is(tok::r_paren)) {
Daniel Jasperb8b42952013-05-31 16:14:28 +0000654 FormatToken *LeftOfParens = NULL;
655 if (Current.MatchingParen)
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000656 LeftOfParens = Current.MatchingParen->getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000657 bool IsCast = false;
658 bool ParensAreEmpty = Current.Previous == Current.MatchingParen;
659 bool ParensAreType = !Current.Previous ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000660 Current.Previous->Type == TT_PointerOrReference ||
Daniel Jasperb8b42952013-05-31 16:14:28 +0000661 Current.Previous->Type == TT_TemplateCloser ||
662 isSimpleTypeSpecifier(*Current.Previous);
Nico Weber37d69312013-02-13 04:13:13 +0000663 bool ParensCouldEndDecl =
Manuel Klimekb3987012013-05-29 14:47:47 +0000664 Current.Next &&
665 Current.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
Daniel Jasper6a365aa2013-03-13 17:13:53 +0000666 bool IsSizeOfOrAlignOf =
Daniel Jasperb8b42952013-05-31 16:14:28 +0000667 LeftOfParens &&
668 LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
669 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
Daniel Jasper0c368782013-07-15 15:04:42 +0000670 (Contexts.back().IsExpression ||
671 (Current.Next && Current.Next->isBinaryOperator())))
Daniel Jasperb8b42952013-05-31 16:14:28 +0000672 IsCast = true;
Daniel Jasper2a409b62013-07-08 14:34:09 +0000673 if (Current.Next && Current.Next->isNot(tok::string_literal) &&
Daniel Jasperb8b42952013-05-31 16:14:28 +0000674 (Current.Next->Tok.isLiteral() ||
675 Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
676 IsCast = true;
677 // If there is an identifier after the (), it is likely a cast, unless
678 // there is also an identifier before the ().
Daniel Jasperff1a2e52013-06-06 08:20:20 +0000679 if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL ||
680 LeftOfParens->is(tok::kw_return)) &&
Daniel Jasper526df0f2013-07-08 14:58:01 +0000681 LeftOfParens->Type != TT_OverloadedOperator &&
Nico Weber465e8612013-06-25 00:55:57 +0000682 LeftOfParens->Type != TT_TemplateCloser && Current.Next &&
683 Current.Next->is(tok::identifier))
Daniel Jasperb8b42952013-05-31 16:14:28 +0000684 IsCast = true;
685 if (IsCast && !ParensAreEmpty)
Nico Weber37d69312013-02-13 04:13:13 +0000686 Current.Type = TT_CastRParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000687 } else if (Current.is(tok::at) && Current.Next) {
688 switch (Current.Next->Tok.getObjCKeywordID()) {
Daniel Jasper01786732013-02-04 07:21:18 +0000689 case tok::objc_interface:
690 case tok::objc_implementation:
691 case tok::objc_protocol:
692 Current.Type = TT_ObjCDecl;
693 break;
694 case tok::objc_property:
695 Current.Type = TT_ObjCProperty;
696 break;
697 default:
698 break;
699 }
Daniel Jasper5ad390d2013-05-28 11:30:49 +0000700 } else if (Current.is(tok::period)) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000701 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
Daniel Jasper5ad390d2013-05-28 11:30:49 +0000702 if (PreviousNoComment &&
703 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
704 Current.Type = TT_DesignatedInitializerPeriod;
Daniel Jasper01786732013-02-04 07:21:18 +0000705 }
706 }
707 }
708
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000709 /// \brief Take a guess at whether \p Tok starts a name of a function or
710 /// variable declaration.
711 ///
712 /// This is a heuristic based on whether \p Tok is an identifier following
713 /// something that is likely a type.
714 bool isStartOfName(const FormatToken &Tok) {
715 if (Tok.isNot(tok::identifier) || Tok.Previous == NULL)
716 return false;
717
718 // Skip "const" as it does not have an influence on whether this is a name.
719 FormatToken *PreviousNotConst = Tok.Previous;
720 while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const))
721 PreviousNotConst = PreviousNotConst->Previous;
722
723 if (PreviousNotConst == NULL)
724 return false;
725
Daniel Jasper2a409b62013-07-08 14:34:09 +0000726 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
727 PreviousNotConst->Previous &&
728 PreviousNotConst->Previous->is(tok::hash);
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000729
Daniel Jasper92495a82013-08-19 10:16:18 +0000730 if (PreviousNotConst->Type == TT_TemplateCloser)
731 return PreviousNotConst && PreviousNotConst->MatchingParen &&
732 PreviousNotConst->MatchingParen->Previous &&
733 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
734
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000735 return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
736 PreviousNotConst->Type == TT_PointerOrReference ||
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000737 isSimpleTypeSpecifier(*PreviousNotConst);
738 }
739
Daniel Jasper01786732013-02-04 07:21:18 +0000740 /// \brief Return the type of the given token assuming it is * or &.
Manuel Klimekb3987012013-05-29 14:47:47 +0000741 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000742 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasper01786732013-02-04 07:21:18 +0000743 if (PrevToken == NULL)
744 return TT_UnaryOperator;
745
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000746 const FormatToken *NextToken = Tok.getNextNonComment();
Daniel Jasper01786732013-02-04 07:21:18 +0000747 if (NextToken == NULL)
748 return TT_Unknown;
749
Daniel Jasper431f5912013-05-28 08:33:00 +0000750 if (PrevToken->is(tok::coloncolon) ||
751 (PrevToken->is(tok::l_paren) && !IsExpression))
Daniel Jasper8a5d7cd2013-03-01 17:13:29 +0000752 return TT_PointerOrReference;
753
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000754 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
Daniel Jasperd3cf17b2013-03-14 10:50:25 +0000755 tok::comma, tok::semi, tok::kw_return, tok::colon,
Daniel Jasper65da8e92013-09-21 17:31:51 +0000756 tok::equal, tok::kw_delete, tok::kw_sizeof) ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000757 PrevToken->Type == TT_BinaryOperator ||
Daniel Jasper01786732013-02-04 07:21:18 +0000758 PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
759 return TT_UnaryOperator;
760
Nico Webere8a97982013-02-06 06:20:11 +0000761 if (NextToken->is(tok::l_square))
762 return TT_PointerOrReference;
763
Daniel Jasperdb8afe42013-09-10 10:26:38 +0000764 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
765 PrevToken->MatchingParen->Previous &&
766 PrevToken->MatchingParen->Previous->is(tok::kw_typeof))
767 return TT_PointerOrReference;
768
Manuel Klimekb3987012013-05-29 14:47:47 +0000769 if (PrevToken->Tok.isLiteral() ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000770 PrevToken->isOneOf(tok::r_paren, tok::r_square) ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000771 NextToken->Tok.isLiteral() || NextToken->isUnaryOperator())
Daniel Jasper01786732013-02-04 07:21:18 +0000772 return TT_BinaryOperator;
773
Daniel Jasper01786732013-02-04 07:21:18 +0000774 // It is very unlikely that we are going to find a pointer or reference type
775 // definition on the RHS of an assignment.
776 if (IsExpression)
777 return TT_BinaryOperator;
778
779 return TT_PointerOrReference;
780 }
781
Manuel Klimekb3987012013-05-29 14:47:47 +0000782 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000783 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000784 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper01786732013-02-04 07:21:18 +0000785 return TT_UnaryOperator;
786
787 // Use heuristics to recognize unary operators.
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000788 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
789 tok::question, tok::colon, tok::kw_return,
790 tok::kw_case, tok::at, tok::l_brace))
Daniel Jasper01786732013-02-04 07:21:18 +0000791 return TT_UnaryOperator;
792
Nico Weberee0feec2013-02-05 16:21:00 +0000793 // There can't be two consecutive binary operators.
Daniel Jasper01786732013-02-04 07:21:18 +0000794 if (PrevToken->Type == TT_BinaryOperator)
795 return TT_UnaryOperator;
796
797 // Fall back to marking the token as binary operator.
798 return TT_BinaryOperator;
799 }
800
801 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
Manuel Klimekb3987012013-05-29 14:47:47 +0000802 TokenType determineIncrementUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000803 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperb8b42952013-05-31 16:14:28 +0000804 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper01786732013-02-04 07:21:18 +0000805 return TT_UnaryOperator;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000806 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
Daniel Jasper01786732013-02-04 07:21:18 +0000807 return TT_TrailingUnaryOperator;
808
809 return TT_UnaryOperator;
810 }
Daniel Jasper4e778092013-02-06 10:05:46 +0000811
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000812 // FIXME: This is copy&pasted from Sema. Put it in a common place and remove
813 // duplication.
814 /// \brief Determine whether the token kind starts a simple-type-specifier.
Manuel Klimekb3987012013-05-29 14:47:47 +0000815 bool isSimpleTypeSpecifier(const FormatToken &Tok) const {
816 switch (Tok.Tok.getKind()) {
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000817 case tok::kw_short:
818 case tok::kw_long:
819 case tok::kw___int64:
820 case tok::kw___int128:
821 case tok::kw_signed:
822 case tok::kw_unsigned:
823 case tok::kw_void:
824 case tok::kw_char:
825 case tok::kw_int:
826 case tok::kw_half:
827 case tok::kw_float:
828 case tok::kw_double:
829 case tok::kw_wchar_t:
830 case tok::kw_bool:
831 case tok::kw___underlying_type:
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000832 case tok::annot_typename:
833 case tok::kw_char16_t:
834 case tok::kw_char32_t:
835 case tok::kw_typeof:
836 case tok::kw_decltype:
Alexander Kornienko00895102013-06-05 14:09:10 +0000837 return true;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000838 default:
Alexander Kornienko00895102013-06-05 14:09:10 +0000839 return false;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000840 }
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000841 }
842
Daniel Jasper4e778092013-02-06 10:05:46 +0000843 SmallVector<Context, 8> Contexts;
844
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000845 const FormatStyle &Style;
Daniel Jasper4e778092013-02-06 10:05:46 +0000846 AnnotatedLine &Line;
Manuel Klimekb3987012013-05-29 14:47:47 +0000847 FormatToken *CurrentToken;
Daniel Jasper4e778092013-02-06 10:05:46 +0000848 bool KeywordVirtualFound;
Daniel Jasper1407bee2013-04-11 14:29:13 +0000849 bool NameFound;
Daniel Jasper2ca37412013-07-09 14:36:48 +0000850 bool AutoFound;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000851 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000852};
853
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000854static int PrecedenceUnaryOperator = prec::PointerToMember + 1;
855static int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
856
Daniel Jasper29f123b2013-02-08 15:28:42 +0000857/// \brief Parses binary expressions by inserting fake parenthesis based on
858/// operator precedence.
859class ExpressionParser {
860public:
Daniel Jasper9acb8b42013-06-06 09:11:58 +0000861 ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {
862 // Skip leading "}", e.g. in "} else if (...) {".
863 if (Current->is(tok::r_brace))
864 next();
865 }
Daniel Jasper29f123b2013-02-08 15:28:42 +0000866
867 /// \brief Parse expressions with the given operatore precedence.
Daniel Jasper237d4c12013-02-23 21:01:55 +0000868 void parse(int Precedence = 0) {
Daniel Jasperf78bf4a2013-09-30 08:29:03 +0000869 // Skip 'return' as it is not part of a binary expression.
870 while (Current && Current->is(tok::kw_return))
871 next();
872
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000873 if (Current == NULL || Precedence > PrecedenceArrowAndPeriod)
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000874 return;
875
Daniel Jasperc01897c2013-05-31 14:56:12 +0000876 // Conditional expressions need to be parsed separately for proper nesting.
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000877 if (Precedence == prec::Conditional) {
Daniel Jasperc01897c2013-05-31 14:56:12 +0000878 parseConditionalExpr();
879 return;
880 }
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000881
882 // Parse unary operators, which all have a higher precedence than binary
883 // operators.
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000884 if (Precedence == PrecedenceUnaryOperator) {
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000885 parseUnaryOperator();
Daniel Jasper29f123b2013-02-08 15:28:42 +0000886 return;
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000887 }
Daniel Jasper29f123b2013-02-08 15:28:42 +0000888
Manuel Klimekb3987012013-05-29 14:47:47 +0000889 FormatToken *Start = Current;
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000890 FormatToken *LatestOperator = NULL;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000891
Daniel Jasper237d4c12013-02-23 21:01:55 +0000892 while (Current) {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000893 // Consume operators with higher precedence.
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000894 parse(Precedence + 1);
Daniel Jasper29f123b2013-02-08 15:28:42 +0000895
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000896 int CurrentPrecedence = getCurrentPrecedence();
897
898 if (Current && Current->Type == TT_ObjCSelectorName &&
899 Precedence == CurrentPrecedence)
900 Start = Current;
Daniel Jasper237d4c12013-02-23 21:01:55 +0000901
Daniel Jasper29f123b2013-02-08 15:28:42 +0000902 // At the end of the line or when an operator with higher precedence is
903 // found, insert fake parenthesis and return.
Daniel Jasperac3223e2013-04-10 09:49:49 +0000904 if (Current == NULL || Current->closesScope() ||
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000905 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) {
906 if (LatestOperator) {
907 if (Precedence == PrecedenceArrowAndPeriod) {
908 LatestOperator->LastInChainOfCalls = true;
909 // Call expressions don't have a binary operator precedence.
910 addFakeParenthesis(Start, prec::Unknown);
911 } else {
912 addFakeParenthesis(Start, prec::Level(Precedence));
913 }
914 }
Daniel Jasper29f123b2013-02-08 15:28:42 +0000915 return;
916 }
917
918 // Consume scopes: (), [], <> and {}
Daniel Jasperac3223e2013-04-10 09:49:49 +0000919 if (Current->opensScope()) {
920 while (Current && !Current->closesScope()) {
Daniel Jasper29f123b2013-02-08 15:28:42 +0000921 next();
922 parse();
923 }
924 next();
925 } else {
926 // Operator found.
Daniel Jasper237d4c12013-02-23 21:01:55 +0000927 if (CurrentPrecedence == Precedence)
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000928 LatestOperator = Current;
Daniel Jasper29f123b2013-02-08 15:28:42 +0000929
930 next();
931 }
932 }
933 }
934
935private:
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000936 /// \brief Gets the precedence (+1) of the given token for binary operators
937 /// and other tokens that we treat like binary operators.
938 int getCurrentPrecedence() {
939 if (Current) {
940 if (Current->Type == TT_ConditionalExpr)
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000941 return prec::Conditional;
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000942 else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon)
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000943 return 0;
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000944 else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000945 return Current->getPrecedence();
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000946 else if (Current->Type == TT_ObjCSelectorName)
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000947 return prec::Assignment;
948 else if (Current->isOneOf(tok::period, tok::arrow))
949 return PrecedenceArrowAndPeriod;
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000950 }
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000951 return -1;
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000952 }
953
Daniel Jasperc01897c2013-05-31 14:56:12 +0000954 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
955 Start->FakeLParens.push_back(Precedence);
Daniel Jasperdb4813a2013-09-06 08:08:14 +0000956 if (Precedence > prec::Unknown)
957 Start->StartsBinaryExpression = true;
958 if (Current) {
Daniel Jasperc01897c2013-05-31 14:56:12 +0000959 ++Current->Previous->FakeRParens;
Daniel Jasperdb4813a2013-09-06 08:08:14 +0000960 if (Precedence > prec::Unknown)
961 Current->Previous->EndsBinaryExpression = true;
962 }
Daniel Jasperc01897c2013-05-31 14:56:12 +0000963 }
964
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000965 /// \brief Parse unary operator expressions and surround them with fake
966 /// parentheses if appropriate.
967 void parseUnaryOperator() {
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000968 if (Current == NULL || Current->Type != TT_UnaryOperator) {
969 parse(PrecedenceArrowAndPeriod);
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000970 return;
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000971 }
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000972
973 FormatToken *Start = Current;
974 next();
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000975 parseUnaryOperator();
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000976
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000977 // The actual precedence doesn't matter.
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000978 addFakeParenthesis(Start, prec::Unknown);
Daniel Jasper3618e6f2013-08-23 15:14:03 +0000979 }
980
Daniel Jasperc01897c2013-05-31 14:56:12 +0000981 void parseConditionalExpr() {
982 FormatToken *Start = Current;
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000983 parse(prec::LogicalOr);
Daniel Jasperc01897c2013-05-31 14:56:12 +0000984 if (!Current || !Current->is(tok::question))
985 return;
986 next();
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000987 parse(prec::LogicalOr);
Daniel Jasperc01897c2013-05-31 14:56:12 +0000988 if (!Current || Current->Type != TT_ConditionalExpr)
989 return;
990 next();
991 parseConditionalExpr();
992 addFakeParenthesis(Start, prec::Conditional);
993 }
994
Daniel Jasper29f123b2013-02-08 15:28:42 +0000995 void next() {
Alexander Kornienkod71b15b2013-06-17 13:19:53 +0000996 if (Current)
997 Current = Current->Next;
998 while (Current && Current->isTrailingComment())
Manuel Klimekb3987012013-05-29 14:47:47 +0000999 Current = Current->Next;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001000 }
1001
Manuel Klimekb3987012013-05-29 14:47:47 +00001002 FormatToken *Current;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001003};
1004
Craig Topper14e66492013-07-01 04:03:19 +00001005} // end anonymous namespace
1006
Daniel Jasperb77d7412013-09-06 07:54:20 +00001007void
1008TokenAnnotator::setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines) {
1009 if (Lines.empty())
1010 return;
1011
1012 const AnnotatedLine *NextNonCommentLine = NULL;
1013 for (unsigned i = Lines.size() - 1; i > 0; --i) {
1014 if (NextNonCommentLine && Lines[i]->First->is(tok::comment) &&
1015 !Lines[i]->First->Next)
1016 Lines[i]->Level = NextNonCommentLine->Level;
1017 else
1018 NextNonCommentLine =
1019 Lines[i]->First->isNot(tok::r_brace) ? Lines[i] : NULL;
1020 }
1021}
1022
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001023void TokenAnnotator::annotate(AnnotatedLine &Line) {
Daniel Jasperb77d7412013-09-06 07:54:20 +00001024 setCommentLineLevels(Line.Children);
1025 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1026 E = Line.Children.end();
Daniel Jasper567dcf92013-09-05 09:29:45 +00001027 I != E; ++I) {
1028 annotate(**I);
1029 }
Daniel Jasperd4a03db2013-08-22 15:00:41 +00001030 AnnotatingParser Parser(Style, Line, Ident_in);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001031 Line.Type = Parser.parseLine();
1032 if (Line.Type == LT_Invalid)
1033 return;
1034
Daniel Jasper29f123b2013-02-08 15:28:42 +00001035 ExpressionParser ExprParser(Line);
1036 ExprParser.parse();
1037
Manuel Klimekb3987012013-05-29 14:47:47 +00001038 if (Line.First->Type == TT_ObjCMethodSpecifier)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001039 Line.Type = LT_ObjCMethodDecl;
Manuel Klimekb3987012013-05-29 14:47:47 +00001040 else if (Line.First->Type == TT_ObjCDecl)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001041 Line.Type = LT_ObjCDecl;
Manuel Klimekb3987012013-05-29 14:47:47 +00001042 else if (Line.First->Type == TT_ObjCProperty)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001043 Line.Type = LT_ObjCProperty;
1044
Manuel Klimekb3987012013-05-29 14:47:47 +00001045 Line.First->SpacesRequiredBefore = 1;
1046 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001047}
1048
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001049void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001050 Line.First->TotalLength =
1051 Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth;
Manuel Klimekb3987012013-05-29 14:47:47 +00001052 if (!Line.First->Next)
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001053 return;
Manuel Klimekb3987012013-05-29 14:47:47 +00001054 FormatToken *Current = Line.First->Next;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001055 while (Current != NULL) {
Daniel Jasper729a7432013-02-11 12:36:37 +00001056 if (Current->Type == TT_LineComment)
1057 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
Alexander Kornienkob18c2582013-10-11 21:43:05 +00001058 else if (Current->SpacesRequiredBefore == 0 &&
1059 spaceRequiredBefore(Line, *Current))
1060 Current->SpacesRequiredBefore = 1;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001061
Daniel Jasperebaa1712013-09-17 09:52:48 +00001062 Current->MustBreakBefore =
1063 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
1064
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001065 Current->CanBreakBefore =
1066 Current->MustBreakBefore || canBreakBefore(Line, *Current);
Daniel Jasper567dcf92013-09-05 09:29:45 +00001067 if (Current->MustBreakBefore || !Current->Children.empty() ||
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001068 Current->IsMultiline)
Manuel Klimekb3987012013-05-29 14:47:47 +00001069 Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001070 else
Daniel Jasper2a409b62013-07-08 14:34:09 +00001071 Current->TotalLength = Current->Previous->TotalLength +
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001072 Current->ColumnWidth +
Daniel Jasper2a409b62013-07-08 14:34:09 +00001073 Current->SpacesRequiredBefore;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001074 // FIXME: Only calculate this if CanBreakBefore is true once static
1075 // initializers etc. are sorted out.
1076 // FIXME: Move magic numbers to a better place.
1077 Current->SplitPenalty =
1078 20 * Current->BindingStrength + splitPenalty(Line, *Current);
1079
Manuel Klimekb3987012013-05-29 14:47:47 +00001080 Current = Current->Next;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001081 }
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001082
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001083 calculateUnbreakableTailLengths(Line);
Daniel Jasperd4a03db2013-08-22 15:00:41 +00001084 for (Current = Line.First; Current != NULL; Current = Current->Next) {
1085 if (Current->Role)
1086 Current->Role->precomputeFormattingInfos(Current);
1087 }
1088
Daniel Jasper567dcf92013-09-05 09:29:45 +00001089 DEBUG({ printDebugInfo(Line); });
1090
Daniel Jasperb77d7412013-09-06 07:54:20 +00001091 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1092 E = Line.Children.end();
Daniel Jasper567dcf92013-09-05 09:29:45 +00001093 I != E; ++I) {
1094 calculateFormattingInformation(**I);
1095 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001096}
1097
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001098void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1099 unsigned UnbreakableTailLength = 0;
Manuel Klimekb3987012013-05-29 14:47:47 +00001100 FormatToken *Current = Line.Last;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001101 while (Current != NULL) {
1102 Current->UnbreakableTailLength = UnbreakableTailLength;
1103 if (Current->CanBreakBefore ||
1104 Current->isOneOf(tok::comment, tok::string_literal)) {
1105 UnbreakableTailLength = 0;
1106 } else {
1107 UnbreakableTailLength +=
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001108 Current->ColumnWidth + Current->SpacesRequiredBefore;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001109 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001110 Current = Current->Previous;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001111 }
1112}
1113
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001114unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001115 const FormatToken &Tok) {
1116 const FormatToken &Left = *Tok.Previous;
1117 const FormatToken &Right = Tok;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001118
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001119 if (Left.is(tok::semi))
1120 return 0;
1121 if (Left.is(tok::comma))
1122 return 1;
Daniel Jasper011c35d2013-07-12 11:19:37 +00001123 if (Right.is(tok::l_square))
1124 return 150;
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001125
Daniel Jasper6561f6a2013-07-09 07:43:55 +00001126 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001127 if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001128 return 3;
Daniel Jasperc18cff32013-07-11 12:34:23 +00001129 if (Left.Type == TT_StartOfName)
1130 return 20;
Daniel Jasper92495a82013-08-19 10:16:18 +00001131 if (Line.MightBeFunctionDecl && Right.BindingStrength == 1)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001132 // FIXME: Clean up hack of using BindingStrength to find top-level names.
1133 return Style.PenaltyReturnTypeOnItsOwnLine;
Daniel Jasper92495a82013-08-19 10:16:18 +00001134 return 200;
Daniel Jasper3c08a812013-02-24 18:54:32 +00001135 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001136 if (Left.is(tok::equal) && Right.is(tok::l_brace))
1137 return 150;
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001138 if (Left.Type == TT_CastRParen)
1139 return 100;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001140 if (Left.is(tok::coloncolon))
1141 return 500;
Daniel Jasper6b119d62013-04-05 17:22:09 +00001142 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1143 return 5000;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001144
Daniel Jasper6cabab42013-02-14 08:42:54 +00001145 if (Left.Type == TT_RangeBasedForLoopColon ||
1146 Left.Type == TT_InheritanceColon)
Daniel Jasper84a1a632013-02-26 13:18:08 +00001147 return 2;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001148
Daniel Jasperd3fef0f2013-08-27 14:24:43 +00001149 if (Right.isMemberAccess()) {
Daniel Jasper2f0a0202013-09-06 08:54:24 +00001150 if (Left.isOneOf(tok::r_paren, tok::r_square) && Left.MatchingParen &&
1151 Left.MatchingParen->ParameterCount > 0)
Daniel Jasper518ee342013-02-26 13:59:14 +00001152 return 20; // Should be smaller than breaking at a nested comma.
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001153 return 150;
1154 }
1155
Daniel Jasper20a0f8c2013-07-11 21:02:56 +00001156 // Breaking before a trailing 'const' or not-function-like annotation is bad.
Daniel Jasperaa9e7b12013-08-01 13:46:58 +00001157 if (Left.is(tok::r_paren) && Line.Type != LT_ObjCProperty &&
Daniel Jasper20a0f8c2013-07-11 21:02:56 +00001158 (Right.is(tok::kw_const) || (Right.is(tok::identifier) && Right.Next &&
1159 Right.Next->isNot(tok::l_paren))))
Daniel Jasper53eb05a2013-10-18 16:34:40 +00001160 return 100;
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001161
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001162 // In for-loops, prefer breaking at ',' and ';'.
Manuel Klimekb3987012013-05-29 14:47:47 +00001163 if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
Daniel Jasper7d812812013-02-21 15:00:29 +00001164 return 4;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001165
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001166 // In Objective-C method expressions, prefer breaking before "param:" over
1167 // breaking after it.
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001168 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001169 return 0;
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001170 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001171 return 20;
1172
Daniel Jasper1407bee2013-04-11 14:29:13 +00001173 if (Left.is(tok::l_paren) && Line.MightBeFunctionDecl)
1174 return 100;
Daniel Jasperac3223e2013-04-10 09:49:49 +00001175 if (Left.opensScope())
Daniel Jaspere60084d2013-08-13 06:50:04 +00001176 return Left.ParameterCount > 1 ? prec::Comma : 19;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001177
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001178 if (Right.is(tok::lessless)) {
1179 if (Left.is(tok::string_literal)) {
Alexander Kornienko00895102013-06-05 14:09:10 +00001180 StringRef Content = Left.TokenText;
Daniel Jasper20376982013-09-29 12:02:57 +00001181 if (Content.startswith("\""))
1182 Content = Content.drop_front(1);
1183 if (Content.endswith("\""))
1184 Content = Content.drop_back(1);
1185 Content = Content.trim();
Daniel Jasperbfa1edd2013-03-14 14:00:17 +00001186 if (Content.size() > 1 &&
1187 (Content.back() == ':' || Content.back() == '='))
Daniel Jasper9637dda2013-07-15 14:33:14 +00001188 return 25;
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001189 }
Daniel Jasper0c368782013-07-15 15:04:42 +00001190 return 1; // Breaking at a << is really cheap.
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001191 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001192 if (Left.Type == TT_ConditionalExpr)
Daniel Jasper518ee342013-02-26 13:59:14 +00001193 return prec::Conditional;
Manuel Klimekb3987012013-05-29 14:47:47 +00001194 prec::Level Level = Left.getPrecedence();
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001195
1196 if (Level != prec::Unknown)
1197 return Level;
Daniel Jasper24849712013-03-01 16:48:32 +00001198
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001199 return 3;
1200}
1201
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001202bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001203 const FormatToken &Left,
1204 const FormatToken &Right) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001205 if (Right.is(tok::hashhash))
1206 return Left.is(tok::hash);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001207 if (Left.isOneOf(tok::hashhash, tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001208 return Right.is(tok::hash);
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001209 if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1210 return Style.SpaceInEmptyParentheses;
1211 if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
Daniel Jasper34f3d052013-08-21 08:39:01 +00001212 return (Right.Type == TT_CastRParen ||
1213 (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen))
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001214 ? Style.SpacesInCStyleCastParentheses
1215 : Style.SpacesInParentheses;
1216 if (Right.isOneOf(tok::semi, tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001217 return false;
1218 if (Right.is(tok::less) &&
1219 (Left.is(tok::kw_template) ||
1220 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1221 return true;
1222 if (Left.is(tok::arrow) || Right.is(tok::arrow))
1223 return false;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001224 if (Left.isOneOf(tok::exclaim, tok::tilde))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001225 return false;
1226 if (Left.is(tok::at) &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001227 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1228 tok::numeric_constant, tok::l_paren, tok::l_brace,
1229 tok::kw_true, tok::kw_false))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001230 return false;
1231 if (Left.is(tok::coloncolon))
1232 return false;
1233 if (Right.is(tok::coloncolon))
Daniel Jasper78a4e612013-10-12 05:16:06 +00001234 return (Left.is(tok::less) && Style.Standard == FormatStyle::LS_Cpp03) ||
1235 !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren,
1236 tok::r_paren, tok::less);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001237 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001238 return false;
Daniel Jasperc47d7f12013-07-01 09:47:25 +00001239 if (Right.is(tok::ellipsis))
Daniel Jasperb3c887d2013-10-20 16:56:16 +00001240 return Left.Tok.isLiteral();
Manuel Klimek31e44f72013-09-04 08:20:47 +00001241 if (Left.is(tok::l_square) && Right.is(tok::amp))
1242 return false;
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001243 if (Right.Type == TT_PointerOrReference)
Manuel Klimekb3987012013-05-29 14:47:47 +00001244 return Left.Tok.isLiteral() ||
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001245 ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
1246 !Style.PointerBindsToType);
Daniel Jasper3ff4a2f2013-05-28 15:27:10 +00001247 if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
Daniel Jasper395228f2013-05-08 14:58:20 +00001248 (Left.Type != TT_PointerOrReference || Style.PointerBindsToType))
1249 return true;
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001250 if (Left.Type == TT_PointerOrReference)
Daniel Jasper3a1847e2013-07-01 09:34:09 +00001251 return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
Daniel Jasper9322aae2013-03-20 09:53:18 +00001252 ((Right.Type != TT_PointerOrReference) &&
Daniel Jasper81d2d382013-04-01 17:13:26 +00001253 Right.isNot(tok::l_paren) && Style.PointerBindsToType &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001254 Left.Previous &&
1255 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001256 if (Right.is(tok::star) && Left.is(tok::l_paren))
1257 return false;
Nico Weber051860e2013-02-10 02:08:05 +00001258 if (Left.is(tok::l_square))
1259 return Left.Type == TT_ObjCArrayLiteral && Right.isNot(tok::r_square);
1260 if (Right.is(tok::r_square))
1261 return Right.Type == TT_ObjCArrayLiteral;
Daniel Jasperec172262013-08-30 10:36:58 +00001262 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr &&
Daniel Jasper567dcf92013-09-05 09:29:45 +00001263 Right.Type != TT_LambdaLSquare && Left.isNot(tok::numeric_constant))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001264 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001265 if (Left.is(tok::colon))
1266 return Left.Type != TT_ObjCMethodExpr;
1267 if (Right.is(tok::colon))
Daniel Jasperab3ce592013-08-01 22:05:00 +00001268 return Right.Type != TT_ObjCMethodExpr && !Left.is(tok::question);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001269 if (Right.is(tok::l_paren)) {
Daniel Jaspere0fa4c52013-07-17 20:25:02 +00001270 if (Left.is(tok::r_paren) && Left.MatchingParen &&
1271 Left.MatchingParen->Previous &&
1272 Left.MatchingParen->Previous->is(tok::kw___attribute))
1273 return true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001274 return Line.Type == LT_ObjCDecl ||
Daniel Jasper34f3d052013-08-21 08:39:01 +00001275 Left.isOneOf(tok::kw_return, tok::kw_new, tok::kw_delete,
1276 tok::semi) ||
1277 (Style.SpaceAfterControlStatementKeyword &&
1278 Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
1279 tok::kw_catch));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001280 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001281 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001282 return false;
1283 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
Daniel Jasper567dcf92013-09-05 09:29:45 +00001284 return !Left.Children.empty(); // No spaces in "{}".
Daniel Jasper2424eef2013-05-23 10:15:45 +00001285 if (Left.is(tok::l_brace) || Right.is(tok::r_brace))
Daniel Jasperb5dc3f42013-07-16 18:22:10 +00001286 return !Style.Cpp11BracedListStyle;
Daniel Jasper1bee0732013-05-23 18:05:18 +00001287 if (Right.Type == TT_UnaryOperator)
1288 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1289 (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
Daniel Jasperce933562013-05-23 21:35:49 +00001290 if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) &&
Manuel Klimek31e44f72013-09-04 08:20:47 +00001291 Right.is(tok::l_brace) && Right.getNextNonComment() &&
1292 Right.BlockKind != BK_Block)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001293 return false;
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001294 if (Left.is(tok::period) || Right.is(tok::period))
1295 return false;
Nico Weber861576b2013-06-26 00:15:19 +00001296 if (Left.Type == TT_BlockComment && Left.TokenText.endswith("=*/"))
1297 return false;
Alexander Kornienkodaa07e92013-09-10 13:41:43 +00001298 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
1299 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001300 return true;
1301}
1302
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001303bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001304 const FormatToken &Tok) {
1305 if (Tok.Tok.getIdentifierInfo() && Tok.Previous->Tok.getIdentifierInfo())
Daniel Jasper2b4c9242013-02-11 08:01:18 +00001306 return true; // Never ever merge two identifiers.
Daniel Jasper1d82b1a2013-10-11 19:45:02 +00001307 if (Tok.Previous->Type == TT_ImplicitStringLiteral)
1308 return Tok.WhitespaceRange.getBegin() != Tok.WhitespaceRange.getEnd();
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001309 if (Line.Type == LT_ObjCMethodDecl) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001310 if (Tok.Previous->Type == TT_ObjCMethodSpecifier)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001311 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001312 if (Tok.Previous->is(tok::r_paren) && Tok.is(tok::identifier))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001313 // Don't space between ')' and <id>
1314 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001315 }
1316 if (Line.Type == LT_ObjCProperty &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001317 (Tok.is(tok::equal) || Tok.Previous->is(tok::equal)))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001318 return false;
1319
Daniel Jasper2ca37412013-07-09 14:36:48 +00001320 if (Tok.Type == TT_TrailingReturnArrow ||
1321 Tok.Previous->Type == TT_TrailingReturnArrow)
1322 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001323 if (Tok.Previous->is(tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001324 return true;
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001325 if (Tok.is(tok::comma))
1326 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001327 if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
1328 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001329 if (Tok.Previous->Tok.is(tok::kw_operator))
Daniel Jasper2b4c9242013-02-11 08:01:18 +00001330 return false;
1331 if (Tok.Type == TT_OverloadedOperatorLParen)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001332 return false;
1333 if (Tok.is(tok::colon))
Manuel Klimekb3987012013-05-29 14:47:47 +00001334 return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Daniel Jasperab3ce592013-08-01 22:05:00 +00001335 Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr &&
1336 !Tok.Previous->is(tok::question);
Manuel Klimekb3987012013-05-29 14:47:47 +00001337 if (Tok.Previous->Type == TT_UnaryOperator ||
1338 Tok.Previous->Type == TT_CastRParen)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001339 return false;
Manuel Klimekb3987012013-05-29 14:47:47 +00001340 if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) {
Daniel Jasper29f123b2013-02-08 15:28:42 +00001341 return Tok.Type == TT_TemplateCloser &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001342 Tok.Previous->Type == TT_TemplateCloser &&
Daniel Jasper29f123b2013-02-08 15:28:42 +00001343 Style.Standard != FormatStyle::LS_Cpp11;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001344 }
Alexander Kornienko54a38bd2013-03-20 16:41:56 +00001345 if (Tok.isOneOf(tok::arrowstar, tok::periodstar) ||
Manuel Klimekb3987012013-05-29 14:47:47 +00001346 Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar))
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001347 return false;
Daniel Jasper9b4de852013-09-25 15:15:02 +00001348 if (!Style.SpaceBeforeAssignmentOperators &&
1349 Tok.getPrecedence() == prec::Assignment)
1350 return false;
Daniel Jasper1dc6f742013-08-07 16:29:23 +00001351 if ((Tok.Type == TT_BinaryOperator && !Tok.Previous->is(tok::l_paren)) ||
1352 Tok.Previous->Type == TT_BinaryOperator)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001353 return true;
Manuel Klimekb3987012013-05-29 14:47:47 +00001354 if (Tok.Previous->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001355 return false;
Daniel Jaspera4dd9822013-08-28 08:24:04 +00001356 if (Tok.is(tok::less) && Tok.Previous->isNot(tok::l_paren) &&
1357 Line.First->is(tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001358 return true;
1359 if (Tok.Type == TT_TrailingUnaryOperator)
1360 return false;
Manuel Klimekb3987012013-05-29 14:47:47 +00001361 return spaceRequiredBetween(Line, *Tok.Previous, Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001362}
1363
Daniel Jasperebaa1712013-09-17 09:52:48 +00001364bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
1365 const FormatToken &Right) {
1366 if (Right.is(tok::comment)) {
1367 return Right.NewlinesBefore > 0;
1368 } else if (Right.Previous->isTrailingComment() ||
1369 (Right.is(tok::string_literal) &&
1370 Right.Previous->is(tok::string_literal))) {
1371 return true;
1372 } else if (Right.Previous->IsUnterminatedLiteral) {
1373 return true;
1374 } else if (Right.is(tok::lessless) && Right.Next &&
1375 Right.Previous->is(tok::string_literal) &&
1376 Right.Next->is(tok::string_literal)) {
1377 return true;
1378 } else if (Right.Previous->ClosesTemplateDeclaration &&
1379 Right.Previous->MatchingParen &&
1380 Right.Previous->MatchingParen->BindingStrength == 1 &&
1381 Style.AlwaysBreakTemplateDeclarations) {
1382 // FIXME: Fix horrible hack of using BindingStrength to find top-level <>.
1383 return true;
1384 } else if (Right.Type == TT_CtorInitializerComma &&
Daniel Jasper19ccb122013-10-08 05:11:18 +00001385 Style.BreakConstructorInitializersBeforeComma &&
1386 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
Daniel Jasperebaa1712013-09-17 09:52:48 +00001387 return true;
1388 } else if (Right.Previous->BlockKind == BK_Block &&
Alexander Kornienko01fe9f92013-10-10 13:36:20 +00001389 Right.Previous->isNot(tok::r_brace) && Right.isNot(tok::r_brace)) {
Daniel Jasperebaa1712013-09-17 09:52:48 +00001390 return true;
1391 } else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) {
1392 return Style.BreakBeforeBraces == FormatStyle::BS_Allman;
1393 }
1394 return false;
1395}
1396
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001397bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001398 const FormatToken &Right) {
1399 const FormatToken &Left = *Right.Previous;
Daniel Jasper6561f6a2013-07-09 07:43:55 +00001400 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001401 return true;
Nico Weberf2ff8122013-05-26 05:39:26 +00001402 if (Right.is(tok::colon) &&
1403 (Right.Type == TT_ObjCDictLiteral || Right.Type == TT_ObjCMethodExpr))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001404 return false;
Nico Weberf2ff8122013-05-26 05:39:26 +00001405 if (Left.is(tok::colon) &&
1406 (Left.Type == TT_ObjCDictLiteral || Left.Type == TT_ObjCMethodExpr))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001407 return true;
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001408 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001409 return true;
Daniel Jasperaa9e7b12013-08-01 13:46:58 +00001410 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
1411 return true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001412 if (Left.ClosesTemplateDeclaration)
1413 return true;
Daniel Jasperab3ce592013-08-01 22:05:00 +00001414 if ((Right.Type == TT_ConditionalExpr &&
1415 !(Right.is(tok::colon) && Left.is(tok::question))) ||
1416 Right.is(tok::question))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001417 return true;
Daniel Jasper6cabab42013-02-14 08:42:54 +00001418 if (Right.Type == TT_RangeBasedForLoopColon ||
Daniel Jasperc476ea92013-08-28 07:27:35 +00001419 Right.Type == TT_OverloadedOperatorLParen ||
1420 Right.Type == TT_OverloadedOperator)
Daniel Jasper6cabab42013-02-14 08:42:54 +00001421 return false;
Daniel Jasperc194c952013-05-06 06:45:09 +00001422 if (Left.Type == TT_RangeBasedForLoopColon)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001423 return true;
Daniel Jasper7d812812013-02-21 15:00:29 +00001424 if (Right.Type == TT_RangeBasedForLoopColon)
1425 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001426 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
1427 Left.Type == TT_UnaryOperator || Left.Type == TT_ConditionalExpr ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001428 Left.isOneOf(tok::question, tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001429 return false;
1430 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
1431 return false;
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001432 if (Left.Previous) {
1433 if (Left.is(tok::l_paren) && Right.is(tok::l_paren) &&
1434 Left.Previous->is(tok::kw___attribute))
1435 return false;
Daniel Jasper2ca37412013-07-09 14:36:48 +00001436 if (Left.is(tok::l_paren) && (Left.Previous->Type == TT_BinaryOperator ||
Daniel Jasper5e2169f2013-07-18 14:46:07 +00001437 Left.Previous->Type == TT_CastRParen))
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001438 return false;
1439 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001440
Daniel Jasper65d2c382013-06-06 16:08:57 +00001441 if (Right.isTrailingComment())
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001442 // We rely on MustBreakBefore being set correctly here as we should not
1443 // change the "binding" behavior of a comment.
1444 return false;
1445
Daniel Jasper567dcf92013-09-05 09:29:45 +00001446 if (Right.is(tok::r_paren) || Right.Type == TT_TemplateCloser)
1447 return false;
1448
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001449 // We only break before r_brace if there was a corresponding break before
1450 // the l_brace, which is tracked by BreakBeforeClosingBrace.
Daniel Jasper567dcf92013-09-05 09:29:45 +00001451 if (Right.is(tok::r_brace))
1452 return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001453
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001454 // Allow breaking after a trailing 'const', e.g. after a method declaration,
1455 // unless it is follow by ';', '{' or '='.
Manuel Klimekb3987012013-05-29 14:47:47 +00001456 if (Left.is(tok::kw_const) && Left.Previous != NULL &&
1457 Left.Previous->is(tok::r_paren))
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001458 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001459
Daniel Jasper8ef19a22013-03-14 09:50:46 +00001460 if (Right.is(tok::kw___attribute))
1461 return true;
1462
Daniel Jasper3a204412013-02-23 07:46:38 +00001463 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
1464 return true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001465
1466 if (Left.Type == TT_CtorInitializerComma &&
1467 Style.BreakConstructorInitializersBeforeComma)
1468 return false;
Daniel Jasper19ccb122013-10-08 05:11:18 +00001469 if (Right.Type == TT_CtorInitializerComma &&
1470 Style.BreakConstructorInitializersBeforeComma)
1471 return true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001472 if (Right.isBinaryOperator() && Style.BreakBeforeBinaryOperators)
1473 return true;
Daniel Jasper26356cc2013-09-17 08:15:46 +00001474 if (Left.is(tok::greater) && Right.is(tok::greater) &&
1475 Left.Type != TT_TemplateCloser)
1476 return false;
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001477 return (Left.isBinaryOperator() && Left.isNot(tok::lessless) &&
1478 !Style.BreakBeforeBinaryOperators) ||
Daniel Jasper6b119d62013-04-05 17:22:09 +00001479 Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
1480 tok::kw_class, tok::kw_struct) ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001481 Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon) ||
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001482 (Left.is(tok::r_paren) &&
Daniel Jaspere033e872013-05-21 09:16:31 +00001483 Right.isOneOf(tok::identifier, tok::kw_const, tok::kw___attribute)) ||
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001484 (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
Daniel Jasper011c35d2013-07-12 11:19:37 +00001485 Right.is(tok::l_square);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001486}
1487
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001488void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
1489 llvm::errs() << "AnnotatedTokens:\n";
Manuel Klimekb3987012013-05-29 14:47:47 +00001490 const FormatToken *Tok = Line.First;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001491 while (Tok) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001492 llvm::errs() << " M=" << Tok->MustBreakBefore
Daniel Jasperc7bd68f2013-07-10 14:02:49 +00001493 << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
1494 << " S=" << Tok->SpacesRequiredBefore
1495 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
Manuel Klimekae76f7f2013-10-11 21:25:45 +00001496 << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
1497 << " FakeLParens=";
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001498 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
1499 llvm::errs() << Tok->FakeLParens[i] << "/";
1500 llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
Manuel Klimekae76f7f2013-10-11 21:25:45 +00001501 if (Tok->Next == NULL)
1502 assert(Tok == Line.Last);
Manuel Klimekb3987012013-05-29 14:47:47 +00001503 Tok = Tok->Next;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001504 }
1505 llvm::errs() << "----\n";
1506}
1507
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001508} // namespace format
1509} // namespace clang