1 //===--- TokenAnnotator.cpp - Format C++ code -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements a token annotator, i.e. creates
11 /// \c AnnotatedTokens out of \c FormatTokens with required extra information.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "TokenAnnotator.h"
16 #include "FormatToken.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Basic/TokenKinds.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Support/Debug.h"
21
22 #define DEBUG_TYPE "format-token-annotator"
23
24 namespace clang {
25 namespace format {
26
27 namespace {
28
29 /// Returns \c true if the token can be used as an identifier in
30 /// an Objective-C \c \@selector, \c false otherwise.
31 ///
32 /// Because getFormattingLangOpts() always lexes source code as
33 /// Objective-C++, C++ keywords like \c new and \c delete are
34 /// lexed as tok::kw_*, not tok::identifier, even for Objective-C.
35 ///
36 /// For Objective-C and Objective-C++, both identifiers and keywords
37 /// are valid inside @selector(...) (or a macro which
38 /// invokes @selector(...)). So, we allow treat any identifier or
39 /// keyword as a potential Objective-C selector component.
canBeObjCSelectorComponent(const FormatToken & Tok)40 static bool canBeObjCSelectorComponent(const FormatToken &Tok) {
41 return Tok.Tok.getIdentifierInfo() != nullptr;
42 }
43
44 /// With `Left` being '(', check if we're at either `[...](` or
45 /// `[...]<...>(`, where the [ opens a lambda capture list.
isLambdaParameterList(const FormatToken * Left)46 static bool isLambdaParameterList(const FormatToken *Left) {
47 // Skip <...> if present.
48 if (Left->Previous && Left->Previous->is(tok::greater) &&
49 Left->Previous->MatchingParen &&
50 Left->Previous->MatchingParen->is(TT_TemplateOpener))
51 Left = Left->Previous->MatchingParen;
52
53 // Check for `[...]`.
54 return Left->Previous && Left->Previous->is(tok::r_square) &&
55 Left->Previous->MatchingParen &&
56 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
57 }
58
59 /// Returns \c true if the token is followed by a boolean condition, \c false
60 /// otherwise.
isKeywordWithCondition(const FormatToken & Tok)61 static bool isKeywordWithCondition(const FormatToken &Tok) {
62 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
63 tok::kw_constexpr, tok::kw_catch);
64 }
65
66 /// A parser that gathers additional information about tokens.
67 ///
68 /// The \c TokenAnnotator tries to match parenthesis and square brakets and
69 /// store a parenthesis levels. It also tries to resolve matching "<" and ">"
70 /// into template parameter lists.
71 class AnnotatingParser {
72 public:
AnnotatingParser(const FormatStyle & Style,AnnotatedLine & Line,const AdditionalKeywords & Keywords)73 AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
74 const AdditionalKeywords &Keywords)
75 : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
76 Keywords(Keywords) {
77 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
78 resetTokenMetadata(CurrentToken);
79 }
80
81 private:
parseAngle()82 bool parseAngle() {
83 if (!CurrentToken || !CurrentToken->Previous)
84 return false;
85 if (NonTemplateLess.count(CurrentToken->Previous))
86 return false;
87
88 const FormatToken &Previous = *CurrentToken->Previous; // The '<'.
89 if (Previous.Previous) {
90 if (Previous.Previous->Tok.isLiteral())
91 return false;
92 if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
93 (!Previous.Previous->MatchingParen ||
94 !Previous.Previous->MatchingParen->is(TT_OverloadedOperatorLParen)))
95 return false;
96 }
97
98 FormatToken *Left = CurrentToken->Previous;
99 Left->ParentBracket = Contexts.back().ContextKind;
100 ScopedContextCreator ContextCreator(*this, tok::less, 12);
101
102 // If this angle is in the context of an expression, we need to be more
103 // hesitant to detect it as opening template parameters.
104 bool InExprContext = Contexts.back().IsExpression;
105
106 Contexts.back().IsExpression = false;
107 // If there's a template keyword before the opening angle bracket, this is a
108 // template parameter, not an argument.
109 Contexts.back().InTemplateArgument =
110 Left->Previous && Left->Previous->Tok.isNot(tok::kw_template);
111
112 if (Style.Language == FormatStyle::LK_Java &&
113 CurrentToken->is(tok::question))
114 next();
115
116 while (CurrentToken) {
117 if (CurrentToken->is(tok::greater)) {
118 // Try to do a better job at looking for ">>" within the condition of
119 // a statement.
120 if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) &&
121 Left->ParentBracket != tok::less &&
122 isKeywordWithCondition(*Line.First))
123 return false;
124 Left->MatchingParen = CurrentToken;
125 CurrentToken->MatchingParen = Left;
126 // In TT_Proto, we must distignuish between:
127 // map<key, value>
128 // msg < item: data >
129 // msg: < item: data >
130 // In TT_TextProto, map<key, value> does not occur.
131 if (Style.Language == FormatStyle::LK_TextProto ||
132 (Style.Language == FormatStyle::LK_Proto && Left->Previous &&
133 Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral)))
134 CurrentToken->setType(TT_DictLiteral);
135 else
136 CurrentToken->setType(TT_TemplateCloser);
137 next();
138 return true;
139 }
140 if (CurrentToken->is(tok::question) &&
141 Style.Language == FormatStyle::LK_Java) {
142 next();
143 continue;
144 }
145 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
146 (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
147 !Style.isCSharp() && Style.Language != FormatStyle::LK_Proto &&
148 Style.Language != FormatStyle::LK_TextProto))
149 return false;
150 // If a && or || is found and interpreted as a binary operator, this set
151 // of angles is likely part of something like "a < b && c > d". If the
152 // angles are inside an expression, the ||/&& might also be a binary
153 // operator that was misinterpreted because we are parsing template
154 // parameters.
155 // FIXME: This is getting out of hand, write a decent parser.
156 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
157 CurrentToken->Previous->is(TT_BinaryOperator) &&
158 Contexts[Contexts.size() - 2].IsExpression &&
159 !Line.startsWith(tok::kw_template))
160 return false;
161 updateParameterCount(Left, CurrentToken);
162 if (Style.Language == FormatStyle::LK_Proto) {
163 if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) {
164 if (CurrentToken->is(tok::colon) ||
165 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
166 Previous->isNot(tok::colon)))
167 Previous->setType(TT_SelectorName);
168 }
169 }
170 if (!consumeToken())
171 return false;
172 }
173 return false;
174 }
175
parseUntouchableParens()176 bool parseUntouchableParens() {
177 while (CurrentToken) {
178 CurrentToken->Finalized = true;
179 switch (CurrentToken->Tok.getKind()) {
180 case tok::l_paren:
181 next();
182 if (!parseUntouchableParens())
183 return false;
184 continue;
185 case tok::r_paren:
186 next();
187 return true;
188 default:
189 // no-op
190 break;
191 }
192 next();
193 }
194 return false;
195 }
196
parseParens(bool LookForDecls=false)197 bool parseParens(bool LookForDecls = false) {
198 if (!CurrentToken)
199 return false;
200 FormatToken *Left = CurrentToken->Previous;
201 assert(Left && "Unknown previous token");
202 FormatToken *PrevNonComment = Left->getPreviousNonComment();
203 Left->ParentBracket = Contexts.back().ContextKind;
204 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
205
206 // FIXME: This is a bit of a hack. Do better.
207 Contexts.back().ColonIsForRangeExpr =
208 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
209
210 if (Left->Previous && Left->Previous->is(TT_UntouchableMacroFunc)) {
211 Left->Finalized = true;
212 return parseUntouchableParens();
213 }
214
215 bool StartsObjCMethodExpr = false;
216 if (FormatToken *MaybeSel = Left->Previous) {
217 // @selector( starts a selector.
218 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
219 MaybeSel->Previous->is(tok::at)) {
220 StartsObjCMethodExpr = true;
221 }
222 }
223
224 if (Left->is(TT_OverloadedOperatorLParen)) {
225 Contexts.back().IsExpression = false;
226 } else if (Style.Language == FormatStyle::LK_JavaScript &&
227 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
228 Line.startsWith(tok::kw_export, Keywords.kw_type,
229 tok::identifier))) {
230 // type X = (...);
231 // export type X = (...);
232 Contexts.back().IsExpression = false;
233 } else if (Left->Previous &&
234 (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_while,
235 tok::l_paren, tok::comma) ||
236 Left->Previous->isIf() ||
237 Left->Previous->is(TT_BinaryOperator))) {
238 // static_assert, if and while usually contain expressions.
239 Contexts.back().IsExpression = true;
240 } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous &&
241 (Left->Previous->is(Keywords.kw_function) ||
242 (Left->Previous->endsSequence(tok::identifier,
243 Keywords.kw_function)))) {
244 // function(...) or function f(...)
245 Contexts.back().IsExpression = false;
246 } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous &&
247 Left->Previous->is(TT_JsTypeColon)) {
248 // let x: (SomeType);
249 Contexts.back().IsExpression = false;
250 } else if (isLambdaParameterList(Left)) {
251 // This is a parameter list of a lambda expression.
252 Contexts.back().IsExpression = false;
253 } else if (Line.InPPDirective &&
254 (!Left->Previous || !Left->Previous->is(tok::identifier))) {
255 Contexts.back().IsExpression = true;
256 } else if (Contexts[Contexts.size() - 2].CaretFound) {
257 // This is the parameter list of an ObjC block.
258 Contexts.back().IsExpression = false;
259 } else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) {
260 // The first argument to a foreach macro is a declaration.
261 Contexts.back().IsForEachMacro = true;
262 Contexts.back().IsExpression = false;
263 } else if (Left->Previous && Left->Previous->MatchingParen &&
264 Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
265 Contexts.back().IsExpression = false;
266 } else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
267 bool IsForOrCatch =
268 Left->Previous && Left->Previous->isOneOf(tok::kw_for, tok::kw_catch);
269 Contexts.back().IsExpression = !IsForOrCatch;
270 }
271
272 // Infer the role of the l_paren based on the previous token if we haven't
273 // detected one one yet.
274 if (PrevNonComment && Left->is(TT_Unknown)) {
275 if (PrevNonComment->is(tok::kw___attribute)) {
276 Left->setType(TT_AttributeParen);
277 } else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
278 tok::kw_typeof, tok::kw__Atomic,
279 tok::kw___underlying_type)) {
280 Left->setType(TT_TypeDeclarationParen);
281 // decltype() and typeof() usually contain expressions.
282 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
283 Contexts.back().IsExpression = true;
284 }
285 }
286
287 if (StartsObjCMethodExpr) {
288 Contexts.back().ColonIsObjCMethodExpr = true;
289 Left->setType(TT_ObjCMethodExpr);
290 }
291
292 // MightBeFunctionType and ProbablyFunctionType are used for
293 // function pointer and reference types as well as Objective-C
294 // block types:
295 //
296 // void (*FunctionPointer)(void);
297 // void (&FunctionReference)(void);
298 // void (^ObjCBlock)(void);
299 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
300 bool ProbablyFunctionType =
301 CurrentToken->isOneOf(tok::star, tok::amp, tok::caret);
302 bool HasMultipleLines = false;
303 bool HasMultipleParametersOnALine = false;
304 bool MightBeObjCForRangeLoop =
305 Left->Previous && Left->Previous->is(tok::kw_for);
306 FormatToken *PossibleObjCForInToken = nullptr;
307 while (CurrentToken) {
308 // LookForDecls is set when "if (" has been seen. Check for
309 // 'identifier' '*' 'identifier' followed by not '=' -- this
310 // '*' has to be a binary operator but determineStarAmpUsage() will
311 // categorize it as an unary operator, so set the right type here.
312 if (LookForDecls && CurrentToken->Next) {
313 FormatToken *Prev = CurrentToken->getPreviousNonComment();
314 if (Prev) {
315 FormatToken *PrevPrev = Prev->getPreviousNonComment();
316 FormatToken *Next = CurrentToken->Next;
317 if (PrevPrev && PrevPrev->is(tok::identifier) &&
318 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
319 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
320 Prev->setType(TT_BinaryOperator);
321 LookForDecls = false;
322 }
323 }
324 }
325
326 if (CurrentToken->Previous->is(TT_PointerOrReference) &&
327 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
328 tok::coloncolon))
329 ProbablyFunctionType = true;
330 if (CurrentToken->is(tok::comma))
331 MightBeFunctionType = false;
332 if (CurrentToken->Previous->is(TT_BinaryOperator))
333 Contexts.back().IsExpression = true;
334 if (CurrentToken->is(tok::r_paren)) {
335 if (MightBeFunctionType && ProbablyFunctionType && CurrentToken->Next &&
336 (CurrentToken->Next->is(tok::l_paren) ||
337 (CurrentToken->Next->is(tok::l_square) && Line.MustBeDeclaration)))
338 Left->setType(Left->Next->is(tok::caret) ? TT_ObjCBlockLParen
339 : TT_FunctionTypeLParen);
340 Left->MatchingParen = CurrentToken;
341 CurrentToken->MatchingParen = Left;
342
343 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
344 Left->Previous && Left->Previous->is(tok::l_paren)) {
345 // Detect the case where macros are used to generate lambdas or
346 // function bodies, e.g.:
347 // auto my_lambda = MARCO((Type *type, int i) { .. body .. });
348 for (FormatToken *Tok = Left; Tok != CurrentToken; Tok = Tok->Next) {
349 if (Tok->is(TT_BinaryOperator) &&
350 Tok->isOneOf(tok::star, tok::amp, tok::ampamp))
351 Tok->setType(TT_PointerOrReference);
352 }
353 }
354
355 if (StartsObjCMethodExpr) {
356 CurrentToken->setType(TT_ObjCMethodExpr);
357 if (Contexts.back().FirstObjCSelectorName) {
358 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
359 Contexts.back().LongestObjCSelectorName;
360 }
361 }
362
363 if (Left->is(TT_AttributeParen))
364 CurrentToken->setType(TT_AttributeParen);
365 if (Left->is(TT_TypeDeclarationParen))
366 CurrentToken->setType(TT_TypeDeclarationParen);
367 if (Left->Previous && Left->Previous->is(TT_JavaAnnotation))
368 CurrentToken->setType(TT_JavaAnnotation);
369 if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation))
370 CurrentToken->setType(TT_LeadingJavaAnnotation);
371 if (Left->Previous && Left->Previous->is(TT_AttributeSquare))
372 CurrentToken->setType(TT_AttributeSquare);
373
374 if (!HasMultipleLines)
375 Left->setPackingKind(PPK_Inconclusive);
376 else if (HasMultipleParametersOnALine)
377 Left->setPackingKind(PPK_BinPacked);
378 else
379 Left->setPackingKind(PPK_OnePerLine);
380
381 next();
382 return true;
383 }
384 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
385 return false;
386
387 if (CurrentToken->is(tok::l_brace))
388 Left->setType(TT_Unknown); // Not TT_ObjCBlockLParen
389 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
390 !CurrentToken->Next->HasUnescapedNewline &&
391 !CurrentToken->Next->isTrailingComment())
392 HasMultipleParametersOnALine = true;
393 if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
394 CurrentToken->Previous->isSimpleTypeSpecifier()) &&
395 !CurrentToken->is(tok::l_brace))
396 Contexts.back().IsExpression = false;
397 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
398 MightBeObjCForRangeLoop = false;
399 if (PossibleObjCForInToken) {
400 PossibleObjCForInToken->setType(TT_Unknown);
401 PossibleObjCForInToken = nullptr;
402 }
403 }
404 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
405 PossibleObjCForInToken = CurrentToken;
406 PossibleObjCForInToken->setType(TT_ObjCForIn);
407 }
408 // When we discover a 'new', we set CanBeExpression to 'false' in order to
409 // parse the type correctly. Reset that after a comma.
410 if (CurrentToken->is(tok::comma))
411 Contexts.back().CanBeExpression = true;
412
413 FormatToken *Tok = CurrentToken;
414 if (!consumeToken())
415 return false;
416 updateParameterCount(Left, Tok);
417 if (CurrentToken && CurrentToken->HasUnescapedNewline)
418 HasMultipleLines = true;
419 }
420 return false;
421 }
422
isCSharpAttributeSpecifier(const FormatToken & Tok)423 bool isCSharpAttributeSpecifier(const FormatToken &Tok) {
424 if (!Style.isCSharp())
425 return false;
426
427 // `identifier[i]` is not an attribute.
428 if (Tok.Previous && Tok.Previous->is(tok::identifier))
429 return false;
430
431 // Chains of [] in `identifier[i][j][k]` are not attributes.
432 if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
433 auto *MatchingParen = Tok.Previous->MatchingParen;
434 if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
435 return false;
436 }
437
438 const FormatToken *AttrTok = Tok.Next;
439 if (!AttrTok)
440 return false;
441
442 // Just an empty declaration e.g. string [].
443 if (AttrTok->is(tok::r_square))
444 return false;
445
446 // Move along the tokens inbetween the '[' and ']' e.g. [STAThread].
447 while (AttrTok && AttrTok->isNot(tok::r_square)) {
448 AttrTok = AttrTok->Next;
449 }
450
451 if (!AttrTok)
452 return false;
453
454 // Allow an attribute to be the only content of a file.
455 AttrTok = AttrTok->Next;
456 if (!AttrTok)
457 return true;
458
459 // Limit this to being an access modifier that follows.
460 if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
461 tok::comment, tok::kw_class, tok::kw_static,
462 tok::l_square, Keywords.kw_internal)) {
463 return true;
464 }
465
466 // incase its a [XXX] retval func(....
467 if (AttrTok->Next &&
468 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren))
469 return true;
470
471 return false;
472 }
473
isCpp11AttributeSpecifier(const FormatToken & Tok)474 bool isCpp11AttributeSpecifier(const FormatToken &Tok) {
475 if (!Style.isCpp() || !Tok.startsSequence(tok::l_square, tok::l_square))
476 return false;
477 // The first square bracket is part of an ObjC array literal
478 if (Tok.Previous && Tok.Previous->is(tok::at)) {
479 return false;
480 }
481 const FormatToken *AttrTok = Tok.Next->Next;
482 if (!AttrTok)
483 return false;
484 // C++17 '[[using ns: foo, bar(baz, blech)]]'
485 // We assume nobody will name an ObjC variable 'using'.
486 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
487 return true;
488 if (AttrTok->isNot(tok::identifier))
489 return false;
490 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
491 // ObjC message send. We assume nobody will use : in a C++11 attribute
492 // specifier parameter, although this is technically valid:
493 // [[foo(:)]].
494 if (AttrTok->is(tok::colon) ||
495 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
496 AttrTok->startsSequence(tok::r_paren, tok::identifier))
497 return false;
498 if (AttrTok->is(tok::ellipsis))
499 return true;
500 AttrTok = AttrTok->Next;
501 }
502 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
503 }
504
parseSquare()505 bool parseSquare() {
506 if (!CurrentToken)
507 return false;
508
509 // A '[' could be an index subscript (after an identifier or after
510 // ')' or ']'), it could be the start of an Objective-C method
511 // expression, it could the start of an Objective-C array literal,
512 // or it could be a C++ attribute specifier [[foo::bar]].
513 FormatToken *Left = CurrentToken->Previous;
514 Left->ParentBracket = Contexts.back().ContextKind;
515 FormatToken *Parent = Left->getPreviousNonComment();
516
517 // Cases where '>' is followed by '['.
518 // In C++, this can happen either in array of templates (foo<int>[10])
519 // or when array is a nested template type (unique_ptr<type1<type2>[]>).
520 bool CppArrayTemplates =
521 Style.isCpp() && Parent && Parent->is(TT_TemplateCloser) &&
522 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
523 Contexts.back().InTemplateArgument);
524
525 bool IsCpp11AttributeSpecifier = isCpp11AttributeSpecifier(*Left) ||
526 Contexts.back().InCpp11AttributeSpecifier;
527
528 // Treat C# Attributes [STAThread] much like C++ attributes [[...]].
529 bool IsCSharpAttributeSpecifier =
530 isCSharpAttributeSpecifier(*Left) ||
531 Contexts.back().InCSharpAttributeSpecifier;
532
533 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
534 bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style);
535 bool StartsObjCMethodExpr =
536 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
537 Style.isCpp() && !IsCpp11AttributeSpecifier &&
538 !IsCSharpAttributeSpecifier && Contexts.back().CanBeExpression &&
539 Left->isNot(TT_LambdaLSquare) &&
540 !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
541 (!Parent ||
542 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
543 tok::kw_return, tok::kw_throw) ||
544 Parent->isUnaryOperator() ||
545 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
546 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
547 (getBinOpPrecedence(Parent->Tok.getKind(), true, true) >
548 prec::Unknown));
549 bool ColonFound = false;
550
551 unsigned BindingIncrease = 1;
552 if (IsCppStructuredBinding) {
553 Left->setType(TT_StructuredBindingLSquare);
554 } else if (Left->is(TT_Unknown)) {
555 if (StartsObjCMethodExpr) {
556 Left->setType(TT_ObjCMethodExpr);
557 } else if (InsideInlineASM) {
558 Left->setType(TT_InlineASMSymbolicNameLSquare);
559 } else if (IsCpp11AttributeSpecifier) {
560 Left->setType(TT_AttributeSquare);
561 } else if (Style.Language == FormatStyle::LK_JavaScript && Parent &&
562 Contexts.back().ContextKind == tok::l_brace &&
563 Parent->isOneOf(tok::l_brace, tok::comma)) {
564 Left->setType(TT_JsComputedPropertyName);
565 } else if (Style.isCpp() && Contexts.back().ContextKind == tok::l_brace &&
566 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
567 Left->setType(TT_DesignatedInitializerLSquare);
568 } else if (IsCSharpAttributeSpecifier) {
569 Left->setType(TT_AttributeSquare);
570 } else if (CurrentToken->is(tok::r_square) && Parent &&
571 Parent->is(TT_TemplateCloser)) {
572 Left->setType(TT_ArraySubscriptLSquare);
573 } else if (Style.Language == FormatStyle::LK_Proto ||
574 Style.Language == FormatStyle::LK_TextProto) {
575 // Square braces in LK_Proto can either be message field attributes:
576 //
577 // optional Aaa aaa = 1 [
578 // (aaa) = aaa
579 // ];
580 //
581 // extensions 123 [
582 // (aaa) = aaa
583 // ];
584 //
585 // or text proto extensions (in options):
586 //
587 // option (Aaa.options) = {
588 // [type.type/type] {
589 // key: value
590 // }
591 // }
592 //
593 // or repeated fields (in options):
594 //
595 // option (Aaa.options) = {
596 // keys: [ 1, 2, 3 ]
597 // }
598 //
599 // In the first and the third case we want to spread the contents inside
600 // the square braces; in the second we want to keep them inline.
601 Left->setType(TT_ArrayInitializerLSquare);
602 if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
603 tok::equal) &&
604 !Left->endsSequence(tok::l_square, tok::numeric_constant,
605 tok::identifier) &&
606 !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
607 Left->setType(TT_ProtoExtensionLSquare);
608 BindingIncrease = 10;
609 }
610 } else if (!CppArrayTemplates && Parent &&
611 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
612 tok::comma, tok::l_paren, tok::l_square,
613 tok::question, tok::colon, tok::kw_return,
614 // Should only be relevant to JavaScript:
615 tok::kw_default)) {
616 Left->setType(TT_ArrayInitializerLSquare);
617 } else {
618 BindingIncrease = 10;
619 Left->setType(TT_ArraySubscriptLSquare);
620 }
621 }
622
623 ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease);
624 Contexts.back().IsExpression = true;
625 if (Style.Language == FormatStyle::LK_JavaScript && Parent &&
626 Parent->is(TT_JsTypeColon))
627 Contexts.back().IsExpression = false;
628
629 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
630 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
631 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
632
633 while (CurrentToken) {
634 if (CurrentToken->is(tok::r_square)) {
635 if (IsCpp11AttributeSpecifier)
636 CurrentToken->setType(TT_AttributeSquare);
637 if (IsCSharpAttributeSpecifier)
638 CurrentToken->setType(TT_AttributeSquare);
639 else if (((CurrentToken->Next &&
640 CurrentToken->Next->is(tok::l_paren)) ||
641 (CurrentToken->Previous &&
642 CurrentToken->Previous->Previous == Left)) &&
643 Left->is(TT_ObjCMethodExpr)) {
644 // An ObjC method call is rarely followed by an open parenthesis. It
645 // also can't be composed of just one token, unless it's a macro that
646 // will be expanded to more tokens.
647 // FIXME: Do we incorrectly label ":" with this?
648 StartsObjCMethodExpr = false;
649 Left->setType(TT_Unknown);
650 }
651 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
652 CurrentToken->setType(TT_ObjCMethodExpr);
653 // If we haven't seen a colon yet, make sure the last identifier
654 // before the r_square is tagged as a selector name component.
655 if (!ColonFound && CurrentToken->Previous &&
656 CurrentToken->Previous->is(TT_Unknown) &&
657 canBeObjCSelectorComponent(*CurrentToken->Previous))
658 CurrentToken->Previous->setType(TT_SelectorName);
659 // determineStarAmpUsage() thinks that '*' '[' is allocating an
660 // array of pointers, but if '[' starts a selector then '*' is a
661 // binary operator.
662 if (Parent && Parent->is(TT_PointerOrReference))
663 Parent->setType(TT_BinaryOperator);
664 }
665 // An arrow after an ObjC method expression is not a lambda arrow.
666 if (CurrentToken->getType() == TT_ObjCMethodExpr &&
667 CurrentToken->Next && CurrentToken->Next->is(TT_LambdaArrow))
668 CurrentToken->Next->setType(TT_Unknown);
669 Left->MatchingParen = CurrentToken;
670 CurrentToken->MatchingParen = Left;
671 // FirstObjCSelectorName is set when a colon is found. This does
672 // not work, however, when the method has no parameters.
673 // Here, we set FirstObjCSelectorName when the end of the method call is
674 // reached, in case it was not set already.
675 if (!Contexts.back().FirstObjCSelectorName) {
676 FormatToken *Previous = CurrentToken->getPreviousNonComment();
677 if (Previous && Previous->is(TT_SelectorName)) {
678 Previous->ObjCSelectorNameParts = 1;
679 Contexts.back().FirstObjCSelectorName = Previous;
680 }
681 } else {
682 Left->ParameterCount =
683 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
684 }
685 if (Contexts.back().FirstObjCSelectorName) {
686 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
687 Contexts.back().LongestObjCSelectorName;
688 if (Left->BlockParameterCount > 1)
689 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
690 }
691 next();
692 return true;
693 }
694 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
695 return false;
696 if (CurrentToken->is(tok::colon)) {
697 if (IsCpp11AttributeSpecifier &&
698 CurrentToken->endsSequence(tok::colon, tok::identifier,
699 tok::kw_using)) {
700 // Remember that this is a [[using ns: foo]] C++ attribute, so we
701 // don't add a space before the colon (unlike other colons).
702 CurrentToken->setType(TT_AttributeColon);
703 } else if (Left->isOneOf(TT_ArraySubscriptLSquare,
704 TT_DesignatedInitializerLSquare)) {
705 Left->setType(TT_ObjCMethodExpr);
706 StartsObjCMethodExpr = true;
707 Contexts.back().ColonIsObjCMethodExpr = true;
708 if (Parent && Parent->is(tok::r_paren))
709 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
710 Parent->setType(TT_CastRParen);
711 }
712 ColonFound = true;
713 }
714 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
715 !ColonFound)
716 Left->setType(TT_ArrayInitializerLSquare);
717 FormatToken *Tok = CurrentToken;
718 if (!consumeToken())
719 return false;
720 updateParameterCount(Left, Tok);
721 }
722 return false;
723 }
724
parseBrace()725 bool parseBrace() {
726 if (CurrentToken) {
727 FormatToken *Left = CurrentToken->Previous;
728 Left->ParentBracket = Contexts.back().ContextKind;
729
730 if (Contexts.back().CaretFound)
731 Left->setType(TT_ObjCBlockLBrace);
732 Contexts.back().CaretFound = false;
733
734 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
735 Contexts.back().ColonIsDictLiteral = true;
736 if (Left->is(BK_BracedInit))
737 Contexts.back().IsExpression = true;
738 if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous &&
739 Left->Previous->is(TT_JsTypeColon))
740 Contexts.back().IsExpression = false;
741
742 while (CurrentToken) {
743 if (CurrentToken->is(tok::r_brace)) {
744 Left->MatchingParen = CurrentToken;
745 CurrentToken->MatchingParen = Left;
746 next();
747 return true;
748 }
749 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
750 return false;
751 updateParameterCount(Left, CurrentToken);
752 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
753 FormatToken *Previous = CurrentToken->getPreviousNonComment();
754 if (Previous->is(TT_JsTypeOptionalQuestion))
755 Previous = Previous->getPreviousNonComment();
756 if ((CurrentToken->is(tok::colon) &&
757 (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
758 Style.Language == FormatStyle::LK_Proto ||
759 Style.Language == FormatStyle::LK_TextProto) {
760 Left->setType(TT_DictLiteral);
761 if (Previous->Tok.getIdentifierInfo() ||
762 Previous->is(tok::string_literal))
763 Previous->setType(TT_SelectorName);
764 }
765 if (CurrentToken->is(tok::colon) ||
766 Style.Language == FormatStyle::LK_JavaScript)
767 Left->setType(TT_DictLiteral);
768 }
769 if (CurrentToken->is(tok::comma) &&
770 Style.Language == FormatStyle::LK_JavaScript)
771 Left->setType(TT_DictLiteral);
772 if (!consumeToken())
773 return false;
774 }
775 }
776 return true;
777 }
778
updateParameterCount(FormatToken * Left,FormatToken * Current)779 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
780 // For ObjC methods, the number of parameters is calculated differently as
781 // method declarations have a different structure (the parameters are not
782 // inside a bracket scope).
783 if (Current->is(tok::l_brace) && Current->is(BK_Block))
784 ++Left->BlockParameterCount;
785 if (Current->is(tok::comma)) {
786 ++Left->ParameterCount;
787 if (!Left->Role)
788 Left->Role.reset(new CommaSeparatedList(Style));
789 Left->Role->CommaFound(Current);
790 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
791 Left->ParameterCount = 1;
792 }
793 }
794
parseConditional()795 bool parseConditional() {
796 while (CurrentToken) {
797 if (CurrentToken->is(tok::colon)) {
798 CurrentToken->setType(TT_ConditionalExpr);
799 next();
800 return true;
801 }
802 if (!consumeToken())
803 return false;
804 }
805 return false;
806 }
807
parseTemplateDeclaration()808 bool parseTemplateDeclaration() {
809 if (CurrentToken && CurrentToken->is(tok::less)) {
810 CurrentToken->setType(TT_TemplateOpener);
811 next();
812 if (!parseAngle())
813 return false;
814 if (CurrentToken)
815 CurrentToken->Previous->ClosesTemplateDeclaration = true;
816 return true;
817 }
818 return false;
819 }
820
consumeToken()821 bool consumeToken() {
822 FormatToken *Tok = CurrentToken;
823 next();
824 switch (Tok->Tok.getKind()) {
825 case tok::plus:
826 case tok::minus:
827 if (!Tok->Previous && Line.MustBeDeclaration)
828 Tok->setType(TT_ObjCMethodSpecifier);
829 break;
830 case tok::colon:
831 if (!Tok->Previous)
832 return false;
833 // Colons from ?: are handled in parseConditional().
834 if (Style.Language == FormatStyle::LK_JavaScript) {
835 if (Contexts.back().ColonIsForRangeExpr || // colon in for loop
836 (Contexts.size() == 1 && // switch/case labels
837 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
838 Contexts.back().ContextKind == tok::l_paren || // function params
839 Contexts.back().ContextKind == tok::l_square || // array type
840 (!Contexts.back().IsExpression &&
841 Contexts.back().ContextKind == tok::l_brace) || // object type
842 (Contexts.size() == 1 &&
843 Line.MustBeDeclaration)) { // method/property declaration
844 Contexts.back().IsExpression = false;
845 Tok->setType(TT_JsTypeColon);
846 break;
847 }
848 } else if (Style.isCSharp()) {
849 if (Contexts.back().InCSharpAttributeSpecifier) {
850 Tok->setType(TT_AttributeColon);
851 break;
852 }
853 if (Contexts.back().ContextKind == tok::l_paren) {
854 Tok->setType(TT_CSharpNamedArgumentColon);
855 break;
856 }
857 }
858 if (Contexts.back().ColonIsDictLiteral ||
859 Style.Language == FormatStyle::LK_Proto ||
860 Style.Language == FormatStyle::LK_TextProto) {
861 Tok->setType(TT_DictLiteral);
862 if (Style.Language == FormatStyle::LK_TextProto) {
863 if (FormatToken *Previous = Tok->getPreviousNonComment())
864 Previous->setType(TT_SelectorName);
865 }
866 } else if (Contexts.back().ColonIsObjCMethodExpr ||
867 Line.startsWith(TT_ObjCMethodSpecifier)) {
868 Tok->setType(TT_ObjCMethodExpr);
869 const FormatToken *BeforePrevious = Tok->Previous->Previous;
870 // Ensure we tag all identifiers in method declarations as
871 // TT_SelectorName.
872 bool UnknownIdentifierInMethodDeclaration =
873 Line.startsWith(TT_ObjCMethodSpecifier) &&
874 Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
875 if (!BeforePrevious ||
876 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
877 !(BeforePrevious->is(TT_CastRParen) ||
878 (BeforePrevious->is(TT_ObjCMethodExpr) &&
879 BeforePrevious->is(tok::colon))) ||
880 BeforePrevious->is(tok::r_square) ||
881 Contexts.back().LongestObjCSelectorName == 0 ||
882 UnknownIdentifierInMethodDeclaration) {
883 Tok->Previous->setType(TT_SelectorName);
884 if (!Contexts.back().FirstObjCSelectorName)
885 Contexts.back().FirstObjCSelectorName = Tok->Previous;
886 else if (Tok->Previous->ColumnWidth >
887 Contexts.back().LongestObjCSelectorName)
888 Contexts.back().LongestObjCSelectorName =
889 Tok->Previous->ColumnWidth;
890 Tok->Previous->ParameterIndex =
891 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
892 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
893 }
894 } else if (Contexts.back().ColonIsForRangeExpr) {
895 Tok->setType(TT_RangeBasedForLoopColon);
896 } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
897 Tok->setType(TT_BitFieldColon);
898 } else if (Contexts.size() == 1 &&
899 !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
900 tok::kw_default)) {
901 FormatToken *Prev = Tok->getPreviousNonComment();
902 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept))
903 Tok->setType(TT_CtorInitializerColon);
904 else if (Prev->is(tok::kw_try)) {
905 // Member initializer list within function try block.
906 FormatToken *PrevPrev = Prev->getPreviousNonComment();
907 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
908 Tok->setType(TT_CtorInitializerColon);
909 } else
910 Tok->setType(TT_InheritanceColon);
911 } else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
912 (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
913 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
914 Tok->Next->Next->is(tok::colon)))) {
915 // This handles a special macro in ObjC code where selectors including
916 // the colon are passed as macro arguments.
917 Tok->setType(TT_ObjCMethodExpr);
918 } else if (Contexts.back().ContextKind == tok::l_paren) {
919 Tok->setType(TT_InlineASMColon);
920 }
921 break;
922 case tok::pipe:
923 case tok::amp:
924 // | and & in declarations/type expressions represent union and
925 // intersection types, respectively.
926 if (Style.Language == FormatStyle::LK_JavaScript &&
927 !Contexts.back().IsExpression)
928 Tok->setType(TT_JsTypeOperator);
929 break;
930 case tok::kw_if:
931 case tok::kw_while:
932 if (Tok->is(tok::kw_if) && CurrentToken &&
933 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier))
934 next();
935 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
936 next();
937 if (!parseParens(/*LookForDecls=*/true))
938 return false;
939 }
940 break;
941 case tok::kw_for:
942 if (Style.Language == FormatStyle::LK_JavaScript) {
943 // x.for and {for: ...}
944 if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
945 (Tok->Next && Tok->Next->is(tok::colon)))
946 break;
947 // JS' for await ( ...
948 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
949 next();
950 }
951 Contexts.back().ColonIsForRangeExpr = true;
952 next();
953 if (!parseParens())
954 return false;
955 break;
956 case tok::l_paren:
957 // When faced with 'operator()()', the kw_operator handler incorrectly
958 // marks the first l_paren as a OverloadedOperatorLParen. Here, we make
959 // the first two parens OverloadedOperators and the second l_paren an
960 // OverloadedOperatorLParen.
961 if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
962 Tok->Previous->MatchingParen &&
963 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
964 Tok->Previous->setType(TT_OverloadedOperator);
965 Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
966 Tok->setType(TT_OverloadedOperatorLParen);
967 }
968
969 if (!parseParens())
970 return false;
971 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
972 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
973 !Tok->is(TT_TypeDeclarationParen) &&
974 (!Tok->Previous || !Tok->Previous->isOneOf(tok::kw___attribute,
975 TT_LeadingJavaAnnotation)))
976 Line.MightBeFunctionDecl = true;
977 break;
978 case tok::l_square:
979 if (!parseSquare())
980 return false;
981 break;
982 case tok::l_brace:
983 if (Style.Language == FormatStyle::LK_TextProto) {
984 FormatToken *Previous = Tok->getPreviousNonComment();
985 if (Previous && Previous->getType() != TT_DictLiteral)
986 Previous->setType(TT_SelectorName);
987 }
988 if (!parseBrace())
989 return false;
990 break;
991 case tok::less:
992 if (parseAngle()) {
993 Tok->setType(TT_TemplateOpener);
994 // In TT_Proto, we must distignuish between:
995 // map<key, value>
996 // msg < item: data >
997 // msg: < item: data >
998 // In TT_TextProto, map<key, value> does not occur.
999 if (Style.Language == FormatStyle::LK_TextProto ||
1000 (Style.Language == FormatStyle::LK_Proto && Tok->Previous &&
1001 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1002 Tok->setType(TT_DictLiteral);
1003 FormatToken *Previous = Tok->getPreviousNonComment();
1004 if (Previous && Previous->getType() != TT_DictLiteral)
1005 Previous->setType(TT_SelectorName);
1006 }
1007 } else {
1008 Tok->setType(TT_BinaryOperator);
1009 NonTemplateLess.insert(Tok);
1010 CurrentToken = Tok;
1011 next();
1012 }
1013 break;
1014 case tok::r_paren:
1015 case tok::r_square:
1016 return false;
1017 case tok::r_brace:
1018 // Lines can start with '}'.
1019 if (Tok->Previous)
1020 return false;
1021 break;
1022 case tok::greater:
1023 if (Style.Language != FormatStyle::LK_TextProto)
1024 Tok->setType(TT_BinaryOperator);
1025 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1026 Tok->SpacesRequiredBefore = 1;
1027 break;
1028 case tok::kw_operator:
1029 if (Style.Language == FormatStyle::LK_TextProto ||
1030 Style.Language == FormatStyle::LK_Proto)
1031 break;
1032 while (CurrentToken &&
1033 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1034 if (CurrentToken->isOneOf(tok::star, tok::amp))
1035 CurrentToken->setType(TT_PointerOrReference);
1036 consumeToken();
1037 if (CurrentToken && CurrentToken->is(tok::comma) &&
1038 CurrentToken->Previous->isNot(tok::kw_operator))
1039 break;
1040 if (CurrentToken && CurrentToken->Previous->isOneOf(
1041 TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1042 tok::star, tok::arrow, tok::amp, tok::ampamp))
1043 CurrentToken->Previous->setType(TT_OverloadedOperator);
1044 }
1045 if (CurrentToken && CurrentToken->is(tok::l_paren))
1046 CurrentToken->setType(TT_OverloadedOperatorLParen);
1047 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1048 CurrentToken->Previous->setType(TT_OverloadedOperator);
1049 break;
1050 case tok::question:
1051 if (Tok->is(TT_CSharpNullConditionalLSquare)) {
1052 if (!parseSquare())
1053 return false;
1054 break;
1055 }
1056 if (Tok->isOneOf(TT_CSharpNullConditional, TT_CSharpNullCoalescing))
1057 break;
1058 if (Style.Language == FormatStyle::LK_JavaScript && Tok->Next &&
1059 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1060 tok::r_brace)) {
1061 // Question marks before semicolons, colons, etc. indicate optional
1062 // types (fields, parameters), e.g.
1063 // function(x?: string, y?) {...}
1064 // class X { y?; }
1065 Tok->setType(TT_JsTypeOptionalQuestion);
1066 break;
1067 }
1068 // Declarations cannot be conditional expressions, this can only be part
1069 // of a type declaration.
1070 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1071 Style.Language == FormatStyle::LK_JavaScript)
1072 break;
1073 if (Style.isCSharp()) {
1074 // `Type?)`, `Type?>`, `Type? name;` and `Type? name =` can only be
1075 // nullable types.
1076 // Line.MustBeDeclaration will be true for `Type? name;`.
1077 if ((!Contexts.back().IsExpression && Line.MustBeDeclaration) ||
1078 (Tok->Next && Tok->Next->isOneOf(tok::r_paren, tok::greater)) ||
1079 (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1080 Tok->Next->Next->is(tok::equal))) {
1081 Tok->setType(TT_CSharpNullable);
1082 break;
1083 }
1084 }
1085 parseConditional();
1086 break;
1087 case tok::kw_template:
1088 parseTemplateDeclaration();
1089 break;
1090 case tok::comma:
1091 if (Contexts.back().InCtorInitializer)
1092 Tok->setType(TT_CtorInitializerComma);
1093 else if (Contexts.back().InInheritanceList)
1094 Tok->setType(TT_InheritanceComma);
1095 else if (Contexts.back().FirstStartOfName &&
1096 (Contexts.size() == 1 || Line.startsWith(tok::kw_for))) {
1097 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
1098 Line.IsMultiVariableDeclStmt = true;
1099 }
1100 if (Contexts.back().IsForEachMacro)
1101 Contexts.back().IsExpression = true;
1102 break;
1103 case tok::identifier:
1104 if (Tok->isOneOf(Keywords.kw___has_include,
1105 Keywords.kw___has_include_next)) {
1106 parseHasInclude();
1107 }
1108 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1109 Tok->Next->isNot(tok::l_paren)) {
1110 Tok->setType(TT_CSharpGenericTypeConstraint);
1111 parseCSharpGenericTypeConstraint();
1112 }
1113 break;
1114 default:
1115 break;
1116 }
1117 return true;
1118 }
1119
parseCSharpGenericTypeConstraint()1120 void parseCSharpGenericTypeConstraint() {
1121 int OpenAngleBracketsCount = 0;
1122 while (CurrentToken) {
1123 if (CurrentToken->is(tok::less)) {
1124 // parseAngle is too greedy and will consume the whole line.
1125 CurrentToken->setType(TT_TemplateOpener);
1126 ++OpenAngleBracketsCount;
1127 next();
1128 } else if (CurrentToken->is(tok::greater)) {
1129 CurrentToken->setType(TT_TemplateCloser);
1130 --OpenAngleBracketsCount;
1131 next();
1132 } else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1133 // We allow line breaks after GenericTypeConstraintComma's
1134 // so do not flag commas in Generics as GenericTypeConstraintComma's.
1135 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1136 next();
1137 } else if (CurrentToken->is(Keywords.kw_where)) {
1138 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1139 next();
1140 } else if (CurrentToken->is(tok::colon)) {
1141 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1142 next();
1143 } else {
1144 next();
1145 }
1146 }
1147 }
1148
parseIncludeDirective()1149 void parseIncludeDirective() {
1150 if (CurrentToken && CurrentToken->is(tok::less)) {
1151 next();
1152 while (CurrentToken) {
1153 // Mark tokens up to the trailing line comments as implicit string
1154 // literals.
1155 if (CurrentToken->isNot(tok::comment) &&
1156 !CurrentToken->TokenText.startswith("//"))
1157 CurrentToken->setType(TT_ImplicitStringLiteral);
1158 next();
1159 }
1160 }
1161 }
1162
parseWarningOrError()1163 void parseWarningOrError() {
1164 next();
1165 // We still want to format the whitespace left of the first token of the
1166 // warning or error.
1167 next();
1168 while (CurrentToken) {
1169 CurrentToken->setType(TT_ImplicitStringLiteral);
1170 next();
1171 }
1172 }
1173
parsePragma()1174 void parsePragma() {
1175 next(); // Consume "pragma".
1176 if (CurrentToken &&
1177 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) {
1178 bool IsMark = CurrentToken->is(Keywords.kw_mark);
1179 next(); // Consume "mark".
1180 next(); // Consume first token (so we fix leading whitespace).
1181 while (CurrentToken) {
1182 if (IsMark || CurrentToken->Previous->is(TT_BinaryOperator))
1183 CurrentToken->setType(TT_ImplicitStringLiteral);
1184 next();
1185 }
1186 }
1187 }
1188
parseHasInclude()1189 void parseHasInclude() {
1190 if (!CurrentToken || !CurrentToken->is(tok::l_paren))
1191 return;
1192 next(); // '('
1193 parseIncludeDirective();
1194 next(); // ')'
1195 }
1196
parsePreprocessorDirective()1197 LineType parsePreprocessorDirective() {
1198 bool IsFirstToken = CurrentToken->IsFirst;
1199 LineType Type = LT_PreprocessorDirective;
1200 next();
1201 if (!CurrentToken)
1202 return Type;
1203
1204 if (Style.Language == FormatStyle::LK_JavaScript && IsFirstToken) {
1205 // JavaScript files can contain shebang lines of the form:
1206 // #!/usr/bin/env node
1207 // Treat these like C++ #include directives.
1208 while (CurrentToken) {
1209 // Tokens cannot be comments here.
1210 CurrentToken->setType(TT_ImplicitStringLiteral);
1211 next();
1212 }
1213 return LT_ImportStatement;
1214 }
1215
1216 if (CurrentToken->Tok.is(tok::numeric_constant)) {
1217 CurrentToken->SpacesRequiredBefore = 1;
1218 return Type;
1219 }
1220 // Hashes in the middle of a line can lead to any strange token
1221 // sequence.
1222 if (!CurrentToken->Tok.getIdentifierInfo())
1223 return Type;
1224 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1225 case tok::pp_include:
1226 case tok::pp_include_next:
1227 case tok::pp_import:
1228 next();
1229 parseIncludeDirective();
1230 Type = LT_ImportStatement;
1231 break;
1232 case tok::pp_error:
1233 case tok::pp_warning:
1234 parseWarningOrError();
1235 break;
1236 case tok::pp_pragma:
1237 parsePragma();
1238 break;
1239 case tok::pp_if:
1240 case tok::pp_elif:
1241 Contexts.back().IsExpression = true;
1242 next();
1243 parseLine();
1244 break;
1245 default:
1246 break;
1247 }
1248 while (CurrentToken) {
1249 FormatToken *Tok = CurrentToken;
1250 next();
1251 if (Tok->is(tok::l_paren))
1252 parseParens();
1253 else if (Tok->isOneOf(Keywords.kw___has_include,
1254 Keywords.kw___has_include_next))
1255 parseHasInclude();
1256 }
1257 return Type;
1258 }
1259
1260 public:
parseLine()1261 LineType parseLine() {
1262 if (!CurrentToken)
1263 return LT_Invalid;
1264 NonTemplateLess.clear();
1265 if (CurrentToken->is(tok::hash))
1266 return parsePreprocessorDirective();
1267
1268 // Directly allow to 'import <string-literal>' to support protocol buffer
1269 // definitions (github.com/google/protobuf) or missing "#" (either way we
1270 // should not break the line).
1271 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1272 if ((Style.Language == FormatStyle::LK_Java &&
1273 CurrentToken->is(Keywords.kw_package)) ||
1274 (Info && Info->getPPKeywordID() == tok::pp_import &&
1275 CurrentToken->Next &&
1276 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1277 tok::kw_static))) {
1278 next();
1279 parseIncludeDirective();
1280 return LT_ImportStatement;
1281 }
1282
1283 // If this line starts and ends in '<' and '>', respectively, it is likely
1284 // part of "#define <a/b.h>".
1285 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1286 parseIncludeDirective();
1287 return LT_ImportStatement;
1288 }
1289
1290 // In .proto files, top-level options and package statements are very
1291 // similar to import statements and should not be line-wrapped.
1292 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
1293 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1294 next();
1295 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1296 while (CurrentToken)
1297 next();
1298 return LT_ImportStatement;
1299 }
1300 }
1301
1302 bool KeywordVirtualFound = false;
1303 bool ImportStatement = false;
1304
1305 // import {...} from '...';
1306 if (Style.Language == FormatStyle::LK_JavaScript &&
1307 CurrentToken->is(Keywords.kw_import))
1308 ImportStatement = true;
1309
1310 while (CurrentToken) {
1311 if (CurrentToken->is(tok::kw_virtual))
1312 KeywordVirtualFound = true;
1313 if (Style.Language == FormatStyle::LK_JavaScript) {
1314 // export {...} from '...';
1315 // An export followed by "from 'some string';" is a re-export from
1316 // another module identified by a URI and is treated as a
1317 // LT_ImportStatement (i.e. prevent wraps on it for long URIs).
1318 // Just "export {...};" or "export class ..." should not be treated as
1319 // an import in this sense.
1320 if (Line.First->is(tok::kw_export) &&
1321 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
1322 CurrentToken->Next->isStringLiteral())
1323 ImportStatement = true;
1324 if (isClosureImportStatement(*CurrentToken))
1325 ImportStatement = true;
1326 }
1327 if (!consumeToken())
1328 return LT_Invalid;
1329 }
1330 if (KeywordVirtualFound)
1331 return LT_VirtualFunctionDecl;
1332 if (ImportStatement)
1333 return LT_ImportStatement;
1334
1335 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
1336 if (Contexts.back().FirstObjCSelectorName)
1337 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
1338 Contexts.back().LongestObjCSelectorName;
1339 return LT_ObjCMethodDecl;
1340 }
1341
1342 return LT_Other;
1343 }
1344
1345 private:
isClosureImportStatement(const FormatToken & Tok)1346 bool isClosureImportStatement(const FormatToken &Tok) {
1347 // FIXME: Closure-library specific stuff should not be hard-coded but be
1348 // configurable.
1349 return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
1350 Tok.Next->Next &&
1351 (Tok.Next->Next->TokenText == "module" ||
1352 Tok.Next->Next->TokenText == "provide" ||
1353 Tok.Next->Next->TokenText == "require" ||
1354 Tok.Next->Next->TokenText == "requireType" ||
1355 Tok.Next->Next->TokenText == "forwardDeclare") &&
1356 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
1357 }
1358
resetTokenMetadata(FormatToken * Token)1359 void resetTokenMetadata(FormatToken *Token) {
1360 if (!Token)
1361 return;
1362
1363 // Reset token type in case we have already looked at it and then
1364 // recovered from an error (e.g. failure to find the matching >).
1365 if (!CurrentToken->isOneOf(
1366 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro,
1367 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
1368 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_JsFatArrow,
1369 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
1370 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
1371 TT_UntouchableMacroFunc, TT_ConstraintJunctions,
1372 TT_StatementAttributeLikeMacro))
1373 CurrentToken->setType(TT_Unknown);
1374 CurrentToken->Role.reset();
1375 CurrentToken->MatchingParen = nullptr;
1376 CurrentToken->FakeLParens.clear();
1377 CurrentToken->FakeRParens = 0;
1378 }
1379
next()1380 void next() {
1381 if (CurrentToken) {
1382 CurrentToken->NestingLevel = Contexts.size() - 1;
1383 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
1384 modifyContext(*CurrentToken);
1385 determineTokenType(*CurrentToken);
1386 CurrentToken = CurrentToken->Next;
1387 }
1388
1389 resetTokenMetadata(CurrentToken);
1390 }
1391
1392 /// A struct to hold information valid in a specific context, e.g.
1393 /// a pair of parenthesis.
1394 struct Context {
Contextclang::format::__anon0f16db810111::AnnotatingParser::Context1395 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
1396 bool IsExpression)
1397 : ContextKind(ContextKind), BindingStrength(BindingStrength),
1398 IsExpression(IsExpression) {}
1399
1400 tok::TokenKind ContextKind;
1401 unsigned BindingStrength;
1402 bool IsExpression;
1403 unsigned LongestObjCSelectorName = 0;
1404 bool ColonIsForRangeExpr = false;
1405 bool ColonIsDictLiteral = false;
1406 bool ColonIsObjCMethodExpr = false;
1407 FormatToken *FirstObjCSelectorName = nullptr;
1408 FormatToken *FirstStartOfName = nullptr;
1409 bool CanBeExpression = true;
1410 bool InTemplateArgument = false;
1411 bool InCtorInitializer = false;
1412 bool InInheritanceList = false;
1413 bool CaretFound = false;
1414 bool IsForEachMacro = false;
1415 bool InCpp11AttributeSpecifier = false;
1416 bool InCSharpAttributeSpecifier = false;
1417 };
1418
1419 /// Puts a new \c Context onto the stack \c Contexts for the lifetime
1420 /// of each instance.
1421 struct ScopedContextCreator {
1422 AnnotatingParser &P;
1423
ScopedContextCreatorclang::format::__anon0f16db810111::AnnotatingParser::ScopedContextCreator1424 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
1425 unsigned Increase)
1426 : P(P) {
1427 P.Contexts.push_back(Context(ContextKind,
1428 P.Contexts.back().BindingStrength + Increase,
1429 P.Contexts.back().IsExpression));
1430 }
1431
~ScopedContextCreatorclang::format::__anon0f16db810111::AnnotatingParser::ScopedContextCreator1432 ~ScopedContextCreator() { P.Contexts.pop_back(); }
1433 };
1434
modifyContext(const FormatToken & Current)1435 void modifyContext(const FormatToken &Current) {
1436 if (Current.getPrecedence() == prec::Assignment &&
1437 !Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) &&
1438 // Type aliases use `type X = ...;` in TypeScript and can be exported
1439 // using `export type ...`.
1440 !(Style.Language == FormatStyle::LK_JavaScript &&
1441 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
1442 Line.startsWith(tok::kw_export, Keywords.kw_type,
1443 tok::identifier))) &&
1444 (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
1445 Contexts.back().IsExpression = true;
1446 if (!Line.startsWith(TT_UnaryOperator)) {
1447 for (FormatToken *Previous = Current.Previous;
1448 Previous && Previous->Previous &&
1449 !Previous->Previous->isOneOf(tok::comma, tok::semi);
1450 Previous = Previous->Previous) {
1451 if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
1452 Previous = Previous->MatchingParen;
1453 if (!Previous)
1454 break;
1455 }
1456 if (Previous->opensScope())
1457 break;
1458 if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
1459 Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
1460 Previous->Previous && Previous->Previous->isNot(tok::equal))
1461 Previous->setType(TT_PointerOrReference);
1462 }
1463 }
1464 } else if (Current.is(tok::lessless) &&
1465 (!Current.Previous || !Current.Previous->is(tok::kw_operator))) {
1466 Contexts.back().IsExpression = true;
1467 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
1468 Contexts.back().IsExpression = true;
1469 } else if (Current.is(TT_TrailingReturnArrow)) {
1470 Contexts.back().IsExpression = false;
1471 } else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) {
1472 Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
1473 } else if (Current.Previous &&
1474 Current.Previous->is(TT_CtorInitializerColon)) {
1475 Contexts.back().IsExpression = true;
1476 Contexts.back().InCtorInitializer = true;
1477 } else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
1478 Contexts.back().InInheritanceList = true;
1479 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
1480 for (FormatToken *Previous = Current.Previous;
1481 Previous && Previous->isOneOf(tok::star, tok::amp);
1482 Previous = Previous->Previous)
1483 Previous->setType(TT_PointerOrReference);
1484 if (Line.MustBeDeclaration && !Contexts.front().InCtorInitializer)
1485 Contexts.back().IsExpression = false;
1486 } else if (Current.is(tok::kw_new)) {
1487 Contexts.back().CanBeExpression = false;
1488 } else if (Current.is(tok::semi) ||
1489 (Current.is(tok::exclaim) && Current.Previous &&
1490 !Current.Previous->is(tok::kw_operator))) {
1491 // This should be the condition or increment in a for-loop.
1492 // But not operator !() (can't use TT_OverloadedOperator here as its not
1493 // been annotated yet).
1494 Contexts.back().IsExpression = true;
1495 }
1496 }
1497
untilMatchingParen(FormatToken * Current)1498 static FormatToken *untilMatchingParen(FormatToken *Current) {
1499 // Used when `MatchingParen` is not yet established.
1500 int ParenLevel = 0;
1501 while (Current) {
1502 if (Current->is(tok::l_paren))
1503 ParenLevel++;
1504 if (Current->is(tok::r_paren))
1505 ParenLevel--;
1506 if (ParenLevel < 1)
1507 break;
1508 Current = Current->Next;
1509 }
1510 return Current;
1511 }
1512
isDeductionGuide(FormatToken & Current)1513 static bool isDeductionGuide(FormatToken &Current) {
1514 // Look for a deduction guide template<T> A(...) -> A<...>;
1515 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
1516 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
1517 // Find the TemplateCloser.
1518 FormatToken *TemplateCloser = Current.Next->Next;
1519 int NestingLevel = 0;
1520 while (TemplateCloser) {
1521 // Skip over an expressions in parens A<(3 < 2)>;
1522 if (TemplateCloser->is(tok::l_paren)) {
1523 // No Matching Paren yet so skip to matching paren
1524 TemplateCloser = untilMatchingParen(TemplateCloser);
1525 }
1526 if (TemplateCloser->is(tok::less))
1527 NestingLevel++;
1528 if (TemplateCloser->is(tok::greater))
1529 NestingLevel--;
1530 if (NestingLevel < 1)
1531 break;
1532 TemplateCloser = TemplateCloser->Next;
1533 }
1534 // Assuming we have found the end of the template ensure its followed
1535 // with a semi-colon.
1536 if (TemplateCloser && TemplateCloser->Next &&
1537 TemplateCloser->Next->is(tok::semi) &&
1538 Current.Previous->MatchingParen) {
1539 // Determine if the identifier `A` prior to the A<..>; is the same as
1540 // prior to the A(..)
1541 FormatToken *LeadingIdentifier =
1542 Current.Previous->MatchingParen->Previous;
1543
1544 // Differentiate a deduction guide by seeing the
1545 // > of the template prior to the leading identifier.
1546 if (LeadingIdentifier) {
1547 FormatToken *PriorLeadingIdentifier = LeadingIdentifier->Previous;
1548 // Skip back past explicit decoration
1549 if (PriorLeadingIdentifier &&
1550 PriorLeadingIdentifier->is(tok::kw_explicit))
1551 PriorLeadingIdentifier = PriorLeadingIdentifier->Previous;
1552
1553 return (PriorLeadingIdentifier &&
1554 PriorLeadingIdentifier->is(TT_TemplateCloser) &&
1555 LeadingIdentifier->TokenText == Current.Next->TokenText);
1556 }
1557 }
1558 }
1559 return false;
1560 }
1561
determineTokenType(FormatToken & Current)1562 void determineTokenType(FormatToken &Current) {
1563 if (!Current.is(TT_Unknown))
1564 // The token type is already known.
1565 return;
1566
1567 if (Style.isCSharp() && CurrentToken->is(tok::question)) {
1568 if (CurrentToken->TokenText == "??") {
1569 Current.setType(TT_CSharpNullCoalescing);
1570 return;
1571 }
1572 if (CurrentToken->TokenText == "?.") {
1573 Current.setType(TT_CSharpNullConditional);
1574 return;
1575 }
1576 if (CurrentToken->TokenText == "?[") {
1577 Current.setType(TT_CSharpNullConditionalLSquare);
1578 return;
1579 }
1580 }
1581
1582 if (Style.Language == FormatStyle::LK_JavaScript) {
1583 if (Current.is(tok::exclaim)) {
1584 if (Current.Previous &&
1585 (Keywords.IsJavaScriptIdentifier(
1586 *Current.Previous, /* AcceptIdentifierName= */ true) ||
1587 Current.Previous->isOneOf(
1588 tok::kw_namespace, tok::r_paren, tok::r_square, tok::r_brace,
1589 Keywords.kw_type, Keywords.kw_get, Keywords.kw_set) ||
1590 Current.Previous->Tok.isLiteral())) {
1591 Current.setType(TT_JsNonNullAssertion);
1592 return;
1593 }
1594 if (Current.Next &&
1595 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
1596 Current.setType(TT_JsNonNullAssertion);
1597 return;
1598 }
1599 }
1600 }
1601
1602 // Line.MightBeFunctionDecl can only be true after the parentheses of a
1603 // function declaration have been found. In this case, 'Current' is a
1604 // trailing token of this declaration and thus cannot be a name.
1605 if (Current.is(Keywords.kw_instanceof)) {
1606 Current.setType(TT_BinaryOperator);
1607 } else if (isStartOfName(Current) &&
1608 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
1609 Contexts.back().FirstStartOfName = &Current;
1610 Current.setType(TT_StartOfName);
1611 } else if (Current.is(tok::semi)) {
1612 // Reset FirstStartOfName after finding a semicolon so that a for loop
1613 // with multiple increment statements is not confused with a for loop
1614 // having multiple variable declarations.
1615 Contexts.back().FirstStartOfName = nullptr;
1616 } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
1617 AutoFound = true;
1618 } else if (Current.is(tok::arrow) &&
1619 Style.Language == FormatStyle::LK_Java) {
1620 Current.setType(TT_LambdaArrow);
1621 } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration &&
1622 Current.NestingLevel == 0 &&
1623 !Current.Previous->is(tok::kw_operator)) {
1624 // not auto operator->() -> xxx;
1625 Current.setType(TT_TrailingReturnArrow);
1626 } else if (Current.is(tok::arrow) && Current.Previous &&
1627 Current.Previous->is(tok::r_brace)) {
1628 // Concept implicit conversion contraint needs to be treated like
1629 // a trailing return type ... } -> <type>.
1630 Current.setType(TT_TrailingReturnArrow);
1631 } else if (isDeductionGuide(Current)) {
1632 // Deduction guides trailing arrow " A(...) -> A<T>;".
1633 Current.setType(TT_TrailingReturnArrow);
1634 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
1635 Current.setType(determineStarAmpUsage(
1636 Current,
1637 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
1638 Contexts.back().InTemplateArgument));
1639 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
1640 Current.setType(determinePlusMinusCaretUsage(Current));
1641 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
1642 Contexts.back().CaretFound = true;
1643 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
1644 Current.setType(determineIncrementUsage(Current));
1645 } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
1646 Current.setType(TT_UnaryOperator);
1647 } else if (Current.is(tok::question)) {
1648 if (Style.Language == FormatStyle::LK_JavaScript &&
1649 Line.MustBeDeclaration && !Contexts.back().IsExpression) {
1650 // In JavaScript, `interface X { foo?(): bar; }` is an optional method
1651 // on the interface, not a ternary expression.
1652 Current.setType(TT_JsTypeOptionalQuestion);
1653 } else {
1654 Current.setType(TT_ConditionalExpr);
1655 }
1656 } else if (Current.isBinaryOperator() &&
1657 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
1658 (!Current.is(tok::greater) &&
1659 Style.Language != FormatStyle::LK_TextProto)) {
1660 Current.setType(TT_BinaryOperator);
1661 } else if (Current.is(tok::comment)) {
1662 if (Current.TokenText.startswith("/*")) {
1663 if (Current.TokenText.endswith("*/"))
1664 Current.setType(TT_BlockComment);
1665 else
1666 // The lexer has for some reason determined a comment here. But we
1667 // cannot really handle it, if it isn't properly terminated.
1668 Current.Tok.setKind(tok::unknown);
1669 } else {
1670 Current.setType(TT_LineComment);
1671 }
1672 } else if (Current.is(tok::r_paren)) {
1673 if (rParenEndsCast(Current))
1674 Current.setType(TT_CastRParen);
1675 if (Current.MatchingParen && Current.Next &&
1676 !Current.Next->isBinaryOperator() &&
1677 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
1678 tok::comma, tok::period, tok::arrow,
1679 tok::coloncolon))
1680 if (FormatToken *AfterParen = Current.MatchingParen->Next) {
1681 // Make sure this isn't the return type of an Obj-C block declaration
1682 if (AfterParen->Tok.isNot(tok::caret)) {
1683 if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
1684 if (BeforeParen->is(tok::identifier) &&
1685 !BeforeParen->is(TT_TypenameMacro) &&
1686 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
1687 (!BeforeParen->Previous ||
1688 BeforeParen->Previous->ClosesTemplateDeclaration))
1689 Current.setType(TT_FunctionAnnotationRParen);
1690 }
1691 }
1692 } else if (Current.is(tok::at) && Current.Next &&
1693 Style.Language != FormatStyle::LK_JavaScript &&
1694 Style.Language != FormatStyle::LK_Java) {
1695 // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
1696 // marks declarations and properties that need special formatting.
1697 switch (Current.Next->Tok.getObjCKeywordID()) {
1698 case tok::objc_interface:
1699 case tok::objc_implementation:
1700 case tok::objc_protocol:
1701 Current.setType(TT_ObjCDecl);
1702 break;
1703 case tok::objc_property:
1704 Current.setType(TT_ObjCProperty);
1705 break;
1706 default:
1707 break;
1708 }
1709 } else if (Current.is(tok::period)) {
1710 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
1711 if (PreviousNoComment &&
1712 PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
1713 Current.setType(TT_DesignatedInitializerPeriod);
1714 else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
1715 Current.Previous->isOneOf(TT_JavaAnnotation,
1716 TT_LeadingJavaAnnotation)) {
1717 Current.setType(Current.Previous->getType());
1718 }
1719 } else if (canBeObjCSelectorComponent(Current) &&
1720 // FIXME(bug 36976): ObjC return types shouldn't use
1721 // TT_CastRParen.
1722 Current.Previous && Current.Previous->is(TT_CastRParen) &&
1723 Current.Previous->MatchingParen &&
1724 Current.Previous->MatchingParen->Previous &&
1725 Current.Previous->MatchingParen->Previous->is(
1726 TT_ObjCMethodSpecifier)) {
1727 // This is the first part of an Objective-C selector name. (If there's no
1728 // colon after this, this is the only place which annotates the identifier
1729 // as a selector.)
1730 Current.setType(TT_SelectorName);
1731 } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
1732 tok::kw_requires) &&
1733 Current.Previous &&
1734 !Current.Previous->isOneOf(tok::equal, tok::at) &&
1735 Line.MightBeFunctionDecl && Contexts.size() == 1) {
1736 // Line.MightBeFunctionDecl can only be true after the parentheses of a
1737 // function declaration have been found.
1738 Current.setType(TT_TrailingAnnotation);
1739 } else if ((Style.Language == FormatStyle::LK_Java ||
1740 Style.Language == FormatStyle::LK_JavaScript) &&
1741 Current.Previous) {
1742 if (Current.Previous->is(tok::at) &&
1743 Current.isNot(Keywords.kw_interface)) {
1744 const FormatToken &AtToken = *Current.Previous;
1745 const FormatToken *Previous = AtToken.getPreviousNonComment();
1746 if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
1747 Current.setType(TT_LeadingJavaAnnotation);
1748 else
1749 Current.setType(TT_JavaAnnotation);
1750 } else if (Current.Previous->is(tok::period) &&
1751 Current.Previous->isOneOf(TT_JavaAnnotation,
1752 TT_LeadingJavaAnnotation)) {
1753 Current.setType(Current.Previous->getType());
1754 }
1755 }
1756 }
1757
1758 /// Take a guess at whether \p Tok starts a name of a function or
1759 /// variable declaration.
1760 ///
1761 /// This is a heuristic based on whether \p Tok is an identifier following
1762 /// something that is likely a type.
isStartOfName(const FormatToken & Tok)1763 bool isStartOfName(const FormatToken &Tok) {
1764 if (Tok.isNot(tok::identifier) || !Tok.Previous)
1765 return false;
1766
1767 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
1768 Keywords.kw_as))
1769 return false;
1770 if (Style.Language == FormatStyle::LK_JavaScript &&
1771 Tok.Previous->is(Keywords.kw_in))
1772 return false;
1773
1774 // Skip "const" as it does not have an influence on whether this is a name.
1775 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
1776 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
1777 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
1778
1779 if (!PreviousNotConst)
1780 return false;
1781
1782 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
1783 PreviousNotConst->Previous &&
1784 PreviousNotConst->Previous->is(tok::hash);
1785
1786 if (PreviousNotConst->is(TT_TemplateCloser))
1787 return PreviousNotConst && PreviousNotConst->MatchingParen &&
1788 PreviousNotConst->MatchingParen->Previous &&
1789 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
1790 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
1791
1792 if (PreviousNotConst->is(tok::r_paren) &&
1793 PreviousNotConst->is(TT_TypeDeclarationParen))
1794 return true;
1795
1796 return (!IsPPKeyword &&
1797 PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto)) ||
1798 PreviousNotConst->is(TT_PointerOrReference) ||
1799 PreviousNotConst->isSimpleTypeSpecifier();
1800 }
1801
1802 /// Determine whether ')' is ending a cast.
rParenEndsCast(const FormatToken & Tok)1803 bool rParenEndsCast(const FormatToken &Tok) {
1804 // C-style casts are only used in C++, C# and Java.
1805 if (!Style.isCSharp() && !Style.isCpp() &&
1806 Style.Language != FormatStyle::LK_Java)
1807 return false;
1808
1809 // Empty parens aren't casts and there are no casts at the end of the line.
1810 if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
1811 return false;
1812
1813 FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
1814 if (LeftOfParens) {
1815 // If there is a closing parenthesis left of the current parentheses,
1816 // look past it as these might be chained casts.
1817 if (LeftOfParens->is(tok::r_paren)) {
1818 if (!LeftOfParens->MatchingParen ||
1819 !LeftOfParens->MatchingParen->Previous)
1820 return false;
1821 LeftOfParens = LeftOfParens->MatchingParen->Previous;
1822 }
1823
1824 // If there is an identifier (or with a few exceptions a keyword) right
1825 // before the parentheses, this is unlikely to be a cast.
1826 if (LeftOfParens->Tok.getIdentifierInfo() &&
1827 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
1828 tok::kw_delete))
1829 return false;
1830
1831 // Certain other tokens right before the parentheses are also signals that
1832 // this cannot be a cast.
1833 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
1834 TT_TemplateCloser, tok::ellipsis))
1835 return false;
1836 }
1837
1838 if (Tok.Next->is(tok::question))
1839 return false;
1840
1841 // `foreach((A a, B b) in someList)` should not be seen as a cast.
1842 if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
1843 return false;
1844
1845 // Functions which end with decorations like volatile, noexcept are unlikely
1846 // to be casts.
1847 if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
1848 tok::kw_requires, tok::kw_throw, tok::arrow,
1849 Keywords.kw_override, Keywords.kw_final) ||
1850 isCpp11AttributeSpecifier(*Tok.Next))
1851 return false;
1852
1853 // As Java has no function types, a "(" after the ")" likely means that this
1854 // is a cast.
1855 if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
1856 return true;
1857
1858 // If a (non-string) literal follows, this is likely a cast.
1859 if (Tok.Next->isNot(tok::string_literal) &&
1860 (Tok.Next->Tok.isLiteral() ||
1861 Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
1862 return true;
1863
1864 // Heuristically try to determine whether the parentheses contain a type.
1865 auto IsQualifiedPointerOrReference = [](FormatToken *T) {
1866 // This is used to handle cases such as x = (foo *const)&y;
1867 assert(!T->isSimpleTypeSpecifier() && "Should have already been checked");
1868 // Strip trailing qualifiers such as const or volatile when checking
1869 // whether the parens could be a cast to a pointer/reference type.
1870 while (T) {
1871 if (T->is(TT_AttributeParen)) {
1872 // Handle `x = (foo *__attribute__((foo)))&v;`:
1873 if (T->MatchingParen && T->MatchingParen->Previous &&
1874 T->MatchingParen->Previous->is(tok::kw___attribute)) {
1875 T = T->MatchingParen->Previous->Previous;
1876 continue;
1877 }
1878 } else if (T->is(TT_AttributeSquare)) {
1879 // Handle `x = (foo *[[clang::foo]])&v;`:
1880 if (T->MatchingParen && T->MatchingParen->Previous) {
1881 T = T->MatchingParen->Previous;
1882 continue;
1883 }
1884 } else if (T->canBePointerOrReferenceQualifier()) {
1885 T = T->Previous;
1886 continue;
1887 }
1888 break;
1889 }
1890 return T && T->is(TT_PointerOrReference);
1891 };
1892 bool ParensAreType =
1893 !Tok.Previous ||
1894 Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
1895 Tok.Previous->isSimpleTypeSpecifier() ||
1896 IsQualifiedPointerOrReference(Tok.Previous);
1897 bool ParensCouldEndDecl =
1898 Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
1899 if (ParensAreType && !ParensCouldEndDecl)
1900 return true;
1901
1902 // At this point, we heuristically assume that there are no casts at the
1903 // start of the line. We assume that we have found most cases where there
1904 // are by the logic above, e.g. "(void)x;".
1905 if (!LeftOfParens)
1906 return false;
1907
1908 // Certain token types inside the parentheses mean that this can't be a
1909 // cast.
1910 for (const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
1911 Token = Token->Next)
1912 if (Token->is(TT_BinaryOperator))
1913 return false;
1914
1915 // If the following token is an identifier or 'this', this is a cast. All
1916 // cases where this can be something else are handled above.
1917 if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
1918 return true;
1919
1920 // Look for a cast `( x ) (`.
1921 if (Tok.Next->is(tok::l_paren) && Tok.Previous && Tok.Previous->Previous) {
1922 if (Tok.Previous->is(tok::identifier) &&
1923 Tok.Previous->Previous->is(tok::l_paren))
1924 return true;
1925 }
1926
1927 if (!Tok.Next->Next)
1928 return false;
1929
1930 // If the next token after the parenthesis is a unary operator, assume
1931 // that this is cast, unless there are unexpected tokens inside the
1932 // parenthesis.
1933 bool NextIsUnary =
1934 Tok.Next->isUnaryOperator() || Tok.Next->isOneOf(tok::amp, tok::star);
1935 if (!NextIsUnary || Tok.Next->is(tok::plus) ||
1936 !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant))
1937 return false;
1938 // Search for unexpected tokens.
1939 for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
1940 Prev = Prev->Previous) {
1941 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
1942 return false;
1943 }
1944 return true;
1945 }
1946
1947 /// Return the type of the given token assuming it is * or &.
determineStarAmpUsage(const FormatToken & Tok,bool IsExpression,bool InTemplateArgument)1948 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
1949 bool InTemplateArgument) {
1950 if (Style.Language == FormatStyle::LK_JavaScript)
1951 return TT_BinaryOperator;
1952
1953 // && in C# must be a binary operator.
1954 if (Style.isCSharp() && Tok.is(tok::ampamp))
1955 return TT_BinaryOperator;
1956
1957 const FormatToken *PrevToken = Tok.getPreviousNonComment();
1958 if (!PrevToken)
1959 return TT_UnaryOperator;
1960
1961 const FormatToken *NextToken = Tok.getNextNonComment();
1962 if (!NextToken ||
1963 NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_noexcept) ||
1964 NextToken->canBePointerOrReferenceQualifier() ||
1965 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
1966 return TT_PointerOrReference;
1967
1968 if (PrevToken->is(tok::coloncolon))
1969 return TT_PointerOrReference;
1970
1971 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
1972 return TT_PointerOrReference;
1973
1974 if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
1975 tok::comma, tok::semi, tok::kw_return, tok::colon,
1976 tok::kw_co_return, tok::kw_co_await,
1977 tok::kw_co_yield, tok::equal, tok::kw_delete,
1978 tok::kw_sizeof, tok::kw_throw) ||
1979 PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
1980 TT_UnaryOperator, TT_CastRParen))
1981 return TT_UnaryOperator;
1982
1983 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
1984 return TT_PointerOrReference;
1985 if (NextToken->is(tok::kw_operator) && !IsExpression)
1986 return TT_PointerOrReference;
1987 if (NextToken->isOneOf(tok::comma, tok::semi))
1988 return TT_PointerOrReference;
1989
1990 if (PrevToken->Tok.isLiteral() ||
1991 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
1992 tok::kw_false, tok::r_brace) ||
1993 NextToken->Tok.isLiteral() ||
1994 NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
1995 NextToken->isUnaryOperator() ||
1996 // If we know we're in a template argument, there are no named
1997 // declarations. Thus, having an identifier on the right-hand side
1998 // indicates a binary operator.
1999 (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
2000 return TT_BinaryOperator;
2001
2002 // "&&(" is quite unlikely to be two successive unary "&".
2003 if (Tok.is(tok::ampamp) && NextToken->is(tok::l_paren))
2004 return TT_BinaryOperator;
2005
2006 // This catches some cases where evaluation order is used as control flow:
2007 // aaa && aaa->f();
2008 if (NextToken->Tok.isAnyIdentifier()) {
2009 const FormatToken *NextNextToken = NextToken->getNextNonComment();
2010 if (NextNextToken && NextNextToken->is(tok::arrow))
2011 return TT_BinaryOperator;
2012 }
2013
2014 // It is very unlikely that we are going to find a pointer or reference type
2015 // definition on the RHS of an assignment.
2016 if (IsExpression && !Contexts.back().CaretFound)
2017 return TT_BinaryOperator;
2018
2019 return TT_PointerOrReference;
2020 }
2021
determinePlusMinusCaretUsage(const FormatToken & Tok)2022 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
2023 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2024 if (!PrevToken)
2025 return TT_UnaryOperator;
2026
2027 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2028 // This must be a sequence of leading unary operators.
2029 return TT_UnaryOperator;
2030
2031 // Use heuristics to recognize unary operators.
2032 if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
2033 tok::question, tok::colon, tok::kw_return,
2034 tok::kw_case, tok::at, tok::l_brace, tok::kw_throw,
2035 tok::kw_co_return, tok::kw_co_yield))
2036 return TT_UnaryOperator;
2037
2038 // There can't be two consecutive binary operators.
2039 if (PrevToken->is(TT_BinaryOperator))
2040 return TT_UnaryOperator;
2041
2042 // Fall back to marking the token as binary operator.
2043 return TT_BinaryOperator;
2044 }
2045
2046 /// Determine whether ++/-- are pre- or post-increments/-decrements.
determineIncrementUsage(const FormatToken & Tok)2047 TokenType determineIncrementUsage(const FormatToken &Tok) {
2048 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2049 if (!PrevToken || PrevToken->is(TT_CastRParen))
2050 return TT_UnaryOperator;
2051 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
2052 return TT_TrailingUnaryOperator;
2053
2054 return TT_UnaryOperator;
2055 }
2056
2057 SmallVector<Context, 8> Contexts;
2058
2059 const FormatStyle &Style;
2060 AnnotatedLine &Line;
2061 FormatToken *CurrentToken;
2062 bool AutoFound;
2063 const AdditionalKeywords &Keywords;
2064
2065 // Set of "<" tokens that do not open a template parameter list. If parseAngle
2066 // determines that a specific token can't be a template opener, it will make
2067 // same decision irrespective of the decisions for tokens leading up to it.
2068 // Store this information to prevent this from causing exponential runtime.
2069 llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
2070 };
2071
2072 static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
2073 static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
2074
2075 /// Parses binary expressions by inserting fake parenthesis based on
2076 /// operator precedence.
2077 class ExpressionParser {
2078 public:
ExpressionParser(const FormatStyle & Style,const AdditionalKeywords & Keywords,AnnotatedLine & Line)2079 ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
2080 AnnotatedLine &Line)
2081 : Style(Style), Keywords(Keywords), Current(Line.First) {}
2082
2083 /// Parse expressions with the given operator precedence.
parse(int Precedence=0)2084 void parse(int Precedence = 0) {
2085 // Skip 'return' and ObjC selector colons as they are not part of a binary
2086 // expression.
2087 while (Current && (Current->is(tok::kw_return) ||
2088 (Current->is(tok::colon) &&
2089 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))))
2090 next();
2091
2092 if (!Current || Precedence > PrecedenceArrowAndPeriod)
2093 return;
2094
2095 // Conditional expressions need to be parsed separately for proper nesting.
2096 if (Precedence == prec::Conditional) {
2097 parseConditionalExpr();
2098 return;
2099 }
2100
2101 // Parse unary operators, which all have a higher precedence than binary
2102 // operators.
2103 if (Precedence == PrecedenceUnaryOperator) {
2104 parseUnaryOperator();
2105 return;
2106 }
2107
2108 FormatToken *Start = Current;
2109 FormatToken *LatestOperator = nullptr;
2110 unsigned OperatorIndex = 0;
2111
2112 while (Current) {
2113 // Consume operators with higher precedence.
2114 parse(Precedence + 1);
2115
2116 int CurrentPrecedence = getCurrentPrecedence();
2117
2118 if (Current && Current->is(TT_SelectorName) &&
2119 Precedence == CurrentPrecedence) {
2120 if (LatestOperator)
2121 addFakeParenthesis(Start, prec::Level(Precedence));
2122 Start = Current;
2123 }
2124
2125 // At the end of the line or when an operator with higher precedence is
2126 // found, insert fake parenthesis and return.
2127 if (!Current ||
2128 (Current->closesScope() &&
2129 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
2130 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
2131 (CurrentPrecedence == prec::Conditional &&
2132 Precedence == prec::Assignment && Current->is(tok::colon))) {
2133 break;
2134 }
2135
2136 // Consume scopes: (), [], <> and {}
2137 if (Current->opensScope()) {
2138 // In fragment of a JavaScript template string can look like '}..${' and
2139 // thus close a scope and open a new one at the same time.
2140 while (Current && (!Current->closesScope() || Current->opensScope())) {
2141 next();
2142 parse();
2143 }
2144 next();
2145 } else {
2146 // Operator found.
2147 if (CurrentPrecedence == Precedence) {
2148 if (LatestOperator)
2149 LatestOperator->NextOperator = Current;
2150 LatestOperator = Current;
2151 Current->OperatorIndex = OperatorIndex;
2152 ++OperatorIndex;
2153 }
2154 next(/*SkipPastLeadingComments=*/Precedence > 0);
2155 }
2156 }
2157
2158 if (LatestOperator && (Current || Precedence > 0)) {
2159 // LatestOperator->LastOperator = true;
2160 if (Precedence == PrecedenceArrowAndPeriod) {
2161 // Call expressions don't have a binary operator precedence.
2162 addFakeParenthesis(Start, prec::Unknown);
2163 } else {
2164 addFakeParenthesis(Start, prec::Level(Precedence));
2165 }
2166 }
2167 }
2168
2169 private:
2170 /// Gets the precedence (+1) of the given token for binary operators
2171 /// and other tokens that we treat like binary operators.
getCurrentPrecedence()2172 int getCurrentPrecedence() {
2173 if (Current) {
2174 const FormatToken *NextNonComment = Current->getNextNonComment();
2175 if (Current->is(TT_ConditionalExpr))
2176 return prec::Conditional;
2177 if (NextNonComment && Current->is(TT_SelectorName) &&
2178 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
2179 ((Style.Language == FormatStyle::LK_Proto ||
2180 Style.Language == FormatStyle::LK_TextProto) &&
2181 NextNonComment->is(tok::less))))
2182 return prec::Assignment;
2183 if (Current->is(TT_JsComputedPropertyName))
2184 return prec::Assignment;
2185 if (Current->is(TT_LambdaArrow))
2186 return prec::Comma;
2187 if (Current->is(TT_JsFatArrow))
2188 return prec::Assignment;
2189 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
2190 (Current->is(tok::comment) && NextNonComment &&
2191 NextNonComment->is(TT_SelectorName)))
2192 return 0;
2193 if (Current->is(TT_RangeBasedForLoopColon))
2194 return prec::Comma;
2195 if ((Style.Language == FormatStyle::LK_Java ||
2196 Style.Language == FormatStyle::LK_JavaScript) &&
2197 Current->is(Keywords.kw_instanceof))
2198 return prec::Relational;
2199 if (Style.Language == FormatStyle::LK_JavaScript &&
2200 Current->isOneOf(Keywords.kw_in, Keywords.kw_as))
2201 return prec::Relational;
2202 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
2203 return Current->getPrecedence();
2204 if (Current->isOneOf(tok::period, tok::arrow))
2205 return PrecedenceArrowAndPeriod;
2206 if ((Style.Language == FormatStyle::LK_Java ||
2207 Style.Language == FormatStyle::LK_JavaScript) &&
2208 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
2209 Keywords.kw_throws))
2210 return 0;
2211 }
2212 return -1;
2213 }
2214
addFakeParenthesis(FormatToken * Start,prec::Level Precedence)2215 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
2216 Start->FakeLParens.push_back(Precedence);
2217 if (Precedence > prec::Unknown)
2218 Start->StartsBinaryExpression = true;
2219 if (Current) {
2220 FormatToken *Previous = Current->Previous;
2221 while (Previous->is(tok::comment) && Previous->Previous)
2222 Previous = Previous->Previous;
2223 ++Previous->FakeRParens;
2224 if (Precedence > prec::Unknown)
2225 Previous->EndsBinaryExpression = true;
2226 }
2227 }
2228
2229 /// Parse unary operator expressions and surround them with fake
2230 /// parentheses if appropriate.
parseUnaryOperator()2231 void parseUnaryOperator() {
2232 llvm::SmallVector<FormatToken *, 2> Tokens;
2233 while (Current && Current->is(TT_UnaryOperator)) {
2234 Tokens.push_back(Current);
2235 next();
2236 }
2237 parse(PrecedenceArrowAndPeriod);
2238 for (FormatToken *Token : llvm::reverse(Tokens))
2239 // The actual precedence doesn't matter.
2240 addFakeParenthesis(Token, prec::Unknown);
2241 }
2242
parseConditionalExpr()2243 void parseConditionalExpr() {
2244 while (Current && Current->isTrailingComment()) {
2245 next();
2246 }
2247 FormatToken *Start = Current;
2248 parse(prec::LogicalOr);
2249 if (!Current || !Current->is(tok::question))
2250 return;
2251 next();
2252 parse(prec::Assignment);
2253 if (!Current || Current->isNot(TT_ConditionalExpr))
2254 return;
2255 next();
2256 parse(prec::Assignment);
2257 addFakeParenthesis(Start, prec::Conditional);
2258 }
2259
next(bool SkipPastLeadingComments=true)2260 void next(bool SkipPastLeadingComments = true) {
2261 if (Current)
2262 Current = Current->Next;
2263 while (Current &&
2264 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
2265 Current->isTrailingComment())
2266 Current = Current->Next;
2267 }
2268
2269 const FormatStyle &Style;
2270 const AdditionalKeywords &Keywords;
2271 FormatToken *Current;
2272 };
2273
2274 } // end anonymous namespace
2275
setCommentLineLevels(SmallVectorImpl<AnnotatedLine * > & Lines)2276 void TokenAnnotator::setCommentLineLevels(
2277 SmallVectorImpl<AnnotatedLine *> &Lines) {
2278 const AnnotatedLine *NextNonCommentLine = nullptr;
2279 for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(),
2280 E = Lines.rend();
2281 I != E; ++I) {
2282 bool CommentLine = true;
2283 for (const FormatToken *Tok = (*I)->First; Tok; Tok = Tok->Next) {
2284 if (!Tok->is(tok::comment)) {
2285 CommentLine = false;
2286 break;
2287 }
2288 }
2289
2290 // If the comment is currently aligned with the line immediately following
2291 // it, that's probably intentional and we should keep it.
2292 if (NextNonCommentLine && CommentLine &&
2293 NextNonCommentLine->First->NewlinesBefore <= 1 &&
2294 NextNonCommentLine->First->OriginalColumn ==
2295 (*I)->First->OriginalColumn) {
2296 // Align comments for preprocessor lines with the # in column 0 if
2297 // preprocessor lines are not indented. Otherwise, align with the next
2298 // line.
2299 (*I)->Level =
2300 (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
2301 (NextNonCommentLine->Type == LT_PreprocessorDirective ||
2302 NextNonCommentLine->Type == LT_ImportStatement))
2303 ? 0
2304 : NextNonCommentLine->Level;
2305 } else {
2306 NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr;
2307 }
2308
2309 setCommentLineLevels((*I)->Children);
2310 }
2311 }
2312
maxNestingDepth(const AnnotatedLine & Line)2313 static unsigned maxNestingDepth(const AnnotatedLine &Line) {
2314 unsigned Result = 0;
2315 for (const auto *Tok = Line.First; Tok != nullptr; Tok = Tok->Next)
2316 Result = std::max(Result, Tok->NestingLevel);
2317 return Result;
2318 }
2319
annotate(AnnotatedLine & Line)2320 void TokenAnnotator::annotate(AnnotatedLine &Line) {
2321 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
2322 E = Line.Children.end();
2323 I != E; ++I) {
2324 annotate(**I);
2325 }
2326 AnnotatingParser Parser(Style, Line, Keywords);
2327 Line.Type = Parser.parseLine();
2328
2329 // With very deep nesting, ExpressionParser uses lots of stack and the
2330 // formatting algorithm is very slow. We're not going to do a good job here
2331 // anyway - it's probably generated code being formatted by mistake.
2332 // Just skip the whole line.
2333 if (maxNestingDepth(Line) > 50)
2334 Line.Type = LT_Invalid;
2335
2336 if (Line.Type == LT_Invalid)
2337 return;
2338
2339 ExpressionParser ExprParser(Style, Keywords, Line);
2340 ExprParser.parse();
2341
2342 if (Line.startsWith(TT_ObjCMethodSpecifier))
2343 Line.Type = LT_ObjCMethodDecl;
2344 else if (Line.startsWith(TT_ObjCDecl))
2345 Line.Type = LT_ObjCDecl;
2346 else if (Line.startsWith(TT_ObjCProperty))
2347 Line.Type = LT_ObjCProperty;
2348
2349 Line.First->SpacesRequiredBefore = 1;
2350 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
2351 }
2352
2353 // This function heuristically determines whether 'Current' starts the name of a
2354 // function declaration.
isFunctionDeclarationName(const FormatToken & Current,const AnnotatedLine & Line)2355 static bool isFunctionDeclarationName(const FormatToken &Current,
2356 const AnnotatedLine &Line) {
2357 auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
2358 for (; Next; Next = Next->Next) {
2359 if (Next->is(TT_OverloadedOperatorLParen))
2360 return Next;
2361 if (Next->is(TT_OverloadedOperator))
2362 continue;
2363 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
2364 // For 'new[]' and 'delete[]'.
2365 if (Next->Next &&
2366 Next->Next->startsSequence(tok::l_square, tok::r_square))
2367 Next = Next->Next->Next;
2368 continue;
2369 }
2370 if (Next->startsSequence(tok::l_square, tok::r_square)) {
2371 // For operator[]().
2372 Next = Next->Next;
2373 continue;
2374 }
2375 if ((Next->isSimpleTypeSpecifier() || Next->is(tok::identifier)) &&
2376 Next->Next && Next->Next->isOneOf(tok::star, tok::amp, tok::ampamp)) {
2377 // For operator void*(), operator char*(), operator Foo*().
2378 Next = Next->Next;
2379 continue;
2380 }
2381 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
2382 Next = Next->MatchingParen;
2383 continue;
2384 }
2385
2386 break;
2387 }
2388 return nullptr;
2389 };
2390
2391 // Find parentheses of parameter list.
2392 const FormatToken *Next = Current.Next;
2393 if (Current.is(tok::kw_operator)) {
2394 if (Current.Previous && Current.Previous->is(tok::coloncolon))
2395 return false;
2396 Next = skipOperatorName(Next);
2397 } else {
2398 if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0)
2399 return false;
2400 for (; Next; Next = Next->Next) {
2401 if (Next->is(TT_TemplateOpener)) {
2402 Next = Next->MatchingParen;
2403 } else if (Next->is(tok::coloncolon)) {
2404 Next = Next->Next;
2405 if (!Next)
2406 return false;
2407 if (Next->is(tok::kw_operator)) {
2408 Next = skipOperatorName(Next->Next);
2409 break;
2410 }
2411 if (!Next->is(tok::identifier))
2412 return false;
2413 } else if (Next->is(tok::l_paren)) {
2414 break;
2415 } else {
2416 return false;
2417 }
2418 }
2419 }
2420
2421 // Check whether parameter list can belong to a function declaration.
2422 if (!Next || !Next->is(tok::l_paren) || !Next->MatchingParen)
2423 return false;
2424 // If the lines ends with "{", this is likely an function definition.
2425 if (Line.Last->is(tok::l_brace))
2426 return true;
2427 if (Next->Next == Next->MatchingParen)
2428 return true; // Empty parentheses.
2429 // If there is an &/&& after the r_paren, this is likely a function.
2430 if (Next->MatchingParen->Next &&
2431 Next->MatchingParen->Next->is(TT_PointerOrReference))
2432 return true;
2433 for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
2434 Tok = Tok->Next) {
2435 if (Tok->is(TT_TypeDeclarationParen))
2436 return true;
2437 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
2438 Tok = Tok->MatchingParen;
2439 continue;
2440 }
2441 if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
2442 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis))
2443 return true;
2444 if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
2445 Tok->Tok.isLiteral())
2446 return false;
2447 }
2448 return false;
2449 }
2450
mustBreakForReturnType(const AnnotatedLine & Line) const2451 bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
2452 assert(Line.MightBeFunctionDecl);
2453
2454 if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
2455 Style.AlwaysBreakAfterReturnType ==
2456 FormatStyle::RTBS_TopLevelDefinitions) &&
2457 Line.Level > 0)
2458 return false;
2459
2460 switch (Style.AlwaysBreakAfterReturnType) {
2461 case FormatStyle::RTBS_None:
2462 return false;
2463 case FormatStyle::RTBS_All:
2464 case FormatStyle::RTBS_TopLevel:
2465 return true;
2466 case FormatStyle::RTBS_AllDefinitions:
2467 case FormatStyle::RTBS_TopLevelDefinitions:
2468 return Line.mightBeFunctionDefinition();
2469 }
2470
2471 return false;
2472 }
2473
calculateFormattingInformation(AnnotatedLine & Line)2474 void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
2475 for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(),
2476 E = Line.Children.end();
2477 I != E; ++I) {
2478 calculateFormattingInformation(**I);
2479 }
2480
2481 Line.First->TotalLength =
2482 Line.First->IsMultiline ? Style.ColumnLimit
2483 : Line.FirstStartColumn + Line.First->ColumnWidth;
2484 FormatToken *Current = Line.First->Next;
2485 bool InFunctionDecl = Line.MightBeFunctionDecl;
2486 while (Current) {
2487 if (isFunctionDeclarationName(*Current, Line))
2488 Current->setType(TT_FunctionDeclarationName);
2489 if (Current->is(TT_LineComment)) {
2490 if (Current->Previous->is(BK_BracedInit) &&
2491 Current->Previous->opensScope())
2492 Current->SpacesRequiredBefore =
2493 (Style.Cpp11BracedListStyle && !Style.SpacesInParentheses) ? 0 : 1;
2494 else
2495 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
2496
2497 // If we find a trailing comment, iterate backwards to determine whether
2498 // it seems to relate to a specific parameter. If so, break before that
2499 // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
2500 // to the previous line in:
2501 // SomeFunction(a,
2502 // b, // comment
2503 // c);
2504 if (!Current->HasUnescapedNewline) {
2505 for (FormatToken *Parameter = Current->Previous; Parameter;
2506 Parameter = Parameter->Previous) {
2507 if (Parameter->isOneOf(tok::comment, tok::r_brace))
2508 break;
2509 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
2510 if (!Parameter->Previous->is(TT_CtorInitializerComma) &&
2511 Parameter->HasUnescapedNewline)
2512 Parameter->MustBreakBefore = true;
2513 break;
2514 }
2515 }
2516 }
2517 } else if (Current->SpacesRequiredBefore == 0 &&
2518 spaceRequiredBefore(Line, *Current)) {
2519 Current->SpacesRequiredBefore = 1;
2520 }
2521
2522 Current->MustBreakBefore =
2523 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
2524
2525 if (!Current->MustBreakBefore && InFunctionDecl &&
2526 Current->is(TT_FunctionDeclarationName))
2527 Current->MustBreakBefore = mustBreakForReturnType(Line);
2528
2529 Current->CanBreakBefore =
2530 Current->MustBreakBefore || canBreakBefore(Line, *Current);
2531 unsigned ChildSize = 0;
2532 if (Current->Previous->Children.size() == 1) {
2533 FormatToken &LastOfChild = *Current->Previous->Children[0]->Last;
2534 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
2535 : LastOfChild.TotalLength + 1;
2536 }
2537 const FormatToken *Prev = Current->Previous;
2538 if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
2539 (Prev->Children.size() == 1 &&
2540 Prev->Children[0]->First->MustBreakBefore) ||
2541 Current->IsMultiline)
2542 Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
2543 else
2544 Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
2545 ChildSize + Current->SpacesRequiredBefore;
2546
2547 if (Current->is(TT_CtorInitializerColon))
2548 InFunctionDecl = false;
2549
2550 // FIXME: Only calculate this if CanBreakBefore is true once static
2551 // initializers etc. are sorted out.
2552 // FIXME: Move magic numbers to a better place.
2553
2554 // Reduce penalty for aligning ObjC method arguments using the colon
2555 // alignment as this is the canonical way (still prefer fitting everything
2556 // into one line if possible). Trying to fit a whole expression into one
2557 // line should not force other line breaks (e.g. when ObjC method
2558 // expression is a part of other expression).
2559 Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
2560 if (Style.Language == FormatStyle::LK_ObjC &&
2561 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
2562 if (Current->ParameterIndex == 1)
2563 Current->SplitPenalty += 5 * Current->BindingStrength;
2564 } else {
2565 Current->SplitPenalty += 20 * Current->BindingStrength;
2566 }
2567
2568 Current = Current->Next;
2569 }
2570
2571 calculateUnbreakableTailLengths(Line);
2572 unsigned IndentLevel = Line.Level;
2573 for (Current = Line.First; Current != nullptr; Current = Current->Next) {
2574 if (Current->Role)
2575 Current->Role->precomputeFormattingInfos(Current);
2576 if (Current->MatchingParen &&
2577 Current->MatchingParen->opensBlockOrBlockTypeList(Style)) {
2578 assert(IndentLevel > 0);
2579 --IndentLevel;
2580 }
2581 Current->IndentLevel = IndentLevel;
2582 if (Current->opensBlockOrBlockTypeList(Style))
2583 ++IndentLevel;
2584 }
2585
2586 LLVM_DEBUG({ printDebugInfo(Line); });
2587 }
2588
calculateUnbreakableTailLengths(AnnotatedLine & Line)2589 void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
2590 unsigned UnbreakableTailLength = 0;
2591 FormatToken *Current = Line.Last;
2592 while (Current) {
2593 Current->UnbreakableTailLength = UnbreakableTailLength;
2594 if (Current->CanBreakBefore ||
2595 Current->isOneOf(tok::comment, tok::string_literal)) {
2596 UnbreakableTailLength = 0;
2597 } else {
2598 UnbreakableTailLength +=
2599 Current->ColumnWidth + Current->SpacesRequiredBefore;
2600 }
2601 Current = Current->Previous;
2602 }
2603 }
2604
splitPenalty(const AnnotatedLine & Line,const FormatToken & Tok,bool InFunctionDecl)2605 unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
2606 const FormatToken &Tok,
2607 bool InFunctionDecl) {
2608 const FormatToken &Left = *Tok.Previous;
2609 const FormatToken &Right = Tok;
2610
2611 if (Left.is(tok::semi))
2612 return 0;
2613
2614 if (Style.Language == FormatStyle::LK_Java) {
2615 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
2616 return 1;
2617 if (Right.is(Keywords.kw_implements))
2618 return 2;
2619 if (Left.is(tok::comma) && Left.NestingLevel == 0)
2620 return 3;
2621 } else if (Style.Language == FormatStyle::LK_JavaScript) {
2622 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
2623 return 100;
2624 if (Left.is(TT_JsTypeColon))
2625 return 35;
2626 if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
2627 (Right.is(TT_TemplateString) && Right.TokenText.startswith("}")))
2628 return 100;
2629 // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
2630 if (Left.opensScope() && Right.closesScope())
2631 return 200;
2632 }
2633
2634 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
2635 return 1;
2636 if (Right.is(tok::l_square)) {
2637 if (Style.Language == FormatStyle::LK_Proto)
2638 return 1;
2639 if (Left.is(tok::r_square))
2640 return 200;
2641 // Slightly prefer formatting local lambda definitions like functions.
2642 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
2643 return 35;
2644 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
2645 TT_ArrayInitializerLSquare,
2646 TT_DesignatedInitializerLSquare, TT_AttributeSquare))
2647 return 500;
2648 }
2649
2650 if (Left.is(tok::coloncolon) ||
2651 (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
2652 return 500;
2653 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
2654 Right.is(tok::kw_operator)) {
2655 if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
2656 return 3;
2657 if (Left.is(TT_StartOfName))
2658 return 110;
2659 if (InFunctionDecl && Right.NestingLevel == 0)
2660 return Style.PenaltyReturnTypeOnItsOwnLine;
2661 return 200;
2662 }
2663 if (Right.is(TT_PointerOrReference))
2664 return 190;
2665 if (Right.is(TT_LambdaArrow))
2666 return 110;
2667 if (Left.is(tok::equal) && Right.is(tok::l_brace))
2668 return 160;
2669 if (Left.is(TT_CastRParen))
2670 return 100;
2671 if (Left.isOneOf(tok::kw_class, tok::kw_struct))
2672 return 5000;
2673 if (Left.is(tok::comment))
2674 return 1000;
2675
2676 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
2677 TT_CtorInitializerColon))
2678 return 2;
2679
2680 if (Right.isMemberAccess()) {
2681 // Breaking before the "./->" of a chained call/member access is reasonably
2682 // cheap, as formatting those with one call per line is generally
2683 // desirable. In particular, it should be cheaper to break before the call
2684 // than it is to break inside a call's parameters, which could lead to weird
2685 // "hanging" indents. The exception is the very last "./->" to support this
2686 // frequent pattern:
2687 //
2688 // aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
2689 // dddddddd);
2690 //
2691 // which might otherwise be blown up onto many lines. Here, clang-format
2692 // won't produce "hanging" indents anyway as there is no other trailing
2693 // call.
2694 //
2695 // Also apply higher penalty is not a call as that might lead to a wrapping
2696 // like:
2697 //
2698 // aaaaaaa
2699 // .aaaaaaaaa.bbbbbbbb(cccccccc);
2700 return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
2701 ? 150
2702 : 35;
2703 }
2704
2705 if (Right.is(TT_TrailingAnnotation) &&
2706 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
2707 // Moving trailing annotations to the next line is fine for ObjC method
2708 // declarations.
2709 if (Line.startsWith(TT_ObjCMethodSpecifier))
2710 return 10;
2711 // Generally, breaking before a trailing annotation is bad unless it is
2712 // function-like. It seems to be especially preferable to keep standard
2713 // annotations (i.e. "const", "final" and "override") on the same line.
2714 // Use a slightly higher penalty after ")" so that annotations like
2715 // "const override" are kept together.
2716 bool is_short_annotation = Right.TokenText.size() < 10;
2717 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
2718 }
2719
2720 // In for-loops, prefer breaking at ',' and ';'.
2721 if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
2722 return 4;
2723
2724 // In Objective-C method expressions, prefer breaking before "param:" over
2725 // breaking after it.
2726 if (Right.is(TT_SelectorName))
2727 return 0;
2728 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
2729 return Line.MightBeFunctionDecl ? 50 : 500;
2730
2731 // In Objective-C type declarations, avoid breaking after the category's
2732 // open paren (we'll prefer breaking after the protocol list's opening
2733 // angle bracket, if present).
2734 if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous &&
2735 Left.Previous->isOneOf(tok::identifier, tok::greater))
2736 return 500;
2737
2738 if (Left.is(tok::l_paren) && InFunctionDecl &&
2739 Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign)
2740 return 100;
2741 if (Left.is(tok::l_paren) && Left.Previous &&
2742 (Left.Previous->is(tok::kw_for) || Left.Previous->isIf()))
2743 return 1000;
2744 if (Left.is(tok::equal) && InFunctionDecl)
2745 return 110;
2746 if (Right.is(tok::r_brace))
2747 return 1;
2748 if (Left.is(TT_TemplateOpener))
2749 return 100;
2750 if (Left.opensScope()) {
2751 // If we aren't aligning after opening parens/braces we can always break
2752 // here unless the style does not want us to place all arguments on the
2753 // next line.
2754 if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
2755 (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine))
2756 return 0;
2757 if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
2758 return 19;
2759 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
2760 : 19;
2761 }
2762 if (Left.is(TT_JavaAnnotation))
2763 return 50;
2764
2765 if (Left.is(TT_UnaryOperator))
2766 return 60;
2767 if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
2768 Left.Previous->isLabelString() &&
2769 (Left.NextOperator || Left.OperatorIndex != 0))
2770 return 50;
2771 if (Right.is(tok::plus) && Left.isLabelString() &&
2772 (Right.NextOperator || Right.OperatorIndex != 0))
2773 return 25;
2774 if (Left.is(tok::comma))
2775 return 1;
2776 if (Right.is(tok::lessless) && Left.isLabelString() &&
2777 (Right.NextOperator || Right.OperatorIndex != 1))
2778 return 25;
2779 if (Right.is(tok::lessless)) {
2780 // Breaking at a << is really cheap.
2781 if (!Left.is(tok::r_paren) || Right.OperatorIndex > 0)
2782 // Slightly prefer to break before the first one in log-like statements.
2783 return 2;
2784 return 1;
2785 }
2786 if (Left.ClosesTemplateDeclaration)
2787 return Style.PenaltyBreakTemplateDeclaration;
2788 if (Left.is(TT_ConditionalExpr))
2789 return prec::Conditional;
2790 prec::Level Level = Left.getPrecedence();
2791 if (Level == prec::Unknown)
2792 Level = Right.getPrecedence();
2793 if (Level == prec::Assignment)
2794 return Style.PenaltyBreakAssignment;
2795 if (Level != prec::Unknown)
2796 return Level;
2797
2798 return 3;
2799 }
2800
spaceRequiredBeforeParens(const FormatToken & Right) const2801 bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
2802 return Style.SpaceBeforeParens == FormatStyle::SBPO_Always ||
2803 (Style.SpaceBeforeParens == FormatStyle::SBPO_NonEmptyParentheses &&
2804 Right.ParameterCount > 0);
2805 }
2806
spaceRequiredBetween(const AnnotatedLine & Line,const FormatToken & Left,const FormatToken & Right)2807 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
2808 const FormatToken &Left,
2809 const FormatToken &Right) {
2810 if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
2811 return true;
2812 if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java)
2813 return true;
2814 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
2815 Left.Tok.getObjCKeywordID() == tok::objc_property)
2816 return true;
2817 if (Right.is(tok::hashhash))
2818 return Left.is(tok::hash);
2819 if (Left.isOneOf(tok::hashhash, tok::hash))
2820 return Right.is(tok::hash);
2821 if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
2822 (Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
2823 Right.is(tok::r_brace) && Right.isNot(BK_Block)))
2824 return Style.SpaceInEmptyParentheses;
2825 if (Style.SpacesInConditionalStatement) {
2826 if (Left.is(tok::l_paren) && Left.Previous &&
2827 isKeywordWithCondition(*Left.Previous))
2828 return true;
2829 if (Right.is(tok::r_paren) && Right.MatchingParen &&
2830 Right.MatchingParen->Previous &&
2831 isKeywordWithCondition(*Right.MatchingParen->Previous))
2832 return true;
2833 }
2834
2835 // requires ( or requires(
2836 if (Right.is(tok::l_paren) && Left.is(tok::kw_requires))
2837 return spaceRequiredBeforeParens(Right);
2838 // requires clause Concept1<T> && Concept2<T>
2839 if (Left.is(TT_ConstraintJunctions) && Right.is(tok::identifier))
2840 return true;
2841
2842 if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
2843 return (Right.is(TT_CastRParen) ||
2844 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
2845 ? Style.SpacesInCStyleCastParentheses
2846 : Style.SpacesInParentheses;
2847 if (Right.isOneOf(tok::semi, tok::comma))
2848 return false;
2849 if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) {
2850 bool IsLightweightGeneric = Right.MatchingParen &&
2851 Right.MatchingParen->Next &&
2852 Right.MatchingParen->Next->is(tok::colon);
2853 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
2854 }
2855 if (Right.is(tok::less) && Left.is(tok::kw_template))
2856 return Style.SpaceAfterTemplateKeyword;
2857 if (Left.isOneOf(tok::exclaim, tok::tilde))
2858 return false;
2859 if (Left.is(tok::at) &&
2860 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
2861 tok::numeric_constant, tok::l_paren, tok::l_brace,
2862 tok::kw_true, tok::kw_false))
2863 return false;
2864 if (Left.is(tok::colon))
2865 return !Left.is(TT_ObjCMethodExpr);
2866 if (Left.is(tok::coloncolon))
2867 return false;
2868 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
2869 if (Style.Language == FormatStyle::LK_TextProto ||
2870 (Style.Language == FormatStyle::LK_Proto &&
2871 (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
2872 // Format empty list as `<>`.
2873 if (Left.is(tok::less) && Right.is(tok::greater))
2874 return false;
2875 return !Style.Cpp11BracedListStyle;
2876 }
2877 return false;
2878 }
2879 if (Right.is(tok::ellipsis))
2880 return Left.Tok.isLiteral() || (Left.is(tok::identifier) && Left.Previous &&
2881 Left.Previous->is(tok::kw_case));
2882 if (Left.is(tok::l_square) && Right.is(tok::amp))
2883 return Style.SpacesInSquareBrackets;
2884 if (Right.is(TT_PointerOrReference)) {
2885 if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) {
2886 if (!Left.MatchingParen)
2887 return true;
2888 FormatToken *TokenBeforeMatchingParen =
2889 Left.MatchingParen->getPreviousNonComment();
2890 if (!TokenBeforeMatchingParen || !Left.is(TT_TypeDeclarationParen))
2891 return true;
2892 }
2893 // Add a space if the previous token is a pointer qualifer or the closing
2894 // parenthesis of __attribute__(()) expression and the style requires spaces
2895 // after pointer qualifiers.
2896 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
2897 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
2898 (Left.is(TT_AttributeParen) || Left.canBePointerOrReferenceQualifier()))
2899 return true;
2900 return (Left.Tok.isLiteral() ||
2901 (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
2902 (Style.PointerAlignment != FormatStyle::PAS_Left ||
2903 (Line.IsMultiVariableDeclStmt &&
2904 (Left.NestingLevel == 0 ||
2905 (Left.NestingLevel == 1 && Line.First->is(tok::kw_for)))))));
2906 }
2907 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
2908 (!Left.is(TT_PointerOrReference) ||
2909 (Style.PointerAlignment != FormatStyle::PAS_Right &&
2910 !Line.IsMultiVariableDeclStmt)))
2911 return true;
2912 if (Left.is(TT_PointerOrReference)) {
2913 // Add a space if the next token is a pointer qualifer and the style
2914 // requires spaces before pointer qualifiers.
2915 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
2916 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
2917 Right.canBePointerOrReferenceQualifier())
2918 return true;
2919 return Right.Tok.isLiteral() || Right.is(TT_BlockComment) ||
2920 (Right.isOneOf(Keywords.kw_override, Keywords.kw_final) &&
2921 !Right.is(TT_StartOfName)) ||
2922 (Right.is(tok::l_brace) && Right.is(BK_Block)) ||
2923 (!Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
2924 tok::l_paren) &&
2925 (Style.PointerAlignment != FormatStyle::PAS_Right &&
2926 !Line.IsMultiVariableDeclStmt) &&
2927 Left.Previous &&
2928 !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon,
2929 tok::l_square));
2930 }
2931 // Ensure right pointer alignement with ellipsis e.g. int *...P
2932 if (Left.is(tok::ellipsis) && Left.Previous &&
2933 Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp))
2934 return Style.PointerAlignment != FormatStyle::PAS_Right;
2935
2936 if (Right.is(tok::star) && Left.is(tok::l_paren))
2937 return false;
2938 if (Left.is(tok::star) && Right.isOneOf(tok::star, tok::amp, tok::ampamp))
2939 return false;
2940 if (Right.isOneOf(tok::star, tok::amp, tok::ampamp)) {
2941 const FormatToken *Previous = &Left;
2942 while (Previous && !Previous->is(tok::kw_operator)) {
2943 if (Previous->is(tok::identifier) || Previous->isSimpleTypeSpecifier()) {
2944 Previous = Previous->getPreviousNonComment();
2945 continue;
2946 }
2947 if (Previous->is(TT_TemplateCloser) && Previous->MatchingParen) {
2948 Previous = Previous->MatchingParen->getPreviousNonComment();
2949 continue;
2950 }
2951 if (Previous->is(tok::coloncolon)) {
2952 Previous = Previous->getPreviousNonComment();
2953 continue;
2954 }
2955 break;
2956 }
2957 // Space between the type and the * in:
2958 // operator void*()
2959 // operator char*()
2960 // operator /*comment*/ const char*()
2961 // operator volatile /*comment*/ char*()
2962 // operator Foo*()
2963 // operator C<T>*()
2964 // operator std::Foo*()
2965 // operator C<T>::D<U>*()
2966 // dependent on PointerAlignment style.
2967 if (Previous &&
2968 (Previous->endsSequence(tok::kw_operator) ||
2969 Previous->endsSequence(tok::kw_const, tok::kw_operator) ||
2970 Previous->endsSequence(tok::kw_volatile, tok::kw_operator)))
2971 return (Style.PointerAlignment != FormatStyle::PAS_Left);
2972 }
2973 const auto SpaceRequiredForArrayInitializerLSquare =
2974 [](const FormatToken &LSquareTok, const FormatStyle &Style) {
2975 return Style.SpacesInContainerLiterals ||
2976 ((Style.Language == FormatStyle::LK_Proto ||
2977 Style.Language == FormatStyle::LK_TextProto) &&
2978 !Style.Cpp11BracedListStyle &&
2979 LSquareTok.endsSequence(tok::l_square, tok::colon,
2980 TT_SelectorName));
2981 };
2982 if (Left.is(tok::l_square))
2983 return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
2984 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
2985 (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
2986 TT_LambdaLSquare) &&
2987 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
2988 if (Right.is(tok::r_square))
2989 return Right.MatchingParen &&
2990 ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
2991 SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
2992 Style)) ||
2993 (Style.SpacesInSquareBrackets &&
2994 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
2995 TT_StructuredBindingLSquare,
2996 TT_LambdaLSquare)) ||
2997 Right.MatchingParen->is(TT_AttributeParen));
2998 if (Right.is(tok::l_square) &&
2999 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
3000 TT_DesignatedInitializerLSquare,
3001 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
3002 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
3003 !(!Left.is(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
3004 Right.is(TT_ArraySubscriptLSquare)))
3005 return false;
3006 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
3007 return !Left.Children.empty(); // No spaces in "{}".
3008 if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) ||
3009 (Right.is(tok::r_brace) && Right.MatchingParen &&
3010 Right.MatchingParen->isNot(BK_Block)))
3011 return Style.Cpp11BracedListStyle ? Style.SpacesInParentheses : true;
3012 if (Left.is(TT_BlockComment))
3013 // No whitespace in x(/*foo=*/1), except for JavaScript.
3014 return Style.Language == FormatStyle::LK_JavaScript ||
3015 !Left.TokenText.endswith("=*/");
3016
3017 // Space between template and attribute.
3018 // e.g. template <typename T> [[nodiscard]] ...
3019 if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
3020 return true;
3021 if (Right.is(tok::l_paren)) {
3022 if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) ||
3023 (Left.is(tok::r_square) && Left.is(TT_AttributeSquare)))
3024 return true;
3025 if (Style.SpaceBeforeParens ==
3026 FormatStyle::SBPO_ControlStatementsExceptForEachMacros &&
3027 Left.is(TT_ForEachMacro))
3028 return false;
3029 return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
3030 (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
3031 (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while,
3032 tok::kw_switch, tok::kw_case, TT_ForEachMacro,
3033 TT_ObjCForIn) ||
3034 Left.isIf(Line.Type != LT_PreprocessorDirective) ||
3035 (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch,
3036 tok::kw_new, tok::kw_delete) &&
3037 (!Left.Previous || Left.Previous->isNot(tok::period))))) ||
3038 (spaceRequiredBeforeParens(Right) &&
3039 (Left.is(tok::identifier) || Left.isFunctionLikeKeyword() ||
3040 Left.is(tok::r_paren) || Left.isSimpleTypeSpecifier() ||
3041 (Left.is(tok::r_square) && Left.MatchingParen &&
3042 Left.MatchingParen->is(TT_LambdaLSquare))) &&
3043 Line.Type != LT_PreprocessorDirective);
3044 }
3045 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
3046 return false;
3047 if (Right.is(TT_UnaryOperator))
3048 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
3049 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
3050 if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
3051 tok::r_paren) ||
3052 Left.isSimpleTypeSpecifier()) &&
3053 Right.is(tok::l_brace) && Right.getNextNonComment() &&
3054 Right.isNot(BK_Block))
3055 return false;
3056 if (Left.is(tok::period) || Right.is(tok::period))
3057 return false;
3058 if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L")
3059 return false;
3060 if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
3061 Left.MatchingParen->Previous &&
3062 (Left.MatchingParen->Previous->is(tok::period) ||
3063 Left.MatchingParen->Previous->is(tok::coloncolon)))
3064 // Java call to generic function with explicit type:
3065 // A.<B<C<...>>>DoSomething();
3066 // A::<B<C<...>>>DoSomething(); // With a Java 8 method reference.
3067 return false;
3068 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
3069 return false;
3070 if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at))
3071 // Objective-C dictionary literal -> no space after opening brace.
3072 return false;
3073 if (Right.is(tok::r_brace) && Right.MatchingParen &&
3074 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at))
3075 // Objective-C dictionary literal -> no space before closing brace.
3076 return false;
3077 if (Right.getType() == TT_TrailingAnnotation &&
3078 Right.isOneOf(tok::amp, tok::ampamp) &&
3079 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
3080 (!Right.Next || Right.Next->is(tok::semi)))
3081 // Match const and volatile ref-qualifiers without any additional
3082 // qualifiers such as
3083 // void Fn() const &;
3084 return Style.PointerAlignment != FormatStyle::PAS_Left;
3085 return true;
3086 }
3087
spaceRequiredBefore(const AnnotatedLine & Line,const FormatToken & Right)3088 bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
3089 const FormatToken &Right) {
3090 const FormatToken &Left = *Right.Previous;
3091 if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
3092 return true; // Never ever merge two identifiers.
3093 if (Style.isCpp()) {
3094 if (Left.is(tok::kw_operator))
3095 return Right.is(tok::coloncolon);
3096 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
3097 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList)
3098 return true;
3099 } else if (Style.Language == FormatStyle::LK_Proto ||
3100 Style.Language == FormatStyle::LK_TextProto) {
3101 if (Right.is(tok::period) &&
3102 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
3103 Keywords.kw_repeated, Keywords.kw_extend))
3104 return true;
3105 if (Right.is(tok::l_paren) &&
3106 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option))
3107 return true;
3108 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
3109 return true;
3110 // Slashes occur in text protocol extension syntax: [type/type] { ... }.
3111 if (Left.is(tok::slash) || Right.is(tok::slash))
3112 return false;
3113 if (Left.MatchingParen &&
3114 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
3115 Right.isOneOf(tok::l_brace, tok::less))
3116 return !Style.Cpp11BracedListStyle;
3117 // A percent is probably part of a formatting specification, such as %lld.
3118 if (Left.is(tok::percent))
3119 return false;
3120 // Preserve the existence of a space before a percent for cases like 0x%04x
3121 // and "%d %d"
3122 if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
3123 return Right.WhitespaceRange.getEnd() != Right.WhitespaceRange.getBegin();
3124 } else if (Style.isCSharp()) {
3125 // Require spaces around '{' and before '}' unless they appear in
3126 // interpolated strings. Interpolated strings are merged into a single token
3127 // so cannot have spaces inserted by this function.
3128
3129 // No space between 'this' and '['
3130 if (Left.is(tok::kw_this) && Right.is(tok::l_square))
3131 return false;
3132
3133 // No space between 'new' and '('
3134 if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
3135 return false;
3136
3137 // Space before { (including space within '{ {').
3138 if (Right.is(tok::l_brace))
3139 return true;
3140
3141 // Spaces inside braces.
3142 if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace))
3143 return true;
3144
3145 if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace))
3146 return true;
3147
3148 // Spaces around '=>'.
3149 if (Left.is(TT_JsFatArrow) || Right.is(TT_JsFatArrow))
3150 return true;
3151
3152 // No spaces around attribute target colons
3153 if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon))
3154 return false;
3155
3156 // space between type and variable e.g. Dictionary<string,string> foo;
3157 if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
3158 return true;
3159
3160 // spaces inside square brackets.
3161 if (Left.is(tok::l_square) || Right.is(tok::r_square))
3162 return Style.SpacesInSquareBrackets;
3163
3164 // No space before ? in nullable types.
3165 if (Right.is(TT_CSharpNullable))
3166 return false;
3167
3168 // Require space after ? in nullable types except in generics and casts.
3169 if (Left.is(TT_CSharpNullable))
3170 return !Right.isOneOf(TT_TemplateCloser, tok::r_paren);
3171
3172 // No space before or after '?.'.
3173 if (Left.is(TT_CSharpNullConditional) || Right.is(TT_CSharpNullConditional))
3174 return false;
3175
3176 // Space before and after '??'.
3177 if (Left.is(TT_CSharpNullCoalescing) || Right.is(TT_CSharpNullCoalescing))
3178 return true;
3179
3180 // No space before '?['.
3181 if (Right.is(TT_CSharpNullConditionalLSquare))
3182 return false;
3183
3184 // No space between consecutive commas '[,,]'.
3185 if (Left.is(tok::comma) && Right.is(tok::comma))
3186 return false;
3187
3188 // Possible space inside `?[ 0 ]`.
3189 if (Left.is(TT_CSharpNullConditionalLSquare))
3190 return Style.SpacesInSquareBrackets;
3191
3192 // space after var in `var (key, value)`
3193 if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren))
3194 return true;
3195
3196 // space between keywords and paren e.g. "using ("
3197 if (Right.is(tok::l_paren))
3198 if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
3199 Keywords.kw_lock))
3200 return Style.SpaceBeforeParens == FormatStyle::SBPO_ControlStatements ||
3201 spaceRequiredBeforeParens(Right);
3202
3203 // space between method modifier and opening parenthesis of a tuple return
3204 // type
3205 if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
3206 tok::kw_virtual, tok::kw_extern, tok::kw_static,
3207 Keywords.kw_internal, Keywords.kw_abstract,
3208 Keywords.kw_sealed, Keywords.kw_override,
3209 Keywords.kw_async, Keywords.kw_unsafe) &&
3210 Right.is(tok::l_paren))
3211 return true;
3212 } else if (Style.Language == FormatStyle::LK_JavaScript) {
3213 if (Left.is(TT_JsFatArrow))
3214 return true;
3215 // for await ( ...
3216 if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
3217 Left.Previous->is(tok::kw_for))
3218 return true;
3219 if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
3220 Right.MatchingParen) {
3221 const FormatToken *Next = Right.MatchingParen->getNextNonComment();
3222 // An async arrow function, for example: `x = async () => foo();`,
3223 // as opposed to calling a function called async: `x = async();`
3224 if (Next && Next->is(TT_JsFatArrow))
3225 return true;
3226 }
3227 if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
3228 (Right.is(TT_TemplateString) && Right.TokenText.startswith("}")))
3229 return false;
3230 // In tagged template literals ("html`bar baz`"), there is no space between
3231 // the tag identifier and the template string.
3232 if (Keywords.IsJavaScriptIdentifier(Left,
3233 /* AcceptIdentifierName= */ false) &&
3234 Right.is(TT_TemplateString))
3235 return false;
3236 if (Right.is(tok::star) &&
3237 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield))
3238 return false;
3239 if (Right.isOneOf(tok::l_brace, tok::l_square) &&
3240 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
3241 Keywords.kw_extends, Keywords.kw_implements))
3242 return true;
3243 if (Right.is(tok::l_paren)) {
3244 // JS methods can use some keywords as names (e.g. `delete()`).
3245 if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
3246 return false;
3247 // Valid JS method names can include keywords, e.g. `foo.delete()` or
3248 // `bar.instanceof()`. Recognize call positions by preceding period.
3249 if (Left.Previous && Left.Previous->is(tok::period) &&
3250 Left.Tok.getIdentifierInfo())
3251 return false;
3252 // Additional unary JavaScript operators that need a space after.
3253 if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
3254 tok::kw_void))
3255 return true;
3256 }
3257 // `foo as const;` casts into a const type.
3258 if (Left.endsSequence(tok::kw_const, Keywords.kw_as)) {
3259 return false;
3260 }
3261 if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
3262 tok::kw_const) ||
3263 // "of" is only a keyword if it appears after another identifier
3264 // (e.g. as "const x of y" in a for loop), or after a destructuring
3265 // operation (const [x, y] of z, const {a, b} of c).
3266 (Left.is(Keywords.kw_of) && Left.Previous &&
3267 (Left.Previous->Tok.is(tok::identifier) ||
3268 Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
3269 (!Left.Previous || !Left.Previous->is(tok::period)))
3270 return true;
3271 if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
3272 Left.Previous->is(tok::period) && Right.is(tok::l_paren))
3273 return false;
3274 if (Left.is(Keywords.kw_as) &&
3275 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren))
3276 return true;
3277 if (Left.is(tok::kw_default) && Left.Previous &&
3278 Left.Previous->is(tok::kw_export))
3279 return true;
3280 if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
3281 return true;
3282 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
3283 return false;
3284 if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
3285 return false;
3286 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
3287 Line.First->isOneOf(Keywords.kw_import, tok::kw_export))
3288 return false;
3289 if (Left.is(tok::ellipsis))
3290 return false;
3291 if (Left.is(TT_TemplateCloser) &&
3292 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
3293 Keywords.kw_implements, Keywords.kw_extends))
3294 // Type assertions ('<type>expr') are not followed by whitespace. Other
3295 // locations that should have whitespace following are identified by the
3296 // above set of follower tokens.
3297 return false;
3298 if (Right.is(TT_JsNonNullAssertion))
3299 return false;
3300 if (Left.is(TT_JsNonNullAssertion) &&
3301 Right.isOneOf(Keywords.kw_as, Keywords.kw_in))
3302 return true; // "x! as string", "x! in y"
3303 } else if (Style.Language == FormatStyle::LK_Java) {
3304 if (Left.is(tok::r_square) && Right.is(tok::l_brace))
3305 return true;
3306 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
3307 return Style.SpaceBeforeParens != FormatStyle::SBPO_Never;
3308 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
3309 tok::kw_protected) ||
3310 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
3311 Keywords.kw_native)) &&
3312 Right.is(TT_TemplateOpener))
3313 return true;
3314 }
3315 if (Left.is(TT_ImplicitStringLiteral))
3316 return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
3317 if (Line.Type == LT_ObjCMethodDecl) {
3318 if (Left.is(TT_ObjCMethodSpecifier))
3319 return true;
3320 if (Left.is(tok::r_paren) && canBeObjCSelectorComponent(Right))
3321 // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a
3322 // keyword in Objective-C, and '+ (instancetype)new;' is a standard class
3323 // method declaration.
3324 return false;
3325 }
3326 if (Line.Type == LT_ObjCProperty &&
3327 (Right.is(tok::equal) || Left.is(tok::equal)))
3328 return false;
3329
3330 if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
3331 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow))
3332 return true;
3333 if (Right.is(TT_OverloadedOperatorLParen))
3334 return spaceRequiredBeforeParens(Right);
3335 if (Left.is(tok::comma))
3336 return true;
3337 if (Right.is(tok::comma))
3338 return false;
3339 if (Right.is(TT_ObjCBlockLParen))
3340 return true;
3341 if (Right.is(TT_CtorInitializerColon))
3342 return Style.SpaceBeforeCtorInitializerColon;
3343 if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
3344 return false;
3345 if (Right.is(TT_RangeBasedForLoopColon) &&
3346 !Style.SpaceBeforeRangeBasedForLoopColon)
3347 return false;
3348 if (Left.is(TT_BitFieldColon))
3349 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
3350 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
3351 if (Right.is(tok::colon)) {
3352 if (Line.First->isOneOf(tok::kw_default, tok::kw_case))
3353 return Style.SpaceBeforeCaseColon;
3354 if (!Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi))
3355 return false;
3356 if (Right.is(TT_ObjCMethodExpr))
3357 return false;
3358 if (Left.is(tok::question))
3359 return false;
3360 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
3361 return false;
3362 if (Right.is(TT_DictLiteral))
3363 return Style.SpacesInContainerLiterals;
3364 if (Right.is(TT_AttributeColon))
3365 return false;
3366 if (Right.is(TT_CSharpNamedArgumentColon))
3367 return false;
3368 if (Right.is(TT_BitFieldColon))
3369 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
3370 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
3371 return true;
3372 }
3373 if (Left.is(TT_UnaryOperator)) {
3374 if (!Right.is(tok::l_paren)) {
3375 // The alternative operators for ~ and ! are "compl" and "not".
3376 // If they are used instead, we do not want to combine them with
3377 // the token to the right, unless that is a left paren.
3378 if (Left.is(tok::exclaim) && Left.TokenText == "not")
3379 return true;
3380 if (Left.is(tok::tilde) && Left.TokenText == "compl")
3381 return true;
3382 // Lambda captures allow for a lone &, so "&]" needs to be properly
3383 // handled.
3384 if (Left.is(tok::amp) && Right.is(tok::r_square))
3385 return Style.SpacesInSquareBrackets;
3386 }
3387 return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
3388 Right.is(TT_BinaryOperator);
3389 }
3390
3391 // If the next token is a binary operator or a selector name, we have
3392 // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
3393 if (Left.is(TT_CastRParen))
3394 return Style.SpaceAfterCStyleCast ||
3395 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
3396
3397 if (Left.is(tok::greater) && Right.is(tok::greater)) {
3398 if (Style.Language == FormatStyle::LK_TextProto ||
3399 (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral)))
3400 return !Style.Cpp11BracedListStyle;
3401 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
3402 (Style.Standard < FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
3403 }
3404 if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
3405 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
3406 (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod)))
3407 return false;
3408 if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) &&
3409 Right.getPrecedence() == prec::Assignment)
3410 return false;
3411 if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) &&
3412 (Left.is(tok::identifier) || Left.is(tok::kw_this)))
3413 return false;
3414 if (Right.is(tok::coloncolon) && Left.is(tok::identifier))
3415 // Generally don't remove existing spaces between an identifier and "::".
3416 // The identifier might actually be a macro name such as ALWAYS_INLINE. If
3417 // this turns out to be too lenient, add analysis of the identifier itself.
3418 return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd();
3419 if (Right.is(tok::coloncolon) &&
3420 !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren))
3421 // Put a space between < and :: in vector< ::std::string >
3422 return (Left.is(TT_TemplateOpener) &&
3423 (Style.Standard < FormatStyle::LS_Cpp11 || Style.SpacesInAngles)) ||
3424 !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
3425 tok::kw___super, TT_TemplateOpener,
3426 TT_TemplateCloser)) ||
3427 (Left.is(tok::l_paren) && Style.SpacesInParentheses);
3428 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
3429 return Style.SpacesInAngles;
3430 // Space before TT_StructuredBindingLSquare.
3431 if (Right.is(TT_StructuredBindingLSquare))
3432 return !Left.isOneOf(tok::amp, tok::ampamp) ||
3433 Style.PointerAlignment != FormatStyle::PAS_Right;
3434 // Space before & or && following a TT_StructuredBindingLSquare.
3435 if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) &&
3436 Right.isOneOf(tok::amp, tok::ampamp))
3437 return Style.PointerAlignment != FormatStyle::PAS_Left;
3438 if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) ||
3439 (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
3440 !Right.is(tok::r_paren)))
3441 return true;
3442 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_paren) &&
3443 Right.isNot(TT_FunctionTypeLParen))
3444 return spaceRequiredBeforeParens(Right);
3445 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
3446 Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen))
3447 return false;
3448 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
3449 Line.startsWith(tok::hash))
3450 return true;
3451 if (Right.is(TT_TrailingUnaryOperator))
3452 return false;
3453 if (Left.is(TT_RegexLiteral))
3454 return false;
3455 return spaceRequiredBetween(Line, Left, Right);
3456 }
3457
3458 // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
isAllmanBrace(const FormatToken & Tok)3459 static bool isAllmanBrace(const FormatToken &Tok) {
3460 return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
3461 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
3462 }
3463
3464 // Returns 'true' if 'Tok' is an function argument.
IsFunctionArgument(const FormatToken & Tok)3465 static bool IsFunctionArgument(const FormatToken &Tok) {
3466 return Tok.MatchingParen && Tok.MatchingParen->Next &&
3467 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren);
3468 }
3469
3470 static bool
isItAnEmptyLambdaAllowed(const FormatToken & Tok,FormatStyle::ShortLambdaStyle ShortLambdaOption)3471 isItAnEmptyLambdaAllowed(const FormatToken &Tok,
3472 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
3473 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
3474 }
3475
3476 static bool
isItAInlineLambdaAllowed(const FormatToken & Tok,FormatStyle::ShortLambdaStyle ShortLambdaOption)3477 isItAInlineLambdaAllowed(const FormatToken &Tok,
3478 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
3479 return (ShortLambdaOption == FormatStyle::SLS_Inline &&
3480 IsFunctionArgument(Tok)) ||
3481 (ShortLambdaOption == FormatStyle::SLS_All);
3482 }
3483
isOneChildWithoutMustBreakBefore(const FormatToken & Tok)3484 static bool isOneChildWithoutMustBreakBefore(const FormatToken &Tok) {
3485 if (Tok.Children.size() != 1)
3486 return false;
3487 FormatToken *curElt = Tok.Children[0]->First;
3488 while (curElt) {
3489 if (curElt->MustBreakBefore)
3490 return false;
3491 curElt = curElt->Next;
3492 }
3493 return true;
3494 }
isAllmanLambdaBrace(const FormatToken & Tok)3495 static bool isAllmanLambdaBrace(const FormatToken &Tok) {
3496 return (Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
3497 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral));
3498 }
3499
isAllmanBraceIncludedBreakableLambda(const FormatToken & Tok,FormatStyle::ShortLambdaStyle ShortLambdaOption)3500 static bool isAllmanBraceIncludedBreakableLambda(
3501 const FormatToken &Tok, FormatStyle::ShortLambdaStyle ShortLambdaOption) {
3502 if (!isAllmanLambdaBrace(Tok))
3503 return false;
3504
3505 if (isItAnEmptyLambdaAllowed(Tok, ShortLambdaOption))
3506 return false;
3507
3508 return !isItAInlineLambdaAllowed(Tok, ShortLambdaOption) ||
3509 !isOneChildWithoutMustBreakBefore(Tok);
3510 }
3511
mustBreakBefore(const AnnotatedLine & Line,const FormatToken & Right)3512 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
3513 const FormatToken &Right) {
3514 const FormatToken &Left = *Right.Previous;
3515 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
3516 return true;
3517
3518 if (Style.isCSharp()) {
3519 if (Right.is(TT_CSharpNamedArgumentColon) ||
3520 Left.is(TT_CSharpNamedArgumentColon))
3521 return false;
3522 if (Right.is(TT_CSharpGenericTypeConstraint))
3523 return true;
3524 } else if (Style.Language == FormatStyle::LK_JavaScript) {
3525 // FIXME: This might apply to other languages and token kinds.
3526 if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous &&
3527 Left.Previous->is(tok::string_literal))
3528 return true;
3529 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
3530 Left.Previous && Left.Previous->is(tok::equal) &&
3531 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
3532 tok::kw_const) &&
3533 // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
3534 // above.
3535 !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let))
3536 // Object literals on the top level of a file are treated as "enum-style".
3537 // Each key/value pair is put on a separate line, instead of bin-packing.
3538 return true;
3539 if (Left.is(tok::l_brace) && Line.Level == 0 &&
3540 (Line.startsWith(tok::kw_enum) ||
3541 Line.startsWith(tok::kw_const, tok::kw_enum) ||
3542 Line.startsWith(tok::kw_export, tok::kw_enum) ||
3543 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum)))
3544 // JavaScript top-level enum key/value pairs are put on separate lines
3545 // instead of bin-packing.
3546 return true;
3547 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && Left.Previous &&
3548 Left.Previous->is(TT_JsFatArrow)) {
3549 // JS arrow function (=> {...}).
3550 switch (Style.AllowShortLambdasOnASingleLine) {
3551 case FormatStyle::SLS_All:
3552 return false;
3553 case FormatStyle::SLS_None:
3554 return true;
3555 case FormatStyle::SLS_Empty:
3556 return !Left.Children.empty();
3557 case FormatStyle::SLS_Inline:
3558 // allow one-lining inline (e.g. in function call args) and empty arrow
3559 // functions.
3560 return (Left.NestingLevel == 0 && Line.Level == 0) &&
3561 !Left.Children.empty();
3562 }
3563 llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum");
3564 }
3565
3566 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
3567 !Left.Children.empty())
3568 // Support AllowShortFunctionsOnASingleLine for JavaScript.
3569 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
3570 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
3571 (Left.NestingLevel == 0 && Line.Level == 0 &&
3572 Style.AllowShortFunctionsOnASingleLine &
3573 FormatStyle::SFS_InlineOnly);
3574 } else if (Style.Language == FormatStyle::LK_Java) {
3575 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
3576 Right.Next->is(tok::string_literal))
3577 return true;
3578 } else if (Style.Language == FormatStyle::LK_Cpp ||
3579 Style.Language == FormatStyle::LK_ObjC ||
3580 Style.Language == FormatStyle::LK_Proto ||
3581 Style.Language == FormatStyle::LK_TableGen ||
3582 Style.Language == FormatStyle::LK_TextProto) {
3583 if (Left.isStringLiteral() && Right.isStringLiteral())
3584 return true;
3585 }
3586
3587 // If the last token before a '}', ']', or ')' is a comma or a trailing
3588 // comment, the intention is to insert a line break after it in order to make
3589 // shuffling around entries easier. Import statements, especially in
3590 // JavaScript, can be an exception to this rule.
3591 if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) {
3592 const FormatToken *BeforeClosingBrace = nullptr;
3593 if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
3594 (Style.Language == FormatStyle::LK_JavaScript &&
3595 Left.is(tok::l_paren))) &&
3596 Left.isNot(BK_Block) && Left.MatchingParen)
3597 BeforeClosingBrace = Left.MatchingParen->Previous;
3598 else if (Right.MatchingParen &&
3599 (Right.MatchingParen->isOneOf(tok::l_brace,
3600 TT_ArrayInitializerLSquare) ||
3601 (Style.Language == FormatStyle::LK_JavaScript &&
3602 Right.MatchingParen->is(tok::l_paren))))
3603 BeforeClosingBrace = &Left;
3604 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
3605 BeforeClosingBrace->isTrailingComment()))
3606 return true;
3607 }
3608
3609 if (Right.is(tok::comment))
3610 return Left.isNot(BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
3611 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
3612 if (Left.isTrailingComment())
3613 return true;
3614 if (Right.Previous->IsUnterminatedLiteral)
3615 return true;
3616 if (Right.is(tok::lessless) && Right.Next &&
3617 Right.Previous->is(tok::string_literal) &&
3618 Right.Next->is(tok::string_literal))
3619 return true;
3620 // Can break after template<> declaration
3621 if (Right.Previous->ClosesTemplateDeclaration &&
3622 Right.Previous->MatchingParen &&
3623 Right.Previous->MatchingParen->NestingLevel == 0) {
3624 // Put concepts on the next line e.g.
3625 // template<typename T>
3626 // concept ...
3627 if (Right.is(tok::kw_concept))
3628 return Style.BreakBeforeConceptDeclarations;
3629 return (Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes);
3630 }
3631 if (Right.is(TT_CtorInitializerComma) &&
3632 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
3633 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
3634 return true;
3635 if (Right.is(TT_CtorInitializerColon) &&
3636 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
3637 !Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
3638 return true;
3639 // Break only if we have multiple inheritance.
3640 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
3641 Right.is(TT_InheritanceComma))
3642 return true;
3643 if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\""))
3644 // Multiline raw string literals are special wrt. line breaks. The author
3645 // has made a deliberate choice and might have aligned the contents of the
3646 // string literal accordingly. Thus, we try keep existing line breaks.
3647 return Right.IsMultiline && Right.NewlinesBefore > 0;
3648 if ((Right.Previous->is(tok::l_brace) ||
3649 (Right.Previous->is(tok::less) && Right.Previous->Previous &&
3650 Right.Previous->Previous->is(tok::equal))) &&
3651 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
3652 // Don't put enums or option definitions onto single lines in protocol
3653 // buffers.
3654 return true;
3655 }
3656 if (Right.is(TT_InlineASMBrace))
3657 return Right.HasUnescapedNewline;
3658
3659 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
3660 if (Style.BraceWrapping.BeforeLambdaBody &&
3661 (isAllmanBraceIncludedBreakableLambda(Left, ShortLambdaOption) ||
3662 isAllmanBraceIncludedBreakableLambda(Right, ShortLambdaOption))) {
3663 return true;
3664 }
3665
3666 if (isAllmanBrace(Left) || isAllmanBrace(Right))
3667 return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
3668 (Line.startsWith(tok::kw_typedef, tok::kw_enum) &&
3669 Style.BraceWrapping.AfterEnum) ||
3670 (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
3671 (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
3672 if (Left.is(TT_ObjCBlockLBrace) &&
3673 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
3674 return true;
3675
3676 if (Left.is(TT_LambdaLBrace)) {
3677 if (IsFunctionArgument(Left) &&
3678 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline)
3679 return false;
3680
3681 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
3682 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
3683 (!Left.Children.empty() &&
3684 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty))
3685 return true;
3686 }
3687
3688 // Put multiple Java annotation on a new line.
3689 if ((Style.Language == FormatStyle::LK_Java ||
3690 Style.Language == FormatStyle::LK_JavaScript) &&
3691 Left.is(TT_LeadingJavaAnnotation) &&
3692 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
3693 (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations))
3694 return true;
3695
3696 if (Right.is(TT_ProtoExtensionLSquare))
3697 return true;
3698
3699 // In text proto instances if a submessage contains at least 2 entries and at
3700 // least one of them is a submessage, like A { ... B { ... } ... },
3701 // put all of the entries of A on separate lines by forcing the selector of
3702 // the submessage B to be put on a newline.
3703 //
3704 // Example: these can stay on one line:
3705 // a { scalar_1: 1 scalar_2: 2 }
3706 // a { b { key: value } }
3707 //
3708 // and these entries need to be on a new line even if putting them all in one
3709 // line is under the column limit:
3710 // a {
3711 // scalar: 1
3712 // b { key: value }
3713 // }
3714 //
3715 // We enforce this by breaking before a submessage field that has previous
3716 // siblings, *and* breaking before a field that follows a submessage field.
3717 //
3718 // Be careful to exclude the case [proto.ext] { ... } since the `]` is
3719 // the TT_SelectorName there, but we don't want to break inside the brackets.
3720 //
3721 // Another edge case is @submessage { key: value }, which is a common
3722 // substitution placeholder. In this case we want to keep `@` and `submessage`
3723 // together.
3724 //
3725 // We ensure elsewhere that extensions are always on their own line.
3726 if ((Style.Language == FormatStyle::LK_Proto ||
3727 Style.Language == FormatStyle::LK_TextProto) &&
3728 Right.is(TT_SelectorName) && !Right.is(tok::r_square) && Right.Next) {
3729 // Keep `@submessage` together in:
3730 // @submessage { key: value }
3731 if (Right.Previous && Right.Previous->is(tok::at))
3732 return false;
3733 // Look for the scope opener after selector in cases like:
3734 // selector { ...
3735 // selector: { ...
3736 // selector: @base { ...
3737 FormatToken *LBrace = Right.Next;
3738 if (LBrace && LBrace->is(tok::colon)) {
3739 LBrace = LBrace->Next;
3740 if (LBrace && LBrace->is(tok::at)) {
3741 LBrace = LBrace->Next;
3742 if (LBrace)
3743 LBrace = LBrace->Next;
3744 }
3745 }
3746 if (LBrace &&
3747 // The scope opener is one of {, [, <:
3748 // selector { ... }
3749 // selector [ ... ]
3750 // selector < ... >
3751 //
3752 // In case of selector { ... }, the l_brace is TT_DictLiteral.
3753 // In case of an empty selector {}, the l_brace is not TT_DictLiteral,
3754 // so we check for immediately following r_brace.
3755 ((LBrace->is(tok::l_brace) &&
3756 (LBrace->is(TT_DictLiteral) ||
3757 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
3758 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
3759 // If Left.ParameterCount is 0, then this submessage entry is not the
3760 // first in its parent submessage, and we want to break before this entry.
3761 // If Left.ParameterCount is greater than 0, then its parent submessage
3762 // might contain 1 or more entries and we want to break before this entry
3763 // if it contains at least 2 entries. We deal with this case later by
3764 // detecting and breaking before the next entry in the parent submessage.
3765 if (Left.ParameterCount == 0)
3766 return true;
3767 // However, if this submessage is the first entry in its parent
3768 // submessage, Left.ParameterCount might be 1 in some cases.
3769 // We deal with this case later by detecting an entry
3770 // following a closing paren of this submessage.
3771 }
3772
3773 // If this is an entry immediately following a submessage, it will be
3774 // preceded by a closing paren of that submessage, like in:
3775 // left---. .---right
3776 // v v
3777 // sub: { ... } key: value
3778 // If there was a comment between `}` an `key` above, then `key` would be
3779 // put on a new line anyways.
3780 if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
3781 return true;
3782 }
3783
3784 // Deal with lambda arguments in C++ - we want consistent line breaks whether
3785 // they happen to be at arg0, arg1 or argN. The selection is a bit nuanced
3786 // as aggressive line breaks are placed when the lambda is not the last arg.
3787 if ((Style.Language == FormatStyle::LK_Cpp ||
3788 Style.Language == FormatStyle::LK_ObjC) &&
3789 Left.is(tok::l_paren) && Left.BlockParameterCount > 0 &&
3790 !Right.isOneOf(tok::l_paren, TT_LambdaLSquare)) {
3791 // Multiple lambdas in the same function call force line breaks.
3792 if (Left.BlockParameterCount > 1)
3793 return true;
3794
3795 // A lambda followed by another arg forces a line break.
3796 if (!Left.Role)
3797 return false;
3798 auto Comma = Left.Role->lastComma();
3799 if (!Comma)
3800 return false;
3801 auto Next = Comma->getNextNonComment();
3802 if (!Next)
3803 return false;
3804 if (!Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret))
3805 return true;
3806 }
3807
3808 return false;
3809 }
3810
canBreakBefore(const AnnotatedLine & Line,const FormatToken & Right)3811 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
3812 const FormatToken &Right) {
3813 const FormatToken &Left = *Right.Previous;
3814 // Language-specific stuff.
3815 if (Style.isCSharp()) {
3816 if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
3817 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon))
3818 return false;
3819 // Only break after commas for generic type constraints.
3820 if (Line.First->is(TT_CSharpGenericTypeConstraint))
3821 return Left.is(TT_CSharpGenericTypeConstraintComma);
3822 } else if (Style.Language == FormatStyle::LK_Java) {
3823 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
3824 Keywords.kw_implements))
3825 return false;
3826 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
3827 Keywords.kw_implements))
3828 return true;
3829 } else if (Style.Language == FormatStyle::LK_JavaScript) {
3830 const FormatToken *NonComment = Right.getPreviousNonComment();
3831 if (NonComment &&
3832 NonComment->isOneOf(
3833 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
3834 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
3835 tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
3836 Keywords.kw_readonly, Keywords.kw_abstract, Keywords.kw_get,
3837 Keywords.kw_set, Keywords.kw_async, Keywords.kw_await))
3838 return false; // Otherwise automatic semicolon insertion would trigger.
3839 if (Right.NestingLevel == 0 &&
3840 (Left.Tok.getIdentifierInfo() ||
3841 Left.isOneOf(tok::r_square, tok::r_paren)) &&
3842 Right.isOneOf(tok::l_square, tok::l_paren))
3843 return false; // Otherwise automatic semicolon insertion would trigger.
3844 if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace))
3845 return false;
3846 if (Left.is(TT_JsTypeColon))
3847 return true;
3848 // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
3849 if (Left.is(tok::exclaim) && Right.is(tok::colon))
3850 return false;
3851 // Look for is type annotations like:
3852 // function f(): a is B { ... }
3853 // Do not break before is in these cases.
3854 if (Right.is(Keywords.kw_is)) {
3855 const FormatToken *Next = Right.getNextNonComment();
3856 // If `is` is followed by a colon, it's likely that it's a dict key, so
3857 // ignore it for this check.
3858 // For example this is common in Polymer:
3859 // Polymer({
3860 // is: 'name',
3861 // ...
3862 // });
3863 if (!Next || !Next->is(tok::colon))
3864 return false;
3865 }
3866 if (Left.is(Keywords.kw_in))
3867 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
3868 if (Right.is(Keywords.kw_in))
3869 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
3870 if (Right.is(Keywords.kw_as))
3871 return false; // must not break before as in 'x as type' casts
3872 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
3873 // extends and infer can appear as keywords in conditional types:
3874 // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
3875 // do not break before them, as the expressions are subject to ASI.
3876 return false;
3877 }
3878 if (Left.is(Keywords.kw_as))
3879 return true;
3880 if (Left.is(TT_JsNonNullAssertion))
3881 return true;
3882 if (Left.is(Keywords.kw_declare) &&
3883 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
3884 Keywords.kw_function, tok::kw_class, tok::kw_enum,
3885 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
3886 Keywords.kw_let, tok::kw_const))
3887 // See grammar for 'declare' statements at:
3888 // https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#A.10
3889 return false;
3890 if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
3891 Right.isOneOf(tok::identifier, tok::string_literal))
3892 return false; // must not break in "module foo { ...}"
3893 if (Right.is(TT_TemplateString) && Right.closesScope())
3894 return false;
3895 // Don't split tagged template literal so there is a break between the tag
3896 // identifier and template string.
3897 if (Left.is(tok::identifier) && Right.is(TT_TemplateString)) {
3898 return false;
3899 }
3900 if (Left.is(TT_TemplateString) && Left.opensScope())
3901 return true;
3902 }
3903
3904 if (Left.is(tok::at))
3905 return false;
3906 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
3907 return false;
3908 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
3909 return !Right.is(tok::l_paren);
3910 if (Right.is(TT_PointerOrReference))
3911 return Line.IsMultiVariableDeclStmt ||
3912 (Style.PointerAlignment == FormatStyle::PAS_Right &&
3913 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
3914 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
3915 Right.is(tok::kw_operator))
3916 return true;
3917 if (Left.is(TT_PointerOrReference))
3918 return false;
3919 if (Right.isTrailingComment())
3920 // We rely on MustBreakBefore being set correctly here as we should not
3921 // change the "binding" behavior of a comment.
3922 // The first comment in a braced lists is always interpreted as belonging to
3923 // the first list element. Otherwise, it should be placed outside of the
3924 // list.
3925 return Left.is(BK_BracedInit) ||
3926 (Left.is(TT_CtorInitializerColon) &&
3927 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
3928 if (Left.is(tok::question) && Right.is(tok::colon))
3929 return false;
3930 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
3931 return Style.BreakBeforeTernaryOperators;
3932 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
3933 return !Style.BreakBeforeTernaryOperators;
3934 if (Left.is(TT_InheritanceColon))
3935 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
3936 if (Right.is(TT_InheritanceColon))
3937 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
3938 if (Right.is(TT_ObjCMethodExpr) && !Right.is(tok::r_square) &&
3939 Left.isNot(TT_SelectorName))
3940 return true;
3941
3942 if (Right.is(tok::colon) &&
3943 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon))
3944 return false;
3945 if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
3946 if (Style.Language == FormatStyle::LK_Proto ||
3947 Style.Language == FormatStyle::LK_TextProto) {
3948 if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
3949 return false;
3950 // Prevent cases like:
3951 //
3952 // submessage:
3953 // { key: valueeeeeeeeeeee }
3954 //
3955 // when the snippet does not fit into one line.
3956 // Prefer:
3957 //
3958 // submessage: {
3959 // key: valueeeeeeeeeeee
3960 // }
3961 //
3962 // instead, even if it is longer by one line.
3963 //
3964 // Note that this allows allows the "{" to go over the column limit
3965 // when the column limit is just between ":" and "{", but that does
3966 // not happen too often and alternative formattings in this case are
3967 // not much better.
3968 //
3969 // The code covers the cases:
3970 //
3971 // submessage: { ... }
3972 // submessage: < ... >
3973 // repeated: [ ... ]
3974 if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
3975 Right.is(TT_DictLiteral)) ||
3976 Right.is(TT_ArrayInitializerLSquare))
3977 return false;
3978 }
3979 return true;
3980 }
3981 if (Right.is(tok::r_square) && Right.MatchingParen &&
3982 Right.MatchingParen->is(TT_ProtoExtensionLSquare))
3983 return false;
3984 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
3985 Right.Next->is(TT_ObjCMethodExpr)))
3986 return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
3987 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
3988 return true;
3989 if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen))
3990 return true;
3991 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
3992 TT_OverloadedOperator))
3993 return false;
3994 if (Left.is(TT_RangeBasedForLoopColon))
3995 return true;
3996 if (Right.is(TT_RangeBasedForLoopColon))
3997 return false;
3998 if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
3999 return true;
4000 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
4001 Left.is(tok::kw_operator))
4002 return false;
4003 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
4004 Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0)
4005 return false;
4006 if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
4007 !Style.Cpp11BracedListStyle)
4008 return false;
4009 if (Left.is(tok::l_paren) &&
4010 Left.isOneOf(TT_AttributeParen, TT_TypeDeclarationParen))
4011 return false;
4012 if (Left.is(tok::l_paren) && Left.Previous &&
4013 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen)))
4014 return false;
4015 if (Right.is(TT_ImplicitStringLiteral))
4016 return false;
4017
4018 if (Right.is(tok::r_paren) || Right.is(TT_TemplateCloser))
4019 return false;
4020 if (Right.is(tok::r_square) && Right.MatchingParen &&
4021 Right.MatchingParen->is(TT_LambdaLSquare))
4022 return false;
4023
4024 // We only break before r_brace if there was a corresponding break before
4025 // the l_brace, which is tracked by BreakBeforeClosingBrace.
4026 if (Right.is(tok::r_brace))
4027 return Right.MatchingParen && Right.MatchingParen->is(BK_Block);
4028
4029 // Allow breaking after a trailing annotation, e.g. after a method
4030 // declaration.
4031 if (Left.is(TT_TrailingAnnotation))
4032 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
4033 tok::less, tok::coloncolon);
4034
4035 if (Right.is(tok::kw___attribute) ||
4036 (Right.is(tok::l_square) && Right.is(TT_AttributeSquare)))
4037 return !Left.is(TT_AttributeSquare);
4038
4039 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
4040 return true;
4041
4042 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
4043 return true;
4044
4045 if (Left.is(TT_CtorInitializerColon))
4046 return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon;
4047 if (Right.is(TT_CtorInitializerColon))
4048 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
4049 if (Left.is(TT_CtorInitializerComma) &&
4050 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
4051 return false;
4052 if (Right.is(TT_CtorInitializerComma) &&
4053 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
4054 return true;
4055 if (Left.is(TT_InheritanceComma) &&
4056 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma)
4057 return false;
4058 if (Right.is(TT_InheritanceComma) &&
4059 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma)
4060 return true;
4061 if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
4062 (Left.is(tok::less) && Right.is(tok::less)))
4063 return false;
4064 if (Right.is(TT_BinaryOperator) &&
4065 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
4066 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
4067 Right.getPrecedence() != prec::Assignment))
4068 return true;
4069 if (Left.is(TT_ArrayInitializerLSquare))
4070 return true;
4071 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
4072 return true;
4073 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
4074 !Left.isOneOf(tok::arrowstar, tok::lessless) &&
4075 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
4076 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
4077 Left.getPrecedence() == prec::Assignment))
4078 return true;
4079 if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) ||
4080 (Left.is(tok::r_square) && Right.is(TT_AttributeSquare)))
4081 return false;
4082
4083 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
4084 if (Style.BraceWrapping.BeforeLambdaBody) {
4085 if (isAllmanLambdaBrace(Left))
4086 return !isItAnEmptyLambdaAllowed(Left, ShortLambdaOption);
4087 if (isAllmanLambdaBrace(Right))
4088 return !isItAnEmptyLambdaAllowed(Right, ShortLambdaOption);
4089 }
4090
4091 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
4092 tok::kw_class, tok::kw_struct, tok::comment) ||
4093 Right.isMemberAccess() ||
4094 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
4095 tok::colon, tok::l_square, tok::at) ||
4096 (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) ||
4097 (Left.is(tok::r_paren) &&
4098 Right.isOneOf(tok::identifier, tok::kw_const)) ||
4099 (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
4100 (Left.is(TT_TemplateOpener) && !Right.is(TT_TemplateCloser));
4101 }
4102
printDebugInfo(const AnnotatedLine & Line)4103 void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
4104 llvm::errs() << "AnnotatedTokens(L=" << Line.Level << "):\n";
4105 const FormatToken *Tok = Line.First;
4106 while (Tok) {
4107 llvm::errs() << " M=" << Tok->MustBreakBefore
4108 << " C=" << Tok->CanBreakBefore
4109 << " T=" << getTokenTypeName(Tok->getType())
4110 << " S=" << Tok->SpacesRequiredBefore
4111 << " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount
4112 << " BK=" << Tok->getBlockKind() << " P=" << Tok->SplitPenalty
4113 << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength
4114 << " PPK=" << Tok->getPackingKind() << " FakeLParens=";
4115 for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
4116 llvm::errs() << Tok->FakeLParens[i] << "/";
4117 llvm::errs() << " FakeRParens=" << Tok->FakeRParens;
4118 llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo();
4119 llvm::errs() << " Text='" << Tok->TokenText << "'\n";
4120 if (!Tok->Next)
4121 assert(Tok == Line.Last);
4122 Tok = Tok->Next;
4123 }
4124 llvm::errs() << "----\n";
4125 }
4126
4127 } // namespace format
4128 } // namespace clang
4129