blob: 1056d434200c4a02b845983ea04e5ade02dd97d3 [file] [log] [blame]
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001//===--- TokenAnnotator.cpp - Format C++ code -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file implements a token annotator, i.e. creates
12/// \c AnnotatedTokens out of \c FormatTokens with required extra information.
13///
14//===----------------------------------------------------------------------===//
15
16#include "TokenAnnotator.h"
17#include "clang/Basic/SourceManager.h"
Daniel Jasper6bee6822013-04-08 20:33:42 +000018#include "llvm/Support/Debug.h"
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000019
Chandler Carruth57f5fbe2014-04-21 22:55:36 +000020#define DEBUG_TYPE "format-token-annotator"
21
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000022namespace clang {
23namespace format {
24
Craig Topper318ed7c2013-07-01 04:03:19 +000025namespace {
26
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000027/// \brief A parser that gathers additional information about tokens.
28///
Alexander Kornienkoa5151272013-03-12 16:28:18 +000029/// The \c TokenAnnotator tries to match parenthesis and square brakets and
Daniel Jasper7a6d09b2013-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 Jasper8de9ed02013-08-22 15:00:41 +000034 AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
35 IdentifierInfo &Ident_in)
36 : Style(Style), Line(Line), CurrentToken(Line.First),
Daniel Jasperbc5cb4e2013-11-07 17:43:07 +000037 KeywordVirtualFound(false), AutoFound(false), Ident_in(Ident_in) {
Nico Weber9096fc02013-06-26 00:30:14 +000038 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
Manuel Klimek819788d2014-03-18 11:22:45 +000039 resetTokenMetadata(CurrentToken);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000040 }
41
Nico Weber44449172013-02-12 16:17:07 +000042private:
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000043 bool parseAngle() {
Craig Topper2145bc02014-05-09 08:15:10 +000044 if (!CurrentToken)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000045 return false;
Daniel Jasper40aacf42013-03-14 13:45:21 +000046 ScopedContextCreator ContextCreator(*this, tok::less, 10);
Manuel Klimek6e6310e2013-05-29 14:47:47 +000047 FormatToken *Left = CurrentToken->Previous;
Daniel Jasperc697ad22013-02-06 10:05:46 +000048 Contexts.back().IsExpression = false;
Manuel Klimekf81e5c02014-03-27 11:17:36 +000049 // If there's a template keyword before the opening angle bracket, this is a
50 // template parameter, not an argument.
51 Contexts.back().InTemplateArgument =
Craig Topper2145bc02014-05-09 08:15:10 +000052 Left->Previous && Left->Previous->Tok.isNot(tok::kw_template);
Manuel Klimekf81e5c02014-03-27 11:17:36 +000053
Daniel Jasper16b107e2014-10-21 09:57:09 +000054 if (Style.Language == FormatStyle::LK_Java &&
55 CurrentToken->is(tok::question))
56 next();
57
Craig Topper2145bc02014-05-09 08:15:10 +000058 while (CurrentToken) {
Daniel Jasper7a6d09b2013-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 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +000066 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
Daniel Jasper7bd618f2014-11-02 21:52:57 +000067 tok::colon))
68 return false;
69 if (CurrentToken->is(tok::question) &&
70 Style.Language != FormatStyle::LK_Java)
Daniel Jasper6f05e592013-05-15 13:46:48 +000071 return false;
Daniel Jasperd5893912013-06-01 18:56:00 +000072 // If a && or || is found and interpreted as a binary operator, this set
Daniel Jasper1027c6e2013-06-03 16:16:41 +000073 // of angles is likely part of something like "a < b && c > d". If the
Daniel Jasperd5893912013-06-01 18:56:00 +000074 // angles are inside an expression, the ||/&& might also be a binary
75 // operator that was misinterpreted because we are parsing template
76 // parameters.
77 // FIXME: This is getting out of hand, write a decent parser.
Manuel Klimek6e6310e2013-05-29 14:47:47 +000078 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
Daniel Jasper6ba16382014-07-28 13:19:58 +000079 CurrentToken->Previous->Type == TT_BinaryOperator &&
80 Contexts[Contexts.size() - 2].IsExpression &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +000081 Line.First->isNot(tok::kw_template))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000082 return false;
Daniel Jaspere11095a2013-02-14 15:01:34 +000083 updateParameterCount(Left, CurrentToken);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000084 if (!consumeToken())
85 return false;
86 }
87 return false;
88 }
89
90 bool parseParens(bool LookForDecls = false) {
Craig Topper2145bc02014-05-09 08:15:10 +000091 if (!CurrentToken)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000092 return false;
Daniel Jasper40aacf42013-03-14 13:45:21 +000093 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
Daniel Jasperc697ad22013-02-06 10:05:46 +000094
95 // FIXME: This is a bit of a hack. Do better.
96 Contexts.back().ColonIsForRangeExpr =
97 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
98
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000099 bool StartsObjCMethodExpr = false;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000100 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000101 if (CurrentToken->is(tok::caret)) {
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000102 // (^ can start a block type.
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000103 Left->Type = TT_ObjCBlockLParen;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000104 } else if (FormatToken *MaybeSel = Left->Previous) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000105 // @selector( starts a selector.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000106 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
107 MaybeSel->Previous->is(tok::at)) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000108 StartsObjCMethodExpr = true;
109 }
110 }
111
Daniel Jaspera65e8872014-03-25 10:52:45 +0000112 if (Left->Previous &&
113 (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_if,
114 tok::kw_while, tok::l_paren, tok::comma) ||
115 Left->Previous->Type == TT_BinaryOperator)) {
Daniel Jasperd46e07e2013-10-20 18:15:30 +0000116 // static_assert, if and while usually contain expressions.
Daniel Jasper8b1c6352013-08-01 17:58:23 +0000117 Contexts.back().IsExpression = true;
Daniel Jasper72ab43b2014-04-14 12:50:02 +0000118 } else if (Line.InPPDirective &&
Daniel Jasper866468a2014-04-14 13:15:29 +0000119 (!Left->Previous ||
120 (Left->Previous->isNot(tok::identifier) &&
121 Left->Previous->Type != TT_OverloadedOperator))) {
Daniel Jasper72ab43b2014-04-14 12:50:02 +0000122 Contexts.back().IsExpression = true;
Daniel Jasperd46e07e2013-10-20 18:15:30 +0000123 } else if (Left->Previous && Left->Previous->is(tok::r_square) &&
124 Left->Previous->MatchingParen &&
125 Left->Previous->MatchingParen->Type == TT_LambdaLSquare) {
126 // This is a parameter list of a lambda expression.
127 Contexts.back().IsExpression = false;
Daniel Jasperc13ee342014-03-27 09:43:54 +0000128 } else if (Contexts[Contexts.size() - 2].CaretFound) {
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000129 // This is the parameter list of an ObjC block.
130 Contexts.back().IsExpression = false;
Daniel Jasper559b63c2014-01-28 20:13:43 +0000131 } else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
132 Left->Type = TT_AttributeParen;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000133 } else if (Left->Previous && Left->Previous->IsForEachMacro) {
134 // The first argument to a foreach macro is a declaration.
135 Contexts.back().IsForEachMacro = true;
136 Contexts.back().IsExpression = false;
Daniel Jasperf1f267b2014-10-21 07:57:50 +0000137 } else if (Left->Previous && Left->Previous->MatchingParen &&
138 Left->Previous->MatchingParen->Type == TT_ObjCBlockLParen) {
139 Contexts.back().IsExpression = false;
Daniel Jasperd46e07e2013-10-20 18:15:30 +0000140 }
Daniel Jasper8b1c6352013-08-01 17:58:23 +0000141
Daniel Jasperc697ad22013-02-06 10:05:46 +0000142 if (StartsObjCMethodExpr) {
143 Contexts.back().ColonIsObjCMethodExpr = true;
144 Left->Type = TT_ObjCMethodExpr;
145 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000146
Daniel Jasper37194282013-05-28 08:33:00 +0000147 bool MightBeFunctionType = CurrentToken->is(tok::star);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000148 bool HasMultipleLines = false;
149 bool HasMultipleParametersOnALine = false;
Craig Topper2145bc02014-05-09 08:15:10 +0000150 while (CurrentToken) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000151 // LookForDecls is set when "if (" has been seen. Check for
152 // 'identifier' '*' 'identifier' followed by not '=' -- this
153 // '*' has to be a binary operator but determineStarAmpUsage() will
154 // categorize it as an unary operator, so set the right type here.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000155 if (LookForDecls && CurrentToken->Next) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000156 FormatToken *Prev = CurrentToken->getPreviousNonComment();
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000157 if (Prev) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000158 FormatToken *PrevPrev = Prev->getPreviousNonComment();
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000159 FormatToken *Next = CurrentToken->Next;
160 if (PrevPrev && PrevPrev->is(tok::identifier) &&
161 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
162 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
163 Prev->Type = TT_BinaryOperator;
164 LookForDecls = false;
165 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000166 }
167 }
168
Daniel Jasper59036852013-08-12 12:16:34 +0000169 if (CurrentToken->Previous->Type == TT_PointerOrReference &&
170 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
171 tok::coloncolon))
172 MightBeFunctionType = true;
Daniel Jasper91beebd2014-06-30 13:44:47 +0000173 if (CurrentToken->Previous->Type == TT_BinaryOperator)
174 Contexts.back().IsExpression = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000175 if (CurrentToken->is(tok::r_paren)) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000176 if (MightBeFunctionType && CurrentToken->Next &&
Daniel Jasper655d96a2013-07-16 11:37:21 +0000177 (CurrentToken->Next->is(tok::l_paren) ||
178 (CurrentToken->Next->is(tok::l_square) &&
179 !Contexts.back().IsExpression)))
Daniel Jasper37194282013-05-28 08:33:00 +0000180 Left->Type = TT_FunctionTypeLParen;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000181 Left->MatchingParen = CurrentToken;
182 CurrentToken->MatchingParen = Left;
183
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000184 if (StartsObjCMethodExpr) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000185 CurrentToken->Type = TT_ObjCMethodExpr;
Craig Topper2145bc02014-05-09 08:15:10 +0000186 if (Contexts.back().FirstObjCSelectorName) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000187 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
188 Contexts.back().LongestObjCSelectorName;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000189 }
190 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000191
Daniel Jasper559b63c2014-01-28 20:13:43 +0000192 if (Left->Type == TT_AttributeParen)
193 CurrentToken->Type = TT_AttributeParen;
Daniel Jasperf1f0c352014-10-21 09:25:39 +0000194 if (Left->Previous && Left->Previous->Type == TT_JavaAnnotation)
195 CurrentToken->Type = TT_JavaAnnotation;
Daniel Jaspere9ab42d2014-10-31 18:23:49 +0000196 if (Left->Previous && Left->Previous->Type == TT_LeadingJavaAnnotation)
197 CurrentToken->Type = TT_LeadingJavaAnnotation;
Daniel Jasper559b63c2014-01-28 20:13:43 +0000198
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000199 if (!HasMultipleLines)
200 Left->PackingKind = PPK_Inconclusive;
201 else if (HasMultipleParametersOnALine)
202 Left->PackingKind = PPK_BinPacked;
203 else
204 Left->PackingKind = PPK_OnePerLine;
205
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000206 next();
207 return true;
208 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000209 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000210 return false;
Daniel Jasper31745732014-01-19 07:46:32 +0000211 else if (CurrentToken->is(tok::l_brace))
212 Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000213 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
214 !CurrentToken->Next->HasUnescapedNewline &&
215 !CurrentToken->Next->isTrailingComment())
216 HasMultipleParametersOnALine = true;
Daniel Jaspercc7bf7f2014-04-03 09:00:49 +0000217 if (CurrentToken->isOneOf(tok::kw_const, tok::kw_auto) ||
Daniel Jasperc580af92014-03-11 09:29:46 +0000218 CurrentToken->isSimpleTypeSpecifier())
219 Contexts.back().IsExpression = false;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000220 FormatToken *Tok = CurrentToken;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000221 if (!consumeToken())
222 return false;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000223 updateParameterCount(Left, Tok);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000224 if (CurrentToken && CurrentToken->HasUnescapedNewline)
225 HasMultipleLines = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000226 }
227 return false;
228 }
229
230 bool parseSquare() {
231 if (!CurrentToken)
232 return false;
233
Alexander Kornienkoafaa8f52013-06-17 13:19:53 +0000234 // A '[' could be an index subscript (after an identifier or after
Nico Weber2a726b62013-02-10 02:08:05 +0000235 // ')' or ']'), it could be the start of an Objective-C method
236 // expression, or it could the the start of an Objective-C array literal.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000237 FormatToken *Left = CurrentToken->Previous;
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000238 FormatToken *Parent = Left->getPreviousNonComment();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000239 bool StartsObjCMethodExpr =
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000240 Contexts.back().CanBeExpression && Left->Type != TT_LambdaLSquare &&
Daniel Jasper89519082014-05-09 10:26:08 +0000241 CurrentToken->isNot(tok::l_brace) &&
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000242 (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
243 tok::kw_return, tok::kw_throw) ||
Daniel Jasperc04baae2013-04-10 09:49:49 +0000244 Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn ||
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000245 Parent->Type == TT_CastRParen ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000246 getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
Daniel Jasper40aacf42013-03-14 13:45:21 +0000247 ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
Daniel Jasper97b89482013-03-13 07:49:51 +0000248 Contexts.back().IsExpression = true;
Daniel Jaspera1ea4cb2013-10-26 17:00:22 +0000249 bool ColonFound = false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000250
Daniel Jasperc697ad22013-02-06 10:05:46 +0000251 if (StartsObjCMethodExpr) {
252 Contexts.back().ColonIsObjCMethodExpr = true;
253 Left->Type = TT_ObjCMethodExpr;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000254 } else if (Parent && Parent->is(tok::at)) {
255 Left->Type = TT_ArrayInitializerLSquare;
256 } else if (Left->Type == TT_Unknown) {
257 Left->Type = TT_ArraySubscriptLSquare;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000258 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000259
Craig Topper2145bc02014-05-09 08:15:10 +0000260 while (CurrentToken) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000261 if (CurrentToken->is(tok::r_square)) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000262 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
263 Left->Type == TT_ObjCMethodExpr) {
Nico Weber5d2624e2013-02-06 06:20:11 +0000264 // An ObjC method call is rarely followed by an open parenthesis.
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000265 // FIXME: Do we incorrectly label ":" with this?
266 StartsObjCMethodExpr = false;
267 Left->Type = TT_Unknown;
268 }
Daniel Jasper139d4a32014-04-08 13:07:41 +0000269 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000270 CurrentToken->Type = TT_ObjCMethodExpr;
Nico Weber5d2624e2013-02-06 06:20:11 +0000271 // determineStarAmpUsage() thinks that '*' '[' is allocating an
272 // array of pointers, but if '[' starts a selector then '*' is a
273 // binary operator.
Craig Topper2145bc02014-05-09 08:15:10 +0000274 if (Parent && Parent->Type == TT_PointerOrReference)
Nico Weberac9bde22013-02-06 16:54:35 +0000275 Parent->Type = TT_BinaryOperator;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000276 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000277 Left->MatchingParen = CurrentToken;
278 CurrentToken->MatchingParen = Left;
Craig Topper2145bc02014-05-09 08:15:10 +0000279 if (Contexts.back().FirstObjCSelectorName) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000280 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
281 Contexts.back().LongestObjCSelectorName;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000282 if (Left->BlockParameterCount > 1)
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000283 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
284 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000285 next();
286 return true;
287 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000288 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000289 return false;
Daniel Jasperea772b4d2014-10-16 08:38:51 +0000290 if (CurrentToken->is(tok::colon)) {
291 if (Left->Type == TT_ArraySubscriptLSquare) {
292 Left->Type = TT_ObjCMethodExpr;
293 StartsObjCMethodExpr = true;
294 Contexts.back().ColonIsObjCMethodExpr = true;
295 if (Parent && Parent->is(tok::r_paren))
296 Parent->Type = TT_CastRParen;
297 }
Daniel Jaspera1ea4cb2013-10-26 17:00:22 +0000298 ColonFound = true;
Daniel Jasperea772b4d2014-10-16 08:38:51 +0000299 }
Daniel Jasper1db6c382013-10-22 15:30:28 +0000300 if (CurrentToken->is(tok::comma) &&
Daniel Jaspera0e9be22014-01-28 18:51:11 +0000301 Style.Language != FormatStyle::LK_Proto &&
Daniel Jasperb596fb22013-10-24 10:31:50 +0000302 (Left->Type == TT_ArraySubscriptLSquare ||
Daniel Jaspera1ea4cb2013-10-26 17:00:22 +0000303 (Left->Type == TT_ObjCMethodExpr && !ColonFound)))
Daniel Jasper1db6c382013-10-22 15:30:28 +0000304 Left->Type = TT_ArrayInitializerLSquare;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000305 FormatToken* Tok = CurrentToken;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000306 if (!consumeToken())
307 return false;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000308 updateParameterCount(Left, Tok);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000309 }
310 return false;
311 }
312
313 bool parseBrace() {
Craig Topper2145bc02014-05-09 08:15:10 +0000314 if (CurrentToken) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000315 FormatToken *Left = CurrentToken->Previous;
Daniel Jasperc13ee342014-03-27 09:43:54 +0000316
317 if (Contexts.back().CaretFound)
318 Left->Type = TT_ObjCBlockLBrace;
319 Contexts.back().CaretFound = false;
320
Daniel Jasperb596fb22013-10-24 10:31:50 +0000321 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
322 Contexts.back().ColonIsDictLiteral = true;
Daniel Jasper39485162014-05-22 09:00:33 +0000323 if (Left->BlockKind == BK_BracedInit)
324 Contexts.back().IsExpression = true;
Nico Weberced7d412013-05-26 05:39:26 +0000325
Craig Topper2145bc02014-05-09 08:15:10 +0000326 while (CurrentToken) {
Daniel Jasper8e357692013-05-06 08:27:33 +0000327 if (CurrentToken->is(tok::r_brace)) {
328 Left->MatchingParen = CurrentToken;
329 CurrentToken->MatchingParen = Left;
330 next();
331 return true;
332 }
333 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
334 return false;
335 updateParameterCount(Left, CurrentToken);
Daniel Jasper497d9fd2014-08-15 05:00:35 +0000336 if (CurrentToken->isOneOf(tok::colon, tok::l_brace)) {
337 FormatToken *Previous = CurrentToken->getPreviousNonComment();
Daniel Jasper168c8aa2014-08-27 11:53:26 +0000338 if ((CurrentToken->is(tok::colon) ||
339 Style.Language == FormatStyle::LK_Proto) &&
340 Previous->is(tok::identifier))
Daniel Jasper497d9fd2014-08-15 05:00:35 +0000341 Previous->Type = TT_SelectorName;
342 if (CurrentToken->is(tok::colon))
343 Left->Type = TT_DictLiteral;
Daniel Jasper17062ff2014-06-10 14:44:02 +0000344 }
Daniel Jasper8e357692013-05-06 08:27:33 +0000345 if (!consumeToken())
346 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000347 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000348 }
349 return true;
350 }
Daniel Jasperdc7d5812013-02-20 12:56:39 +0000351
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000352 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000353 if (Current->Type == TT_LambdaLSquare ||
354 (Current->is(tok::caret) && Current->Type == TT_UnaryOperator) ||
355 (Style.Language == FormatStyle::LK_JavaScript &&
356 Current->TokenText == "function")) {
357 ++Left->BlockParameterCount;
358 }
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000359 if (Current->is(tok::comma)) {
Daniel Jaspere11095a2013-02-14 15:01:34 +0000360 ++Left->ParameterCount;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000361 if (!Left->Role)
362 Left->Role.reset(new CommaSeparatedList(Style));
363 Left->Role->CommaFound(Current);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000364 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
Daniel Jaspere11095a2013-02-14 15:01:34 +0000365 Left->ParameterCount = 1;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000366 }
Daniel Jaspere11095a2013-02-14 15:01:34 +0000367 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000368
369 bool parseConditional() {
Daniel Jasper7bd618f2014-11-02 21:52:57 +0000370 if (Style.Language == FormatStyle::LK_Java &&
371 CurrentToken->isOneOf(tok::comma, tok::greater))
372 return true; // This is a generic "?".
373
Craig Topper2145bc02014-05-09 08:15:10 +0000374 while (CurrentToken) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000375 if (CurrentToken->is(tok::colon)) {
376 CurrentToken->Type = TT_ConditionalExpr;
377 next();
378 return true;
379 }
380 if (!consumeToken())
381 return false;
382 }
383 return false;
384 }
385
386 bool parseTemplateDeclaration() {
Craig Topper2145bc02014-05-09 08:15:10 +0000387 if (CurrentToken && CurrentToken->is(tok::less)) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000388 CurrentToken->Type = TT_TemplateOpener;
389 next();
390 if (!parseAngle())
391 return false;
Craig Topper2145bc02014-05-09 08:15:10 +0000392 if (CurrentToken)
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000393 CurrentToken->Previous->ClosesTemplateDeclaration = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000394 return true;
395 }
396 return false;
397 }
398
399 bool consumeToken() {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000400 FormatToken *Tok = CurrentToken;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000401 next();
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000402 switch (Tok->Tok.getKind()) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000403 case tok::plus:
404 case tok::minus:
Craig Topper2145bc02014-05-09 08:15:10 +0000405 if (!Tok->Previous && Line.MustBeDeclaration)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000406 Tok->Type = TT_ObjCMethodSpecifier;
407 break;
408 case tok::colon:
Craig Topper2145bc02014-05-09 08:15:10 +0000409 if (!Tok->Previous)
Daniel Jasper850677d2013-03-18 12:50:26 +0000410 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000411 // Colons from ?: are handled in parseConditional().
Daniel Jasper031e2402014-04-28 07:48:36 +0000412 if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1 &&
413 Line.First->isNot(tok::kw_case)) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000414 Tok->Type = TT_CtorInitializerColon;
Daniel Jasperb596fb22013-10-24 10:31:50 +0000415 } else if (Contexts.back().ColonIsDictLiteral) {
416 Tok->Type = TT_DictLiteral;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000417 } else if (Contexts.back().ColonIsObjCMethodExpr ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000418 Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000419 Tok->Type = TT_ObjCMethodExpr;
Daniel Jasper17062ff2014-06-10 14:44:02 +0000420 Tok->Previous->Type = TT_SelectorName;
Alexander Kornienko39856b72013-09-10 09:38:25 +0000421 if (Tok->Previous->ColumnWidth >
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000422 Contexts.back().LongestObjCSelectorName) {
Alexander Kornienko60d1b042013-10-10 13:36:20 +0000423 Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000424 }
Craig Topper2145bc02014-05-09 08:15:10 +0000425 if (!Contexts.back().FirstObjCSelectorName)
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000426 Contexts.back().FirstObjCSelectorName = Tok->Previous;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000427 } else if (Contexts.back().ColonIsForRangeExpr) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000428 Tok->Type = TT_RangeBasedForLoopColon;
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000429 } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
Alexander Kornienko60d1b042013-10-10 13:36:20 +0000430 Tok->Type = TT_BitFieldColon;
Daniel Jasperd39312ec2014-05-28 10:09:11 +0000431 } else if (Contexts.size() == 1 &&
432 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
Daniel Jaspereead02b2013-02-14 08:42:54 +0000433 Tok->Type = TT_InheritanceColon;
Daniel Jasper78580792014-10-20 12:01:45 +0000434 } else if (Tok->Previous->is(tok::identifier) && Tok->Next &&
435 Tok->Next->isOneOf(tok::r_paren, tok::comma)) {
436 // This handles a special macro in ObjC code where selectors including
437 // the colon are passed as macro arguments.
438 Tok->Type = TT_ObjCMethodExpr;
Daniel Jasper40aacf42013-03-14 13:45:21 +0000439 } else if (Contexts.back().ContextKind == tok::l_paren) {
440 Tok->Type = TT_InlineASMColon;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000441 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000442 break;
443 case tok::kw_if:
444 case tok::kw_while:
Craig Topper2145bc02014-05-09 08:15:10 +0000445 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000446 next();
Nico Weber9096fc02013-06-26 00:30:14 +0000447 if (!parseParens(/*LookForDecls=*/true))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000448 return false;
449 }
450 break;
451 case tok::kw_for:
Daniel Jasperc697ad22013-02-06 10:05:46 +0000452 Contexts.back().ColonIsForRangeExpr = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000453 next();
454 if (!parseParens())
455 return false;
456 break;
457 case tok::l_paren:
458 if (!parseParens())
459 return false;
Daniel Jasperbc5cb4e2013-11-07 17:43:07 +0000460 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
Daniel Jasperf10a28d2014-05-05 13:48:09 +0000461 !Contexts.back().IsExpression &&
462 Line.First->Type != TT_ObjCProperty &&
463 (!Tok->Previous || Tok->Previous->isNot(tok::kw_decltype)))
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000464 Line.MightBeFunctionDecl = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000465 break;
466 case tok::l_square:
467 if (!parseSquare())
468 return false;
469 break;
470 case tok::l_brace:
471 if (!parseBrace())
472 return false;
473 break;
474 case tok::less:
Daniel Jasper62c0ac02013-07-30 22:37:19 +0000475 if (Tok->Previous && !Tok->Previous->Tok.isLiteral() && parseAngle())
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000476 Tok->Type = TT_TemplateOpener;
477 else {
478 Tok->Type = TT_BinaryOperator;
479 CurrentToken = Tok;
480 next();
481 }
482 break;
483 case tok::r_paren:
484 case tok::r_square:
485 return false;
486 case tok::r_brace:
487 // Lines can start with '}'.
Craig Topper2145bc02014-05-09 08:15:10 +0000488 if (Tok->Previous)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000489 return false;
490 break;
491 case tok::greater:
492 Tok->Type = TT_BinaryOperator;
493 break;
494 case tok::kw_operator:
Daniel Jasper42401c82013-09-02 09:20:39 +0000495 while (CurrentToken &&
496 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000497 if (CurrentToken->isOneOf(tok::star, tok::amp))
Daniel Jasper35d2dc72013-02-11 08:01:18 +0000498 CurrentToken->Type = TT_PointerOrReference;
499 consumeToken();
Daniel Jasper42401c82013-09-02 09:20:39 +0000500 if (CurrentToken && CurrentToken->Previous->Type == TT_BinaryOperator)
Daniel Jasperd215b8b2013-08-28 07:27:35 +0000501 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000502 }
Daniel Jasper8f9624b2013-05-10 07:59:58 +0000503 if (CurrentToken) {
Daniel Jasper35d2dc72013-02-11 08:01:18 +0000504 CurrentToken->Type = TT_OverloadedOperatorLParen;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000505 if (CurrentToken->Previous->Type == TT_BinaryOperator)
506 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper8f9624b2013-05-10 07:59:58 +0000507 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000508 break;
509 case tok::question:
510 parseConditional();
511 break;
512 case tok::kw_template:
513 parseTemplateDeclaration();
514 break;
Nico Weber29f9dea2013-02-11 15:32:15 +0000515 case tok::identifier:
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000516 if (Line.First->is(tok::kw_for) &&
517 Tok->Tok.getIdentifierInfo() == &Ident_in)
Nico Weber29f9dea2013-02-11 15:32:15 +0000518 Tok->Type = TT_ObjCForIn;
519 break;
Daniel Jaspera628c982013-04-03 13:36:17 +0000520 case tok::comma:
521 if (Contexts.back().FirstStartOfName)
522 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000523 if (Contexts.back().InCtorInitializer)
524 Tok->Type = TT_CtorInitializerComma;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000525 if (Contexts.back().IsForEachMacro)
526 Contexts.back().IsExpression = true;
Daniel Jaspera628c982013-04-03 13:36:17 +0000527 break;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000528 default:
529 break;
530 }
531 return true;
532 }
533
534 void parseIncludeDirective() {
Craig Topper2145bc02014-05-09 08:15:10 +0000535 if (CurrentToken && CurrentToken->is(tok::less)) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000536 next();
Craig Topper2145bc02014-05-09 08:15:10 +0000537 while (CurrentToken) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000538 if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000539 CurrentToken->Type = TT_ImplicitStringLiteral;
540 next();
541 }
542 } else {
Craig Topper2145bc02014-05-09 08:15:10 +0000543 while (CurrentToken) {
Daniel Jasperaf5ba0e2013-02-23 07:46:38 +0000544 if (CurrentToken->is(tok::string_literal))
545 // Mark these string literals as "implicit" literals, too, so that
546 // they are not split or line-wrapped.
547 CurrentToken->Type = TT_ImplicitStringLiteral;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000548 next();
549 }
550 }
551 }
552
553 void parseWarningOrError() {
554 next();
555 // We still want to format the whitespace left of the first token of the
556 // warning or error.
557 next();
Craig Topper2145bc02014-05-09 08:15:10 +0000558 while (CurrentToken) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000559 CurrentToken->Type = TT_ImplicitStringLiteral;
560 next();
561 }
562 }
563
Daniel Jasperdc32c1b2014-01-09 13:56:49 +0000564 void parsePragma() {
565 next(); // Consume "pragma".
566 if (CurrentToken && CurrentToken->TokenText == "mark") {
567 next(); // Consume "mark".
568 next(); // Consume first token (so we fix leading whitespace).
Craig Topper2145bc02014-05-09 08:15:10 +0000569 while (CurrentToken) {
Daniel Jasperdc32c1b2014-01-09 13:56:49 +0000570 CurrentToken->Type = TT_ImplicitStringLiteral;
571 next();
572 }
573 }
574 }
575
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000576 void parsePreprocessorDirective() {
577 next();
Craig Topper2145bc02014-05-09 08:15:10 +0000578 if (!CurrentToken)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000579 return;
Alexander Kornienko384b40b2013-10-11 21:43:05 +0000580 if (CurrentToken->Tok.is(tok::numeric_constant)) {
581 CurrentToken->SpacesRequiredBefore = 1;
582 return;
583 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000584 // Hashes in the middle of a line can lead to any strange token
585 // sequence.
Craig Topper2145bc02014-05-09 08:15:10 +0000586 if (!CurrentToken->Tok.getIdentifierInfo())
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000587 return;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000588 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000589 case tok::pp_include:
590 case tok::pp_import:
Daniel Jasper343643b2014-08-13 08:29:18 +0000591 next();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000592 parseIncludeDirective();
593 break;
594 case tok::pp_error:
595 case tok::pp_warning:
596 parseWarningOrError();
597 break;
Daniel Jasperdc32c1b2014-01-09 13:56:49 +0000598 case tok::pp_pragma:
599 parsePragma();
600 break;
Daniel Jasper4431aa92013-04-23 13:54:04 +0000601 case tok::pp_if:
602 case tok::pp_elif:
Daniel Jasper7cfde412014-01-21 08:56:09 +0000603 Contexts.back().IsExpression = true;
Daniel Jasper4431aa92013-04-23 13:54:04 +0000604 parseLine();
605 break;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000606 default:
607 break;
608 }
Craig Topper2145bc02014-05-09 08:15:10 +0000609 while (CurrentToken)
Daniel Jaspera885dbe2013-02-05 09:34:14 +0000610 next();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000611 }
612
Nico Weber44449172013-02-12 16:17:07 +0000613public:
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000614 LineType parseLine() {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000615 if (CurrentToken->is(tok::hash)) {
616 parsePreprocessorDirective();
617 return LT_PreprocessorDirective;
618 }
Daniel Jasper7cfde412014-01-21 08:56:09 +0000619
Daniel Jasper47ef6dd2014-01-17 16:21:39 +0000620 // Directly allow to 'import <string-literal>' to support protocol buffer
621 // definitions (code.google.com/p/protobuf) or missing "#" (either way we
622 // should not break the line).
623 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
624 if (Info && Info->getPPKeywordID() == tok::pp_import &&
Daniel Jasper343643b2014-08-13 08:29:18 +0000625 CurrentToken->Next && CurrentToken->Next->is(tok::string_literal)) {
626 next();
Daniel Jasper47ef6dd2014-01-17 16:21:39 +0000627 parseIncludeDirective();
Daniel Jasper343643b2014-08-13 08:29:18 +0000628 return LT_Other;
629 }
630
631 // If this line starts and ends in '<' and '>', respectively, it is likely
632 // part of "#define <a/b.h>".
633 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
634 parseIncludeDirective();
635 return LT_Other;
636 }
Daniel Jasper47ef6dd2014-01-17 16:21:39 +0000637
Craig Topper2145bc02014-05-09 08:15:10 +0000638 while (CurrentToken) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000639 if (CurrentToken->is(tok::kw_virtual))
640 KeywordVirtualFound = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000641 if (!consumeToken())
642 return LT_Invalid;
643 }
644 if (KeywordVirtualFound)
645 return LT_VirtualFunctionDecl;
646
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000647 if (Line.First->Type == TT_ObjCMethodSpecifier) {
Craig Topper2145bc02014-05-09 08:15:10 +0000648 if (Contexts.back().FirstObjCSelectorName)
Daniel Jasperc697ad22013-02-06 10:05:46 +0000649 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
650 Contexts.back().LongestObjCSelectorName;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000651 return LT_ObjCMethodDecl;
652 }
653
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000654 return LT_Other;
655 }
656
Nico Weber44449172013-02-12 16:17:07 +0000657private:
Manuel Klimek819788d2014-03-18 11:22:45 +0000658 void resetTokenMetadata(FormatToken *Token) {
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000659 if (!Token)
660 return;
Manuel Klimek819788d2014-03-18 11:22:45 +0000661
662 // Reset token type in case we have already looked at it and then
663 // recovered from an error (e.g. failure to find the matching >).
664 if (CurrentToken->Type != TT_LambdaLSquare &&
665 CurrentToken->Type != TT_FunctionLBrace &&
666 CurrentToken->Type != TT_ImplicitStringLiteral &&
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000667 CurrentToken->Type != TT_RegexLiteral &&
Manuel Klimek819788d2014-03-18 11:22:45 +0000668 CurrentToken->Type != TT_TrailingReturnArrow)
669 CurrentToken->Type = TT_Unknown;
David Blaikie3875a822014-07-19 01:06:45 +0000670 CurrentToken->Role.reset();
Manuel Klimek819788d2014-03-18 11:22:45 +0000671 CurrentToken->FakeLParens.clear();
672 CurrentToken->FakeRParens = 0;
673 }
674
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000675 void next() {
Craig Topper2145bc02014-05-09 08:15:10 +0000676 if (CurrentToken) {
Daniel Jasper63af7c42013-12-09 14:40:19 +0000677 CurrentToken->NestingLevel = Contexts.size() - 1;
Daniel Jasper78b45332014-08-14 10:52:56 +0000678 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
679 determineTokenType(*CurrentToken);
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000680 CurrentToken = CurrentToken->Next;
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000681 }
Daniel Jasper5065bc42013-02-18 12:44:35 +0000682
Manuel Klimek819788d2014-03-18 11:22:45 +0000683 resetTokenMetadata(CurrentToken);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000684 }
685
Daniel Jasperc697ad22013-02-06 10:05:46 +0000686 /// \brief A struct to hold information valid in a specific context, e.g.
687 /// a pair of parenthesis.
688 struct Context {
Daniel Jasper40aacf42013-03-14 13:45:21 +0000689 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
690 bool IsExpression)
691 : ContextKind(ContextKind), BindingStrength(BindingStrength),
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000692 LongestObjCSelectorName(0), ColonIsForRangeExpr(false),
693 ColonIsDictLiteral(false), ColonIsObjCMethodExpr(false),
694 FirstObjCSelectorName(nullptr), FirstStartOfName(nullptr),
695 IsExpression(IsExpression), CanBeExpression(true),
696 InTemplateArgument(false), InCtorInitializer(false),
697 CaretFound(false), IsForEachMacro(false) {}
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000698
Daniel Jasper40aacf42013-03-14 13:45:21 +0000699 tok::TokenKind ContextKind;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000700 unsigned BindingStrength;
701 unsigned LongestObjCSelectorName;
702 bool ColonIsForRangeExpr;
Daniel Jasperb596fb22013-10-24 10:31:50 +0000703 bool ColonIsDictLiteral;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000704 bool ColonIsObjCMethodExpr;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000705 FormatToken *FirstObjCSelectorName;
706 FormatToken *FirstStartOfName;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000707 bool IsExpression;
Daniel Jasper97b89482013-03-13 07:49:51 +0000708 bool CanBeExpression;
Manuel Klimekf81e5c02014-03-27 11:17:36 +0000709 bool InTemplateArgument;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000710 bool InCtorInitializer;
Daniel Jaspera225bce2014-01-16 19:14:34 +0000711 bool CaretFound;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000712 bool IsForEachMacro;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000713 };
714
715 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
716 /// of each instance.
717 struct ScopedContextCreator {
718 AnnotatingParser &P;
719
Daniel Jasper40aacf42013-03-14 13:45:21 +0000720 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
721 unsigned Increase)
722 : P(P) {
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000723 P.Contexts.push_back(Context(ContextKind,
724 P.Contexts.back().BindingStrength + Increase,
725 P.Contexts.back().IsExpression));
Daniel Jasperc697ad22013-02-06 10:05:46 +0000726 }
727
728 ~ScopedContextCreator() { P.Contexts.pop_back(); }
729 };
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000730
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000731 void determineTokenType(FormatToken &Current) {
732 if (Current.getPrecedence() == prec::Assignment &&
Daniel Jasper59036852013-08-12 12:16:34 +0000733 !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000734 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000735 Contexts.back().IsExpression = true;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000736 for (FormatToken *Previous = Current.Previous;
Daniel Jasper5ca9b712013-09-11 20:37:10 +0000737 Previous && !Previous->isOneOf(tok::comma, tok::semi);
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000738 Previous = Previous->Previous) {
Manuel Klimek5f594f82014-08-13 14:00:41 +0000739 if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
Daniel Jasper8e559272013-02-27 11:43:50 +0000740 Previous = Previous->MatchingParen;
Manuel Klimek5f594f82014-08-13 14:00:41 +0000741 if (!Previous)
742 break;
743 }
Daniel Jasper8184d662014-07-28 12:24:21 +0000744 if ((Previous->Type == TT_BinaryOperator ||
745 Previous->Type == TT_UnaryOperator) &&
Daniel Jasper1904e9b2014-08-14 10:53:19 +0000746 Previous->isOneOf(tok::star, tok::amp) && Previous->Previous &&
747 Previous->Previous->isNot(tok::equal)) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000748 Previous->Type = TT_PointerOrReference;
749 }
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000750 }
Daniel Jasper3682fcd2013-12-16 08:36:18 +0000751 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000752 Contexts.back().IsExpression = true;
Daniel Jasper3682fcd2013-12-16 08:36:18 +0000753 } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
Dinesh Dwivedi2e92e662014-05-06 11:46:49 +0000754 !Line.InPPDirective &&
755 (!Current.Previous ||
756 Current.Previous->isNot(tok::kw_decltype))) {
Daniel Jasper3682fcd2013-12-16 08:36:18 +0000757 bool ParametersOfFunctionType =
758 Current.Previous && Current.Previous->is(tok::r_paren) &&
759 Current.Previous->MatchingParen &&
760 Current.Previous->MatchingParen->Type == TT_FunctionTypeLParen;
761 bool IsForOrCatch = Current.Previous &&
762 Current.Previous->isOneOf(tok::kw_for, tok::kw_catch);
763 Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000764 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000765 for (FormatToken *Previous = Current.Previous;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000766 Previous && Previous->isOneOf(tok::star, tok::amp);
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000767 Previous = Previous->Previous)
Nico Weber44449172013-02-12 16:17:07 +0000768 Previous->Type = TT_PointerOrReference;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000769 } else if (Current.Previous &&
770 Current.Previous->Type == TT_CtorInitializerColon) {
Daniel Jasper5065bc42013-02-18 12:44:35 +0000771 Contexts.back().IsExpression = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000772 Contexts.back().InCtorInitializer = true;
Daniel Jasper97b89482013-03-13 07:49:51 +0000773 } else if (Current.is(tok::kw_new)) {
774 Contexts.back().CanBeExpression = false;
Daniel Jaspera98da3d2013-11-07 19:56:07 +0000775 } else if (Current.is(tok::semi) || Current.is(tok::exclaim)) {
Daniel Jasperc37de302013-05-03 14:41:24 +0000776 // This should be the condition or increment in a for-loop.
777 Contexts.back().IsExpression = true;
Nico Weber44449172013-02-12 16:17:07 +0000778 }
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000779
780 if (Current.Type == TT_Unknown) {
Daniel Jasperf3167902013-09-27 08:29:16 +0000781 // Line.MightBeFunctionDecl can only be true after the parentheses of a
782 // function declaration have been found. In this case, 'Current' is a
783 // trailing token of this declaration and thus cannot be a name.
Daniel Jasper78b45332014-08-14 10:52:56 +0000784 if (isStartOfName(Current) &&
785 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
Daniel Jaspera628c982013-04-03 13:36:17 +0000786 Contexts.back().FirstStartOfName = &Current;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000787 Current.Type = TT_StartOfName;
Daniel Jasper6cdec7c2013-07-09 14:36:48 +0000788 } else if (Current.is(tok::kw_auto)) {
789 AutoFound = true;
Daniel Jaspera3501d42013-07-11 14:33:06 +0000790 } else if (Current.is(tok::arrow) && AutoFound &&
Daniel Jaspere8a49392014-10-22 08:42:58 +0000791 Line.MustBeDeclaration && Current.NestingLevel == 0) {
Daniel Jasper6cdec7c2013-07-09 14:36:48 +0000792 Current.Type = TT_TrailingReturnArrow;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000793 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000794 Current.Type =
Daniel Jasper6f9c8d22013-07-05 13:30:40 +0000795 determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
Manuel Klimekf81e5c02014-03-27 11:17:36 +0000796 Contexts.back().IsExpression,
797 Contexts.back().InTemplateArgument);
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000798 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000799 Current.Type = determinePlusMinusCaretUsage(Current);
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000800 if (Current.Type == TT_UnaryOperator && Current.is(tok::caret))
Daniel Jasperb77105d2014-04-08 14:04:31 +0000801 Contexts.back().CaretFound = true;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000802 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000803 Current.Type = determineIncrementUsage(Current);
Daniel Jasper2ac3fdf2014-07-28 12:08:16 +0000804 } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000805 Current.Type = TT_UnaryOperator;
Daniel Jasperc0d606a2014-04-14 11:08:45 +0000806 } else if (Current.is(tok::question)) {
807 Current.Type = TT_ConditionalExpr;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000808 } else if (Current.isBinaryOperator() &&
809 (!Current.Previous ||
810 Current.Previous->isNot(tok::l_square))) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000811 Current.Type = TT_BinaryOperator;
812 } else if (Current.is(tok::comment)) {
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000813 if (Current.TokenText.startswith("//"))
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000814 Current.Type = TT_LineComment;
815 else
816 Current.Type = TT_BlockComment;
Nico Weberc6fe2162013-02-13 04:13:13 +0000817 } else if (Current.is(tok::r_paren)) {
Dinesh Dwivedi13b9b7e2014-05-06 09:08:34 +0000818 if (rParenEndsCast(Current))
Nico Weberc6fe2162013-02-13 04:13:13 +0000819 Current.Type = TT_CastRParen;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000820 } else if (Current.is(tok::at) && Current.Next) {
821 switch (Current.Next->Tok.getObjCKeywordID()) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000822 case tok::objc_interface:
823 case tok::objc_implementation:
824 case tok::objc_protocol:
825 Current.Type = TT_ObjCDecl;
826 break;
827 case tok::objc_property:
828 Current.Type = TT_ObjCProperty;
829 break;
830 default:
831 break;
832 }
Daniel Jasperbca4bbe2013-05-28 11:30:49 +0000833 } else if (Current.is(tok::period)) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000834 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
Daniel Jasperbca4bbe2013-05-28 11:30:49 +0000835 if (PreviousNoComment &&
836 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
837 Current.Type = TT_DesignatedInitializerPeriod;
Daniel Jasper43e6a282013-12-16 15:01:54 +0000838 } else if (Current.isOneOf(tok::identifier, tok::kw_const) &&
Daniel Jasperd78c4222014-10-21 11:17:56 +0000839 Current.Previous &&
840 !Current.Previous->isOneOf(tok::equal, tok::at) &&
Daniel Jasper43e6a282013-12-16 15:01:54 +0000841 Line.MightBeFunctionDecl && Contexts.size() == 1) {
842 // Line.MightBeFunctionDecl can only be true after the parentheses of a
843 // function declaration have been found.
844 Current.Type = TT_TrailingAnnotation;
Daniel Jasperfab69ff2014-10-21 08:24:18 +0000845 } else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
846 Current.Previous->is(tok::at)) {
Daniel Jaspere9ab42d2014-10-31 18:23:49 +0000847 const FormatToken& AtToken = *Current.Previous;
848 if (!AtToken.Previous ||
849 AtToken.Previous->Type == TT_LeadingJavaAnnotation)
850 Current.Type = TT_LeadingJavaAnnotation;
851 else
852 Current.Type = TT_JavaAnnotation;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000853 }
854 }
855 }
856
Daniel Jasperdba1c552013-07-02 09:47:29 +0000857 /// \brief Take a guess at whether \p Tok starts a name of a function or
858 /// variable declaration.
859 ///
860 /// This is a heuristic based on whether \p Tok is an identifier following
861 /// something that is likely a type.
862 bool isStartOfName(const FormatToken &Tok) {
Craig Topper2145bc02014-05-09 08:15:10 +0000863 if (Tok.isNot(tok::identifier) || !Tok.Previous)
Daniel Jasperdba1c552013-07-02 09:47:29 +0000864 return false;
865
866 // Skip "const" as it does not have an influence on whether this is a name.
867 FormatToken *PreviousNotConst = Tok.Previous;
Craig Topper2145bc02014-05-09 08:15:10 +0000868 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
Daniel Jasperdba1c552013-07-02 09:47:29 +0000869 PreviousNotConst = PreviousNotConst->Previous;
870
Craig Topper2145bc02014-05-09 08:15:10 +0000871 if (!PreviousNotConst)
Daniel Jasperdba1c552013-07-02 09:47:29 +0000872 return false;
873
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000874 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
875 PreviousNotConst->Previous &&
876 PreviousNotConst->Previous->is(tok::hash);
Daniel Jasperdba1c552013-07-02 09:47:29 +0000877
Daniel Jasper53643062013-08-19 10:16:18 +0000878 if (PreviousNotConst->Type == TT_TemplateCloser)
879 return PreviousNotConst && PreviousNotConst->MatchingParen &&
880 PreviousNotConst->MatchingParen->Previous &&
881 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
882
Daniel Jasperf10a28d2014-05-05 13:48:09 +0000883 if (PreviousNotConst->is(tok::r_paren) && PreviousNotConst->MatchingParen &&
884 PreviousNotConst->MatchingParen->Previous &&
885 PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype))
886 return true;
887
Daniel Jasperdba1c552013-07-02 09:47:29 +0000888 return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
889 PreviousNotConst->Type == TT_PointerOrReference ||
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000890 PreviousNotConst->isSimpleTypeSpecifier();
Daniel Jasperdba1c552013-07-02 09:47:29 +0000891 }
892
Dinesh Dwivedi13b9b7e2014-05-06 09:08:34 +0000893 /// \brief Determine whether ')' is ending a cast.
894 bool rParenEndsCast(const FormatToken &Tok) {
Craig Topper4b566922014-06-09 02:04:02 +0000895 FormatToken *LeftOfParens = nullptr;
Dinesh Dwivedi13b9b7e2014-05-06 09:08:34 +0000896 if (Tok.MatchingParen)
897 LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
Daniel Jasper4b3ba212014-08-25 09:36:07 +0000898 if (LeftOfParens && LeftOfParens->is(tok::r_paren) &&
899 LeftOfParens->MatchingParen)
900 LeftOfParens = LeftOfParens->MatchingParen->Previous;
Daniel Jasper8b76d602014-07-28 12:08:06 +0000901 if (LeftOfParens && LeftOfParens->is(tok::r_square) &&
902 LeftOfParens->MatchingParen &&
903 LeftOfParens->MatchingParen->Type == TT_LambdaLSquare)
904 return false;
Dinesh Dwivedi13b9b7e2014-05-06 09:08:34 +0000905 bool IsCast = false;
906 bool ParensAreEmpty = Tok.Previous == Tok.MatchingParen;
907 bool ParensAreType = !Tok.Previous ||
908 Tok.Previous->Type == TT_PointerOrReference ||
909 Tok.Previous->Type == TT_TemplateCloser ||
910 Tok.Previous->isSimpleTypeSpecifier();
Daniel Jasper3549ea12014-09-19 10:48:15 +0000911 if (Style.Language == FormatStyle::LK_JavaScript && Tok.Next &&
912 Tok.Next->TokenText == "in")
913 return false;
Dinesh Dwivedi13b9b7e2014-05-06 09:08:34 +0000914 bool ParensCouldEndDecl =
915 Tok.Next && Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
916 bool IsSizeOfOrAlignOf =
917 LeftOfParens && LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
918 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
919 ((Contexts.size() > 1 && Contexts[Contexts.size() - 2].IsExpression) ||
920 (Tok.Next && Tok.Next->isBinaryOperator())))
921 IsCast = true;
922 else if (Tok.Next && Tok.Next->isNot(tok::string_literal) &&
923 (Tok.Next->Tok.isLiteral() ||
924 Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
925 IsCast = true;
926 // If there is an identifier after the (), it is likely a cast, unless
927 // there is also an identifier before the ().
Craig Topper4b566922014-06-09 02:04:02 +0000928 else if (LeftOfParens &&
929 (LeftOfParens->Tok.getIdentifierInfo() == nullptr ||
930 LeftOfParens->is(tok::kw_return)) &&
Dinesh Dwivedi13b9b7e2014-05-06 09:08:34 +0000931 LeftOfParens->Type != TT_OverloadedOperator &&
932 LeftOfParens->isNot(tok::at) &&
933 LeftOfParens->Type != TT_TemplateCloser && Tok.Next) {
934 if (Tok.Next->isOneOf(tok::identifier, tok::numeric_constant)) {
935 IsCast = true;
936 } else {
937 // Use heuristics to recognize c style casting.
938 FormatToken *Prev = Tok.Previous;
939 if (Prev && Prev->isOneOf(tok::amp, tok::star))
940 Prev = Prev->Previous;
941
942 if (Prev && Tok.Next && Tok.Next->Next) {
943 bool NextIsUnary = Tok.Next->isUnaryOperator() ||
944 Tok.Next->isOneOf(tok::amp, tok::star);
Daniel Jaspera45eb4c2014-10-06 13:16:43 +0000945 IsCast =
946 NextIsUnary && !Tok.Next->is(tok::plus) &&
947 Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant);
Dinesh Dwivedi13b9b7e2014-05-06 09:08:34 +0000948 }
949
950 for (; Prev != Tok.MatchingParen; Prev = Prev->Previous) {
951 if (!Prev || !Prev->isOneOf(tok::kw_const, tok::identifier)) {
952 IsCast = false;
953 break;
954 }
955 }
956 }
957 }
958 return IsCast && !ParensAreEmpty;
959 }
960
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000961 /// \brief Return the type of the given token assuming it is * or &.
Manuel Klimekf81e5c02014-03-27 11:17:36 +0000962 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
963 bool InTemplateArgument) {
Daniel Jasper3a038de2014-09-05 08:53:45 +0000964 if (Style.Language == FormatStyle::LK_JavaScript)
965 return TT_BinaryOperator;
966
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000967 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Craig Topper2145bc02014-05-09 08:15:10 +0000968 if (!PrevToken)
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000969 return TT_UnaryOperator;
970
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000971 const FormatToken *NextToken = Tok.getNextNonComment();
Daniel Jasper2520fe92014-06-30 13:54:27 +0000972 if (!NextToken || NextToken->is(tok::l_brace))
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000973 return TT_Unknown;
974
Daniel Jasper73e171f2014-08-29 12:54:38 +0000975 if (PrevToken->is(tok::coloncolon))
Daniel Jasper8eb371b2013-03-01 17:13:29 +0000976 return TT_PointerOrReference;
977
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000978 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
Daniel Jasperae907642013-03-14 10:50:25 +0000979 tok::comma, tok::semi, tok::kw_return, tok::colon,
Daniel Jasperdf620b22013-09-21 17:31:51 +0000980 tok::equal, tok::kw_delete, tok::kw_sizeof) ||
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000981 PrevToken->Type == TT_BinaryOperator ||
Daniel Jasperc0d606a2014-04-14 11:08:45 +0000982 PrevToken->Type == TT_ConditionalExpr ||
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000983 PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
984 return TT_UnaryOperator;
985
Daniel Jasperea2d0422014-05-08 08:50:10 +0000986 if (NextToken->is(tok::l_square) && NextToken->Type != TT_LambdaLSquare)
Nico Weber5d2624e2013-02-06 06:20:11 +0000987 return TT_PointerOrReference;
Daniel Jasper6ba16382014-07-28 13:19:58 +0000988 if (NextToken->isOneOf(tok::kw_operator, tok::comma))
Daniel Jasper32ccb032014-06-23 07:36:18 +0000989 return TT_PointerOrReference;
Nico Weber5d2624e2013-02-06 06:20:11 +0000990
Daniel Jasper71665cd2013-09-10 10:26:38 +0000991 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
992 PrevToken->MatchingParen->Previous &&
Daniel Jasper4ac7de72014-06-11 07:35:16 +0000993 PrevToken->MatchingParen->Previous->isOneOf(tok::kw_typeof,
994 tok::kw_decltype))
Daniel Jasper71665cd2013-09-10 10:26:38 +0000995 return TT_PointerOrReference;
996
Manuel Klimek94815562014-03-28 09:27:09 +0000997 if (PrevToken->Tok.isLiteral() ||
998 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
Daniel Jasperf0c809a2014-10-28 18:28:22 +0000999 tok::kw_false, tok::r_brace) ||
Manuel Klimek94815562014-03-28 09:27:09 +00001000 NextToken->Tok.isLiteral() ||
1001 NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
1002 NextToken->isUnaryOperator() ||
Manuel Klimekf81e5c02014-03-27 11:17:36 +00001003 // If we know we're in a template argument, there are no named
1004 // declarations. Thus, having an identifier on the right-hand side
1005 // indicates a binary operator.
1006 (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
Daniel Jasper3a9370c2013-02-04 07:21:18 +00001007 return TT_BinaryOperator;
1008
Daniel Jasper13a7f462014-10-28 18:11:52 +00001009 // "&&(" is quite unlikely to be two successive unary "&".
1010 if (Tok.is(tok::ampamp) && NextToken && NextToken->is(tok::l_paren))
1011 return TT_BinaryOperator;
1012
Daniel Jasper7d028292014-06-02 11:54:20 +00001013 // This catches some cases where evaluation order is used as control flow:
1014 // aaa && aaa->f();
1015 const FormatToken *NextNextToken = NextToken->getNextNonComment();
1016 if (NextNextToken && NextNextToken->is(tok::arrow))
1017 return TT_BinaryOperator;
1018
Daniel Jasper3a9370c2013-02-04 07:21:18 +00001019 // It is very unlikely that we are going to find a pointer or reference type
1020 // definition on the RHS of an assignment.
1021 if (IsExpression)
1022 return TT_BinaryOperator;
1023
1024 return TT_PointerOrReference;
1025 }
1026
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001027 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +00001028 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Craig Topper2145bc02014-05-09 08:15:10 +00001029 if (!PrevToken || PrevToken->Type == TT_CastRParen)
Daniel Jasper3a9370c2013-02-04 07:21:18 +00001030 return TT_UnaryOperator;
1031
1032 // Use heuristics to recognize unary operators.
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001033 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
1034 tok::question, tok::colon, tok::kw_return,
1035 tok::kw_case, tok::at, tok::l_brace))
Daniel Jasper3a9370c2013-02-04 07:21:18 +00001036 return TT_UnaryOperator;
1037
Nico Weberb76de882013-02-05 16:21:00 +00001038 // There can't be two consecutive binary operators.
Daniel Jasper3a9370c2013-02-04 07:21:18 +00001039 if (PrevToken->Type == TT_BinaryOperator)
1040 return TT_UnaryOperator;
1041
1042 // Fall back to marking the token as binary operator.
1043 return TT_BinaryOperator;
1044 }
1045
1046 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001047 TokenType determineIncrementUsage(const FormatToken &Tok) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +00001048 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Craig Topper2145bc02014-05-09 08:15:10 +00001049 if (!PrevToken || PrevToken->Type == TT_CastRParen)
Daniel Jasper3a9370c2013-02-04 07:21:18 +00001050 return TT_UnaryOperator;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001051 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
Daniel Jasper3a9370c2013-02-04 07:21:18 +00001052 return TT_TrailingUnaryOperator;
1053
1054 return TT_UnaryOperator;
1055 }
Daniel Jasperc697ad22013-02-06 10:05:46 +00001056
1057 SmallVector<Context, 8> Contexts;
1058
Daniel Jasper8de9ed02013-08-22 15:00:41 +00001059 const FormatStyle &Style;
Daniel Jasperc697ad22013-02-06 10:05:46 +00001060 AnnotatedLine &Line;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001061 FormatToken *CurrentToken;
Daniel Jasperc697ad22013-02-06 10:05:46 +00001062 bool KeywordVirtualFound;
Daniel Jasper6cdec7c2013-07-09 14:36:48 +00001063 bool AutoFound;
Nico Weber29f9dea2013-02-11 15:32:15 +00001064 IdentifierInfo &Ident_in;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001065};
1066
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001067static int PrecedenceUnaryOperator = prec::PointerToMember + 1;
1068static int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
1069
Daniel Jasper400adc62013-02-08 15:28:42 +00001070/// \brief Parses binary expressions by inserting fake parenthesis based on
1071/// operator precedence.
1072class ExpressionParser {
1073public:
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001074 ExpressionParser(const FormatStyle &Style, AnnotatedLine &Line)
1075 : Style(Style), Current(Line.First) {}
Daniel Jasper400adc62013-02-08 15:28:42 +00001076
1077 /// \brief Parse expressions with the given operatore precedence.
Daniel Jaspercd8599e2013-02-23 21:01:55 +00001078 void parse(int Precedence = 0) {
Daniel Jasperf48b5ab2013-11-07 19:23:49 +00001079 // Skip 'return' and ObjC selector colons as they are not part of a binary
1080 // expression.
1081 while (Current &&
1082 (Current->is(tok::kw_return) ||
Daniel Jasper17062ff2014-06-10 14:44:02 +00001083 (Current->is(tok::colon) && (Current->Type == TT_ObjCMethodExpr ||
1084 Current->Type == TT_DictLiteral))))
Daniel Jaspereabede62013-09-30 08:29:03 +00001085 next();
1086
Craig Topper2145bc02014-05-09 08:15:10 +00001087 if (!Current || Precedence > PrecedenceArrowAndPeriod)
Daniel Jasper0649d362013-08-23 15:14:03 +00001088 return;
1089
Daniel Jasper2c611c02013-05-31 14:56:12 +00001090 // Conditional expressions need to be parsed separately for proper nesting.
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001091 if (Precedence == prec::Conditional) {
Daniel Jasper2c611c02013-05-31 14:56:12 +00001092 parseConditionalExpr();
1093 return;
1094 }
Daniel Jasper0649d362013-08-23 15:14:03 +00001095
1096 // Parse unary operators, which all have a higher precedence than binary
1097 // operators.
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001098 if (Precedence == PrecedenceUnaryOperator) {
Daniel Jasper0649d362013-08-23 15:14:03 +00001099 parseUnaryOperator();
Daniel Jasper400adc62013-02-08 15:28:42 +00001100 return;
Daniel Jasper0649d362013-08-23 15:14:03 +00001101 }
Daniel Jasper400adc62013-02-08 15:28:42 +00001102
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001103 FormatToken *Start = Current;
Craig Topper2145bc02014-05-09 08:15:10 +00001104 FormatToken *LatestOperator = nullptr;
Daniel Jasper0e617842014-04-16 12:26:54 +00001105 unsigned OperatorIndex = 0;
Daniel Jasper400adc62013-02-08 15:28:42 +00001106
Daniel Jaspercd8599e2013-02-23 21:01:55 +00001107 while (Current) {
Daniel Jasper400adc62013-02-08 15:28:42 +00001108 // Consume operators with higher precedence.
Daniel Jasper6bee6822013-04-08 20:33:42 +00001109 parse(Precedence + 1);
Daniel Jasper400adc62013-02-08 15:28:42 +00001110
Daniel Jasper0649d362013-08-23 15:14:03 +00001111 int CurrentPrecedence = getCurrentPrecedence();
1112
Daniel Jasper17062ff2014-06-10 14:44:02 +00001113 if (Current && Current->Type == TT_SelectorName &&
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001114 Precedence == CurrentPrecedence) {
1115 if (LatestOperator)
1116 addFakeParenthesis(Start, prec::Level(Precedence));
Daniel Jasper0649d362013-08-23 15:14:03 +00001117 Start = Current;
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001118 }
Daniel Jaspercd8599e2013-02-23 21:01:55 +00001119
Daniel Jasper400adc62013-02-08 15:28:42 +00001120 // At the end of the line or when an operator with higher precedence is
1121 // found, insert fake parenthesis and return.
Daniel Jasper86296e32014-10-20 11:12:51 +00001122 if (!Current || (Current->closesScope() && Current->MatchingParen) ||
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001123 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) {
1124 if (LatestOperator) {
Daniel Jasper0e617842014-04-16 12:26:54 +00001125 LatestOperator->LastOperator = true;
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001126 if (Precedence == PrecedenceArrowAndPeriod) {
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001127 // Call expressions don't have a binary operator precedence.
1128 addFakeParenthesis(Start, prec::Unknown);
1129 } else {
1130 addFakeParenthesis(Start, prec::Level(Precedence));
1131 }
1132 }
Daniel Jasper400adc62013-02-08 15:28:42 +00001133 return;
1134 }
1135
1136 // Consume scopes: (), [], <> and {}
Daniel Jasperc04baae2013-04-10 09:49:49 +00001137 if (Current->opensScope()) {
1138 while (Current && !Current->closesScope()) {
Daniel Jasper400adc62013-02-08 15:28:42 +00001139 next();
1140 parse();
1141 }
1142 next();
1143 } else {
1144 // Operator found.
Daniel Jasper0e617842014-04-16 12:26:54 +00001145 if (CurrentPrecedence == Precedence) {
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001146 LatestOperator = Current;
Daniel Jasper0e617842014-04-16 12:26:54 +00001147 Current->OperatorIndex = OperatorIndex;
1148 ++OperatorIndex;
1149 }
Daniel Jasper400adc62013-02-08 15:28:42 +00001150
Daniel Jasper94e11d02014-09-04 14:58:30 +00001151 next(/*SkipPastLeadingComments=*/false);
Daniel Jasper400adc62013-02-08 15:28:42 +00001152 }
1153 }
1154 }
1155
1156private:
Daniel Jasper0649d362013-08-23 15:14:03 +00001157 /// \brief Gets the precedence (+1) of the given token for binary operators
1158 /// and other tokens that we treat like binary operators.
1159 int getCurrentPrecedence() {
1160 if (Current) {
Daniel Jasper97bfb7b2014-09-05 08:29:31 +00001161 const FormatToken *NextNonComment = Current->getNextNonComment();
Daniel Jasper0649d362013-08-23 15:14:03 +00001162 if (Current->Type == TT_ConditionalExpr)
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001163 return prec::Conditional;
Daniel Jasper97bfb7b2014-09-05 08:29:31 +00001164 else if (NextNonComment && NextNonComment->is(tok::colon) &&
1165 NextNonComment->Type == TT_DictLiteral)
1166 return prec::Comma;
Daniel Jasperf48b5ab2013-11-07 19:23:49 +00001167 else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon ||
Daniel Jasper94e11d02014-09-04 14:58:30 +00001168 Current->Type == TT_SelectorName ||
Daniel Jasper97bfb7b2014-09-05 08:29:31 +00001169 (Current->is(tok::comment) && NextNonComment &&
1170 NextNonComment->Type == TT_SelectorName))
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001171 return 0;
Daniel Jasper9cc3e972014-02-07 10:09:46 +00001172 else if (Current->Type == TT_RangeBasedForLoopColon)
1173 return prec::Comma;
Daniel Jasper0649d362013-08-23 15:14:03 +00001174 else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001175 return Current->getPrecedence();
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001176 else if (Current->isOneOf(tok::period, tok::arrow))
1177 return PrecedenceArrowAndPeriod;
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001178 else if (Style.Language == FormatStyle::LK_Java &&
1179 Current->is(tok::identifier) &&
1180 (Current->TokenText == "extends" ||
1181 Current->TokenText == "implements"))
1182 return 0;
Daniel Jasper0649d362013-08-23 15:14:03 +00001183 }
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001184 return -1;
Daniel Jasper0649d362013-08-23 15:14:03 +00001185 }
1186
Daniel Jasper2c611c02013-05-31 14:56:12 +00001187 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
1188 Start->FakeLParens.push_back(Precedence);
Daniel Jasper562ecd42013-09-06 08:08:14 +00001189 if (Precedence > prec::Unknown)
1190 Start->StartsBinaryExpression = true;
1191 if (Current) {
Daniel Jasper2c611c02013-05-31 14:56:12 +00001192 ++Current->Previous->FakeRParens;
Daniel Jasper562ecd42013-09-06 08:08:14 +00001193 if (Precedence > prec::Unknown)
1194 Current->Previous->EndsBinaryExpression = true;
1195 }
Daniel Jasper2c611c02013-05-31 14:56:12 +00001196 }
1197
Daniel Jasper0649d362013-08-23 15:14:03 +00001198 /// \brief Parse unary operator expressions and surround them with fake
1199 /// parentheses if appropriate.
1200 void parseUnaryOperator() {
Craig Topper2145bc02014-05-09 08:15:10 +00001201 if (!Current || Current->Type != TT_UnaryOperator) {
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001202 parse(PrecedenceArrowAndPeriod);
Daniel Jasper0649d362013-08-23 15:14:03 +00001203 return;
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001204 }
Daniel Jasper0649d362013-08-23 15:14:03 +00001205
1206 FormatToken *Start = Current;
1207 next();
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001208 parseUnaryOperator();
Daniel Jasper0649d362013-08-23 15:14:03 +00001209
Daniel Jasper0649d362013-08-23 15:14:03 +00001210 // The actual precedence doesn't matter.
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001211 addFakeParenthesis(Start, prec::Unknown);
Daniel Jasper0649d362013-08-23 15:14:03 +00001212 }
1213
Daniel Jasper2c611c02013-05-31 14:56:12 +00001214 void parseConditionalExpr() {
Daniel Jasper1a31bab2014-10-16 09:10:11 +00001215 while (Current && Current->isTrailingComment()) {
1216 next();
1217 }
Daniel Jasper2c611c02013-05-31 14:56:12 +00001218 FormatToken *Start = Current;
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001219 parse(prec::LogicalOr);
Daniel Jasper2c611c02013-05-31 14:56:12 +00001220 if (!Current || !Current->is(tok::question))
1221 return;
1222 next();
Daniel Jasperfc3861a2014-07-17 12:22:04 +00001223 parseConditionalExpr();
Daniel Jasper2c611c02013-05-31 14:56:12 +00001224 if (!Current || Current->Type != TT_ConditionalExpr)
1225 return;
1226 next();
1227 parseConditionalExpr();
1228 addFakeParenthesis(Start, prec::Conditional);
1229 }
1230
Daniel Jasper94e11d02014-09-04 14:58:30 +00001231 void next(bool SkipPastLeadingComments = true) {
Alexander Kornienkoafaa8f52013-06-17 13:19:53 +00001232 if (Current)
1233 Current = Current->Next;
Daniel Jasper94e11d02014-09-04 14:58:30 +00001234 while (Current &&
1235 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
1236 Current->isTrailingComment())
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001237 Current = Current->Next;
Daniel Jasper400adc62013-02-08 15:28:42 +00001238 }
1239
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001240 const FormatStyle &Style;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001241 FormatToken *Current;
Daniel Jasper400adc62013-02-08 15:28:42 +00001242};
1243
Craig Topper318ed7c2013-07-01 04:03:19 +00001244} // end anonymous namespace
1245
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001246void
1247TokenAnnotator::setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines) {
Craig Topper2145bc02014-05-09 08:15:10 +00001248 const AnnotatedLine *NextNonCommentLine = nullptr;
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001249 for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
1250 E = Lines.rend();
1251 I != E; ++I) {
1252 if (NextNonCommentLine && (*I)->First->is(tok::comment) &&
Craig Topper2145bc02014-05-09 08:15:10 +00001253 (*I)->First->Next == nullptr)
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001254 (*I)->Level = NextNonCommentLine->Level;
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001255 else
Craig Topper2145bc02014-05-09 08:15:10 +00001256 NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr;
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001257
1258 setCommentLineLevels((*I)->Children);
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001259 }
1260}
1261
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001262void TokenAnnotator::annotate(AnnotatedLine &Line) {
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001263 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1264 E = Line.Children.end();
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001265 I != E; ++I) {
1266 annotate(**I);
1267 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +00001268 AnnotatingParser Parser(Style, Line, Ident_in);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001269 Line.Type = Parser.parseLine();
1270 if (Line.Type == LT_Invalid)
1271 return;
1272
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001273 ExpressionParser ExprParser(Style, Line);
Daniel Jasper400adc62013-02-08 15:28:42 +00001274 ExprParser.parse();
1275
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001276 if (Line.First->Type == TT_ObjCMethodSpecifier)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001277 Line.Type = LT_ObjCMethodDecl;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001278 else if (Line.First->Type == TT_ObjCDecl)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001279 Line.Type = LT_ObjCDecl;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001280 else if (Line.First->Type == TT_ObjCProperty)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001281 Line.Type = LT_ObjCProperty;
1282
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001283 Line.First->SpacesRequiredBefore = 1;
1284 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001285}
1286
Daniel Jasper4355e7f2014-07-09 07:50:33 +00001287// This function heuristically determines whether 'Current' starts the name of a
1288// function declaration.
1289static bool isFunctionDeclarationName(const FormatToken &Current) {
1290 if (Current.Type != TT_StartOfName ||
Daniel Jasper2ad0aba2014-10-28 17:06:04 +00001291 Current.NestingLevel != 0)
Daniel Jasper4355e7f2014-07-09 07:50:33 +00001292 return false;
1293 const FormatToken *Next = Current.Next;
1294 for (; Next; Next = Next->Next) {
1295 if (Next->Type == TT_TemplateOpener) {
1296 Next = Next->MatchingParen;
1297 } else if (Next->is(tok::coloncolon)) {
1298 Next = Next->Next;
1299 if (!Next || !Next->is(tok::identifier))
1300 return false;
1301 } else if (Next->is(tok::l_paren)) {
1302 break;
1303 } else {
1304 return false;
1305 }
1306 }
1307 if (!Next)
1308 return false;
1309 assert(Next->is(tok::l_paren));
1310 if (Next->Next == Next->MatchingParen)
1311 return true;
1312 for (const FormatToken *Tok = Next->Next; Tok != Next->MatchingParen;
1313 Tok = Tok->Next) {
1314 if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
1315 Tok->Type == TT_PointerOrReference || Tok->Type == TT_StartOfName)
1316 return true;
1317 if (Tok->isOneOf(tok::l_brace, tok::string_literal) || Tok->Tok.isLiteral())
1318 return false;
1319 }
1320 return false;
1321}
1322
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001323void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
Daniel Jasper5f3ea472014-05-22 08:36:53 +00001324 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1325 E = Line.Children.end();
1326 I != E; ++I) {
1327 calculateFormattingInformation(**I);
1328 }
1329
Alexander Kornienko39856b72013-09-10 09:38:25 +00001330 Line.First->TotalLength =
1331 Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001332 if (!Line.First->Next)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001333 return;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001334 FormatToken *Current = Line.First->Next;
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001335 bool InFunctionDecl = Line.MightBeFunctionDecl;
Craig Topper2145bc02014-05-09 08:15:10 +00001336 while (Current) {
Daniel Jasper4355e7f2014-07-09 07:50:33 +00001337 if (isFunctionDeclarationName(*Current))
1338 Current->Type = TT_FunctionDeclarationName;
Daniel Jasper5a611392013-12-19 21:41:37 +00001339 if (Current->Type == TT_LineComment) {
Daniel Jasper84a12e12014-03-10 15:06:25 +00001340 if (Current->Previous->BlockKind == BK_BracedInit &&
1341 Current->Previous->opensScope())
Daniel Jasper5a611392013-12-19 21:41:37 +00001342 Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1;
1343 else
1344 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
Daniel Jasper14e58e52014-03-21 11:58:45 +00001345
1346 // If we find a trailing comment, iterate backwards to determine whether
1347 // it seems to relate to a specific parameter. If so, break before that
1348 // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
1349 // to the previous line in:
1350 // SomeFunction(a,
1351 // b, // comment
1352 // c);
Daniel Jasper28df0a32014-03-21 12:15:40 +00001353 if (!Current->HasUnescapedNewline) {
Daniel Jasper14e58e52014-03-21 11:58:45 +00001354 for (FormatToken *Parameter = Current->Previous; Parameter;
1355 Parameter = Parameter->Previous) {
1356 if (Parameter->isOneOf(tok::comment, tok::r_brace))
1357 break;
1358 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
1359 if (Parameter->Previous->Type != TT_CtorInitializerComma &&
1360 Parameter->HasUnescapedNewline)
1361 Parameter->MustBreakBefore = true;
1362 break;
1363 }
1364 }
1365 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001366 } else if (Current->SpacesRequiredBefore == 0 &&
Daniel Jasper166c19b2014-05-06 14:12:21 +00001367 spaceRequiredBefore(Line, *Current)) {
Alexander Kornienko384b40b2013-10-11 21:43:05 +00001368 Current->SpacesRequiredBefore = 1;
Daniel Jasper5a611392013-12-19 21:41:37 +00001369 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001370
Daniel Jasperfb81b092013-09-17 09:52:48 +00001371 Current->MustBreakBefore =
1372 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
1373
Daniel Jasperca4ea1c2014-08-05 12:16:31 +00001374 if (Style.AlwaysBreakAfterDefinitionReturnType &&
1375 InFunctionDecl && Current->Type == TT_FunctionDeclarationName &&
Daniel Jasperdb764792014-08-14 11:36:03 +00001376 !Line.Last->isOneOf(tok::semi, tok::comment)) // Only for definitions.
1377 // FIXME: Line.Last points to other characters than tok::semi
1378 // and tok::lbrace.
Daniel Jasperca4ea1c2014-08-05 12:16:31 +00001379 Current->MustBreakBefore = true;
1380
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001381 Current->CanBreakBefore =
1382 Current->MustBreakBefore || canBreakBefore(Line, *Current);
Daniel Jasper5f3ea472014-05-22 08:36:53 +00001383 unsigned ChildSize = 0;
1384 if (Current->Previous->Children.size() == 1) {
1385 FormatToken &LastOfChild = *Current->Previous->Children[0]->Last;
1386 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
1387 : LastOfChild.TotalLength + 1;
1388 }
Daniel Jasper56346192014-10-27 16:31:46 +00001389 const FormatToken *Prev= Current->Previous;
1390 if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
1391 (Prev->Children.size() == 1 &&
1392 Prev->Children[0]->First->MustBreakBefore) ||
Alexander Kornienko39856b72013-09-10 09:38:25 +00001393 Current->IsMultiline)
Daniel Jasper56346192014-10-27 16:31:46 +00001394 Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001395 else
Daniel Jasper56346192014-10-27 16:31:46 +00001396 Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
1397 ChildSize + Current->SpacesRequiredBefore;
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001398
1399 if (Current->Type == TT_CtorInitializerColon)
1400 InFunctionDecl = false;
1401
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001402 // FIXME: Only calculate this if CanBreakBefore is true once static
1403 // initializers etc. are sorted out.
1404 // FIXME: Move magic numbers to a better place.
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001405 Current->SplitPenalty = 20 * Current->BindingStrength +
1406 splitPenalty(Line, *Current, InFunctionDecl);
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001407
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001408 Current = Current->Next;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001409 }
Daniel Jasper6bee6822013-04-08 20:33:42 +00001410
Manuel Klimek4fe43002013-05-22 12:51:29 +00001411 calculateUnbreakableTailLengths(Line);
Craig Topper2145bc02014-05-09 08:15:10 +00001412 for (Current = Line.First; Current != nullptr; Current = Current->Next) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +00001413 if (Current->Role)
1414 Current->Role->precomputeFormattingInfos(Current);
1415 }
1416
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001417 DEBUG({ printDebugInfo(Line); });
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001418}
1419
Manuel Klimek4fe43002013-05-22 12:51:29 +00001420void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1421 unsigned UnbreakableTailLength = 0;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001422 FormatToken *Current = Line.Last;
Craig Topper2145bc02014-05-09 08:15:10 +00001423 while (Current) {
Manuel Klimek4fe43002013-05-22 12:51:29 +00001424 Current->UnbreakableTailLength = UnbreakableTailLength;
1425 if (Current->CanBreakBefore ||
1426 Current->isOneOf(tok::comment, tok::string_literal)) {
1427 UnbreakableTailLength = 0;
1428 } else {
1429 UnbreakableTailLength +=
Alexander Kornienko39856b72013-09-10 09:38:25 +00001430 Current->ColumnWidth + Current->SpacesRequiredBefore;
Manuel Klimek4fe43002013-05-22 12:51:29 +00001431 }
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001432 Current = Current->Previous;
Manuel Klimek4fe43002013-05-22 12:51:29 +00001433 }
1434}
1435
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001436unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001437 const FormatToken &Tok,
1438 bool InFunctionDecl) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001439 const FormatToken &Left = *Tok.Previous;
1440 const FormatToken &Right = Tok;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001441
Daniel Jasperbca4bbe2013-05-28 11:30:49 +00001442 if (Left.is(tok::semi))
1443 return 0;
Daniel Jasper783bac62014-04-15 09:54:30 +00001444 if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next &&
1445 Right.Next->Type == TT_DictLiteral))
Daniel Jasperbca4bbe2013-05-28 11:30:49 +00001446 return 1;
Daniel Jasper7052ce62014-01-19 09:04:08 +00001447 if (Right.is(tok::l_square)) {
1448 if (Style.Language == FormatStyle::LK_Proto)
1449 return 1;
Daniel Jaspera2fb50f2014-06-24 09:15:49 +00001450 if (Right.Type != TT_ObjCMethodExpr && Right.Type != TT_LambdaLSquare)
Daniel Jasperecaba172014-06-10 13:27:57 +00001451 return 500;
Daniel Jasper7052ce62014-01-19 09:04:08 +00001452 }
Daniel Jasper4355e7f2014-07-09 07:50:33 +00001453 if (Right.Type == TT_StartOfName ||
1454 Right.Type == TT_FunctionDeclarationName || Right.is(tok::kw_operator)) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001455 if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
Daniel Jasper26d1b1d2013-02-24 18:54:32 +00001456 return 3;
Daniel Jasper40db06a2013-07-11 12:34:23 +00001457 if (Left.Type == TT_StartOfName)
1458 return 20;
Daniel Jasper63af7c42013-12-09 14:40:19 +00001459 if (InFunctionDecl && Right.NestingLevel == 0)
Daniel Jasper26d1b1d2013-02-24 18:54:32 +00001460 return Style.PenaltyReturnTypeOnItsOwnLine;
Daniel Jasper53643062013-08-19 10:16:18 +00001461 return 200;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +00001462 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001463 if (Left.is(tok::equal) && Right.is(tok::l_brace))
1464 return 150;
Daniel Jasper1bc1b502013-07-05 07:58:34 +00001465 if (Left.Type == TT_CastRParen)
1466 return 100;
Daniel Jasper215d6c82014-01-22 08:04:52 +00001467 if (Left.is(tok::coloncolon) ||
1468 (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001469 return 500;
Daniel Jasper83193602013-04-05 17:22:09 +00001470 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1471 return 5000;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001472
Daniel Jaspereead02b2013-02-14 08:42:54 +00001473 if (Left.Type == TT_RangeBasedForLoopColon ||
1474 Left.Type == TT_InheritanceColon)
Daniel Jasper16b35622013-02-26 13:18:08 +00001475 return 2;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001476
Daniel Jasper4c6e0052013-08-27 14:24:43 +00001477 if (Right.isMemberAccess()) {
Daniel Jasper4d7a97a2014-01-10 08:40:17 +00001478 if (Left.is(tok::r_paren) && Left.MatchingParen &&
Daniel Jasper36c28ce2013-09-06 08:54:24 +00001479 Left.MatchingParen->ParameterCount > 0)
Daniel Jasper70bc8742013-02-26 13:59:14 +00001480 return 20; // Should be smaller than breaking at a nested comma.
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001481 return 150;
1482 }
1483
Daniel Jaspere9ab42d2014-10-31 18:23:49 +00001484 if (Left.Type == TT_LeadingJavaAnnotation)
1485 return 1;
1486
Daniel Jaspere3f907f2014-06-02 09:52:08 +00001487 if (Right.Type == TT_TrailingAnnotation &&
1488 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
Daniel Jasperec8e8382014-10-11 08:24:56 +00001489 // Moving trailing annotations to the next line is fine for ObjC method
1490 // declarations.
1491 if (Line.First->Type == TT_ObjCMethodSpecifier)
1492
1493 return 10;
Daniel Jasper5550de62014-02-17 07:57:46 +00001494 // Generally, breaking before a trailing annotation is bad unless it is
1495 // function-like. It seems to be especially preferable to keep standard
1496 // annotations (i.e. "const", "final" and "override") on the same line.
Daniel Jasper43e6a282013-12-16 15:01:54 +00001497 // Use a slightly higher penalty after ")" so that annotations like
1498 // "const override" are kept together.
Daniel Jasperb48d3af2014-04-09 10:01:49 +00001499 bool is_short_annotation = Right.TokenText.size() < 10;
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001500 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
Daniel Jasper43e6a282013-12-16 15:01:54 +00001501 }
Daniel Jasper13c37b32013-05-22 08:28:26 +00001502
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001503 // In for-loops, prefer breaking at ',' and ';'.
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001504 if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
Daniel Jasper37905f72013-02-21 15:00:29 +00001505 return 4;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001506
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001507 // In Objective-C method expressions, prefer breaking before "param:" over
1508 // breaking after it.
Daniel Jasper17062ff2014-06-10 14:44:02 +00001509 if (Right.Type == TT_SelectorName)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001510 return 0;
Daniel Jasper1ac3e052013-02-05 10:07:47 +00001511 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
Daniel Jasper4bf0d802013-11-23 14:27:27 +00001512 return Line.MightBeFunctionDecl ? 50 : 500;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001513
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001514 if (Left.is(tok::l_paren) && InFunctionDecl)
Daniel Jasper6728fc12013-04-11 14:29:13 +00001515 return 100;
Daniel Jasper126153a2013-12-27 06:39:56 +00001516 if (Left.is(tok::equal) && InFunctionDecl)
1517 return 110;
Daniel Jaspera5621202014-08-08 12:00:13 +00001518 if (Right.is(tok::r_brace))
1519 return 1;
Daniel Jasperea79ea12014-08-15 05:00:39 +00001520 if (Left.Type == TT_TemplateOpener)
1521 return 100;
Daniel Jasperc04baae2013-04-10 09:49:49 +00001522 if (Left.opensScope())
Daniel Jasper33b909c2013-10-25 14:29:37 +00001523 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
1524 : 19;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001525
Daniel Jasperba9ddb62013-02-06 21:04:05 +00001526 if (Right.is(tok::lessless)) {
1527 if (Left.is(tok::string_literal)) {
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001528 StringRef Content = Left.TokenText;
Daniel Jasper0b1f76b2013-09-29 12:02:57 +00001529 if (Content.startswith("\""))
1530 Content = Content.drop_front(1);
1531 if (Content.endswith("\""))
1532 Content = Content.drop_back(1);
1533 Content = Content.trim();
Daniel Jasperf38a0ac2013-03-14 14:00:17 +00001534 if (Content.size() > 1 &&
1535 (Content.back() == ':' || Content.back() == '='))
Daniel Jasperfa21c072013-07-15 14:33:14 +00001536 return 25;
Daniel Jasperba9ddb62013-02-06 21:04:05 +00001537 }
Daniel Jasper49a94482013-07-15 15:04:42 +00001538 return 1; // Breaking at a << is really cheap.
Daniel Jasperba9ddb62013-02-06 21:04:05 +00001539 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001540 if (Left.Type == TT_ConditionalExpr)
Daniel Jasper70bc8742013-02-26 13:59:14 +00001541 return prec::Conditional;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001542 prec::Level Level = Left.getPrecedence();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001543
1544 if (Level != prec::Unknown)
1545 return Level;
Daniel Jasperf9a84b52013-03-01 16:48:32 +00001546
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001547 return 3;
1548}
1549
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001550bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001551 const FormatToken &Left,
1552 const FormatToken &Right) {
Daniel Jasper166c19b2014-05-06 14:12:21 +00001553 if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
1554 return true;
Daniel Jaspere9beea22014-01-28 15:20:33 +00001555 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
1556 Left.Tok.getObjCKeywordID() == tok::objc_property)
1557 return true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001558 if (Right.is(tok::hashhash))
1559 return Left.is(tok::hash);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001560 if (Left.isOneOf(tok::hashhash, tok::hash))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001561 return Right.is(tok::hash);
Daniel Jasperb55acad2013-08-20 12:36:34 +00001562 if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1563 return Style.SpaceInEmptyParentheses;
1564 if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
Daniel Jasperf110e202013-08-21 08:39:01 +00001565 return (Right.Type == TT_CastRParen ||
1566 (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen))
Daniel Jasperb55acad2013-08-20 12:36:34 +00001567 ? Style.SpacesInCStyleCastParentheses
1568 : Style.SpacesInParentheses;
1569 if (Right.isOneOf(tok::semi, tok::comma))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001570 return false;
1571 if (Right.is(tok::less) &&
Daniel Jasperdcf37fb2014-08-01 13:03:05 +00001572 (Left.isOneOf(tok::kw_template, tok::r_paren) ||
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001573 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1574 return true;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001575 if (Left.isOneOf(tok::exclaim, tok::tilde))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001576 return false;
1577 if (Left.is(tok::at) &&
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001578 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1579 tok::numeric_constant, tok::l_paren, tok::l_brace,
1580 tok::kw_true, tok::kw_false))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001581 return false;
1582 if (Left.is(tok::coloncolon))
1583 return false;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001584 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001585 return false;
Daniel Jasperbafa6b72013-07-01 09:47:25 +00001586 if (Right.is(tok::ellipsis))
Daniel Jasper2d0cd492013-10-20 16:56:16 +00001587 return Left.Tok.isLiteral();
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001588 if (Left.is(tok::l_square) && Right.is(tok::amp))
1589 return false;
Alexander Kornienkoa5151272013-03-12 16:28:18 +00001590 if (Right.Type == TT_PointerOrReference)
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001591 return Left.Tok.isLiteral() ||
Alexander Kornienkoa5151272013-03-12 16:28:18 +00001592 ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
Daniel Jasper553d4872014-06-17 12:40:34 +00001593 Style.PointerAlignment != FormatStyle::PAS_Left);
Daniel Jasper4d03d3b2013-05-28 15:27:10 +00001594 if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
Daniel Jasperdcf37fb2014-08-01 13:03:05 +00001595 (Left.Type != TT_PointerOrReference ||
1596 Style.PointerAlignment != FormatStyle::PAS_Right))
Daniel Jaspercfda5172013-05-08 14:58:20 +00001597 return true;
Alexander Kornienkoa5151272013-03-12 16:28:18 +00001598 if (Left.Type == TT_PointerOrReference)
Daniel Jasper022612d2013-07-01 09:34:09 +00001599 return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
Daniel Jasperb8914dd2013-03-20 09:53:18 +00001600 ((Right.Type != TT_PointerOrReference) &&
Daniel Jasperdcf37fb2014-08-01 13:03:05 +00001601 Right.isNot(tok::l_paren) &&
1602 Style.PointerAlignment != FormatStyle::PAS_Right && Left.Previous &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001603 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001604 if (Right.is(tok::star) && Left.is(tok::l_paren))
1605 return false;
Nico Weber2a726b62013-02-10 02:08:05 +00001606 if (Left.is(tok::l_square))
Daniel Jasperad981f82014-08-26 11:41:14 +00001607 return (Left.Type == TT_ArrayInitializerLSquare &&
1608 Style.SpacesInContainerLiterals && Right.isNot(tok::r_square)) ||
1609 (Left.Type == TT_ArraySubscriptLSquare &&
1610 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
Nico Weber2a726b62013-02-10 02:08:05 +00001611 if (Right.is(tok::r_square))
Daniel Jasperad981f82014-08-26 11:41:14 +00001612 return Right.MatchingParen &&
1613 ((Style.SpacesInContainerLiterals &&
1614 Right.MatchingParen->Type == TT_ArrayInitializerLSquare) ||
1615 (Style.SpacesInSquareBrackets &&
1616 Right.MatchingParen->Type == TT_ArraySubscriptLSquare));
Daniel Jasper8ddfa842013-08-30 10:36:58 +00001617 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr &&
Daniel Jasper89519082014-05-09 10:26:08 +00001618 Right.Type != TT_LambdaLSquare && Left.isNot(tok::numeric_constant) &&
1619 Left.Type != TT_DictLiteral)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001620 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001621 if (Left.is(tok::colon))
1622 return Left.Type != TT_ObjCMethodExpr;
Daniel Jasper610381f2014-08-26 09:37:52 +00001623 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
1624 return !Left.Children.empty(); // No spaces in "{}".
1625 if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) ||
1626 (Right.is(tok::r_brace) && Right.MatchingParen &&
1627 Right.MatchingParen->BlockKind != BK_Block))
1628 return !Style.Cpp11BracedListStyle;
Daniel Jasper484033b2014-05-06 14:41:29 +00001629 if (Left.Type == TT_BlockComment)
1630 return !Left.TokenText.endswith("=*/");
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001631 if (Right.is(tok::l_paren)) {
Daniel Jasper559b63c2014-01-28 20:13:43 +00001632 if (Left.is(tok::r_paren) && Left.Type == TT_AttributeParen)
Daniel Jasperee6d6502013-07-17 20:25:02 +00001633 return true;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001634 return Line.Type == LT_ObjCDecl ||
Daniel Jasper166c19b2014-05-06 14:12:21 +00001635 Left.isOneOf(tok::kw_new, tok::kw_delete, tok::semi) ||
Alexander Kornienkofdca83d2013-12-10 10:18:34 +00001636 (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001637 (Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while,
Daniel Jasper8f2e94c2014-09-04 15:03:34 +00001638 tok::kw_switch, tok::kw_case) ||
1639 (Left.is(tok::kw_catch) &&
1640 (!Left.Previous || Left.Previous->isNot(tok::period))) ||
Daniel Jaspere1e43192014-04-01 12:55:11 +00001641 Left.IsForEachMacro)) ||
Alexander Kornienkofdca83d2013-12-10 10:18:34 +00001642 (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
Daniel Jasper78b194992014-08-06 14:15:41 +00001643 (Left.is(tok::identifier) || Left.isFunctionLikeKeyword()) &&
Alexander Kornienkofdca83d2013-12-10 10:18:34 +00001644 Line.Type != LT_PreprocessorDirective);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001645 }
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001646 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001647 return false;
Daniel Jasper5bd0b9e2013-05-23 18:05:18 +00001648 if (Right.Type == TT_UnaryOperator)
1649 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1650 (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
Daniel Jasper4afc6b32014-06-02 10:57:55 +00001651 if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
1652 tok::r_paren) ||
Daniel Jasper7a2d60e2014-05-07 07:59:03 +00001653 Left.isSimpleTypeSpecifier()) &&
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001654 Right.is(tok::l_brace) && Right.getNextNonComment() &&
1655 Right.BlockKind != BK_Block)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001656 return false;
Daniel Jasperbca4bbe2013-05-28 11:30:49 +00001657 if (Left.is(tok::period) || Right.is(tok::period))
1658 return false;
Alexander Kornienkod8d47fa2013-09-10 13:41:43 +00001659 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
1660 return false;
Daniel Jasper5ffcb7f2014-10-21 11:13:31 +00001661 if (Left.Type == TT_TemplateCloser && Left.MatchingParen &&
1662 Left.MatchingParen->Previous &&
1663 Left.MatchingParen->Previous->is(tok::period))
1664 // A.<B>DoSomething();
1665 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001666 return true;
1667}
1668
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001669bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Daniel Jasper38efc132014-10-21 07:51:54 +00001670 const FormatToken &Right) {
1671 const FormatToken &Left = *Right.Previous;
1672 if (Style.Language == FormatStyle::LK_Proto) {
1673 if (Right.is(tok::period) &&
1674 (Left.TokenText == "optional" || Left.TokenText == "required" ||
1675 Left.TokenText == "repeated"))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001676 return true;
Daniel Jasper38efc132014-10-21 07:51:54 +00001677 if (Right.is(tok::l_paren) &&
1678 (Left.TokenText == "returns" || Left.TokenText == "option"))
1679 return true;
1680 } else if (Style.Language == FormatStyle::LK_JavaScript) {
1681 if (Left.TokenText == "var")
1682 return true;
Daniel Jasperb9d3db62014-11-02 22:00:57 +00001683 } else if (Style.Language == FormatStyle::LK_Java) {
1684 if (Left.TokenText == "synchronized" && Right.is(tok::l_paren))
1685 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never;
Daniel Jasper38efc132014-10-21 07:51:54 +00001686 }
1687 if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
1688 return true; // Never ever merge two identifiers.
1689 if (Left.Type == TT_ImplicitStringLiteral)
1690 return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
1691 if (Line.Type == LT_ObjCMethodDecl) {
1692 if (Left.Type == TT_ObjCMethodSpecifier)
1693 return true;
1694 if (Left.is(tok::r_paren) && Right.is(tok::identifier))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001695 // Don't space between ')' and <id>
1696 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001697 }
1698 if (Line.Type == LT_ObjCProperty &&
Daniel Jasper38efc132014-10-21 07:51:54 +00001699 (Right.is(tok::equal) || Left.is(tok::equal)))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001700 return false;
1701
Daniel Jasper38efc132014-10-21 07:51:54 +00001702 if (Right.Type == TT_TrailingReturnArrow ||
1703 Left.Type == TT_TrailingReturnArrow)
Daniel Jasper6cdec7c2013-07-09 14:36:48 +00001704 return true;
Daniel Jasper38efc132014-10-21 07:51:54 +00001705 if (Left.is(tok::comma))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001706 return true;
Daniel Jasper38efc132014-10-21 07:51:54 +00001707 if (Right.is(tok::comma))
Daniel Jasperff6c3a92013-02-28 13:40:17 +00001708 return false;
Daniel Jasper38efc132014-10-21 07:51:54 +00001709 if (Right.Type == TT_CtorInitializerColon || Right.Type == TT_ObjCBlockLParen)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001710 return true;
Daniel Jasper38efc132014-10-21 07:51:54 +00001711 if (Left.is(tok::kw_operator))
1712 return Right.is(tok::coloncolon);
1713 if (Right.Type == TT_OverloadedOperatorLParen)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001714 return false;
Daniel Jasper38efc132014-10-21 07:51:54 +00001715 if (Right.is(tok::colon))
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001716 return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Daniel Jasper38efc132014-10-21 07:51:54 +00001717 Right.getNextNonComment() && Right.Type != TT_ObjCMethodExpr &&
1718 !Left.is(tok::question) &&
1719 !(Right.Type == TT_InlineASMColon && Left.is(tok::coloncolon)) &&
1720 (Right.Type != TT_DictLiteral || Style.SpacesInContainerLiterals);
1721 if (Left.Type == TT_UnaryOperator)
1722 return Right.Type == TT_BinaryOperator;
1723 if (Left.Type == TT_CastRParen)
1724 return Style.SpaceAfterCStyleCast || Right.Type == TT_BinaryOperator;
1725 if (Left.is(tok::greater) && Right.is(tok::greater)) {
1726 return Right.Type == TT_TemplateCloser && Left.Type == TT_TemplateCloser &&
Daniel Jasperdd978ae2013-10-29 14:52:02 +00001727 (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001728 }
Daniel Jasper38efc132014-10-21 07:51:54 +00001729 if (Right.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
1730 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar))
Daniel Jasperff6c3a92013-02-28 13:40:17 +00001731 return false;
Daniel Jasperd94bff32013-09-25 15:15:02 +00001732 if (!Style.SpaceBeforeAssignmentOperators &&
Daniel Jasper38efc132014-10-21 07:51:54 +00001733 Right.getPrecedence() == prec::Assignment)
Daniel Jasperd94bff32013-09-25 15:15:02 +00001734 return false;
Daniel Jasper16b107e2014-10-21 09:57:09 +00001735 if (Right.is(tok::coloncolon) && Left.isNot(tok::l_brace))
Daniel Jasperf322eb52014-10-23 20:22:22 +00001736 return (Left.Type == TT_TemplateOpener &&
1737 Style.Standard == FormatStyle::LS_Cpp03) ||
1738 !(Left.isOneOf(tok::identifier, tok::l_paren, tok::r_paren) ||
1739 Left.Type == TT_TemplateCloser || Left.Type == TT_TemplateOpener);
Daniel Jasper16b107e2014-10-21 09:57:09 +00001740 if ((Left.Type == TT_TemplateOpener) != (Right.Type == TT_TemplateCloser))
1741 return Style.SpacesInAngles;
Daniel Jasper38efc132014-10-21 07:51:54 +00001742 if ((Right.Type == TT_BinaryOperator && !Left.is(tok::l_paren)) ||
1743 Left.Type == TT_BinaryOperator || Left.Type == TT_ConditionalExpr)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001744 return true;
Daniel Jasper38efc132014-10-21 07:51:54 +00001745 if (Left.Type == TT_TemplateCloser && Right.is(tok::l_paren))
Chad Rosier0a84f172014-08-05 17:58:54 +00001746 return Style.SpaceBeforeParens == FormatStyle::SBPO_Always;
Daniel Jasper38efc132014-10-21 07:51:54 +00001747 if (Right.Type == TT_TemplateOpener && Left.is(tok::r_paren) &&
1748 Left.MatchingParen &&
1749 Left.MatchingParen->Type == TT_OverloadedOperatorLParen)
Daniel Jasper8835a322014-10-20 13:56:30 +00001750 return false;
Daniel Jasper38efc132014-10-21 07:51:54 +00001751 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
Daniel Jaspered8f1c62013-08-28 08:24:04 +00001752 Line.First->is(tok::hash))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001753 return true;
Daniel Jasper38efc132014-10-21 07:51:54 +00001754 if (Right.Type == TT_TrailingUnaryOperator)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001755 return false;
Daniel Jasper38efc132014-10-21 07:51:54 +00001756 if (Left.Type == TT_RegexLiteral)
Daniel Jasperf9ae3122014-05-08 07:01:45 +00001757 return false;
Daniel Jasper38efc132014-10-21 07:51:54 +00001758 return spaceRequiredBetween(Line, Left, Right);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001759}
1760
Daniel Jaspere18ff372014-06-02 10:17:32 +00001761// Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
1762static bool isAllmanBrace(const FormatToken &Tok) {
1763 return Tok.is(tok::l_brace) && Tok.BlockKind == BK_Block &&
1764 Tok.Type != TT_ObjCBlockLBrace && Tok.Type != TT_DictLiteral;
1765}
1766
Daniel Jasperfb81b092013-09-17 09:52:48 +00001767bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
1768 const FormatToken &Right) {
Daniel Jaspera125d532014-03-21 12:38:57 +00001769 const FormatToken &Left = *Right.Previous;
Daniel Jaspera69ca9b2014-06-04 12:40:57 +00001770 if (Right.NewlinesBefore > 1)
1771 return true;
Daniel Jasperfb81b092013-09-17 09:52:48 +00001772 if (Right.is(tok::comment)) {
Daniel Jasper5a611392013-12-19 21:41:37 +00001773 return Right.Previous->BlockKind != BK_BracedInit &&
Daniel Jasperf6c7c182014-01-13 14:10:04 +00001774 Right.Previous->Type != TT_CtorInitializerColon &&
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001775 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
Daniel Jasperfb81b092013-09-17 09:52:48 +00001776 } else if (Right.Previous->isTrailingComment() ||
Daniel Jasper04b6a082013-12-20 06:22:01 +00001777 (Right.isStringLiteral() && Right.Previous->isStringLiteral())) {
Daniel Jasperfb81b092013-09-17 09:52:48 +00001778 return true;
1779 } else if (Right.Previous->IsUnterminatedLiteral) {
1780 return true;
1781 } else if (Right.is(tok::lessless) && Right.Next &&
1782 Right.Previous->is(tok::string_literal) &&
1783 Right.Next->is(tok::string_literal)) {
1784 return true;
1785 } else if (Right.Previous->ClosesTemplateDeclaration &&
1786 Right.Previous->MatchingParen &&
Daniel Jasper63af7c42013-12-09 14:40:19 +00001787 Right.Previous->MatchingParen->NestingLevel == 0 &&
Daniel Jasperfb81b092013-09-17 09:52:48 +00001788 Style.AlwaysBreakTemplateDeclarations) {
Daniel Jasperfb81b092013-09-17 09:52:48 +00001789 return true;
Alexander Kornienkoa594ba82013-12-16 14:35:51 +00001790 } else if ((Right.Type == TT_CtorInitializerComma ||
1791 Right.Type == TT_CtorInitializerColon) &&
Daniel Jasperec01cd62013-10-08 05:11:18 +00001792 Style.BreakConstructorInitializersBeforeComma &&
1793 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
Daniel Jasperfb81b092013-09-17 09:52:48 +00001794 return true;
Daniel Jasperc39b56f2013-12-16 07:23:08 +00001795 } else if (Right.is(tok::string_literal) &&
1796 Right.TokenText.startswith("R\"")) {
1797 // Raw string literals are special wrt. line breaks. The author has made a
1798 // deliberate choice and might have aligned the contents of the string
1799 // literal accordingly. Thus, we try keep existing line breaks.
1800 return Right.NewlinesBefore > 0;
Daniel Jasper6e58fee2014-01-29 18:43:40 +00001801 } else if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 &&
Daniel Jasper7052ce62014-01-19 09:04:08 +00001802 Style.Language == FormatStyle::LK_Proto) {
Daniel Jasperce2fdb02014-10-27 13:25:59 +00001803 // Don't put enums onto single lines in protocol buffers.
Daniel Jasper7052ce62014-01-19 09:04:08 +00001804 return true;
Daniel Jasper67f8ad22014-09-30 17:57:06 +00001805 } else if (Style.Language == FormatStyle::LK_JavaScript &&
1806 Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
1807 !Left.Children.empty()) {
1808 // Support AllowShortFunctionsOnASingleLine for JavaScript.
1809 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
1810 (Left.NestingLevel == 0 && Line.Level == 0 &&
1811 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline);
Daniel Jaspere18ff372014-06-02 10:17:32 +00001812 } else if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
1813 return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1814 Style.BreakBeforeBraces == FormatStyle::BS_GNU;
Daniel Jaspera382cbe2014-07-28 14:08:09 +00001815 } else if (Style.Language == FormatStyle::LK_Proto &&
1816 Left.isNot(tok::l_brace) && Right.Type == TT_SelectorName) {
1817 return true;
Daniel Jasper76284682014-10-22 09:12:44 +00001818 } else if (Left.Type == TT_ObjCBlockLBrace &&
1819 !Style.AllowShortBlocksOnASingleLine) {
1820 return true;
Daniel Jasperfb81b092013-09-17 09:52:48 +00001821 }
Daniel Jasperb175d572014-04-09 09:53:23 +00001822
Daniel Jasper610381f2014-08-26 09:37:52 +00001823 // If the last token before a '}' is a comma or a trailing comment, the
1824 // intention is to insert a line break after it in order to make shuffling
1825 // around entries easier.
Daniel Jasperb175d572014-04-09 09:53:23 +00001826 const FormatToken *BeforeClosingBrace = nullptr;
1827 if (Left.is(tok::l_brace) && Left.MatchingParen)
Daniel Jasperf9fc2152014-04-09 13:18:49 +00001828 BeforeClosingBrace = Left.MatchingParen->Previous;
Daniel Jasperb175d572014-04-09 09:53:23 +00001829 else if (Right.is(tok::r_brace))
Daniel Jasperf9fc2152014-04-09 13:18:49 +00001830 BeforeClosingBrace = Right.Previous;
Daniel Jasper610381f2014-08-26 09:37:52 +00001831 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
1832 BeforeClosingBrace->isTrailingComment()))
Daniel Jasperb175d572014-04-09 09:53:23 +00001833 return true;
1834
Daniel Jasper49802ef2014-05-22 09:10:04 +00001835 if (Style.Language == FormatStyle::LK_JavaScript) {
1836 // FIXME: This might apply to other languages and token kinds.
1837 if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
1838 Left.Previous->is(tok::char_constant))
1839 return true;
Daniel Jasperfab69ff2014-10-21 08:24:18 +00001840 } else if (Style.Language == FormatStyle::LK_Java) {
Daniel Jaspere9ab42d2014-10-31 18:23:49 +00001841 if (Left.Type == TT_LeadingJavaAnnotation && Right.isNot(tok::l_paren) &&
Daniel Jasperc7d024a2014-10-21 10:02:03 +00001842 Line.Last->is(tok::l_brace))
Daniel Jasperfab69ff2014-10-21 08:24:18 +00001843 return true;
Daniel Jasperc0126862014-10-21 11:34:53 +00001844 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
1845 Right.Next->is(tok::string_literal))
1846 return true;
Daniel Jasper49802ef2014-05-22 09:10:04 +00001847 }
1848
Daniel Jasperfb81b092013-09-17 09:52:48 +00001849 return false;
1850}
1851
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001852bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001853 const FormatToken &Right) {
1854 const FormatToken &Left = *Right.Previous;
Daniel Jasperf26c7552014-10-17 13:36:14 +00001855
1856 if (Style.Language == FormatStyle::LK_Java) {
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001857 if (Left.is(tok::identifier) &&
1858 (Left.TokenText == "throws" || Left.TokenText == "extends" ||
1859 Left.TokenText == "implements"))
Daniel Jasperf26c7552014-10-17 13:36:14 +00001860 return false;
1861 }
1862
Daniel Jasper4bf0d802013-11-23 14:27:27 +00001863 if (Left.is(tok::at))
1864 return false;
Daniel Jasper437c3f52014-04-28 07:34:48 +00001865 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
1866 return false;
Daniel Jaspere9ab42d2014-10-31 18:23:49 +00001867 if (Left.Type == TT_JavaAnnotation || Left.Type == TT_LeadingJavaAnnotation)
Daniel Jasperfd681912014-10-21 10:58:14 +00001868 return true;
Daniel Jasper4355e7f2014-07-09 07:50:33 +00001869 if (Right.Type == TT_StartOfName ||
1870 Right.Type == TT_FunctionDeclarationName || Right.is(tok::kw_operator))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001871 return true;
Daniel Jasper165b29e2013-11-08 00:57:11 +00001872 if (Right.isTrailingComment())
1873 // We rely on MustBreakBefore being set correctly here as we should not
1874 // change the "binding" behavior of a comment.
Daniel Jasper5a611392013-12-19 21:41:37 +00001875 // The first comment in a braced lists is always interpreted as belonging to
1876 // the first list element. Otherwise, it should be placed outside of the
1877 // list.
1878 return Left.BlockKind == BK_BracedInit;
Daniel Jasper165b29e2013-11-08 00:57:11 +00001879 if (Left.is(tok::question) && Right.is(tok::colon))
1880 return false;
1881 if (Right.Type == TT_ConditionalExpr || Right.is(tok::question))
1882 return Style.BreakBeforeTernaryOperators;
1883 if (Left.Type == TT_ConditionalExpr || Left.is(tok::question))
1884 return !Style.BreakBeforeTernaryOperators;
Daniel Jasper3a122c02014-02-14 18:22:40 +00001885 if (Right.Type == TT_InheritanceColon)
1886 return true;
Daniel Jasperd39312ec2014-05-28 10:09:11 +00001887 if (Right.is(tok::colon) && (Right.Type != TT_CtorInitializerColon &&
1888 Right.Type != TT_InlineASMColon))
1889 return false;
Nico Weberced7d412013-05-26 05:39:26 +00001890 if (Left.is(tok::colon) &&
Daniel Jasperb596fb22013-10-24 10:31:50 +00001891 (Left.Type == TT_DictLiteral || Left.Type == TT_ObjCMethodExpr))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001892 return true;
Daniel Jasper17062ff2014-06-10 14:44:02 +00001893 if (Right.Type == TT_SelectorName)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001894 return true;
Daniel Jasper9688ff12013-08-01 13:46:58 +00001895 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
1896 return true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001897 if (Left.ClosesTemplateDeclaration)
1898 return true;
Daniel Jaspereead02b2013-02-14 08:42:54 +00001899 if (Right.Type == TT_RangeBasedForLoopColon ||
Daniel Jasperd215b8b2013-08-28 07:27:35 +00001900 Right.Type == TT_OverloadedOperatorLParen ||
1901 Right.Type == TT_OverloadedOperator)
Daniel Jaspereead02b2013-02-14 08:42:54 +00001902 return false;
Daniel Jaspera61aefb2013-05-06 06:45:09 +00001903 if (Left.Type == TT_RangeBasedForLoopColon)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001904 return true;
Daniel Jasper37905f72013-02-21 15:00:29 +00001905 if (Right.Type == TT_RangeBasedForLoopColon)
1906 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001907 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
Daniel Jasper165b29e2013-11-08 00:57:11 +00001908 Left.Type == TT_UnaryOperator || Left.is(tok::kw_operator))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001909 return false;
1910 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
1911 return false;
Daniel Jasper559b63c2014-01-28 20:13:43 +00001912 if (Left.is(tok::l_paren) && Left.Type == TT_AttributeParen)
1913 return false;
1914 if (Left.is(tok::l_paren) && Left.Previous &&
1915 (Left.Previous->Type == TT_BinaryOperator ||
Daniel Jasper8acf8222014-05-07 09:23:05 +00001916 Left.Previous->Type == TT_CastRParen || Left.Previous->is(tok::kw_if)))
Daniel Jasper559b63c2014-01-28 20:13:43 +00001917 return false;
Daniel Jasper98857842013-10-30 13:54:53 +00001918 if (Right.Type == TT_ImplicitStringLiteral)
1919 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001920
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001921 if (Right.is(tok::r_paren) || Right.Type == TT_TemplateCloser)
1922 return false;
1923
Daniel Jasper13c37b32013-05-22 08:28:26 +00001924 // We only break before r_brace if there was a corresponding break before
1925 // the l_brace, which is tracked by BreakBeforeClosingBrace.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001926 if (Right.is(tok::r_brace))
1927 return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
Daniel Jasper13c37b32013-05-22 08:28:26 +00001928
Daniel Jasperf9a09062014-04-09 10:29:11 +00001929 // Allow breaking after a trailing annotation, e.g. after a method
1930 // declaration.
1931 if (Left.Type == TT_TrailingAnnotation)
1932 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
1933 tok::less, tok::coloncolon);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001934
Daniel Jasperbf4755b2013-03-14 09:50:46 +00001935 if (Right.is(tok::kw___attribute))
1936 return true;
1937
Daniel Jasperaf5ba0e2013-02-23 07:46:38 +00001938 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
1939 return true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +00001940
Daniel Jasper783bac62014-04-15 09:54:30 +00001941 if (Right.is(tok::identifier) && Right.Next &&
1942 Right.Next->Type == TT_DictLiteral)
1943 return true;
1944
Daniel Jaspere33d4af2013-07-26 16:56:36 +00001945 if (Left.Type == TT_CtorInitializerComma &&
1946 Style.BreakConstructorInitializersBeforeComma)
1947 return false;
Daniel Jasperec01cd62013-10-08 05:11:18 +00001948 if (Right.Type == TT_CtorInitializerComma &&
1949 Style.BreakConstructorInitializersBeforeComma)
1950 return true;
Daniel Jasper0de8efa2013-09-17 08:15:46 +00001951 if (Left.is(tok::greater) && Right.is(tok::greater) &&
1952 Left.Type != TT_TemplateCloser)
1953 return false;
Daniel Jasperac043c92014-09-15 11:11:00 +00001954 if (Right.Type == TT_BinaryOperator &&
1955 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
1956 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
1957 Right.getPrecedence() != prec::Assignment))
Daniel Jasper0a1e5ac2014-05-13 08:01:47 +00001958 return true;
Daniel Jasper1db6c382013-10-22 15:30:28 +00001959 if (Left.Type == TT_ArrayInitializerLSquare)
1960 return true;
Daniel Jasper598dd332014-08-12 13:22:26 +00001961 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
1962 return true;
Daniel Jasperac043c92014-09-15 11:11:00 +00001963 if (Left.isBinaryOperator() && !Left.isOneOf(tok::arrowstar, tok::lessless) &&
1964 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
1965 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
1966 Left.getPrecedence() == prec::Assignment))
1967 return true;
1968 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
Daniel Jasper83193602013-04-05 17:22:09 +00001969 tok::kw_class, tok::kw_struct) ||
Daniel Jasperda07a722014-10-17 14:37:40 +00001970 Right.isMemberAccess() || Right.Type == TT_TrailingReturnArrow ||
Daniel Jasper598dd332014-08-12 13:22:26 +00001971 Right.isOneOf(tok::lessless, tok::colon, tok::l_square, tok::at) ||
Daniel Jasper1bc1b502013-07-05 07:58:34 +00001972 (Left.is(tok::r_paren) &&
Daniel Jasper559b63c2014-01-28 20:13:43 +00001973 Right.isOneOf(tok::identifier, tok::kw_const)) ||
Daniel Jasper1db6c382013-10-22 15:30:28 +00001974 (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001975}
1976
Daniel Jasper6bee6822013-04-08 20:33:42 +00001977void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
1978 llvm::errs() << "AnnotatedTokens:\n";
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001979 const FormatToken *Tok = Line.First;
Daniel Jasper6bee6822013-04-08 20:33:42 +00001980 while (Tok) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001981 llvm::errs() << " M=" << Tok->MustBreakBefore
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001982 << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
1983 << " S=" << Tok->SpacesRequiredBefore
Daniel Jasper17062ff2014-06-10 14:44:02 +00001984 << " B=" << Tok->BlockParameterCount
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001985 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
Manuel Klimek71814b42013-10-11 21:25:45 +00001986 << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
1987 << " FakeLParens=";
Daniel Jasper6bee6822013-04-08 20:33:42 +00001988 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
1989 llvm::errs() << Tok->FakeLParens[i] << "/";
1990 llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001991 if (!Tok->Next)
Manuel Klimek71814b42013-10-11 21:25:45 +00001992 assert(Tok == Line.Last);
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001993 Tok = Tok->Next;
Daniel Jasper6bee6822013-04-08 20:33:42 +00001994 }
1995 llvm::errs() << "----\n";
1996}
1997
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001998} // namespace format
1999} // namespace clang