blob: 5b148eab6b13b00c42aa6a804b6ae038f0303875 [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)
Stephen Hines0e2c34f2015-03-23 12:09:02 -070036 : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
37 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 Hines0e2c34f2015-03-23 12:09:02 -070081 CurrentToken->Previous->is(TT_BinaryOperator) &&
Stephen Hines176edba2014-12-01 14:53:08 -080082 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) ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700117 Left->Previous->is(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 ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700122 !Left->Previous->isOneOf(tok::identifier,
123 TT_OverloadedOperator))) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700124 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 &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700127 Left->Previous->MatchingParen->is(TT_LambdaLSquare)) {
Daniel Jasper363193b2013-10-20 18:15:30 +0000128 // 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 &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700140 Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
Stephen Hines176edba2014-12-01 14:53:08 -0800141 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;
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700152 bool MightBeObjCForRangeLoop =
153 Left->Previous && Left->Previous->is(tok::kw_for);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700154 while (CurrentToken) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000155 // LookForDecls is set when "if (" has been seen. Check for
156 // 'identifier' '*' 'identifier' followed by not '=' -- this
157 // '*' has to be a binary operator but determineStarAmpUsage() will
158 // categorize it as an unary operator, so set the right type here.
Manuel Klimekb3987012013-05-29 14:47:47 +0000159 if (LookForDecls && CurrentToken->Next) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000160 FormatToken *Prev = CurrentToken->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000161 if (Prev) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000162 FormatToken *PrevPrev = Prev->getPreviousNonComment();
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000163 FormatToken *Next = CurrentToken->Next;
164 if (PrevPrev && PrevPrev->is(tok::identifier) &&
165 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
166 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
167 Prev->Type = TT_BinaryOperator;
168 LookForDecls = false;
169 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000170 }
171 }
172
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700173 if (CurrentToken->Previous->is(TT_PointerOrReference) &&
Daniel Jasper53352602013-08-12 12:16:34 +0000174 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
175 tok::coloncolon))
176 MightBeFunctionType = true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700177 if (CurrentToken->Previous->is(TT_BinaryOperator))
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700178 Contexts.back().IsExpression = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000179 if (CurrentToken->is(tok::r_paren)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000180 if (MightBeFunctionType && CurrentToken->Next &&
Daniel Jaspere7d3bff2013-07-16 11:37:21 +0000181 (CurrentToken->Next->is(tok::l_paren) ||
182 (CurrentToken->Next->is(tok::l_square) &&
183 !Contexts.back().IsExpression)))
Daniel Jasper431f5912013-05-28 08:33:00 +0000184 Left->Type = TT_FunctionTypeLParen;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000185 Left->MatchingParen = CurrentToken;
186 CurrentToken->MatchingParen = Left;
187
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000188 if (StartsObjCMethodExpr) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000189 CurrentToken->Type = TT_ObjCMethodExpr;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700190 if (Contexts.back().FirstObjCSelectorName) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000191 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
192 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000193 }
194 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000195
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700196 if (Left->is(TT_AttributeParen))
Stephen Hines651f13c2014-04-23 16:59:28 -0700197 CurrentToken->Type = TT_AttributeParen;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700198 if (Left->Previous && Left->Previous->is(TT_JavaAnnotation))
Stephen Hines176edba2014-12-01 14:53:08 -0800199 CurrentToken->Type = TT_JavaAnnotation;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700200 if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation))
Stephen Hines176edba2014-12-01 14:53:08 -0800201 CurrentToken->Type = TT_LeadingJavaAnnotation;
Stephen Hines651f13c2014-04-23 16:59:28 -0700202
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000203 if (!HasMultipleLines)
204 Left->PackingKind = PPK_Inconclusive;
205 else if (HasMultipleParametersOnALine)
206 Left->PackingKind = PPK_BinPacked;
207 else
208 Left->PackingKind = PPK_OnePerLine;
209
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000210 next();
211 return true;
212 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000213 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000214 return false;
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700215
216 if (CurrentToken->is(tok::l_brace))
Stephen Hines651f13c2014-04-23 16:59:28 -0700217 Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000218 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
219 !CurrentToken->Next->HasUnescapedNewline &&
220 !CurrentToken->Next->isTrailingComment())
221 HasMultipleParametersOnALine = true;
Stephen Hines651f13c2014-04-23 16:59:28 -0700222 if (CurrentToken->isOneOf(tok::kw_const, tok::kw_auto) ||
223 CurrentToken->isSimpleTypeSpecifier())
224 Contexts.back().IsExpression = false;
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700225 if (CurrentToken->isOneOf(tok::semi, tok::colon))
226 MightBeObjCForRangeLoop = false;
227 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in))
228 CurrentToken->Type = TT_ObjCForIn;
229
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700230 FormatToken *Tok = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000231 if (!consumeToken())
232 return false;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700233 updateParameterCount(Left, Tok);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000234 if (CurrentToken && CurrentToken->HasUnescapedNewline)
235 HasMultipleLines = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000236 }
237 return false;
238 }
239
240 bool parseSquare() {
241 if (!CurrentToken)
242 return false;
243
Alexander Kornienkod71b15b2013-06-17 13:19:53 +0000244 // A '[' could be an index subscript (after an identifier or after
Nico Weber051860e2013-02-10 02:08:05 +0000245 // ')' or ']'), it could be the start of an Objective-C method
246 // expression, or it could the the start of an Objective-C array literal.
Manuel Klimekb3987012013-05-29 14:47:47 +0000247 FormatToken *Left = CurrentToken->Previous;
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000248 FormatToken *Parent = Left->getPreviousNonComment();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000249 bool StartsObjCMethodExpr =
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700250 Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700251 CurrentToken->isNot(tok::l_brace) &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700252 (!Parent ||
253 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
254 tok::kw_return, tok::kw_throw) ||
255 Parent->isUnaryOperator() ||
256 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
Manuel Klimekb3987012013-05-29 14:47:47 +0000257 getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
Daniel Jasper923ebef2013-03-14 13:45:21 +0000258 ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
Daniel Jasper6f21a982013-03-13 07:49:51 +0000259 Contexts.back().IsExpression = true;
Daniel Jasper8f54d882013-10-26 17:00:22 +0000260 bool ColonFound = false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000261
Daniel Jasper4e778092013-02-06 10:05:46 +0000262 if (StartsObjCMethodExpr) {
263 Contexts.back().ColonIsObjCMethodExpr = true;
264 Left->Type = TT_ObjCMethodExpr;
Daniel Jaspera07aa662013-10-22 15:30:28 +0000265 } else if (Parent && Parent->is(tok::at)) {
266 Left->Type = TT_ArrayInitializerLSquare;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700267 } else if (Left->is(TT_Unknown)) {
Daniel Jaspera07aa662013-10-22 15:30:28 +0000268 Left->Type = TT_ArraySubscriptLSquare;
Daniel Jasper4e778092013-02-06 10:05:46 +0000269 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000270
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700271 while (CurrentToken) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000272 if (CurrentToken->is(tok::r_square)) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000273 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700274 Left->is(TT_ObjCMethodExpr)) {
Nico Webere8a97982013-02-06 06:20:11 +0000275 // An ObjC method call is rarely followed by an open parenthesis.
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000276 // FIXME: Do we incorrectly label ":" with this?
277 StartsObjCMethodExpr = false;
278 Left->Type = TT_Unknown;
279 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700280 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000281 CurrentToken->Type = TT_ObjCMethodExpr;
Nico Webere8a97982013-02-06 06:20:11 +0000282 // determineStarAmpUsage() thinks that '*' '[' is allocating an
283 // array of pointers, but if '[' starts a selector then '*' is a
284 // binary operator.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700285 if (Parent && Parent->is(TT_PointerOrReference))
Nico Weber4ed7f3e2013-02-06 16:54:35 +0000286 Parent->Type = TT_BinaryOperator;
Daniel Jasper01786732013-02-04 07:21:18 +0000287 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000288 Left->MatchingParen = CurrentToken;
289 CurrentToken->MatchingParen = Left;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700290 if (Contexts.back().FirstObjCSelectorName) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000291 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
292 Contexts.back().LongestObjCSelectorName;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700293 if (Left->BlockParameterCount > 1)
Stephen Hines651f13c2014-04-23 16:59:28 -0700294 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
295 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000296 next();
297 return true;
298 }
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000299 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000300 return false;
Stephen Hines176edba2014-12-01 14:53:08 -0800301 if (CurrentToken->is(tok::colon)) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700302 if (Left->is(TT_ArraySubscriptLSquare)) {
Stephen Hines176edba2014-12-01 14:53:08 -0800303 Left->Type = TT_ObjCMethodExpr;
304 StartsObjCMethodExpr = true;
305 Contexts.back().ColonIsObjCMethodExpr = true;
306 if (Parent && Parent->is(tok::r_paren))
307 Parent->Type = TT_CastRParen;
308 }
Daniel Jasper8f54d882013-10-26 17:00:22 +0000309 ColonFound = true;
Stephen Hines176edba2014-12-01 14:53:08 -0800310 }
Daniel Jaspera07aa662013-10-22 15:30:28 +0000311 if (CurrentToken->is(tok::comma) &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700312 Style.Language != FormatStyle::LK_Proto &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700313 (Left->is(TT_ArraySubscriptLSquare) ||
314 (Left->is(TT_ObjCMethodExpr) && !ColonFound)))
Daniel Jaspera07aa662013-10-22 15:30:28 +0000315 Left->Type = TT_ArrayInitializerLSquare;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700316 FormatToken *Tok = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000317 if (!consumeToken())
318 return false;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700319 updateParameterCount(Left, Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000320 }
321 return false;
322 }
323
324 bool parseBrace() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700325 if (CurrentToken) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000326 FormatToken *Left = CurrentToken->Previous;
Stephen Hines651f13c2014-04-23 16:59:28 -0700327
328 if (Contexts.back().CaretFound)
329 Left->Type = TT_ObjCBlockLBrace;
330 Contexts.back().CaretFound = false;
331
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000332 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
333 Contexts.back().ColonIsDictLiteral = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700334 if (Left->BlockKind == BK_BracedInit)
335 Contexts.back().IsExpression = true;
Nico Weberf2ff8122013-05-26 05:39:26 +0000336
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700337 while (CurrentToken) {
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000338 if (CurrentToken->is(tok::r_brace)) {
339 Left->MatchingParen = CurrentToken;
340 CurrentToken->MatchingParen = Left;
341 next();
342 return true;
343 }
344 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
345 return false;
346 updateParameterCount(Left, CurrentToken);
Stephen Hines176edba2014-12-01 14:53:08 -0800347 if (CurrentToken->isOneOf(tok::colon, tok::l_brace)) {
348 FormatToken *Previous = CurrentToken->getPreviousNonComment();
349 if ((CurrentToken->is(tok::colon) ||
350 Style.Language == FormatStyle::LK_Proto) &&
351 Previous->is(tok::identifier))
352 Previous->Type = TT_SelectorName;
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700353 if (CurrentToken->is(tok::colon) ||
354 Style.Language == FormatStyle::LK_JavaScript)
Stephen Hines176edba2014-12-01 14:53:08 -0800355 Left->Type = TT_DictLiteral;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700356 }
Daniel Jasper53e72cd2013-05-06 08:27:33 +0000357 if (!consumeToken())
358 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000359 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000360 }
361 return true;
362 }
Daniel Jasperc4615b72013-02-20 12:56:39 +0000363
Manuel Klimekb3987012013-05-29 14:47:47 +0000364 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700365 if (Current->is(TT_LambdaLSquare) ||
366 (Current->is(tok::caret) && Current->is(TT_UnaryOperator)) ||
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700367 (Style.Language == FormatStyle::LK_JavaScript &&
Stephen Hines176edba2014-12-01 14:53:08 -0800368 Current->is(Keywords.kw_function))) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700369 ++Left->BlockParameterCount;
370 }
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000371 if (Current->is(tok::comma)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000372 ++Left->ParameterCount;
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000373 if (!Left->Role)
374 Left->Role.reset(new CommaSeparatedList(Style));
375 Left->Role->CommaFound(Current);
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000376 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000377 Left->ParameterCount = 1;
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000378 }
Daniel Jasper9fc56f22013-02-14 15:01:34 +0000379 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000380
381 bool parseConditional() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700382 while (CurrentToken) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000383 if (CurrentToken->is(tok::colon)) {
384 CurrentToken->Type = TT_ConditionalExpr;
385 next();
386 return true;
387 }
388 if (!consumeToken())
389 return false;
390 }
391 return false;
392 }
393
394 bool parseTemplateDeclaration() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700395 if (CurrentToken && CurrentToken->is(tok::less)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000396 CurrentToken->Type = TT_TemplateOpener;
397 next();
398 if (!parseAngle())
399 return false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700400 if (CurrentToken)
Manuel Klimekb3987012013-05-29 14:47:47 +0000401 CurrentToken->Previous->ClosesTemplateDeclaration = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000402 return true;
403 }
404 return false;
405 }
406
407 bool consumeToken() {
Manuel Klimekb3987012013-05-29 14:47:47 +0000408 FormatToken *Tok = CurrentToken;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000409 next();
Manuel Klimekb3987012013-05-29 14:47:47 +0000410 switch (Tok->Tok.getKind()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000411 case tok::plus:
412 case tok::minus:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700413 if (!Tok->Previous && Line.MustBeDeclaration)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000414 Tok->Type = TT_ObjCMethodSpecifier;
415 break;
416 case tok::colon:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700417 if (!Tok->Previous)
Daniel Jaspercf6d76a2013-03-18 12:50:26 +0000418 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000419 // Colons from ?: are handled in parseConditional().
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700420 if (Style.Language == FormatStyle::LK_JavaScript) {
421 if (Contexts.back().ColonIsForRangeExpr ||
422 (Contexts.size() == 1 &&
423 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
424 Contexts.back().ContextKind == tok::l_paren ||
425 Contexts.back().ContextKind == tok::l_square) {
426 Tok->Type = TT_JsTypeColon;
427 break;
428 }
429 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700430 if (Contexts.back().ColonIsDictLiteral) {
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000431 Tok->Type = TT_DictLiteral;
Daniel Jasper4e778092013-02-06 10:05:46 +0000432 } else if (Contexts.back().ColonIsObjCMethodExpr ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700433 Line.First->is(TT_ObjCMethodSpecifier)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000434 Tok->Type = TT_ObjCMethodExpr;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700435 Tok->Previous->Type = TT_SelectorName;
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000436 if (Tok->Previous->ColumnWidth >
Alexander Kornienko00895102013-06-05 14:09:10 +0000437 Contexts.back().LongestObjCSelectorName) {
Alexander Kornienko01fe9f92013-10-10 13:36:20 +0000438 Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
Alexander Kornienko00895102013-06-05 14:09:10 +0000439 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700440 if (!Contexts.back().FirstObjCSelectorName)
Manuel Klimekb3987012013-05-29 14:47:47 +0000441 Contexts.back().FirstObjCSelectorName = Tok->Previous;
Daniel Jasper4e778092013-02-06 10:05:46 +0000442 } else if (Contexts.back().ColonIsForRangeExpr) {
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700443 Tok->Type = TT_RangeBasedForLoopColon;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700444 } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
Alexander Kornienko01fe9f92013-10-10 13:36:20 +0000445 Tok->Type = TT_BitFieldColon;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700446 } else if (Contexts.size() == 1 &&
447 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700448 if (Tok->Previous->is(tok::r_paren))
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700449 Tok->Type = TT_CtorInitializerColon;
450 else
451 Tok->Type = TT_InheritanceColon;
Stephen Hines176edba2014-12-01 14:53:08 -0800452 } else if (Tok->Previous->is(tok::identifier) && Tok->Next &&
453 Tok->Next->isOneOf(tok::r_paren, tok::comma)) {
454 // This handles a special macro in ObjC code where selectors including
455 // the colon are passed as macro arguments.
456 Tok->Type = TT_ObjCMethodExpr;
Daniel Jasper923ebef2013-03-14 13:45:21 +0000457 } else if (Contexts.back().ContextKind == tok::l_paren) {
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700458 Tok->Type = TT_InlineASMColon;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000459 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000460 break;
461 case tok::kw_if:
462 case tok::kw_while:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700463 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000464 next();
Nico Weber27268772013-06-26 00:30:14 +0000465 if (!parseParens(/*LookForDecls=*/true))
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000466 return false;
467 }
468 break;
469 case tok::kw_for:
Daniel Jasper4e778092013-02-06 10:05:46 +0000470 Contexts.back().ColonIsForRangeExpr = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000471 next();
472 if (!parseParens())
473 return false;
474 break;
475 case tok::l_paren:
476 if (!parseParens())
477 return false;
Daniel Jasper59875ac2013-11-07 17:43:07 +0000478 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700479 !Contexts.back().IsExpression && Line.First->isNot(TT_ObjCProperty) &&
480 (!Tok->Previous ||
481 !Tok->Previous->isOneOf(tok::kw_decltype, TT_LeadingJavaAnnotation)))
Daniel Jasper3c08a812013-02-24 18:54:32 +0000482 Line.MightBeFunctionDecl = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000483 break;
484 case tok::l_square:
485 if (!parseSquare())
486 return false;
487 break;
488 case tok::l_brace:
489 if (!parseBrace())
490 return false;
491 break;
492 case tok::less:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700493 if ((!Tok->Previous ||
494 (!Tok->Previous->Tok.isLiteral() &&
495 !(Tok->Previous->is(tok::r_paren) && Contexts.size() > 1))) &&
496 parseAngle()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000497 Tok->Type = TT_TemplateOpener;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700498 } else {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000499 Tok->Type = TT_BinaryOperator;
500 CurrentToken = Tok;
501 next();
502 }
503 break;
504 case tok::r_paren:
505 case tok::r_square:
506 return false;
507 case tok::r_brace:
508 // Lines can start with '}'.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700509 if (Tok->Previous)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000510 return false;
511 break;
512 case tok::greater:
513 Tok->Type = TT_BinaryOperator;
514 break;
515 case tok::kw_operator:
Daniel Jasper174f60f2013-09-02 09:20:39 +0000516 while (CurrentToken &&
517 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000518 if (CurrentToken->isOneOf(tok::star, tok::amp))
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000519 CurrentToken->Type = TT_PointerOrReference;
520 consumeToken();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700521 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
Daniel Jasperc476ea92013-08-28 07:27:35 +0000522 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000523 }
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000524 if (CurrentToken) {
Daniel Jasper2b4c9242013-02-11 08:01:18 +0000525 CurrentToken->Type = TT_OverloadedOperatorLParen;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700526 if (CurrentToken->Previous->is(TT_BinaryOperator))
Manuel Klimekb3987012013-05-29 14:47:47 +0000527 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper6ea933c2013-05-10 07:59:58 +0000528 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000529 break;
530 case tok::question:
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700531 if (Style.Language == FormatStyle::LK_JavaScript && Tok->Next &&
532 Tok->Next->isOneOf(tok::colon, tok::semi, tok::r_paren,
533 tok::r_brace)) {
534 // Question marks before semicolons, colons, commas, etc. indicate
535 // optional types (fields, parameters), e.g.
536 // `function(x?: string, y?) {...}` or `class X {y?;}`
537 Tok->Type = TT_JsTypeOptionalQuestion;
538 break;
539 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000540 parseConditional();
541 break;
542 case tok::kw_template:
543 parseTemplateDeclaration();
544 break;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000545 case tok::comma:
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700546 if (Contexts.back().FirstStartOfName && Contexts.size() == 1) {
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000547 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700548 Line.IsMultiVariableDeclStmt = true;
549 }
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000550 if (Contexts.back().InCtorInitializer)
551 Tok->Type = TT_CtorInitializerComma;
Stephen Hines651f13c2014-04-23 16:59:28 -0700552 if (Contexts.back().IsForEachMacro)
553 Contexts.back().IsExpression = true;
Daniel Jasper8ed9f2b2013-04-03 13:36:17 +0000554 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000555 default:
556 break;
557 }
558 return true;
559 }
560
561 void parseIncludeDirective() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700562 if (CurrentToken && CurrentToken->is(tok::less)) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000563 next();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700564 while (CurrentToken) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000565 if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000566 CurrentToken->Type = TT_ImplicitStringLiteral;
567 next();
568 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000569 }
570 }
571
572 void parseWarningOrError() {
573 next();
574 // We still want to format the whitespace left of the first token of the
575 // warning or error.
576 next();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700577 while (CurrentToken) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000578 CurrentToken->Type = TT_ImplicitStringLiteral;
579 next();
580 }
581 }
582
Stephen Hines651f13c2014-04-23 16:59:28 -0700583 void parsePragma() {
584 next(); // Consume "pragma".
585 if (CurrentToken && CurrentToken->TokenText == "mark") {
586 next(); // Consume "mark".
587 next(); // Consume first token (so we fix leading whitespace).
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700588 while (CurrentToken) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700589 CurrentToken->Type = TT_ImplicitStringLiteral;
590 next();
591 }
592 }
593 }
594
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700595 LineType parsePreprocessorDirective() {
596 LineType Type = LT_PreprocessorDirective;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000597 next();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700598 if (!CurrentToken)
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700599 return Type;
Alexander Kornienkob18c2582013-10-11 21:43:05 +0000600 if (CurrentToken->Tok.is(tok::numeric_constant)) {
601 CurrentToken->SpacesRequiredBefore = 1;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700602 return Type;
Alexander Kornienkob18c2582013-10-11 21:43:05 +0000603 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000604 // Hashes in the middle of a line can lead to any strange token
605 // sequence.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700606 if (!CurrentToken->Tok.getIdentifierInfo())
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700607 return Type;
Manuel Klimekb3987012013-05-29 14:47:47 +0000608 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000609 case tok::pp_include:
610 case tok::pp_import:
Stephen Hines176edba2014-12-01 14:53:08 -0800611 next();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000612 parseIncludeDirective();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700613 Type = LT_ImportStatement;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000614 break;
615 case tok::pp_error:
616 case tok::pp_warning:
617 parseWarningOrError();
618 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700619 case tok::pp_pragma:
620 parsePragma();
621 break;
Daniel Jasperaae7bad2013-04-23 13:54:04 +0000622 case tok::pp_if:
623 case tok::pp_elif:
Stephen Hines651f13c2014-04-23 16:59:28 -0700624 Contexts.back().IsExpression = true;
Daniel Jasperaae7bad2013-04-23 13:54:04 +0000625 parseLine();
626 break;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000627 default:
628 break;
629 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700630 while (CurrentToken)
Daniel Jasper5b7e7b02013-02-05 09:34:14 +0000631 next();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700632 return Type;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000633 }
634
Nico Weber95e8e462013-02-12 16:17:07 +0000635public:
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000636 LineType parseLine() {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000637 if (CurrentToken->is(tok::hash)) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700638 return parsePreprocessorDirective();
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000639 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700640
641 // Directly allow to 'import <string-literal>' to support protocol buffer
642 // definitions (code.google.com/p/protobuf) or missing "#" (either way we
643 // should not break the line).
644 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700645 if ((Style.Language == FormatStyle::LK_Java &&
646 CurrentToken->is(Keywords.kw_package)) ||
647 (Info && Info->getPPKeywordID() == tok::pp_import &&
648 CurrentToken->Next &&
649 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
650 tok::kw_static))) {
Stephen Hines176edba2014-12-01 14:53:08 -0800651 next();
Stephen Hines651f13c2014-04-23 16:59:28 -0700652 parseIncludeDirective();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700653 return LT_ImportStatement;
Stephen Hines176edba2014-12-01 14:53:08 -0800654 }
655
656 // If this line starts and ends in '<' and '>', respectively, it is likely
657 // part of "#define <a/b.h>".
658 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
659 parseIncludeDirective();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700660 return LT_ImportStatement;
Stephen Hines176edba2014-12-01 14:53:08 -0800661 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700662
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700663 bool KeywordVirtualFound = false;
664 bool ImportStatement = false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700665 while (CurrentToken) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000666 if (CurrentToken->is(tok::kw_virtual))
667 KeywordVirtualFound = true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700668 if (IsImportStatement(*CurrentToken))
669 ImportStatement = true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000670 if (!consumeToken())
671 return LT_Invalid;
672 }
673 if (KeywordVirtualFound)
674 return LT_VirtualFunctionDecl;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700675 if (ImportStatement)
676 return LT_ImportStatement;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000677
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700678 if (Line.First->is(TT_ObjCMethodSpecifier)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700679 if (Contexts.back().FirstObjCSelectorName)
Daniel Jasper4e778092013-02-06 10:05:46 +0000680 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
681 Contexts.back().LongestObjCSelectorName;
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000682 return LT_ObjCMethodDecl;
683 }
684
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000685 return LT_Other;
686 }
687
Nico Weber95e8e462013-02-12 16:17:07 +0000688private:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700689 bool IsImportStatement(const FormatToken &Tok) {
690 // FIXME: Closure-library specific stuff should not be hard-coded but be
691 // configurable.
692 return Style.Language == FormatStyle::LK_JavaScript &&
693 Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
694 Tok.Next->Next && (Tok.Next->Next->TokenText == "module" ||
695 Tok.Next->Next->TokenText == "require" ||
696 Tok.Next->Next->TokenText == "provide") &&
697 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
698 }
699
Stephen Hines651f13c2014-04-23 16:59:28 -0700700 void resetTokenMetadata(FormatToken *Token) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700701 if (!Token)
702 return;
Stephen Hines651f13c2014-04-23 16:59:28 -0700703
704 // Reset token type in case we have already looked at it and then
705 // recovered from an error (e.g. failure to find the matching >).
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700706 if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_FunctionLBrace,
707 TT_ImplicitStringLiteral, TT_RegexLiteral,
708 TT_TrailingReturnArrow))
Stephen Hines651f13c2014-04-23 16:59:28 -0700709 CurrentToken->Type = TT_Unknown;
Stephen Hines176edba2014-12-01 14:53:08 -0800710 CurrentToken->Role.reset();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700711 CurrentToken->MatchingParen = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700712 CurrentToken->FakeLParens.clear();
713 CurrentToken->FakeRParens = 0;
714 }
715
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000716 void next() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700717 if (CurrentToken) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700718 CurrentToken->NestingLevel = Contexts.size() - 1;
Stephen Hines176edba2014-12-01 14:53:08 -0800719 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700720 modifyContext(*CurrentToken);
Stephen Hines176edba2014-12-01 14:53:08 -0800721 determineTokenType(*CurrentToken);
Manuel Klimekb3987012013-05-29 14:47:47 +0000722 CurrentToken = CurrentToken->Next;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700723 }
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000724
Stephen Hines651f13c2014-04-23 16:59:28 -0700725 resetTokenMetadata(CurrentToken);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000726 }
727
Daniel Jasper4e778092013-02-06 10:05:46 +0000728 /// \brief A struct to hold information valid in a specific context, e.g.
729 /// a pair of parenthesis.
730 struct Context {
Daniel Jasper923ebef2013-03-14 13:45:21 +0000731 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
732 bool IsExpression)
733 : ContextKind(ContextKind), BindingStrength(BindingStrength),
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700734 IsExpression(IsExpression) {}
Daniel Jasper01786732013-02-04 07:21:18 +0000735
Daniel Jasper923ebef2013-03-14 13:45:21 +0000736 tok::TokenKind ContextKind;
Daniel Jasper4e778092013-02-06 10:05:46 +0000737 unsigned BindingStrength;
Daniel Jasper4e778092013-02-06 10:05:46 +0000738 bool IsExpression;
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -0700739 unsigned LongestObjCSelectorName = 0;
740 bool ColonIsForRangeExpr = false;
741 bool ColonIsDictLiteral = false;
742 bool ColonIsObjCMethodExpr = false;
743 FormatToken *FirstObjCSelectorName = nullptr;
744 FormatToken *FirstStartOfName = nullptr;
745 bool CanBeExpression = true;
746 bool InTemplateArgument = false;
747 bool InCtorInitializer = false;
748 bool CaretFound = false;
749 bool IsForEachMacro = false;
Daniel Jasper4e778092013-02-06 10:05:46 +0000750 };
751
752 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
753 /// of each instance.
754 struct ScopedContextCreator {
755 AnnotatingParser &P;
756
Daniel Jasper923ebef2013-03-14 13:45:21 +0000757 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
758 unsigned Increase)
759 : P(P) {
Daniel Jasper2a409b62013-07-08 14:34:09 +0000760 P.Contexts.push_back(Context(ContextKind,
761 P.Contexts.back().BindingStrength + Increase,
762 P.Contexts.back().IsExpression));
Daniel Jasper4e778092013-02-06 10:05:46 +0000763 }
764
765 ~ScopedContextCreator() { P.Contexts.pop_back(); }
766 };
Daniel Jasper01786732013-02-04 07:21:18 +0000767
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700768 void modifyContext(const FormatToken &Current) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000769 if (Current.getPrecedence() == prec::Assignment &&
Daniel Jasper53352602013-08-12 12:16:34 +0000770 !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
Manuel Klimekb3987012013-05-29 14:47:47 +0000771 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000772 Contexts.back().IsExpression = true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700773 if (!Line.First->is(TT_UnaryOperator)) {
774 for (FormatToken *Previous = Current.Previous;
775 Previous && !Previous->isOneOf(tok::comma, tok::semi);
776 Previous = Previous->Previous) {
777 if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
778 Previous = Previous->MatchingParen;
779 if (!Previous)
780 break;
781 }
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700782 if (Previous->opensScope())
783 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700784 if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
785 Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
786 Previous->Previous && Previous->Previous->isNot(tok::equal))
787 Previous->Type = TT_PointerOrReference;
Daniel Jasper01786732013-02-04 07:21:18 +0000788 }
Daniel Jasper01786732013-02-04 07:21:18 +0000789 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700790 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
Daniel Jasper4e778092013-02-06 10:05:46 +0000791 Contexts.back().IsExpression = true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700792 } else if (Current.is(TT_TrailingReturnArrow)) {
793 Contexts.back().IsExpression = false;
Stephen Hines651f13c2014-04-23 16:59:28 -0700794 } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700795 !Line.InPPDirective &&
796 (!Current.Previous ||
797 Current.Previous->isNot(tok::kw_decltype))) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700798 bool ParametersOfFunctionType =
799 Current.Previous && Current.Previous->is(tok::r_paren) &&
800 Current.Previous->MatchingParen &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700801 Current.Previous->MatchingParen->is(TT_FunctionTypeLParen);
Stephen Hines651f13c2014-04-23 16:59:28 -0700802 bool IsForOrCatch = Current.Previous &&
803 Current.Previous->isOneOf(tok::kw_for, tok::kw_catch);
804 Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000805 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
Manuel Klimekb3987012013-05-29 14:47:47 +0000806 for (FormatToken *Previous = Current.Previous;
Alexander Kornienkoe74de282013-03-13 14:41:29 +0000807 Previous && Previous->isOneOf(tok::star, tok::amp);
Manuel Klimekb3987012013-05-29 14:47:47 +0000808 Previous = Previous->Previous)
Nico Weber95e8e462013-02-12 16:17:07 +0000809 Previous->Type = TT_PointerOrReference;
Stephen Hines176edba2014-12-01 14:53:08 -0800810 if (Line.MustBeDeclaration)
811 Contexts.back().IsExpression = Contexts.front().InCtorInitializer;
Manuel Klimekb3987012013-05-29 14:47:47 +0000812 } else if (Current.Previous &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700813 Current.Previous->is(TT_CtorInitializerColon)) {
Daniel Jasperd0f349b2013-02-18 12:44:35 +0000814 Contexts.back().IsExpression = true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +0000815 Contexts.back().InCtorInitializer = true;
Daniel Jasper6f21a982013-03-13 07:49:51 +0000816 } else if (Current.is(tok::kw_new)) {
817 Contexts.back().CanBeExpression = false;
Daniel Jasperf9504aa2013-11-07 19:56:07 +0000818 } else if (Current.is(tok::semi) || Current.is(tok::exclaim)) {
Daniel Jasper16a69ef2013-05-03 14:41:24 +0000819 // This should be the condition or increment in a for-loop.
820 Contexts.back().IsExpression = true;
Nico Weber95e8e462013-02-12 16:17:07 +0000821 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700822 }
Daniel Jasper01786732013-02-04 07:21:18 +0000823
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700824 void determineTokenType(FormatToken &Current) {
825 if (!Current.is(TT_Unknown))
826 // The token type is already known.
827 return;
828
829 // Line.MightBeFunctionDecl can only be true after the parentheses of a
830 // function declaration have been found. In this case, 'Current' is a
831 // trailing token of this declaration and thus cannot be a name.
832 if (Current.is(Keywords.kw_instanceof)) {
833 Current.Type = TT_BinaryOperator;
834 } else if (isStartOfName(Current) &&
835 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
836 Contexts.back().FirstStartOfName = &Current;
837 Current.Type = TT_StartOfName;
838 } else if (Current.is(tok::kw_auto)) {
839 AutoFound = true;
840 } else if (Current.is(tok::arrow) &&
841 Style.Language == FormatStyle::LK_Java) {
842 Current.Type = TT_LambdaArrow;
843 } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration &&
844 Current.NestingLevel == 0) {
845 Current.Type = TT_TrailingReturnArrow;
846 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
847 Current.Type =
848 determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
849 Contexts.back().IsExpression,
850 Contexts.back().InTemplateArgument);
851 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
852 Current.Type = determinePlusMinusCaretUsage(Current);
853 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
854 Contexts.back().CaretFound = true;
855 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
856 Current.Type = determineIncrementUsage(Current);
857 } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
858 Current.Type = TT_UnaryOperator;
859 } else if (Current.is(tok::question)) {
860 Current.Type = TT_ConditionalExpr;
861 } else if (Current.isBinaryOperator() &&
862 (!Current.Previous || Current.Previous->isNot(tok::l_square))) {
863 Current.Type = TT_BinaryOperator;
864 } else if (Current.is(tok::comment)) {
865 Current.Type =
866 Current.TokenText.startswith("/*") ? TT_BlockComment : TT_LineComment;
867 } else if (Current.is(tok::r_paren)) {
868 if (rParenEndsCast(Current))
869 Current.Type = TT_CastRParen;
870 } else if (Current.is(tok::at) && Current.Next) {
871 switch (Current.Next->Tok.getObjCKeywordID()) {
872 case tok::objc_interface:
873 case tok::objc_implementation:
874 case tok::objc_protocol:
875 Current.Type = TT_ObjCDecl;
876 break;
877 case tok::objc_property:
878 Current.Type = TT_ObjCProperty;
879 break;
880 default:
881 break;
882 }
883 } else if (Current.is(tok::period)) {
884 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
885 if (PreviousNoComment &&
886 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
887 Current.Type = TT_DesignatedInitializerPeriod;
888 else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
889 Current.Previous->isOneOf(TT_JavaAnnotation,
890 TT_LeadingJavaAnnotation)) {
891 Current.Type = Current.Previous->Type;
892 }
893 } else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
894 Current.Previous &&
895 !Current.Previous->isOneOf(tok::equal, tok::at) &&
896 Line.MightBeFunctionDecl && Contexts.size() == 1) {
Daniel Jasper6b3ff8c2013-09-27 08:29:16 +0000897 // Line.MightBeFunctionDecl can only be true after the parentheses of a
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700898 // function declaration have been found.
899 Current.Type = TT_TrailingAnnotation;
900 } else if ((Style.Language == FormatStyle::LK_Java ||
901 Style.Language == FormatStyle::LK_JavaScript) &&
902 Current.Previous) {
903 if (Current.Previous->is(tok::at) &&
904 Current.isNot(Keywords.kw_interface)) {
905 const FormatToken &AtToken = *Current.Previous;
906 const FormatToken *Previous = AtToken.getPreviousNonComment();
907 if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
Stephen Hines176edba2014-12-01 14:53:08 -0800908 Current.Type = TT_LeadingJavaAnnotation;
909 else
910 Current.Type = TT_JavaAnnotation;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700911 } else if (Current.Previous->is(tok::period) &&
912 Current.Previous->isOneOf(TT_JavaAnnotation,
913 TT_LeadingJavaAnnotation)) {
914 Current.Type = Current.Previous->Type;
Daniel Jasper01786732013-02-04 07:21:18 +0000915 }
916 }
917 }
918
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000919 /// \brief Take a guess at whether \p Tok starts a name of a function or
920 /// variable declaration.
921 ///
922 /// This is a heuristic based on whether \p Tok is an identifier following
923 /// something that is likely a type.
924 bool isStartOfName(const FormatToken &Tok) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700925 if (Tok.isNot(tok::identifier) || !Tok.Previous)
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000926 return false;
927
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700928 if (Tok.Previous->is(TT_LeadingJavaAnnotation))
929 return false;
930
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000931 // Skip "const" as it does not have an influence on whether this is a name.
932 FormatToken *PreviousNotConst = Tok.Previous;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700933 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000934 PreviousNotConst = PreviousNotConst->Previous;
935
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700936 if (!PreviousNotConst)
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000937 return false;
938
Daniel Jasper2a409b62013-07-08 14:34:09 +0000939 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
940 PreviousNotConst->Previous &&
941 PreviousNotConst->Previous->is(tok::hash);
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000942
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700943 if (PreviousNotConst->is(TT_TemplateCloser))
Daniel Jasper92495a82013-08-19 10:16:18 +0000944 return PreviousNotConst && PreviousNotConst->MatchingParen &&
945 PreviousNotConst->MatchingParen->Previous &&
Stephen Hines176edba2014-12-01 14:53:08 -0800946 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
Daniel Jasper92495a82013-08-19 10:16:18 +0000947 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
948
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700949 if (PreviousNotConst->is(tok::r_paren) && PreviousNotConst->MatchingParen &&
950 PreviousNotConst->MatchingParen->Previous &&
951 PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype))
952 return true;
953
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000954 return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700955 PreviousNotConst->is(TT_PointerOrReference) ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700956 PreviousNotConst->isSimpleTypeSpecifier();
Daniel Jasper6ac431c2013-07-02 09:47:29 +0000957 }
958
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700959 /// \brief Determine whether ')' is ending a cast.
960 bool rParenEndsCast(const FormatToken &Tok) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700961 FormatToken *LeftOfParens = nullptr;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700962 if (Tok.MatchingParen)
963 LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
Stephen Hines176edba2014-12-01 14:53:08 -0800964 if (LeftOfParens && LeftOfParens->is(tok::r_paren) &&
965 LeftOfParens->MatchingParen)
966 LeftOfParens = LeftOfParens->MatchingParen->Previous;
967 if (LeftOfParens && LeftOfParens->is(tok::r_square) &&
968 LeftOfParens->MatchingParen &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700969 LeftOfParens->MatchingParen->is(TT_LambdaLSquare))
Stephen Hines176edba2014-12-01 14:53:08 -0800970 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700971 if (Tok.Next) {
972 if (Tok.Next->is(tok::question))
973 return false;
974 if (Style.Language == FormatStyle::LK_JavaScript &&
975 Tok.Next->is(Keywords.kw_in))
976 return false;
977 if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
978 return true;
979 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700980 bool IsCast = false;
981 bool ParensAreEmpty = Tok.Previous == Tok.MatchingParen;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700982 bool ParensAreType =
983 !Tok.Previous ||
984 Tok.Previous->isOneOf(TT_PointerOrReference, TT_TemplateCloser) ||
985 Tok.Previous->isSimpleTypeSpecifier();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700986 bool ParensCouldEndDecl =
987 Tok.Next && Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
988 bool IsSizeOfOrAlignOf =
989 LeftOfParens && LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
990 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700991 (Contexts.size() > 1 && Contexts[Contexts.size() - 2].IsExpression))
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700992 IsCast = true;
993 else if (Tok.Next && Tok.Next->isNot(tok::string_literal) &&
994 (Tok.Next->Tok.isLiteral() ||
995 Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
996 IsCast = true;
997 // If there is an identifier after the (), it is likely a cast, unless
998 // there is also an identifier before the ().
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700999 else if (LeftOfParens && Tok.Next &&
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001000 (LeftOfParens->Tok.getIdentifierInfo() == nullptr ||
1001 LeftOfParens->is(tok::kw_return)) &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001002 !LeftOfParens->isOneOf(TT_OverloadedOperator, tok::at,
1003 TT_TemplateCloser)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001004 if (Tok.Next->isOneOf(tok::identifier, tok::numeric_constant)) {
1005 IsCast = true;
1006 } else {
1007 // Use heuristics to recognize c style casting.
1008 FormatToken *Prev = Tok.Previous;
1009 if (Prev && Prev->isOneOf(tok::amp, tok::star))
1010 Prev = Prev->Previous;
1011
1012 if (Prev && Tok.Next && Tok.Next->Next) {
1013 bool NextIsUnary = Tok.Next->isUnaryOperator() ||
1014 Tok.Next->isOneOf(tok::amp, tok::star);
Stephen Hines176edba2014-12-01 14:53:08 -08001015 IsCast =
1016 NextIsUnary && !Tok.Next->is(tok::plus) &&
1017 Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001018 }
1019
1020 for (; Prev != Tok.MatchingParen; Prev = Prev->Previous) {
1021 if (!Prev || !Prev->isOneOf(tok::kw_const, tok::identifier)) {
1022 IsCast = false;
1023 break;
1024 }
1025 }
1026 }
1027 }
1028 return IsCast && !ParensAreEmpty;
1029 }
1030
Daniel Jasper01786732013-02-04 07:21:18 +00001031 /// \brief Return the type of the given token assuming it is * or &.
Stephen Hines651f13c2014-04-23 16:59:28 -07001032 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
1033 bool InTemplateArgument) {
Stephen Hines176edba2014-12-01 14:53:08 -08001034 if (Style.Language == FormatStyle::LK_JavaScript)
1035 return TT_BinaryOperator;
1036
Alexander Kornienko0bdc6432013-07-04 14:47:51 +00001037 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001038 if (!PrevToken)
Daniel Jasper01786732013-02-04 07:21:18 +00001039 return TT_UnaryOperator;
1040
Alexander Kornienko0bdc6432013-07-04 14:47:51 +00001041 const FormatToken *NextToken = Tok.getNextNonComment();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001042 if (!NextToken ||
1043 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
Daniel Jasper01786732013-02-04 07:21:18 +00001044 return TT_Unknown;
1045
Stephen Hines176edba2014-12-01 14:53:08 -08001046 if (PrevToken->is(tok::coloncolon))
Daniel Jasper8a5d7cd2013-03-01 17:13:29 +00001047 return TT_PointerOrReference;
1048
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001049 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
Daniel Jasperd3cf17b2013-03-14 10:50:25 +00001050 tok::comma, tok::semi, tok::kw_return, tok::colon,
Daniel Jasper65da8e92013-09-21 17:31:51 +00001051 tok::equal, tok::kw_delete, tok::kw_sizeof) ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001052 PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
1053 TT_UnaryOperator, TT_CastRParen))
Daniel Jasper01786732013-02-04 07:21:18 +00001054 return TT_UnaryOperator;
1055
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001056 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
Nico Webere8a97982013-02-06 06:20:11 +00001057 return TT_PointerOrReference;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001058 if (NextToken->isOneOf(tok::kw_operator, tok::comma, tok::semi))
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001059 return TT_PointerOrReference;
Nico Webere8a97982013-02-06 06:20:11 +00001060
Daniel Jasperdb8afe42013-09-10 10:26:38 +00001061 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
1062 PrevToken->MatchingParen->Previous &&
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001063 PrevToken->MatchingParen->Previous->isOneOf(tok::kw_typeof,
1064 tok::kw_decltype))
Daniel Jasperdb8afe42013-09-10 10:26:38 +00001065 return TT_PointerOrReference;
1066
Manuel Klimekb3987012013-05-29 14:47:47 +00001067 if (PrevToken->Tok.isLiteral() ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001068 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
Stephen Hines176edba2014-12-01 14:53:08 -08001069 tok::kw_false, tok::r_brace) ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001070 NextToken->Tok.isLiteral() ||
1071 NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
1072 NextToken->isUnaryOperator() ||
1073 // If we know we're in a template argument, there are no named
1074 // declarations. Thus, having an identifier on the right-hand side
1075 // indicates a binary operator.
1076 (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
Daniel Jasper01786732013-02-04 07:21:18 +00001077 return TT_BinaryOperator;
1078
Stephen Hines176edba2014-12-01 14:53:08 -08001079 // "&&(" is quite unlikely to be two successive unary "&".
1080 if (Tok.is(tok::ampamp) && NextToken && NextToken->is(tok::l_paren))
1081 return TT_BinaryOperator;
1082
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001083 // This catches some cases where evaluation order is used as control flow:
1084 // aaa && aaa->f();
1085 const FormatToken *NextNextToken = NextToken->getNextNonComment();
1086 if (NextNextToken && NextNextToken->is(tok::arrow))
1087 return TT_BinaryOperator;
1088
Daniel Jasper01786732013-02-04 07:21:18 +00001089 // It is very unlikely that we are going to find a pointer or reference type
1090 // definition on the RHS of an assignment.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001091 if (IsExpression && !Contexts.back().CaretFound)
Daniel Jasper01786732013-02-04 07:21:18 +00001092 return TT_BinaryOperator;
1093
1094 return TT_PointerOrReference;
1095 }
1096
Manuel Klimekb3987012013-05-29 14:47:47 +00001097 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +00001098 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001099 if (!PrevToken || PrevToken->is(TT_CastRParen))
Daniel Jasper01786732013-02-04 07:21:18 +00001100 return TT_UnaryOperator;
1101
1102 // Use heuristics to recognize unary operators.
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001103 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
1104 tok::question, tok::colon, tok::kw_return,
1105 tok::kw_case, tok::at, tok::l_brace))
Daniel Jasper01786732013-02-04 07:21:18 +00001106 return TT_UnaryOperator;
1107
Nico Weberee0feec2013-02-05 16:21:00 +00001108 // There can't be two consecutive binary operators.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001109 if (PrevToken->is(TT_BinaryOperator))
Daniel Jasper01786732013-02-04 07:21:18 +00001110 return TT_UnaryOperator;
1111
1112 // Fall back to marking the token as binary operator.
1113 return TT_BinaryOperator;
1114 }
1115
1116 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
Manuel Klimekb3987012013-05-29 14:47:47 +00001117 TokenType determineIncrementUsage(const FormatToken &Tok) {
Alexander Kornienko0bdc6432013-07-04 14:47:51 +00001118 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001119 if (!PrevToken || PrevToken->is(TT_CastRParen))
Daniel Jasper01786732013-02-04 07:21:18 +00001120 return TT_UnaryOperator;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001121 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
Daniel Jasper01786732013-02-04 07:21:18 +00001122 return TT_TrailingUnaryOperator;
1123
1124 return TT_UnaryOperator;
1125 }
Daniel Jasper4e778092013-02-06 10:05:46 +00001126
1127 SmallVector<Context, 8> Contexts;
1128
Daniel Jasperd4a03db2013-08-22 15:00:41 +00001129 const FormatStyle &Style;
Daniel Jasper4e778092013-02-06 10:05:46 +00001130 AnnotatedLine &Line;
Manuel Klimekb3987012013-05-29 14:47:47 +00001131 FormatToken *CurrentToken;
Daniel Jasper2ca37412013-07-09 14:36:48 +00001132 bool AutoFound;
Stephen Hines176edba2014-12-01 14:53:08 -08001133 const AdditionalKeywords &Keywords;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001134};
1135
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001136static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
1137static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001138
Daniel Jasper29f123b2013-02-08 15:28:42 +00001139/// \brief Parses binary expressions by inserting fake parenthesis based on
1140/// operator precedence.
1141class ExpressionParser {
1142public:
Stephen Hines176edba2014-12-01 14:53:08 -08001143 ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
1144 AnnotatedLine &Line)
1145 : Style(Style), Keywords(Keywords), Current(Line.First) {}
Daniel Jasper29f123b2013-02-08 15:28:42 +00001146
1147 /// \brief Parse expressions with the given operatore precedence.
Daniel Jasper237d4c12013-02-23 21:01:55 +00001148 void parse(int Precedence = 0) {
Daniel Jasper966e6d32013-11-07 19:23:49 +00001149 // Skip 'return' and ObjC selector colons as they are not part of a binary
1150 // expression.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001151 while (Current && (Current->is(tok::kw_return) ||
1152 (Current->is(tok::colon) &&
1153 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))))
Daniel Jasperf78bf4a2013-09-30 08:29:03 +00001154 next();
1155
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001156 if (!Current || Precedence > PrecedenceArrowAndPeriod)
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001157 return;
1158
Daniel Jasperc01897c2013-05-31 14:56:12 +00001159 // Conditional expressions need to be parsed separately for proper nesting.
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001160 if (Precedence == prec::Conditional) {
Daniel Jasperc01897c2013-05-31 14:56:12 +00001161 parseConditionalExpr();
1162 return;
1163 }
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001164
1165 // Parse unary operators, which all have a higher precedence than binary
1166 // operators.
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001167 if (Precedence == PrecedenceUnaryOperator) {
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001168 parseUnaryOperator();
Daniel Jasper29f123b2013-02-08 15:28:42 +00001169 return;
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001170 }
Daniel Jasper29f123b2013-02-08 15:28:42 +00001171
Manuel Klimekb3987012013-05-29 14:47:47 +00001172 FormatToken *Start = Current;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001173 FormatToken *LatestOperator = nullptr;
1174 unsigned OperatorIndex = 0;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001175
Daniel Jasper237d4c12013-02-23 21:01:55 +00001176 while (Current) {
Daniel Jasper29f123b2013-02-08 15:28:42 +00001177 // Consume operators with higher precedence.
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001178 parse(Precedence + 1);
Daniel Jasper29f123b2013-02-08 15:28:42 +00001179
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001180 int CurrentPrecedence = getCurrentPrecedence();
1181
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001182 if (Current && Current->is(TT_SelectorName) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001183 Precedence == CurrentPrecedence) {
1184 if (LatestOperator)
1185 addFakeParenthesis(Start, prec::Level(Precedence));
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001186 Start = Current;
Stephen Hines651f13c2014-04-23 16:59:28 -07001187 }
Daniel Jasper237d4c12013-02-23 21:01:55 +00001188
Daniel Jasper29f123b2013-02-08 15:28:42 +00001189 // At the end of the line or when an operator with higher precedence is
1190 // found, insert fake parenthesis and return.
Stephen Hines176edba2014-12-01 14:53:08 -08001191 if (!Current || (Current->closesScope() && Current->MatchingParen) ||
1192 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
1193 (CurrentPrecedence == prec::Conditional &&
1194 Precedence == prec::Assignment && Current->is(tok::colon))) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001195 break;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001196 }
1197
1198 // Consume scopes: (), [], <> and {}
Daniel Jasperac3223e2013-04-10 09:49:49 +00001199 if (Current->opensScope()) {
1200 while (Current && !Current->closesScope()) {
Daniel Jasper29f123b2013-02-08 15:28:42 +00001201 next();
1202 parse();
1203 }
1204 next();
1205 } else {
1206 // Operator found.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001207 if (CurrentPrecedence == Precedence) {
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001208 LatestOperator = Current;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001209 Current->OperatorIndex = OperatorIndex;
1210 ++OperatorIndex;
1211 }
Stephen Hines176edba2014-12-01 14:53:08 -08001212 next(/*SkipPastLeadingComments=*/Precedence > 0);
Daniel Jasper29f123b2013-02-08 15:28:42 +00001213 }
1214 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001215
1216 if (LatestOperator && (Current || Precedence > 0)) {
1217 LatestOperator->LastOperator = true;
1218 if (Precedence == PrecedenceArrowAndPeriod) {
1219 // Call expressions don't have a binary operator precedence.
1220 addFakeParenthesis(Start, prec::Unknown);
1221 } else {
1222 addFakeParenthesis(Start, prec::Level(Precedence));
1223 }
1224 }
Daniel Jasper29f123b2013-02-08 15:28:42 +00001225 }
1226
1227private:
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001228 /// \brief Gets the precedence (+1) of the given token for binary operators
1229 /// and other tokens that we treat like binary operators.
1230 int getCurrentPrecedence() {
1231 if (Current) {
Stephen Hines176edba2014-12-01 14:53:08 -08001232 const FormatToken *NextNonComment = Current->getNextNonComment();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001233 if (Current->is(TT_ConditionalExpr))
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001234 return prec::Conditional;
Stephen Hines176edba2014-12-01 14:53:08 -08001235 else if (NextNonComment && NextNonComment->is(tok::colon) &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001236 NextNonComment->is(TT_DictLiteral))
Stephen Hines176edba2014-12-01 14:53:08 -08001237 return prec::Comma;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001238 else if (Current->is(TT_LambdaArrow))
1239 return prec::Comma;
1240 else if (Current->isOneOf(tok::semi, TT_InlineASMColon,
1241 TT_SelectorName) ||
Stephen Hines176edba2014-12-01 14:53:08 -08001242 (Current->is(tok::comment) && NextNonComment &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001243 NextNonComment->is(TT_SelectorName)))
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001244 return 0;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001245 else if (Current->is(TT_RangeBasedForLoopColon))
Stephen Hines651f13c2014-04-23 16:59:28 -07001246 return prec::Comma;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001247 else if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001248 return Current->getPrecedence();
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001249 else if (Current->isOneOf(tok::period, tok::arrow))
1250 return PrecedenceArrowAndPeriod;
Stephen Hines176edba2014-12-01 14:53:08 -08001251 else if (Style.Language == FormatStyle::LK_Java &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001252 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
1253 Keywords.kw_throws))
Stephen Hines176edba2014-12-01 14:53:08 -08001254 return 0;
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001255 }
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001256 return -1;
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001257 }
1258
Daniel Jasperc01897c2013-05-31 14:56:12 +00001259 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
1260 Start->FakeLParens.push_back(Precedence);
Daniel Jasperdb4813a2013-09-06 08:08:14 +00001261 if (Precedence > prec::Unknown)
1262 Start->StartsBinaryExpression = true;
1263 if (Current) {
Stephen Hines176edba2014-12-01 14:53:08 -08001264 FormatToken *Previous = Current->Previous;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001265 while (Previous->is(tok::comment) && Previous->Previous)
Stephen Hines176edba2014-12-01 14:53:08 -08001266 Previous = Previous->Previous;
1267 ++Previous->FakeRParens;
Daniel Jasperdb4813a2013-09-06 08:08:14 +00001268 if (Precedence > prec::Unknown)
Stephen Hines176edba2014-12-01 14:53:08 -08001269 Previous->EndsBinaryExpression = true;
Daniel Jasperdb4813a2013-09-06 08:08:14 +00001270 }
Daniel Jasperc01897c2013-05-31 14:56:12 +00001271 }
1272
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001273 /// \brief Parse unary operator expressions and surround them with fake
1274 /// parentheses if appropriate.
1275 void parseUnaryOperator() {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001276 if (!Current || Current->isNot(TT_UnaryOperator)) {
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001277 parse(PrecedenceArrowAndPeriod);
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001278 return;
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001279 }
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001280
1281 FormatToken *Start = Current;
1282 next();
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001283 parseUnaryOperator();
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001284
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001285 // The actual precedence doesn't matter.
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001286 addFakeParenthesis(Start, prec::Unknown);
Daniel Jasper3618e6f2013-08-23 15:14:03 +00001287 }
1288
Daniel Jasperc01897c2013-05-31 14:56:12 +00001289 void parseConditionalExpr() {
Stephen Hines176edba2014-12-01 14:53:08 -08001290 while (Current && Current->isTrailingComment()) {
1291 next();
1292 }
Daniel Jasperc01897c2013-05-31 14:56:12 +00001293 FormatToken *Start = Current;
Daniel Jasperd489f8c2013-08-27 11:09:05 +00001294 parse(prec::LogicalOr);
Daniel Jasperc01897c2013-05-31 14:56:12 +00001295 if (!Current || !Current->is(tok::question))
1296 return;
1297 next();
Stephen Hines176edba2014-12-01 14:53:08 -08001298 parse(prec::Assignment);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001299 if (!Current || Current->isNot(TT_ConditionalExpr))
Daniel Jasperc01897c2013-05-31 14:56:12 +00001300 return;
1301 next();
Stephen Hines176edba2014-12-01 14:53:08 -08001302 parse(prec::Assignment);
Daniel Jasperc01897c2013-05-31 14:56:12 +00001303 addFakeParenthesis(Start, prec::Conditional);
1304 }
1305
Stephen Hines176edba2014-12-01 14:53:08 -08001306 void next(bool SkipPastLeadingComments = true) {
Alexander Kornienkod71b15b2013-06-17 13:19:53 +00001307 if (Current)
1308 Current = Current->Next;
Stephen Hines176edba2014-12-01 14:53:08 -08001309 while (Current &&
1310 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
1311 Current->isTrailingComment())
Manuel Klimekb3987012013-05-29 14:47:47 +00001312 Current = Current->Next;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001313 }
1314
Stephen Hines176edba2014-12-01 14:53:08 -08001315 const FormatStyle &Style;
1316 const AdditionalKeywords &Keywords;
Manuel Klimekb3987012013-05-29 14:47:47 +00001317 FormatToken *Current;
Daniel Jasper29f123b2013-02-08 15:28:42 +00001318};
1319
Craig Topper14e66492013-07-01 04:03:19 +00001320} // end anonymous namespace
1321
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001322void TokenAnnotator::setCommentLineLevels(
1323 SmallVectorImpl<AnnotatedLine *> &Lines) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001324 const AnnotatedLine *NextNonCommentLine = nullptr;
Daniel Jasper2a80ad62013-11-05 19:10:03 +00001325 for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
1326 E = Lines.rend();
1327 I != E; ++I) {
1328 if (NextNonCommentLine && (*I)->First->is(tok::comment) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001329 (*I)->First->Next == nullptr)
Daniel Jasper2a80ad62013-11-05 19:10:03 +00001330 (*I)->Level = NextNonCommentLine->Level;
Daniel Jasperb77d7412013-09-06 07:54:20 +00001331 else
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001332 NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr;
Daniel Jasper2a80ad62013-11-05 19:10:03 +00001333
1334 setCommentLineLevels((*I)->Children);
Daniel Jasperb77d7412013-09-06 07:54:20 +00001335 }
1336}
1337
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001338void TokenAnnotator::annotate(AnnotatedLine &Line) {
Daniel Jasperb77d7412013-09-06 07:54:20 +00001339 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1340 E = Line.Children.end();
Daniel Jasper567dcf92013-09-05 09:29:45 +00001341 I != E; ++I) {
1342 annotate(**I);
1343 }
Stephen Hines176edba2014-12-01 14:53:08 -08001344 AnnotatingParser Parser(Style, Line, Keywords);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001345 Line.Type = Parser.parseLine();
1346 if (Line.Type == LT_Invalid)
1347 return;
1348
Stephen Hines176edba2014-12-01 14:53:08 -08001349 ExpressionParser ExprParser(Style, Keywords, Line);
Daniel Jasper29f123b2013-02-08 15:28:42 +00001350 ExprParser.parse();
1351
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001352 if (Line.First->is(TT_ObjCMethodSpecifier))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001353 Line.Type = LT_ObjCMethodDecl;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001354 else if (Line.First->is(TT_ObjCDecl))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001355 Line.Type = LT_ObjCDecl;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001356 else if (Line.First->is(TT_ObjCProperty))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001357 Line.Type = LT_ObjCProperty;
1358
Manuel Klimekb3987012013-05-29 14:47:47 +00001359 Line.First->SpacesRequiredBefore = 1;
1360 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001361}
1362
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001363// This function heuristically determines whether 'Current' starts the name of a
1364// function declaration.
1365static bool isFunctionDeclarationName(const FormatToken &Current) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001366 if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0)
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001367 return false;
1368 const FormatToken *Next = Current.Next;
1369 for (; Next; Next = Next->Next) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001370 if (Next->is(TT_TemplateOpener)) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001371 Next = Next->MatchingParen;
1372 } else if (Next->is(tok::coloncolon)) {
1373 Next = Next->Next;
1374 if (!Next || !Next->is(tok::identifier))
1375 return false;
1376 } else if (Next->is(tok::l_paren)) {
1377 break;
1378 } else {
1379 return false;
1380 }
1381 }
1382 if (!Next)
1383 return false;
1384 assert(Next->is(tok::l_paren));
1385 if (Next->Next == Next->MatchingParen)
1386 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001387 for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001388 Tok = Tok->Next) {
1389 if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001390 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName))
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001391 return true;
1392 if (Tok->isOneOf(tok::l_brace, tok::string_literal) || Tok->Tok.isLiteral())
1393 return false;
1394 }
1395 return false;
1396}
1397
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001398void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001399 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1400 E = Line.Children.end();
1401 I != E; ++I) {
1402 calculateFormattingInformation(**I);
1403 }
1404
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001405 Line.First->TotalLength =
1406 Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth;
Manuel Klimekb3987012013-05-29 14:47:47 +00001407 if (!Line.First->Next)
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001408 return;
Manuel Klimekb3987012013-05-29 14:47:47 +00001409 FormatToken *Current = Line.First->Next;
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001410 bool InFunctionDecl = Line.MightBeFunctionDecl;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001411 while (Current) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001412 if (isFunctionDeclarationName(*Current))
1413 Current->Type = TT_FunctionDeclarationName;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001414 if (Current->is(TT_LineComment)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001415 if (Current->Previous->BlockKind == BK_BracedInit &&
1416 Current->Previous->opensScope())
1417 Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1;
1418 else
1419 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
1420
1421 // If we find a trailing comment, iterate backwards to determine whether
1422 // it seems to relate to a specific parameter. If so, break before that
1423 // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
1424 // to the previous line in:
1425 // SomeFunction(a,
1426 // b, // comment
1427 // c);
1428 if (!Current->HasUnescapedNewline) {
1429 for (FormatToken *Parameter = Current->Previous; Parameter;
1430 Parameter = Parameter->Previous) {
1431 if (Parameter->isOneOf(tok::comment, tok::r_brace))
1432 break;
1433 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001434 if (!Parameter->Previous->is(TT_CtorInitializerComma) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001435 Parameter->HasUnescapedNewline)
1436 Parameter->MustBreakBefore = true;
1437 break;
1438 }
1439 }
1440 }
1441 } else if (Current->SpacesRequiredBefore == 0 &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001442 spaceRequiredBefore(Line, *Current)) {
Alexander Kornienkob18c2582013-10-11 21:43:05 +00001443 Current->SpacesRequiredBefore = 1;
Stephen Hines651f13c2014-04-23 16:59:28 -07001444 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001445
Daniel Jasperebaa1712013-09-17 09:52:48 +00001446 Current->MustBreakBefore =
1447 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
1448
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001449 if (Style.AlwaysBreakAfterDefinitionReturnType && InFunctionDecl &&
1450 Current->is(TT_FunctionDeclarationName) &&
1451 !Line.Last->isOneOf(tok::semi, tok::comment)) // Only for definitions.
Stephen Hines176edba2014-12-01 14:53:08 -08001452 // FIXME: Line.Last points to other characters than tok::semi
1453 // and tok::lbrace.
1454 Current->MustBreakBefore = true;
1455
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001456 Current->CanBreakBefore =
1457 Current->MustBreakBefore || canBreakBefore(Line, *Current);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001458 unsigned ChildSize = 0;
1459 if (Current->Previous->Children.size() == 1) {
1460 FormatToken &LastOfChild = *Current->Previous->Children[0]->Last;
1461 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
1462 : LastOfChild.TotalLength + 1;
1463 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001464 const FormatToken *Prev = Current->Previous;
Stephen Hines176edba2014-12-01 14:53:08 -08001465 if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
1466 (Prev->Children.size() == 1 &&
1467 Prev->Children[0]->First->MustBreakBefore) ||
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001468 Current->IsMultiline)
Stephen Hines176edba2014-12-01 14:53:08 -08001469 Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001470 else
Stephen Hines176edba2014-12-01 14:53:08 -08001471 Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
1472 ChildSize + Current->SpacesRequiredBefore;
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001473
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001474 if (Current->is(TT_CtorInitializerColon))
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001475 InFunctionDecl = false;
1476
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001477 // FIXME: Only calculate this if CanBreakBefore is true once static
1478 // initializers etc. are sorted out.
1479 // FIXME: Move magic numbers to a better place.
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001480 Current->SplitPenalty = 20 * Current->BindingStrength +
1481 splitPenalty(Line, *Current, InFunctionDecl);
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001482
Manuel Klimekb3987012013-05-29 14:47:47 +00001483 Current = Current->Next;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001484 }
Daniel Jasperbf71ba22013-04-08 20:33:42 +00001485
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001486 calculateUnbreakableTailLengths(Line);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001487 for (Current = Line.First; Current != nullptr; Current = Current->Next) {
Daniel Jasperd4a03db2013-08-22 15:00:41 +00001488 if (Current->Role)
1489 Current->Role->precomputeFormattingInfos(Current);
1490 }
1491
Daniel Jasper567dcf92013-09-05 09:29:45 +00001492 DEBUG({ printDebugInfo(Line); });
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001493}
1494
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001495void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1496 unsigned UnbreakableTailLength = 0;
Manuel Klimekb3987012013-05-29 14:47:47 +00001497 FormatToken *Current = Line.Last;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001498 while (Current) {
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001499 Current->UnbreakableTailLength = UnbreakableTailLength;
1500 if (Current->CanBreakBefore ||
1501 Current->isOneOf(tok::comment, tok::string_literal)) {
1502 UnbreakableTailLength = 0;
1503 } else {
1504 UnbreakableTailLength +=
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +00001505 Current->ColumnWidth + Current->SpacesRequiredBefore;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001506 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001507 Current = Current->Previous;
Manuel Klimeke573c3f2013-05-22 12:51:29 +00001508 }
1509}
1510
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001511unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
Daniel Jasperdbfb5f32013-11-07 17:52:51 +00001512 const FormatToken &Tok,
1513 bool InFunctionDecl) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001514 const FormatToken &Left = *Tok.Previous;
1515 const FormatToken &Right = Tok;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001516
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001517 if (Left.is(tok::semi))
1518 return 0;
Stephen Hines176edba2014-12-01 14:53:08 -08001519
1520 if (Style.Language == FormatStyle::LK_Java) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001521 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
Stephen Hines176edba2014-12-01 14:53:08 -08001522 return 1;
1523 if (Right.is(Keywords.kw_implements))
1524 return 2;
1525 if (Left.is(tok::comma) && Left.NestingLevel == 0)
1526 return 3;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001527 } else if (Style.Language == FormatStyle::LK_JavaScript) {
1528 if (Right.is(Keywords.kw_function))
1529 return 100;
Stephen Hines176edba2014-12-01 14:53:08 -08001530 }
1531
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001532 if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001533 Right.Next->is(TT_DictLiteral)))
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001534 return 1;
Stephen Hines651f13c2014-04-23 16:59:28 -07001535 if (Right.is(tok::l_square)) {
1536 if (Style.Language == FormatStyle::LK_Proto)
1537 return 1;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001538 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare))
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001539 return 500;
Stephen Hines651f13c2014-04-23 16:59:28 -07001540 }
Stephen Hines176edba2014-12-01 14:53:08 -08001541
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001542 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
1543 Right.is(tok::kw_operator)) {
Manuel Klimekb3987012013-05-29 14:47:47 +00001544 if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001545 return 3;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001546 if (Left.is(TT_StartOfName))
Daniel Jasperc18cff32013-07-11 12:34:23 +00001547 return 20;
Stephen Hines651f13c2014-04-23 16:59:28 -07001548 if (InFunctionDecl && Right.NestingLevel == 0)
Daniel Jasper3c08a812013-02-24 18:54:32 +00001549 return Style.PenaltyReturnTypeOnItsOwnLine;
Daniel Jasper92495a82013-08-19 10:16:18 +00001550 return 200;
Daniel Jasper3c08a812013-02-24 18:54:32 +00001551 }
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001552 if (Right.is(TT_PointerOrReference))
1553 return 190;
1554 if (Right.is(TT_TrailingReturnArrow))
1555 return 110;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001556 if (Left.is(tok::equal) && Right.is(tok::l_brace))
1557 return 150;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001558 if (Left.is(TT_CastRParen))
Daniel Jasper198c8bf2013-07-05 07:58:34 +00001559 return 100;
Stephen Hines651f13c2014-04-23 16:59:28 -07001560 if (Left.is(tok::coloncolon) ||
1561 (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001562 return 500;
Daniel Jasper6b119d62013-04-05 17:22:09 +00001563 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1564 return 5000;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001565
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001566 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon))
Daniel Jasper84a1a632013-02-26 13:18:08 +00001567 return 2;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001568
Daniel Jasperd3fef0f2013-08-27 14:24:43 +00001569 if (Right.isMemberAccess()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001570 if (Left.is(tok::r_paren) && Left.MatchingParen &&
Daniel Jasper2f0a0202013-09-06 08:54:24 +00001571 Left.MatchingParen->ParameterCount > 0)
Daniel Jasper518ee342013-02-26 13:59:14 +00001572 return 20; // Should be smaller than breaking at a nested comma.
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001573 return 150;
1574 }
1575
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001576 if (Right.is(TT_TrailingAnnotation) &&
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001577 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
Stephen Hines176edba2014-12-01 14:53:08 -08001578 // Moving trailing annotations to the next line is fine for ObjC method
1579 // declarations.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001580 if (Line.First->is(TT_ObjCMethodSpecifier))
Stephen Hines176edba2014-12-01 14:53:08 -08001581
1582 return 10;
Stephen Hines651f13c2014-04-23 16:59:28 -07001583 // Generally, breaking before a trailing annotation is bad unless it is
1584 // function-like. It seems to be especially preferable to keep standard
1585 // annotations (i.e. "const", "final" and "override") on the same line.
1586 // Use a slightly higher penalty after ")" so that annotations like
1587 // "const override" are kept together.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001588 bool is_short_annotation = Right.TokenText.size() < 10;
1589 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
Stephen Hines651f13c2014-04-23 16:59:28 -07001590 }
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00001591
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001592 // In for-loops, prefer breaking at ',' and ';'.
Manuel Klimekb3987012013-05-29 14:47:47 +00001593 if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
Daniel Jasper7d812812013-02-21 15:00:29 +00001594 return 4;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001595
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001596 // In Objective-C method expressions, prefer breaking before "param:" over
1597 // breaking after it.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001598 if (Right.is(TT_SelectorName))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001599 return 0;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001600 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
Stephen Hines651f13c2014-04-23 16:59:28 -07001601 return Line.MightBeFunctionDecl ? 50 : 500;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001602
Stephen Hines176edba2014-12-01 14:53:08 -08001603 if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
Daniel Jasper1407bee2013-04-11 14:29:13 +00001604 return 100;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001605 if (Left.is(tok::l_paren) && Left.Previous && Left.Previous->is(tok::kw_if))
1606 return 1000;
Stephen Hines651f13c2014-04-23 16:59:28 -07001607 if (Left.is(tok::equal) && InFunctionDecl)
1608 return 110;
Stephen Hines176edba2014-12-01 14:53:08 -08001609 if (Right.is(tok::r_brace))
1610 return 1;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001611 if (Left.is(TT_TemplateOpener))
Stephen Hines176edba2014-12-01 14:53:08 -08001612 return 100;
1613 if (Left.opensScope()) {
1614 if (!Style.AlignAfterOpenBracket)
1615 return 0;
Daniel Jasper47066e42013-10-25 14:29:37 +00001616 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
1617 : 19;
Stephen Hines176edba2014-12-01 14:53:08 -08001618 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001619 if (Left.is(TT_JavaAnnotation))
1620 return 50;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001621
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001622 if (Right.is(tok::lessless)) {
1623 if (Left.is(tok::string_literal)) {
Alexander Kornienko00895102013-06-05 14:09:10 +00001624 StringRef Content = Left.TokenText;
Daniel Jasper20376982013-09-29 12:02:57 +00001625 if (Content.startswith("\""))
1626 Content = Content.drop_front(1);
1627 if (Content.endswith("\""))
1628 Content = Content.drop_back(1);
1629 Content = Content.trim();
Daniel Jasperbfa1edd2013-03-14 14:00:17 +00001630 if (Content.size() > 1 &&
1631 (Content.back() == ':' || Content.back() == '='))
Daniel Jasper9637dda2013-07-15 14:33:14 +00001632 return 25;
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001633 }
Daniel Jasper0c368782013-07-15 15:04:42 +00001634 return 1; // Breaking at a << is really cheap.
Daniel Jasper4e8a7b42013-02-06 21:04:05 +00001635 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001636 if (Left.is(TT_ConditionalExpr))
Daniel Jasper518ee342013-02-26 13:59:14 +00001637 return prec::Conditional;
Manuel Klimekb3987012013-05-29 14:47:47 +00001638 prec::Level Level = Left.getPrecedence();
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001639 if (Level != prec::Unknown)
1640 return Level;
Daniel Jasper24849712013-03-01 16:48:32 +00001641
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001642 return 3;
1643}
1644
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001645bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001646 const FormatToken &Left,
1647 const FormatToken &Right) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001648 if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
1649 return true;
Stephen Hines651f13c2014-04-23 16:59:28 -07001650 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
1651 Left.Tok.getObjCKeywordID() == tok::objc_property)
1652 return true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001653 if (Right.is(tok::hashhash))
1654 return Left.is(tok::hash);
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001655 if (Left.isOneOf(tok::hashhash, tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001656 return Right.is(tok::hash);
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001657 if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1658 return Style.SpaceInEmptyParentheses;
1659 if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001660 return (Right.is(TT_CastRParen) ||
1661 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
Daniel Jasper7df56bf2013-08-20 12:36:34 +00001662 ? Style.SpacesInCStyleCastParentheses
1663 : Style.SpacesInParentheses;
1664 if (Right.isOneOf(tok::semi, tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001665 return false;
1666 if (Right.is(tok::less) &&
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -07001667 (Left.is(tok::kw_template) ||
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001668 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1669 return true;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001670 if (Left.isOneOf(tok::exclaim, tok::tilde))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001671 return false;
1672 if (Left.is(tok::at) &&
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001673 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1674 tok::numeric_constant, tok::l_paren, tok::l_brace,
1675 tok::kw_true, tok::kw_false))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001676 return false;
1677 if (Left.is(tok::coloncolon))
1678 return false;
Alexander Kornienkoe74de282013-03-13 14:41:29 +00001679 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001680 return false;
Daniel Jasperc47d7f12013-07-01 09:47:25 +00001681 if (Right.is(tok::ellipsis))
Daniel Jasperb3c887d2013-10-20 16:56:16 +00001682 return Left.Tok.isLiteral();
Manuel Klimek31e44f72013-09-04 08:20:47 +00001683 if (Left.is(tok::l_square) && Right.is(tok::amp))
1684 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001685 if (Right.is(TT_PointerOrReference))
1686 return !(Left.is(tok::r_paren) && Left.MatchingParen &&
1687 (Left.MatchingParen->is(TT_OverloadedOperatorLParen) ||
1688 (Left.MatchingParen->Previous &&
1689 Left.MatchingParen->Previous->is(
1690 TT_FunctionDeclarationName)))) &&
1691 (Left.Tok.isLiteral() ||
1692 (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001693 (Style.PointerAlignment != FormatStyle::PAS_Left ||
1694 Line.IsMultiVariableDeclStmt)));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001695 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
1696 (!Left.is(TT_PointerOrReference) ||
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001697 (Style.PointerAlignment != FormatStyle::PAS_Right &&
1698 !Line.IsMultiVariableDeclStmt)))
Daniel Jasper395228f2013-05-08 14:58:20 +00001699 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001700 if (Left.is(TT_PointerOrReference))
1701 return Right.Tok.isLiteral() || Right.is(TT_BlockComment) ||
1702 (!Right.isOneOf(TT_PointerOrReference, tok::l_paren) &&
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001703 (Style.PointerAlignment != FormatStyle::PAS_Right &&
1704 !Line.IsMultiVariableDeclStmt) &&
1705 Left.Previous &&
Manuel Klimekb3987012013-05-29 14:47:47 +00001706 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001707 if (Right.is(tok::star) && Left.is(tok::l_paren))
1708 return false;
Nico Weber051860e2013-02-10 02:08:05 +00001709 if (Left.is(tok::l_square))
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001710 return (Left.is(TT_ArrayInitializerLSquare) &&
Stephen Hines176edba2014-12-01 14:53:08 -08001711 Style.SpacesInContainerLiterals && Right.isNot(tok::r_square)) ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001712 (Left.is(TT_ArraySubscriptLSquare) && Style.SpacesInSquareBrackets &&
1713 Right.isNot(tok::r_square));
Nico Weber051860e2013-02-10 02:08:05 +00001714 if (Right.is(tok::r_square))
Stephen Hines176edba2014-12-01 14:53:08 -08001715 return Right.MatchingParen &&
1716 ((Style.SpacesInContainerLiterals &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001717 Right.MatchingParen->is(TT_ArrayInitializerLSquare)) ||
Stephen Hines176edba2014-12-01 14:53:08 -08001718 (Style.SpacesInSquareBrackets &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001719 Right.MatchingParen->is(TT_ArraySubscriptLSquare)));
1720 if (Right.is(tok::l_square) &&
1721 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare) &&
1722 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001723 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001724 if (Left.is(tok::colon))
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001725 return !Left.is(TT_ObjCMethodExpr);
Stephen Hines176edba2014-12-01 14:53:08 -08001726 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
1727 return !Left.Children.empty(); // No spaces in "{}".
1728 if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) ||
1729 (Right.is(tok::r_brace) && Right.MatchingParen &&
1730 Right.MatchingParen->BlockKind != BK_Block))
1731 return !Style.Cpp11BracedListStyle;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001732 if (Left.is(TT_BlockComment))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001733 return !Left.TokenText.endswith("=*/");
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001734 if (Right.is(tok::l_paren)) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001735 if (Left.is(tok::r_paren) && Left.is(TT_AttributeParen))
Daniel Jaspere0fa4c52013-07-17 20:25:02 +00001736 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001737 return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001738 (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
1739 (Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while,
Stephen Hines176edba2014-12-01 14:53:08 -08001740 tok::kw_switch, tok::kw_case) ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001741 (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch,
1742 tok::kw_new, tok::kw_delete) &&
Stephen Hines176edba2014-12-01 14:53:08 -08001743 (!Left.Previous || Left.Previous->isNot(tok::period))) ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001744 Left.IsForEachMacro)) ||
1745 (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
Stephen Hines176edba2014-12-01 14:53:08 -08001746 (Left.is(tok::identifier) || Left.isFunctionLikeKeyword()) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001747 Line.Type != LT_PreprocessorDirective);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001748 }
Manuel Klimekb3987012013-05-29 14:47:47 +00001749 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001750 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001751 if (Right.is(TT_UnaryOperator))
Daniel Jasper1bee0732013-05-23 18:05:18 +00001752 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001753 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001754 if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
1755 tok::r_paren) ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001756 Left.isSimpleTypeSpecifier()) &&
Manuel Klimek31e44f72013-09-04 08:20:47 +00001757 Right.is(tok::l_brace) && Right.getNextNonComment() &&
1758 Right.BlockKind != BK_Block)
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001759 return false;
Daniel Jasper5ad390d2013-05-28 11:30:49 +00001760 if (Left.is(tok::period) || Right.is(tok::period))
1761 return false;
Alexander Kornienkodaa07e92013-09-10 13:41:43 +00001762 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
1763 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001764 if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
Stephen Hines176edba2014-12-01 14:53:08 -08001765 Left.MatchingParen->Previous &&
1766 Left.MatchingParen->Previous->is(tok::period))
1767 // A.<B>DoSomething();
1768 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001769 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
Stephen Hines176edba2014-12-01 14:53:08 -08001770 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001771 return true;
1772}
1773
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001774bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Stephen Hines176edba2014-12-01 14:53:08 -08001775 const FormatToken &Right) {
1776 const FormatToken &Left = *Right.Previous;
1777 if (Style.Language == FormatStyle::LK_Proto) {
1778 if (Right.is(tok::period) &&
1779 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
1780 Keywords.kw_repeated))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001781 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001782 if (Right.is(tok::l_paren) &&
1783 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option))
1784 return true;
1785 } else if (Style.Language == FormatStyle::LK_JavaScript) {
1786 if (Left.is(Keywords.kw_var))
1787 return true;
Pirama Arumuga Nainar33337ca2015-05-06 11:48:57 -07001788 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001789 return false;
1790 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
1791 Line.First->isOneOf(Keywords.kw_import, tok::kw_export))
1792 return false;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001793 if (Left.is(TT_TemplateCloser) &&
1794 !Right.isOneOf(tok::l_brace, tok::comma, tok::l_square,
1795 Keywords.kw_implements, Keywords.kw_extends))
1796 // Type assertions ('<type>expr') are not followed by whitespace. Other
1797 // locations that should have whitespace following are identified by the
1798 // above set of follower tokens.
1799 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001800 } else if (Style.Language == FormatStyle::LK_Java) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001801 if (Left.is(tok::r_square) && Right.is(tok::l_brace))
1802 return true;
1803 if (Left.is(TT_LambdaArrow) || Right.is(TT_LambdaArrow))
1804 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001805 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
1806 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001807 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
1808 tok::kw_protected) ||
1809 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
1810 Keywords.kw_native)) &&
1811 Right.is(TT_TemplateOpener))
Stephen Hines176edba2014-12-01 14:53:08 -08001812 return true;
1813 }
1814 if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
1815 return true; // Never ever merge two identifiers.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001816 if (Left.is(TT_ImplicitStringLiteral))
Stephen Hines176edba2014-12-01 14:53:08 -08001817 return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
1818 if (Line.Type == LT_ObjCMethodDecl) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001819 if (Left.is(TT_ObjCMethodSpecifier))
Stephen Hines176edba2014-12-01 14:53:08 -08001820 return true;
1821 if (Left.is(tok::r_paren) && Right.is(tok::identifier))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001822 // Don't space between ')' and <id>
1823 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001824 }
1825 if (Line.Type == LT_ObjCProperty &&
Stephen Hines176edba2014-12-01 14:53:08 -08001826 (Right.is(tok::equal) || Left.is(tok::equal)))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001827 return false;
1828
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001829 if (Right.is(TT_TrailingReturnArrow) || Left.is(TT_TrailingReturnArrow))
Daniel Jasper2ca37412013-07-09 14:36:48 +00001830 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001831 if (Left.is(tok::comma))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001832 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001833 if (Right.is(tok::comma))
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001834 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001835 if (Right.isOneOf(TT_CtorInitializerColon, TT_ObjCBlockLParen))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001836 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001837 if (Left.is(tok::kw_operator))
1838 return Right.is(tok::coloncolon);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001839 if (Right.is(TT_OverloadedOperatorLParen))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001840 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001841 if (Right.is(tok::colon))
Manuel Klimekb3987012013-05-29 14:47:47 +00001842 return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001843 Right.getNextNonComment() && Right.isNot(TT_ObjCMethodExpr) &&
Stephen Hines176edba2014-12-01 14:53:08 -08001844 !Left.is(tok::question) &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001845 !(Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon)) &&
1846 (Right.isNot(TT_DictLiteral) || Style.SpacesInContainerLiterals);
1847 if (Left.is(TT_UnaryOperator))
1848 return Right.is(TT_BinaryOperator);
1849 if (Left.is(TT_CastRParen))
1850 return Style.SpaceAfterCStyleCast || Right.is(TT_BinaryOperator);
Stephen Hines176edba2014-12-01 14:53:08 -08001851 if (Left.is(tok::greater) && Right.is(tok::greater)) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001852 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
Daniel Jasperd8ee5c12013-10-29 14:52:02 +00001853 (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001854 }
Stephen Hines176edba2014-12-01 14:53:08 -08001855 if (Right.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
1856 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar))
Daniel Jasper9c3c7b32013-02-28 13:40:17 +00001857 return false;
Daniel Jasper9b4de852013-09-25 15:15:02 +00001858 if (!Style.SpaceBeforeAssignmentOperators &&
Stephen Hines176edba2014-12-01 14:53:08 -08001859 Right.getPrecedence() == prec::Assignment)
Daniel Jasper9b4de852013-09-25 15:15:02 +00001860 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001861 if (Right.is(tok::coloncolon) && Left.isNot(tok::l_brace))
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001862 return (Left.is(TT_TemplateOpener) &&
Stephen Hines176edba2014-12-01 14:53:08 -08001863 Style.Standard == FormatStyle::LS_Cpp03) ||
1864 !(Left.isOneOf(tok::identifier, tok::l_paren, tok::r_paren) ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001865 Left.isOneOf(TT_TemplateCloser, TT_TemplateOpener));
1866 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
Stephen Hines176edba2014-12-01 14:53:08 -08001867 return Style.SpacesInAngles;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001868 if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) ||
1869 Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001870 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001871 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_paren) &&
1872 Right.isNot(TT_FunctionTypeLParen))
Stephen Hines176edba2014-12-01 14:53:08 -08001873 return Style.SpaceBeforeParens == FormatStyle::SBPO_Always;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001874 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
1875 Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001876 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001877 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
Daniel Jaspera4dd9822013-08-28 08:24:04 +00001878 Line.First->is(tok::hash))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001879 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001880 if (Right.is(TT_TrailingUnaryOperator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001881 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001882 if (Left.is(TT_RegexLiteral))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001883 return false;
Stephen Hines176edba2014-12-01 14:53:08 -08001884 return spaceRequiredBetween(Line, Left, Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001885}
1886
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001887// Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
1888static bool isAllmanBrace(const FormatToken &Tok) {
1889 return Tok.is(tok::l_brace) && Tok.BlockKind == BK_Block &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001890 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001891}
1892
Daniel Jasperebaa1712013-09-17 09:52:48 +00001893bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
1894 const FormatToken &Right) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001895 const FormatToken &Left = *Right.Previous;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001896 if (Right.NewlinesBefore > 1)
1897 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08001898
1899 // If the last token before a '}' is a comma or a trailing comment, the
1900 // intention is to insert a line break after it in order to make shuffling
1901 // around entries easier.
1902 const FormatToken *BeforeClosingBrace = nullptr;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001903 if (Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
1904 Left.BlockKind != BK_Block && Left.MatchingParen)
Stephen Hines176edba2014-12-01 14:53:08 -08001905 BeforeClosingBrace = Left.MatchingParen->Previous;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001906 else if (Right.MatchingParen &&
1907 Right.MatchingParen->isOneOf(tok::l_brace,
1908 TT_ArrayInitializerLSquare))
Stephen Hines176edba2014-12-01 14:53:08 -08001909 BeforeClosingBrace = &Left;
1910 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
1911 BeforeClosingBrace->isTrailingComment()))
1912 return true;
1913
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001914 if (Right.is(tok::comment))
Stephen Hines176edba2014-12-01 14:53:08 -08001915 return Left.BlockKind != BK_BracedInit &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001916 Left.isNot(TT_CtorInitializerColon) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001917 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001918 if (Right.Previous->isTrailingComment() ||
1919 (Right.isStringLiteral() && Right.Previous->isStringLiteral()))
Daniel Jasperebaa1712013-09-17 09:52:48 +00001920 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001921 if (Right.Previous->IsUnterminatedLiteral)
Daniel Jasperebaa1712013-09-17 09:52:48 +00001922 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001923 if (Right.is(tok::lessless) && Right.Next &&
1924 Right.Previous->is(tok::string_literal) &&
1925 Right.Next->is(tok::string_literal))
Daniel Jasperebaa1712013-09-17 09:52:48 +00001926 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001927 if (Right.Previous->ClosesTemplateDeclaration &&
1928 Right.Previous->MatchingParen &&
1929 Right.Previous->MatchingParen->NestingLevel == 0 &&
1930 Style.AlwaysBreakTemplateDeclarations)
Daniel Jasperebaa1712013-09-17 09:52:48 +00001931 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001932 if ((Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) &&
1933 Style.BreakConstructorInitializersBeforeComma &&
1934 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
Daniel Jasperebaa1712013-09-17 09:52:48 +00001935 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001936 if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\""))
Stephen Hines651f13c2014-04-23 16:59:28 -07001937 // Raw string literals are special wrt. line breaks. The author has made a
1938 // deliberate choice and might have aligned the contents of the string
1939 // literal accordingly. Thus, we try keep existing line breaks.
1940 return Right.NewlinesBefore > 0;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001941 if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 &&
1942 Style.Language == FormatStyle::LK_Proto)
Stephen Hines176edba2014-12-01 14:53:08 -08001943 // Don't put enums onto single lines in protocol buffers.
Stephen Hines651f13c2014-04-23 16:59:28 -07001944 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001945 if (Style.Language == FormatStyle::LK_JavaScript && Right.is(tok::r_brace) &&
1946 Left.is(tok::l_brace) && !Left.Children.empty())
Stephen Hines176edba2014-12-01 14:53:08 -08001947 // Support AllowShortFunctionsOnASingleLine for JavaScript.
1948 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
1949 (Left.NestingLevel == 0 && Line.Level == 0 &&
1950 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001951 if (isAllmanBrace(Left) || isAllmanBrace(Right))
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001952 return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1953 Style.BreakBeforeBraces == FormatStyle::BS_GNU;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001954 if (Style.Language == FormatStyle::LK_Proto && Left.isNot(tok::l_brace) &&
1955 Right.is(TT_SelectorName))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001956 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001957 if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine)
Stephen Hines176edba2014-12-01 14:53:08 -08001958 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001959
1960 if ((Style.Language == FormatStyle::LK_Java ||
1961 Style.Language == FormatStyle::LK_JavaScript) &&
1962 Left.is(TT_LeadingJavaAnnotation) &&
1963 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
1964 Line.Last->is(tok::l_brace))
1965 return true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001966
1967 if (Style.Language == FormatStyle::LK_JavaScript) {
1968 // FIXME: This might apply to other languages and token kinds.
1969 if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
1970 Left.Previous->is(tok::char_constant))
1971 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001972 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) &&
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001973 Left.NestingLevel == 0 && Left.Previous &&
1974 Left.Previous->is(tok::equal) &&
1975 Line.First->isOneOf(tok::identifier, Keywords.kw_import,
1976 tok::kw_export) &&
1977 // kw_var is a pseudo-token that's a tok::identifier, so matches above.
1978 !Line.First->is(Keywords.kw_var))
1979 // Enum style object literal.
Stephen Hines176edba2014-12-01 14:53:08 -08001980 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001981 } else if (Style.Language == FormatStyle::LK_Java) {
Stephen Hines176edba2014-12-01 14:53:08 -08001982 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
1983 Right.Next->is(tok::string_literal))
1984 return true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001985 }
1986
Daniel Jasperebaa1712013-09-17 09:52:48 +00001987 return false;
1988}
1989
Daniel Jasper8ff690a2013-02-06 14:22:40 +00001990bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
Manuel Klimekb3987012013-05-29 14:47:47 +00001991 const FormatToken &Right) {
1992 const FormatToken &Left = *Right.Previous;
Stephen Hines176edba2014-12-01 14:53:08 -08001993
1994 if (Style.Language == FormatStyle::LK_Java) {
1995 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
1996 Keywords.kw_implements))
1997 return false;
1998 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
1999 Keywords.kw_implements))
2000 return true;
2001 }
2002
Stephen Hines651f13c2014-04-23 16:59:28 -07002003 if (Left.is(tok::at))
2004 return false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002005 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
2006 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002007 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
2008 return !Right.is(tok::l_paren);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002009 if (Right.is(TT_PointerOrReference))
2010 return Line.IsMultiVariableDeclStmt ||
2011 (Style.PointerAlignment == FormatStyle::PAS_Right &&
2012 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002013 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
2014 Right.is(tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00002015 return true;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002016 if (Left.is(TT_PointerOrReference))
2017 return false;
Daniel Jasper1a896a52013-11-08 00:57:11 +00002018 if (Right.isTrailingComment())
2019 // We rely on MustBreakBefore being set correctly here as we should not
2020 // change the "binding" behavior of a comment.
Stephen Hines651f13c2014-04-23 16:59:28 -07002021 // The first comment in a braced lists is always interpreted as belonging to
2022 // the first list element. Otherwise, it should be placed outside of the
2023 // list.
2024 return Left.BlockKind == BK_BracedInit;
Daniel Jasper1a896a52013-11-08 00:57:11 +00002025 if (Left.is(tok::question) && Right.is(tok::colon))
2026 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002027 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
Daniel Jasper1a896a52013-11-08 00:57:11 +00002028 return Style.BreakBeforeTernaryOperators;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002029 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
Daniel Jasper1a896a52013-11-08 00:57:11 +00002030 return !Style.BreakBeforeTernaryOperators;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002031 if (Right.is(TT_InheritanceColon))
Stephen Hines651f13c2014-04-23 16:59:28 -07002032 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002033 if (Right.is(tok::colon) &&
2034 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon))
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002035 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002036 if (Left.is(tok::colon) && (Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00002037 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002038 if (Right.is(TT_SelectorName))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00002039 return true;
Daniel Jasperaa9e7b12013-08-01 13:46:58 +00002040 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
2041 return true;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00002042 if (Left.ClosesTemplateDeclaration)
2043 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002044 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
2045 TT_OverloadedOperator))
Daniel Jasper6cabab42013-02-14 08:42:54 +00002046 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002047 if (Left.is(TT_RangeBasedForLoopColon))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00002048 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002049 if (Right.is(TT_RangeBasedForLoopColon))
Daniel Jasper7d812812013-02-21 15:00:29 +00002050 return false;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002051 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002052 Left.is(tok::kw_operator))
Daniel Jasper32d28ee2013-01-29 21:01:14 +00002053 return false;
2054 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
2055 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002056 if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen))
Stephen Hines651f13c2014-04-23 16:59:28 -07002057 return false;
2058 if (Left.is(tok::l_paren) && Left.Previous &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002059 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen)))
Stephen Hines651f13c2014-04-23 16:59:28 -07002060 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002061 if (Right.is(TT_ImplicitStringLiteral))
Daniel Jasper84379572013-10-30 13:54:53 +00002062 return false;
Daniel Jasper32d28ee2013-01-29 21:01:14 +00002063
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002064 if (Right.is(tok::r_paren) || Right.is(TT_TemplateCloser))
Daniel Jasper567dcf92013-09-05 09:29:45 +00002065 return false;
2066
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00002067 // We only break before r_brace if there was a corresponding break before
2068 // the l_brace, which is tracked by BreakBeforeClosingBrace.
Daniel Jasper567dcf92013-09-05 09:29:45 +00002069 if (Right.is(tok::r_brace))
2070 return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
Daniel Jasper5ad72bb2013-05-22 08:28:26 +00002071
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002072 // Allow breaking after a trailing annotation, e.g. after a method
2073 // declaration.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002074 if (Left.is(TT_TrailingAnnotation))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002075 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
2076 tok::less, tok::coloncolon);
Daniel Jasper32d28ee2013-01-29 21:01:14 +00002077
Daniel Jasper8ef19a22013-03-14 09:50:46 +00002078 if (Right.is(tok::kw___attribute))
2079 return true;
2080
Daniel Jasper3a204412013-02-23 07:46:38 +00002081 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
2082 return true;
Daniel Jaspere8b10d32013-07-26 16:56:36 +00002083
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002084 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002085 return true;
2086
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002087 if (Left.is(TT_CtorInitializerComma) &&
Daniel Jaspere8b10d32013-07-26 16:56:36 +00002088 Style.BreakConstructorInitializersBeforeComma)
2089 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002090 if (Right.is(TT_CtorInitializerComma) &&
Daniel Jasper19ccb122013-10-08 05:11:18 +00002091 Style.BreakConstructorInitializersBeforeComma)
2092 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002093 if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
2094 (Left.is(tok::less) && Right.is(tok::less)))
Daniel Jasper26356cc2013-09-17 08:15:46 +00002095 return false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002096 if (Right.is(TT_BinaryOperator) &&
Stephen Hines176edba2014-12-01 14:53:08 -08002097 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
2098 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
2099 Right.getPrecedence() != prec::Assignment))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002100 return true;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002101 if (Left.is(TT_ArrayInitializerLSquare))
Daniel Jaspera07aa662013-10-22 15:30:28 +00002102 return true;
Stephen Hines176edba2014-12-01 14:53:08 -08002103 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
2104 return true;
2105 if (Left.isBinaryOperator() && !Left.isOneOf(tok::arrowstar, tok::lessless) &&
2106 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
2107 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
2108 Left.getPrecedence() == prec::Assignment))
2109 return true;
2110 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
Daniel Jasper6b119d62013-04-05 17:22:09 +00002111 tok::kw_class, tok::kw_struct) ||
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002112 Right.isMemberAccess() ||
2113 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
2114 tok::colon, tok::l_square, tok::at) ||
Daniel Jasper198c8bf2013-07-05 07:58:34 +00002115 (Left.is(tok::r_paren) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002116 Right.isOneOf(tok::identifier, tok::kw_const)) ||
Daniel Jaspera07aa662013-10-22 15:30:28 +00002117 (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
Daniel Jasper32d28ee2013-01-29 21:01:14 +00002118}
2119
Daniel Jasperbf71ba22013-04-08 20:33:42 +00002120void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
2121 llvm::errs() << "AnnotatedTokens:\n";
Manuel Klimekb3987012013-05-29 14:47:47 +00002122 const FormatToken *Tok = Line.First;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00002123 while (Tok) {
Manuel Klimekb3987012013-05-29 14:47:47 +00002124 llvm::errs() << " M=" << Tok->MustBreakBefore
Daniel Jasperc7bd68f2013-07-10 14:02:49 +00002125 << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
2126 << " S=" << Tok->SpacesRequiredBefore
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002127 << " B=" << Tok->BlockParameterCount
Daniel Jasperc7bd68f2013-07-10 14:02:49 +00002128 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
Manuel Klimekae76f7f2013-10-11 21:25:45 +00002129 << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
2130 << " FakeLParens=";
Daniel Jasperbf71ba22013-04-08 20:33:42 +00002131 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
2132 llvm::errs() << Tok->FakeLParens[i] << "/";
2133 llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002134 if (!Tok->Next)
Manuel Klimekae76f7f2013-10-11 21:25:45 +00002135 assert(Tok == Line.Last);
Manuel Klimekb3987012013-05-29 14:47:47 +00002136 Tok = Tok->Next;
Daniel Jasperbf71ba22013-04-08 20:33:42 +00002137 }
2138 llvm::errs() << "----\n";
2139}
2140
Daniel Jasper32d28ee2013-01-29 21:01:14 +00002141} // namespace format
2142} // namespace clang