blob: 19759afc85423bff4c22a4397dbccc81ccc2aedd [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
20namespace clang {
21namespace format {
22
Craig Topper318ed7c2013-07-01 04:03:19 +000023namespace {
24
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000025/// \brief A parser that gathers additional information about tokens.
26///
Alexander Kornienkoa5151272013-03-12 16:28:18 +000027/// The \c TokenAnnotator tries to match parenthesis and square brakets and
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000028/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
29/// into template parameter lists.
30class AnnotatingParser {
31public:
Daniel Jasper8de9ed02013-08-22 15:00:41 +000032 AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
33 IdentifierInfo &Ident_in)
34 : Style(Style), Line(Line), CurrentToken(Line.First),
Daniel Jasperbc5cb4e2013-11-07 17:43:07 +000035 KeywordVirtualFound(false), AutoFound(false), Ident_in(Ident_in) {
Nico Weber9096fc02013-06-26 00:30:14 +000036 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000037 }
38
Nico Weber44449172013-02-12 16:17:07 +000039private:
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000040 bool parseAngle() {
41 if (CurrentToken == NULL)
42 return false;
Daniel Jasper40aacf42013-03-14 13:45:21 +000043 ScopedContextCreator ContextCreator(*this, tok::less, 10);
Manuel Klimek6e6310e2013-05-29 14:47:47 +000044 FormatToken *Left = CurrentToken->Previous;
Daniel Jasperc697ad22013-02-06 10:05:46 +000045 Contexts.back().IsExpression = false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000046 while (CurrentToken != NULL) {
47 if (CurrentToken->is(tok::greater)) {
48 Left->MatchingParen = CurrentToken;
49 CurrentToken->MatchingParen = Left;
50 CurrentToken->Type = TT_TemplateCloser;
51 next();
52 return true;
53 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +000054 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
Daniel Jasper6f05e592013-05-15 13:46:48 +000055 tok::question, tok::colon))
56 return false;
Daniel Jasperd5893912013-06-01 18:56:00 +000057 // If a && or || is found and interpreted as a binary operator, this set
Daniel Jasper1027c6e2013-06-03 16:16:41 +000058 // of angles is likely part of something like "a < b && c > d". If the
Daniel Jasperd5893912013-06-01 18:56:00 +000059 // angles are inside an expression, the ||/&& might also be a binary
60 // operator that was misinterpreted because we are parsing template
61 // parameters.
62 // FIXME: This is getting out of hand, write a decent parser.
Manuel Klimek6e6310e2013-05-29 14:47:47 +000063 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
Daniel Jasperd5893912013-06-01 18:56:00 +000064 (CurrentToken->Previous->Type == TT_BinaryOperator ||
65 Contexts[Contexts.size() - 2].IsExpression) &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +000066 Line.First->isNot(tok::kw_template))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000067 return false;
Daniel Jaspere11095a2013-02-14 15:01:34 +000068 updateParameterCount(Left, CurrentToken);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000069 if (!consumeToken())
70 return false;
71 }
72 return false;
73 }
74
75 bool parseParens(bool LookForDecls = false) {
76 if (CurrentToken == NULL)
77 return false;
Daniel Jasper40aacf42013-03-14 13:45:21 +000078 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
Daniel Jasperc697ad22013-02-06 10:05:46 +000079
80 // FIXME: This is a bit of a hack. Do better.
81 Contexts.back().ColonIsForRangeExpr =
82 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
83
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000084 bool StartsObjCMethodExpr = false;
Manuel Klimek6e6310e2013-05-29 14:47:47 +000085 FormatToken *Left = CurrentToken->Previous;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000086 if (CurrentToken->is(tok::caret)) {
87 // ^( starts a block.
88 Left->Type = TT_ObjCBlockLParen;
Manuel Klimek6e6310e2013-05-29 14:47:47 +000089 } else if (FormatToken *MaybeSel = Left->Previous) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000090 // @selector( starts a selector.
Manuel Klimek6e6310e2013-05-29 14:47:47 +000091 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
92 MaybeSel->Previous->is(tok::at)) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000093 StartsObjCMethodExpr = true;
94 }
95 }
96
Daniel Jasper29a98cf2013-08-13 09:09:09 +000097 if (Left->Previous && Left->Previous->isOneOf(tok::kw_static_assert,
Daniel Jasperd46e07e2013-10-20 18:15:30 +000098 tok::kw_if, tok::kw_while)) {
99 // static_assert, if and while usually contain expressions.
Daniel Jasper8b1c6352013-08-01 17:58:23 +0000100 Contexts.back().IsExpression = true;
Daniel Jasperd46e07e2013-10-20 18:15:30 +0000101 } else if (Left->Previous && Left->Previous->is(tok::r_square) &&
102 Left->Previous->MatchingParen &&
103 Left->Previous->MatchingParen->Type == TT_LambdaLSquare) {
104 // This is a parameter list of a lambda expression.
105 Contexts.back().IsExpression = false;
106 }
Daniel Jasper8b1c6352013-08-01 17:58:23 +0000107
Daniel Jasperc697ad22013-02-06 10:05:46 +0000108 if (StartsObjCMethodExpr) {
109 Contexts.back().ColonIsObjCMethodExpr = true;
110 Left->Type = TT_ObjCMethodExpr;
111 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000112
Daniel Jasper37194282013-05-28 08:33:00 +0000113 bool MightBeFunctionType = CurrentToken->is(tok::star);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000114 bool HasMultipleLines = false;
115 bool HasMultipleParametersOnALine = false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000116 while (CurrentToken != NULL) {
117 // LookForDecls is set when "if (" has been seen. Check for
118 // 'identifier' '*' 'identifier' followed by not '=' -- this
119 // '*' has to be a binary operator but determineStarAmpUsage() will
120 // categorize it as an unary operator, so set the right type here.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000121 if (LookForDecls && CurrentToken->Next) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000122 FormatToken *Prev = CurrentToken->getPreviousNonComment();
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000123 if (Prev) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000124 FormatToken *PrevPrev = Prev->getPreviousNonComment();
Alexander Kornienkodd7ece52013-06-07 16:02:52 +0000125 FormatToken *Next = CurrentToken->Next;
126 if (PrevPrev && PrevPrev->is(tok::identifier) &&
127 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
128 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
129 Prev->Type = TT_BinaryOperator;
130 LookForDecls = false;
131 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000132 }
133 }
134
Daniel Jasper59036852013-08-12 12:16:34 +0000135 if (CurrentToken->Previous->Type == TT_PointerOrReference &&
136 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
137 tok::coloncolon))
138 MightBeFunctionType = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000139 if (CurrentToken->is(tok::r_paren)) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000140 if (MightBeFunctionType && CurrentToken->Next &&
Daniel Jasper655d96a2013-07-16 11:37:21 +0000141 (CurrentToken->Next->is(tok::l_paren) ||
142 (CurrentToken->Next->is(tok::l_square) &&
143 !Contexts.back().IsExpression)))
Daniel Jasper37194282013-05-28 08:33:00 +0000144 Left->Type = TT_FunctionTypeLParen;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000145 Left->MatchingParen = CurrentToken;
146 CurrentToken->MatchingParen = Left;
147
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000148 if (StartsObjCMethodExpr) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000149 CurrentToken->Type = TT_ObjCMethodExpr;
150 if (Contexts.back().FirstObjCSelectorName != NULL) {
151 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
152 Contexts.back().LongestObjCSelectorName;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000153 }
154 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000155
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000156 if (!HasMultipleLines)
157 Left->PackingKind = PPK_Inconclusive;
158 else if (HasMultipleParametersOnALine)
159 Left->PackingKind = PPK_BinPacked;
160 else
161 Left->PackingKind = PPK_OnePerLine;
162
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000163 next();
164 return true;
165 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000166 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000167 return false;
Daniel Jaspere11095a2013-02-14 15:01:34 +0000168 updateParameterCount(Left, CurrentToken);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000169 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
170 !CurrentToken->Next->HasUnescapedNewline &&
171 !CurrentToken->Next->isTrailingComment())
172 HasMultipleParametersOnALine = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000173 if (!consumeToken())
174 return false;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000175 if (CurrentToken && CurrentToken->HasUnescapedNewline)
176 HasMultipleLines = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000177 }
178 return false;
179 }
180
181 bool parseSquare() {
182 if (!CurrentToken)
183 return false;
184
Alexander Kornienkoafaa8f52013-06-17 13:19:53 +0000185 // A '[' could be an index subscript (after an identifier or after
Nico Weber2a726b62013-02-10 02:08:05 +0000186 // ')' or ']'), it could be the start of an Objective-C method
187 // expression, or it could the the start of an Objective-C array literal.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000188 FormatToken *Left = CurrentToken->Previous;
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000189 FormatToken *Parent = Left->getPreviousNonComment();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000190 bool StartsObjCMethodExpr =
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000191 Contexts.back().CanBeExpression && Left->Type != TT_LambdaLSquare &&
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000192 (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
193 tok::kw_return, tok::kw_throw) ||
Daniel Jasperc04baae2013-04-10 09:49:49 +0000194 Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn ||
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000195 Parent->Type == TT_CastRParen ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000196 getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown);
Daniel Jasper40aacf42013-03-14 13:45:21 +0000197 ScopedContextCreator ContextCreator(*this, tok::l_square, 10);
Daniel Jasper97b89482013-03-13 07:49:51 +0000198 Contexts.back().IsExpression = true;
Daniel Jaspera1ea4cb2013-10-26 17:00:22 +0000199 bool ColonFound = false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000200
Daniel Jasperc697ad22013-02-06 10:05:46 +0000201 if (StartsObjCMethodExpr) {
202 Contexts.back().ColonIsObjCMethodExpr = true;
203 Left->Type = TT_ObjCMethodExpr;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000204 } else if (Parent && Parent->is(tok::at)) {
205 Left->Type = TT_ArrayInitializerLSquare;
206 } else if (Left->Type == TT_Unknown) {
207 Left->Type = TT_ArraySubscriptLSquare;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000208 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000209
210 while (CurrentToken != NULL) {
211 if (CurrentToken->is(tok::r_square)) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000212 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) &&
213 Left->Type == TT_ObjCMethodExpr) {
Nico Weber5d2624e2013-02-06 06:20:11 +0000214 // An ObjC method call is rarely followed by an open parenthesis.
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000215 // FIXME: Do we incorrectly label ":" with this?
216 StartsObjCMethodExpr = false;
217 Left->Type = TT_Unknown;
218 }
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000219 if (StartsObjCMethodExpr) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000220 CurrentToken->Type = TT_ObjCMethodExpr;
Nico Weber5d2624e2013-02-06 06:20:11 +0000221 // determineStarAmpUsage() thinks that '*' '[' is allocating an
222 // array of pointers, but if '[' starts a selector then '*' is a
223 // binary operator.
Alexander Kornienkoa5151272013-03-12 16:28:18 +0000224 if (Parent != NULL && Parent->Type == TT_PointerOrReference)
Nico Weberac9bde22013-02-06 16:54:35 +0000225 Parent->Type = TT_BinaryOperator;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000226 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000227 Left->MatchingParen = CurrentToken;
228 CurrentToken->MatchingParen = Left;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000229 if (Contexts.back().FirstObjCSelectorName != NULL)
230 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
231 Contexts.back().LongestObjCSelectorName;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000232 next();
233 return true;
234 }
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000235 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000236 return false;
Daniel Jaspera1ea4cb2013-10-26 17:00:22 +0000237 if (CurrentToken->is(tok::colon))
238 ColonFound = true;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000239 if (CurrentToken->is(tok::comma) &&
Daniel Jasperb596fb22013-10-24 10:31:50 +0000240 (Left->Type == TT_ArraySubscriptLSquare ||
Daniel Jaspera1ea4cb2013-10-26 17:00:22 +0000241 (Left->Type == TT_ObjCMethodExpr && !ColonFound)))
Daniel Jasper1db6c382013-10-22 15:30:28 +0000242 Left->Type = TT_ArrayInitializerLSquare;
Daniel Jaspere11095a2013-02-14 15:01:34 +0000243 updateParameterCount(Left, CurrentToken);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000244 if (!consumeToken())
245 return false;
246 }
247 return false;
248 }
249
250 bool parseBrace() {
Daniel Jasper8e357692013-05-06 08:27:33 +0000251 if (CurrentToken != NULL) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000252 FormatToken *Left = CurrentToken->Previous;
Daniel Jasperb596fb22013-10-24 10:31:50 +0000253 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
254 Contexts.back().ColonIsDictLiteral = true;
Nico Weberced7d412013-05-26 05:39:26 +0000255
Daniel Jasper8e357692013-05-06 08:27:33 +0000256 while (CurrentToken != NULL) {
257 if (CurrentToken->is(tok::r_brace)) {
258 Left->MatchingParen = CurrentToken;
259 CurrentToken->MatchingParen = Left;
260 next();
261 return true;
262 }
263 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
264 return false;
265 updateParameterCount(Left, CurrentToken);
Daniel Jasperb596fb22013-10-24 10:31:50 +0000266 if (CurrentToken->is(tok::colon))
267 Left->Type = TT_DictLiteral;
Daniel Jasper8e357692013-05-06 08:27:33 +0000268 if (!consumeToken())
269 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000270 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000271 }
Daniel Jasper8e357692013-05-06 08:27:33 +0000272 // No closing "}" found, this probably starts a definition.
273 Line.StartsDefinition = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000274 return true;
275 }
Daniel Jasperdc7d5812013-02-20 12:56:39 +0000276
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000277 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000278 if (Current->is(tok::comma)) {
Daniel Jaspere11095a2013-02-14 15:01:34 +0000279 ++Left->ParameterCount;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000280 if (!Left->Role)
281 Left->Role.reset(new CommaSeparatedList(Style));
282 Left->Role->CommaFound(Current);
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000283 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
Daniel Jaspere11095a2013-02-14 15:01:34 +0000284 Left->ParameterCount = 1;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000285 }
Daniel Jaspere11095a2013-02-14 15:01:34 +0000286 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000287
288 bool parseConditional() {
289 while (CurrentToken != NULL) {
290 if (CurrentToken->is(tok::colon)) {
291 CurrentToken->Type = TT_ConditionalExpr;
292 next();
293 return true;
294 }
295 if (!consumeToken())
296 return false;
297 }
298 return false;
299 }
300
301 bool parseTemplateDeclaration() {
302 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
303 CurrentToken->Type = TT_TemplateOpener;
304 next();
305 if (!parseAngle())
306 return false;
Daniel Jasper00475962013-02-19 17:14:38 +0000307 if (CurrentToken != NULL)
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000308 CurrentToken->Previous->ClosesTemplateDeclaration = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000309 return true;
310 }
311 return false;
312 }
313
314 bool consumeToken() {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000315 FormatToken *Tok = CurrentToken;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000316 next();
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000317 switch (Tok->Tok.getKind()) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000318 case tok::plus:
319 case tok::minus:
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000320 if (Tok->Previous == NULL && Line.MustBeDeclaration)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000321 Tok->Type = TT_ObjCMethodSpecifier;
322 break;
323 case tok::colon:
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000324 if (Tok->Previous == NULL)
Daniel Jasper850677d2013-03-18 12:50:26 +0000325 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000326 // Colons from ?: are handled in parseConditional().
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000327 if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000328 Tok->Type = TT_CtorInitializerColon;
Daniel Jasperb596fb22013-10-24 10:31:50 +0000329 } else if (Contexts.back().ColonIsDictLiteral) {
330 Tok->Type = TT_DictLiteral;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000331 } else if (Contexts.back().ColonIsObjCMethodExpr ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000332 Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000333 Tok->Type = TT_ObjCMethodExpr;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000334 Tok->Previous->Type = TT_ObjCSelectorName;
Alexander Kornienko39856b72013-09-10 09:38:25 +0000335 if (Tok->Previous->ColumnWidth >
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000336 Contexts.back().LongestObjCSelectorName) {
Alexander Kornienko60d1b042013-10-10 13:36:20 +0000337 Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000338 }
Daniel Jasperc697ad22013-02-06 10:05:46 +0000339 if (Contexts.back().FirstObjCSelectorName == NULL)
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000340 Contexts.back().FirstObjCSelectorName = Tok->Previous;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000341 } else if (Contexts.back().ColonIsForRangeExpr) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000342 Tok->Type = TT_RangeBasedForLoopColon;
Alexander Kornienko60d1b042013-10-10 13:36:20 +0000343 } else if (CurrentToken != NULL &&
344 CurrentToken->is(tok::numeric_constant)) {
345 Tok->Type = TT_BitFieldColon;
Daniel Jasperf9a5e402013-10-08 16:24:07 +0000346 } else if (Contexts.size() == 1 && Line.First->isNot(tok::kw_enum)) {
Daniel Jaspereead02b2013-02-14 08:42:54 +0000347 Tok->Type = TT_InheritanceColon;
Daniel Jasper40aacf42013-03-14 13:45:21 +0000348 } else if (Contexts.back().ContextKind == tok::l_paren) {
349 Tok->Type = TT_InlineASMColon;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000350 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000351 break;
352 case tok::kw_if:
353 case tok::kw_while:
354 if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
355 next();
Nico Weber9096fc02013-06-26 00:30:14 +0000356 if (!parseParens(/*LookForDecls=*/true))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000357 return false;
358 }
359 break;
360 case tok::kw_for:
Daniel Jasperc697ad22013-02-06 10:05:46 +0000361 Contexts.back().ColonIsForRangeExpr = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000362 next();
363 if (!parseParens())
364 return false;
365 break;
366 case tok::l_paren:
367 if (!parseParens())
368 return false;
Daniel Jasperbc5cb4e2013-11-07 17:43:07 +0000369 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
370 !Contexts.back().IsExpression)
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000371 Line.MightBeFunctionDecl = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000372 break;
373 case tok::l_square:
374 if (!parseSquare())
375 return false;
376 break;
377 case tok::l_brace:
378 if (!parseBrace())
379 return false;
380 break;
381 case tok::less:
Daniel Jasper62c0ac02013-07-30 22:37:19 +0000382 if (Tok->Previous && !Tok->Previous->Tok.isLiteral() && parseAngle())
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000383 Tok->Type = TT_TemplateOpener;
384 else {
385 Tok->Type = TT_BinaryOperator;
386 CurrentToken = Tok;
387 next();
388 }
389 break;
390 case tok::r_paren:
391 case tok::r_square:
392 return false;
393 case tok::r_brace:
394 // Lines can start with '}'.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000395 if (Tok->Previous != NULL)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000396 return false;
397 break;
398 case tok::greater:
399 Tok->Type = TT_BinaryOperator;
400 break;
401 case tok::kw_operator:
Daniel Jasper42401c82013-09-02 09:20:39 +0000402 while (CurrentToken &&
403 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000404 if (CurrentToken->isOneOf(tok::star, tok::amp))
Daniel Jasper35d2dc72013-02-11 08:01:18 +0000405 CurrentToken->Type = TT_PointerOrReference;
406 consumeToken();
Daniel Jasper42401c82013-09-02 09:20:39 +0000407 if (CurrentToken && CurrentToken->Previous->Type == TT_BinaryOperator)
Daniel Jasperd215b8b2013-08-28 07:27:35 +0000408 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000409 }
Daniel Jasper8f9624b2013-05-10 07:59:58 +0000410 if (CurrentToken) {
Daniel Jasper35d2dc72013-02-11 08:01:18 +0000411 CurrentToken->Type = TT_OverloadedOperatorLParen;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000412 if (CurrentToken->Previous->Type == TT_BinaryOperator)
413 CurrentToken->Previous->Type = TT_OverloadedOperator;
Daniel Jasper8f9624b2013-05-10 07:59:58 +0000414 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000415 break;
416 case tok::question:
417 parseConditional();
418 break;
419 case tok::kw_template:
420 parseTemplateDeclaration();
421 break;
Nico Weber29f9dea2013-02-11 15:32:15 +0000422 case tok::identifier:
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000423 if (Line.First->is(tok::kw_for) &&
424 Tok->Tok.getIdentifierInfo() == &Ident_in)
Nico Weber29f9dea2013-02-11 15:32:15 +0000425 Tok->Type = TT_ObjCForIn;
426 break;
Daniel Jaspera628c982013-04-03 13:36:17 +0000427 case tok::comma:
428 if (Contexts.back().FirstStartOfName)
429 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000430 if (Contexts.back().InCtorInitializer)
431 Tok->Type = TT_CtorInitializerComma;
Daniel Jaspera628c982013-04-03 13:36:17 +0000432 break;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000433 default:
434 break;
435 }
436 return true;
437 }
438
439 void parseIncludeDirective() {
440 next();
441 if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
442 next();
443 while (CurrentToken != NULL) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000444 if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000445 CurrentToken->Type = TT_ImplicitStringLiteral;
446 next();
447 }
448 } else {
449 while (CurrentToken != NULL) {
Daniel Jasperaf5ba0e2013-02-23 07:46:38 +0000450 if (CurrentToken->is(tok::string_literal))
451 // Mark these string literals as "implicit" literals, too, so that
452 // they are not split or line-wrapped.
453 CurrentToken->Type = TT_ImplicitStringLiteral;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000454 next();
455 }
456 }
457 }
458
459 void parseWarningOrError() {
460 next();
461 // We still want to format the whitespace left of the first token of the
462 // warning or error.
463 next();
464 while (CurrentToken != NULL) {
465 CurrentToken->Type = TT_ImplicitStringLiteral;
466 next();
467 }
468 }
469
470 void parsePreprocessorDirective() {
471 next();
472 if (CurrentToken == NULL)
473 return;
Alexander Kornienko384b40b2013-10-11 21:43:05 +0000474 if (CurrentToken->Tok.is(tok::numeric_constant)) {
475 CurrentToken->SpacesRequiredBefore = 1;
476 return;
477 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000478 // Hashes in the middle of a line can lead to any strange token
479 // sequence.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000480 if (CurrentToken->Tok.getIdentifierInfo() == NULL)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000481 return;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000482 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000483 case tok::pp_include:
484 case tok::pp_import:
485 parseIncludeDirective();
486 break;
487 case tok::pp_error:
488 case tok::pp_warning:
489 parseWarningOrError();
490 break;
Daniel Jasper4431aa92013-04-23 13:54:04 +0000491 case tok::pp_if:
492 case tok::pp_elif:
493 parseLine();
494 break;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000495 default:
496 break;
497 }
Daniel Jaspera885dbe2013-02-05 09:34:14 +0000498 while (CurrentToken != NULL)
499 next();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000500 }
501
Nico Weber44449172013-02-12 16:17:07 +0000502public:
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000503 LineType parseLine() {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000504 if (CurrentToken->is(tok::hash)) {
505 parsePreprocessorDirective();
506 return LT_PreprocessorDirective;
507 }
508 while (CurrentToken != NULL) {
509 if (CurrentToken->is(tok::kw_virtual))
510 KeywordVirtualFound = true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000511 if (!consumeToken())
512 return LT_Invalid;
513 }
514 if (KeywordVirtualFound)
515 return LT_VirtualFunctionDecl;
516
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000517 if (Line.First->Type == TT_ObjCMethodSpecifier) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000518 if (Contexts.back().FirstObjCSelectorName != NULL)
519 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
520 Contexts.back().LongestObjCSelectorName;
Daniel Jasper1ac3e052013-02-05 10:07:47 +0000521 return LT_ObjCMethodDecl;
522 }
523
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000524 return LT_Other;
525 }
526
Nico Weber44449172013-02-12 16:17:07 +0000527private:
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000528 void next() {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000529 if (CurrentToken != NULL) {
530 determineTokenType(*CurrentToken);
Daniel Jasperc697ad22013-02-06 10:05:46 +0000531 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
Daniel Jasper63af7c42013-12-09 14:40:19 +0000532 CurrentToken->NestingLevel = Contexts.size() - 1;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000533 }
534
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000535 if (CurrentToken != NULL)
536 CurrentToken = CurrentToken->Next;
Daniel Jasper5065bc42013-02-18 12:44:35 +0000537
Manuel Klimek71814b42013-10-11 21:25:45 +0000538 if (CurrentToken != NULL) {
539 // Reset token type in case we have already looked at it and then
540 // recovered from an error (e.g. failure to find the matching >).
541 if (CurrentToken->Type != TT_LambdaLSquare &&
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000542 CurrentToken->Type != TT_FunctionLBrace &&
Manuel Klimek71814b42013-10-11 21:25:45 +0000543 CurrentToken->Type != TT_ImplicitStringLiteral)
544 CurrentToken->Type = TT_Unknown;
545 if (CurrentToken->Role)
546 CurrentToken->Role.reset(NULL);
547 CurrentToken->FakeLParens.clear();
548 CurrentToken->FakeRParens = 0;
549 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000550 }
551
Daniel Jasperc697ad22013-02-06 10:05:46 +0000552 /// \brief A struct to hold information valid in a specific context, e.g.
553 /// a pair of parenthesis.
554 struct Context {
Daniel Jasper40aacf42013-03-14 13:45:21 +0000555 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
556 bool IsExpression)
557 : ContextKind(ContextKind), BindingStrength(BindingStrength),
558 LongestObjCSelectorName(0), ColonIsForRangeExpr(false),
Daniel Jasperb596fb22013-10-24 10:31:50 +0000559 ColonIsDictLiteral(false), ColonIsObjCMethodExpr(false),
Nico Weberced7d412013-05-26 05:39:26 +0000560 FirstObjCSelectorName(NULL), FirstStartOfName(NULL),
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000561 IsExpression(IsExpression), CanBeExpression(true),
562 InCtorInitializer(false) {}
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000563
Daniel Jasper40aacf42013-03-14 13:45:21 +0000564 tok::TokenKind ContextKind;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000565 unsigned BindingStrength;
566 unsigned LongestObjCSelectorName;
567 bool ColonIsForRangeExpr;
Daniel Jasperb596fb22013-10-24 10:31:50 +0000568 bool ColonIsDictLiteral;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000569 bool ColonIsObjCMethodExpr;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000570 FormatToken *FirstObjCSelectorName;
571 FormatToken *FirstStartOfName;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000572 bool IsExpression;
Daniel Jasper97b89482013-03-13 07:49:51 +0000573 bool CanBeExpression;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000574 bool InCtorInitializer;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000575 };
576
577 /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
578 /// of each instance.
579 struct ScopedContextCreator {
580 AnnotatingParser &P;
581
Daniel Jasper40aacf42013-03-14 13:45:21 +0000582 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
583 unsigned Increase)
584 : P(P) {
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000585 P.Contexts.push_back(Context(ContextKind,
586 P.Contexts.back().BindingStrength + Increase,
587 P.Contexts.back().IsExpression));
Daniel Jasperc697ad22013-02-06 10:05:46 +0000588 }
589
590 ~ScopedContextCreator() { P.Contexts.pop_back(); }
591 };
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000592
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000593 void determineTokenType(FormatToken &Current) {
594 if (Current.getPrecedence() == prec::Assignment &&
Daniel Jasper59036852013-08-12 12:16:34 +0000595 !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000596 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000597 Contexts.back().IsExpression = true;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000598 for (FormatToken *Previous = Current.Previous;
Daniel Jasper5ca9b712013-09-11 20:37:10 +0000599 Previous && !Previous->isOneOf(tok::comma, tok::semi);
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000600 Previous = Previous->Previous) {
Daniel Jasper8e559272013-02-27 11:43:50 +0000601 if (Previous->is(tok::r_square))
602 Previous = Previous->MatchingParen;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000603 if (Previous->Type == TT_BinaryOperator &&
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000604 Previous->isOneOf(tok::star, tok::amp)) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000605 Previous->Type = TT_PointerOrReference;
606 }
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000607 }
Daniel Jasper3682fcd2013-12-16 08:36:18 +0000608 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000609 Contexts.back().IsExpression = true;
Daniel Jasper3682fcd2013-12-16 08:36:18 +0000610 } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
611 !Line.InPPDirective) {
612 bool ParametersOfFunctionType =
613 Current.Previous && Current.Previous->is(tok::r_paren) &&
614 Current.Previous->MatchingParen &&
615 Current.Previous->MatchingParen->Type == TT_FunctionTypeLParen;
616 bool IsForOrCatch = Current.Previous &&
617 Current.Previous->isOneOf(tok::kw_for, tok::kw_catch);
618 Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000619 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000620 for (FormatToken *Previous = Current.Previous;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000621 Previous && Previous->isOneOf(tok::star, tok::amp);
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000622 Previous = Previous->Previous)
Nico Weber44449172013-02-12 16:17:07 +0000623 Previous->Type = TT_PointerOrReference;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000624 } else if (Current.Previous &&
625 Current.Previous->Type == TT_CtorInitializerColon) {
Daniel Jasper5065bc42013-02-18 12:44:35 +0000626 Contexts.back().IsExpression = true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +0000627 Contexts.back().InCtorInitializer = true;
Daniel Jasper97b89482013-03-13 07:49:51 +0000628 } else if (Current.is(tok::kw_new)) {
629 Contexts.back().CanBeExpression = false;
Daniel Jaspera98da3d2013-11-07 19:56:07 +0000630 } else if (Current.is(tok::semi) || Current.is(tok::exclaim)) {
Daniel Jasperc37de302013-05-03 14:41:24 +0000631 // This should be the condition or increment in a for-loop.
632 Contexts.back().IsExpression = true;
Nico Weber44449172013-02-12 16:17:07 +0000633 }
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000634
635 if (Current.Type == TT_Unknown) {
Daniel Jasperf3167902013-09-27 08:29:16 +0000636 // Line.MightBeFunctionDecl can only be true after the parentheses of a
637 // function declaration have been found. In this case, 'Current' is a
638 // trailing token of this declaration and thus cannot be a name.
639 if (isStartOfName(Current) && !Line.MightBeFunctionDecl) {
Daniel Jaspera628c982013-04-03 13:36:17 +0000640 Contexts.back().FirstStartOfName = &Current;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000641 Current.Type = TT_StartOfName;
Daniel Jasper6cdec7c2013-07-09 14:36:48 +0000642 } else if (Current.is(tok::kw_auto)) {
643 AutoFound = true;
Daniel Jaspera3501d42013-07-11 14:33:06 +0000644 } else if (Current.is(tok::arrow) && AutoFound &&
645 Line.MustBeDeclaration) {
Daniel Jasper6cdec7c2013-07-09 14:36:48 +0000646 Current.Type = TT_TrailingReturnArrow;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000647 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
Daniel Jasperc697ad22013-02-06 10:05:46 +0000648 Current.Type =
Daniel Jasper6f9c8d22013-07-05 13:30:40 +0000649 determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
650 Contexts.back().IsExpression);
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000651 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000652 Current.Type = determinePlusMinusCaretUsage(Current);
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000653 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000654 Current.Type = determineIncrementUsage(Current);
655 } else if (Current.is(tok::exclaim)) {
656 Current.Type = TT_UnaryOperator;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000657 } else if (Current.isBinaryOperator() &&
658 (!Current.Previous ||
659 Current.Previous->isNot(tok::l_square))) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000660 Current.Type = TT_BinaryOperator;
661 } else if (Current.is(tok::comment)) {
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000662 if (Current.TokenText.startswith("//"))
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000663 Current.Type = TT_LineComment;
664 else
665 Current.Type = TT_BlockComment;
Nico Weberc6fe2162013-02-13 04:13:13 +0000666 } else if (Current.is(tok::r_paren)) {
Daniel Jasperda6f2252013-05-31 16:14:28 +0000667 FormatToken *LeftOfParens = NULL;
668 if (Current.MatchingParen)
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000669 LeftOfParens = Current.MatchingParen->getPreviousNonComment();
Daniel Jasperda6f2252013-05-31 16:14:28 +0000670 bool IsCast = false;
671 bool ParensAreEmpty = Current.Previous == Current.MatchingParen;
672 bool ParensAreType = !Current.Previous ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000673 Current.Previous->Type == TT_PointerOrReference ||
Daniel Jasperda6f2252013-05-31 16:14:28 +0000674 Current.Previous->Type == TT_TemplateCloser ||
675 isSimpleTypeSpecifier(*Current.Previous);
Nico Weberc6fe2162013-02-13 04:13:13 +0000676 bool ParensCouldEndDecl =
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000677 Current.Next &&
678 Current.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
Daniel Jasper8a68b952013-03-13 17:13:53 +0000679 bool IsSizeOfOrAlignOf =
Daniel Jasperda6f2252013-05-31 16:14:28 +0000680 LeftOfParens &&
681 LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
682 if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
Daniel Jasper49a94482013-07-15 15:04:42 +0000683 (Contexts.back().IsExpression ||
684 (Current.Next && Current.Next->isBinaryOperator())))
Daniel Jasperda6f2252013-05-31 16:14:28 +0000685 IsCast = true;
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000686 if (Current.Next && Current.Next->isNot(tok::string_literal) &&
Daniel Jasperda6f2252013-05-31 16:14:28 +0000687 (Current.Next->Tok.isLiteral() ||
688 Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
689 IsCast = true;
690 // If there is an identifier after the (), it is likely a cast, unless
691 // there is also an identifier before the ().
Daniel Jasper05866372013-06-06 08:20:20 +0000692 if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL ||
693 LeftOfParens->is(tok::kw_return)) &&
Daniel Jasper6a09df72013-07-08 14:58:01 +0000694 LeftOfParens->Type != TT_OverloadedOperator &&
Nico Weberec9e4102013-06-25 00:55:57 +0000695 LeftOfParens->Type != TT_TemplateCloser && Current.Next &&
696 Current.Next->is(tok::identifier))
Daniel Jasperda6f2252013-05-31 16:14:28 +0000697 IsCast = true;
698 if (IsCast && !ParensAreEmpty)
Nico Weberc6fe2162013-02-13 04:13:13 +0000699 Current.Type = TT_CastRParen;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000700 } else if (Current.is(tok::at) && Current.Next) {
701 switch (Current.Next->Tok.getObjCKeywordID()) {
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000702 case tok::objc_interface:
703 case tok::objc_implementation:
704 case tok::objc_protocol:
705 Current.Type = TT_ObjCDecl;
706 break;
707 case tok::objc_property:
708 Current.Type = TT_ObjCProperty;
709 break;
710 default:
711 break;
712 }
Daniel Jasperbca4bbe2013-05-28 11:30:49 +0000713 } else if (Current.is(tok::period)) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000714 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
Daniel Jasperbca4bbe2013-05-28 11:30:49 +0000715 if (PreviousNoComment &&
716 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
717 Current.Type = TT_DesignatedInitializerPeriod;
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000718 }
719 }
720 }
721
Daniel Jasperdba1c552013-07-02 09:47:29 +0000722 /// \brief Take a guess at whether \p Tok starts a name of a function or
723 /// variable declaration.
724 ///
725 /// This is a heuristic based on whether \p Tok is an identifier following
726 /// something that is likely a type.
727 bool isStartOfName(const FormatToken &Tok) {
728 if (Tok.isNot(tok::identifier) || Tok.Previous == NULL)
729 return false;
730
731 // Skip "const" as it does not have an influence on whether this is a name.
732 FormatToken *PreviousNotConst = Tok.Previous;
733 while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const))
734 PreviousNotConst = PreviousNotConst->Previous;
735
736 if (PreviousNotConst == NULL)
737 return false;
738
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +0000739 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
740 PreviousNotConst->Previous &&
741 PreviousNotConst->Previous->is(tok::hash);
Daniel Jasperdba1c552013-07-02 09:47:29 +0000742
Daniel Jasper53643062013-08-19 10:16:18 +0000743 if (PreviousNotConst->Type == TT_TemplateCloser)
744 return PreviousNotConst && PreviousNotConst->MatchingParen &&
745 PreviousNotConst->MatchingParen->Previous &&
746 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
747
Daniel Jasperdba1c552013-07-02 09:47:29 +0000748 return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
749 PreviousNotConst->Type == TT_PointerOrReference ||
Daniel Jasperdba1c552013-07-02 09:47:29 +0000750 isSimpleTypeSpecifier(*PreviousNotConst);
751 }
752
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000753 /// \brief Return the type of the given token assuming it is * or &.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000754 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000755 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000756 if (PrevToken == NULL)
757 return TT_UnaryOperator;
758
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000759 const FormatToken *NextToken = Tok.getNextNonComment();
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000760 if (NextToken == NULL)
761 return TT_Unknown;
762
Daniel Jasper37194282013-05-28 08:33:00 +0000763 if (PrevToken->is(tok::coloncolon) ||
764 (PrevToken->is(tok::l_paren) && !IsExpression))
Daniel Jasper8eb371b2013-03-01 17:13:29 +0000765 return TT_PointerOrReference;
766
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000767 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
Daniel Jasperae907642013-03-14 10:50:25 +0000768 tok::comma, tok::semi, tok::kw_return, tok::colon,
Daniel Jasperdf620b22013-09-21 17:31:51 +0000769 tok::equal, tok::kw_delete, tok::kw_sizeof) ||
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000770 PrevToken->Type == TT_BinaryOperator ||
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000771 PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
772 return TT_UnaryOperator;
773
Nico Weber5d2624e2013-02-06 06:20:11 +0000774 if (NextToken->is(tok::l_square))
775 return TT_PointerOrReference;
776
Daniel Jasper71665cd2013-09-10 10:26:38 +0000777 if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
778 PrevToken->MatchingParen->Previous &&
779 PrevToken->MatchingParen->Previous->is(tok::kw_typeof))
780 return TT_PointerOrReference;
781
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000782 if (PrevToken->Tok.isLiteral() ||
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000783 PrevToken->isOneOf(tok::r_paren, tok::r_square) ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000784 NextToken->Tok.isLiteral() || NextToken->isUnaryOperator())
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000785 return TT_BinaryOperator;
786
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000787 // It is very unlikely that we are going to find a pointer or reference type
788 // definition on the RHS of an assignment.
789 if (IsExpression)
790 return TT_BinaryOperator;
791
792 return TT_PointerOrReference;
793 }
794
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000795 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000796 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperda6f2252013-05-31 16:14:28 +0000797 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000798 return TT_UnaryOperator;
799
800 // Use heuristics to recognize unary operators.
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000801 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
802 tok::question, tok::colon, tok::kw_return,
803 tok::kw_case, tok::at, tok::l_brace))
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000804 return TT_UnaryOperator;
805
Nico Weberb76de882013-02-05 16:21:00 +0000806 // There can't be two consecutive binary operators.
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000807 if (PrevToken->Type == TT_BinaryOperator)
808 return TT_UnaryOperator;
809
810 // Fall back to marking the token as binary operator.
811 return TT_BinaryOperator;
812 }
813
814 /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000815 TokenType determineIncrementUsage(const FormatToken &Tok) {
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000816 const FormatToken *PrevToken = Tok.getPreviousNonComment();
Daniel Jasperda6f2252013-05-31 16:14:28 +0000817 if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000818 return TT_UnaryOperator;
Alexander Kornienko62b85b92013-03-13 14:41:29 +0000819 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
Daniel Jasper3a9370c2013-02-04 07:21:18 +0000820 return TT_TrailingUnaryOperator;
821
822 return TT_UnaryOperator;
823 }
Daniel Jasperc697ad22013-02-06 10:05:46 +0000824
Daniel Jaspera628c982013-04-03 13:36:17 +0000825 // FIXME: This is copy&pasted from Sema. Put it in a common place and remove
826 // duplication.
827 /// \brief Determine whether the token kind starts a simple-type-specifier.
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000828 bool isSimpleTypeSpecifier(const FormatToken &Tok) const {
829 switch (Tok.Tok.getKind()) {
Daniel Jaspera628c982013-04-03 13:36:17 +0000830 case tok::kw_short:
831 case tok::kw_long:
832 case tok::kw___int64:
833 case tok::kw___int128:
834 case tok::kw_signed:
835 case tok::kw_unsigned:
836 case tok::kw_void:
837 case tok::kw_char:
838 case tok::kw_int:
839 case tok::kw_half:
840 case tok::kw_float:
841 case tok::kw_double:
842 case tok::kw_wchar_t:
843 case tok::kw_bool:
844 case tok::kw___underlying_type:
Daniel Jaspera628c982013-04-03 13:36:17 +0000845 case tok::annot_typename:
846 case tok::kw_char16_t:
847 case tok::kw_char32_t:
848 case tok::kw_typeof:
849 case tok::kw_decltype:
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000850 return true;
Daniel Jaspera628c982013-04-03 13:36:17 +0000851 default:
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000852 return false;
Daniel Jaspera628c982013-04-03 13:36:17 +0000853 }
Daniel Jaspera628c982013-04-03 13:36:17 +0000854 }
855
Daniel Jasperc697ad22013-02-06 10:05:46 +0000856 SmallVector<Context, 8> Contexts;
857
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000858 const FormatStyle &Style;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000859 AnnotatedLine &Line;
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000860 FormatToken *CurrentToken;
Daniel Jasperc697ad22013-02-06 10:05:46 +0000861 bool KeywordVirtualFound;
Daniel Jasper6cdec7c2013-07-09 14:36:48 +0000862 bool AutoFound;
Nico Weber29f9dea2013-02-11 15:32:15 +0000863 IdentifierInfo &Ident_in;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000864};
865
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000866static int PrecedenceUnaryOperator = prec::PointerToMember + 1;
867static int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
868
Daniel Jasper400adc62013-02-08 15:28:42 +0000869/// \brief Parses binary expressions by inserting fake parenthesis based on
870/// operator precedence.
871class ExpressionParser {
872public:
Daniel Jasper6dcecb62013-06-06 09:11:58 +0000873 ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {
874 // Skip leading "}", e.g. in "} else if (...) {".
875 if (Current->is(tok::r_brace))
876 next();
877 }
Daniel Jasper400adc62013-02-08 15:28:42 +0000878
879 /// \brief Parse expressions with the given operatore precedence.
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000880 void parse(int Precedence = 0) {
Daniel Jasperf48b5ab2013-11-07 19:23:49 +0000881 // Skip 'return' and ObjC selector colons as they are not part of a binary
882 // expression.
883 while (Current &&
884 (Current->is(tok::kw_return) ||
885 (Current->is(tok::colon) && Current->Type == TT_ObjCMethodExpr)))
Daniel Jaspereabede62013-09-30 08:29:03 +0000886 next();
887
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000888 if (Current == NULL || Precedence > PrecedenceArrowAndPeriod)
Daniel Jasper0649d362013-08-23 15:14:03 +0000889 return;
890
Daniel Jasper2c611c02013-05-31 14:56:12 +0000891 // Conditional expressions need to be parsed separately for proper nesting.
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000892 if (Precedence == prec::Conditional) {
Daniel Jasper2c611c02013-05-31 14:56:12 +0000893 parseConditionalExpr();
894 return;
895 }
Daniel Jasper0649d362013-08-23 15:14:03 +0000896
897 // Parse unary operators, which all have a higher precedence than binary
898 // operators.
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000899 if (Precedence == PrecedenceUnaryOperator) {
Daniel Jasper0649d362013-08-23 15:14:03 +0000900 parseUnaryOperator();
Daniel Jasper400adc62013-02-08 15:28:42 +0000901 return;
Daniel Jasper0649d362013-08-23 15:14:03 +0000902 }
Daniel Jasper400adc62013-02-08 15:28:42 +0000903
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000904 FormatToken *Start = Current;
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000905 FormatToken *LatestOperator = NULL;
Daniel Jasper400adc62013-02-08 15:28:42 +0000906
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000907 while (Current) {
Daniel Jasper400adc62013-02-08 15:28:42 +0000908 // Consume operators with higher precedence.
Daniel Jasper6bee6822013-04-08 20:33:42 +0000909 parse(Precedence + 1);
Daniel Jasper400adc62013-02-08 15:28:42 +0000910
Daniel Jasper0649d362013-08-23 15:14:03 +0000911 int CurrentPrecedence = getCurrentPrecedence();
912
913 if (Current && Current->Type == TT_ObjCSelectorName &&
914 Precedence == CurrentPrecedence)
915 Start = Current;
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000916
Daniel Jasper400adc62013-02-08 15:28:42 +0000917 // At the end of the line or when an operator with higher precedence is
918 // found, insert fake parenthesis and return.
Daniel Jasperc04baae2013-04-10 09:49:49 +0000919 if (Current == NULL || Current->closesScope() ||
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000920 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) {
921 if (LatestOperator) {
922 if (Precedence == PrecedenceArrowAndPeriod) {
923 LatestOperator->LastInChainOfCalls = true;
924 // Call expressions don't have a binary operator precedence.
925 addFakeParenthesis(Start, prec::Unknown);
926 } else {
927 addFakeParenthesis(Start, prec::Level(Precedence));
928 }
929 }
Daniel Jasper400adc62013-02-08 15:28:42 +0000930 return;
931 }
932
933 // Consume scopes: (), [], <> and {}
Daniel Jasperc04baae2013-04-10 09:49:49 +0000934 if (Current->opensScope()) {
935 while (Current && !Current->closesScope()) {
Daniel Jasper400adc62013-02-08 15:28:42 +0000936 next();
937 parse();
938 }
939 next();
940 } else {
941 // Operator found.
Daniel Jaspercd8599e2013-02-23 21:01:55 +0000942 if (CurrentPrecedence == Precedence)
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000943 LatestOperator = Current;
Daniel Jasper400adc62013-02-08 15:28:42 +0000944
945 next();
946 }
947 }
948 }
949
950private:
Daniel Jasper0649d362013-08-23 15:14:03 +0000951 /// \brief Gets the precedence (+1) of the given token for binary operators
952 /// and other tokens that we treat like binary operators.
953 int getCurrentPrecedence() {
954 if (Current) {
955 if (Current->Type == TT_ConditionalExpr)
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000956 return prec::Conditional;
Daniel Jasperf48b5ab2013-11-07 19:23:49 +0000957 else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon ||
958 Current->Type == TT_ObjCSelectorName)
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000959 return 0;
Daniel Jasper0649d362013-08-23 15:14:03 +0000960 else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000961 return Current->getPrecedence();
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000962 else if (Current->isOneOf(tok::period, tok::arrow))
963 return PrecedenceArrowAndPeriod;
Daniel Jasper0649d362013-08-23 15:14:03 +0000964 }
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000965 return -1;
Daniel Jasper0649d362013-08-23 15:14:03 +0000966 }
967
Daniel Jasper2c611c02013-05-31 14:56:12 +0000968 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
969 Start->FakeLParens.push_back(Precedence);
Daniel Jasper562ecd42013-09-06 08:08:14 +0000970 if (Precedence > prec::Unknown)
971 Start->StartsBinaryExpression = true;
972 if (Current) {
Daniel Jasper2c611c02013-05-31 14:56:12 +0000973 ++Current->Previous->FakeRParens;
Daniel Jasper562ecd42013-09-06 08:08:14 +0000974 if (Precedence > prec::Unknown)
975 Current->Previous->EndsBinaryExpression = true;
976 }
Daniel Jasper2c611c02013-05-31 14:56:12 +0000977 }
978
Daniel Jasper0649d362013-08-23 15:14:03 +0000979 /// \brief Parse unary operator expressions and surround them with fake
980 /// parentheses if appropriate.
981 void parseUnaryOperator() {
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000982 if (Current == NULL || Current->Type != TT_UnaryOperator) {
983 parse(PrecedenceArrowAndPeriod);
Daniel Jasper0649d362013-08-23 15:14:03 +0000984 return;
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000985 }
Daniel Jasper0649d362013-08-23 15:14:03 +0000986
987 FormatToken *Start = Current;
988 next();
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000989 parseUnaryOperator();
Daniel Jasper0649d362013-08-23 15:14:03 +0000990
Daniel Jasper0649d362013-08-23 15:14:03 +0000991 // The actual precedence doesn't matter.
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000992 addFakeParenthesis(Start, prec::Unknown);
Daniel Jasper0649d362013-08-23 15:14:03 +0000993 }
994
Daniel Jasper2c611c02013-05-31 14:56:12 +0000995 void parseConditionalExpr() {
996 FormatToken *Start = Current;
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000997 parse(prec::LogicalOr);
Daniel Jasper2c611c02013-05-31 14:56:12 +0000998 if (!Current || !Current->is(tok::question))
999 return;
1000 next();
Daniel Jasperb27c4b72013-08-27 11:09:05 +00001001 parse(prec::LogicalOr);
Daniel Jasper2c611c02013-05-31 14:56:12 +00001002 if (!Current || Current->Type != TT_ConditionalExpr)
1003 return;
1004 next();
1005 parseConditionalExpr();
1006 addFakeParenthesis(Start, prec::Conditional);
1007 }
1008
Daniel Jasper400adc62013-02-08 15:28:42 +00001009 void next() {
Alexander Kornienkoafaa8f52013-06-17 13:19:53 +00001010 if (Current)
1011 Current = Current->Next;
1012 while (Current && Current->isTrailingComment())
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001013 Current = Current->Next;
Daniel Jasper400adc62013-02-08 15:28:42 +00001014 }
1015
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001016 FormatToken *Current;
Daniel Jasper400adc62013-02-08 15:28:42 +00001017};
1018
Craig Topper318ed7c2013-07-01 04:03:19 +00001019} // end anonymous namespace
1020
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001021void
1022TokenAnnotator::setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines) {
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001023 const AnnotatedLine *NextNonCommentLine = NULL;
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001024 for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
1025 E = Lines.rend();
1026 I != E; ++I) {
1027 if (NextNonCommentLine && (*I)->First->is(tok::comment) &&
1028 (*I)->First->Next == NULL)
1029 (*I)->Level = NextNonCommentLine->Level;
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001030 else
Daniel Jasperbbf5c1c2013-11-05 19:10:03 +00001031 NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : NULL;
1032
1033 setCommentLineLevels((*I)->Children);
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001034 }
1035}
1036
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001037void TokenAnnotator::annotate(AnnotatedLine &Line) {
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001038 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1039 E = Line.Children.end();
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001040 I != E; ++I) {
1041 annotate(**I);
1042 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +00001043 AnnotatingParser Parser(Style, Line, Ident_in);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001044 Line.Type = Parser.parseLine();
1045 if (Line.Type == LT_Invalid)
1046 return;
1047
Daniel Jasper400adc62013-02-08 15:28:42 +00001048 ExpressionParser ExprParser(Line);
1049 ExprParser.parse();
1050
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001051 if (Line.First->Type == TT_ObjCMethodSpecifier)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001052 Line.Type = LT_ObjCMethodDecl;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001053 else if (Line.First->Type == TT_ObjCDecl)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001054 Line.Type = LT_ObjCDecl;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001055 else if (Line.First->Type == TT_ObjCProperty)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001056 Line.Type = LT_ObjCProperty;
1057
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001058 Line.First->SpacesRequiredBefore = 1;
1059 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001060}
1061
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001062void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
Alexander Kornienko39856b72013-09-10 09:38:25 +00001063 Line.First->TotalLength =
1064 Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001065 if (!Line.First->Next)
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001066 return;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001067 FormatToken *Current = Line.First->Next;
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001068 bool InFunctionDecl = Line.MightBeFunctionDecl;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001069 while (Current != NULL) {
Daniel Jaspereef30492013-02-11 12:36:37 +00001070 if (Current->Type == TT_LineComment)
1071 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
Alexander Kornienko384b40b2013-10-11 21:43:05 +00001072 else if (Current->SpacesRequiredBefore == 0 &&
1073 spaceRequiredBefore(Line, *Current))
1074 Current->SpacesRequiredBefore = 1;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001075
Daniel Jasperfb81b092013-09-17 09:52:48 +00001076 Current->MustBreakBefore =
1077 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
1078
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001079 Current->CanBreakBefore =
1080 Current->MustBreakBefore || canBreakBefore(Line, *Current);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001081 if (Current->MustBreakBefore || !Current->Children.empty() ||
Alexander Kornienko39856b72013-09-10 09:38:25 +00001082 Current->IsMultiline)
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001083 Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit;
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001084 else
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +00001085 Current->TotalLength = Current->Previous->TotalLength +
Alexander Kornienko39856b72013-09-10 09:38:25 +00001086 Current->ColumnWidth +
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +00001087 Current->SpacesRequiredBefore;
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001088
1089 if (Current->Type == TT_CtorInitializerColon)
1090 InFunctionDecl = false;
1091
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001092 // FIXME: Only calculate this if CanBreakBefore is true once static
1093 // initializers etc. are sorted out.
1094 // FIXME: Move magic numbers to a better place.
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001095 Current->SplitPenalty = 20 * Current->BindingStrength +
1096 splitPenalty(Line, *Current, InFunctionDecl);
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001097
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001098 Current = Current->Next;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001099 }
Daniel Jasper6bee6822013-04-08 20:33:42 +00001100
Manuel Klimek4fe43002013-05-22 12:51:29 +00001101 calculateUnbreakableTailLengths(Line);
Daniel Jasper8de9ed02013-08-22 15:00:41 +00001102 for (Current = Line.First; Current != NULL; Current = Current->Next) {
1103 if (Current->Role)
1104 Current->Role->precomputeFormattingInfos(Current);
1105 }
1106
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001107 DEBUG({ printDebugInfo(Line); });
1108
Daniel Jasper1c5d9df2013-09-06 07:54:20 +00001109 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
1110 E = Line.Children.end();
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001111 I != E; ++I) {
1112 calculateFormattingInformation(**I);
1113 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001114}
1115
Manuel Klimek4fe43002013-05-22 12:51:29 +00001116void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1117 unsigned UnbreakableTailLength = 0;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001118 FormatToken *Current = Line.Last;
Manuel Klimek4fe43002013-05-22 12:51:29 +00001119 while (Current != NULL) {
1120 Current->UnbreakableTailLength = UnbreakableTailLength;
1121 if (Current->CanBreakBefore ||
1122 Current->isOneOf(tok::comment, tok::string_literal)) {
1123 UnbreakableTailLength = 0;
1124 } else {
1125 UnbreakableTailLength +=
Alexander Kornienko39856b72013-09-10 09:38:25 +00001126 Current->ColumnWidth + Current->SpacesRequiredBefore;
Manuel Klimek4fe43002013-05-22 12:51:29 +00001127 }
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001128 Current = Current->Previous;
Manuel Klimek4fe43002013-05-22 12:51:29 +00001129 }
1130}
1131
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001132unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001133 const FormatToken &Tok,
1134 bool InFunctionDecl) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001135 const FormatToken &Left = *Tok.Previous;
1136 const FormatToken &Right = Tok;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001137
Daniel Jasperbca4bbe2013-05-28 11:30:49 +00001138 if (Left.is(tok::semi))
1139 return 0;
1140 if (Left.is(tok::comma))
1141 return 1;
Daniel Jasperaea3bde2013-07-12 11:19:37 +00001142 if (Right.is(tok::l_square))
Daniel Jaspere0ab9e72013-12-06 15:19:50 +00001143 return 250;
Daniel Jasperbca4bbe2013-05-28 11:30:49 +00001144
Daniel Jasper6331da02013-07-09 07:43:55 +00001145 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001146 if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
Daniel Jasper26d1b1d2013-02-24 18:54:32 +00001147 return 3;
Daniel Jasper40db06a2013-07-11 12:34:23 +00001148 if (Left.Type == TT_StartOfName)
1149 return 20;
Daniel Jasper63af7c42013-12-09 14:40:19 +00001150 if (InFunctionDecl && Right.NestingLevel == 0)
Daniel Jasper26d1b1d2013-02-24 18:54:32 +00001151 return Style.PenaltyReturnTypeOnItsOwnLine;
Daniel Jasper53643062013-08-19 10:16:18 +00001152 return 200;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +00001153 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001154 if (Left.is(tok::equal) && Right.is(tok::l_brace))
1155 return 150;
Daniel Jasper1bc1b502013-07-05 07:58:34 +00001156 if (Left.Type == TT_CastRParen)
1157 return 100;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001158 if (Left.is(tok::coloncolon))
1159 return 500;
Daniel Jasper83193602013-04-05 17:22:09 +00001160 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
1161 return 5000;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001162
Daniel Jaspereead02b2013-02-14 08:42:54 +00001163 if (Left.Type == TT_RangeBasedForLoopColon ||
1164 Left.Type == TT_InheritanceColon)
Daniel Jasper16b35622013-02-26 13:18:08 +00001165 return 2;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001166
Daniel Jasper4c6e0052013-08-27 14:24:43 +00001167 if (Right.isMemberAccess()) {
Daniel Jasper36c28ce2013-09-06 08:54:24 +00001168 if (Left.isOneOf(tok::r_paren, tok::r_square) && Left.MatchingParen &&
1169 Left.MatchingParen->ParameterCount > 0)
Daniel Jasper70bc8742013-02-26 13:59:14 +00001170 return 20; // Should be smaller than breaking at a nested comma.
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001171 return 150;
1172 }
1173
Daniel Jasper718bd362013-07-11 21:02:56 +00001174 // Breaking before a trailing 'const' or not-function-like annotation is bad.
Daniel Jasper9688ff12013-08-01 13:46:58 +00001175 if (Left.is(tok::r_paren) && Line.Type != LT_ObjCProperty &&
Daniel Jasper718bd362013-07-11 21:02:56 +00001176 (Right.is(tok::kw_const) || (Right.is(tok::identifier) && Right.Next &&
1177 Right.Next->isNot(tok::l_paren))))
Daniel Jasperd8c36d02013-10-18 16:34:40 +00001178 return 100;
Daniel Jasper13c37b32013-05-22 08:28:26 +00001179
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001180 // In for-loops, prefer breaking at ',' and ';'.
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001181 if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
Daniel Jasper37905f72013-02-21 15:00:29 +00001182 return 4;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001183
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001184 // In Objective-C method expressions, prefer breaking before "param:" over
1185 // breaking after it.
Daniel Jasper1ac3e052013-02-05 10:07:47 +00001186 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001187 return 0;
Daniel Jasper1ac3e052013-02-05 10:07:47 +00001188 if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
Daniel Jasper4bf0d802013-11-23 14:27:27 +00001189 return Line.MightBeFunctionDecl ? 50 : 500;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001190
Daniel Jasper4fcc8b92013-11-07 17:52:51 +00001191 if (Left.is(tok::l_paren) && InFunctionDecl)
Daniel Jasper6728fc12013-04-11 14:29:13 +00001192 return 100;
Daniel Jasperc04baae2013-04-10 09:49:49 +00001193 if (Left.opensScope())
Daniel Jasper33b909c2013-10-25 14:29:37 +00001194 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
1195 : 19;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001196
Daniel Jasperba9ddb62013-02-06 21:04:05 +00001197 if (Right.is(tok::lessless)) {
1198 if (Left.is(tok::string_literal)) {
Alexander Kornienkoffcc0102013-06-05 14:09:10 +00001199 StringRef Content = Left.TokenText;
Daniel Jasper0b1f76b2013-09-29 12:02:57 +00001200 if (Content.startswith("\""))
1201 Content = Content.drop_front(1);
1202 if (Content.endswith("\""))
1203 Content = Content.drop_back(1);
1204 Content = Content.trim();
Daniel Jasperf38a0ac2013-03-14 14:00:17 +00001205 if (Content.size() > 1 &&
1206 (Content.back() == ':' || Content.back() == '='))
Daniel Jasperfa21c072013-07-15 14:33:14 +00001207 return 25;
Daniel Jasperba9ddb62013-02-06 21:04:05 +00001208 }
Daniel Jasper49a94482013-07-15 15:04:42 +00001209 return 1; // Breaking at a << is really cheap.
Daniel Jasperba9ddb62013-02-06 21:04:05 +00001210 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001211 if (Left.Type == TT_ConditionalExpr)
Daniel Jasper70bc8742013-02-26 13:59:14 +00001212 return prec::Conditional;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001213 prec::Level Level = Left.getPrecedence();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001214
1215 if (Level != prec::Unknown)
1216 return Level;
Daniel Jasperf9a84b52013-03-01 16:48:32 +00001217
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001218 return 3;
1219}
1220
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001221bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001222 const FormatToken &Left,
1223 const FormatToken &Right) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001224 if (Right.is(tok::hashhash))
1225 return Left.is(tok::hash);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001226 if (Left.isOneOf(tok::hashhash, tok::hash))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001227 return Right.is(tok::hash);
Daniel Jasperb55acad2013-08-20 12:36:34 +00001228 if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
1229 return Style.SpaceInEmptyParentheses;
1230 if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
Daniel Jasperf110e202013-08-21 08:39:01 +00001231 return (Right.Type == TT_CastRParen ||
1232 (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen))
Daniel Jasperb55acad2013-08-20 12:36:34 +00001233 ? Style.SpacesInCStyleCastParentheses
1234 : Style.SpacesInParentheses;
Daniel Jasperdd978ae2013-10-29 14:52:02 +00001235 if (Style.SpacesInAngles &&
1236 ((Left.Type == TT_TemplateOpener) != (Right.Type == TT_TemplateCloser)))
1237 return true;
Daniel Jasperb55acad2013-08-20 12:36:34 +00001238 if (Right.isOneOf(tok::semi, tok::comma))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001239 return false;
1240 if (Right.is(tok::less) &&
1241 (Left.is(tok::kw_template) ||
1242 (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
1243 return true;
1244 if (Left.is(tok::arrow) || Right.is(tok::arrow))
1245 return false;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001246 if (Left.isOneOf(tok::exclaim, tok::tilde))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001247 return false;
1248 if (Left.is(tok::at) &&
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001249 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1250 tok::numeric_constant, tok::l_paren, tok::l_brace,
1251 tok::kw_true, tok::kw_false))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001252 return false;
1253 if (Left.is(tok::coloncolon))
1254 return false;
1255 if (Right.is(tok::coloncolon))
Daniel Jasperfba84ff2013-10-12 05:16:06 +00001256 return (Left.is(tok::less) && Style.Standard == FormatStyle::LS_Cpp03) ||
1257 !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren,
1258 tok::r_paren, tok::less);
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001259 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001260 return false;
Daniel Jasperbafa6b72013-07-01 09:47:25 +00001261 if (Right.is(tok::ellipsis))
Daniel Jasper2d0cd492013-10-20 16:56:16 +00001262 return Left.Tok.isLiteral();
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001263 if (Left.is(tok::l_square) && Right.is(tok::amp))
1264 return false;
Alexander Kornienkoa5151272013-03-12 16:28:18 +00001265 if (Right.Type == TT_PointerOrReference)
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001266 return Left.Tok.isLiteral() ||
Alexander Kornienkoa5151272013-03-12 16:28:18 +00001267 ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
1268 !Style.PointerBindsToType);
Daniel Jasper4d03d3b2013-05-28 15:27:10 +00001269 if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
Daniel Jaspercfda5172013-05-08 14:58:20 +00001270 (Left.Type != TT_PointerOrReference || Style.PointerBindsToType))
1271 return true;
Alexander Kornienkoa5151272013-03-12 16:28:18 +00001272 if (Left.Type == TT_PointerOrReference)
Daniel Jasper022612d2013-07-01 09:34:09 +00001273 return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
Daniel Jasperb8914dd2013-03-20 09:53:18 +00001274 ((Right.Type != TT_PointerOrReference) &&
Daniel Jasper6e42b1e2013-04-01 17:13:26 +00001275 Right.isNot(tok::l_paren) && Style.PointerBindsToType &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001276 Left.Previous &&
1277 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001278 if (Right.is(tok::star) && Left.is(tok::l_paren))
1279 return false;
Nico Weber2a726b62013-02-10 02:08:05 +00001280 if (Left.is(tok::l_square))
Daniel Jasper1db6c382013-10-22 15:30:28 +00001281 return Left.Type == TT_ArrayInitializerLSquare &&
1282 Right.isNot(tok::r_square);
Nico Weber2a726b62013-02-10 02:08:05 +00001283 if (Right.is(tok::r_square))
Daniel Jasper1db6c382013-10-22 15:30:28 +00001284 return Right.MatchingParen &&
1285 Right.MatchingParen->Type == TT_ArrayInitializerLSquare;
Daniel Jasper8ddfa842013-08-30 10:36:58 +00001286 if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr &&
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001287 Right.Type != TT_LambdaLSquare && Left.isNot(tok::numeric_constant))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001288 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001289 if (Left.is(tok::colon))
1290 return Left.Type != TT_ObjCMethodExpr;
1291 if (Right.is(tok::colon))
Daniel Jasperb1ae7342013-08-01 22:05:00 +00001292 return Right.Type != TT_ObjCMethodExpr && !Left.is(tok::question);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001293 if (Right.is(tok::l_paren)) {
Daniel Jasperee6d6502013-07-17 20:25:02 +00001294 if (Left.is(tok::r_paren) && Left.MatchingParen &&
1295 Left.MatchingParen->Previous &&
1296 Left.MatchingParen->Previous->is(tok::kw___attribute))
1297 return true;
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001298 return Line.Type == LT_ObjCDecl ||
Daniel Jasperf110e202013-08-21 08:39:01 +00001299 Left.isOneOf(tok::kw_return, tok::kw_new, tok::kw_delete,
1300 tok::semi) ||
Alexander Kornienkofdca83d2013-12-10 10:18:34 +00001301 (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
Daniel Jasperf110e202013-08-21 08:39:01 +00001302 Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
Alexander Kornienkofdca83d2013-12-10 10:18:34 +00001303 tok::kw_catch)) ||
1304 (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
1305 Left.isOneOf(tok::identifier, tok::kw___attribute) &&
1306 Line.Type != LT_PreprocessorDirective);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001307 }
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001308 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001309 return false;
1310 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001311 return !Left.Children.empty(); // No spaces in "{}".
Daniel Jaspere5777d22013-05-23 10:15:45 +00001312 if (Left.is(tok::l_brace) || Right.is(tok::r_brace))
Daniel Jasper6ab54682013-07-16 18:22:10 +00001313 return !Style.Cpp11BracedListStyle;
Daniel Jasper40bc7462013-11-23 14:51:47 +00001314 if (Left.Type == TT_BlockComment && Left.TokenText.endswith("=*/"))
1315 return false;
Daniel Jasper5bd0b9e2013-05-23 18:05:18 +00001316 if (Right.Type == TT_UnaryOperator)
1317 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
1318 (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
Daniel Jasperf632f692013-05-23 21:35:49 +00001319 if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) &&
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001320 Right.is(tok::l_brace) && Right.getNextNonComment() &&
1321 Right.BlockKind != BK_Block)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001322 return false;
Daniel Jasperbca4bbe2013-05-28 11:30:49 +00001323 if (Left.is(tok::period) || Right.is(tok::period))
1324 return false;
Alexander Kornienkod8d47fa2013-09-10 13:41:43 +00001325 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
1326 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001327 return true;
1328}
1329
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001330bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001331 const FormatToken &Tok) {
1332 if (Tok.Tok.getIdentifierInfo() && Tok.Previous->Tok.getIdentifierInfo())
Daniel Jasper35d2dc72013-02-11 08:01:18 +00001333 return true; // Never ever merge two identifiers.
Daniel Jasper877615c2013-10-11 19:45:02 +00001334 if (Tok.Previous->Type == TT_ImplicitStringLiteral)
1335 return Tok.WhitespaceRange.getBegin() != Tok.WhitespaceRange.getEnd();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001336 if (Line.Type == LT_ObjCMethodDecl) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001337 if (Tok.Previous->Type == TT_ObjCMethodSpecifier)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001338 return true;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001339 if (Tok.Previous->is(tok::r_paren) && Tok.is(tok::identifier))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001340 // Don't space between ')' and <id>
1341 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001342 }
1343 if (Line.Type == LT_ObjCProperty &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001344 (Tok.is(tok::equal) || Tok.Previous->is(tok::equal)))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001345 return false;
1346
Daniel Jasper6cdec7c2013-07-09 14:36:48 +00001347 if (Tok.Type == TT_TrailingReturnArrow ||
1348 Tok.Previous->Type == TT_TrailingReturnArrow)
1349 return true;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001350 if (Tok.Previous->is(tok::comma))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001351 return true;
Daniel Jasperff6c3a92013-02-28 13:40:17 +00001352 if (Tok.is(tok::comma))
1353 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001354 if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
1355 return true;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001356 if (Tok.Previous->Tok.is(tok::kw_operator))
Daniel Jasperedc5f092013-10-29 12:24:23 +00001357 return Tok.is(tok::coloncolon);
Daniel Jasper35d2dc72013-02-11 08:01:18 +00001358 if (Tok.Type == TT_OverloadedOperatorLParen)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001359 return false;
1360 if (Tok.is(tok::colon))
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001361 return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
Daniel Jasperb1ae7342013-08-01 22:05:00 +00001362 Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr &&
1363 !Tok.Previous->is(tok::question);
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001364 if (Tok.Previous->Type == TT_UnaryOperator ||
1365 Tok.Previous->Type == TT_CastRParen)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001366 return false;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001367 if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) {
Daniel Jasper400adc62013-02-08 15:28:42 +00001368 return Tok.Type == TT_TemplateCloser &&
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001369 Tok.Previous->Type == TT_TemplateCloser &&
Daniel Jasperdd978ae2013-10-29 14:52:02 +00001370 (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001371 }
Alexander Kornienko674be0a2013-03-20 16:41:56 +00001372 if (Tok.isOneOf(tok::arrowstar, tok::periodstar) ||
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001373 Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar))
Daniel Jasperff6c3a92013-02-28 13:40:17 +00001374 return false;
Daniel Jasperd94bff32013-09-25 15:15:02 +00001375 if (!Style.SpaceBeforeAssignmentOperators &&
1376 Tok.getPrecedence() == prec::Assignment)
1377 return false;
Daniel Jasper9613c812013-08-07 16:29:23 +00001378 if ((Tok.Type == TT_BinaryOperator && !Tok.Previous->is(tok::l_paren)) ||
1379 Tok.Previous->Type == TT_BinaryOperator)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001380 return true;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001381 if (Tok.Previous->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001382 return false;
Daniel Jaspered8f1c62013-08-28 08:24:04 +00001383 if (Tok.is(tok::less) && Tok.Previous->isNot(tok::l_paren) &&
1384 Line.First->is(tok::hash))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001385 return true;
1386 if (Tok.Type == TT_TrailingUnaryOperator)
1387 return false;
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001388 return spaceRequiredBetween(Line, *Tok.Previous, Tok);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001389}
1390
Daniel Jasperfb81b092013-09-17 09:52:48 +00001391bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
1392 const FormatToken &Right) {
1393 if (Right.is(tok::comment)) {
1394 return Right.NewlinesBefore > 0;
1395 } else if (Right.Previous->isTrailingComment() ||
1396 (Right.is(tok::string_literal) &&
1397 Right.Previous->is(tok::string_literal))) {
1398 return true;
1399 } else if (Right.Previous->IsUnterminatedLiteral) {
1400 return true;
1401 } else if (Right.is(tok::lessless) && Right.Next &&
1402 Right.Previous->is(tok::string_literal) &&
1403 Right.Next->is(tok::string_literal)) {
1404 return true;
1405 } else if (Right.Previous->ClosesTemplateDeclaration &&
1406 Right.Previous->MatchingParen &&
Daniel Jasper63af7c42013-12-09 14:40:19 +00001407 Right.Previous->MatchingParen->NestingLevel == 0 &&
Daniel Jasperfb81b092013-09-17 09:52:48 +00001408 Style.AlwaysBreakTemplateDeclarations) {
Daniel Jasperfb81b092013-09-17 09:52:48 +00001409 return true;
1410 } else if (Right.Type == TT_CtorInitializerComma &&
Daniel Jasperec01cd62013-10-08 05:11:18 +00001411 Style.BreakConstructorInitializersBeforeComma &&
1412 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
Daniel Jasperfb81b092013-09-17 09:52:48 +00001413 return true;
1414 } else if (Right.Previous->BlockKind == BK_Block &&
Alexander Kornienko60d1b042013-10-10 13:36:20 +00001415 Right.Previous->isNot(tok::r_brace) && Right.isNot(tok::r_brace)) {
Daniel Jasperfb81b092013-09-17 09:52:48 +00001416 return true;
1417 } else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001418 return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1419 Style.BreakBeforeBraces == FormatStyle::BS_GNU;
Daniel Jasperc39b56f2013-12-16 07:23:08 +00001420 } else if (Right.is(tok::string_literal) &&
1421 Right.TokenText.startswith("R\"")) {
1422 // Raw string literals are special wrt. line breaks. The author has made a
1423 // deliberate choice and might have aligned the contents of the string
1424 // literal accordingly. Thus, we try keep existing line breaks.
1425 return Right.NewlinesBefore > 0;
Daniel Jasperfb81b092013-09-17 09:52:48 +00001426 }
1427 return false;
1428}
1429
Daniel Jasper7fce3ab2013-02-06 14:22:40 +00001430bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001431 const FormatToken &Right) {
1432 const FormatToken &Left = *Right.Previous;
Daniel Jasper4bf0d802013-11-23 14:27:27 +00001433 if (Left.is(tok::at))
1434 return false;
Daniel Jasper6331da02013-07-09 07:43:55 +00001435 if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001436 return true;
Daniel Jasper165b29e2013-11-08 00:57:11 +00001437 if (Right.isTrailingComment())
1438 // We rely on MustBreakBefore being set correctly here as we should not
1439 // change the "binding" behavior of a comment.
1440 return false;
1441 if (Left.is(tok::question) && Right.is(tok::colon))
1442 return false;
1443 if (Right.Type == TT_ConditionalExpr || Right.is(tok::question))
1444 return Style.BreakBeforeTernaryOperators;
1445 if (Left.Type == TT_ConditionalExpr || Left.is(tok::question))
1446 return !Style.BreakBeforeTernaryOperators;
Nico Weberced7d412013-05-26 05:39:26 +00001447 if (Right.is(tok::colon) &&
Daniel Jasperb596fb22013-10-24 10:31:50 +00001448 (Right.Type == TT_DictLiteral || Right.Type == TT_ObjCMethodExpr))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001449 return false;
Nico Weberced7d412013-05-26 05:39:26 +00001450 if (Left.is(tok::colon) &&
Daniel Jasperb596fb22013-10-24 10:31:50 +00001451 (Left.Type == TT_DictLiteral || Left.Type == TT_ObjCMethodExpr))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001452 return true;
Daniel Jasper1ac3e052013-02-05 10:07:47 +00001453 if (Right.Type == TT_ObjCSelectorName)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001454 return true;
Daniel Jasper9688ff12013-08-01 13:46:58 +00001455 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
1456 return true;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001457 if (Left.ClosesTemplateDeclaration)
1458 return true;
Daniel Jaspereead02b2013-02-14 08:42:54 +00001459 if (Right.Type == TT_RangeBasedForLoopColon ||
Daniel Jasperd215b8b2013-08-28 07:27:35 +00001460 Right.Type == TT_OverloadedOperatorLParen ||
1461 Right.Type == TT_OverloadedOperator)
Daniel Jaspereead02b2013-02-14 08:42:54 +00001462 return false;
Daniel Jaspera61aefb2013-05-06 06:45:09 +00001463 if (Left.Type == TT_RangeBasedForLoopColon)
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001464 return true;
Daniel Jasper37905f72013-02-21 15:00:29 +00001465 if (Right.Type == TT_RangeBasedForLoopColon)
1466 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001467 if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
Daniel Jasper165b29e2013-11-08 00:57:11 +00001468 Left.Type == TT_UnaryOperator || Left.is(tok::kw_operator))
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001469 return false;
1470 if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
1471 return false;
Daniel Jasper1bc1b502013-07-05 07:58:34 +00001472 if (Left.Previous) {
1473 if (Left.is(tok::l_paren) && Right.is(tok::l_paren) &&
1474 Left.Previous->is(tok::kw___attribute))
1475 return false;
Daniel Jasper6cdec7c2013-07-09 14:36:48 +00001476 if (Left.is(tok::l_paren) && (Left.Previous->Type == TT_BinaryOperator ||
Daniel Jasper998cabc2013-07-18 14:46:07 +00001477 Left.Previous->Type == TT_CastRParen))
Daniel Jasper1bc1b502013-07-05 07:58:34 +00001478 return false;
1479 }
Daniel Jasper98857842013-10-30 13:54:53 +00001480 if (Right.Type == TT_ImplicitStringLiteral)
1481 return false;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001482
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001483 if (Right.is(tok::r_paren) || Right.Type == TT_TemplateCloser)
1484 return false;
1485
Daniel Jasper13c37b32013-05-22 08:28:26 +00001486 // We only break before r_brace if there was a corresponding break before
1487 // the l_brace, which is tracked by BreakBeforeClosingBrace.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001488 if (Right.is(tok::r_brace))
1489 return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
Daniel Jasper13c37b32013-05-22 08:28:26 +00001490
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001491 // Allow breaking after a trailing 'const', e.g. after a method declaration,
1492 // unless it is follow by ';', '{' or '='.
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001493 if (Left.is(tok::kw_const) && Left.Previous != NULL &&
1494 Left.Previous->is(tok::r_paren))
Alexander Kornienko62b85b92013-03-13 14:41:29 +00001495 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001496
Daniel Jasperbf4755b2013-03-14 09:50:46 +00001497 if (Right.is(tok::kw___attribute))
1498 return true;
1499
Daniel Jasperaf5ba0e2013-02-23 07:46:38 +00001500 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
1501 return true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +00001502
1503 if (Left.Type == TT_CtorInitializerComma &&
1504 Style.BreakConstructorInitializersBeforeComma)
1505 return false;
Daniel Jasperec01cd62013-10-08 05:11:18 +00001506 if (Right.Type == TT_CtorInitializerComma &&
1507 Style.BreakConstructorInitializersBeforeComma)
1508 return true;
Daniel Jasper446d1cd2013-11-23 14:45:49 +00001509 if (Right.Type == TT_BinaryOperator && Style.BreakBeforeBinaryOperators)
Daniel Jaspere33d4af2013-07-26 16:56:36 +00001510 return true;
Daniel Jasper0de8efa2013-09-17 08:15:46 +00001511 if (Left.is(tok::greater) && Right.is(tok::greater) &&
1512 Left.Type != TT_TemplateCloser)
1513 return false;
Daniel Jasper1db6c382013-10-22 15:30:28 +00001514 if (Left.Type == TT_ArrayInitializerLSquare)
1515 return true;
Daniel Jaspere33d4af2013-07-26 16:56:36 +00001516 return (Left.isBinaryOperator() && Left.isNot(tok::lessless) &&
1517 !Style.BreakBeforeBinaryOperators) ||
Daniel Jasper83193602013-04-05 17:22:09 +00001518 Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
1519 tok::kw_class, tok::kw_struct) ||
Daniel Jasper1db6c382013-10-22 15:30:28 +00001520 Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon,
1521 tok::l_square, tok::at) ||
Daniel Jasper1bc1b502013-07-05 07:58:34 +00001522 (Left.is(tok::r_paren) &&
Daniel Jasper5be31f72013-05-21 09:16:31 +00001523 Right.isOneOf(tok::identifier, tok::kw_const, tok::kw___attribute)) ||
Daniel Jasper1db6c382013-10-22 15:30:28 +00001524 (Left.is(tok::l_paren) && !Right.is(tok::r_paren));
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001525}
1526
Daniel Jasper6bee6822013-04-08 20:33:42 +00001527void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
1528 llvm::errs() << "AnnotatedTokens:\n";
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001529 const FormatToken *Tok = Line.First;
Daniel Jasper6bee6822013-04-08 20:33:42 +00001530 while (Tok) {
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001531 llvm::errs() << " M=" << Tok->MustBreakBefore
Daniel Jasperb10cbc42013-07-10 14:02:49 +00001532 << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
1533 << " S=" << Tok->SpacesRequiredBefore
1534 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
Manuel Klimek71814b42013-10-11 21:25:45 +00001535 << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind
1536 << " FakeLParens=";
Daniel Jasper6bee6822013-04-08 20:33:42 +00001537 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
1538 llvm::errs() << Tok->FakeLParens[i] << "/";
1539 llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
Manuel Klimek71814b42013-10-11 21:25:45 +00001540 if (Tok->Next == NULL)
1541 assert(Tok == Line.Last);
Manuel Klimek6e6310e2013-05-29 14:47:47 +00001542 Tok = Tok->Next;
Daniel Jasper6bee6822013-04-08 20:33:42 +00001543 }
1544 llvm::errs() << "----\n";
1545}
1546
Daniel Jasper7a6d09b2013-01-29 21:01:14 +00001547} // namespace format
1548} // namespace clang