blob: 1afdb19b29d1e78d456cec3e84450c2538cfeae7 [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
Stephen Hines6bcf27b2014-05-29 04:14:42 -070020#define DEBUG_TYPE "format-token-annotator"
21
Daniel Jasper32d28ee2013-01-29 21:01:14 +000022namespace clang {
23namespace format {
24
Craig Topper14e66492013-07-01 04:03:19 +000025namespace {
26
Daniel Jasper32d28ee2013-01-29 21:01:14 +000027/// \brief A parser that gathers additional information about tokens.
28///
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +000029/// The \c TokenAnnotator tries to match parenthesis and square brakets and
Daniel Jasper32d28ee2013-01-29 21:01:14 +000030/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
31/// into template parameter lists.
32class AnnotatingParser {
33public:
Daniel Jasperd4a03db2013-08-22 15:00:41 +000034 AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
Stephen Hines176edba2014-12-01 14:53:08 -080035 const AdditionalKeywords &Keywords)
Daniel Jasperd4a03db2013-08-22 15:00:41 +000036 : Style(Style), Line(Line), CurrentToken(Line.First),
Stephen Hines176edba2014-12-01 14:53:08 -080037 KeywordVirtualFound(false), AutoFound(false), Keywords(Keywords) {
Nico Weber27268772013-06-26 00:30:14 +000038 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
Stephen Hines651f13c2014-04-23 16:59:28 -070039 resetTokenMetadata(CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000040 }
41
Nico Weber95e8e462013-02-12 16:17:07 +000042private:
Daniel Jasper32d28ee2013-01-29 21:01:14 +000043 bool parseAngle() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070044 if (!CurrentToken)
Daniel Jasper32d28ee2013-01-29 21:01:14 +000045 return false;
Daniel Jasper923ebef2013-03-14 13:45:21 +000046 ScopedContextCreator ContextCreator(*this, tok::less, 10);
Manuel Klimekb3987012013-05-29 14:47:47 +000047 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper4e778092013-02-06 10:05:46 +000048 Contexts.back().IsExpression = false;
Stephen Hines651f13c2014-04-23 16:59:28 -070049 // If there's a template keyword before the opening angle bracket, this is a
50 // template parameter, not an argument.
51 Contexts.back().InTemplateArgument =
Stephen Hines6bcf27b2014-05-29 04:14:42 -070052 Left->Previous && Left->Previous->Tok.isNot(tok::kw_template);
Stephen Hines651f13c2014-04-23 16:59:28 -070053
Stephen Hines176edba2014-12-01 14:53:08 -080054 if (Style.Language == FormatStyle::LK_Java &&
55 CurrentToken->is(tok::question))
56 next();
57
Stephen Hines6bcf27b2014-05-29 04:14:42 -070058 while (CurrentToken) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000059 if (CurrentToken->is(tok::greater)) {
60 Left->MatchingParen = CurrentToken;
61 CurrentToken->MatchingParen = Left;
62 CurrentToken->Type = TT_TemplateCloser;
63 next();
64 return true;
65 }
Stephen Hines176edba2014-12-01 14:53:08 -080066 if (CurrentToken->is(tok::question) &&
67 Style.Language == FormatStyle::LK_Java) {
68 next();
69 continue;
70 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +000071 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
Stephen Hines176edba2014-12-01 14:53:08 -080072 tok::colon, tok::question))
Daniel Jasper5d823e32013-05-15 13:46:48 +000073 return false;
Daniel Jasper0348be02013-06-01 18:56:00 +000074 // If a && or || is found and interpreted as a binary operator, this set
Daniel Jasper15f33f02013-06-03 16:16:41 +000075 // of angles is likely part of something like "a < b && c > d". If the
Daniel Jasper0348be02013-06-01 18:56:00 +000076 // angles are inside an expression, the ||/&& might also be a binary
77 // operator that was misinterpreted because we are parsing template
78 // parameters.
79 // FIXME: This is getting out of hand, write a decent parser.
Manuel Klimekb3987012013-05-29 14:47:47 +000080 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
Stephen Hines176edba2014-12-01 14:53:08 -080081 CurrentToken->Previous->Type == TT_BinaryOperator &&
82 Contexts[Contexts.size() - 2].IsExpression &&
Manuel Klimekb3987012013-05-29 14:47:47 +000083 Line.First->isNot(tok::kw_template))
Daniel Jasper32d28ee2013-01-29 21:01:14 +000084 return false;
Daniel Jasper9fc56f22013-02-14 15:01:34 +000085 updateParameterCount(Left, CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000086 if (!consumeToken())
87 return false;
88 }
89 return false;
90 }
91
92 bool parseParens(bool LookForDecls = false) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070093 if (!CurrentToken)
Daniel Jasper32d28ee2013-01-29 21:01:14 +000094 return false;
Daniel Jasper923ebef2013-03-14 13:45:21 +000095 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
Daniel Jasper4e778092013-02-06 10:05:46 +000096
97 // FIXME: This is a bit of a hack. Do better.
98 Contexts.back().ColonIsForRangeExpr =
99 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
100
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000101 bool StartsObjCMethodExpr = false;
Manuel Klimekb3987012013-05-29 14:47:47 +0000102 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000103 if (CurrentToken->is(tok::caret)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700104 // (^ can start a block type.
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000105 Left->Type = TT_ObjCBlockLParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000106 } else if (FormatToken *MaybeSel = Left->Previous) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000107 // @selector( starts a selector.
Manuel Klimekb3987012013-05-29 14:47:47 +0000108 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
109 MaybeSel->Previous->is(tok::at)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000110 StartsObjCMethodExpr = true;
111 }
112 }
113
Stephen Hines651f13c2014-04-23 16:59:28 -0700114 if (Left->Previous &&
115 (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_if,
116 tok::kw_while, tok::l_paren, tok::comma) ||
117 Left->Previous->Type == TT_BinaryOperator)) {
Daniel Jasper363193b2013-10-20 18:15:30 +0000118 // static_assert, if and while usually contain expressions.
Daniel Jasperb7000ca2013-08-01 17:58:23 +0000119 Contexts.back().IsExpression = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700120 } else if (Line.InPPDirective &&
121 (!Left->Previous ||
122 (Left->Previous->isNot(tok::identifier) &&
123 Left->Previous->Type != TT_OverloadedOperator))) {
124 Contexts.back().IsExpression = true;
Daniel Jasper363193b2013-10-20 18:15:30 +0000125 } else if (Left->Previous && Left->Previous->is(tok::r_square) &&
126 Left->Previous->MatchingParen &&
127 Left->Previous->MatchingParen->Type == TT_LambdaLSquare) {
128 // This is a parameter list of a lambda expression.
129 Contexts.back().IsExpression = false;
Stephen Hines651f13c2014-04-23 16:59:28 -0700130 } else if (Contexts[Contexts.size() - 2].CaretFound) {
131 // This is the parameter list of an ObjC block.
132 Contexts.back().IsExpression = false;
133 } else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
134 Left->Type = TT_AttributeParen;
135 } else if (Left->Previous && Left->Previous->IsForEachMacro) {
136 // The first argument to a foreach macro is a declaration.
137 Contexts.back().IsForEachMacro = true;
138 Contexts.back().IsExpression = false;
Stephen Hines176edba2014-12-01 14:53:08 -0800139 } else if (Left->Previous && Left->Previous->MatchingParen &&
140 Left->Previous->MatchingParen->Type == TT_ObjCBlockLParen) {
141 Contexts.back().IsExpression = false;
Daniel Jasper363193b2013-10-20 18:15:30 +0000142 }
Daniel Jasperb7000ca2013-08-01 17:58:23 +0000143
Daniel Jasper4e778092013-02-06 10:05:46 +0000144 if (StartsObjCMethodExpr) {
145 Contexts.back().ColonIsObjCMethodExpr = true;
146 Left->Type = TT_ObjCMethodExpr;
147 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000148
Daniel Jasper431f5912013-05-28 08:33:00 +0000149 bool MightBeFunctionType = CurrentToken->is(tok::star);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000150 bool HasMultipleLines = false;
151 bool HasMultipleParametersOnALine = false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700152 while (CurrentToken) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000153 // LookForDecls is set when "if (" has been seen. Check for
154 // 'identifier' '*' 'identifier' followed by not '=' -- this
155 // '*' has to be a binary operator but determineStarAmpUsage() will
156 // categorize it as an unary operator, so set the right type here.
Manuel Klimekb3987012013-05-29 14:47:47 +0000157 if (LookForDecls && CurrentToken->Next) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000158 FormatToken *Prev = CurrentToken->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000159 if (Prev) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000160 FormatToken *PrevPrev = Prev->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000161 FormatToken *Next = CurrentToken->Next;
162 if (PrevPrev && PrevPrev->is(tok::identifier) &&
163 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
164 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
165 Prev->Type = TT_BinaryOperator;
166 LookForDecls = false;
167 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000168 }
169 }
170
Daniel Jasper53352602013-08-12 12:16:34 +0000171 if (CurrentToken->Previous->Type == TT_PointerOrReference &&
172 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
173 tok::coloncolon))
174 MightBeFunctionType = true;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700175 if (CurrentToken->Previous->Type == TT_BinaryOperator)
176 Contexts.back().IsExpression = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000177 if (CurrentToken->is(tok::r_paren)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000178 if (MightBeFunctionType && CurrentToken->Next &&
Daniel Jaspere7d3bff2013-07-16 11:37:21 +0000179 (CurrentToken->Next->is(tok::l_paren) ||
180 (CurrentToken->Next->is(tok::l_square) &&
181 !Contexts.back().IsExpression)))
Daniel Jasper431f5912013-05-28 08:33:00 +0000182 Left->Type = TT_FunctionTypeLParen;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000183 Left->MatchingParen = CurrentToken;
184 CurrentToken->MatchingParen = Left;
185
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000186 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000187 CurrentToken->Type = TT_ObjCMethodExpr;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700188 if (Contexts.back().FirstObjCSelectorName) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000189 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
190 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000191 }
192 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000193
Stephen Hines651f13c2014-04-23 16:59:28 -0700194 if (Left->Type == TT_AttributeParen)
195 CurrentToken->Type = TT_AttributeParen;
Stephen Hines176edba2014-12-01 14:53:08 -0800196 if (Left->Previous && Left->Previous->Type == TT_JavaAnnotation)
197 CurrentToken->Type = TT_JavaAnnotation;
198 if (Left->Previous && Left->Previous->Type == TT_LeadingJavaAnnotation)
199 CurrentToken->Type = TT_LeadingJavaAnnotation;
Stephen Hines651f13c2014-04-23 16:59:28 -0700200
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000201 if (!HasMultipleLines)
202 Left->PackingKind = PPK_Inconclusive;
203 else if (HasMultipleParametersOnALine)
204 Left->PackingKind = PPK_BinPacked;
205 else
206 Left->PackingKind = PPK_OnePerLine;
207
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000208 next();
209 return true;
210 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000211 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000212 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -0700213 else if (CurrentToken->is(tok::l_brace))
214 Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000215 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
216 !CurrentToken->Next->HasUnescapedNewline &&
217 !CurrentToken->Next->isTrailingComment())
218 HasMultipleParametersOnALine = true;
Stephen Hines651f13c2014-04-23 16:59:28 -0700219 if (CurrentToken->isOneOf(tok::kw_const, tok::kw_auto) ||
220 CurrentToken->isSimpleTypeSpecifier())
221 Contexts.back().IsExpression = false;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700222 FormatToken *Tok = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000223 if (!consumeToken())
224 return false;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700225 updateParameterCount(Left, Tok);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000226 if (CurrentToken && CurrentToken->HasUnescapedNewline)
227 HasMultipleLines = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000228 }
229 return false;
230 }
231
232 bool parseSquare() {
233 if (!CurrentToken)
234 return false;
235
Alexander Kornienkod71b15b2013-06-17 13:19:53 +0000236 // A '[' could be an index subscript (after an identifier or after
Nico Weber051860e2013-02-10 02:08:05 +0000237 // ')' or ']'), it could be the start of an Objective-C method
238 // expression, or it could the the start of an Objective-C array literal.
Manuel Klimekb3987012013-05-29 14:47:47 +0000239 FormatToken *Left = CurrentToken->Previous;
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000240 FormatToken *Parent = Left->getPreviousNonComment();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000241 bool StartsObjCMethodExpr =
Daniel Jasper567dcf92013-09-05 09:29:45 +0000242 Contexts.back().CanBeExpression && Left->Type != TT_LambdaLSquare &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700243 CurrentToken->isNot(tok::l_brace) &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000244 (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
245 tok::kw_return, tok::kw_throw) ||
Daniel Jasperac3223e2013-04-10 09:49:49 +0000246 Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000247 Parent->Type == TT_CastRParen ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000248 getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
Daniel Jasper923ebef2013-03-14 13:45:21 +0000249 ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
Daniel Jasper6f21a982013-03-13 07:49:51 +0000250 Contexts.back().IsExpression = true;
Daniel Jasper8f54d882013-10-26 17:00:22 +0000251 bool ColonFound = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000252
Daniel Jasper4e778092013-02-06 10:05:46 +0000253 if (StartsObjCMethodExpr) {
254 Contexts.back().ColonIsObjCMethodExpr = true;
255 Left->Type = TT_ObjCMethodExpr;
Daniel Jaspera07aa662013-10-22 15:30:28 +0000256 } else if (Parent && Parent->is(tok::at)) {
257 Left->Type = TT_ArrayInitializerLSquare;
258 } else if (Left->Type == TT_Unknown) {
259 Left->Type = TT_ArraySubscriptLSquare;
Daniel Jasper4e778092013-02-06 10:05:46 +0000260 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000261
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700262 while (CurrentToken) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000263 if (CurrentToken->is(tok::r_square)) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000264 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
265 Left->Type == TT_ObjCMethodExpr) {
Nico Webere8a97982013-02-06 06:20:11 +0000266 // An ObjC method call is rarely followed by an open parenthesis.
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000267 // FIXME: Do we incorrectly label ":" with this?
268 StartsObjCMethodExpr = false;
269 Left->Type = TT_Unknown;
270 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700271 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000272 CurrentToken->Type = TT_ObjCMethodExpr;
Nico Webere8a97982013-02-06 06:20:11 +0000273 // determineStarAmpUsage() thinks that '*' '[' is allocating an
274 // array of pointers, but if '[' starts a selector then '*' is a
275 // binary operator.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700276 if (Parent && Parent->Type == TT_PointerOrReference)
Nico Weber4ed7f3e2013-02-06 16:54:35 +0000277 Parent->Type = TT_BinaryOperator;
Daniel Jasper01786732013-02-04 07:21:18 +0000278 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000279 Left->MatchingParen = CurrentToken;
280 CurrentToken->MatchingParen = Left;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700281 if (Contexts.back().FirstObjCSelectorName) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000282 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
283 Contexts.back().LongestObjCSelectorName;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700284 if (Left->BlockParameterCount > 1)
Stephen Hines651f13c2014-04-23 16:59:28 -0700285 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
286 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000287 next();
288 return true;
289 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000290 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000291 return false;
Stephen Hines176edba2014-12-01 14:53:08 -0800292 if (CurrentToken->is(tok::colon)) {
293 if (Left->Type == TT_ArraySubscriptLSquare) {
294 Left->Type = TT_ObjCMethodExpr;
295 StartsObjCMethodExpr = true;
296 Contexts.back().ColonIsObjCMethodExpr = true;
297 if (Parent && Parent->is(tok::r_paren))
298 Parent->Type = TT_CastRParen;
299 }
Daniel Jasper8f54d882013-10-26 17:00:22 +0000300 ColonFound = true;
Stephen Hines176edba2014-12-01 14:53:08 -0800301 }
Daniel Jaspera07aa662013-10-22 15:30:28 +0000302 if (CurrentToken->is(tok::comma) &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700303 Style.Language != FormatStyle::LK_Proto &&
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000304 (Left->Type == TT_ArraySubscriptLSquare ||
Daniel Jasper8f54d882013-10-26 17:00:22 +0000305 (Left->Type == TT_ObjCMethodExpr && !ColonFound)))
Daniel Jaspera07aa662013-10-22 15:30:28 +0000306 Left->Type = TT_ArrayInitializerLSquare;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700307 FormatToken* Tok = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000308 if (!consumeToken())
309 return false;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700310 updateParameterCount(Left, Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000311 }
312 return false;
313 }
314
315 bool parseBrace() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700316 if (CurrentToken) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000317 FormatToken *Left = CurrentToken->Previous;
Stephen Hines651f13c2014-04-23 16:59:28 -0700318
319 if (Contexts.back().CaretFound)
320 Left->Type = TT_ObjCBlockLBrace;
321 Contexts.back().CaretFound = false;
322
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000323 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
324 Contexts.back().ColonIsDictLiteral = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700325 if (Left->BlockKind == BK_BracedInit)
326 Contexts.back().IsExpression = true;
Nico Weberf2ff8122013-05-26 05:39:26 +0000327
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700328 while (CurrentToken) {
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000329 if (CurrentToken->is(tok::r_brace)) {
330 Left->MatchingParen = CurrentToken;
331 CurrentToken->MatchingParen = Left;
332 next();
333 return true;
334 }
335 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
336 return false;
337 updateParameterCount(Left, CurrentToken);
Stephen Hines176edba2014-12-01 14:53:08 -0800338 if (CurrentToken->isOneOf(tok::colon, tok::l_brace)) {
339 FormatToken *Previous = CurrentToken->getPreviousNonComment();
340 if ((CurrentToken->is(tok::colon) ||
341 Style.Language == FormatStyle::LK_Proto) &&
342 Previous->is(tok::identifier))
343 Previous->Type = TT_SelectorName;
344 if (CurrentToken->is(tok::colon))
345 Left->Type = TT_DictLiteral;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700346 }
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000347 if (!consumeToken())
348 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000349 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000350 }
351 return true;
352 }
Daniel Jasperc4615b72013-02-20 12:56:39 +0000353
Manuel Klimekb3987012013-05-29 14:47:47 +0000354 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700355 if (Current->Type == TT_LambdaLSquare ||
356 (Current->is(tok::caret) && Current->Type == TT_UnaryOperator) ||
357 (Style.Language == FormatStyle::LK_JavaScript &&
Stephen Hines176edba2014-12-01 14:53:08 -0800358 Current->is(Keywords.kw_function))) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700359 ++Left->BlockParameterCount;
360 }
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000361 if (Current->is(tok::comma)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000362 ++Left->ParameterCount;
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000363 if (!Left->Role)
364 Left->Role.reset(new CommaSeparatedList(Style));
365 Left->Role->CommaFound(Current);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000366 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000367 Left->ParameterCount = 1;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000368 }
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000369 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000370
371 bool parseConditional() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700372 while (CurrentToken) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000373 if (CurrentToken->is(tok::colon)) {
374 CurrentToken->Type = TT_ConditionalExpr;
375 next();
376 return true;
377 }
378 if (!consumeToken())
379 return false;
380 }
381 return false;
382 }
383
384 bool parseTemplateDeclaration() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700385 if (CurrentToken && CurrentToken->is(tok::less)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000386 CurrentToken->Type = TT_TemplateOpener;
387 next();
388 if (!parseAngle())
389 return false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700390 if (CurrentToken)
Manuel Klimekb3987012013-05-29 14:47:47 +0000391 CurrentToken->Previous->ClosesTemplateDeclaration = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000392 return true;
393 }
394 return false;
395 }
396
397 bool consumeToken() {
Manuel Klimekb3987012013-05-29 14:47:47 +0000398 FormatToken *Tok = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000399 next();
Manuel Klimekb3987012013-05-29 14:47:47 +0000400 switch (Tok->Tok.getKind()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000401 case tok::plus:
402 case tok::minus:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700403 if (!Tok->Previous && Line.MustBeDeclaration)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000404 Tok->Type = TT_ObjCMethodSpecifier;
405 break;
406 case tok::colon:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700407 if (!Tok->Previous)
Daniel Jaspercf6d76a2013-03-18 12:50:26 +0000408 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000409 // Colons from ?: are handled in parseConditional().
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700410 if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1 &&
411 Line.First->isNot(tok::kw_case)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000412 Tok->Type = TT_CtorInitializerColon;
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000413 } else if (Contexts.back().ColonIsDictLiteral) {
414 Tok->Type = TT_DictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000415 } else if (Contexts.back().ColonIsObjCMethodExpr ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000416 Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000417 Tok->Type = TT_ObjCMethodExpr;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700418 Tok->Previous->Type = TT_SelectorName;
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000419 if (Tok->Previous->ColumnWidth >
Alexander Kornienko00895102013-06-05 14:09:10 +0000420 Contexts.back().LongestObjCSelectorName) {
Alexander Kornienko01fe9f92013-10-10 13:36:20 +0000421 Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
Alexander Kornienko00895102013-06-05 14:09:10 +0000422 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700423 if (!Contexts.back().FirstObjCSelectorName)
Manuel Klimekb3987012013-05-29 14:47:47 +0000424 Contexts.back().FirstObjCSelectorName = Tok->Previous;
Daniel Jasper4e778092013-02-06 10:05:46 +0000425 } else if (Contexts.back().ColonIsForRangeExpr) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000426 Tok->Type = TT_RangeBasedForLoopColon;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700427 } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
Alexander Kornienko01fe9f92013-10-10 13:36:20 +0000428 Tok->Type = TT_BitFieldColon;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700429 } else if (Contexts.size() == 1 &&
430 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
Daniel Jasper6cabab42013-02-14 08:42:54 +0000431 Tok->Type = TT_InheritanceColon;
Stephen Hines176edba2014-12-01 14:53:08 -0800432 } else if (Tok->Previous->is(tok::identifier) && Tok->Next &&
433 Tok->Next->isOneOf(tok::r_paren, tok::comma)) {
434 // This handles a special macro in ObjC code where selectors including
435 // the colon are passed as macro arguments.
436 Tok->Type = TT_ObjCMethodExpr;
Daniel Jasper923ebef2013-03-14 13:45:21 +0000437 } else if (Contexts.back().ContextKind == tok::l_paren) {
438 Tok->Type = TT_InlineASMColon;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000439 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000440 break;
441 case tok::kw_if:
442 case tok::kw_while:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700443 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000444 next();
Nico Weber27268772013-06-26 00:30:14 +0000445 if (!parseParens(/*LookForDecls=*/true))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000446 return false;
447 }
448 break;
449 case tok::kw_for:
Daniel Jasper4e778092013-02-06 10:05:46 +0000450 Contexts.back().ColonIsForRangeExpr = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000451 next();
452 if (!parseParens())
453 return false;
454 break;
455 case tok::l_paren:
456 if (!parseParens())
457 return false;
Daniel Jasper59875ac2013-11-07 17:43:07 +0000458 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700459 !Contexts.back().IsExpression &&
460 Line.First->Type != TT_ObjCProperty &&
461 (!Tok->Previous || Tok->Previous->isNot(tok::kw_decltype)))
Daniel Jasper3c08a812013-02-24 18:54:32 +0000462 Line.MightBeFunctionDecl = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000463 break;
464 case tok::l_square:
465 if (!parseSquare())
466 return false;
467 break;
468 case tok::l_brace:
469 if (!parseBrace())
470 return false;
471 break;
472 case tok::less:
Stephen Hines176edba2014-12-01 14:53:08 -0800473 if ((!Tok->Previous || !Tok->Previous->Tok.isLiteral()) && parseAngle())
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000474 Tok->Type = TT_TemplateOpener;
475 else {
476 Tok->Type = TT_BinaryOperator;
477 CurrentToken = Tok;
478 next();
479 }
480 break;
481 case tok::r_paren:
482 case tok::r_square:
483 return false;
484 case tok::r_brace:
485 // Lines can start with '}'.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700486 if (Tok->Previous)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000487 return false;
488 break;
489 case tok::greater:
490 Tok->Type = TT_BinaryOperator;
491 break;
492 case tok::kw_operator:
Daniel Jasper174f60f2013-09-02 09:20:39 +0000493 while (CurrentToken &&
494 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000495 if (CurrentToken->isOneOf(tok::star, tok::amp))
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000496 CurrentToken->Type = TT_PointerOrReference;
497 consumeToken();
Daniel Jasper174f60f2013-09-02 09:20:39 +0000498 if (CurrentToken && CurrentToken->Previous->Type == TT_BinaryOperator)
Daniel Jasperc476ea92013-08-28 07:27:35 +0000499 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000500 }
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000501 if (CurrentToken) {
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000502 CurrentToken->Type = TT_OverloadedOperatorLParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000503 if (CurrentToken->Previous->Type == TT_BinaryOperator)
504 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000505 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000506 break;
507 case tok::question:
508 parseConditional();
509 break;
510 case tok::kw_template:
511 parseTemplateDeclaration();
512 break;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000513 case tok::identifier:
Stephen Hines176edba2014-12-01 14:53:08 -0800514 if (Line.First->is(tok::kw_for) && Tok->is(Keywords.kw_in))
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000515 Tok->Type = TT_ObjCForIn;
516 break;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000517 case tok::comma:
518 if (Contexts.back().FirstStartOfName)
519 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000520 if (Contexts.back().InCtorInitializer)
521 Tok->Type = TT_CtorInitializerComma;
Stephen Hines651f13c2014-04-23 16:59:28 -0700522 if (Contexts.back().IsForEachMacro)
523 Contexts.back().IsExpression = true;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000524 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000525 default:
526 break;
527 }
528 return true;
529 }
530
531 void parseIncludeDirective() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700532 if (CurrentToken && CurrentToken->is(tok::less)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000533 next();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700534 while (CurrentToken) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000535 if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000536 CurrentToken->Type = TT_ImplicitStringLiteral;
537 next();
538 }
539 } else {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700540 while (CurrentToken) {
Stephen Hines176edba2014-12-01 14:53:08 -0800541 if (CurrentToken->isNot(tok::comment))
542 // Mark these tokens as "implicit" string literals, so that
Daniel Jasper3a204412013-02-23 07:46:38 +0000543 // they are not split or line-wrapped.
544 CurrentToken->Type = TT_ImplicitStringLiteral;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000545 next();
546 }
547 }
548 }
549
550 void parseWarningOrError() {
551 next();
552 // We still want to format the whitespace left of the first token of the
553 // warning or error.
554 next();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700555 while (CurrentToken) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000556 CurrentToken->Type = TT_ImplicitStringLiteral;
557 next();
558 }
559 }
560
Stephen Hines651f13c2014-04-23 16:59:28 -0700561 void parsePragma() {
562 next(); // Consume "pragma".
563 if (CurrentToken && CurrentToken->TokenText == "mark") {
564 next(); // Consume "mark".
565 next(); // Consume first token (so we fix leading whitespace).
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700566 while (CurrentToken) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700567 CurrentToken->Type = TT_ImplicitStringLiteral;
568 next();
569 }
570 }
571 }
572
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000573 void parsePreprocessorDirective() {
574 next();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700575 if (!CurrentToken)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000576 return;
Alexander Kornienkob18c2582013-10-11 21:43:05 +0000577 if (CurrentToken->Tok.is(tok::numeric_constant)) {
578 CurrentToken->SpacesRequiredBefore = 1;
579 return;
580 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000581 // Hashes in the middle of a line can lead to any strange token
582 // sequence.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700583 if (!CurrentToken->Tok.getIdentifierInfo())
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000584 return;
Manuel Klimekb3987012013-05-29 14:47:47 +0000585 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000586 case tok::pp_include:
587 case tok::pp_import:
Stephen Hines176edba2014-12-01 14:53:08 -0800588 next();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000589 parseIncludeDirective();
590 break;
591 case tok::pp_error:
592 case tok::pp_warning:
593 parseWarningOrError();
594 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700595 case tok::pp_pragma:
596 parsePragma();
597 break;
Daniel Jasperaae7bad2013-04-23 13:54:04 +0000598 case tok::pp_if:
599 case tok::pp_elif:
Stephen Hines651f13c2014-04-23 16:59:28 -0700600 Contexts.back().IsExpression = true;
Daniel Jasperaae7bad2013-04-23 13:54:04 +0000601 parseLine();
602 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000603 default:
604 break;
605 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700606 while (CurrentToken)
Daniel Jasper5b7e7b02013-02-05 09:34:14 +0000607 next();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000608 }
609
Nico Weber95e8e462013-02-12 16:17:07 +0000610public:
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000611 LineType parseLine() {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000612 if (CurrentToken->is(tok::hash)) {
613 parsePreprocessorDirective();
614 return LT_PreprocessorDirective;
615 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700616
617 // Directly allow to 'import <string-literal>' to support protocol buffer
618 // definitions (code.google.com/p/protobuf) or missing "#" (either way we
619 // should not break the line).
620 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
621 if (Info && Info->getPPKeywordID() == tok::pp_import &&
Stephen Hines176edba2014-12-01 14:53:08 -0800622 CurrentToken->Next) {
623 next();
Stephen Hines651f13c2014-04-23 16:59:28 -0700624 parseIncludeDirective();
Stephen Hines176edba2014-12-01 14:53:08 -0800625 return LT_Other;
626 }
627
628 // If this line starts and ends in '<' and '>', respectively, it is likely
629 // part of "#define <a/b.h>".
630 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
631 parseIncludeDirective();
632 return LT_Other;
633 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700634
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700635 while (CurrentToken) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000636 if (CurrentToken->is(tok::kw_virtual))
637 KeywordVirtualFound = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000638 if (!consumeToken())
639 return LT_Invalid;
640 }
641 if (KeywordVirtualFound)
642 return LT_VirtualFunctionDecl;
643
Manuel Klimekb3987012013-05-29 14:47:47 +0000644 if (Line.First->Type == TT_ObjCMethodSpecifier) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700645 if (Contexts.back().FirstObjCSelectorName)
Daniel Jasper4e778092013-02-06 10:05:46 +0000646 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
647 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000648 return LT_ObjCMethodDecl;
649 }
650
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000651 return LT_Other;
652 }
653
Nico Weber95e8e462013-02-12 16:17:07 +0000654private:
Stephen Hines651f13c2014-04-23 16:59:28 -0700655 void resetTokenMetadata(FormatToken *Token) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700656 if (!Token)
657 return;
Stephen Hines651f13c2014-04-23 16:59:28 -0700658
659 // Reset token type in case we have already looked at it and then
660 // recovered from an error (e.g. failure to find the matching >).
661 if (CurrentToken->Type != TT_LambdaLSquare &&
662 CurrentToken->Type != TT_FunctionLBrace &&
663 CurrentToken->Type != TT_ImplicitStringLiteral &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700664 CurrentToken->Type != TT_RegexLiteral &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700665 CurrentToken->Type != TT_TrailingReturnArrow)
666 CurrentToken->Type = TT_Unknown;
Stephen Hines176edba2014-12-01 14:53:08 -0800667 CurrentToken->Role.reset();
Stephen Hines651f13c2014-04-23 16:59:28 -0700668 CurrentToken->FakeLParens.clear();
669 CurrentToken->FakeRParens = 0;
670 }
671
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000672 void next() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700673 if (CurrentToken) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700674 CurrentToken->NestingLevel = Contexts.size() - 1;
Stephen Hines176edba2014-12-01 14:53:08 -0800675 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
676 determineTokenType(*CurrentToken);
Manuel Klimekb3987012013-05-29 14:47:47 +0000677 CurrentToken = CurrentToken->Next;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700678 }
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000679
Stephen Hines651f13c2014-04-23 16:59:28 -0700680 resetTokenMetadata(CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000681 }
682
Daniel Jasper4e778092013-02-06 10:05:46 +0000683 /// \brief A struct to hold information valid in a specific context, e.g.
684 /// a pair of parenthesis.
685 struct Context {
Daniel Jasper923ebef2013-03-14 13:45:21 +0000686 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
687 bool IsExpression)
688 : ContextKind(ContextKind), BindingStrength(BindingStrength),
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700689 LongestObjCSelectorName(0), ColonIsForRangeExpr(false),
690 ColonIsDictLiteral(false), ColonIsObjCMethodExpr(false),
691 FirstObjCSelectorName(nullptr), FirstStartOfName(nullptr),
692 IsExpression(IsExpression), CanBeExpression(true),
693 InTemplateArgument(false), InCtorInitializer(false),
694 CaretFound(false), IsForEachMacro(false) {}
Daniel Jasper01786732013-02-04 07:21:18 +0000695
Daniel Jasper923ebef2013-03-14 13:45:21 +0000696 tok::TokenKind ContextKind;
Daniel Jasper4e778092013-02-06 10:05:46 +0000697 unsigned BindingStrength;
698 unsigned LongestObjCSelectorName;
699 bool ColonIsForRangeExpr;
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000700 bool ColonIsDictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000701 bool ColonIsObjCMethodExpr;
Manuel Klimekb3987012013-05-29 14:47:47 +0000702 FormatToken *FirstObjCSelectorName;
703 FormatToken *FirstStartOfName;
Daniel Jasper4e778092013-02-06 10:05:46 +0000704 bool IsExpression;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000705 bool CanBeExpression;
Stephen Hines651f13c2014-04-23 16:59:28 -0700706 bool InTemplateArgument;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000707 bool InCtorInitializer;
Stephen Hines651f13c2014-04-23 16:59:28 -0700708 bool CaretFound;
709 bool IsForEachMacro;
Daniel Jasper4e778092013-02-06 10:05:46 +0000710 };
711
712 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
713 /// of each instance.
714 struct ScopedContextCreator {
715 AnnotatingParser &P;
716
Daniel Jasper923ebef2013-03-14 13:45:21 +0000717 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
718 unsigned Increase)
719 : P(P) {
Daniel Jasper2a409b62013-07-08 14:34:09 +0000720 P.Contexts.push_back(Context(ContextKind,
721 P.Contexts.back().BindingStrength + Increase,
722 P.Contexts.back().IsExpression));
Daniel Jasper4e778092013-02-06 10:05:46 +0000723 }
724
725 ~ScopedContextCreator() { P.Contexts.pop_back(); }
726 };
Daniel Jasper01786732013-02-04 07:21:18 +0000727
Manuel Klimekb3987012013-05-29 14:47:47 +0000728 void determineTokenType(FormatToken &Current) {
729 if (Current.getPrecedence() == prec::Assignment &&
Daniel Jasper53352602013-08-12 12:16:34 +0000730 !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
Manuel Klimekb3987012013-05-29 14:47:47 +0000731 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000732 Contexts.back().IsExpression = true;
Manuel Klimekb3987012013-05-29 14:47:47 +0000733 for (FormatToken *Previous = Current.Previous;
Daniel Jasper7e274002013-09-11 20:37:10 +0000734 Previous && !Previous->isOneOf(tok::comma, tok::semi);
Manuel Klimekb3987012013-05-29 14:47:47 +0000735 Previous = Previous->Previous) {
Stephen Hines176edba2014-12-01 14:53:08 -0800736 if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
Daniel Jasper9c65b062013-02-27 11:43:50 +0000737 Previous = Previous->MatchingParen;
Stephen Hines176edba2014-12-01 14:53:08 -0800738 if (!Previous)
739 break;
740 }
741 if ((Previous->Type == TT_BinaryOperator ||
742 Previous->Type == TT_UnaryOperator) &&
743 Previous->isOneOf(tok::star, tok::amp) && Previous->Previous &&
744 Previous->Previous->isNot(tok::equal)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000745 Previous->Type = TT_PointerOrReference;
746 }
Daniel Jasper01786732013-02-04 07:21:18 +0000747 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700748 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000749 Contexts.back().IsExpression = true;
Stephen Hines651f13c2014-04-23 16:59:28 -0700750 } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700751 !Line.InPPDirective &&
752 (!Current.Previous ||
753 Current.Previous->isNot(tok::kw_decltype))) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700754 bool ParametersOfFunctionType =
755 Current.Previous && Current.Previous->is(tok::r_paren) &&
756 Current.Previous->MatchingParen &&
757 Current.Previous->MatchingParen->Type == TT_FunctionTypeLParen;
758 bool IsForOrCatch = Current.Previous &&
759 Current.Previous->isOneOf(tok::kw_for, tok::kw_catch);
760 Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000761 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000762 for (FormatToken *Previous = Current.Previous;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000763 Previous && Previous->isOneOf(tok::star, tok::amp);
Manuel Klimekb3987012013-05-29 14:47:47 +0000764 Previous = Previous->Previous)
Nico Weber95e8e462013-02-12 16:17:07 +0000765 Previous->Type = TT_PointerOrReference;
Stephen Hines176edba2014-12-01 14:53:08 -0800766 if (Line.MustBeDeclaration)
767 Contexts.back().IsExpression = Contexts.front().InCtorInitializer;
Manuel Klimekb3987012013-05-29 14:47:47 +0000768 } else if (Current.Previous &&
769 Current.Previous->Type == TT_CtorInitializerColon) {
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000770 Contexts.back().IsExpression = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000771 Contexts.back().InCtorInitializer = true;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000772 } else if (Current.is(tok::kw_new)) {
773 Contexts.back().CanBeExpression = false;
Daniel Jasperf9504aa2013-11-07 19:56:07 +0000774 } else if (Current.is(tok::semi) || Current.is(tok::exclaim)) {
Daniel Jasper16a69ef2013-05-03 14:41:24 +0000775 // This should be the condition or increment in a for-loop.
776 Contexts.back().IsExpression = true;
Nico Weber95e8e462013-02-12 16:17:07 +0000777 }
Daniel Jasper01786732013-02-04 07:21:18 +0000778
779 if (Current.Type == TT_Unknown) {
Daniel Jasper6b3ff8c2013-09-27 08:29:16 +0000780 // Line.MightBeFunctionDecl can only be true after the parentheses of a
781 // function declaration have been found. In this case, 'Current' is a
782 // trailing token of this declaration and thus cannot be a name.
Stephen Hines176edba2014-12-01 14:53:08 -0800783 if (isStartOfName(Current) &&
784 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000785 Contexts.back().FirstStartOfName = &Current;
Daniel Jasper3c08a812013-02-24 18:54:32 +0000786 Current.Type = TT_StartOfName;
Daniel Jasper2ca37412013-07-09 14:36:48 +0000787 } else if (Current.is(tok::kw_auto)) {
788 AutoFound = true;
Daniel Jasper3262f4c2013-07-11 14:33:06 +0000789 } else if (Current.is(tok::arrow) && AutoFound &&
Stephen Hines176edba2014-12-01 14:53:08 -0800790 Line.MustBeDeclaration && Current.NestingLevel == 0) {
Daniel Jasper2ca37412013-07-09 14:36:48 +0000791 Current.Type = TT_TrailingReturnArrow;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000792 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000793 Current.Type =
Daniel Jasperd6104f62013-07-05 13:30:40 +0000794 determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700795 Contexts.back().IsExpression,
796 Contexts.back().InTemplateArgument);
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000797 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000798 Current.Type = determinePlusMinusCaretUsage(Current);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700799 if (Current.Type == TT_UnaryOperator && Current.is(tok::caret))
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700800 Contexts.back().CaretFound = true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000801 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000802 Current.Type = determineIncrementUsage(Current);
Stephen Hines176edba2014-12-01 14:53:08 -0800803 } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
Daniel Jasper01786732013-02-04 07:21:18 +0000804 Current.Type = TT_UnaryOperator;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700805 } else if (Current.is(tok::question)) {
806 Current.Type = TT_ConditionalExpr;
Manuel Klimek31e44f72013-09-04 08:20:47 +0000807 } else if (Current.isBinaryOperator() &&
808 (!Current.Previous ||
809 Current.Previous->isNot(tok::l_square))) {
Daniel Jasper01786732013-02-04 07:21:18 +0000810 Current.Type = TT_BinaryOperator;
811 } else if (Current.is(tok::comment)) {
Alexander Kornienko00895102013-06-05 14:09:10 +0000812 if (Current.TokenText.startswith("//"))
Daniel Jasper01786732013-02-04 07:21:18 +0000813 Current.Type = TT_LineComment;
814 else
815 Current.Type = TT_BlockComment;
Nico Weber37d69312013-02-13 04:13:13 +0000816 } else if (Current.is(tok::r_paren)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700817 if (rParenEndsCast(Current))
Nico Weber37d69312013-02-13 04:13:13 +0000818 Current.Type = TT_CastRParen;
Manuel Klimekb3987012013-05-29 14:47:47 +0000819 } else if (Current.is(tok::at) && Current.Next) {
820 switch (Current.Next->Tok.getObjCKeywordID()) {
Daniel Jasper01786732013-02-04 07:21:18 +0000821 case tok::objc_interface:
822 case tok::objc_implementation:
823 case tok::objc_protocol:
824 Current.Type = TT_ObjCDecl;
825 break;
826 case tok::objc_property:
827 Current.Type = TT_ObjCProperty;
828 break;
829 default:
830 break;
831 }
Daniel Jasper5ad390d2013-05-28 11:30:49 +0000832 } else if (Current.is(tok::period)) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000833 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
Daniel Jasper5ad390d2013-05-28 11:30:49 +0000834 if (PreviousNoComment &&
835 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
836 Current.Type = TT_DesignatedInitializerPeriod;
Stephen Hines651f13c2014-04-23 16:59:28 -0700837 } else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
Stephen Hines176edba2014-12-01 14:53:08 -0800838 Current.Previous &&
839 !Current.Previous->isOneOf(tok::equal, tok::at) &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700840 Line.MightBeFunctionDecl && Contexts.size() == 1) {
841 // Line.MightBeFunctionDecl can only be true after the parentheses of a
842 // function declaration have been found.
843 Current.Type = TT_TrailingAnnotation;
Stephen Hines176edba2014-12-01 14:53:08 -0800844 } else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
845 Current.Previous->is(tok::at) &&
846 Current.isNot(Keywords.kw_interface)) {
847 const FormatToken& AtToken = *Current.Previous;
848 if (!AtToken.Previous ||
849 AtToken.Previous->Type == TT_LeadingJavaAnnotation)
850 Current.Type = TT_LeadingJavaAnnotation;
851 else
852 Current.Type = TT_JavaAnnotation;
Daniel Jasper01786732013-02-04 07:21:18 +0000853 }
854 }
855 }
856
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000857 /// \brief Take a guess at whether \p Tok starts a name of a function or
858 /// variable declaration.
859 ///
860 /// This is a heuristic based on whether \p Tok is an identifier following
861 /// something that is likely a type.
862 bool isStartOfName(const FormatToken &Tok) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700863 if (Tok.isNot(tok::identifier) || !Tok.Previous)
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000864 return false;
865
866 // Skip "const" as it does not have an influence on whether this is a name.
867 FormatToken *PreviousNotConst = Tok.Previous;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700868 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000869 PreviousNotConst = PreviousNotConst->Previous;
870
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700871 if (!PreviousNotConst)
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000872 return false;
873
Daniel Jasper2a409b62013-07-08 14:34:09 +0000874 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
875 PreviousNotConst->Previous &&
876 PreviousNotConst->Previous->is(tok::hash);
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000877
Daniel Jasper92495a82013-08-19 10:16:18 +0000878 if (PreviousNotConst->Type == TT_TemplateCloser)
879 return PreviousNotConst && PreviousNotConst->MatchingParen &&
880 PreviousNotConst->MatchingParen->Previous &&
Stephen Hines176edba2014-12-01 14:53:08 -0800881 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
Daniel Jasper92495a82013-08-19 10:16:18 +0000882 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
883
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700884 if (PreviousNotConst->is(tok::r_paren) && PreviousNotConst->MatchingParen &&
885 PreviousNotConst->MatchingParen->Previous &&
886 PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype))
887 return true;
888
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000889 return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
890 PreviousNotConst->Type == TT_PointerOrReference ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700891 PreviousNotConst->isSimpleTypeSpecifier();
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000892 }
893
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700894 /// \brief Determine whether ')' is ending a cast.
895 bool rParenEndsCast(const FormatToken &Tok) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700896 FormatToken *LeftOfParens = nullptr;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700897 if (Tok.MatchingParen)
898 LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
Stephen Hines176edba2014-12-01 14:53:08 -0800899 if (LeftOfParens && LeftOfParens->is(tok::r_paren) &&
900 LeftOfParens->MatchingParen)
901 LeftOfParens = LeftOfParens->MatchingParen->Previous;
902 if (LeftOfParens && LeftOfParens->is(tok::r_square) &&
903 LeftOfParens->MatchingParen &&
904 LeftOfParens->MatchingParen->Type == TT_LambdaLSquare)
905 return false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700906 bool IsCast = false;
907 bool ParensAreEmpty = Tok.Previous == Tok.MatchingParen;
908 bool ParensAreType = !Tok.Previous ||
909 Tok.Previous->Type == TT_PointerOrReference ||
910 Tok.Previous->Type == TT_TemplateCloser ||
911 Tok.Previous->isSimpleTypeSpecifier();
Stephen Hines176edba2014-12-01 14:53:08 -0800912 if (Style.Language == FormatStyle::LK_JavaScript && Tok.Next &&
913 Tok.Next->is(Keywords.kw_in))
914 return false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700915 bool ParensCouldEndDecl =
916 Tok.Next && Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
917 bool IsSizeOfOrAlignOf =
918 LeftOfParens && LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
919 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
920 ((Contexts.size() > 1 && Contexts[Contexts.size() - 2].IsExpression) ||
921 (Tok.Next && Tok.Next->isBinaryOperator())))
922 IsCast = true;
923 else if (Tok.Next && Tok.Next->isNot(tok::string_literal) &&
924 (Tok.Next->Tok.isLiteral() ||
925 Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
926 IsCast = true;
927 // If there is an identifier after the (), it is likely a cast, unless
928 // there is also an identifier before the ().
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700929 else if (LeftOfParens &&
930 (LeftOfParens->Tok.getIdentifierInfo() == nullptr ||
931 LeftOfParens->is(tok::kw_return)) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700932 LeftOfParens->Type != TT_OverloadedOperator &&
933 LeftOfParens->isNot(tok::at) &&
934 LeftOfParens->Type != TT_TemplateCloser && Tok.Next) {
935 if (Tok.Next->isOneOf(tok::identifier, tok::numeric_constant)) {
936 IsCast = true;
937 } else {
938 // Use heuristics to recognize c style casting.
939 FormatToken *Prev = Tok.Previous;
940 if (Prev && Prev->isOneOf(tok::amp, tok::star))
941 Prev = Prev->Previous;
942
943 if (Prev && Tok.Next && Tok.Next->Next) {
944 bool NextIsUnary = Tok.Next->isUnaryOperator() ||
945 Tok.Next->isOneOf(tok::amp, tok::star);
Stephen Hines176edba2014-12-01 14:53:08 -0800946 IsCast =
947 NextIsUnary && !Tok.Next->is(tok::plus) &&
948 Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700949 }
950
951 for (; Prev != Tok.MatchingParen; Prev = Prev->Previous) {
952 if (!Prev || !Prev->isOneOf(tok::kw_const, tok::identifier)) {
953 IsCast = false;
954 break;
955 }
956 }
957 }
958 }
959 return IsCast && !ParensAreEmpty;
960 }
961
Daniel Jasper01786732013-02-04 07:21:18 +0000962 /// \brief Return the type of the given token assuming it is * or &.
Stephen Hines651f13c2014-04-23 16:59:28 -0700963 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
964 bool InTemplateArgument) {
Stephen Hines176edba2014-12-01 14:53:08 -0800965 if (Style.Language == FormatStyle::LK_JavaScript)
966 return TT_BinaryOperator;
967
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000968 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700969 if (!PrevToken)
Daniel Jasper01786732013-02-04 07:21:18 +0000970 return TT_UnaryOperator;
971
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000972 const FormatToken *NextToken = Tok.getNextNonComment();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700973 if (!NextToken || NextToken->is(tok::l_brace))
Daniel Jasper01786732013-02-04 07:21:18 +0000974 return TT_Unknown;
975
Stephen Hines176edba2014-12-01 14:53:08 -0800976 if (PrevToken->is(tok::coloncolon))
Daniel Jasper8a5d7cd2013-03-01 17:13:29 +0000977 return TT_PointerOrReference;
978
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000979 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
Daniel Jasperd3cf17b2013-03-14 10:50:25 +0000980 tok::comma, tok::semi, tok::kw_return, tok::colon,
Daniel Jasper65da8e92013-09-21 17:31:51 +0000981 tok::equal, tok::kw_delete, tok::kw_sizeof) ||
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000982 PrevToken->Type == TT_BinaryOperator ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700983 PrevToken->Type == TT_ConditionalExpr ||
Daniel Jasper01786732013-02-04 07:21:18 +0000984 PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
985 return TT_UnaryOperator;
986
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700987 if (NextToken->is(tok::l_square) && NextToken->Type != TT_LambdaLSquare)
Nico Webere8a97982013-02-06 06:20:11 +0000988 return TT_PointerOrReference;
Stephen Hines176edba2014-12-01 14:53:08 -0800989 if (NextToken->isOneOf(tok::kw_operator, tok::comma))
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700990 return TT_PointerOrReference;
Nico Webere8a97982013-02-06 06:20:11 +0000991
Daniel Jasperdb8afe42013-09-10 10:26:38 +0000992 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
993 PrevToken->MatchingParen->Previous &&
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700994 PrevToken->MatchingParen->Previous->isOneOf(tok::kw_typeof,
995 tok::kw_decltype))
Daniel Jasperdb8afe42013-09-10 10:26:38 +0000996 return TT_PointerOrReference;
997
Manuel Klimekb3987012013-05-29 14:47:47 +0000998 if (PrevToken->Tok.isLiteral() ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700999 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
Stephen Hines176edba2014-12-01 14:53:08 -08001000 tok::kw_false, tok::r_brace) ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001001 NextToken->Tok.isLiteral() ||
1002 NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
1003 NextToken->isUnaryOperator() ||
1004 // If we know we're in a template argument, there are no named
1005 // declarations. Thus, having an identifier on the right-hand side
1006 // indicates a binary operator.
1007 (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
Daniel Jasper01786732013-02-04 07:21:18 +00001008 return TT_BinaryOperator;
1009
Stephen Hines176edba2014-12-01 14:53:08 -08001010 // "&&(" is quite unlikely to be two successive unary "&".
1011 if (Tok.is(tok::ampamp) && NextToken && NextToken->is(tok::l_paren))
1012 return TT_BinaryOperator;
1013
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001014 // This catches some cases where evaluation order is used as control flow:
1015 // aaa && aaa->f();
1016 const FormatToken *NextNextToken = NextToken->getNextNonComment();
1017 if (NextNextToken && NextNextToken->is(tok::arrow))
1018 return TT_BinaryOperator;
1019
Daniel Jasper01786732013-02-04 07:21:18 +00001020 // It is very unlikely that we are going to find a pointer or reference type
1021 // definition on the RHS of an assignment.
1022 if (IsExpression)
1023 return TT_BinaryOperator;
1024
1025 return TT_PointerOrReference;
1026 }
1027
Manuel Klimekb3987012013-05-29 14:47:47 +00001028 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +00001029 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001030 if (!PrevToken || PrevToken->Type == TT_CastRParen)
Daniel Jasper01786732013-02-04 07:21:18 +00001031 return TT_UnaryOperator;
1032
1033 // Use heuristics to recognize unary operators.
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001034 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
1035 tok::question, tok::colon, tok::kw_return,
1036 tok::kw_case, tok::at, tok::l_brace))
Daniel Jasper01786732013-02-04 07:21:18 +00001037 return TT_UnaryOperator;
1038
Nico Weberee0feec2013-02-05 16:21:00 +00001039 // There can't be two consecutive binary operators.
Daniel Jasper01786732013-02-04 07:21:18 +00001040 if (PrevToken->Type == TT_BinaryOperator)
1041 return TT_UnaryOperator;
1042
1043 // Fall back to marking the token as binary operator.
1044 return TT_BinaryOperator;
1045 }
1046
1047 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
Manuel Klimekb3987012013-05-29 14:47:47 +00001048 TokenType determineIncrementUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +00001049 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001050 if (!PrevToken || PrevToken->Type == TT_CastRParen)
Daniel Jasper01786732013-02-04 07:21:18 +00001051 return TT_UnaryOperator;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001052 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
Daniel Jasper01786732013-02-04 07:21:18 +00001053 return TT_TrailingUnaryOperator;
1054
1055 return TT_UnaryOperator;
1056 }
Daniel Jasper4e778092013-02-06 10:05:46 +00001057
1058 SmallVector<Context, 8> Contexts;
1059
Daniel Jasperd4a03db2013-08-22 15:00:41 +00001060 const FormatStyle &Style;
Daniel Jasper4e778092013-02-06 10:05:46 +00001061 AnnotatedLine &Line;
Manuel Klimekb3987012013-05-29 14:47:47 +00001062 FormatToken *CurrentToken;
Daniel Jasper4e778092013-02-06 10:05:46 +00001063 bool KeywordVirtualFound;
Daniel Jasper2ca37412013-07-09 14:36:48 +00001064 bool AutoFound;
Stephen Hines176edba2014-12-01 14:53:08 -08001065 const AdditionalKeywords &Keywords;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001066};
1067
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001068static int PrecedenceUnaryOperator = prec::PointerToMember + 1;
1069static int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
1070
Daniel Jasper29f123b2013-02-08 15:28:42 +00001071/// \brief Parses binary expressions by inserting fake parenthesis based on
1072/// operator precedence.
1073class ExpressionParser {
1074public:
Stephen Hines176edba2014-12-01 14:53:08 -08001075 ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
1076 AnnotatedLine &Line)
1077 : Style(Style), Keywords(Keywords), Current(Line.First) {}
Daniel Jasper29f123b2013-02-08 15:28:42 +00001078
1079 /// \brief Parse expressions with the given operatore precedence.
Daniel Jasper237d4c12013-02-23 21:01:55 +00001080 void parse(int Precedence = 0) {
Daniel Jasper966e6d32013-11-07 19:23:49 +00001081 // Skip 'return' and ObjC selector colons as they are not part of a binary
1082 // expression.
1083 while (Current &&
1084 (Current->is(tok::kw_return) ||
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001085 (Current->is(tok::colon) && (Current->Type == TT_ObjCMethodExpr ||
1086 Current->Type == TT_DictLiteral))))
Daniel Jasperf78bf4a2013-09-30 08:29:03 +00001087 next();
1088
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001089 if (!Current || Precedence > PrecedenceArrowAndPeriod)
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001090 return;
1091
Daniel Jasperc01897c2013-05-31 14:56:12 +00001092 // Conditional expressions need to be parsed separately for proper nesting.
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001093 if (Precedence == prec::Conditional) {
Daniel Jasperc01897c2013-05-31 14:56:12 +00001094 parseConditionalExpr();
1095 return;
1096 }
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001097
1098 // Parse unary operators, which all have a higher precedence than binary
1099 // operators.
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001100 if (Precedence == PrecedenceUnaryOperator) {
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001101 parseUnaryOperator();
Daniel Jasper29f123b2013-02-08 15:28:42 +00001102 return;
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001103 }
Daniel Jasper29f123b2013-02-08 15:28:42 +00001104
Manuel Klimekb3987012013-05-29 14:47:47 +00001105 FormatToken *Start = Current;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001106 FormatToken *LatestOperator = nullptr;
1107 unsigned OperatorIndex = 0;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001108
Daniel Jasper237d4c12013-02-23 21:01:55 +00001109 while (Current) {
Daniel Jasper29f123b2013-02-08 15:28:42 +00001110 // Consume operators with higher precedence.
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001111 parse(Precedence + 1);
Daniel Jasper29f123b2013-02-08 15:28:42 +00001112
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001113 int CurrentPrecedence = getCurrentPrecedence();
1114
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001115 if (Current && Current->Type == TT_SelectorName &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001116 Precedence == CurrentPrecedence) {
1117 if (LatestOperator)
1118 addFakeParenthesis(Start, prec::Level(Precedence));
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001119 Start = Current;
Stephen Hines651f13c2014-04-23 16:59:28 -07001120 }
Daniel Jasper237d4c12013-02-23 21:01:55 +00001121
Daniel Jasper29f123b2013-02-08 15:28:42 +00001122 // At the end of the line or when an operator with higher precedence is
1123 // found, insert fake parenthesis and return.
Stephen Hines176edba2014-12-01 14:53:08 -08001124 if (!Current || (Current->closesScope() && Current->MatchingParen) ||
1125 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
1126 (CurrentPrecedence == prec::Conditional &&
1127 Precedence == prec::Assignment && Current->is(tok::colon))) {
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001128 if (LatestOperator) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001129 LatestOperator->LastOperator = true;
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001130 if (Precedence == PrecedenceArrowAndPeriod) {
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001131 // Call expressions don't have a binary operator precedence.
1132 addFakeParenthesis(Start, prec::Unknown);
1133 } else {
1134 addFakeParenthesis(Start, prec::Level(Precedence));
1135 }
1136 }
Daniel Jasper29f123b2013-02-08 15:28:42 +00001137 return;
1138 }
1139
1140 // Consume scopes: (), [], <> and {}
Daniel Jasperac3223e2013-04-10 09:49:49 +00001141 if (Current->opensScope()) {
1142 while (Current && !Current->closesScope()) {
Daniel Jasper29f123b2013-02-08 15:28:42 +00001143 next();
1144 parse();
1145 }
1146 next();
1147 } else {
1148 // Operator found.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001149 if (CurrentPrecedence == Precedence) {
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001150 LatestOperator = Current;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001151 Current->OperatorIndex = OperatorIndex;
1152 ++OperatorIndex;
1153 }
Stephen Hines176edba2014-12-01 14:53:08 -08001154 next(/*SkipPastLeadingComments=*/Precedence > 0);
Daniel Jasper29f123b2013-02-08 15:28:42 +00001155 }
1156 }
1157 }
1158
1159private:
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001160 /// \brief Gets the precedence (+1) of the given token for binary operators
1161 /// and other tokens that we treat like binary operators.
1162 int getCurrentPrecedence() {
1163 if (Current) {
Stephen Hines176edba2014-12-01 14:53:08 -08001164 const FormatToken *NextNonComment = Current->getNextNonComment();
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001165 if (Current->Type == TT_ConditionalExpr)
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001166 return prec::Conditional;
Stephen Hines176edba2014-12-01 14:53:08 -08001167 else if (NextNonComment && NextNonComment->is(tok::colon) &&
1168 NextNonComment->Type == TT_DictLiteral)
1169 return prec::Comma;
Daniel Jasper966e6d32013-11-07 19:23:49 +00001170 else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon ||
Stephen Hines176edba2014-12-01 14:53:08 -08001171 Current->Type == TT_SelectorName ||
1172 (Current->is(tok::comment) && NextNonComment &&
1173 NextNonComment->Type == TT_SelectorName))
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001174 return 0;
Stephen Hines651f13c2014-04-23 16:59:28 -07001175 else if (Current->Type == TT_RangeBasedForLoopColon)
1176 return prec::Comma;
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001177 else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001178 return Current->getPrecedence();
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001179 else if (Current->isOneOf(tok::period, tok::arrow))
1180 return PrecedenceArrowAndPeriod;
Stephen Hines176edba2014-12-01 14:53:08 -08001181 else if (Style.Language == FormatStyle::LK_Java &&
1182 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements))
1183 return 0;
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001184 }
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001185 return -1;
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001186 }
1187
Daniel Jasperc01897c2013-05-31 14:56:12 +00001188 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
1189 Start->FakeLParens.push_back(Precedence);
Daniel Jasperdb4813a2013-09-06 08:08:14 +00001190 if (Precedence > prec::Unknown)
1191 Start->StartsBinaryExpression = true;
1192 if (Current) {
Stephen Hines176edba2014-12-01 14:53:08 -08001193 FormatToken *Previous = Current->Previous;
1194 if (Previous->is(tok::comment) && Previous->Previous)
1195 Previous = Previous->Previous;
1196 ++Previous->FakeRParens;
Daniel Jasperdb4813a2013-09-06 08:08:14 +00001197 if (Precedence > prec::Unknown)
Stephen Hines176edba2014-12-01 14:53:08 -08001198 Previous->EndsBinaryExpression = true;
Daniel Jasperdb4813a2013-09-06 08:08:14 +00001199 }
Daniel Jasperc01897c2013-05-31 14:56:12 +00001200 }
1201
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001202 /// \brief Parse unary operator expressions and surround them with fake
1203 /// parentheses if appropriate.
1204 void parseUnaryOperator() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001205 if (!Current || Current->Type != TT_UnaryOperator) {
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001206 parse(PrecedenceArrowAndPeriod);
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001207 return;
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001208 }
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001209
1210 FormatToken *Start = Current;
1211 next();
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001212 parseUnaryOperator();
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001213
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001214 // The actual precedence doesn't matter.
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001215 addFakeParenthesis(Start, prec::Unknown);
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001216 }
1217
Daniel Jasperc01897c2013-05-31 14:56:12 +00001218 void parseConditionalExpr() {
Stephen Hines176edba2014-12-01 14:53:08 -08001219 while (Current && Current->isTrailingComment()) {
1220 next();
1221 }
Daniel Jasperc01897c2013-05-31 14:56:12 +00001222 FormatToken *Start = Current;
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001223 parse(prec::LogicalOr);
Daniel Jasperc01897c2013-05-31 14:56:12 +00001224 if (!Current || !Current->is(tok::question))
1225 return;
1226 next();
Stephen Hines176edba2014-12-01 14:53:08 -08001227 parse(prec::Assignment);
Daniel Jasperc01897c2013-05-31 14:56:12 +00001228 if (!Current || Current->Type != TT_ConditionalExpr)
1229 return;
1230 next();
Stephen Hines176edba2014-12-01 14:53:08 -08001231 parse(prec::Assignment);
Daniel Jasperc01897c2013-05-31 14:56:12 +00001232 addFakeParenthesis(Start, prec::Conditional);
1233 }
1234
Stephen Hines176edba2014-12-01 14:53:08 -08001235 void next(bool SkipPastLeadingComments = true) {
Alexander Kornienkod71b15b2013-06-17 13:19:53 +00001236 if (Current)
1237 Current = Current->Next;
Stephen Hines176edba2014-12-01 14:53:08 -08001238 while (Current &&
1239 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
1240 Current->isTrailingComment())
Manuel Klimekb3987012013-05-29 14:47:47 +00001241 Current = Current->Next;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001242 }
1243
Stephen Hines176edba2014-12-01 14:53:08 -08001244 const FormatStyle &Style;
1245 const AdditionalKeywords &Keywords;
Manuel Klimekb3987012013-05-29 14:47:47 +00001246 FormatToken *Current;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001247};
1248
Craig Topper14e66492013-07-01 04:03:19 +00001249} // end anonymous namespace
1250
Daniel Jasperb77d7412013-09-06 07:54:20 +00001251void
1252TokenAnnotator::setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001253 const AnnotatedLine *NextNonCommentLine = nullptr;
Daniel Jasper2a80ad62013-11-05 19:10:03 +00001254 for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
1255 E = Lines.rend();
1256 I != E; ++I) {
1257 if (NextNonCommentLine && (*I)->First->is(tok::comment) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001258 (*I)->First->Next == nullptr)
Daniel Jasper2a80ad62013-11-05 19:10:03 +00001259 (*I)->Level = NextNonCommentLine->Level;
Daniel Jasperb77d7412013-09-06 07:54:20 +00001260 else
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001261 NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr;
Daniel Jasper2a80ad62013-11-05 19:10:03 +00001262
1263 setCommentLineLevels((*I)->Children);
Daniel Jasperb77d7412013-09-06 07:54:20 +00001264 }
1265}
1266
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001267void TokenAnnotator::annotate(AnnotatedLine &Line) {
Daniel Jasperb77d7412013-09-06 07:54:20 +00001268 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1269 E = Line.Children.end();
Daniel Jasper567dcf92013-09-05 09:29:45 +00001270 I != E; ++I) {
1271 annotate(**I);
1272 }
Stephen Hines176edba2014-12-01 14:53:08 -08001273 AnnotatingParser Parser(Style, Line, Keywords);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001274 Line.Type = Parser.parseLine();
1275 if (Line.Type == LT_Invalid)
1276 return;
1277
Stephen Hines176edba2014-12-01 14:53:08 -08001278 ExpressionParser ExprParser(Style, Keywords, Line);
Daniel Jasper29f123b2013-02-08 15:28:42 +00001279 ExprParser.parse();
1280
Manuel Klimekb3987012013-05-29 14:47:47 +00001281 if (Line.First->Type == TT_ObjCMethodSpecifier)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001282 Line.Type = LT_ObjCMethodDecl;
Manuel Klimekb3987012013-05-29 14:47:47 +00001283 else if (Line.First->Type == TT_ObjCDecl)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001284 Line.Type = LT_ObjCDecl;
Manuel Klimekb3987012013-05-29 14:47:47 +00001285 else if (Line.First->Type == TT_ObjCProperty)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001286 Line.Type = LT_ObjCProperty;
1287
Manuel Klimekb3987012013-05-29 14:47:47 +00001288 Line.First->SpacesRequiredBefore = 1;
1289 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001290}
1291
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001292// This function heuristically determines whether 'Current' starts the name of a
1293// function declaration.
1294static bool isFunctionDeclarationName(const FormatToken &Current) {
1295 if (Current.Type != TT_StartOfName ||
Stephen Hines176edba2014-12-01 14:53:08 -08001296 Current.NestingLevel != 0)
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001297 return false;
1298 const FormatToken *Next = Current.Next;
1299 for (; Next; Next = Next->Next) {
1300 if (Next->Type == TT_TemplateOpener) {
1301 Next = Next->MatchingParen;
1302 } else if (Next->is(tok::coloncolon)) {
1303 Next = Next->Next;
1304 if (!Next || !Next->is(tok::identifier))
1305 return false;
1306 } else if (Next->is(tok::l_paren)) {
1307 break;
1308 } else {
1309 return false;
1310 }
1311 }
1312 if (!Next)
1313 return false;
1314 assert(Next->is(tok::l_paren));
1315 if (Next->Next == Next->MatchingParen)
1316 return true;
1317 for (const FormatToken *Tok = Next->Next; Tok != Next->MatchingParen;
1318 Tok = Tok->Next) {
1319 if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
1320 Tok->Type == TT_PointerOrReference || Tok->Type == TT_StartOfName)
1321 return true;
1322 if (Tok->isOneOf(tok::l_brace, tok::string_literal) || Tok->Tok.isLiteral())
1323 return false;
1324 }
1325 return false;
1326}
1327
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001328void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001329 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1330 E = Line.Children.end();
1331 I != E; ++I) {
1332 calculateFormattingInformation(**I);
1333 }
1334
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001335 Line.First->TotalLength =
1336 Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth;
Manuel Klimekb3987012013-05-29 14:47:47 +00001337 if (!Line.First->Next)
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001338 return;
Manuel Klimekb3987012013-05-29 14:47:47 +00001339 FormatToken *Current = Line.First->Next;
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001340 bool InFunctionDecl = Line.MightBeFunctionDecl;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001341 while (Current) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001342 if (isFunctionDeclarationName(*Current))
1343 Current->Type = TT_FunctionDeclarationName;
Stephen Hines651f13c2014-04-23 16:59:28 -07001344 if (Current->Type == TT_LineComment) {
1345 if (Current->Previous->BlockKind == BK_BracedInit &&
1346 Current->Previous->opensScope())
1347 Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1;
1348 else
1349 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
1350
1351 // If we find a trailing comment, iterate backwards to determine whether
1352 // it seems to relate to a specific parameter. If so, break before that
1353 // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
1354 // to the previous line in:
1355 // SomeFunction(a,
1356 // b, // comment
1357 // c);
1358 if (!Current->HasUnescapedNewline) {
1359 for (FormatToken *Parameter = Current->Previous; Parameter;
1360 Parameter = Parameter->Previous) {
1361 if (Parameter->isOneOf(tok::comment, tok::r_brace))
1362 break;
1363 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
1364 if (Parameter->Previous->Type != TT_CtorInitializerComma &&
1365 Parameter->HasUnescapedNewline)
1366 Parameter->MustBreakBefore = true;
1367 break;
1368 }
1369 }
1370 }
1371 } else if (Current->SpacesRequiredBefore == 0 &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001372 spaceRequiredBefore(Line, *Current)) {
Alexander Kornienkob18c2582013-10-11 21:43:05 +00001373 Current->SpacesRequiredBefore = 1;
Stephen Hines651f13c2014-04-23 16:59:28 -07001374 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001375
Daniel Jasperebaa1712013-09-17 09:52:48 +00001376 Current->MustBreakBefore =
1377 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
1378
Stephen Hines176edba2014-12-01 14:53:08 -08001379 if (Style.AlwaysBreakAfterDefinitionReturnType &&
1380 InFunctionDecl && Current->Type == TT_FunctionDeclarationName &&
1381 !Line.Last->isOneOf(tok::semi, tok::comment)) // Only for definitions.
1382 // FIXME: Line.Last points to other characters than tok::semi
1383 // and tok::lbrace.
1384 Current->MustBreakBefore = true;
1385
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001386 Current->CanBreakBefore =
1387 Current->MustBreakBefore || canBreakBefore(Line, *Current);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001388 unsigned ChildSize = 0;
1389 if (Current->Previous->Children.size() == 1) {
1390 FormatToken &LastOfChild = *Current->Previous->Children[0]->Last;
1391 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
1392 : LastOfChild.TotalLength + 1;
1393 }
Stephen Hines176edba2014-12-01 14:53:08 -08001394 const FormatToken *Prev= Current->Previous;
1395 if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
1396 (Prev->Children.size() == 1 &&
1397 Prev->Children[0]->First->MustBreakBefore) ||
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001398 Current->IsMultiline)
Stephen Hines176edba2014-12-01 14:53:08 -08001399 Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001400 else
Stephen Hines176edba2014-12-01 14:53:08 -08001401 Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
1402 ChildSize + Current->SpacesRequiredBefore;
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001403
1404 if (Current->Type == TT_CtorInitializerColon)
1405 InFunctionDecl = false;
1406
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001407 // FIXME: Only calculate this if CanBreakBefore is true once static
1408 // initializers etc. are sorted out.
1409 // FIXME: Move magic numbers to a better place.
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001410 Current->SplitPenalty = 20 * Current->BindingStrength +
1411 splitPenalty(Line, *Current, InFunctionDecl);
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001412
Manuel Klimekb3987012013-05-29 14:47:47 +00001413 Current = Current->Next;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001414 }
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001415
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001416 calculateUnbreakableTailLengths(Line);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001417 for (Current = Line.First; Current != nullptr; Current = Current->Next) {
Daniel Jasperd4a03db2013-08-22 15:00:41 +00001418 if (Current->Role)
1419 Current->Role->precomputeFormattingInfos(Current);
1420 }
1421
Daniel Jasper567dcf92013-09-05 09:29:45 +00001422 DEBUG({ printDebugInfo(Line); });
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001423}
1424
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001425void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1426 unsigned UnbreakableTailLength = 0;
Manuel Klimekb3987012013-05-29 14:47:47 +00001427 FormatToken *Current = Line.Last;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001428 while (Current) {
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001429 Current->UnbreakableTailLength = UnbreakableTailLength;
1430 if (Current->CanBreakBefore ||
1431 Current->isOneOf(tok::comment, tok::string_literal)) {
1432 UnbreakableTailLength = 0;
1433 } else {
1434 UnbreakableTailLength +=
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001435 Current->ColumnWidth + Current->SpacesRequiredBefore;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001436 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001437 Current = Current->Previous;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001438 }
1439}
1440
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001441unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001442 const FormatToken &Tok,
1443 bool InFunctionDecl) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001444 const FormatToken &Left = *Tok.Previous;
1445 const FormatToken &Right = Tok;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001446
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001447 if (Left.is(tok::semi))
1448 return 0;
Stephen Hines176edba2014-12-01 14:53:08 -08001449
1450 if (Style.Language == FormatStyle::LK_Java) {
1451 if (Left.Type == TT_LeadingJavaAnnotation)
1452 return 1;
1453 if (Right.is(Keywords.kw_extends))
1454 return 1;
1455 if (Right.is(Keywords.kw_implements))
1456 return 2;
1457 if (Left.is(tok::comma) && Left.NestingLevel == 0)
1458 return 3;
1459 }
1460
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001461 if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next &&
1462 Right.Next->Type == TT_DictLiteral))
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001463 return 1;
Stephen Hines651f13c2014-04-23 16:59:28 -07001464 if (Right.is(tok::l_square)) {
1465 if (Style.Language == FormatStyle::LK_Proto)
1466 return 1;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001467 if (Right.Type != TT_ObjCMethodExpr && Right.Type != TT_LambdaLSquare)
1468 return 500;
Stephen Hines651f13c2014-04-23 16:59:28 -07001469 }
Stephen Hines176edba2014-12-01 14:53:08 -08001470
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001471 if (Right.Type == TT_StartOfName ||
1472 Right.Type == TT_FunctionDeclarationName || Right.is(tok::kw_operator)) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001473 if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001474 return 3;
Daniel Jasperc18cff32013-07-11 12:34:23 +00001475 if (Left.Type == TT_StartOfName)
1476 return 20;
Stephen Hines651f13c2014-04-23 16:59:28 -07001477 if (InFunctionDecl && Right.NestingLevel == 0)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001478 return Style.PenaltyReturnTypeOnItsOwnLine;
Daniel Jasper92495a82013-08-19 10:16:18 +00001479 return 200;
Daniel Jasper3c08a812013-02-24 18:54:32 +00001480 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001481 if (Left.is(tok::equal) && Right.is(tok::l_brace))
1482 return 150;
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001483 if (Left.Type == TT_CastRParen)
1484 return 100;
Stephen Hines651f13c2014-04-23 16:59:28 -07001485 if (Left.is(tok::coloncolon) ||
1486 (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001487 return 500;
Daniel Jasper6b119d62013-04-05 17:22:09 +00001488 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1489 return 5000;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001490
Daniel Jasper6cabab42013-02-14 08:42:54 +00001491 if (Left.Type == TT_RangeBasedForLoopColon ||
1492 Left.Type == TT_InheritanceColon)
Daniel Jasper84a1a632013-02-26 13:18:08 +00001493 return 2;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001494
Daniel Jasperd3fef0f2013-08-27 14:24:43 +00001495 if (Right.isMemberAccess()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001496 if (Left.is(tok::r_paren) && Left.MatchingParen &&
Daniel Jasper2f0a0202013-09-06 08:54:24 +00001497 Left.MatchingParen->ParameterCount > 0)
Daniel Jasper518ee342013-02-26 13:59:14 +00001498 return 20; // Should be smaller than breaking at a nested comma.
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001499 return 150;
1500 }
1501
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001502 if (Right.Type == TT_TrailingAnnotation &&
1503 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
Stephen Hines176edba2014-12-01 14:53:08 -08001504 // Moving trailing annotations to the next line is fine for ObjC method
1505 // declarations.
1506 if (Line.First->Type == TT_ObjCMethodSpecifier)
1507
1508 return 10;
Stephen Hines651f13c2014-04-23 16:59:28 -07001509 // Generally, breaking before a trailing annotation is bad unless it is
1510 // function-like. It seems to be especially preferable to keep standard
1511 // annotations (i.e. "const", "final" and "override") on the same line.
1512 // Use a slightly higher penalty after ")" so that annotations like
1513 // "const override" are kept together.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001514 bool is_short_annotation = Right.TokenText.size() < 10;
1515 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
Stephen Hines651f13c2014-04-23 16:59:28 -07001516 }
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001517
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001518 // In for-loops, prefer breaking at ',' and ';'.
Manuel Klimekb3987012013-05-29 14:47:47 +00001519 if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
Daniel Jasper7d812812013-02-21 15:00:29 +00001520 return 4;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001521
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001522 // In Objective-C method expressions, prefer breaking before "param:" over
1523 // breaking after it.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001524 if (Right.Type == TT_SelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001525 return 0;
Daniel Jasper63d7ced2013-02-05 10:07:47 +00001526 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
Stephen Hines651f13c2014-04-23 16:59:28 -07001527 return Line.MightBeFunctionDecl ? 50 : 500;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001528
Stephen Hines176edba2014-12-01 14:53:08 -08001529 if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
Daniel Jasper1407bee2013-04-11 14:29:13 +00001530 return 100;
Stephen Hines651f13c2014-04-23 16:59:28 -07001531 if (Left.is(tok::equal) && InFunctionDecl)
1532 return 110;
Stephen Hines176edba2014-12-01 14:53:08 -08001533 if (Right.is(tok::r_brace))
1534 return 1;
1535 if (Left.Type == TT_TemplateOpener)
1536 return 100;
1537 if (Left.opensScope()) {
1538 if (!Style.AlignAfterOpenBracket)
1539 return 0;
Daniel Jasper47066e42013-10-25 14:29:37 +00001540 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
1541 : 19;
Stephen Hines176edba2014-12-01 14:53:08 -08001542 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001543
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001544 if (Right.is(tok::lessless)) {
1545 if (Left.is(tok::string_literal)) {
Alexander Kornienko00895102013-06-05 14:09:10 +00001546 StringRef Content = Left.TokenText;
Daniel Jasper20376982013-09-29 12:02:57 +00001547 if (Content.startswith("\""))
1548 Content = Content.drop_front(1);
1549 if (Content.endswith("\""))
1550 Content = Content.drop_back(1);
1551 Content = Content.trim();
Daniel Jasperbfa1edd2013-03-14 14:00:17 +00001552 if (Content.size() > 1 &&
1553 (Content.back() == ':' || Content.back() == '='))
Daniel Jasper9637dda2013-07-15 14:33:14 +00001554 return 25;
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001555 }
Daniel Jasper0c368782013-07-15 15:04:42 +00001556 return 1; // Breaking at a << is really cheap.
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001557 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001558 if (Left.Type == TT_ConditionalExpr)
Daniel Jasper518ee342013-02-26 13:59:14 +00001559 return prec::Conditional;
Manuel Klimekb3987012013-05-29 14:47:47 +00001560 prec::Level Level = Left.getPrecedence();
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001561
1562 if (Level != prec::Unknown)
1563 return Level;
Daniel Jasper24849712013-03-01 16:48:32 +00001564
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001565 return 3;
1566}
1567
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001568bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001569 const FormatToken &Left,
1570 const FormatToken &Right) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001571 if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
1572 return true;
Stephen Hines651f13c2014-04-23 16:59:28 -07001573 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
1574 Left.Tok.getObjCKeywordID() == tok::objc_property)
1575 return true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001576 if (Right.is(tok::hashhash))
1577 return Left.is(tok::hash);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001578 if (Left.isOneOf(tok::hashhash, tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001579 return Right.is(tok::hash);
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001580 if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1581 return Style.SpaceInEmptyParentheses;
1582 if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
Daniel Jasper34f3d052013-08-21 08:39:01 +00001583 return (Right.Type == TT_CastRParen ||
1584 (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen))
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001585 ? Style.SpacesInCStyleCastParentheses
1586 : Style.SpacesInParentheses;
1587 if (Right.isOneOf(tok::semi, tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001588 return false;
1589 if (Right.is(tok::less) &&
Stephen Hines176edba2014-12-01 14:53:08 -08001590 (Left.isOneOf(tok::kw_template, tok::r_paren) ||
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001591 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1592 return true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001593 if (Left.isOneOf(tok::exclaim, tok::tilde))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001594 return false;
1595 if (Left.is(tok::at) &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001596 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1597 tok::numeric_constant, tok::l_paren, tok::l_brace,
1598 tok::kw_true, tok::kw_false))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001599 return false;
1600 if (Left.is(tok::coloncolon))
1601 return false;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001602 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001603 return false;
Daniel Jasperc47d7f12013-07-01 09:47:25 +00001604 if (Right.is(tok::ellipsis))
Daniel Jasperb3c887d2013-10-20 16:56:16 +00001605 return Left.Tok.isLiteral();
Manuel Klimek31e44f72013-09-04 08:20:47 +00001606 if (Left.is(tok::l_square) && Right.is(tok::amp))
1607 return false;
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001608 if (Right.Type == TT_PointerOrReference)
Manuel Klimekb3987012013-05-29 14:47:47 +00001609 return Left.Tok.isLiteral() ||
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001610 ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001611 Style.PointerAlignment != FormatStyle::PAS_Left);
Daniel Jasper3ff4a2f2013-05-28 15:27:10 +00001612 if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
Stephen Hines176edba2014-12-01 14:53:08 -08001613 (Left.Type != TT_PointerOrReference ||
1614 Style.PointerAlignment != FormatStyle::PAS_Right))
Daniel Jasper395228f2013-05-08 14:58:20 +00001615 return true;
Alexander Kornienko3fd9ccd2013-03-12 16:28:18 +00001616 if (Left.Type == TT_PointerOrReference)
Daniel Jasper3a1847e2013-07-01 09:34:09 +00001617 return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
Daniel Jasper9322aae2013-03-20 09:53:18 +00001618 ((Right.Type != TT_PointerOrReference) &&
Stephen Hines176edba2014-12-01 14:53:08 -08001619 Right.isNot(tok::l_paren) &&
1620 Style.PointerAlignment != FormatStyle::PAS_Right && Left.Previous &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001621 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001622 if (Right.is(tok::star) && Left.is(tok::l_paren))
1623 return false;
Nico Weber051860e2013-02-10 02:08:05 +00001624 if (Left.is(tok::l_square))
Stephen Hines176edba2014-12-01 14:53:08 -08001625 return (Left.Type == TT_ArrayInitializerLSquare &&
1626 Style.SpacesInContainerLiterals && Right.isNot(tok::r_square)) ||
1627 (Left.Type == TT_ArraySubscriptLSquare &&
1628 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
Nico Weber051860e2013-02-10 02:08:05 +00001629 if (Right.is(tok::r_square))
Stephen Hines176edba2014-12-01 14:53:08 -08001630 return Right.MatchingParen &&
1631 ((Style.SpacesInContainerLiterals &&
1632 Right.MatchingParen->Type == TT_ArrayInitializerLSquare) ||
1633 (Style.SpacesInSquareBrackets &&
1634 Right.MatchingParen->Type == TT_ArraySubscriptLSquare));
Daniel Jasperec172262013-08-30 10:36:58 +00001635 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001636 Right.Type != TT_LambdaLSquare && Left.isNot(tok::numeric_constant) &&
1637 Left.Type != TT_DictLiteral)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001638 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001639 if (Left.is(tok::colon))
1640 return Left.Type != TT_ObjCMethodExpr;
Stephen Hines176edba2014-12-01 14:53:08 -08001641 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
1642 return !Left.Children.empty(); // No spaces in "{}".
1643 if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) ||
1644 (Right.is(tok::r_brace) && Right.MatchingParen &&
1645 Right.MatchingParen->BlockKind != BK_Block))
1646 return !Style.Cpp11BracedListStyle;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001647 if (Left.Type == TT_BlockComment)
1648 return !Left.TokenText.endswith("=*/");
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001649 if (Right.is(tok::l_paren)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001650 if (Left.is(tok::r_paren) && Left.Type == TT_AttributeParen)
Daniel Jaspere0fa4c52013-07-17 20:25:02 +00001651 return true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001652 return Line.Type == LT_ObjCDecl ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001653 Left.isOneOf(tok::kw_new, tok::kw_delete, tok::semi) ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001654 (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
1655 (Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while,
Stephen Hines176edba2014-12-01 14:53:08 -08001656 tok::kw_switch, tok::kw_case) ||
1657 (Left.is(tok::kw_catch) &&
1658 (!Left.Previous || Left.Previous->isNot(tok::period))) ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001659 Left.IsForEachMacro)) ||
1660 (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
Stephen Hines176edba2014-12-01 14:53:08 -08001661 (Left.is(tok::identifier) || Left.isFunctionLikeKeyword()) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001662 Line.Type != LT_PreprocessorDirective);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001663 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001664 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001665 return false;
Daniel Jasper1bee0732013-05-23 18:05:18 +00001666 if (Right.Type == TT_UnaryOperator)
1667 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1668 (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001669 if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
1670 tok::r_paren) ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001671 Left.isSimpleTypeSpecifier()) &&
Manuel Klimek31e44f72013-09-04 08:20:47 +00001672 Right.is(tok::l_brace) && Right.getNextNonComment() &&
1673 Right.BlockKind != BK_Block)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001674 return false;
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001675 if (Left.is(tok::period) || Right.is(tok::period))
1676 return false;
Alexander Kornienkodaa07e92013-09-10 13:41:43 +00001677 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
1678 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001679 if (Left.Type == TT_TemplateCloser && Left.MatchingParen &&
1680 Left.MatchingParen->Previous &&
1681 Left.MatchingParen->Previous->is(tok::period))
1682 // A.<B>DoSomething();
1683 return false;
1684 if (Left.Type == TT_TemplateCloser && Right.is(tok::l_square))
1685 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001686 return true;
1687}
1688
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001689bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Stephen Hines176edba2014-12-01 14:53:08 -08001690 const FormatToken &Right) {
1691 const FormatToken &Left = *Right.Previous;
1692 if (Style.Language == FormatStyle::LK_Proto) {
1693 if (Right.is(tok::period) &&
1694 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
1695 Keywords.kw_repeated))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001696 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001697 if (Right.is(tok::l_paren) &&
1698 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option))
1699 return true;
1700 } else if (Style.Language == FormatStyle::LK_JavaScript) {
1701 if (Left.is(Keywords.kw_var))
1702 return true;
1703 } else if (Style.Language == FormatStyle::LK_Java) {
1704 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
1705 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never;
1706 if (Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
1707 tok::kw_protected) &&
1708 Right.Type == TT_TemplateOpener)
1709 return true;
1710 }
1711 if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
1712 return true; // Never ever merge two identifiers.
1713 if (Left.Type == TT_ImplicitStringLiteral)
1714 return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
1715 if (Line.Type == LT_ObjCMethodDecl) {
1716 if (Left.Type == TT_ObjCMethodSpecifier)
1717 return true;
1718 if (Left.is(tok::r_paren) && Right.is(tok::identifier))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001719 // Don't space between ')' and <id>
1720 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001721 }
1722 if (Line.Type == LT_ObjCProperty &&
Stephen Hines176edba2014-12-01 14:53:08 -08001723 (Right.is(tok::equal) || Left.is(tok::equal)))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001724 return false;
1725
Stephen Hines176edba2014-12-01 14:53:08 -08001726 if (Right.Type == TT_TrailingReturnArrow ||
1727 Left.Type == TT_TrailingReturnArrow)
Daniel Jasper2ca37412013-07-09 14:36:48 +00001728 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001729 if (Left.is(tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001730 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001731 if (Right.is(tok::comma))
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001732 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001733 if (Right.Type == TT_CtorInitializerColon || Right.Type == TT_ObjCBlockLParen)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001734 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001735 if (Left.is(tok::kw_operator))
1736 return Right.is(tok::coloncolon);
1737 if (Right.Type == TT_OverloadedOperatorLParen)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001738 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001739 if (Right.is(tok::colon))
Manuel Klimekb3987012013-05-29 14:47:47 +00001740 return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Stephen Hines176edba2014-12-01 14:53:08 -08001741 Right.getNextNonComment() && Right.Type != TT_ObjCMethodExpr &&
1742 !Left.is(tok::question) &&
1743 !(Right.Type == TT_InlineASMColon && Left.is(tok::coloncolon)) &&
1744 (Right.Type != TT_DictLiteral || Style.SpacesInContainerLiterals);
1745 if (Left.Type == TT_UnaryOperator)
1746 return Right.Type == TT_BinaryOperator;
1747 if (Left.Type == TT_CastRParen)
1748 return Style.SpaceAfterCStyleCast || Right.Type == TT_BinaryOperator;
1749 if (Left.is(tok::greater) && Right.is(tok::greater)) {
1750 return Right.Type == TT_TemplateCloser && Left.Type == TT_TemplateCloser &&
Daniel Jasperd8ee5c12013-10-29 14:52:02 +00001751 (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001752 }
Stephen Hines176edba2014-12-01 14:53:08 -08001753 if (Right.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
1754 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar))
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001755 return false;
Daniel Jasper9b4de852013-09-25 15:15:02 +00001756 if (!Style.SpaceBeforeAssignmentOperators &&
Stephen Hines176edba2014-12-01 14:53:08 -08001757 Right.getPrecedence() == prec::Assignment)
Daniel Jasper9b4de852013-09-25 15:15:02 +00001758 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001759 if (Right.is(tok::coloncolon) && Left.isNot(tok::l_brace))
1760 return (Left.Type == TT_TemplateOpener &&
1761 Style.Standard == FormatStyle::LS_Cpp03) ||
1762 !(Left.isOneOf(tok::identifier, tok::l_paren, tok::r_paren) ||
1763 Left.Type == TT_TemplateCloser || Left.Type == TT_TemplateOpener);
1764 if ((Left.Type == TT_TemplateOpener) != (Right.Type == TT_TemplateCloser))
1765 return Style.SpacesInAngles;
1766 if ((Right.Type == TT_BinaryOperator && !Left.is(tok::l_paren)) ||
1767 Left.Type == TT_BinaryOperator || Left.Type == TT_ConditionalExpr)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001768 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001769 if (Left.Type == TT_TemplateCloser && Right.is(tok::l_paren))
1770 return Style.SpaceBeforeParens == FormatStyle::SBPO_Always;
1771 if (Right.Type == TT_TemplateOpener && Left.is(tok::r_paren) &&
1772 Left.MatchingParen &&
1773 Left.MatchingParen->Type == TT_OverloadedOperatorLParen)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001774 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001775 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
Daniel Jaspera4dd9822013-08-28 08:24:04 +00001776 Line.First->is(tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001777 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001778 if (Right.Type == TT_TrailingUnaryOperator)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001779 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001780 if (Left.Type == TT_RegexLiteral)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001781 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001782 return spaceRequiredBetween(Line, Left, Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001783}
1784
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001785// Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
1786static bool isAllmanBrace(const FormatToken &Tok) {
1787 return Tok.is(tok::l_brace) && Tok.BlockKind == BK_Block &&
1788 Tok.Type != TT_ObjCBlockLBrace && Tok.Type != TT_DictLiteral;
1789}
1790
Daniel Jasperebaa1712013-09-17 09:52:48 +00001791bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
1792 const FormatToken &Right) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001793 const FormatToken &Left = *Right.Previous;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001794 if (Right.NewlinesBefore > 1)
1795 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001796
1797 // If the last token before a '}' is a comma or a trailing comment, the
1798 // intention is to insert a line break after it in order to make shuffling
1799 // around entries easier.
1800 const FormatToken *BeforeClosingBrace = nullptr;
1801 if (Left.is(tok::l_brace) && Left.BlockKind != BK_Block && Left.MatchingParen)
1802 BeforeClosingBrace = Left.MatchingParen->Previous;
1803 else if (Right.is(tok::r_brace) && Right.BlockKind != BK_Block)
1804 BeforeClosingBrace = &Left;
1805 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
1806 BeforeClosingBrace->isTrailingComment()))
1807 return true;
1808
Daniel Jasperebaa1712013-09-17 09:52:48 +00001809 if (Right.is(tok::comment)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001810 return Left.BlockKind != BK_BracedInit &&
1811 Left.Type != TT_CtorInitializerColon &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001812 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
Daniel Jasperebaa1712013-09-17 09:52:48 +00001813 } else if (Right.Previous->isTrailingComment() ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001814 (Right.isStringLiteral() && Right.Previous->isStringLiteral())) {
Daniel Jasperebaa1712013-09-17 09:52:48 +00001815 return true;
1816 } else if (Right.Previous->IsUnterminatedLiteral) {
1817 return true;
1818 } else if (Right.is(tok::lessless) && Right.Next &&
1819 Right.Previous->is(tok::string_literal) &&
1820 Right.Next->is(tok::string_literal)) {
1821 return true;
1822 } else if (Right.Previous->ClosesTemplateDeclaration &&
1823 Right.Previous->MatchingParen &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001824 Right.Previous->MatchingParen->NestingLevel == 0 &&
Daniel Jasperebaa1712013-09-17 09:52:48 +00001825 Style.AlwaysBreakTemplateDeclarations) {
Daniel Jasperebaa1712013-09-17 09:52:48 +00001826 return true;
Stephen Hines651f13c2014-04-23 16:59:28 -07001827 } else if ((Right.Type == TT_CtorInitializerComma ||
1828 Right.Type == TT_CtorInitializerColon) &&
Daniel Jasper19ccb122013-10-08 05:11:18 +00001829 Style.BreakConstructorInitializersBeforeComma &&
1830 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
Daniel Jasperebaa1712013-09-17 09:52:48 +00001831 return true;
Stephen Hines651f13c2014-04-23 16:59:28 -07001832 } else if (Right.is(tok::string_literal) &&
1833 Right.TokenText.startswith("R\"")) {
1834 // Raw string literals are special wrt. line breaks. The author has made a
1835 // deliberate choice and might have aligned the contents of the string
1836 // literal accordingly. Thus, we try keep existing line breaks.
1837 return Right.NewlinesBefore > 0;
1838 } else if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 &&
1839 Style.Language == FormatStyle::LK_Proto) {
Stephen Hines176edba2014-12-01 14:53:08 -08001840 // Don't put enums onto single lines in protocol buffers.
Stephen Hines651f13c2014-04-23 16:59:28 -07001841 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001842 } else if (Style.Language == FormatStyle::LK_JavaScript &&
1843 Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
1844 !Left.Children.empty()) {
1845 // Support AllowShortFunctionsOnASingleLine for JavaScript.
1846 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
1847 (Left.NestingLevel == 0 && Line.Level == 0 &&
1848 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001849 } else if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
1850 return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1851 Style.BreakBeforeBraces == FormatStyle::BS_GNU;
Stephen Hines176edba2014-12-01 14:53:08 -08001852 } else if (Style.Language == FormatStyle::LK_Proto &&
1853 Left.isNot(tok::l_brace) && Right.Type == TT_SelectorName) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001854 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001855 } else if (Left.Type == TT_ObjCBlockLBrace &&
1856 !Style.AllowShortBlocksOnASingleLine) {
1857 return true;
1858 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001859
1860 if (Style.Language == FormatStyle::LK_JavaScript) {
1861 // FIXME: This might apply to other languages and token kinds.
1862 if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
1863 Left.Previous->is(tok::char_constant))
1864 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001865 } else if (Style.Language == FormatStyle::LK_Java) {
1866 if (Left.Type == TT_LeadingJavaAnnotation && Right.isNot(tok::l_paren) &&
1867 Line.Last->is(tok::l_brace))
1868 return true;
1869 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
1870 Right.Next->is(tok::string_literal))
1871 return true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001872 }
1873
Daniel Jasperebaa1712013-09-17 09:52:48 +00001874 return false;
1875}
1876
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001877bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001878 const FormatToken &Right) {
1879 const FormatToken &Left = *Right.Previous;
Stephen Hines176edba2014-12-01 14:53:08 -08001880
1881 if (Style.Language == FormatStyle::LK_Java) {
1882 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
1883 Keywords.kw_implements))
1884 return false;
1885 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
1886 Keywords.kw_implements))
1887 return true;
1888 }
1889
Stephen Hines651f13c2014-04-23 16:59:28 -07001890 if (Left.is(tok::at))
1891 return false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001892 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
1893 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001894 if (Left.Type == TT_JavaAnnotation || Left.Type == TT_LeadingJavaAnnotation)
1895 return true;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001896 if (Right.Type == TT_StartOfName ||
1897 Right.Type == TT_FunctionDeclarationName || Right.is(tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001898 return true;
Daniel Jasper1a896a52013-11-08 00:57:11 +00001899 if (Right.isTrailingComment())
1900 // We rely on MustBreakBefore being set correctly here as we should not
1901 // change the "binding" behavior of a comment.
Stephen Hines651f13c2014-04-23 16:59:28 -07001902 // The first comment in a braced lists is always interpreted as belonging to
1903 // the first list element. Otherwise, it should be placed outside of the
1904 // list.
1905 return Left.BlockKind == BK_BracedInit;
Daniel Jasper1a896a52013-11-08 00:57:11 +00001906 if (Left.is(tok::question) && Right.is(tok::colon))
1907 return false;
1908 if (Right.Type == TT_ConditionalExpr || Right.is(tok::question))
1909 return Style.BreakBeforeTernaryOperators;
1910 if (Left.Type == TT_ConditionalExpr || Left.is(tok::question))
1911 return !Style.BreakBeforeTernaryOperators;
Stephen Hines651f13c2014-04-23 16:59:28 -07001912 if (Right.Type == TT_InheritanceColon)
1913 return true;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001914 if (Right.is(tok::colon) && (Right.Type != TT_CtorInitializerColon &&
1915 Right.Type != TT_InlineASMColon))
1916 return false;
Nico Weberf2ff8122013-05-26 05:39:26 +00001917 if (Left.is(tok::colon) &&
Daniel Jasper3c6aea72013-10-24 10:31:50 +00001918 (Left.Type == TT_DictLiteral || Left.Type == TT_ObjCMethodExpr))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001919 return true;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001920 if (Right.Type == TT_SelectorName)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001921 return true;
Daniel Jasperaa9e7b12013-08-01 13:46:58 +00001922 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
1923 return true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001924 if (Left.ClosesTemplateDeclaration)
1925 return true;
Daniel Jasper6cabab42013-02-14 08:42:54 +00001926 if (Right.Type == TT_RangeBasedForLoopColon ||
Daniel Jasperc476ea92013-08-28 07:27:35 +00001927 Right.Type == TT_OverloadedOperatorLParen ||
1928 Right.Type == TT_OverloadedOperator)
Daniel Jasper6cabab42013-02-14 08:42:54 +00001929 return false;
Daniel Jasperc194c952013-05-06 06:45:09 +00001930 if (Left.Type == TT_RangeBasedForLoopColon)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001931 return true;
Daniel Jasper7d812812013-02-21 15:00:29 +00001932 if (Right.Type == TT_RangeBasedForLoopColon)
1933 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001934 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
Daniel Jasper1a896a52013-11-08 00:57:11 +00001935 Left.Type == TT_UnaryOperator || Left.is(tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001936 return false;
1937 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
1938 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001939 if (Left.is(tok::l_paren) && Left.Type == TT_AttributeParen)
1940 return false;
1941 if (Left.is(tok::l_paren) && Left.Previous &&
1942 (Left.Previous->Type == TT_BinaryOperator ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001943 Left.Previous->Type == TT_CastRParen || Left.Previous->is(tok::kw_if)))
Stephen Hines651f13c2014-04-23 16:59:28 -07001944 return false;
Daniel Jasper84379572013-10-30 13:54:53 +00001945 if (Right.Type == TT_ImplicitStringLiteral)
1946 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001947
Daniel Jasper567dcf92013-09-05 09:29:45 +00001948 if (Right.is(tok::r_paren) || Right.Type == TT_TemplateCloser)
1949 return false;
1950
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001951 // We only break before r_brace if there was a corresponding break before
1952 // the l_brace, which is tracked by BreakBeforeClosingBrace.
Daniel Jasper567dcf92013-09-05 09:29:45 +00001953 if (Right.is(tok::r_brace))
1954 return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001955
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001956 // Allow breaking after a trailing annotation, e.g. after a method
1957 // declaration.
1958 if (Left.Type == TT_TrailingAnnotation)
1959 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
1960 tok::less, tok::coloncolon);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001961
Daniel Jasper8ef19a22013-03-14 09:50:46 +00001962 if (Right.is(tok::kw___attribute))
1963 return true;
1964
Daniel Jasper3a204412013-02-23 07:46:38 +00001965 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
1966 return true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001967
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001968 if (Right.is(tok::identifier) && Right.Next &&
1969 Right.Next->Type == TT_DictLiteral)
1970 return true;
1971
Daniel Jaspere8b10d32013-07-26 16:56:36 +00001972 if (Left.Type == TT_CtorInitializerComma &&
1973 Style.BreakConstructorInitializersBeforeComma)
1974 return false;
Daniel Jasper19ccb122013-10-08 05:11:18 +00001975 if (Right.Type == TT_CtorInitializerComma &&
1976 Style.BreakConstructorInitializersBeforeComma)
1977 return true;
Daniel Jasper26356cc2013-09-17 08:15:46 +00001978 if (Left.is(tok::greater) && Right.is(tok::greater) &&
1979 Left.Type != TT_TemplateCloser)
1980 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001981 if (Right.Type == TT_BinaryOperator &&
1982 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
1983 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
1984 Right.getPrecedence() != prec::Assignment))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001985 return true;
Daniel Jaspera07aa662013-10-22 15:30:28 +00001986 if (Left.Type == TT_ArrayInitializerLSquare)
1987 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001988 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
1989 return true;
1990 if (Left.isBinaryOperator() && !Left.isOneOf(tok::arrowstar, tok::lessless) &&
1991 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
1992 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
1993 Left.getPrecedence() == prec::Assignment))
1994 return true;
1995 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
Daniel Jasper6b119d62013-04-05 17:22:09 +00001996 tok::kw_class, tok::kw_struct) ||
Stephen Hines176edba2014-12-01 14:53:08 -08001997 Right.isMemberAccess() || Right.Type == TT_TrailingReturnArrow ||
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001998 Right.isOneOf(tok::lessless, tok::colon, tok::l_square, tok::at) ||
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001999 (Left.is(tok::r_paren) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002000 Right.isOneOf(tok::identifier, tok::kw_const)) ||
Daniel Jaspera07aa662013-10-22 15:30:28 +00002001 (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00002002}
2003
Daniel Jasperbf71ba22013-04-08 20:33:42 +00002004void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
2005 llvm::errs() << "AnnotatedTokens:\n";
Manuel Klimekb3987012013-05-29 14:47:47 +00002006 const FormatToken *Tok = Line.First;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00002007 while (Tok) {
Manuel Klimekb3987012013-05-29 14:47:47 +00002008 llvm::errs() << " M=" << Tok->MustBreakBefore
Daniel Jasperc7bd68f2013-07-10 14:02:49 +00002009 << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
2010 << " S=" << Tok->SpacesRequiredBefore
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002011 << " B=" << Tok->BlockParameterCount
Daniel Jasperc7bd68f2013-07-10 14:02:49 +00002012 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
Manuel Klimekae76f7f2013-10-11 21:25:45 +00002013 << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
2014 << " FakeLParens=";
Daniel Jasperbf71ba22013-04-08 20:33:42 +00002015 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
2016 llvm::errs() << Tok->FakeLParens[i] << "/";
2017 llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002018 if (!Tok->Next)
Manuel Klimekae76f7f2013-10-11 21:25:45 +00002019 assert(Tok == Line.Last);
Manuel Klimekb3987012013-05-29 14:47:47 +00002020 Tok = Tok->Next;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00002021 }
2022 llvm::errs() << "----\n";
2023}
2024
Daniel Jasper32d28ee2013-01-29 21:01:14 +00002025} // namespace format
2026} // namespace clang