clang 18.0.0git
TokenAnnotator.cpp
Go to the documentation of this file.
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"
19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/Support/Debug.h"
21
22#define DEBUG_TYPE "format-token-annotator"
23
24namespace clang {
25namespace format {
26
27namespace {
28
29/// Returns \c true if the line starts with a token that can start a statement
30/// with an initializer.
31static bool startsWithInitStatement(const AnnotatedLine &Line) {
32 return Line.startsWith(tok::kw_for) || Line.startsWith(tok::kw_if) ||
33 Line.startsWith(tok::kw_switch);
34}
35
36/// Returns \c true if the token can be used as an identifier in
37/// an Objective-C \c \@selector, \c false otherwise.
38///
39/// Because getFormattingLangOpts() always lexes source code as
40/// Objective-C++, C++ keywords like \c new and \c delete are
41/// lexed as tok::kw_*, not tok::identifier, even for Objective-C.
42///
43/// For Objective-C and Objective-C++, both identifiers and keywords
44/// are valid inside @selector(...) (or a macro which
45/// invokes @selector(...)). So, we allow treat any identifier or
46/// keyword as a potential Objective-C selector component.
47static bool canBeObjCSelectorComponent(const FormatToken &Tok) {
48 return Tok.Tok.getIdentifierInfo();
49}
50
51/// With `Left` being '(', check if we're at either `[...](` or
52/// `[...]<...>(`, where the [ opens a lambda capture list.
53static bool isLambdaParameterList(const FormatToken *Left) {
54 // Skip <...> if present.
55 if (Left->Previous && Left->Previous->is(tok::greater) &&
56 Left->Previous->MatchingParen &&
57 Left->Previous->MatchingParen->is(TT_TemplateOpener)) {
58 Left = Left->Previous->MatchingParen;
59 }
60
61 // Check for `[...]`.
62 return Left->Previous && Left->Previous->is(tok::r_square) &&
63 Left->Previous->MatchingParen &&
64 Left->Previous->MatchingParen->is(TT_LambdaLSquare);
65}
66
67/// Returns \c true if the token is followed by a boolean condition, \c false
68/// otherwise.
69static bool isKeywordWithCondition(const FormatToken &Tok) {
70 return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
71 tok::kw_constexpr, tok::kw_catch);
72}
73
74/// Returns \c true if the token starts a C++ attribute, \c false otherwise.
75static bool isCppAttribute(bool IsCpp, const FormatToken &Tok) {
76 if (!IsCpp || !Tok.startsSequence(tok::l_square, tok::l_square))
77 return false;
78 // The first square bracket is part of an ObjC array literal
79 if (Tok.Previous && Tok.Previous->is(tok::at))
80 return false;
81 const FormatToken *AttrTok = Tok.Next->Next;
82 if (!AttrTok)
83 return false;
84 // C++17 '[[using ns: foo, bar(baz, blech)]]'
85 // We assume nobody will name an ObjC variable 'using'.
86 if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
87 return true;
88 if (AttrTok->isNot(tok::identifier))
89 return false;
90 while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
91 // ObjC message send. We assume nobody will use : in a C++11 attribute
92 // specifier parameter, although this is technically valid:
93 // [[foo(:)]].
94 if (AttrTok->is(tok::colon) ||
95 AttrTok->startsSequence(tok::identifier, tok::identifier) ||
96 AttrTok->startsSequence(tok::r_paren, tok::identifier)) {
97 return false;
98 }
99 if (AttrTok->is(tok::ellipsis))
100 return true;
101 AttrTok = AttrTok->Next;
102 }
103 return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
104}
105
106/// A parser that gathers additional information about tokens.
107///
108/// The \c TokenAnnotator tries to match parenthesis and square brakets and
109/// store a parenthesis levels. It also tries to resolve matching "<" and ">"
110/// into template parameter lists.
111class AnnotatingParser {
112public:
113 AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
114 const AdditionalKeywords &Keywords,
115 SmallVector<ScopeType> &Scopes)
116 : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
117 Keywords(Keywords), Scopes(Scopes) {
118 Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
119 resetTokenMetadata();
120 }
121
122private:
123 ScopeType getScopeType(const FormatToken &Token) const {
124 switch (Token.getType()) {
125 case TT_FunctionLBrace:
126 case TT_LambdaLBrace:
127 return ST_Function;
128 case TT_ClassLBrace:
129 case TT_StructLBrace:
130 case TT_UnionLBrace:
131 return ST_Class;
132 default:
133 return ST_Other;
134 }
135 }
136
137 bool parseAngle() {
138 if (!CurrentToken || !CurrentToken->Previous)
139 return false;
140 if (NonTemplateLess.count(CurrentToken->Previous) > 0)
141 return false;
142
143 const FormatToken &Previous = *CurrentToken->Previous; // The '<'.
144 if (Previous.Previous) {
145 if (Previous.Previous->Tok.isLiteral())
146 return false;
147 if (Previous.Previous->is(tok::r_brace))
148 return false;
149 if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
150 (!Previous.Previous->MatchingParen ||
151 Previous.Previous->MatchingParen->isNot(
152 TT_OverloadedOperatorLParen))) {
153 return false;
154 }
155 }
156
157 FormatToken *Left = CurrentToken->Previous;
158 Left->ParentBracket = Contexts.back().ContextKind;
159 ScopedContextCreator ContextCreator(*this, tok::less, 12);
160
161 // If this angle is in the context of an expression, we need to be more
162 // hesitant to detect it as opening template parameters.
163 bool InExprContext = Contexts.back().IsExpression;
164
165 Contexts.back().IsExpression = false;
166 // If there's a template keyword before the opening angle bracket, this is a
167 // template parameter, not an argument.
168 if (Left->Previous && Left->Previous->isNot(tok::kw_template))
169 Contexts.back().ContextType = Context::TemplateArgument;
170
171 if (Style.Language == FormatStyle::LK_Java &&
172 CurrentToken->is(tok::question)) {
173 next();
174 }
175
176 while (CurrentToken) {
177 if (CurrentToken->is(tok::greater)) {
178 // Try to do a better job at looking for ">>" within the condition of
179 // a statement. Conservatively insert spaces between consecutive ">"
180 // tokens to prevent splitting right bitshift operators and potentially
181 // altering program semantics. This check is overly conservative and
182 // will prevent spaces from being inserted in select nested template
183 // parameter cases, but should not alter program semantics.
184 if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) &&
185 Left->ParentBracket != tok::less &&
186 CurrentToken->getStartOfNonWhitespace() ==
187 CurrentToken->Next->getStartOfNonWhitespace().getLocWithOffset(
188 -1)) {
189 return false;
190 }
191 Left->MatchingParen = CurrentToken;
192 CurrentToken->MatchingParen = Left;
193 // In TT_Proto, we must distignuish between:
194 // map<key, value>
195 // msg < item: data >
196 // msg: < item: data >
197 // In TT_TextProto, map<key, value> does not occur.
198 if (Style.Language == FormatStyle::LK_TextProto ||
199 (Style.Language == FormatStyle::LK_Proto && Left->Previous &&
200 Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
201 CurrentToken->setType(TT_DictLiteral);
202 } else {
203 CurrentToken->setType(TT_TemplateCloser);
204 CurrentToken->Tok.setLength(1);
205 }
206 if (CurrentToken->Next && CurrentToken->Next->Tok.isLiteral())
207 return false;
208 next();
209 return true;
210 }
211 if (CurrentToken->is(tok::question) &&
212 Style.Language == FormatStyle::LK_Java) {
213 next();
214 continue;
215 }
216 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
217 (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
218 !Style.isCSharp() && Style.Language != FormatStyle::LK_Proto &&
219 Style.Language != FormatStyle::LK_TextProto)) {
220 return false;
221 }
222 // If a && or || is found and interpreted as a binary operator, this set
223 // of angles is likely part of something like "a < b && c > d". If the
224 // angles are inside an expression, the ||/&& might also be a binary
225 // operator that was misinterpreted because we are parsing template
226 // parameters.
227 // FIXME: This is getting out of hand, write a decent parser.
228 if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
229 CurrentToken->Previous->is(TT_BinaryOperator) &&
230 Contexts[Contexts.size() - 2].IsExpression &&
231 !Line.startsWith(tok::kw_template)) {
232 return false;
233 }
234 updateParameterCount(Left, CurrentToken);
235 if (Style.Language == FormatStyle::LK_Proto) {
236 if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) {
237 if (CurrentToken->is(tok::colon) ||
238 (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
239 Previous->isNot(tok::colon))) {
240 Previous->setType(TT_SelectorName);
241 }
242 }
243 }
244 if (!consumeToken())
245 return false;
246 }
247 return false;
248 }
249
250 bool parseUntouchableParens() {
251 while (CurrentToken) {
252 CurrentToken->Finalized = true;
253 switch (CurrentToken->Tok.getKind()) {
254 case tok::l_paren:
255 next();
256 if (!parseUntouchableParens())
257 return false;
258 continue;
259 case tok::r_paren:
260 next();
261 return true;
262 default:
263 // no-op
264 break;
265 }
266 next();
267 }
268 return false;
269 }
270
271 bool parseParens(bool LookForDecls = false) {
272 if (!CurrentToken)
273 return false;
274 assert(CurrentToken->Previous && "Unknown previous token");
275 FormatToken &OpeningParen = *CurrentToken->Previous;
276 assert(OpeningParen.is(tok::l_paren));
277 FormatToken *PrevNonComment = OpeningParen.getPreviousNonComment();
278 OpeningParen.ParentBracket = Contexts.back().ContextKind;
279 ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
280
281 // FIXME: This is a bit of a hack. Do better.
282 Contexts.back().ColonIsForRangeExpr =
283 Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
284
285 if (OpeningParen.Previous &&
286 OpeningParen.Previous->is(TT_UntouchableMacroFunc)) {
287 OpeningParen.Finalized = true;
288 return parseUntouchableParens();
289 }
290
291 bool StartsObjCMethodExpr = false;
292 if (!Style.isVerilog()) {
293 if (FormatToken *MaybeSel = OpeningParen.Previous) {
294 // @selector( starts a selector.
295 if (MaybeSel->isObjCAtKeyword(tok::objc_selector) &&
296 MaybeSel->Previous && MaybeSel->Previous->is(tok::at)) {
297 StartsObjCMethodExpr = true;
298 }
299 }
300 }
301
302 if (OpeningParen.is(TT_OverloadedOperatorLParen)) {
303 // Find the previous kw_operator token.
304 FormatToken *Prev = &OpeningParen;
305 while (Prev->isNot(tok::kw_operator)) {
306 Prev = Prev->Previous;
307 assert(Prev && "Expect a kw_operator prior to the OperatorLParen!");
308 }
309
310 // If faced with "a.operator*(argument)" or "a->operator*(argument)",
311 // i.e. the operator is called as a member function,
312 // then the argument must be an expression.
313 bool OperatorCalledAsMemberFunction =
314 Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
315 Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
316 } else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
317 Contexts.back().IsExpression = true;
318 Contexts.back().ContextType = Context::VerilogInstancePortList;
319 } else if (Style.isJavaScript() &&
320 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
321 Line.startsWith(tok::kw_export, Keywords.kw_type,
322 tok::identifier))) {
323 // type X = (...);
324 // export type X = (...);
325 Contexts.back().IsExpression = false;
326 } else if (OpeningParen.Previous &&
327 (OpeningParen.Previous->isOneOf(
328 tok::kw_static_assert, tok::kw_noexcept, tok::kw_explicit,
329 tok::kw_while, tok::l_paren, tok::comma,
330 TT_BinaryOperator) ||
331 OpeningParen.Previous->isIf())) {
332 // static_assert, if and while usually contain expressions.
333 Contexts.back().IsExpression = true;
334 } else if (Style.isJavaScript() && OpeningParen.Previous &&
335 (OpeningParen.Previous->is(Keywords.kw_function) ||
336 (OpeningParen.Previous->endsSequence(tok::identifier,
337 Keywords.kw_function)))) {
338 // function(...) or function f(...)
339 Contexts.back().IsExpression = false;
340 } else if (Style.isJavaScript() && OpeningParen.Previous &&
341 OpeningParen.Previous->is(TT_JsTypeColon)) {
342 // let x: (SomeType);
343 Contexts.back().IsExpression = false;
344 } else if (isLambdaParameterList(&OpeningParen)) {
345 // This is a parameter list of a lambda expression.
346 Contexts.back().IsExpression = false;
347 } else if (OpeningParen.is(TT_RequiresExpressionLParen)) {
348 Contexts.back().IsExpression = false;
349 } else if (OpeningParen.Previous &&
350 OpeningParen.Previous->is(tok::kw__Generic)) {
351 Contexts.back().ContextType = Context::C11GenericSelection;
352 Contexts.back().IsExpression = true;
353 } else if (Line.InPPDirective &&
354 (!OpeningParen.Previous ||
355 OpeningParen.Previous->isNot(tok::identifier))) {
356 Contexts.back().IsExpression = true;
357 } else if (Contexts[Contexts.size() - 2].CaretFound) {
358 // This is the parameter list of an ObjC block.
359 Contexts.back().IsExpression = false;
360 } else if (OpeningParen.Previous &&
361 OpeningParen.Previous->is(TT_ForEachMacro)) {
362 // The first argument to a foreach macro is a declaration.
363 Contexts.back().ContextType = Context::ForEachMacro;
364 Contexts.back().IsExpression = false;
365 } else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen &&
366 OpeningParen.Previous->MatchingParen->isOneOf(
367 TT_ObjCBlockLParen, TT_FunctionTypeLParen)) {
368 Contexts.back().IsExpression = false;
369 } else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
370 bool IsForOrCatch =
371 OpeningParen.Previous &&
372 OpeningParen.Previous->isOneOf(tok::kw_for, tok::kw_catch);
373 Contexts.back().IsExpression = !IsForOrCatch;
374 }
375
376 // Infer the role of the l_paren based on the previous token if we haven't
377 // detected one yet.
378 if (PrevNonComment && OpeningParen.is(TT_Unknown)) {
379 if (PrevNonComment->isAttribute()) {
380 OpeningParen.setType(TT_AttributeLParen);
381 } else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
382 tok::kw_typeof,
383#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) tok::kw___##Trait,
384#include "clang/Basic/TransformTypeTraits.def"
385 tok::kw__Atomic)) {
386 OpeningParen.setType(TT_TypeDeclarationParen);
387 // decltype() and typeof() usually contain expressions.
388 if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
389 Contexts.back().IsExpression = true;
390 }
391 }
392
393 if (StartsObjCMethodExpr) {
394 Contexts.back().ColonIsObjCMethodExpr = true;
395 OpeningParen.setType(TT_ObjCMethodExpr);
396 }
397
398 // MightBeFunctionType and ProbablyFunctionType are used for
399 // function pointer and reference types as well as Objective-C
400 // block types:
401 //
402 // void (*FunctionPointer)(void);
403 // void (&FunctionReference)(void);
404 // void (&&FunctionReference)(void);
405 // void (^ObjCBlock)(void);
406 bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
407 bool ProbablyFunctionType =
408 CurrentToken->isOneOf(tok::star, tok::amp, tok::ampamp, tok::caret);
409 bool HasMultipleLines = false;
410 bool HasMultipleParametersOnALine = false;
411 bool MightBeObjCForRangeLoop =
412 OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
413 FormatToken *PossibleObjCForInToken = nullptr;
414 while (CurrentToken) {
415 // LookForDecls is set when "if (" has been seen. Check for
416 // 'identifier' '*' 'identifier' followed by not '=' -- this
417 // '*' has to be a binary operator but determineStarAmpUsage() will
418 // categorize it as an unary operator, so set the right type here.
419 if (LookForDecls && CurrentToken->Next) {
420 FormatToken *Prev = CurrentToken->getPreviousNonComment();
421 if (Prev) {
422 FormatToken *PrevPrev = Prev->getPreviousNonComment();
423 FormatToken *Next = CurrentToken->Next;
424 if (PrevPrev && PrevPrev->is(tok::identifier) &&
425 PrevPrev->isNot(TT_TypeName) &&
426 Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
427 CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
428 Prev->setType(TT_BinaryOperator);
429 LookForDecls = false;
430 }
431 }
432 }
433
434 if (CurrentToken->Previous->is(TT_PointerOrReference) &&
435 CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
436 tok::coloncolon)) {
437 ProbablyFunctionType = true;
438 }
439 if (CurrentToken->is(tok::comma))
440 MightBeFunctionType = false;
441 if (CurrentToken->Previous->is(TT_BinaryOperator))
442 Contexts.back().IsExpression = true;
443 if (CurrentToken->is(tok::r_paren)) {
444 if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
445 ProbablyFunctionType && CurrentToken->Next &&
446 (CurrentToken->Next->is(tok::l_paren) ||
447 (CurrentToken->Next->is(tok::l_square) &&
448 Line.MustBeDeclaration))) {
449 OpeningParen.setType(OpeningParen.Next->is(tok::caret)
450 ? TT_ObjCBlockLParen
451 : TT_FunctionTypeLParen);
452 }
453 OpeningParen.MatchingParen = CurrentToken;
454 CurrentToken->MatchingParen = &OpeningParen;
455
456 if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
457 OpeningParen.Previous && OpeningParen.Previous->is(tok::l_paren)) {
458 // Detect the case where macros are used to generate lambdas or
459 // function bodies, e.g.:
460 // auto my_lambda = MACRO((Type *type, int i) { .. body .. });
461 for (FormatToken *Tok = &OpeningParen; Tok != CurrentToken;
462 Tok = Tok->Next) {
463 if (Tok->is(TT_BinaryOperator) &&
464 Tok->isOneOf(tok::star, tok::amp, tok::ampamp)) {
465 Tok->setType(TT_PointerOrReference);
466 }
467 }
468 }
469
470 if (StartsObjCMethodExpr) {
471 CurrentToken->setType(TT_ObjCMethodExpr);
472 if (Contexts.back().FirstObjCSelectorName) {
473 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
474 Contexts.back().LongestObjCSelectorName;
475 }
476 }
477
478 if (OpeningParen.is(TT_AttributeLParen))
479 CurrentToken->setType(TT_AttributeRParen);
480 if (OpeningParen.is(TT_TypeDeclarationParen))
481 CurrentToken->setType(TT_TypeDeclarationParen);
482 if (OpeningParen.Previous &&
483 OpeningParen.Previous->is(TT_JavaAnnotation)) {
484 CurrentToken->setType(TT_JavaAnnotation);
485 }
486 if (OpeningParen.Previous &&
487 OpeningParen.Previous->is(TT_LeadingJavaAnnotation)) {
488 CurrentToken->setType(TT_LeadingJavaAnnotation);
489 }
490 if (OpeningParen.Previous &&
491 OpeningParen.Previous->is(TT_AttributeSquare)) {
492 CurrentToken->setType(TT_AttributeSquare);
493 }
494
495 if (!HasMultipleLines)
496 OpeningParen.setPackingKind(PPK_Inconclusive);
497 else if (HasMultipleParametersOnALine)
498 OpeningParen.setPackingKind(PPK_BinPacked);
499 else
500 OpeningParen.setPackingKind(PPK_OnePerLine);
501
502 next();
503 return true;
504 }
505 if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
506 return false;
507
508 if (CurrentToken->is(tok::l_brace) && OpeningParen.is(TT_ObjCBlockLParen))
509 OpeningParen.setType(TT_Unknown);
510 if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
511 !CurrentToken->Next->HasUnescapedNewline &&
512 !CurrentToken->Next->isTrailingComment()) {
513 HasMultipleParametersOnALine = true;
514 }
515 bool ProbablyFunctionTypeLParen =
516 (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
517 CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
518 if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
519 CurrentToken->Previous->isSimpleTypeSpecifier()) &&
520 !(CurrentToken->is(tok::l_brace) ||
521 (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
522 Contexts.back().IsExpression = false;
523 }
524 if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
525 MightBeObjCForRangeLoop = false;
526 if (PossibleObjCForInToken) {
527 PossibleObjCForInToken->setType(TT_Unknown);
528 PossibleObjCForInToken = nullptr;
529 }
530 }
531 if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
532 PossibleObjCForInToken = CurrentToken;
533 PossibleObjCForInToken->setType(TT_ObjCForIn);
534 }
535 // When we discover a 'new', we set CanBeExpression to 'false' in order to
536 // parse the type correctly. Reset that after a comma.
537 if (CurrentToken->is(tok::comma))
538 Contexts.back().CanBeExpression = true;
539
540 FormatToken *Tok = CurrentToken;
541 if (!consumeToken())
542 return false;
543 updateParameterCount(&OpeningParen, Tok);
544 if (CurrentToken && CurrentToken->HasUnescapedNewline)
545 HasMultipleLines = true;
546 }
547 return false;
548 }
549
550 bool isCSharpAttributeSpecifier(const FormatToken &Tok) {
551 if (!Style.isCSharp())
552 return false;
553
554 // `identifier[i]` is not an attribute.
555 if (Tok.Previous && Tok.Previous->is(tok::identifier))
556 return false;
557
558 // Chains of [] in `identifier[i][j][k]` are not attributes.
559 if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
560 auto *MatchingParen = Tok.Previous->MatchingParen;
561 if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
562 return false;
563 }
564
565 const FormatToken *AttrTok = Tok.Next;
566 if (!AttrTok)
567 return false;
568
569 // Just an empty declaration e.g. string [].
570 if (AttrTok->is(tok::r_square))
571 return false;
572
573 // Move along the tokens inbetween the '[' and ']' e.g. [STAThread].
574 while (AttrTok && AttrTok->isNot(tok::r_square))
575 AttrTok = AttrTok->Next;
576
577 if (!AttrTok)
578 return false;
579
580 // Allow an attribute to be the only content of a file.
581 AttrTok = AttrTok->Next;
582 if (!AttrTok)
583 return true;
584
585 // Limit this to being an access modifier that follows.
586 if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
587 tok::comment, tok::kw_class, tok::kw_static,
588 tok::l_square, Keywords.kw_internal)) {
589 return true;
590 }
591
592 // incase its a [XXX] retval func(....
593 if (AttrTok->Next &&
594 AttrTok->Next->startsSequence(tok::identifier, tok::l_paren)) {
595 return true;
596 }
597
598 return false;
599 }
600
601 bool parseSquare() {
602 if (!CurrentToken)
603 return false;
604
605 // A '[' could be an index subscript (after an identifier or after
606 // ')' or ']'), it could be the start of an Objective-C method
607 // expression, it could the start of an Objective-C array literal,
608 // or it could be a C++ attribute specifier [[foo::bar]].
609 FormatToken *Left = CurrentToken->Previous;
610 Left->ParentBracket = Contexts.back().ContextKind;
611 FormatToken *Parent = Left->getPreviousNonComment();
612
613 // Cases where '>' is followed by '['.
614 // In C++, this can happen either in array of templates (foo<int>[10])
615 // or when array is a nested template type (unique_ptr<type1<type2>[]>).
616 bool CppArrayTemplates =
617 Style.isCpp() && Parent && Parent->is(TT_TemplateCloser) &&
618 (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
619 Contexts.back().ContextType == Context::TemplateArgument);
620
621 const bool IsInnerSquare = Contexts.back().InCpp11AttributeSpecifier;
622 const bool IsCpp11AttributeSpecifier =
623 isCppAttribute(Style.isCpp(), *Left) || IsInnerSquare;
624
625 // Treat C# Attributes [STAThread] much like C++ attributes [[...]].
626 bool IsCSharpAttributeSpecifier =
627 isCSharpAttributeSpecifier(*Left) ||
628 Contexts.back().InCSharpAttributeSpecifier;
629
630 bool InsideInlineASM = Line.startsWith(tok::kw_asm);
631 bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style);
632 bool StartsObjCMethodExpr =
633 !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
634 Style.isCpp() && !IsCpp11AttributeSpecifier &&
635 !IsCSharpAttributeSpecifier && Contexts.back().CanBeExpression &&
636 Left->isNot(TT_LambdaLSquare) &&
637 !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
638 (!Parent ||
639 Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
640 tok::kw_return, tok::kw_throw) ||
641 Parent->isUnaryOperator() ||
642 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
643 Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
644 (getBinOpPrecedence(Parent->Tok.getKind(), true, true) >
646 bool ColonFound = false;
647
648 unsigned BindingIncrease = 1;
649 if (IsCppStructuredBinding) {
650 Left->setType(TT_StructuredBindingLSquare);
651 } else if (Left->is(TT_Unknown)) {
652 if (StartsObjCMethodExpr) {
653 Left->setType(TT_ObjCMethodExpr);
654 } else if (InsideInlineASM) {
655 Left->setType(TT_InlineASMSymbolicNameLSquare);
656 } else if (IsCpp11AttributeSpecifier) {
657 Left->setType(TT_AttributeSquare);
658 if (!IsInnerSquare && Left->Previous)
659 Left->Previous->EndsCppAttributeGroup = false;
660 } else if (Style.isJavaScript() && Parent &&
661 Contexts.back().ContextKind == tok::l_brace &&
662 Parent->isOneOf(tok::l_brace, tok::comma)) {
663 Left->setType(TT_JsComputedPropertyName);
664 } else if (Style.isCpp() && Contexts.back().ContextKind == tok::l_brace &&
665 Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
666 Left->setType(TT_DesignatedInitializerLSquare);
667 } else if (IsCSharpAttributeSpecifier) {
668 Left->setType(TT_AttributeSquare);
669 } else if (CurrentToken->is(tok::r_square) && Parent &&
670 Parent->is(TT_TemplateCloser)) {
671 Left->setType(TT_ArraySubscriptLSquare);
672 } else if (Style.Language == FormatStyle::LK_Proto ||
673 Style.Language == FormatStyle::LK_TextProto) {
674 // Square braces in LK_Proto can either be message field attributes:
675 //
676 // optional Aaa aaa = 1 [
677 // (aaa) = aaa
678 // ];
679 //
680 // extensions 123 [
681 // (aaa) = aaa
682 // ];
683 //
684 // or text proto extensions (in options):
685 //
686 // option (Aaa.options) = {
687 // [type.type/type] {
688 // key: value
689 // }
690 // }
691 //
692 // or repeated fields (in options):
693 //
694 // option (Aaa.options) = {
695 // keys: [ 1, 2, 3 ]
696 // }
697 //
698 // In the first and the third case we want to spread the contents inside
699 // the square braces; in the second we want to keep them inline.
700 Left->setType(TT_ArrayInitializerLSquare);
701 if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
702 tok::equal) &&
703 !Left->endsSequence(tok::l_square, tok::numeric_constant,
704 tok::identifier) &&
705 !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
706 Left->setType(TT_ProtoExtensionLSquare);
707 BindingIncrease = 10;
708 }
709 } else if (!CppArrayTemplates && Parent &&
710 Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
711 tok::comma, tok::l_paren, tok::l_square,
712 tok::question, tok::colon, tok::kw_return,
713 // Should only be relevant to JavaScript:
714 tok::kw_default)) {
715 Left->setType(TT_ArrayInitializerLSquare);
716 } else {
717 BindingIncrease = 10;
718 Left->setType(TT_ArraySubscriptLSquare);
719 }
720 }
721
722 ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease);
723 Contexts.back().IsExpression = true;
724 if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
725 Contexts.back().IsExpression = false;
726
727 Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
728 Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
729 Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
730
731 while (CurrentToken) {
732 if (CurrentToken->is(tok::r_square)) {
733 if (IsCpp11AttributeSpecifier) {
734 CurrentToken->setType(TT_AttributeSquare);
735 if (!IsInnerSquare)
736 CurrentToken->EndsCppAttributeGroup = true;
737 }
738 if (IsCSharpAttributeSpecifier) {
739 CurrentToken->setType(TT_AttributeSquare);
740 } else if (((CurrentToken->Next &&
741 CurrentToken->Next->is(tok::l_paren)) ||
742 (CurrentToken->Previous &&
743 CurrentToken->Previous->Previous == Left)) &&
744 Left->is(TT_ObjCMethodExpr)) {
745 // An ObjC method call is rarely followed by an open parenthesis. It
746 // also can't be composed of just one token, unless it's a macro that
747 // will be expanded to more tokens.
748 // FIXME: Do we incorrectly label ":" with this?
749 StartsObjCMethodExpr = false;
750 Left->setType(TT_Unknown);
751 }
752 if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
753 CurrentToken->setType(TT_ObjCMethodExpr);
754 // If we haven't seen a colon yet, make sure the last identifier
755 // before the r_square is tagged as a selector name component.
756 if (!ColonFound && CurrentToken->Previous &&
757 CurrentToken->Previous->is(TT_Unknown) &&
758 canBeObjCSelectorComponent(*CurrentToken->Previous)) {
759 CurrentToken->Previous->setType(TT_SelectorName);
760 }
761 // determineStarAmpUsage() thinks that '*' '[' is allocating an
762 // array of pointers, but if '[' starts a selector then '*' is a
763 // binary operator.
764 if (Parent && Parent->is(TT_PointerOrReference))
765 Parent->overwriteFixedType(TT_BinaryOperator);
766 }
767 // An arrow after an ObjC method expression is not a lambda arrow.
768 if (CurrentToken->getType() == TT_ObjCMethodExpr &&
769 CurrentToken->Next && CurrentToken->Next->is(TT_LambdaArrow)) {
770 CurrentToken->Next->overwriteFixedType(TT_Unknown);
771 }
772 Left->MatchingParen = CurrentToken;
773 CurrentToken->MatchingParen = Left;
774 // FirstObjCSelectorName is set when a colon is found. This does
775 // not work, however, when the method has no parameters.
776 // Here, we set FirstObjCSelectorName when the end of the method call is
777 // reached, in case it was not set already.
778 if (!Contexts.back().FirstObjCSelectorName) {
779 FormatToken *Previous = CurrentToken->getPreviousNonComment();
780 if (Previous && Previous->is(TT_SelectorName)) {
781 Previous->ObjCSelectorNameParts = 1;
782 Contexts.back().FirstObjCSelectorName = Previous;
783 }
784 } else {
785 Left->ParameterCount =
786 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
787 }
788 if (Contexts.back().FirstObjCSelectorName) {
789 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
790 Contexts.back().LongestObjCSelectorName;
791 if (Left->BlockParameterCount > 1)
792 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
793 }
794 next();
795 return true;
796 }
797 if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
798 return false;
799 if (CurrentToken->is(tok::colon)) {
800 if (IsCpp11AttributeSpecifier &&
801 CurrentToken->endsSequence(tok::colon, tok::identifier,
802 tok::kw_using)) {
803 // Remember that this is a [[using ns: foo]] C++ attribute, so we
804 // don't add a space before the colon (unlike other colons).
805 CurrentToken->setType(TT_AttributeColon);
806 } else if (!Style.isVerilog() && !Line.InPragmaDirective &&
807 Left->isOneOf(TT_ArraySubscriptLSquare,
808 TT_DesignatedInitializerLSquare)) {
809 Left->setType(TT_ObjCMethodExpr);
810 StartsObjCMethodExpr = true;
811 Contexts.back().ColonIsObjCMethodExpr = true;
812 if (Parent && Parent->is(tok::r_paren)) {
813 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
814 Parent->setType(TT_CastRParen);
815 }
816 }
817 ColonFound = true;
818 }
819 if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
820 !ColonFound) {
821 Left->setType(TT_ArrayInitializerLSquare);
822 }
823 FormatToken *Tok = CurrentToken;
824 if (!consumeToken())
825 return false;
826 updateParameterCount(Left, Tok);
827 }
828 return false;
829 }
830
831 bool couldBeInStructArrayInitializer() const {
832 if (Contexts.size() < 2)
833 return false;
834 // We want to back up no more then 2 context levels i.e.
835 // . { { <-
836 const auto End = std::next(Contexts.rbegin(), 2);
837 auto Last = Contexts.rbegin();
838 unsigned Depth = 0;
839 for (; Last != End; ++Last)
840 if (Last->ContextKind == tok::l_brace)
841 ++Depth;
842 return Depth == 2 && Last->ContextKind != tok::l_brace;
843 }
844
845 bool parseBrace() {
846 if (!CurrentToken)
847 return true;
848
849 assert(CurrentToken->Previous);
850 FormatToken &OpeningBrace = *CurrentToken->Previous;
851 assert(OpeningBrace.is(tok::l_brace));
852 OpeningBrace.ParentBracket = Contexts.back().ContextKind;
853
854 if (Contexts.back().CaretFound)
855 OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
856 Contexts.back().CaretFound = false;
857
858 ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
859 Contexts.back().ColonIsDictLiteral = true;
860 if (OpeningBrace.is(BK_BracedInit))
861 Contexts.back().IsExpression = true;
862 if (Style.isJavaScript() && OpeningBrace.Previous &&
863 OpeningBrace.Previous->is(TT_JsTypeColon)) {
864 Contexts.back().IsExpression = false;
865 }
866 if (Style.isVerilog() &&
867 (!OpeningBrace.getPreviousNonComment() ||
868 OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
869 Contexts.back().VerilogMayBeConcatenation = true;
870 }
871
872 unsigned CommaCount = 0;
873 while (CurrentToken) {
874 if (CurrentToken->is(tok::r_brace)) {
875 assert(!Scopes.empty());
876 assert(Scopes.back() == getScopeType(OpeningBrace));
877 Scopes.pop_back();
878 assert(OpeningBrace.Optional == CurrentToken->Optional);
879 OpeningBrace.MatchingParen = CurrentToken;
880 CurrentToken->MatchingParen = &OpeningBrace;
881 if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
882 if (OpeningBrace.ParentBracket == tok::l_brace &&
883 couldBeInStructArrayInitializer() && CommaCount > 0) {
884 Contexts.back().ContextType = Context::StructArrayInitializer;
885 }
886 }
887 next();
888 return true;
889 }
890 if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
891 return false;
892 updateParameterCount(&OpeningBrace, CurrentToken);
893 if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
894 FormatToken *Previous = CurrentToken->getPreviousNonComment();
895 if (Previous->is(TT_JsTypeOptionalQuestion))
896 Previous = Previous->getPreviousNonComment();
897 if ((CurrentToken->is(tok::colon) &&
898 (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
899 Style.Language == FormatStyle::LK_Proto ||
900 Style.Language == FormatStyle::LK_TextProto) {
901 OpeningBrace.setType(TT_DictLiteral);
902 if (Previous->Tok.getIdentifierInfo() ||
903 Previous->is(tok::string_literal)) {
904 Previous->setType(TT_SelectorName);
905 }
906 }
907 if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown))
908 OpeningBrace.setType(TT_DictLiteral);
909 else if (Style.isJavaScript())
910 OpeningBrace.overwriteFixedType(TT_DictLiteral);
911 }
912 if (CurrentToken->is(tok::comma)) {
913 if (Style.isJavaScript())
914 OpeningBrace.overwriteFixedType(TT_DictLiteral);
915 ++CommaCount;
916 }
917 if (!consumeToken())
918 return false;
919 }
920 return true;
921 }
922
923 void updateParameterCount(FormatToken *Left, FormatToken *Current) {
924 // For ObjC methods, the number of parameters is calculated differently as
925 // method declarations have a different structure (the parameters are not
926 // inside a bracket scope).
927 if (Current->is(tok::l_brace) && Current->is(BK_Block))
928 ++Left->BlockParameterCount;
929 if (Current->is(tok::comma)) {
930 ++Left->ParameterCount;
931 if (!Left->Role)
932 Left->Role.reset(new CommaSeparatedList(Style));
933 Left->Role->CommaFound(Current);
934 } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
935 Left->ParameterCount = 1;
936 }
937 }
938
939 bool parseConditional() {
940 while (CurrentToken) {
941 if (CurrentToken->is(tok::colon)) {
942 CurrentToken->setType(TT_ConditionalExpr);
943 next();
944 return true;
945 }
946 if (!consumeToken())
947 return false;
948 }
949 return false;
950 }
951
952 bool parseTemplateDeclaration() {
953 if (CurrentToken && CurrentToken->is(tok::less)) {
954 CurrentToken->setType(TT_TemplateOpener);
955 next();
956 if (!parseAngle())
957 return false;
958 if (CurrentToken)
959 CurrentToken->Previous->ClosesTemplateDeclaration = true;
960 return true;
961 }
962 return false;
963 }
964
965 bool consumeToken() {
966 FormatToken *Tok = CurrentToken;
967 next();
968 // In Verilog primitives' state tables, `:`, `?`, and `-` aren't normal
969 // operators.
970 if (Tok->is(TT_VerilogTableItem))
971 return true;
972 switch (Tok->Tok.getKind()) {
973 case tok::plus:
974 case tok::minus:
975 if (!Tok->Previous && Line.MustBeDeclaration)
976 Tok->setType(TT_ObjCMethodSpecifier);
977 break;
978 case tok::colon:
979 if (!Tok->Previous)
980 return false;
981 // Goto labels and case labels are already identified in
982 // UnwrappedLineParser.
983 if (Tok->isTypeFinalized())
984 break;
985 // Colons from ?: are handled in parseConditional().
986 if (Style.isJavaScript()) {
987 if (Contexts.back().ColonIsForRangeExpr || // colon in for loop
988 (Contexts.size() == 1 && // switch/case labels
989 !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
990 Contexts.back().ContextKind == tok::l_paren || // function params
991 Contexts.back().ContextKind == tok::l_square || // array type
992 (!Contexts.back().IsExpression &&
993 Contexts.back().ContextKind == tok::l_brace) || // object type
994 (Contexts.size() == 1 &&
995 Line.MustBeDeclaration)) { // method/property declaration
996 Contexts.back().IsExpression = false;
997 Tok->setType(TT_JsTypeColon);
998 break;
999 }
1000 } else if (Style.isCSharp()) {
1001 if (Contexts.back().InCSharpAttributeSpecifier) {
1002 Tok->setType(TT_AttributeColon);
1003 break;
1004 }
1005 if (Contexts.back().ContextKind == tok::l_paren) {
1006 Tok->setType(TT_CSharpNamedArgumentColon);
1007 break;
1008 }
1009 } else if (Style.isVerilog() && Tok->isNot(TT_BinaryOperator)) {
1010 // The distribution weight operators are labeled
1011 // TT_BinaryOperator by the lexer.
1012 if (Keywords.isVerilogEnd(*Tok->Previous) ||
1013 Keywords.isVerilogBegin(*Tok->Previous)) {
1014 Tok->setType(TT_VerilogBlockLabelColon);
1015 } else if (Contexts.back().ContextKind == tok::l_square) {
1016 Tok->setType(TT_BitFieldColon);
1017 } else if (Contexts.back().ColonIsDictLiteral) {
1018 Tok->setType(TT_DictLiteral);
1019 } else if (Contexts.size() == 1) {
1020 // In Verilog a case label doesn't have the case keyword. We
1021 // assume a colon following an expression is a case label.
1022 // Colons from ?: are annotated in parseConditional().
1023 Tok->setType(TT_CaseLabelColon);
1024 if (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))
1025 --Line.Level;
1026 }
1027 break;
1028 }
1029 if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
1030 Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
1031 Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
1032 Tok->setType(TT_ModulePartitionColon);
1033 } else if (Contexts.back().ColonIsDictLiteral ||
1034 Style.Language == FormatStyle::LK_Proto ||
1035 Style.Language == FormatStyle::LK_TextProto) {
1036 Tok->setType(TT_DictLiteral);
1037 if (Style.Language == FormatStyle::LK_TextProto) {
1038 if (FormatToken *Previous = Tok->getPreviousNonComment())
1039 Previous->setType(TT_SelectorName);
1040 }
1041 } else if (Contexts.back().ColonIsObjCMethodExpr ||
1042 Line.startsWith(TT_ObjCMethodSpecifier)) {
1043 Tok->setType(TT_ObjCMethodExpr);
1044 const FormatToken *BeforePrevious = Tok->Previous->Previous;
1045 // Ensure we tag all identifiers in method declarations as
1046 // TT_SelectorName.
1047 bool UnknownIdentifierInMethodDeclaration =
1048 Line.startsWith(TT_ObjCMethodSpecifier) &&
1049 Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
1050 if (!BeforePrevious ||
1051 // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
1052 !(BeforePrevious->is(TT_CastRParen) ||
1053 (BeforePrevious->is(TT_ObjCMethodExpr) &&
1054 BeforePrevious->is(tok::colon))) ||
1055 BeforePrevious->is(tok::r_square) ||
1056 Contexts.back().LongestObjCSelectorName == 0 ||
1057 UnknownIdentifierInMethodDeclaration) {
1058 Tok->Previous->setType(TT_SelectorName);
1059 if (!Contexts.back().FirstObjCSelectorName) {
1060 Contexts.back().FirstObjCSelectorName = Tok->Previous;
1061 } else if (Tok->Previous->ColumnWidth >
1062 Contexts.back().LongestObjCSelectorName) {
1063 Contexts.back().LongestObjCSelectorName =
1064 Tok->Previous->ColumnWidth;
1065 }
1066 Tok->Previous->ParameterIndex =
1067 Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1068 ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
1069 }
1070 } else if (Contexts.back().ColonIsForRangeExpr) {
1071 Tok->setType(TT_RangeBasedForLoopColon);
1072 } else if (Contexts.back().ContextType == Context::C11GenericSelection) {
1073 Tok->setType(TT_GenericSelectionColon);
1074 } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
1075 Tok->setType(TT_BitFieldColon);
1076 } else if (Contexts.size() == 1 &&
1077 !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
1078 tok::kw_default)) {
1079 FormatToken *Prev = Tok->getPreviousNonComment();
1080 if (!Prev)
1081 break;
1082 if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
1083 Prev->ClosesRequiresClause) {
1084 Tok->setType(TT_CtorInitializerColon);
1085 } else if (Prev->is(tok::kw_try)) {
1086 // Member initializer list within function try block.
1087 FormatToken *PrevPrev = Prev->getPreviousNonComment();
1088 if (!PrevPrev)
1089 break;
1090 if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
1091 Tok->setType(TT_CtorInitializerColon);
1092 } else {
1093 Tok->setType(TT_InheritanceColon);
1094 }
1095 } else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
1096 (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
1097 (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
1098 Tok->Next->Next->is(tok::colon)))) {
1099 // This handles a special macro in ObjC code where selectors including
1100 // the colon are passed as macro arguments.
1101 Tok->setType(TT_ObjCMethodExpr);
1102 } else if (Contexts.back().ContextKind == tok::l_paren &&
1103 !Line.InPragmaDirective) {
1104 Tok->setType(TT_InlineASMColon);
1105 }
1106 break;
1107 case tok::pipe:
1108 case tok::amp:
1109 // | and & in declarations/type expressions represent union and
1110 // intersection types, respectively.
1111 if (Style.isJavaScript() && !Contexts.back().IsExpression)
1112 Tok->setType(TT_JsTypeOperator);
1113 break;
1114 case tok::kw_if:
1115 if (CurrentToken &&
1116 CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier)) {
1117 next();
1118 }
1119 [[fallthrough]];
1120 case tok::kw_while:
1121 if (CurrentToken && CurrentToken->is(tok::l_paren)) {
1122 next();
1123 if (!parseParens(/*LookForDecls=*/true))
1124 return false;
1125 }
1126 break;
1127 case tok::kw_for:
1128 if (Style.isJavaScript()) {
1129 // x.for and {for: ...}
1130 if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
1131 (Tok->Next && Tok->Next->is(tok::colon))) {
1132 break;
1133 }
1134 // JS' for await ( ...
1135 if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1136 next();
1137 }
1138 if (Style.isCpp() && CurrentToken && CurrentToken->is(tok::kw_co_await))
1139 next();
1140 Contexts.back().ColonIsForRangeExpr = true;
1141 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1142 return false;
1143 next();
1144 if (!parseParens())
1145 return false;
1146 break;
1147 case tok::l_paren:
1148 // When faced with 'operator()()', the kw_operator handler incorrectly
1149 // marks the first l_paren as a OverloadedOperatorLParen. Here, we make
1150 // the first two parens OverloadedOperators and the second l_paren an
1151 // OverloadedOperatorLParen.
1152 if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
1153 Tok->Previous->MatchingParen &&
1154 Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1155 Tok->Previous->setType(TT_OverloadedOperator);
1156 Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1157 Tok->setType(TT_OverloadedOperatorLParen);
1158 }
1159
1160 if (Style.isVerilog()) {
1161 // Identify the parameter list and port list in a module instantiation.
1162 // This is still needed when we already have
1163 // UnwrappedLineParser::parseVerilogHierarchyHeader because that
1164 // function is only responsible for the definition, not the
1165 // instantiation.
1166 auto IsInstancePort = [&]() {
1167 const FormatToken *Prev = Tok->getPreviousNonComment();
1168 const FormatToken *PrevPrev;
1169 // In the following example all 4 left parentheses will be treated as
1170 // 'TT_VerilogInstancePortLParen'.
1171 //
1172 // module_x instance_1(port_1); // Case A.
1173 // module_x #(parameter_1) // Case B.
1174 // instance_2(port_1), // Case C.
1175 // instance_3(port_1); // Case D.
1176 if (!Prev || !(PrevPrev = Prev->getPreviousNonComment()))
1177 return false;
1178 // Case A.
1179 if (Keywords.isVerilogIdentifier(*Prev) &&
1180 Keywords.isVerilogIdentifier(*PrevPrev)) {
1181 return true;
1182 }
1183 // Case B.
1184 if (Prev->is(Keywords.kw_verilogHash) &&
1185 Keywords.isVerilogIdentifier(*PrevPrev)) {
1186 return true;
1187 }
1188 // Case C.
1189 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::r_paren))
1190 return true;
1191 // Case D.
1192 if (Keywords.isVerilogIdentifier(*Prev) && PrevPrev->is(tok::comma)) {
1193 const FormatToken *PrevParen = PrevPrev->getPreviousNonComment();
1194 if (PrevParen->is(tok::r_paren) && PrevParen->MatchingParen &&
1195 PrevParen->MatchingParen->is(TT_VerilogInstancePortLParen)) {
1196 return true;
1197 }
1198 }
1199 return false;
1200 };
1201
1202 if (IsInstancePort())
1203 Tok->setFinalizedType(TT_VerilogInstancePortLParen);
1204 }
1205
1206 if (!parseParens())
1207 return false;
1208 if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1209 !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1210 !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
1211 if (const auto *Previous = Tok->Previous;
1212 !Previous ||
1213 (!Previous->isAttribute() &&
1214 !Previous->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation))) {
1215 Line.MightBeFunctionDecl = true;
1216 }
1217 }
1218 break;
1219 case tok::l_square:
1220 if (!parseSquare())
1221 return false;
1222 break;
1223 case tok::l_brace:
1224 if (Style.Language == FormatStyle::LK_TextProto) {
1225 FormatToken *Previous = Tok->getPreviousNonComment();
1226 if (Previous && Previous->getType() != TT_DictLiteral)
1227 Previous->setType(TT_SelectorName);
1228 }
1229 Scopes.push_back(getScopeType(*Tok));
1230 if (!parseBrace())
1231 return false;
1232 break;
1233 case tok::less:
1234 if (parseAngle()) {
1235 Tok->setType(TT_TemplateOpener);
1236 // In TT_Proto, we must distignuish between:
1237 // map<key, value>
1238 // msg < item: data >
1239 // msg: < item: data >
1240 // In TT_TextProto, map<key, value> does not occur.
1241 if (Style.Language == FormatStyle::LK_TextProto ||
1242 (Style.Language == FormatStyle::LK_Proto && Tok->Previous &&
1243 Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1244 Tok->setType(TT_DictLiteral);
1245 FormatToken *Previous = Tok->getPreviousNonComment();
1246 if (Previous && Previous->getType() != TT_DictLiteral)
1247 Previous->setType(TT_SelectorName);
1248 }
1249 } else {
1250 Tok->setType(TT_BinaryOperator);
1251 NonTemplateLess.insert(Tok);
1252 CurrentToken = Tok;
1253 next();
1254 }
1255 break;
1256 case tok::r_paren:
1257 case tok::r_square:
1258 return false;
1259 case tok::r_brace:
1260 // Don't pop scope when encountering unbalanced r_brace.
1261 if (!Scopes.empty())
1262 Scopes.pop_back();
1263 // Lines can start with '}'.
1264 if (Tok->Previous)
1265 return false;
1266 break;
1267 case tok::greater:
1268 if (Style.Language != FormatStyle::LK_TextProto)
1269 Tok->setType(TT_BinaryOperator);
1270 if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1271 Tok->SpacesRequiredBefore = 1;
1272 break;
1273 case tok::kw_operator:
1274 if (Style.Language == FormatStyle::LK_TextProto ||
1275 Style.Language == FormatStyle::LK_Proto) {
1276 break;
1277 }
1278 while (CurrentToken &&
1279 !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1280 if (CurrentToken->isOneOf(tok::star, tok::amp))
1281 CurrentToken->setType(TT_PointerOrReference);
1282 auto Next = CurrentToken->getNextNonComment();
1283 if (!Next)
1284 break;
1285 if (Next->is(tok::less))
1286 next();
1287 else
1288 consumeToken();
1289 if (!CurrentToken)
1290 break;
1291 auto Previous = CurrentToken->getPreviousNonComment();
1292 assert(Previous);
1293 if (CurrentToken->is(tok::comma) && Previous->isNot(tok::kw_operator))
1294 break;
1295 if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1296 tok::star, tok::arrow, tok::amp, tok::ampamp) ||
1297 // User defined literal.
1298 Previous->TokenText.startswith("\"\"")) {
1299 Previous->setType(TT_OverloadedOperator);
1300 if (CurrentToken->isOneOf(tok::less, tok::greater))
1301 break;
1302 }
1303 }
1304 if (CurrentToken && CurrentToken->is(tok::l_paren))
1305 CurrentToken->setType(TT_OverloadedOperatorLParen);
1306 if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1307 CurrentToken->Previous->setType(TT_OverloadedOperator);
1308 break;
1309 case tok::question:
1310 if (Style.isJavaScript() && Tok->Next &&
1311 Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1312 tok::r_brace, tok::r_square)) {
1313 // Question marks before semicolons, colons, etc. indicate optional
1314 // types (fields, parameters), e.g.
1315 // function(x?: string, y?) {...}
1316 // class X { y?; }
1317 Tok->setType(TT_JsTypeOptionalQuestion);
1318 break;
1319 }
1320 // Declarations cannot be conditional expressions, this can only be part
1321 // of a type declaration.
1322 if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1323 Style.isJavaScript()) {
1324 break;
1325 }
1326 if (Style.isCSharp()) {
1327 // `Type?)`, `Type?>`, `Type? name;` and `Type? name =` can only be
1328 // nullable types.
1329
1330 // `Type?)`, `Type?>`, `Type? name;`
1331 if (Tok->Next &&
1332 (Tok->Next->startsSequence(tok::question, tok::r_paren) ||
1333 Tok->Next->startsSequence(tok::question, tok::greater) ||
1334 Tok->Next->startsSequence(tok::question, tok::identifier,
1335 tok::semi))) {
1336 Tok->setType(TT_CSharpNullable);
1337 break;
1338 }
1339
1340 // `Type? name =`
1341 if (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1342 Tok->Next->Next->is(tok::equal)) {
1343 Tok->setType(TT_CSharpNullable);
1344 break;
1345 }
1346
1347 // Line.MustBeDeclaration will be true for `Type? name;`.
1348 // But not
1349 // cond ? "A" : "B";
1350 // cond ? id : "B";
1351 // cond ? cond2 ? "A" : "B" : "C";
1352 if (!Contexts.back().IsExpression && Line.MustBeDeclaration &&
1353 (!Tok->Next ||
1354 !Tok->Next->isOneOf(tok::identifier, tok::string_literal) ||
1355 !Tok->Next->Next ||
1356 !Tok->Next->Next->isOneOf(tok::colon, tok::question))) {
1357 Tok->setType(TT_CSharpNullable);
1358 break;
1359 }
1360 }
1361 parseConditional();
1362 break;
1363 case tok::kw_template:
1364 parseTemplateDeclaration();
1365 break;
1366 case tok::comma:
1367 switch (Contexts.back().ContextType) {
1368 case Context::CtorInitializer:
1369 Tok->setType(TT_CtorInitializerComma);
1370 break;
1371 case Context::InheritanceList:
1372 Tok->setType(TT_InheritanceComma);
1373 break;
1374 case Context::VerilogInstancePortList:
1375 Tok->setFinalizedType(TT_VerilogInstancePortComma);
1376 break;
1377 default:
1378 if (Style.isVerilog() && Contexts.size() == 1 &&
1379 Line.startsWith(Keywords.kw_assign)) {
1380 Tok->setFinalizedType(TT_VerilogAssignComma);
1381 } else if (Contexts.back().FirstStartOfName &&
1382 (Contexts.size() == 1 || startsWithInitStatement(Line))) {
1383 Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
1384 Line.IsMultiVariableDeclStmt = true;
1385 }
1386 break;
1387 }
1388 if (Contexts.back().ContextType == Context::ForEachMacro)
1389 Contexts.back().IsExpression = true;
1390 break;
1391 case tok::kw_default:
1392 // Unindent case labels.
1393 if (Style.isVerilog() && Keywords.isVerilogEndOfLabel(*Tok) &&
1394 (Line.Level > 1 || (!Line.InPPDirective && Line.Level > 0))) {
1395 --Line.Level;
1396 }
1397 break;
1398 case tok::identifier:
1399 if (Tok->isOneOf(Keywords.kw___has_include,
1400 Keywords.kw___has_include_next)) {
1401 parseHasInclude();
1402 }
1403 if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1404 Tok->Next->isNot(tok::l_paren)) {
1405 Tok->setType(TT_CSharpGenericTypeConstraint);
1406 parseCSharpGenericTypeConstraint();
1407 if (!Tok->getPreviousNonComment())
1408 Line.IsContinuation = true;
1409 }
1410 break;
1411 case tok::arrow:
1412 if (Tok->isNot(TT_LambdaArrow) && Tok->Previous &&
1413 Tok->Previous->is(tok::kw_noexcept)) {
1414 Tok->setType(TT_TrailingReturnArrow);
1415 }
1416 break;
1417 case tok::eof:
1418 if (Style.InsertNewlineAtEOF && Tok->NewlinesBefore == 0)
1419 Tok->NewlinesBefore = 1;
1420 break;
1421 default:
1422 break;
1423 }
1424 return true;
1425 }
1426
1427 void parseCSharpGenericTypeConstraint() {
1428 int OpenAngleBracketsCount = 0;
1429 while (CurrentToken) {
1430 if (CurrentToken->is(tok::less)) {
1431 // parseAngle is too greedy and will consume the whole line.
1432 CurrentToken->setType(TT_TemplateOpener);
1433 ++OpenAngleBracketsCount;
1434 next();
1435 } else if (CurrentToken->is(tok::greater)) {
1436 CurrentToken->setType(TT_TemplateCloser);
1437 --OpenAngleBracketsCount;
1438 next();
1439 } else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1440 // We allow line breaks after GenericTypeConstraintComma's
1441 // so do not flag commas in Generics as GenericTypeConstraintComma's.
1442 CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1443 next();
1444 } else if (CurrentToken->is(Keywords.kw_where)) {
1445 CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1446 next();
1447 } else if (CurrentToken->is(tok::colon)) {
1448 CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1449 next();
1450 } else {
1451 next();
1452 }
1453 }
1454 }
1455
1456 void parseIncludeDirective() {
1457 if (CurrentToken && CurrentToken->is(tok::less)) {
1458 next();
1459 while (CurrentToken) {
1460 // Mark tokens up to the trailing line comments as implicit string
1461 // literals.
1462 if (CurrentToken->isNot(tok::comment) &&
1463 !CurrentToken->TokenText.startswith("//")) {
1464 CurrentToken->setType(TT_ImplicitStringLiteral);
1465 }
1466 next();
1467 }
1468 }
1469 }
1470
1471 void parseWarningOrError() {
1472 next();
1473 // We still want to format the whitespace left of the first token of the
1474 // warning or error.
1475 next();
1476 while (CurrentToken) {
1477 CurrentToken->setType(TT_ImplicitStringLiteral);
1478 next();
1479 }
1480 }
1481
1482 void parsePragma() {
1483 next(); // Consume "pragma".
1484 if (CurrentToken &&
1485 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option,
1486 Keywords.kw_region)) {
1487 bool IsMarkOrRegion =
1488 CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_region);
1489 next();
1490 next(); // Consume first token (so we fix leading whitespace).
1491 while (CurrentToken) {
1492 if (IsMarkOrRegion || CurrentToken->Previous->is(TT_BinaryOperator))
1493 CurrentToken->setType(TT_ImplicitStringLiteral);
1494 next();
1495 }
1496 }
1497 }
1498
1499 void parseHasInclude() {
1500 if (!CurrentToken || CurrentToken->isNot(tok::l_paren))
1501 return;
1502 next(); // '('
1503 parseIncludeDirective();
1504 next(); // ')'
1505 }
1506
1507 LineType parsePreprocessorDirective() {
1508 bool IsFirstToken = CurrentToken->IsFirst;
1510 next();
1511 if (!CurrentToken)
1512 return Type;
1513
1514 if (Style.isJavaScript() && IsFirstToken) {
1515 // JavaScript files can contain shebang lines of the form:
1516 // #!/usr/bin/env node
1517 // Treat these like C++ #include directives.
1518 while (CurrentToken) {
1519 // Tokens cannot be comments here.
1520 CurrentToken->setType(TT_ImplicitStringLiteral);
1521 next();
1522 }
1523 return LT_ImportStatement;
1524 }
1525
1526 if (CurrentToken->is(tok::numeric_constant)) {
1527 CurrentToken->SpacesRequiredBefore = 1;
1528 return Type;
1529 }
1530 // Hashes in the middle of a line can lead to any strange token
1531 // sequence.
1532 if (!CurrentToken->Tok.getIdentifierInfo())
1533 return Type;
1534 // In Verilog macro expansions start with a backtick just like preprocessor
1535 // directives. Thus we stop if the word is not a preprocessor directive.
1536 if (Style.isVerilog() && !Keywords.isVerilogPPDirective(*CurrentToken))
1537 return LT_Invalid;
1538 switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1539 case tok::pp_include:
1540 case tok::pp_include_next:
1541 case tok::pp_import:
1542 next();
1543 parseIncludeDirective();
1545 break;
1546 case tok::pp_error:
1547 case tok::pp_warning:
1548 parseWarningOrError();
1549 break;
1550 case tok::pp_pragma:
1551 parsePragma();
1552 break;
1553 case tok::pp_if:
1554 case tok::pp_elif:
1555 Contexts.back().IsExpression = true;
1556 next();
1557 parseLine();
1558 break;
1559 default:
1560 break;
1561 }
1562 while (CurrentToken) {
1563 FormatToken *Tok = CurrentToken;
1564 next();
1565 if (Tok->is(tok::l_paren)) {
1566 parseParens();
1567 } else if (Tok->isOneOf(Keywords.kw___has_include,
1568 Keywords.kw___has_include_next)) {
1569 parseHasInclude();
1570 }
1571 }
1572 return Type;
1573 }
1574
1575public:
1576 LineType parseLine() {
1577 if (!CurrentToken)
1578 return LT_Invalid;
1579 NonTemplateLess.clear();
1580 if (!Line.InMacroBody && CurrentToken->is(tok::hash)) {
1581 // We were not yet allowed to use C++17 optional when this was being
1582 // written. So we used LT_Invalid to mark that the line is not a
1583 // preprocessor directive.
1584 auto Type = parsePreprocessorDirective();
1585 if (Type != LT_Invalid)
1586 return Type;
1587 }
1588
1589 // Directly allow to 'import <string-literal>' to support protocol buffer
1590 // definitions (github.com/google/protobuf) or missing "#" (either way we
1591 // should not break the line).
1592 IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1593 if ((Style.Language == FormatStyle::LK_Java &&
1594 CurrentToken->is(Keywords.kw_package)) ||
1595 (!Style.isVerilog() && Info &&
1596 Info->getPPKeywordID() == tok::pp_import && CurrentToken->Next &&
1597 CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1598 tok::kw_static))) {
1599 next();
1600 parseIncludeDirective();
1601 return LT_ImportStatement;
1602 }
1603
1604 // If this line starts and ends in '<' and '>', respectively, it is likely
1605 // part of "#define <a/b.h>".
1606 if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1607 parseIncludeDirective();
1608 return LT_ImportStatement;
1609 }
1610
1611 // In .proto files, top-level options and package statements are very
1612 // similar to import statements and should not be line-wrapped.
1613 if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
1614 CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1615 next();
1616 if (CurrentToken && CurrentToken->is(tok::identifier)) {
1617 while (CurrentToken)
1618 next();
1619 return LT_ImportStatement;
1620 }
1621 }
1622
1623 bool KeywordVirtualFound = false;
1624 bool ImportStatement = false;
1625
1626 // import {...} from '...';
1627 if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
1628 ImportStatement = true;
1629
1630 while (CurrentToken) {
1631 if (CurrentToken->is(tok::kw_virtual))
1632 KeywordVirtualFound = true;
1633 if (Style.isJavaScript()) {
1634 // export {...} from '...';
1635 // An export followed by "from 'some string';" is a re-export from
1636 // another module identified by a URI and is treated as a
1637 // LT_ImportStatement (i.e. prevent wraps on it for long URIs).
1638 // Just "export {...};" or "export class ..." should not be treated as
1639 // an import in this sense.
1640 if (Line.First->is(tok::kw_export) &&
1641 CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
1642 CurrentToken->Next->isStringLiteral()) {
1643 ImportStatement = true;
1644 }
1645 if (isClosureImportStatement(*CurrentToken))
1646 ImportStatement = true;
1647 }
1648 if (!consumeToken())
1649 return LT_Invalid;
1650 }
1651 if (KeywordVirtualFound)
1653 if (ImportStatement)
1654 return LT_ImportStatement;
1655
1656 if (Line.startsWith(TT_ObjCMethodSpecifier)) {
1657 if (Contexts.back().FirstObjCSelectorName) {
1658 Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
1659 Contexts.back().LongestObjCSelectorName;
1660 }
1661 return LT_ObjCMethodDecl;
1662 }
1663
1664 for (const auto &ctx : Contexts)
1665 if (ctx.ContextType == Context::StructArrayInitializer)
1667
1668 return LT_Other;
1669 }
1670
1671private:
1672 bool isClosureImportStatement(const FormatToken &Tok) {
1673 // FIXME: Closure-library specific stuff should not be hard-coded but be
1674 // configurable.
1675 return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
1676 Tok.Next->Next &&
1677 (Tok.Next->Next->TokenText == "module" ||
1678 Tok.Next->Next->TokenText == "provide" ||
1679 Tok.Next->Next->TokenText == "require" ||
1680 Tok.Next->Next->TokenText == "requireType" ||
1681 Tok.Next->Next->TokenText == "forwardDeclare") &&
1682 Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
1683 }
1684
1685 void resetTokenMetadata() {
1686 if (!CurrentToken)
1687 return;
1688
1689 // Reset token type in case we have already looked at it and then
1690 // recovered from an error (e.g. failure to find the matching >).
1691 if (!CurrentToken->isTypeFinalized() &&
1692 !CurrentToken->isOneOf(
1693 TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
1694 TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
1695 TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
1696 TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
1697 TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
1698 TT_UntouchableMacroFunc, TT_StatementAttributeLikeMacro,
1699 TT_FunctionLikeOrFreestandingMacro, TT_ClassLBrace, TT_EnumLBrace,
1700 TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
1701 TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
1702 TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
1703 TT_BracedListLBrace)) {
1704 CurrentToken->setType(TT_Unknown);
1705 }
1706 CurrentToken->Role.reset();
1707 CurrentToken->MatchingParen = nullptr;
1708 CurrentToken->FakeLParens.clear();
1709 CurrentToken->FakeRParens = 0;
1710 }
1711
1712 void next() {
1713 if (!CurrentToken)
1714 return;
1715
1716 CurrentToken->NestingLevel = Contexts.size() - 1;
1717 CurrentToken->BindingStrength = Contexts.back().BindingStrength;
1718 modifyContext(*CurrentToken);
1719 determineTokenType(*CurrentToken);
1720 CurrentToken = CurrentToken->Next;
1721
1722 resetTokenMetadata();
1723 }
1724
1725 /// A struct to hold information valid in a specific context, e.g.
1726 /// a pair of parenthesis.
1727 struct Context {
1728 Context(tok::TokenKind ContextKind, unsigned BindingStrength,
1729 bool IsExpression)
1730 : ContextKind(ContextKind), BindingStrength(BindingStrength),
1732
1733 tok::TokenKind ContextKind;
1740 FormatToken *FirstObjCSelectorName = nullptr;
1741 FormatToken *FirstStartOfName = nullptr;
1742 bool CanBeExpression = true;
1743 bool CaretFound = false;
1747 // Whether the braces may mean concatenation instead of structure or array
1748 // literal.
1750 enum {
1751 Unknown,
1752 // Like the part after `:` in a constructor.
1753 // Context(...) : IsExpression(IsExpression)
1754 CtorInitializer,
1755 // Like in the parentheses in a foreach.
1756 ForEachMacro,
1757 // Like the inheritance list in a class declaration.
1758 // class Input : public IO
1759 InheritanceList,
1760 // Like in the braced list.
1761 // int x[] = {};
1762 StructArrayInitializer,
1763 // Like in `static_cast<int>`.
1764 TemplateArgument,
1765 // C11 _Generic selection.
1766 C11GenericSelection,
1767 // Like in the outer parentheses in `ffnand ff1(.q());`.
1768 VerilogInstancePortList,
1770 };
1771
1772 /// Puts a new \c Context onto the stack \c Contexts for the lifetime
1773 /// of each instance.
1774 struct ScopedContextCreator {
1775 AnnotatingParser &P;
1776
1777 ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
1778 unsigned Increase)
1779 : P(P) {
1780 P.Contexts.push_back(Context(ContextKind,
1781 P.Contexts.back().BindingStrength + Increase,
1782 P.Contexts.back().IsExpression));
1783 }
1784
1785 ~ScopedContextCreator() {
1786 if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1787 if (P.Contexts.back().ContextType == Context::StructArrayInitializer) {
1788 P.Contexts.pop_back();
1789 P.Contexts.back().ContextType = Context::StructArrayInitializer;
1790 return;
1791 }
1792 }
1793 P.Contexts.pop_back();
1794 }
1795 };
1796
1797 void modifyContext(const FormatToken &Current) {
1798 auto AssignmentStartsExpression = [&]() {
1799 if (Current.getPrecedence() != prec::Assignment)
1800 return false;
1801
1802 if (Line.First->isOneOf(tok::kw_using, tok::kw_return))
1803 return false;
1804 if (Line.First->is(tok::kw_template)) {
1805 assert(Current.Previous);
1806 if (Current.Previous->is(tok::kw_operator)) {
1807 // `template ... operator=` cannot be an expression.
1808 return false;
1809 }
1810
1811 // `template` keyword can start a variable template.
1812 const FormatToken *Tok = Line.First->getNextNonComment();
1813 assert(Tok); // Current token is on the same line.
1814 if (Tok->isNot(TT_TemplateOpener)) {
1815 // Explicit template instantiations do not have `<>`.
1816 return false;
1817 }
1818
1819 // This is the default value of a template parameter, determine if it's
1820 // type or non-type.
1821 if (Contexts.back().ContextKind == tok::less) {
1822 assert(Current.Previous->Previous);
1823 return !Current.Previous->Previous->isOneOf(tok::kw_typename,
1824 tok::kw_class);
1825 }
1826
1827 Tok = Tok->MatchingParen;
1828 if (!Tok)
1829 return false;
1830 Tok = Tok->getNextNonComment();
1831 if (!Tok)
1832 return false;
1833
1834 if (Tok->isOneOf(tok::kw_class, tok::kw_enum, tok::kw_struct,
1835 tok::kw_using)) {
1836 return false;
1837 }
1838
1839 return true;
1840 }
1841
1842 // Type aliases use `type X = ...;` in TypeScript and can be exported
1843 // using `export type ...`.
1844 if (Style.isJavaScript() &&
1845 (Line.startsWith(Keywords.kw_type, tok::identifier) ||
1846 Line.startsWith(tok::kw_export, Keywords.kw_type,
1847 tok::identifier))) {
1848 return false;
1849 }
1850
1851 return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
1852 };
1853
1854 if (AssignmentStartsExpression()) {
1855 Contexts.back().IsExpression = true;
1856 if (!Line.startsWith(TT_UnaryOperator)) {
1857 for (FormatToken *Previous = Current.Previous;
1858 Previous && Previous->Previous &&
1859 !Previous->Previous->isOneOf(tok::comma, tok::semi);
1860 Previous = Previous->Previous) {
1861 if (Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
1862 Previous = Previous->MatchingParen;
1863 if (!Previous)
1864 break;
1865 }
1866 if (Previous->opensScope())
1867 break;
1868 if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
1869 Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
1870 Previous->Previous && Previous->Previous->isNot(tok::equal)) {
1871 Previous->setType(TT_PointerOrReference);
1872 }
1873 }
1874 }
1875 } else if (Current.is(tok::lessless) &&
1876 (!Current.Previous ||
1877 Current.Previous->isNot(tok::kw_operator))) {
1878 Contexts.back().IsExpression = true;
1879 } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
1880 Contexts.back().IsExpression = true;
1881 } else if (Current.is(TT_TrailingReturnArrow)) {
1882 Contexts.back().IsExpression = false;
1883 } else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) {
1884 Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
1885 } else if (Current.Previous &&
1886 Current.Previous->is(TT_CtorInitializerColon)) {
1887 Contexts.back().IsExpression = true;
1888 Contexts.back().ContextType = Context::CtorInitializer;
1889 } else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
1890 Contexts.back().ContextType = Context::InheritanceList;
1891 } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
1892 for (FormatToken *Previous = Current.Previous;
1893 Previous && Previous->isOneOf(tok::star, tok::amp);
1894 Previous = Previous->Previous) {
1895 Previous->setType(TT_PointerOrReference);
1896 }
1897 if (Line.MustBeDeclaration &&
1898 Contexts.front().ContextType != Context::CtorInitializer) {
1899 Contexts.back().IsExpression = false;
1900 }
1901 } else if (Current.is(tok::kw_new)) {
1902 Contexts.back().CanBeExpression = false;
1903 } else if (Current.is(tok::semi) ||
1904 (Current.is(tok::exclaim) && Current.Previous &&
1905 Current.Previous->isNot(tok::kw_operator))) {
1906 // This should be the condition or increment in a for-loop.
1907 // But not operator !() (can't use TT_OverloadedOperator here as its not
1908 // been annotated yet).
1909 Contexts.back().IsExpression = true;
1910 }
1911 }
1912
1913 static FormatToken *untilMatchingParen(FormatToken *Current) {
1914 // Used when `MatchingParen` is not yet established.
1915 int ParenLevel = 0;
1916 while (Current) {
1917 if (Current->is(tok::l_paren))
1918 ++ParenLevel;
1919 if (Current->is(tok::r_paren))
1920 --ParenLevel;
1921 if (ParenLevel < 1)
1922 break;
1923 Current = Current->Next;
1924 }
1925 return Current;
1926 }
1927
1928 static bool isDeductionGuide(FormatToken &Current) {
1929 // Look for a deduction guide template<T> A(...) -> A<...>;
1930 if (Current.Previous && Current.Previous->is(tok::r_paren) &&
1931 Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
1932 // Find the TemplateCloser.
1933 FormatToken *TemplateCloser = Current.Next->Next;
1934 int NestingLevel = 0;
1935 while (TemplateCloser) {
1936 // Skip over an expressions in parens A<(3 < 2)>;
1937 if (TemplateCloser->is(tok::l_paren)) {
1938 // No Matching Paren yet so skip to matching paren
1939 TemplateCloser = untilMatchingParen(TemplateCloser);
1940 if (!TemplateCloser)
1941 break;
1942 }
1943 if (TemplateCloser->is(tok::less))
1944 ++NestingLevel;
1945 if (TemplateCloser->is(tok::greater))
1946 --NestingLevel;
1947 if (NestingLevel < 1)
1948 break;
1949 TemplateCloser = TemplateCloser->Next;
1950 }
1951 // Assuming we have found the end of the template ensure its followed
1952 // with a semi-colon.
1953 if (TemplateCloser && TemplateCloser->Next &&
1954 TemplateCloser->Next->is(tok::semi) &&
1955 Current.Previous->MatchingParen) {
1956 // Determine if the identifier `A` prior to the A<..>; is the same as
1957 // prior to the A(..)
1958 FormatToken *LeadingIdentifier =
1959 Current.Previous->MatchingParen->Previous;
1960
1961 return LeadingIdentifier &&
1962 LeadingIdentifier->TokenText == Current.Next->TokenText;
1963 }
1964 }
1965 return false;
1966 }
1967
1968 void determineTokenType(FormatToken &Current) {
1969 if (Current.isNot(TT_Unknown)) {
1970 // The token type is already known.
1971 return;
1972 }
1973
1974 if ((Style.isJavaScript() || Style.isCSharp()) &&
1975 Current.is(tok::exclaim)) {
1976 if (Current.Previous) {
1977 bool IsIdentifier =
1978 Style.isJavaScript()
1979 ? Keywords.IsJavaScriptIdentifier(
1980 *Current.Previous, /* AcceptIdentifierName= */ true)
1981 : Current.Previous->is(tok::identifier);
1982 if (IsIdentifier ||
1983 Current.Previous->isOneOf(
1984 tok::kw_default, tok::kw_namespace, tok::r_paren, tok::r_square,
1985 tok::r_brace, tok::kw_false, tok::kw_true, Keywords.kw_type,
1986 Keywords.kw_get, Keywords.kw_init, Keywords.kw_set) ||
1987 Current.Previous->Tok.isLiteral()) {
1988 Current.setType(TT_NonNullAssertion);
1989 return;
1990 }
1991 }
1992 if (Current.Next &&
1993 Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
1994 Current.setType(TT_NonNullAssertion);
1995 return;
1996 }
1997 }
1998
1999 // Line.MightBeFunctionDecl can only be true after the parentheses of a
2000 // function declaration have been found. In this case, 'Current' is a
2001 // trailing token of this declaration and thus cannot be a name.
2002 if (Current.is(Keywords.kw_instanceof)) {
2003 Current.setType(TT_BinaryOperator);
2004 } else if (isStartOfName(Current) &&
2005 (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
2006 Contexts.back().FirstStartOfName = &Current;
2007 Current.setType(TT_StartOfName);
2008 } else if (Current.is(tok::semi)) {
2009 // Reset FirstStartOfName after finding a semicolon so that a for loop
2010 // with multiple increment statements is not confused with a for loop
2011 // having multiple variable declarations.
2012 Contexts.back().FirstStartOfName = nullptr;
2013 } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
2014 AutoFound = true;
2015 } else if (Current.is(tok::arrow) &&
2016 Style.Language == FormatStyle::LK_Java) {
2017 Current.setType(TT_LambdaArrow);
2018 } else if (Current.is(tok::arrow) && AutoFound &&
2019 (Line.MightBeFunctionDecl || Line.InPPDirective) &&
2020 Current.NestingLevel == 0 &&
2021 !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
2022 // not auto operator->() -> xxx;
2023 Current.setType(TT_TrailingReturnArrow);
2024 } else if (Current.is(tok::arrow) && Current.Previous &&
2025 Current.Previous->is(tok::r_brace)) {
2026 // Concept implicit conversion constraint needs to be treated like
2027 // a trailing return type ... } -> <type>.
2028 Current.setType(TT_TrailingReturnArrow);
2029 } else if (isDeductionGuide(Current)) {
2030 // Deduction guides trailing arrow " A(...) -> A<T>;".
2031 Current.setType(TT_TrailingReturnArrow);
2032 } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
2033 Current.setType(determineStarAmpUsage(
2034 Current,
2035 Contexts.back().CanBeExpression && Contexts.back().IsExpression,
2036 Contexts.back().ContextType == Context::TemplateArgument));
2037 } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret) ||
2038 (Style.isVerilog() && Current.is(tok::pipe))) {
2039 Current.setType(determinePlusMinusCaretUsage(Current));
2040 if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
2041 Contexts.back().CaretFound = true;
2042 } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
2043 Current.setType(determineIncrementUsage(Current));
2044 } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
2045 Current.setType(TT_UnaryOperator);
2046 } else if (Current.is(tok::question)) {
2047 if (Style.isJavaScript() && Line.MustBeDeclaration &&
2048 !Contexts.back().IsExpression) {
2049 // In JavaScript, `interface X { foo?(): bar; }` is an optional method
2050 // on the interface, not a ternary expression.
2051 Current.setType(TT_JsTypeOptionalQuestion);
2052 } else {
2053 Current.setType(TT_ConditionalExpr);
2054 }
2055 } else if (Current.isBinaryOperator() &&
2056 (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
2057 (Current.isNot(tok::greater) &&
2058 Style.Language != FormatStyle::LK_TextProto)) {
2059 if (Style.isVerilog()) {
2060 if (Current.is(tok::lessequal) && Contexts.size() == 1 &&
2061 !Contexts.back().VerilogAssignmentFound) {
2062 // In Verilog `<=` is assignment if in its own statement. It is a
2063 // statement instead of an expression, that is it can not be chained.
2064 Current.ForcedPrecedence = prec::Assignment;
2065 Current.setFinalizedType(TT_BinaryOperator);
2066 }
2067 if (Current.getPrecedence() == prec::Assignment)
2068 Contexts.back().VerilogAssignmentFound = true;
2069 }
2070 Current.setType(TT_BinaryOperator);
2071 } else if (Current.is(tok::comment)) {
2072 if (Current.TokenText.startswith("/*")) {
2073 if (Current.TokenText.endswith("*/")) {
2074 Current.setType(TT_BlockComment);
2075 } else {
2076 // The lexer has for some reason determined a comment here. But we
2077 // cannot really handle it, if it isn't properly terminated.
2078 Current.Tok.setKind(tok::unknown);
2079 }
2080 } else {
2081 Current.setType(TT_LineComment);
2082 }
2083 } else if (Current.is(tok::string_literal)) {
2084 if (Style.isVerilog() && Contexts.back().VerilogMayBeConcatenation &&
2085 Current.getPreviousNonComment() &&
2086 Current.getPreviousNonComment()->isOneOf(tok::comma, tok::l_brace) &&
2087 Current.getNextNonComment() &&
2088 Current.getNextNonComment()->isOneOf(tok::comma, tok::r_brace)) {
2089 Current.setType(TT_StringInConcatenation);
2090 }
2091 } else if (Current.is(tok::l_paren)) {
2092 if (lParenStartsCppCast(Current))
2093 Current.setType(TT_CppCastLParen);
2094 } else if (Current.is(tok::r_paren)) {
2095 if (rParenEndsCast(Current))
2096 Current.setType(TT_CastRParen);
2097 if (Current.MatchingParen && Current.Next &&
2098 !Current.Next->isBinaryOperator() &&
2099 !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
2100 tok::comma, tok::period, tok::arrow,
2101 tok::coloncolon, tok::kw_noexcept)) {
2102 if (FormatToken *AfterParen = Current.MatchingParen->Next) {
2103 // Make sure this isn't the return type of an Obj-C block declaration
2104 if (AfterParen->isNot(tok::caret)) {
2105 if (FormatToken *BeforeParen = Current.MatchingParen->Previous) {
2106 if (BeforeParen->is(tok::identifier) &&
2107 BeforeParen->isNot(TT_TypenameMacro) &&
2108 BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
2109 (!BeforeParen->Previous ||
2110 BeforeParen->Previous->ClosesTemplateDeclaration)) {
2111 Current.setType(TT_FunctionAnnotationRParen);
2112 }
2113 }
2114 }
2115 }
2116 }
2117 } else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
2118 Style.Language != FormatStyle::LK_Java) {
2119 // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
2120 // marks declarations and properties that need special formatting.
2121 switch (Current.Next->Tok.getObjCKeywordID()) {
2122 case tok::objc_interface:
2123 case tok::objc_implementation:
2124 case tok::objc_protocol:
2125 Current.setType(TT_ObjCDecl);
2126 break;
2127 case tok::objc_property:
2128 Current.setType(TT_ObjCProperty);
2129 break;
2130 default:
2131 break;
2132 }
2133 } else if (Current.is(tok::period)) {
2134 FormatToken *PreviousNoComment = Current.getPreviousNonComment();
2135 if (PreviousNoComment &&
2136 PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) {
2137 Current.setType(TT_DesignatedInitializerPeriod);
2138 } else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
2139 Current.Previous->isOneOf(TT_JavaAnnotation,
2140 TT_LeadingJavaAnnotation)) {
2141 Current.setType(Current.Previous->getType());
2142 }
2143 } else if (canBeObjCSelectorComponent(Current) &&
2144 // FIXME(bug 36976): ObjC return types shouldn't use
2145 // TT_CastRParen.
2146 Current.Previous && Current.Previous->is(TT_CastRParen) &&
2147 Current.Previous->MatchingParen &&
2148 Current.Previous->MatchingParen->Previous &&
2149 Current.Previous->MatchingParen->Previous->is(
2150 TT_ObjCMethodSpecifier)) {
2151 // This is the first part of an Objective-C selector name. (If there's no
2152 // colon after this, this is the only place which annotates the identifier
2153 // as a selector.)
2154 Current.setType(TT_SelectorName);
2155 } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
2156 tok::kw_requires) &&
2157 Current.Previous &&
2158 !Current.Previous->isOneOf(tok::equal, tok::at,
2159 TT_CtorInitializerComma,
2160 TT_CtorInitializerColon) &&
2161 Line.MightBeFunctionDecl && Contexts.size() == 1) {
2162 // Line.MightBeFunctionDecl can only be true after the parentheses of a
2163 // function declaration have been found.
2164 Current.setType(TT_TrailingAnnotation);
2165 } else if ((Style.Language == FormatStyle::LK_Java ||
2166 Style.isJavaScript()) &&
2167 Current.Previous) {
2168 if (Current.Previous->is(tok::at) &&
2169 Current.isNot(Keywords.kw_interface)) {
2170 const FormatToken &AtToken = *Current.Previous;
2171 const FormatToken *Previous = AtToken.getPreviousNonComment();
2172 if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
2173 Current.setType(TT_LeadingJavaAnnotation);
2174 else
2175 Current.setType(TT_JavaAnnotation);
2176 } else if (Current.Previous->is(tok::period) &&
2177 Current.Previous->isOneOf(TT_JavaAnnotation,
2178 TT_LeadingJavaAnnotation)) {
2179 Current.setType(Current.Previous->getType());
2180 }
2181 }
2182 }
2183
2184 /// Take a guess at whether \p Tok starts a name of a function or
2185 /// variable declaration.
2186 ///
2187 /// This is a heuristic based on whether \p Tok is an identifier following
2188 /// something that is likely a type.
2189 bool isStartOfName(const FormatToken &Tok) {
2190 // Handled in ExpressionParser for Verilog.
2191 if (Style.isVerilog())
2192 return false;
2193
2194 if (Tok.isNot(tok::identifier) || !Tok.Previous)
2195 return false;
2196
2197 if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
2198 Keywords.kw_as)) {
2199 return false;
2200 }
2201 if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
2202 return false;
2203
2204 // Skip "const" as it does not have an influence on whether this is a name.
2205 FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
2206
2207 // For javascript const can be like "let" or "var"
2208 if (!Style.isJavaScript())
2209 while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
2210 PreviousNotConst = PreviousNotConst->getPreviousNonComment();
2211
2212 if (!PreviousNotConst)
2213 return false;
2214
2215 if (PreviousNotConst->ClosesRequiresClause)
2216 return false;
2217
2218 bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
2219 PreviousNotConst->Previous &&
2220 PreviousNotConst->Previous->is(tok::hash);
2221
2222 if (PreviousNotConst->is(TT_TemplateCloser)) {
2223 return PreviousNotConst && PreviousNotConst->MatchingParen &&
2224 PreviousNotConst->MatchingParen->Previous &&
2225 PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2226 PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2227 }
2228
2229 if (PreviousNotConst->is(tok::r_paren) &&
2230 PreviousNotConst->is(TT_TypeDeclarationParen)) {
2231 return true;
2232 }
2233
2234 // If is a preprocess keyword like #define.
2235 if (IsPPKeyword)
2236 return false;
2237
2238 // int a or auto a.
2239 if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto))
2240 return true;
2241
2242 // *a or &a or &&a.
2243 if (PreviousNotConst->is(TT_PointerOrReference))
2244 return true;
2245
2246 // MyClass a;
2247 if (PreviousNotConst->isSimpleTypeSpecifier())
2248 return true;
2249
2250 // type[] a in Java
2251 if (Style.Language == FormatStyle::LK_Java &&
2252 PreviousNotConst->is(tok::r_square)) {
2253 return true;
2254 }
2255
2256 // const a = in JavaScript.
2257 return Style.isJavaScript() && PreviousNotConst->is(tok::kw_const);
2258 }
2259
2260 /// Determine whether '(' is starting a C++ cast.
2261 bool lParenStartsCppCast(const FormatToken &Tok) {
2262 // C-style casts are only used in C++.
2263 if (!Style.isCpp())
2264 return false;
2265
2266 FormatToken *LeftOfParens = Tok.getPreviousNonComment();
2267 if (LeftOfParens && LeftOfParens->is(TT_TemplateCloser) &&
2268 LeftOfParens->MatchingParen) {
2269 auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
2270 if (Prev &&
2271 Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
2272 tok::kw_reinterpret_cast, tok::kw_static_cast)) {
2273 // FIXME: Maybe we should handle identifiers ending with "_cast",
2274 // e.g. any_cast?
2275 return true;
2276 }
2277 }
2278 return false;
2279 }
2280
2281 /// Determine whether ')' is ending a cast.
2282 bool rParenEndsCast(const FormatToken &Tok) {
2283 // C-style casts are only used in C++, C# and Java.
2284 if (!Style.isCSharp() && !Style.isCpp() &&
2285 Style.Language != FormatStyle::LK_Java) {
2286 return false;
2287 }
2288
2289 // Empty parens aren't casts and there are no casts at the end of the line.
2290 if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
2291 return false;
2292
2293 if (Tok.MatchingParen->is(TT_OverloadedOperatorLParen))
2294 return false;
2295
2296 FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
2297 if (LeftOfParens) {
2298 // If there is a closing parenthesis left of the current
2299 // parentheses, look past it as these might be chained casts.
2300 if (LeftOfParens->is(tok::r_paren) &&
2301 LeftOfParens->isNot(TT_CastRParen)) {
2302 if (!LeftOfParens->MatchingParen ||
2303 !LeftOfParens->MatchingParen->Previous) {
2304 return false;
2305 }
2306 LeftOfParens = LeftOfParens->MatchingParen->Previous;
2307 }
2308
2309 if (LeftOfParens->is(tok::r_square)) {
2310 // delete[] (void *)ptr;
2311 auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
2312 if (Tok->isNot(tok::r_square))
2313 return nullptr;
2314
2315 Tok = Tok->getPreviousNonComment();
2316 if (!Tok || Tok->isNot(tok::l_square))
2317 return nullptr;
2318
2319 Tok = Tok->getPreviousNonComment();
2320 if (!Tok || Tok->isNot(tok::kw_delete))
2321 return nullptr;
2322 return Tok;
2323 };
2324 if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
2325 LeftOfParens = MaybeDelete;
2326 }
2327
2328 // The Condition directly below this one will see the operator arguments
2329 // as a (void *foo) cast.
2330 // void operator delete(void *foo) ATTRIB;
2331 if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
2332 LeftOfParens->Previous->is(tok::kw_operator)) {
2333 return false;
2334 }
2335
2336 // If there is an identifier (or with a few exceptions a keyword) right
2337 // before the parentheses, this is unlikely to be a cast.
2338 if (LeftOfParens->Tok.getIdentifierInfo() &&
2339 !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
2340 tok::kw_delete, tok::kw_throw)) {
2341 return false;
2342 }
2343
2344 // Certain other tokens right before the parentheses are also signals that
2345 // this cannot be a cast.
2346 if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
2347 TT_TemplateCloser, tok::ellipsis)) {
2348 return false;
2349 }
2350 }
2351
2352 if (Tok.Next->is(tok::question))
2353 return false;
2354
2355 // `foreach((A a, B b) in someList)` should not be seen as a cast.
2356 if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
2357 return false;
2358
2359 // Functions which end with decorations like volatile, noexcept are unlikely
2360 // to be casts.
2361 if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2362 tok::kw_requires, tok::kw_throw, tok::arrow,
2363 Keywords.kw_override, Keywords.kw_final) ||
2364 isCppAttribute(Style.isCpp(), *Tok.Next)) {
2365 return false;
2366 }
2367
2368 // As Java has no function types, a "(" after the ")" likely means that this
2369 // is a cast.
2370 if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
2371 return true;
2372
2373 // If a (non-string) literal follows, this is likely a cast.
2374 if (Tok.Next->isNot(tok::string_literal) &&
2375 (Tok.Next->Tok.isLiteral() ||
2376 Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof))) {
2377 return true;
2378 }
2379
2380 // Heuristically try to determine whether the parentheses contain a type.
2381 auto IsQualifiedPointerOrReference = [](FormatToken *T) {
2382 // This is used to handle cases such as x = (foo *const)&y;
2383 assert(!T->isSimpleTypeSpecifier() && "Should have already been checked");
2384 // Strip trailing qualifiers such as const or volatile when checking
2385 // whether the parens could be a cast to a pointer/reference type.
2386 while (T) {
2387 if (T->is(TT_AttributeRParen)) {
2388 // Handle `x = (foo *__attribute__((foo)))&v;`:
2389 assert(T->is(tok::r_paren));
2390 assert(T->MatchingParen);
2391 assert(T->MatchingParen->is(tok::l_paren));
2392 assert(T->MatchingParen->is(TT_AttributeLParen));
2393 if (const auto *Tok = T->MatchingParen->Previous;
2394 Tok && Tok->isAttribute()) {
2395 T = Tok->Previous;
2396 continue;
2397 }
2398 } else if (T->is(TT_AttributeSquare)) {
2399 // Handle `x = (foo *[[clang::foo]])&v;`:
2400 if (T->MatchingParen && T->MatchingParen->Previous) {
2401 T = T->MatchingParen->Previous;
2402 continue;
2403 }
2404 } else if (T->canBePointerOrReferenceQualifier()) {
2405 T = T->Previous;
2406 continue;
2407 }
2408 break;
2409 }
2410 return T && T->is(TT_PointerOrReference);
2411 };
2412 bool ParensAreType =
2413 !Tok.Previous ||
2414 Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
2415 Tok.Previous->isSimpleTypeSpecifier() ||
2416 IsQualifiedPointerOrReference(Tok.Previous);
2417 bool ParensCouldEndDecl =
2418 Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
2419 if (ParensAreType && !ParensCouldEndDecl)
2420 return true;
2421
2422 // At this point, we heuristically assume that there are no casts at the
2423 // start of the line. We assume that we have found most cases where there
2424 // are by the logic above, e.g. "(void)x;".
2425 if (!LeftOfParens)
2426 return false;
2427
2428 // Certain token types inside the parentheses mean that this can't be a
2429 // cast.
2430 for (const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
2431 Token = Token->Next) {
2432 if (Token->is(TT_BinaryOperator))
2433 return false;
2434 }
2435
2436 // If the following token is an identifier or 'this', this is a cast. All
2437 // cases where this can be something else are handled above.
2438 if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
2439 return true;
2440
2441 // Look for a cast `( x ) (`.
2442 if (Tok.Next->is(tok::l_paren) && Tok.Previous && Tok.Previous->Previous) {
2443 if (Tok.Previous->is(tok::identifier) &&
2444 Tok.Previous->Previous->is(tok::l_paren)) {
2445 return true;
2446 }
2447 }
2448
2449 if (!Tok.Next->Next)
2450 return false;
2451
2452 // If the next token after the parenthesis is a unary operator, assume
2453 // that this is cast, unless there are unexpected tokens inside the
2454 // parenthesis.
2455 const bool NextIsAmpOrStar = Tok.Next->isOneOf(tok::amp, tok::star);
2456 if (!(Tok.Next->isUnaryOperator() || NextIsAmpOrStar) ||
2457 Tok.Next->is(tok::plus) ||
2458 !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
2459 return false;
2460 }
2461 if (NextIsAmpOrStar &&
2462 (Tok.Next->Next->is(tok::numeric_constant) || Line.InPPDirective)) {
2463 return false;
2464 }
2465 // Search for unexpected tokens.
2466 for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
2467 Prev = Prev->Previous) {
2468 if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2469 return false;
2470 }
2471 return true;
2472 }
2473
2474 /// Returns true if the token is used as a unary operator.
2475 bool determineUnaryOperatorByUsage(const FormatToken &Tok) {
2476 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2477 if (!PrevToken)
2478 return true;
2479
2480 // These keywords are deliberately not included here because they may
2481 // precede only one of unary star/amp and plus/minus but not both. They are
2482 // either included in determineStarAmpUsage or determinePlusMinusCaretUsage.
2483 //
2484 // @ - It may be followed by a unary `-` in Objective-C literals. We don't
2485 // know how they can be followed by a star or amp.
2486 if (PrevToken->isOneOf(
2487 TT_ConditionalExpr, tok::l_paren, tok::comma, tok::colon, tok::semi,
2488 tok::equal, tok::question, tok::l_square, tok::l_brace,
2489 tok::kw_case, tok::kw_co_await, tok::kw_co_return, tok::kw_co_yield,
2490 tok::kw_delete, tok::kw_return, tok::kw_throw)) {
2491 return true;
2492 }
2493
2494 // We put sizeof here instead of only in determineStarAmpUsage. In the cases
2495 // where the unary `+` operator is overloaded, it is reasonable to write
2496 // things like `sizeof +x`. Like commit 446d6ec996c6c3.
2497 if (PrevToken->is(tok::kw_sizeof))
2498 return true;
2499
2500 // A sequence of leading unary operators.
2501 if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2502 return true;
2503
2504 // There can't be two consecutive binary operators.
2505 if (PrevToken->is(TT_BinaryOperator))
2506 return true;
2507
2508 return false;
2509 }
2510
2511 /// Return the type of the given token assuming it is * or &.
2512 TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
2513 bool InTemplateArgument) {
2514 if (Style.isJavaScript())
2515 return TT_BinaryOperator;
2516
2517 // && in C# must be a binary operator.
2518 if (Style.isCSharp() && Tok.is(tok::ampamp))
2519 return TT_BinaryOperator;
2520
2521 if (Style.isVerilog()) {
2522 // In Verilog, `*` can only be a binary operator. `&` can be either unary
2523 // or binary. `*` also includes `*>` in module path declarations in
2524 // specify blocks because merged tokens take the type of the first one by
2525 // default.
2526 if (Tok.is(tok::star))
2527 return TT_BinaryOperator;
2528 return determineUnaryOperatorByUsage(Tok) ? TT_UnaryOperator
2529 : TT_BinaryOperator;
2530 }
2531
2532 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2533 if (!PrevToken)
2534 return TT_UnaryOperator;
2535 if (PrevToken->is(TT_TypeName))
2536 return TT_PointerOrReference;
2537
2538 const FormatToken *NextToken = Tok.getNextNonComment();
2539
2540 if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
2541 return TT_BinaryOperator;
2542
2543 if (!NextToken ||
2544 NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
2545 TT_RequiresClause) ||
2546 (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
2547 NextToken->canBePointerOrReferenceQualifier() ||
2548 (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
2549 return TT_PointerOrReference;
2550 }
2551
2552 if (PrevToken->is(tok::coloncolon))
2553 return TT_PointerOrReference;
2554
2555 if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
2556 return TT_PointerOrReference;
2557
2558 if (determineUnaryOperatorByUsage(Tok))
2559 return TT_UnaryOperator;
2560
2561 if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
2562 return TT_PointerOrReference;
2563 if (NextToken->is(tok::kw_operator) && !IsExpression)
2564 return TT_PointerOrReference;
2565 if (NextToken->isOneOf(tok::comma, tok::semi))
2566 return TT_PointerOrReference;
2567
2568 // After right braces, star tokens are likely to be pointers to struct,
2569 // union, or class.
2570 // struct {} *ptr;
2571 // This by itself is not sufficient to distinguish from multiplication
2572 // following a brace-initialized expression, as in:
2573 // int i = int{42} * 2;
2574 // In the struct case, the part of the struct declaration until the `{` and
2575 // the `}` are put on separate unwrapped lines; in the brace-initialized
2576 // case, the matching `{` is on the same unwrapped line, so check for the
2577 // presence of the matching brace to distinguish between those.
2578 if (PrevToken->is(tok::r_brace) && Tok.is(tok::star) &&
2579 !PrevToken->MatchingParen) {
2580 return TT_PointerOrReference;
2581 }
2582
2583 if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
2584 return TT_UnaryOperator;
2585
2586 if (PrevToken->Tok.isLiteral() ||
2587 PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
2588 tok::kw_false, tok::r_brace)) {
2589 return TT_BinaryOperator;
2590 }
2591
2592 const FormatToken *NextNonParen = NextToken;
2593 while (NextNonParen && NextNonParen->is(tok::l_paren))
2594 NextNonParen = NextNonParen->getNextNonComment();
2595 if (NextNonParen && (NextNonParen->Tok.isLiteral() ||
2596 NextNonParen->isOneOf(tok::kw_true, tok::kw_false) ||
2597 NextNonParen->isUnaryOperator())) {
2598 return TT_BinaryOperator;
2599 }
2600
2601 // If we know we're in a template argument, there are no named declarations.
2602 // Thus, having an identifier on the right-hand side indicates a binary
2603 // operator.
2604 if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
2605 return TT_BinaryOperator;
2606
2607 // "&&" followed by "(", "*", or "&" is quite unlikely to be two successive
2608 // unary "&".
2609 if (Tok.is(tok::ampamp) &&
2610 NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
2611 return TT_BinaryOperator;
2612 }
2613
2614 // This catches some cases where evaluation order is used as control flow:
2615 // aaa && aaa->f();
2616 if (NextToken->Tok.isAnyIdentifier()) {
2617 const FormatToken *NextNextToken = NextToken->getNextNonComment();
2618 if (NextNextToken && NextNextToken->is(tok::arrow))
2619 return TT_BinaryOperator;
2620 }
2621
2622 // It is very unlikely that we are going to find a pointer or reference type
2623 // definition on the RHS of an assignment.
2624 if (IsExpression && !Contexts.back().CaretFound)
2625 return TT_BinaryOperator;
2626
2627 // Opeartors at class scope are likely pointer or reference members.
2628 if (!Scopes.empty() && Scopes.back() == ST_Class)
2629 return TT_PointerOrReference;
2630
2631 // Tokens that indicate member access or chained operator& use.
2632 auto IsChainedOperatorAmpOrMember = [](const FormatToken *token) {
2633 return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
2634 tok::arrowstar, tok::periodstar);
2635 };
2636
2637 // It's more likely that & represents operator& than an uninitialized
2638 // reference.
2639 if (Tok.is(tok::amp) && PrevToken && PrevToken->Tok.isAnyIdentifier() &&
2640 IsChainedOperatorAmpOrMember(PrevToken->getPreviousNonComment()) &&
2641 NextToken && NextToken->Tok.isAnyIdentifier()) {
2642 if (auto NextNext = NextToken->getNextNonComment();
2643 NextNext &&
2644 (IsChainedOperatorAmpOrMember(NextNext) || NextNext->is(tok::semi))) {
2645 return TT_BinaryOperator;
2646 }
2647 }
2648
2649 return TT_PointerOrReference;
2650 }
2651
2652 TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
2653 if (determineUnaryOperatorByUsage(Tok))
2654 return TT_UnaryOperator;
2655
2656 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2657 if (!PrevToken)
2658 return TT_UnaryOperator;
2659
2660 if (PrevToken->is(tok::at))
2661 return TT_UnaryOperator;
2662
2663 // Fall back to marking the token as binary operator.
2664 return TT_BinaryOperator;
2665 }
2666
2667 /// Determine whether ++/-- are pre- or post-increments/-decrements.
2668 TokenType determineIncrementUsage(const FormatToken &Tok) {
2669 const FormatToken *PrevToken = Tok.getPreviousNonComment();
2670 if (!PrevToken || PrevToken->is(TT_CastRParen))
2671 return TT_UnaryOperator;
2672 if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
2673 return TT_TrailingUnaryOperator;
2674
2675 return TT_UnaryOperator;
2676 }
2677
2678 SmallVector<Context, 8> Contexts;
2679
2680 const FormatStyle &Style;
2681 AnnotatedLine &Line;
2682 FormatToken *CurrentToken;
2683 bool AutoFound;
2684 const AdditionalKeywords &Keywords;
2685
2686 SmallVector<ScopeType> &Scopes;
2687
2688 // Set of "<" tokens that do not open a template parameter list. If parseAngle
2689 // determines that a specific token can't be a template opener, it will make
2690 // same decision irrespective of the decisions for tokens leading up to it.
2691 // Store this information to prevent this from causing exponential runtime.
2693};
2694
2695static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
2696static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
2697
2698/// Parses binary expressions by inserting fake parenthesis based on
2699/// operator precedence.
2700class ExpressionParser {
2701public:
2702 ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
2703 AnnotatedLine &Line)
2704 : Style(Style), Keywords(Keywords), Line(Line), Current(Line.First) {}
2705
2706 /// Parse expressions with the given operator precedence.
2707 void parse(int Precedence = 0) {
2708 // Skip 'return' and ObjC selector colons as they are not part of a binary
2709 // expression.
2710 while (Current && (Current->is(tok::kw_return) ||
2711 (Current->is(tok::colon) &&
2712 Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) {
2713 next();
2714 }
2715
2716 if (!Current || Precedence > PrecedenceArrowAndPeriod)
2717 return;
2718
2719 // Conditional expressions need to be parsed separately for proper nesting.
2720 if (Precedence == prec::Conditional) {
2721 parseConditionalExpr();
2722 return;
2723 }
2724
2725 // Parse unary operators, which all have a higher precedence than binary
2726 // operators.
2727 if (Precedence == PrecedenceUnaryOperator) {
2728 parseUnaryOperator();
2729 return;
2730 }
2731
2732 FormatToken *Start = Current;
2733 FormatToken *LatestOperator = nullptr;
2734 unsigned OperatorIndex = 0;
2735 // The first name of the current type in a port list.
2736 FormatToken *VerilogFirstOfType = nullptr;
2737
2738 while (Current) {
2739 // In Verilog ports in a module header that don't have a type take the
2740 // type of the previous one. For example,
2741 // module a(output b,
2742 // c,
2743 // output d);
2744 // In this case there need to be fake parentheses around b and c.
2745 if (Style.isVerilog() && Precedence == prec::Comma) {
2746 VerilogFirstOfType =
2747 verilogGroupDecl(VerilogFirstOfType, LatestOperator);
2748 }
2749
2750 // Consume operators with higher precedence.
2751 parse(Precedence + 1);
2752
2753 // Do not assign fake parenthesis to tokens that are part of an
2754 // unexpanded macro call. The line within the macro call contains
2755 // the parenthesis and commas, and we will not find operators within
2756 // that structure.
2757 if (Current && Current->MacroParent)
2758 break;
2759
2760 int CurrentPrecedence = getCurrentPrecedence();
2761
2762 if (Precedence == CurrentPrecedence && Current &&
2763 Current->is(TT_SelectorName)) {
2764 if (LatestOperator)
2765 addFakeParenthesis(Start, prec::Level(Precedence));
2766 Start = Current;
2767 }
2768
2769 if ((Style.isCSharp() || Style.isJavaScript() ||
2770 Style.Language == FormatStyle::LK_Java) &&
2771 Precedence == prec::Additive && Current) {
2772 // A string can be broken without parentheses around it when it is
2773 // already in a sequence of strings joined by `+` signs.
2774 FormatToken *Prev = Current->getPreviousNonComment();
2775 if (Prev && Prev->is(tok::string_literal) &&
2776 (Prev == Start || Prev->endsSequence(tok::string_literal, tok::plus,
2777 TT_StringInConcatenation))) {
2778 Prev->setType(TT_StringInConcatenation);
2779 }
2780 }
2781
2782 // At the end of the line or when an operator with lower precedence is
2783 // found, insert fake parenthesis and return.
2784 if (!Current ||
2785 (Current->closesScope() &&
2786 (Current->MatchingParen || Current->is(TT_TemplateString))) ||
2787 (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
2788 (CurrentPrecedence == prec::Conditional &&
2789 Precedence == prec::Assignment && Current->is(tok::colon))) {
2790 break;
2791 }
2792
2793 // Consume scopes: (), [], <> and {}
2794 // In addition to that we handle require clauses as scope, so that the
2795 // constraints in that are correctly indented.
2796 if (Current->opensScope() ||
2797 Current->isOneOf(TT_RequiresClause,
2798 TT_RequiresClauseInARequiresExpression)) {
2799 // In fragment of a JavaScript template string can look like '}..${' and
2800 // thus close a scope and open a new one at the same time.
2801 while (Current && (!Current->closesScope() || Current->opensScope())) {
2802 next();
2803 parse();
2804 }
2805 next();
2806 } else {
2807 // Operator found.
2808 if (CurrentPrecedence == Precedence) {
2809 if (LatestOperator)
2810 LatestOperator->NextOperator = Current;
2811 LatestOperator = Current;
2812 Current->OperatorIndex = OperatorIndex;
2813 ++OperatorIndex;
2814 }
2815 next(/*SkipPastLeadingComments=*/Precedence > 0);
2816 }
2817 }
2818
2819 // Group variables of the same type.
2820 if (Style.isVerilog() && Precedence == prec::Comma && VerilogFirstOfType)
2821 addFakeParenthesis(VerilogFirstOfType, prec::Comma);
2822
2823 if (LatestOperator && (Current || Precedence > 0)) {
2824 // The requires clauses do not neccessarily end in a semicolon or a brace,
2825 // but just go over to struct/class or a function declaration, we need to
2826 // intervene so that the fake right paren is inserted correctly.
2827 auto End =
2828 (Start->Previous &&
2829 Start->Previous->isOneOf(TT_RequiresClause,
2830 TT_RequiresClauseInARequiresExpression))
2831 ? [this]() {
2832 auto Ret = Current ? Current : Line.Last;
2833 while (!Ret->ClosesRequiresClause && Ret->Previous)
2834 Ret = Ret->Previous;
2835 return Ret;
2836 }()
2837 : nullptr;
2838
2839 if (Precedence == PrecedenceArrowAndPeriod) {
2840 // Call expressions don't have a binary operator precedence.
2841 addFakeParenthesis(Start, prec::Unknown, End);
2842 } else {
2843 addFakeParenthesis(Start, prec::Level(Precedence), End);
2844 }
2845 }
2846 }
2847
2848private:
2849 /// Gets the precedence (+1) of the given token for binary operators
2850 /// and other tokens that we treat like binary operators.
2851 int getCurrentPrecedence() {
2852 if (Current) {
2853 const FormatToken *NextNonComment = Current->getNextNonComment();
2854 if (Current->is(TT_ConditionalExpr))
2855 return prec::Conditional;
2856 if (NextNonComment && Current->is(TT_SelectorName) &&
2857 (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
2858 ((Style.Language == FormatStyle::LK_Proto ||
2859 Style.Language == FormatStyle::LK_TextProto) &&
2860 NextNonComment->is(tok::less)))) {
2861 return prec::Assignment;
2862 }
2863 if (Current->is(TT_JsComputedPropertyName))
2864 return prec::Assignment;
2865 if (Current->is(TT_LambdaArrow))
2866 return prec::Comma;
2867 if (Current->is(TT_FatArrow))
2868 return prec::Assignment;
2869 if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
2870 (Current->is(tok::comment) && NextNonComment &&
2871 NextNonComment->is(TT_SelectorName))) {
2872 return 0;
2873 }
2874 if (Current->is(TT_RangeBasedForLoopColon))
2875 return prec::Comma;
2876 if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
2877 Current->is(Keywords.kw_instanceof)) {
2878 return prec::Relational;
2879 }
2880 if (Style.isJavaScript() &&
2881 Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
2882 return prec::Relational;
2883 }
2884 if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
2885 return Current->getPrecedence();
2886 if (Current->isOneOf(tok::period, tok::arrow) &&
2887 Current->isNot(TT_TrailingReturnArrow)) {
2888 return PrecedenceArrowAndPeriod;
2889 }
2890 if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
2891 Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
2892 Keywords.kw_throws)) {
2893 return 0;
2894 }
2895 // In Verilog case labels are not on separate lines straight out of
2896 // UnwrappedLineParser. The colon is not part of an expression.
2897 if (Style.isVerilog() && Current->is(tok::colon))
2898 return 0;
2899 }
2900 return -1;
2901 }
2902
2903 void addFakeParenthesis(FormatToken *Start, prec::Level Precedence,
2904 FormatToken *End = nullptr) {
2905 Start->FakeLParens.push_back(Precedence);
2906 if (Precedence > prec::Unknown)
2907 Start->StartsBinaryExpression = true;
2908 if (!End && Current)
2909 End = Current->getPreviousNonComment();
2910 if (End) {
2911 ++End->FakeRParens;
2912 if (Precedence > prec::Unknown)
2913 End->EndsBinaryExpression = true;
2914 }
2915 }
2916
2917 /// Parse unary operator expressions and surround them with fake
2918 /// parentheses if appropriate.
2919 void parseUnaryOperator() {
2921 while (Current && Current->is(TT_UnaryOperator)) {
2922 Tokens.push_back(Current);
2923 next();
2924 }
2925 parse(PrecedenceArrowAndPeriod);
2926 for (FormatToken *Token : llvm::reverse(Tokens)) {
2927 // The actual precedence doesn't matter.
2928 addFakeParenthesis(Token, prec::Unknown);
2929 }
2930 }
2931
2932 void parseConditionalExpr() {
2933 while (Current && Current->isTrailingComment())
2934 next();
2935 FormatToken *Start = Current;
2936 parse(prec::LogicalOr);
2937 if (!Current || Current->isNot(tok::question))
2938 return;
2939 next();
2940 parse(prec::Assignment);
2941 if (!Current || Current->isNot(TT_ConditionalExpr))
2942 return;
2943 next();
2944 parse(prec::Assignment);
2945 addFakeParenthesis(Start, prec::Conditional);
2946 }
2947
2948 void next(bool SkipPastLeadingComments = true) {
2949 if (Current)
2950 Current = Current->Next;
2951 while (Current &&
2952 (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
2953 Current->isTrailingComment()) {
2954 Current = Current->Next;
2955 }
2956 }
2957
2958 // Add fake parenthesis around declarations of the same type for example in a
2959 // module prototype. Return the first port / variable of the current type.
2960 FormatToken *verilogGroupDecl(FormatToken *FirstOfType,
2961 FormatToken *PreviousComma) {
2962 if (!Current)
2963 return nullptr;
2964
2965 FormatToken *Start = Current;
2966
2967 // Skip attributes.
2968 while (Start->startsSequence(tok::l_paren, tok::star)) {
2969 if (!(Start = Start->MatchingParen) ||
2970 !(Start = Start->getNextNonComment())) {
2971 return nullptr;
2972 }
2973 }
2974
2975 FormatToken *Tok = Start;
2976
2977 if (Tok->is(Keywords.kw_assign))
2978 Tok = Tok->getNextNonComment();
2979
2980 // Skip any type qualifiers to find the first identifier. It may be either a
2981 // new type name or a variable name. There can be several type qualifiers
2982 // preceding a variable name, and we can not tell them apart by looking at
2983 // the word alone since a macro can be defined as either a type qualifier or
2984 // a variable name. Thus we use the last word before the dimensions instead
2985 // of the first word as the candidate for the variable or type name.
2986 FormatToken *First = nullptr;
2987 while (Tok) {
2988 FormatToken *Next = Tok->getNextNonComment();
2989
2990 if (Tok->is(tok::hash)) {
2991 // Start of a macro expansion.
2992 First = Tok;
2993 Tok = Next;
2994 if (Tok)
2995 Tok = Tok->getNextNonComment();
2996 } else if (Tok->is(tok::hashhash)) {
2997 // Concatenation. Skip.
2998 Tok = Next;
2999 if (Tok)
3000 Tok = Tok->getNextNonComment();
3001 } else if (Keywords.isVerilogQualifier(*Tok) ||
3002 Keywords.isVerilogIdentifier(*Tok)) {
3003 First = Tok;
3004 Tok = Next;
3005 // The name may have dots like `interface_foo.modport_foo`.
3006 while (Tok && Tok->isOneOf(tok::period, tok::coloncolon) &&
3007 (Tok = Tok->getNextNonComment())) {
3008 if (Keywords.isVerilogIdentifier(*Tok))
3009 Tok = Tok->getNextNonComment();
3010 }
3011 } else if (!Next) {
3012 Tok = nullptr;
3013 } else if (Tok->is(tok::l_paren)) {
3014 // Make sure the parenthesized list is a drive strength. Otherwise the
3015 // statement may be a module instantiation in which case we have already
3016 // found the instance name.
3017 if (Next->isOneOf(
3018 Keywords.kw_highz0, Keywords.kw_highz1, Keywords.kw_large,
3019 Keywords.kw_medium, Keywords.kw_pull0, Keywords.kw_pull1,
3020 Keywords.kw_small, Keywords.kw_strong0, Keywords.kw_strong1,
3021 Keywords.kw_supply0, Keywords.kw_supply1, Keywords.kw_weak0,
3022 Keywords.kw_weak1)) {
3023 Tok->setType(TT_VerilogStrength);
3024 Tok = Tok->MatchingParen;
3025 if (Tok) {
3026 Tok->setType(TT_VerilogStrength);
3027 Tok = Tok->getNextNonComment();
3028 }
3029 } else {
3030 break;
3031 }
3032 } else if (Tok->is(tok::hash)) {
3033 if (Next->is(tok::l_paren))
3034 Next = Next->MatchingParen;
3035 if (Next)
3036 Tok = Next->getNextNonComment();
3037 } else {
3038 break;
3039 }
3040 }
3041
3042 // Find the second identifier. If it exists it will be the name.
3043 FormatToken *Second = nullptr;
3044 // Dimensions.
3045 while (Tok && Tok->is(tok::l_square) && (Tok = Tok->MatchingParen))
3046 Tok = Tok->getNextNonComment();
3047 if (Tok && (Tok->is(tok::hash) || Keywords.isVerilogIdentifier(*Tok)))
3048 Second = Tok;
3049
3050 // If the second identifier doesn't exist and there are qualifiers, the type
3051 // is implied.
3052 FormatToken *TypedName = nullptr;
3053 if (Second) {
3054 TypedName = Second;
3055 if (First && First->is(TT_Unknown))
3056 First->setType(TT_VerilogDimensionedTypeName);
3057 } else if (First != Start) {
3058 // If 'First' is null, then this isn't a declaration, 'TypedName' gets set
3059 // to null as intended.
3060 TypedName = First;
3061 }
3062
3063 if (TypedName) {
3064 // This is a declaration with a new type.
3065 if (TypedName->is(TT_Unknown))
3066 TypedName->setType(TT_StartOfName);
3067 // Group variables of the previous type.
3068 if (FirstOfType && PreviousComma) {
3069 PreviousComma->setType(TT_VerilogTypeComma);
3070 addFakeParenthesis(FirstOfType, prec::Comma, PreviousComma->Previous);
3071 }
3072
3073 FirstOfType = TypedName;
3074
3075 // Don't let higher precedence handle the qualifiers. For example if we
3076 // have:
3077 // parameter x = 0
3078 // We skip `parameter` here. This way the fake parentheses for the
3079 // assignment will be around `x = 0`.
3080 while (Current && Current != FirstOfType) {
3081 if (Current->opensScope()) {
3082 next();
3083 parse();
3084 }
3085 next();
3086 }
3087 }
3088
3089 return FirstOfType;
3090 }
3091
3092 const FormatStyle &Style;
3093 const AdditionalKeywords &Keywords;
3094 const AnnotatedLine &Line;
3095 FormatToken *Current;
3096};
3097
3098} // end anonymous namespace
3099
3101 SmallVectorImpl<AnnotatedLine *> &Lines) const {
3102 const AnnotatedLine *NextNonCommentLine = nullptr;
3103 for (AnnotatedLine *Line : llvm::reverse(Lines)) {
3104 assert(Line->First);
3105
3106 // If the comment is currently aligned with the line immediately following
3107 // it, that's probably intentional and we should keep it.
3108 if (NextNonCommentLine && !NextNonCommentLine->First->Finalized &&
3109 Line->isComment() && NextNonCommentLine->First->NewlinesBefore <= 1 &&
3110 NextNonCommentLine->First->OriginalColumn ==
3111 Line->First->OriginalColumn) {
3112 const bool PPDirectiveOrImportStmt =
3113 NextNonCommentLine->Type == LT_PreprocessorDirective ||
3114 NextNonCommentLine->Type == LT_ImportStatement;
3115 if (PPDirectiveOrImportStmt)
3116 Line->Type = LT_CommentAbovePPDirective;
3117 // Align comments for preprocessor lines with the # in column 0 if
3118 // preprocessor lines are not indented. Otherwise, align with the next
3119 // line.
3120 Line->Level = Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
3121 PPDirectiveOrImportStmt
3122 ? 0
3123 : NextNonCommentLine->Level;
3124 } else {
3125 NextNonCommentLine = Line->First->isNot(tok::r_brace) ? Line : nullptr;
3126 }
3127
3128 setCommentLineLevels(Line->Children);
3129 }
3130}
3131
3132static unsigned maxNestingDepth(const AnnotatedLine &Line) {
3133 unsigned Result = 0;
3134 for (const auto *Tok = Line.First; Tok; Tok = Tok->Next)
3135 Result = std::max(Result, Tok->NestingLevel);
3136 return Result;
3137}
3138
3139// Returns the name of a function with no return type, e.g. a constructor or
3140// destructor.
3142 for (FormatToken *Tok = Line.getFirstNonComment(), *Name = nullptr; Tok;
3143 Tok = Tok->getNextNonComment()) {
3144 // Skip C++11 attributes both before and after the function name.
3145 if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3146 Tok = Tok->MatchingParen;
3147 if (!Tok)
3148 break;
3149 continue;
3150 }
3151
3152 // Make sure the name is followed by a pair of parentheses.
3153 if (Name) {
3154 return Tok->is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) &&
3155 Tok->MatchingParen
3156 ? Name
3157 : nullptr;
3158 }
3159
3160 // Skip keywords that may precede the constructor/destructor name.
3161 if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3162 tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3163 continue;
3164 }
3165
3166 // A qualified name may start from the global namespace.
3167 if (Tok->is(tok::coloncolon)) {
3168 Tok = Tok->Next;
3169 if (!Tok)
3170 break;
3171 }
3172
3173 // Skip to the unqualified part of the name.
3174 while (Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3175 assert(Tok->Next);
3176 Tok = Tok->Next->Next;
3177 if (!Tok)
3178 return nullptr;
3179 }
3180
3181 // Skip the `~` if a destructor name.
3182 if (Tok->is(tok::tilde)) {
3183 Tok = Tok->Next;
3184 if (!Tok)
3185 break;
3186 }
3187
3188 // Make sure the name is not already annotated, e.g. as NamespaceMacro.
3189 if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
3190 break;
3191
3192 Name = Tok;
3193 }
3194
3195 return nullptr;
3196}
3197
3198// Checks if Tok is a constructor/destructor name qualified by its class name.
3199static bool isCtorOrDtorName(const FormatToken *Tok) {
3200 assert(Tok && Tok->is(tok::identifier));
3201 const auto *Prev = Tok->Previous;
3202
3203 if (Prev && Prev->is(tok::tilde))
3204 Prev = Prev->Previous;
3205
3206 if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3207 return false;
3208
3209 assert(Prev->Previous);
3210 return Prev->Previous->TokenText == Tok->TokenText;
3211}
3212
3214 for (auto &Child : Line.Children)
3215 annotate(*Child);
3216
3217 AnnotatingParser Parser(Style, Line, Keywords, Scopes);
3218 Line.Type = Parser.parseLine();
3219
3220 // With very deep nesting, ExpressionParser uses lots of stack and the
3221 // formatting algorithm is very slow. We're not going to do a good job here
3222 // anyway - it's probably generated code being formatted by mistake.
3223 // Just skip the whole line.
3224 if (maxNestingDepth(Line) > 50)
3225 Line.Type = LT_Invalid;
3226
3227 if (Line.Type == LT_Invalid)
3228 return;
3229
3230 ExpressionParser ExprParser(Style, Keywords, Line);
3231 ExprParser.parse();
3232
3233 if (Style.isCpp()) {
3234 auto *Tok = getFunctionName(Line);
3235 if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
3236 Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
3237 Tok->setFinalizedType(TT_FunctionDeclarationName);
3238 }
3239 }
3240
3241 if (Line.startsWith(TT_ObjCMethodSpecifier))
3242 Line.Type = LT_ObjCMethodDecl;
3243 else if (Line.startsWith(TT_ObjCDecl))
3244 Line.Type = LT_ObjCDecl;
3245 else if (Line.startsWith(TT_ObjCProperty))
3246 Line.Type = LT_ObjCProperty;
3247
3248 Line.First->SpacesRequiredBefore = 1;
3249 Line.First->CanBreakBefore = Line.First->MustBreakBefore;
3250}
3251
3252// This function heuristically determines whether 'Current' starts the name of a
3253// function declaration.
3254static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
3255 const AnnotatedLine &Line) {
3256 assert(Current.Previous);
3257
3258 if (Current.is(TT_FunctionDeclarationName))
3259 return true;
3260
3261 if (!Current.Tok.getIdentifierInfo())
3262 return false;
3263
3264 auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
3265 for (; Next; Next = Next->Next) {
3266 if (Next->is(TT_OverloadedOperatorLParen))
3267 return Next;
3268 if (Next->is(TT_OverloadedOperator))
3269 continue;
3270 if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
3271 // For 'new[]' and 'delete[]'.
3272 if (Next->Next &&
3273 Next->Next->startsSequence(tok::l_square, tok::r_square)) {
3274 Next = Next->Next->Next;
3275 }
3276 continue;
3277 }
3278 if (Next->startsSequence(tok::l_square, tok::r_square)) {
3279 // For operator[]().
3280 Next = Next->Next;
3281 continue;
3282 }
3283 if ((Next->isSimpleTypeSpecifier() || Next->is(tok::identifier)) &&
3284 Next->Next && Next->Next->isOneOf(tok::star, tok::amp, tok::ampamp)) {
3285 // For operator void*(), operator char*(), operator Foo*().
3286 Next = Next->Next;
3287 continue;
3288 }
3289 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3290 Next = Next->MatchingParen;
3291 continue;
3292 }
3293
3294 break;
3295 }
3296 return nullptr;
3297 };
3298
3299 // Find parentheses of parameter list.
3300 const FormatToken *Next = Current.Next;
3301 if (Current.is(tok::kw_operator)) {
3302 const auto *Previous = Current.Previous;
3303 if (Previous->Tok.getIdentifierInfo() &&
3304 !Previous->isOneOf(tok::kw_return, tok::kw_co_return)) {
3305 return true;
3306 }
3307 if (Previous->is(tok::r_paren) && Previous->is(TT_TypeDeclarationParen)) {
3308 assert(Previous->MatchingParen);
3309 assert(Previous->MatchingParen->is(tok::l_paren));
3310 assert(Previous->MatchingParen->is(TT_TypeDeclarationParen));
3311 return true;
3312 }
3313 if (!Previous->isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser))
3314 return false;
3315 Next = skipOperatorName(Next);
3316 } else {
3317 if (Current.isNot(TT_StartOfName) || Current.NestingLevel != 0)
3318 return false;
3319 for (; Next; Next = Next->Next) {
3320 if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
3321 Next = Next->MatchingParen;
3322 } else if (Next->is(tok::coloncolon)) {
3323 Next = Next->Next;
3324 if (!Next)
3325 return false;
3326 if (Next->is(tok::kw_operator)) {
3327 Next = skipOperatorName(Next->Next);
3328 break;
3329 }
3330 if (Next->isNot(tok::identifier))
3331 return false;
3332 } else if (isCppAttribute(IsCpp, *Next)) {
3333 Next = Next->MatchingParen;
3334 if (!Next)
3335 return false;
3336 } else if (Next->is(tok::l_paren)) {
3337 break;
3338 } else {
3339 return false;
3340 }
3341 }
3342 }
3343
3344 // Check whether parameter list can belong to a function declaration.
3345 if (!Next || Next->isNot(tok::l_paren) || !Next->MatchingParen)
3346 return false;
3347 // If the lines ends with "{", this is likely a function definition.
3348 if (Line.Last->is(tok::l_brace))
3349 return true;
3350 if (Next->Next == Next->MatchingParen)
3351 return true; // Empty parentheses.
3352 // If there is an &/&& after the r_paren, this is likely a function.
3353 if (Next->MatchingParen->Next &&
3354 Next->MatchingParen->Next->is(TT_PointerOrReference)) {
3355 return true;
3356 }
3357
3358 // Check for K&R C function definitions (and C++ function definitions with
3359 // unnamed parameters), e.g.:
3360 // int f(i)
3361 // {
3362 // return i + 1;
3363 // }
3364 // bool g(size_t = 0, bool b = false)
3365 // {
3366 // return !b;
3367 // }
3368 if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
3369 !Line.endsWith(tok::semi)) {
3370 return true;
3371 }
3372
3373 for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
3374 Tok = Tok->Next) {
3375 if (Tok->is(TT_TypeDeclarationParen))
3376 return true;
3377 if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
3378 Tok = Tok->MatchingParen;
3379 continue;
3380 }
3381 if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
3382 Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3383 return true;
3384 }
3385 if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
3386 Tok->Tok.isLiteral()) {
3387 return false;
3388 }
3389 }
3390 return false;
3391}
3392
3393bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
3394 assert(Line.MightBeFunctionDecl);
3395
3396 if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
3397 Style.AlwaysBreakAfterReturnType ==
3399 Line.Level > 0) {
3400 return false;
3401 }
3402
3403 switch (Style.AlwaysBreakAfterReturnType) {
3405 return false;
3408 return true;
3411 return Line.mightBeFunctionDefinition();
3412 }
3413
3414 return false;
3415}
3416
3418 const FormatStyle &Style) {
3419 switch (Style.BreakAfterAttributes) {
3421 return true;
3423 return Tok.NewlinesBefore > 0;
3424 default:
3425 return false;
3426 }
3427}
3428
3430 for (AnnotatedLine *ChildLine : Line.Children)
3432
3433 Line.First->TotalLength =
3434 Line.First->IsMultiline ? Style.ColumnLimit
3435 : Line.FirstStartColumn + Line.First->ColumnWidth;
3436 FormatToken *Current = Line.First->Next;
3437 bool InFunctionDecl = Line.MightBeFunctionDecl;
3438 bool AlignArrayOfStructures =
3439 (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
3440 Line.Type == LT_ArrayOfStructInitializer);
3441 if (AlignArrayOfStructures)
3442 calculateArrayInitializerColumnList(Line);
3443
3444 bool LineIsFunctionDeclaration = false;
3445 for (FormatToken *Tok = Current, *AfterLastAttribute = nullptr; Tok;
3446 Tok = Tok->Next) {
3447 if (Tok->Previous->EndsCppAttributeGroup)
3448 AfterLastAttribute = Tok;
3449 if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line)) {
3450 LineIsFunctionDeclaration = true;
3451 Tok->setFinalizedType(TT_FunctionDeclarationName);
3452 if (AfterLastAttribute &&
3453 mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
3454 AfterLastAttribute->MustBreakBefore = true;
3455 Line.ReturnTypeWrapped = true;
3456 }
3457 break;
3458 }
3459 }
3460
3461 if (Style.isCpp() && !LineIsFunctionDeclaration) {
3462 // Annotate */&/&& in `operator` function calls as binary operators.
3463 for (const auto *Tok = Line.First; Tok; Tok = Tok->Next) {
3464 if (Tok->isNot(tok::kw_operator))
3465 continue;
3466 do {
3467 Tok = Tok->Next;
3468 } while (Tok && Tok->isNot(TT_OverloadedOperatorLParen));
3469 if (!Tok)
3470 break;
3471 const auto *LeftParen = Tok;
3472 for (Tok = Tok->Next; Tok && Tok != LeftParen->MatchingParen;
3473 Tok = Tok->Next) {
3474 if (Tok->isNot(tok::identifier))
3475 continue;
3476 auto *Next = Tok->Next;
3477 const bool NextIsBinaryOperator =
3478 Next && Next->isOneOf(tok::star, tok::amp, tok::ampamp) &&
3479 Next->Next && Next->Next->is(tok::identifier);
3480 if (!NextIsBinaryOperator)
3481 continue;
3482 Next->setType(TT_BinaryOperator);
3483 Tok = Next;
3484 }
3485 }
3486 }
3487
3488 while (Current) {
3489 const FormatToken *Prev = Current->Previous;
3490 if (Current->is(TT_LineComment)) {
3491 if (Prev->is(BK_BracedInit) && Prev->opensScope()) {
3492 Current->SpacesRequiredBefore =
3493 (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
3494 ? 0
3495 : 1;
3496 } else if (Prev->is(TT_VerilogMultiLineListLParen)) {
3497 Current->SpacesRequiredBefore = 0;
3498 } else {
3499 Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
3500 }
3501
3502 // If we find a trailing comment, iterate backwards to determine whether
3503 // it seems to relate to a specific parameter. If so, break before that
3504 // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
3505 // to the previous line in:
3506 // SomeFunction(a,
3507 // b, // comment
3508 // c);
3509 if (!Current->HasUnescapedNewline) {
3510 for (FormatToken *Parameter = Current->Previous; Parameter;
3511 Parameter = Parameter->Previous) {
3512 if (Parameter->isOneOf(tok::comment, tok::r_brace))
3513 break;
3514 if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
3515 if (Parameter->Previous->isNot(TT_CtorInitializerComma) &&
3516 Parameter->HasUnescapedNewline) {
3517 Parameter->MustBreakBefore = true;
3518 }
3519 break;
3520 }
3521 }
3522 }
3523 } else if (Current->SpacesRequiredBefore == 0 &&
3524 spaceRequiredBefore(Line, *Current)) {
3525 Current->SpacesRequiredBefore = 1;
3526 }
3527
3528 const auto &Children = Prev->Children;
3529 if (!Children.empty() && Children.back()->Last->is(TT_LineComment)) {
3530 Current->MustBreakBefore = true;
3531 } else {
3532 Current->MustBreakBefore =
3533 Current->MustBreakBefore || mustBreakBefore(Line, *Current);
3534 if (!Current->MustBreakBefore && InFunctionDecl &&
3535 Current->is(TT_FunctionDeclarationName)) {
3536 Current->MustBreakBefore = mustBreakForReturnType(Line);
3537 }
3538 }
3539
3540 Current->CanBreakBefore =
3541 Current->MustBreakBefore || canBreakBefore(Line, *Current);
3542 unsigned ChildSize = 0;
3543 if (Prev->Children.size() == 1) {
3544 FormatToken &LastOfChild = *Prev->Children[0]->Last;
3545 ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
3546 : LastOfChild.TotalLength + 1;
3547 }
3548 if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
3549 (Prev->Children.size() == 1 &&
3550 Prev->Children[0]->First->MustBreakBefore) ||
3551 Current->IsMultiline) {
3552 Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
3553 } else {
3554 Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
3555 ChildSize + Current->SpacesRequiredBefore;
3556 }
3557
3558 if (Current->is(TT_CtorInitializerColon))
3559 InFunctionDecl = false;
3560
3561 // FIXME: Only calculate this if CanBreakBefore is true once static
3562 // initializers etc. are sorted out.
3563 // FIXME: Move magic numbers to a better place.
3564
3565 // Reduce penalty for aligning ObjC method arguments using the colon
3566 // alignment as this is the canonical way (still prefer fitting everything
3567 // into one line if possible). Trying to fit a whole expression into one
3568 // line should not force other line breaks (e.g. when ObjC method
3569 // expression is a part of other expression).
3570 Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
3571 if (Style.Language == FormatStyle::LK_ObjC &&
3572 Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
3573 if (Current->ParameterIndex == 1)
3574 Current->SplitPenalty += 5 * Current->BindingStrength;
3575 } else {
3576 Current->SplitPenalty += 20 * Current->BindingStrength;
3577 }
3578
3579 Current = Current->Next;
3580 }
3581
3582 calculateUnbreakableTailLengths(Line);
3583 unsigned IndentLevel = Line.Level;
3584 for (Current = Line.First; Current; Current = Current->Next) {
3585 if (Current->Role)
3586 Current->Role->precomputeFormattingInfos(Current);
3587 if (Current->MatchingParen &&
3588 Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
3589 IndentLevel > 0) {
3590 --IndentLevel;
3591 }
3592 Current->IndentLevel = IndentLevel;
3593 if (Current->opensBlockOrBlockTypeList(Style))
3594 ++IndentLevel;
3595 }
3596
3597 LLVM_DEBUG({ printDebugInfo(Line); });
3598}
3599
3600void TokenAnnotator::calculateUnbreakableTailLengths(
3601 AnnotatedLine &Line) const {
3602 unsigned UnbreakableTailLength = 0;
3603 FormatToken *Current = Line.Last;
3604 while (Current) {
3605 Current->UnbreakableTailLength = UnbreakableTailLength;
3606 if (Current->CanBreakBefore ||
3607 Current->isOneOf(tok::comment, tok::string_literal)) {
3608 UnbreakableTailLength = 0;
3609 } else {
3610 UnbreakableTailLength +=
3611 Current->ColumnWidth + Current->SpacesRequiredBefore;
3612 }
3613 Current = Current->Previous;
3614 }
3615}
3616
3617void TokenAnnotator::calculateArrayInitializerColumnList(
3618 AnnotatedLine &Line) const {
3619 if (Line.First == Line.Last)
3620 return;
3621 auto *CurrentToken = Line.First;
3622 CurrentToken->ArrayInitializerLineStart = true;
3623 unsigned Depth = 0;
3624 while (CurrentToken && CurrentToken != Line.Last) {
3625 if (CurrentToken->is(tok::l_brace)) {
3626 CurrentToken->IsArrayInitializer = true;
3627 if (CurrentToken->Next)
3628 CurrentToken->Next->MustBreakBefore = true;
3629 CurrentToken =
3630 calculateInitializerColumnList(Line, CurrentToken->Next, Depth + 1);
3631 } else {
3632 CurrentToken = CurrentToken->Next;
3633 }
3634 }
3635}
3636
3637FormatToken *TokenAnnotator::calculateInitializerColumnList(
3638 AnnotatedLine &Line, FormatToken *CurrentToken, unsigned Depth) const {
3639 while (CurrentToken && CurrentToken != Line.Last) {
3640 if (CurrentToken->is(tok::l_brace))
3641 ++Depth;
3642 else if (CurrentToken->is(tok::r_brace))
3643 --Depth;
3644 if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
3645 CurrentToken = CurrentToken->Next;
3646 if (!CurrentToken)
3647 break;
3648 CurrentToken->StartsColumn = true;
3649 CurrentToken = CurrentToken->Previous;
3650 }
3651 CurrentToken = CurrentToken->Next;
3652 }
3653 return CurrentToken;
3654}
3655
3656unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
3657 const FormatToken &Tok,
3658 bool InFunctionDecl) const {
3659 const FormatToken &Left = *Tok.Previous;
3660 const FormatToken &Right = Tok;
3661
3662 if (Left.is(tok::semi))
3663 return 0;
3664
3665 // Language specific handling.
3666 if (Style.Language == FormatStyle::LK_Java) {
3667 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
3668 return 1;
3669 if (Right.is(Keywords.kw_implements))
3670 return 2;
3671 if (Left.is(tok::comma) && Left.NestingLevel == 0)
3672 return 3;
3673 } else if (Style.isJavaScript()) {
3674 if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
3675 return 100;
3676 if (Left.is(TT_JsTypeColon))
3677 return 35;
3678 if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
3679 (Right.is(TT_TemplateString) && Right.TokenText.startswith("}"))) {
3680 return 100;
3681 }
3682 // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
3683 if (Left.opensScope() && Right.closesScope())
3684 return 200;
3685 } else if (Style.isProto()) {
3686 if (Right.is(tok::l_square))
3687 return 1;
3688 if (Right.is(tok::period))
3689 return 500;
3690 }
3691
3692 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
3693 return 1;
3694 if (Right.is(tok::l_square)) {
3695 if (Left.is(tok::r_square))
3696 return 200;
3697 // Slightly prefer formatting local lambda definitions like functions.
3698 if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
3699 return 35;
3700 if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
3701 TT_ArrayInitializerLSquare,
3702 TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
3703 return 500;
3704 }
3705 }
3706
3707 if (Left.is(tok::coloncolon))
3708 return 500;
3709 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
3710 Right.is(tok::kw_operator)) {
3711 if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
3712 return 3;
3713 if (Left.is(TT_StartOfName))
3714 return 110;
3715 if (InFunctionDecl && Right.NestingLevel == 0)
3716 return Style.PenaltyReturnTypeOnItsOwnLine;
3717 return 200;
3718 }
3719 if (Right.is(TT_PointerOrReference))
3720 return 190;
3721 if (Right.is(TT_LambdaArrow))
3722 return 110;
3723 if (Left.is(tok::equal) && Right.is(tok::l_brace))
3724 return 160;
3725 if (Left.is(TT_CastRParen))
3726 return 100;
3727 if (Left.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union))
3728 return 5000;
3729 if (Left.is(tok::comment))
3730 return 1000;
3731
3732 if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
3733 TT_CtorInitializerColon)) {
3734 return 2;
3735 }
3736
3737 if (Right.isMemberAccess()) {
3738 // Breaking before the "./->" of a chained call/member access is reasonably
3739 // cheap, as formatting those with one call per line is generally
3740 // desirable. In particular, it should be cheaper to break before the call
3741 // than it is to break inside a call's parameters, which could lead to weird
3742 // "hanging" indents. The exception is the very last "./->" to support this
3743 // frequent pattern:
3744 //
3745 // aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
3746 // dddddddd);
3747 //
3748 // which might otherwise be blown up onto many lines. Here, clang-format
3749 // won't produce "hanging" indents anyway as there is no other trailing
3750 // call.
3751 //
3752 // Also apply higher penalty is not a call as that might lead to a wrapping
3753 // like:
3754 //
3755 // aaaaaaa
3756 // .aaaaaaaaa.bbbbbbbb(cccccccc);
3757 return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
3758 ? 150
3759 : 35;
3760 }
3761
3762 if (Right.is(TT_TrailingAnnotation) &&
3763 (!Right.Next || Right.Next->isNot(tok::l_paren))) {
3764 // Moving trailing annotations to the next line is fine for ObjC method
3765 // declarations.
3766 if (Line.startsWith(TT_ObjCMethodSpecifier))
3767 return 10;
3768 // Generally, breaking before a trailing annotation is bad unless it is
3769 // function-like. It seems to be especially preferable to keep standard
3770 // annotations (i.e. "const", "final" and "override") on the same line.
3771 // Use a slightly higher penalty after ")" so that annotations like
3772 // "const override" are kept together.
3773 bool is_short_annotation = Right.TokenText.size() < 10;
3774 return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
3775 }
3776
3777 // In for-loops, prefer breaking at ',' and ';'.
3778 if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
3779 return 4;
3780
3781 // In Objective-C method expressions, prefer breaking before "param:" over
3782 // breaking after it.
3783 if (Right.is(TT_SelectorName))
3784 return 0;
3785 if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
3786 return Line.MightBeFunctionDecl ? 50 : 500;
3787
3788 // In Objective-C type declarations, avoid breaking after the category's
3789 // open paren (we'll prefer breaking after the protocol list's opening
3790 // angle bracket, if present).
3791 if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous &&
3792 Left.Previous->isOneOf(tok::identifier, tok::greater)) {
3793 return 500;
3794 }
3795
3796 if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
3797 return Style.PenaltyBreakOpenParenthesis;
3798 if (Left.is(tok::l_paren) && InFunctionDecl &&
3799 Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
3800 return 100;
3801 }
3802 if (Left.is(tok::l_paren) && Left.Previous &&
3803 (Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
3804 Left.Previous->isIf())) {
3805 return 1000;
3806 }
3807 if (Left.is(tok::equal) && InFunctionDecl)
3808 return 110;
3809 if (Right.is(tok::r_brace))
3810 return 1;
3811 if (Left.is(TT_TemplateOpener))
3812 return 100;
3813 if (Left.opensScope()) {
3814 // If we aren't aligning after opening parens/braces we can always break
3815 // here unless the style does not want us to place all arguments on the
3816 // next line.
3817 if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
3818 (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
3819 return 0;
3820 }
3821 if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
3822 return 19;
3823 return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
3824 : 19;
3825 }
3826 if (Left.is(TT_JavaAnnotation))
3827 return 50;
3828
3829 if (Left.is(TT_UnaryOperator))
3830 return 60;
3831 if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
3832 Left.Previous->isLabelString() &&
3833 (Left.NextOperator || Left.OperatorIndex != 0)) {
3834 return 50;
3835 }
3836 if (Right.is(tok::plus) && Left.isLabelString() &&
3837 (Right.NextOperator || Right.OperatorIndex != 0)) {
3838 return 25;
3839 }
3840 if (Left.is(tok::comma))
3841 return 1;
3842 if (Right.is(tok::lessless) && Left.isLabelString() &&
3843 (Right.NextOperator || Right.OperatorIndex != 1)) {
3844 return 25;
3845 }
3846 if (Right.is(tok::lessless)) {
3847 // Breaking at a << is really cheap.
3848 if (Left.isNot(tok::r_paren) || Right.OperatorIndex > 0) {
3849 // Slightly prefer to break before the first one in log-like statements.
3850 return 2;
3851 }
3852 return 1;
3853 }
3854 if (Left.ClosesTemplateDeclaration)
3855 return Style.PenaltyBreakTemplateDeclaration;
3856 if (Left.ClosesRequiresClause)
3857 return 0;
3858 if (Left.is(TT_ConditionalExpr))
3859 return prec::Conditional;
3860 prec::Level Level = Left.getPrecedence();
3861 if (Level == prec::Unknown)
3862 Level = Right.getPrecedence();
3863 if (Level == prec::Assignment)
3864 return Style.PenaltyBreakAssignment;
3865 if (Level != prec::Unknown)
3866 return Level;
3867
3868 return 3;
3869}
3870
3871bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
3872 if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
3873 return true;
3874 if (Right.is(TT_OverloadedOperatorLParen) &&
3875 Style.SpaceBeforeParensOptions.AfterOverloadedOperator) {
3876 return true;
3877 }
3878 if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
3879 Right.ParameterCount > 0) {
3880 return true;
3881 }
3882 return false;
3883}
3884
3885bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
3886 const FormatToken &Left,
3887 const FormatToken &Right) const {
3888 if (Left.is(tok::kw_return) &&
3889 !Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
3890 return true;
3891 }
3892 if (Left.is(tok::kw_throw) && Right.is(tok::l_paren) && Right.MatchingParen &&
3893 Right.MatchingParen->is(TT_CastRParen)) {
3894 return true;
3895 }
3896 if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java)
3897 return true;
3898 if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
3899 Left.Tok.getObjCKeywordID() == tok::objc_property) {
3900 return true;
3901 }
3902 if (Right.is(tok::hashhash))
3903 return Left.is(tok::hash);
3904 if (Left.isOneOf(tok::hashhash, tok::hash))
3905 return Right.is(tok::hash);
3906 if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
3907 (Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
3908 Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
3909 return Style.SpacesInParensOptions.InEmptyParentheses;
3910 }
3911 if (Style.SpacesInParensOptions.InConditionalStatements) {
3912 const FormatToken *LeftParen = nullptr;
3913 if (Left.is(tok::l_paren))
3914 LeftParen = &Left;
3915 else if (Right.is(tok::r_paren) && Right.MatchingParen)
3916 LeftParen = Right.MatchingParen;
3917 if (LeftParen) {
3918 if (LeftParen->is(TT_ConditionLParen))
3919 return true;
3920 if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous))
3921 return true;
3922 }
3923 }
3924
3925 // trailing return type 'auto': []() -> auto {}, auto foo() -> auto {}
3926 if (Left.is(tok::kw_auto) && Right.isOneOf(TT_LambdaLBrace, TT_FunctionLBrace,
3927 // function return type 'auto'
3928 TT_FunctionTypeLParen)) {
3929 return true;
3930 }
3931
3932 // auto{x} auto(x)
3933 if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace))
3934 return false;
3935
3936 // operator co_await(x)
3937 if (Right.is(tok::l_paren) && Left.is(tok::kw_co_await) && Left.Previous &&
3938 Left.Previous->is(tok::kw_operator)) {
3939 return false;
3940 }
3941 // co_await (x), co_yield (x), co_return (x)
3942 if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
3943 !Right.isOneOf(tok::semi, tok::r_paren)) {
3944 return true;
3945 }
3946
3947 if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
3948 return (Right.is(TT_CastRParen) ||
3949 (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
3950 ? Style.SpacesInParensOptions.InCStyleCasts
3951 : Style.SpacesInParensOptions.Other;
3952 }
3953 if (Right.isOneOf(tok::semi, tok::comma))
3954 return false;
3955 if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) {
3956 bool IsLightweightGeneric = Right.MatchingParen &&
3957 Right.MatchingParen->Next &&
3958 Right.MatchingParen->Next->is(tok::colon);
3959 return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
3960 }
3961 if (Right.is(tok::less) && Left.is(tok::kw_template))
3962 return Style.SpaceAfterTemplateKeyword;
3963 if (Left.isOneOf(tok::exclaim, tok::tilde))
3964 return false;
3965 if (Left.is(tok::at) &&
3966 Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
3967 tok::numeric_constant, tok::l_paren, tok::l_brace,
3968 tok::kw_true, tok::kw_false)) {
3969 return false;
3970 }
3971 if (Left.is(tok::colon))
3972 return Left.isNot(TT_ObjCMethodExpr);
3973 if (Left.is(tok::coloncolon))
3974 return false;
3975 if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
3976 if (Style.Language == FormatStyle::LK_TextProto ||
3977 (Style.Language == FormatStyle::LK_Proto &&
3978 (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
3979 // Format empty list as `<>`.
3980 if (Left.is(tok::less) && Right.is(tok::greater))
3981 return false;
3982 return !Style.Cpp11BracedListStyle;
3983 }
3984 // Don't attempt to format operator<(), as it is handled later.
3985 if (Right.isNot(TT_OverloadedOperatorLParen))
3986 return false;
3987 }
3988 if (Right.is(tok::ellipsis)) {
3989 return Left.Tok.isLiteral() || (Left.is(tok::identifier) && Left.Previous &&
3990 Left.Previous->is(tok::kw_case));
3991 }
3992 if (Left.is(tok::l_square) && Right.is(tok::amp))
3993 return Style.SpacesInSquareBrackets;
3994 if (Right.is(TT_PointerOrReference)) {
3995 if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) {
3996 if (!Left.MatchingParen)
3997 return true;
3998 FormatToken *TokenBeforeMatchingParen =
3999 Left.MatchingParen->getPreviousNonComment();
4000 if (!TokenBeforeMatchingParen || Left.isNot(TT_TypeDeclarationParen))
4001 return true;
4002 }
4003 // Add a space if the previous token is a pointer qualifier or the closing
4004 // parenthesis of __attribute__(()) expression and the style requires spaces
4005 // after pointer qualifiers.
4006 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
4007 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4008 (Left.is(TT_AttributeRParen) ||
4009 Left.canBePointerOrReferenceQualifier())) {
4010 return true;
4011 }
4012 if (Left.Tok.isLiteral())
4013 return true;
4014 // for (auto a = 0, b = 0; const auto & c : {1, 2, 3})
4015 if (Left.isTypeOrIdentifier() && Right.Next && Right.Next->Next &&
4016 Right.Next->Next->is(TT_RangeBasedForLoopColon)) {
4017 return getTokenPointerOrReferenceAlignment(Right) !=
4019 }
4020 return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
4021 (getTokenPointerOrReferenceAlignment(Right) !=
4023 (Line.IsMultiVariableDeclStmt &&
4024 (Left.NestingLevel == 0 ||
4025 (Left.NestingLevel == 1 && startsWithInitStatement(Line)))));
4026 }
4027 if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
4028 (Left.isNot(TT_PointerOrReference) ||
4029 (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
4030 !Line.IsMultiVariableDeclStmt))) {
4031 return true;
4032 }
4033 if (Left.is(TT_PointerOrReference)) {
4034 // Add a space if the next token is a pointer qualifier and the style
4035 // requires spaces before pointer qualifiers.
4036 if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
4037 Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
4038 Right.canBePointerOrReferenceQualifier()) {
4039 return true;
4040 }
4041 // & 1
4042 if (Right.Tok.isLiteral())
4043 return true;
4044 // & /* comment
4045 if (Right.is(TT_BlockComment))
4046 return true;
4047 // foo() -> const Bar * override/final
4048 // S::foo() & noexcept/requires
4049 if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final, tok::kw_noexcept,
4050 TT_RequiresClause) &&
4051 Right.isNot(TT_StartOfName)) {
4052 return true;
4053 }
4054 // & {
4055 if (Right.is(tok::l_brace) && Right.is(BK_Block))
4056 return true;
4057 // for (auto a = 0, b = 0; const auto& c : {1, 2, 3})
4058 if (Left.Previous && Left.Previous->isTypeOrIdentifier() && Right.Next &&
4059 Right.Next->is(TT_RangeBasedForLoopColon)) {
4060 return getTokenPointerOrReferenceAlignment(Left) !=
4062 }
4063 if (Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
4064 tok::l_paren)) {
4065 return false;
4066 }
4067 if (getTokenPointerOrReferenceAlignment(Left) == FormatStyle::PAS_Right)
4068 return false;
4069 // FIXME: Setting IsMultiVariableDeclStmt for the whole line is error-prone,
4070 // because it does not take into account nested scopes like lambdas.
4071 // In multi-variable declaration statements, attach */& to the variable
4072 // independently of the style. However, avoid doing it if we are in a nested
4073 // scope, e.g. lambda. We still need to special-case statements with
4074 // initializers.
4075 if (Line.IsMultiVariableDeclStmt &&
4076 (Left.NestingLevel == Line.First->NestingLevel ||
4077 ((Left.NestingLevel == Line.First->NestingLevel + 1) &&
4078 startsWithInitStatement(Line)))) {
4079 return false;
4080 }
4081 return Left.Previous && !Left.Previous->isOneOf(
4082 tok::l_paren, tok::coloncolon, tok::l_square);
4083 }
4084 // Ensure right pointer alignment with ellipsis e.g. int *...P
4085 if (Left.is(tok::ellipsis) && Left.Previous &&
4086 Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp)) {
4087 return Style.PointerAlignment != FormatStyle::PAS_Right;
4088 }
4089
4090 if (Right.is(tok::star) && Left.is(tok::l_paren))
4091 return false;
4092 if (Left.is(tok::star) && Right.isOneOf(tok::star, tok::amp, tok::ampamp))
4093 return false;
4094 if (Right.isOneOf(tok::star, tok::amp, tok::ampamp)) {
4095 const FormatToken *Previous = &Left;
4096 while (Previous && Previous->isNot(tok::kw_operator)) {
4097 if (Previous->is(tok::identifier) || Previous->isSimpleTypeSpecifier()) {
4098 Previous = Previous->getPreviousNonComment();
4099 continue;
4100 }
4101 if (Previous->is(TT_TemplateCloser) && Previous->MatchingParen) {
4102 Previous = Previous->MatchingParen->getPreviousNonComment();
4103 continue;
4104 }
4105 if (Previous->is(tok::coloncolon)) {
4106 Previous = Previous->getPreviousNonComment();
4107 continue;
4108 }
4109 break;
4110 }
4111 // Space between the type and the * in:
4112 // operator void*()
4113 // operator char*()
4114 // operator void const*()
4115 // operator void volatile*()
4116 // operator /*comment*/ const char*()
4117 // operator volatile /*comment*/ char*()
4118 // operator Foo*()
4119 // operator C<T>*()
4120 // operator std::Foo*()
4121 // operator C<T>::D<U>*()
4122 // dependent on PointerAlignment style.
4123 if (Previous) {
4124 if (Previous->endsSequence(tok::kw_operator))
4125 return Style.PointerAlignment != FormatStyle::PAS_Left;
4126 if (Previous->is(tok::kw_const) || Previous->is(tok::kw_volatile)) {
4127 return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
4128 (Style.SpaceAroundPointerQualifiers ==
4130 (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
4131 }
4132 }
4133 }
4134 if (Style.isCSharp() && Left.is(Keywords.kw_is) && Right.is(tok::l_square))
4135 return true;
4136 const auto SpaceRequiredForArrayInitializerLSquare =
4137 [](const FormatToken &LSquareTok, const FormatStyle &Style) {
4138 return Style.SpacesInContainerLiterals ||
4139 ((Style.Language == FormatStyle::LK_Proto ||
4140 Style.Language == FormatStyle::LK_TextProto) &&
4141 !Style.Cpp11BracedListStyle &&
4142 LSquareTok.endsSequence(tok::l_square, tok::colon,
4143 TT_SelectorName));
4144 };
4145 if (Left.is(tok::l_square)) {
4146 return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
4147 SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
4148 (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
4149 TT_LambdaLSquare) &&
4150 Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
4151 }
4152 if (Right.is(tok::r_square)) {
4153 return Right.MatchingParen &&
4154 ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
4155 SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
4156 Style)) ||
4157 (Style.SpacesInSquareBrackets &&
4158 Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
4159 TT_StructuredBindingLSquare,
4160 TT_LambdaLSquare)));
4161 }
4162 if (Right.is(tok::l_square) &&
4163 !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
4164 TT_DesignatedInitializerLSquare,
4165 TT_StructuredBindingLSquare, TT_AttributeSquare) &&
4166 !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
4167 !(Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
4168 Right.is(TT_ArraySubscriptLSquare))) {
4169 return false;
4170 }
4171 if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
4172 return !Left.Children.empty(); // No spaces in "{}".
4173 if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) ||
4174 (Right.is(tok::r_brace) && Right.MatchingParen &&
4175 Right.MatchingParen->isNot(BK_Block))) {
4176 return Style.Cpp11BracedListStyle ? Style.SpacesInParensOptions.Other
4177 : true;
4178 }
4179 if (Left.is(TT_BlockComment)) {
4180 // No whitespace in x(/*foo=*/1), except for JavaScript.
4181 return Style.isJavaScript() || !Left.TokenText.endswith("=*/");
4182 }
4183
4184 // Space between template and attribute.
4185 // e.g. template <typename T> [[nodiscard]] ...
4186 if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
4187 return true;
4188 // Space before parentheses common for all languages
4189 if (Right.is(tok::l_paren)) {
4190 if (Left.is(TT_TemplateCloser) && Right.isNot(TT_FunctionTypeLParen))
4191 return spaceRequiredBeforeParens(Right);
4192 if (Left.isOneOf(TT_RequiresClause,
4193 TT_RequiresClauseInARequiresExpression)) {
4194 return Style.SpaceBeforeParensOptions.AfterRequiresInClause ||
4195 spaceRequiredBeforeParens(Right);
4196 }
4197 if (Left.is(TT_RequiresExpression)) {
4198 return Style.SpaceBeforeParensOptions.AfterRequiresInExpression ||
4199 spaceRequiredBeforeParens(Right);
4200 }
4201 if (Left.is(TT_AttributeRParen) ||
4202 (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) {
4203 return true;
4204 }
4205 if (Left.is(TT_ForEachMacro)) {
4206 return Style.SpaceBeforeParensOptions.AfterForeachMacros ||
4207 spaceRequiredBeforeParens(Right);
4208 }
4209 if (Left.is(TT_IfMacro)) {
4210 return Style.SpaceBeforeParensOptions.AfterIfMacros ||
4211 spaceRequiredBeforeParens(Right);
4212 }
4213 if (Line.Type == LT_ObjCDecl)
4214 return true;
4215 if (Left.is(tok::semi))
4216 return true;
4217 if (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
4218 tok::kw_case, TT_ForEachMacro, TT_ObjCForIn) ||
4219 Left.isIf(Line.Type != LT_PreprocessorDirective) ||
4220 Right.is(TT_ConditionLParen)) {
4221 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4222 spaceRequiredBeforeParens(Right);
4223 }
4224
4225 // TODO add Operator overloading specific Options to
4226 // SpaceBeforeParensOptions
4227 if (Right.is(TT_OverloadedOperatorLParen))
4228 return spaceRequiredBeforeParens(Right);
4229 // Function declaration or definition
4230 if (Line.MightBeFunctionDecl && (Left.is(TT_FunctionDeclarationName))) {
4231 if (Line.mightBeFunctionDefinition()) {
4232 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4233 spaceRequiredBeforeParens(Right);
4234 } else {
4235 return Style.SpaceBeforeParensOptions.AfterFunctionDeclarationName ||
4236 spaceRequiredBeforeParens(Right);
4237 }
4238 }
4239 // Lambda
4240 if (Line.Type != LT_PreprocessorDirective && Left.is(tok::r_square) &&
4241 Left.MatchingParen && Left.MatchingParen->is(TT_LambdaLSquare)) {
4242 return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
4243 spaceRequiredBeforeParens(Right);
4244 }
4245 if (!Left.Previous || Left.Previous->isNot(tok::period)) {
4246 if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) {
4247 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4248 spaceRequiredBeforeParens(Right);
4249 }
4250 if (Left.isOneOf(tok::kw_new, tok::kw_delete)) {
4251 return ((!Line.MightBeFunctionDecl || !Left.Previous) &&
4252 Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
4253 spaceRequiredBeforeParens(Right);
4254 }
4255
4256 if (Left.is(tok::r_square) && Left.MatchingParen &&
4257 Left.MatchingParen->Previous &&
4258 Left.MatchingParen->Previous->is(tok::kw_delete)) {
4259 return (Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
4260 spaceRequiredBeforeParens(Right);
4261 }
4262 }
4263 // Handle builtins like identifiers.
4264 if (Line.Type != LT_PreprocessorDirective &&
4265 (Left.Tok.getIdentifierInfo() || Left.is(tok::r_paren))) {
4266 return spaceRequiredBeforeParens(Right);
4267 }
4268 return false;
4269 }
4270 if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
4271 return false;
4272 if (Right.is(TT_UnaryOperator)) {
4273 return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
4274 (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
4275 }
4276 // No space between the variable name and the initializer list.
4277 // A a1{1};
4278 // Verilog doesn't have such syntax, but it has word operators that are C++
4279 // identifiers like `a inside {b, c}`. So the rule is not applicable.
4280 if (!Style.isVerilog() &&
4281 (Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
4282 tok::r_paren) ||
4283 Left.isSimpleTypeSpecifier()) &&
4284 Right.is(tok::l_brace) && Right.getNextNonComment() &&
4285 Right.isNot(BK_Block)) {
4286 return false;
4287 }
4288 if (Left.is(tok::period) || Right.is(tok::period))
4289 return false;
4290 // u#str, U#str, L#str, u8#str
4291 // uR#str, UR#str, LR#str, u8R#str
4292 if (Right.is(tok::hash) && Left.is(tok::identifier) &&
4293 (Left.TokenText == "L" || Left.TokenText == "u" ||
4294 Left.TokenText == "U" || Left.TokenText == "u8" ||
4295 Left.TokenText == "LR" || Left.TokenText == "uR" ||
4296 Left.TokenText == "UR" || Left.TokenText == "u8R")) {
4297 return false;
4298 }
4299 if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
4300 Left.MatchingParen->Previous &&
4301 (Left.MatchingParen->Previous->is(tok::period) ||
4302 Left.MatchingParen->Previous->is(tok::coloncolon))) {
4303 // Java call to generic function with explicit type:
4304 // A.<B<C<...>>>DoSomething();
4305 // A::<B<C<...>>>DoSomething(); // With a Java 8 method reference.
4306 return false;
4307 }
4308 if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
4309 return false;
4310 if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at)) {
4311 // Objective-C dictionary literal -> no space after opening brace.
4312 return false;
4313 }
4314 if (Right.is(tok::r_brace) && Right.MatchingParen &&
4315 Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) {
4316 // Objective-C dictionary literal -> no space before closing brace.
4317 return false;
4318 }
4319 if (Right.getType() == TT_TrailingAnnotation &&
4320 Right.isOneOf(tok::amp, tok::ampamp) &&
4321 Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
4322 (!Right.Next || Right.Next->is(tok::semi))) {
4323 // Match const and volatile ref-qualifiers without any additional
4324 // qualifiers such as
4325 // void Fn() const &;
4326 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
4327 }
4328
4329 return true;
4330}
4331
4332bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
4333 const FormatToken &Right) const {
4334 const FormatToken &Left = *Right.Previous;
4335
4336 // If the token is finalized don't touch it (as it could be in a
4337 // clang-format-off section).
4338 if (Left.Finalized)
4339 return Right.hasWhitespaceBefore();
4340
4341 // Never ever merge two words.
4342 if (Keywords.isWordLike(Right) && Keywords.isWordLike(Left))
4343 return true;
4344
4345 // Leave a space between * and /* to avoid C4138 `comment end` found outside
4346 // of comment.
4347 if (Left.is(tok::star) && Right.is(tok::comment))
4348 return true;
4349
4350 if (Style.isCpp()) {
4351 if (Left.is(TT_OverloadedOperator) &&
4352 Right.isOneOf(TT_TemplateOpener, TT_TemplateCloser)) {
4353 return true;
4354 }
4355 // Space between UDL and dot: auto b = 4s .count();
4356 if (Right.is(tok::period) && Left.is(tok::numeric_constant))
4357 return true;
4358 // Space between import <iostream>.
4359 // or import .....;
4360 if (Left.is(Keywords.kw_import) && Right.isOneOf(tok::less, tok::ellipsis))
4361 return true;
4362 // Space between `module :` and `import :`.
4363 if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
4364 Right.is(TT_ModulePartitionColon)) {
4365 return true;
4366 }
4367 // No space between import foo:bar but keep a space between import :bar;
4368 if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
4369 return false;
4370 // No space between :bar;
4371 if (Left.is(TT_ModulePartitionColon) &&
4372 Right.isOneOf(tok::identifier, tok::kw_private)) {
4373 return false;
4374 }
4375 if (Left.is(tok::ellipsis) && Right.is(tok::identifier) &&
4376 Line.First->is(Keywords.kw_import)) {
4377 return false;
4378 }
4379 // Space in __attribute__((attr)) ::type.
4380 if (Left.is(TT_AttributeRParen) && Right.is(tok::coloncolon))
4381 return true;
4382
4383 if (Left.is(tok::kw_operator))
4384 return Right.is(tok::coloncolon);
4385 if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
4386 !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) {
4387 return true;
4388 }
4389 if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) &&
4390 Right.is(TT_TemplateOpener)) {
4391 return true;
4392 }
4393 } else if (Style.Language == FormatStyle::LK_Proto ||
4394 Style.Language == FormatStyle::LK_TextProto) {
4395 if (Right.is(tok::period) &&
4396 Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
4397 Keywords.kw_repeated, Keywords.kw_extend)) {
4398 return true;
4399 }
4400 if (Right.is(tok::l_paren) &&
4401 Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) {
4402 return true;
4403 }
4404 if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
4405 return true;
4406 // Slashes occur in text protocol extension syntax: [type/type] { ... }.
4407 if (Left.is(tok::slash) || Right.is(tok::slash))
4408 return false;
4409 if (Left.MatchingParen &&
4410 Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
4411 Right.isOneOf(tok::l_brace, tok::less)) {
4412 return !Style.Cpp11BracedListStyle;
4413 }
4414 // A percent is probably part of a formatting specification, such as %lld.
4415 if (Left.is(tok::percent))
4416 return false;
4417 // Preserve the existence of a space before a percent for cases like 0x%04x
4418 // and "%d %d"
4419 if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
4420 return Right.hasWhitespaceBefore();
4421 } else if (Style.isJson()) {
4422 if (Right.is(tok::colon) && Left.is(tok::string_literal))
4423 return Style.SpaceBeforeJsonColon;
4424 } else if (Style.isCSharp()) {
4425 // Require spaces around '{' and before '}' unless they appear in
4426 // interpolated strings. Interpolated strings are merged into a single token
4427 // so cannot have spaces inserted by this function.
4428
4429 // No space between 'this' and '['
4430 if (Left.is(tok::kw_this) && Right.is(tok::l_square))
4431 return false;
4432
4433 // No space between 'new' and '('
4434 if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
4435 return false;
4436
4437 // Space before { (including space within '{ {').
4438 if (Right.is(tok::l_brace))
4439 return true;
4440
4441 // Spaces inside braces.
4442 if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace))
4443 return true;
4444
4445 if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace))
4446 return true;
4447
4448 // Spaces around '=>'.
4449 if (Left.is(TT_FatArrow) || Right.is(TT_FatArrow))
4450 return true;
4451
4452 // No spaces around attribute target colons
4453 if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon))
4454 return false;
4455
4456 // space between type and variable e.g. Dictionary<string,string> foo;
4457 if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
4458 return true;
4459
4460 // spaces inside square brackets.
4461 if (Left.is(tok::l_square) || Right.is(tok::r_square))
4462 return Style.SpacesInSquareBrackets;
4463
4464 // No space before ? in nullable types.
4465 if (Right.is(TT_CSharpNullable))
4466 return false;
4467
4468 // No space before null forgiving '!'.
4469 if (Right.is(TT_NonNullAssertion))
4470 return false;
4471
4472 // No space between consecutive commas '[,,]'.
4473 if (Left.is(tok::comma) && Right.is(tok::comma))
4474 return false;
4475
4476 // space after var in `var (key, value)`
4477 if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren))
4478 return true;
4479
4480 // space between keywords and paren e.g. "using ("
4481 if (Right.is(tok::l_paren)) {
4482 if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
4483 Keywords.kw_lock)) {
4484 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4485 spaceRequiredBeforeParens(Right);
4486 }
4487 }
4488
4489 // space between method modifier and opening parenthesis of a tuple return
4490 // type
4491 if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
4492 tok::kw_virtual, tok::kw_extern, tok::kw_static,
4493 Keywords.kw_internal, Keywords.kw_abstract,
4494 Keywords.kw_sealed, Keywords.kw_override,
4495 Keywords.kw_async, Keywords.kw_unsafe) &&
4496 Right.is(tok::l_paren)) {
4497 return true;
4498 }
4499 } else if (Style.isJavaScript()) {
4500 if (Left.is(TT_FatArrow))
4501 return true;
4502 // for await ( ...
4503 if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
4504 Left.Previous->is(tok::kw_for)) {
4505 return true;
4506 }
4507 if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
4508 Right.MatchingParen) {
4509 const FormatToken *Next = Right.MatchingParen->getNextNonComment();
4510 // An async arrow function, for example: `x = async () => foo();`,
4511 // as opposed to calling a function called async: `x = async();`
4512 if (Next && Next->is(TT_FatArrow))
4513 return true;
4514 }
4515 if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
4516 (Right.is(TT_TemplateString) && Right.TokenText.startswith("}"))) {
4517 return false;
4518 }
4519 // In tagged template literals ("html`bar baz`"), there is no space between
4520 // the tag identifier and the template string.
4521 if (Keywords.IsJavaScriptIdentifier(Left,
4522 /* AcceptIdentifierName= */ false) &&
4523 Right.is(TT_TemplateString)) {
4524 return false;
4525 }
4526 if (Right.is(tok::star) &&
4527 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) {
4528 return false;
4529 }
4530 if (Right.isOneOf(tok::l_brace, tok::l_square) &&
4531 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
4532 Keywords.kw_extends, Keywords.kw_implements)) {
4533 return true;
4534 }
4535 if (Right.is(tok::l_paren)) {
4536 // JS methods can use some keywords as names (e.g. `delete()`).
4537 if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
4538 return false;
4539 // Valid JS method names can include keywords, e.g. `foo.delete()` or
4540 // `bar.instanceof()`. Recognize call positions by preceding period.
4541 if (Left.Previous && Left.Previous->is(tok::period) &&
4542 Left.Tok.getIdentifierInfo()) {
4543 return false;
4544 }
4545 // Additional unary JavaScript operators that need a space after.
4546 if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
4547 tok::kw_void)) {
4548 return true;
4549 }
4550 }
4551 // `foo as const;` casts into a const type.
4552 if (Left.endsSequence(tok::kw_const, Keywords.kw_as))
4553 return false;
4554 if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
4555 tok::kw_const) ||
4556 // "of" is only a keyword if it appears after another identifier
4557 // (e.g. as "const x of y" in a for loop), or after a destructuring
4558 // operation (const [x, y] of z, const {a, b} of c).
4559 (Left.is(Keywords.kw_of) && Left.Previous &&
4560 (Left.Previous->is(tok::identifier) ||
4561 Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
4562 (!Left.Previous || Left.Previous->isNot(tok::period))) {
4563 return true;
4564 }
4565 if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
4566 Left.Previous->is(tok::period) && Right.is(tok::l_paren)) {
4567 return false;
4568 }
4569 if (Left.is(Keywords.kw_as) &&
4570 Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) {
4571 return true;
4572 }
4573 if (Left.is(tok::kw_default) && Left.Previous &&
4574 Left.Previous->is(tok::kw_export)) {
4575 return true;
4576 }
4577 if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
4578 return true;
4579 if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
4580 return false;
4581 if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
4582 return false;
4583 if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
4584 Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) {
4585 return false;
4586 }
4587 if (Left.is(tok::ellipsis))
4588 return false;
4589 if (Left.is(TT_TemplateCloser) &&
4590 !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
4591 Keywords.kw_implements, Keywords.kw_extends)) {
4592 // Type assertions ('<type>expr') are not followed by whitespace. Other
4593 // locations that should have whitespace following are identified by the
4594 // above set of follower tokens.
4595 return false;
4596 }
4597 if (Right.is(TT_NonNullAssertion))
4598 return false;
4599 if (Left.is(TT_NonNullAssertion) &&
4600 Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) {
4601 return true; // "x! as string", "x! in y"
4602 }
4603 } else if (Style.Language == FormatStyle::LK_Java) {
4604 if (Left.is(tok::r_square) && Right.is(tok::l_brace))
4605 return true;
4606 if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) {
4607 return Style.SpaceBeforeParensOptions.AfterControlStatements ||
4608 spaceRequiredBeforeParens(Right);
4609 }
4610 if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
4611 tok::kw_protected) ||
4612 Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
4613 Keywords.kw_native)) &&
4614 Right.is(TT_TemplateOpener)) {
4615 return true;
4616 }
4617 } else if (Style.isVerilog()) {
4618 // An escaped identifier ends with whitespace.
4619 if (Style.isVerilog() && Left.is(tok::identifier) &&
4620 Left.TokenText[0] == '\\') {
4621 return true;
4622 }
4623 // Add space between things in a primitive's state table unless in a
4624 // transition like `(0?)`.
4625 if ((Left.is(TT_VerilogTableItem) &&
4626 !Right.isOneOf(tok::r_paren, tok::semi)) ||
4627 (Right.is(TT_VerilogTableItem) && Left.isNot(tok::l_paren))) {
4628 const FormatToken *Next = Right.getNextNonComment();
4629 return !(Next && Next->is(tok::r_paren));
4630 }
4631 // Don't add space within a delay like `#0`.
4632 if (Left.isNot(TT_BinaryOperator) &&
4633 Left.isOneOf(Keywords.kw_verilogHash, Keywords.kw_verilogHashHash)) {
4634 return false;
4635 }
4636 // Add space after a delay.
4637 if (Right.isNot(tok::semi) &&
4638 (Left.endsSequence(tok::numeric_constant, Keywords.kw_verilogHash) ||
4639 Left.endsSequence(tok::numeric_constant,
4640 Keywords.kw_verilogHashHash) ||
4641 (Left.is(tok::r_paren) && Left.MatchingParen &&
4642 Left.MatchingParen->endsSequence(tok::l_paren, tok::at)))) {
4643 return true;
4644 }
4645 // Don't add embedded spaces in a number literal like `16'h1?ax` or an array
4646 // literal like `'{}`.
4647 if (Left.is(Keywords.kw_apostrophe) ||
4648 (Left.is(TT_VerilogNumberBase) && Right.is(tok::numeric_constant))) {
4649 return false;
4650 }
4651 // Don't add spaces between two at signs. Like in a coverage event.
4652 // Don't add spaces between at and a sensitivity list like
4653 // `@(posedge clk)`.
4654 if (Left.is(tok::at) && Right.isOneOf(tok::l_paren, tok::star, tok::at))
4655 return false;
4656 // Add space between the type name and dimension like `logic [1:0]`.
4657 if (Right.is(tok::l_square) &&
4658 Left.isOneOf(TT_VerilogDimensionedTypeName, Keywords.kw_function)) {
4659 return true;
4660 }
4661 // Don't add spaces between a casting type and the quote or repetition count
4662 // and the brace.
4663 if ((Right.is(Keywords.kw_apostrophe) ||
4664 (Right.is(BK_BracedInit) && Right.is(tok::l_brace))) &&
4665 !(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
4666 Keywords.isVerilogWordOperator(Left)) &&
4667 (Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace,
4668 tok::numeric_constant) ||
4669 Keywords.isWordLike(Left))) {
4670 return false;
4671 }
4672 // Don't add spaces in imports like `import foo::*;`.
4673 if ((Right.is(tok::star) && Left.is(tok::coloncolon)) ||
4674 (Left.is(tok::star) && Right.is(tok::semi))) {
4675 return false;
4676 }
4677 // Add space in attribute like `(* ASYNC_REG = "TRUE" *)`.
4678 if (Left.endsSequence(tok::star, tok::l_paren) && Right.is(tok::identifier))
4679 return true;
4680 // Add space before drive strength like in `wire (strong1, pull0)`.
4681 if (Right.is(tok::l_paren) && Right.is(TT_VerilogStrength))
4682 return true;
4683 // Don't add space in a streaming concatenation like `{>>{j}}`.
4684 if ((Left.is(tok::l_brace) &&
4685 Right.isOneOf(tok::lessless, tok::greatergreater)) ||
4686 (Left.endsSequence(tok::lessless, tok::l_brace) ||
4687 Left.endsSequence(tok::greatergreater, tok::l_brace))) {
4688 return false;
4689 }
4690 }
4691 if (Left.is(TT_ImplicitStringLiteral))
4692 return Right.hasWhitespaceBefore();
4693 if (Line.Type == LT_ObjCMethodDecl) {
4694 if (Left.is(TT_ObjCMethodSpecifier))
4695 return true;
4696 if (Left.is(tok::r_paren) && canBeObjCSelectorComponent(Right)) {
4697 // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a
4698 // keyword in Objective-C, and '+ (instancetype)new;' is a standard class
4699 // method declaration.
4700 return false;
4701 }
4702 }
4703 if (Line.Type == LT_ObjCProperty &&
4704 (Right.is(tok::equal) || Left.is(tok::equal))) {
4705 return false;
4706 }
4707
4708 if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
4709 Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) {
4710 return true;
4711 }
4712 if (Left.is(tok::comma) && Right.isNot(TT_OverloadedOperatorLParen) &&
4713 // In an unexpanded macro call we only find the parentheses and commas
4714 // in a line; the commas and closing parenthesis do not require a space.
4715 (Left.Children.empty() || !Left.MacroParent)) {
4716 return true;
4717 }
4718 if (Right.is(tok::comma))
4719 return false;
4720 if (Right.is(TT_ObjCBlockLParen))
4721 return true;
4722 if (Right.is(TT_CtorInitializerColon))
4723 return Style.SpaceBeforeCtorInitializerColon;
4724 if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
4725 return false;
4726 if (Right.is(TT_RangeBasedForLoopColon) &&
4727 !Style.SpaceBeforeRangeBasedForLoopColon) {
4728 return false;
4729 }
4730 if (Left.is(TT_BitFieldColon)) {
4731 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
4732 Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
4733 }
4734 if (Right.is(tok::colon)) {
4735 if (Right.is(TT_CaseLabelColon))
4736 return Style.SpaceBeforeCaseColon;
4737 if (Right.is(TT_GotoLabelColon))
4738 return false;
4739 // `private:` and `public:`.
4740 if (!Right.getNextNonComment())
4741 return false;
4742 if (Right.is(TT_ObjCMethodExpr))
4743 return false;
4744 if (Left.is(tok::question))
4745 return false;
4746 if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
4747 return false;
4748 if (Right.is(TT_DictLiteral))
4749 return Style.SpacesInContainerLiterals;
4750 if (Right.is(TT_AttributeColon))
4751 return false;
4752 if (Right.is(TT_CSharpNamedArgumentColon))
4753 return false;
4754 if (Right.is(TT_GenericSelectionColon))
4755 return false;
4756 if (Right.is(TT_BitFieldColon)) {
4757 return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
4758 Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
4759 }
4760 return true;
4761 }
4762 // Do not merge "- -" into "--".
4763 if ((Left.isOneOf(tok::minus, tok::minusminus) &&
4764 Right.isOneOf(tok::minus, tok::minusminus)) ||
4765 (Left.isOneOf(tok::plus, tok::plusplus) &&
4766 Right.isOneOf(tok::plus, tok::plusplus))) {
4767 return true;
4768 }
4769 if (Left.is(TT_UnaryOperator)) {
4770 if (Right.isNot(tok::l_paren)) {
4771 // The alternative operators for ~ and ! are "compl" and "not".
4772 // If they are used instead, we do not want to combine them with
4773 // the token to the right, unless that is a left paren.
4774 if (Left.is(tok::exclaim) && Left.TokenText == "not")
4775 return true;
4776 if (Left.is(tok::tilde) && Left.TokenText == "compl")
4777 return true;
4778 // Lambda captures allow for a lone &, so "&]" needs to be properly
4779 // handled.
4780 if (Left.is(tok::amp) && Right.is(tok::r_square))
4781 return Style.SpacesInSquareBrackets;
4782 }
4783 return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
4784 Right.is(TT_BinaryOperator);
4785 }
4786
4787 // If the next token is a binary operator or a selector name, we have
4788 // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
4789 if (Left.is(TT_CastRParen)) {
4790 return Style.SpaceAfterCStyleCast ||
4791 Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
4792 }
4793
4794 auto ShouldAddSpacesInAngles = [this, &Right]() {
4795 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
4796 return true;
4797 if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
4798 return Right.hasWhitespaceBefore();
4799 return false;
4800 };
4801
4802 if (Left.is(tok::greater) && Right.is(tok::greater)) {
4803 if (Style.Language == FormatStyle::LK_TextProto ||
4804 (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral))) {
4805 return !Style.Cpp11BracedListStyle;
4806 }
4807 return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
4808 ((Style.Standard < FormatStyle::LS_Cpp11) ||
4809 ShouldAddSpacesInAngles());
4810 }
4811 if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
4812 Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
4813 (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) {
4814 return false;
4815 }
4816 if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) &&
4817 Right.getPrecedence() == prec::Assignment) {
4818 return false;
4819 }
4820 if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) &&
4821 (Left.is(tok::identifier) || Left.is(tok::kw_this))) {
4822 return false;
4823 }
4824 if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) {
4825 // Generally don't remove existing spaces between an identifier and "::".
4826 // The identifier might actually be a macro name such as ALWAYS_INLINE. If
4827 // this turns out to be too lenient, add analysis of the identifier itself.
4828 return Right.hasWhitespaceBefore();
4829 }
4830 if (Right.is(tok::coloncolon) &&
4831 !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
4832 // Put a space between < and :: in vector< ::std::string >
4833 return (Left.is(TT_TemplateOpener) &&
4834 ((Style.Standard < FormatStyle::LS_Cpp11) ||
4835 ShouldAddSpacesInAngles())) ||
4836 !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
4837 tok::kw___super, TT_TemplateOpener,
4838 TT_TemplateCloser)) ||
4839 (Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
4840 }
4841 if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
4842 return ShouldAddSpacesInAngles();
4843 // Space before TT_StructuredBindingLSquare.
4844 if (Right.is(TT_StructuredBindingLSquare)) {
4845 return !Left.isOneOf(tok::amp, tok::ampamp) ||
4846 getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
4847 }
4848 // Space before & or && following a TT_StructuredBindingLSquare.
4849 if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) &&
4850 Right.isOneOf(tok::amp, tok::ampamp)) {
4851 return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
4852 }
4853 if ((Right.is(TT_BinaryOperator) && Left.isNot(tok::l_paren)) ||
4854 (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
4855 Right.isNot(tok::r_paren))) {
4856 return true;
4857 }
4858 if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
4859 Left.MatchingParen &&
4860 Left.MatchingParen->is(TT_OverloadedOperatorLParen)) {
4861 return false;
4862 }
4863 if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
4864 Line.Type == LT_ImportStatement) {
4865 return true;
4866 }
4867 if (Right.is(TT_TrailingUnaryOperator))
4868 return false;
4869 if (Left.is(TT_RegexLiteral))
4870 return false;
4871 return spaceRequiredBetween(Line, Left, Right);
4872}
4873
4874// Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
4875static bool isAllmanBrace(const FormatToken &Tok) {
4876 return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
4877 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
4878}
4879
4880// Returns 'true' if 'Tok' is a function argument.
4881static bool IsFunctionArgument(const FormatToken &Tok) {
4882 return Tok.MatchingParen && Tok.MatchingParen->Next &&
4883 Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren);
4884}
4885
4886static bool
4888 FormatStyle::ShortLambdaStyle ShortLambdaOption) {
4889 return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
4890}
4891
4892static bool isAllmanLambdaBrace(const FormatToken &Tok) {
4893 return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
4894 !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
4895}
4896
4897bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
4898 const FormatToken &Right) const {
4899 const FormatToken &Left = *Right.Previous;
4900 if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
4901 return true;
4902
4903 if (Style.isCSharp()) {
4904 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
4905 Style.BraceWrapping.AfterFunction) {
4906 return true;
4907 }
4908 if (Right.is(TT_CSharpNamedArgumentColon) ||
4909 Left.is(TT_CSharpNamedArgumentColon)) {
4910 return false;
4911 }
4912 if (Right.is(TT_CSharpGenericTypeConstraint))
4913 return true;
4914 if (Right.Next && Right.Next->is(TT_FatArrow) &&
4915 (Right.is(tok::numeric_constant) ||
4916 (Right.is(tok::identifier) && Right.TokenText == "_"))) {
4917 return true;
4918 }
4919
4920 // Break after C# [...] and before public/protected/private/internal.
4921 if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) &&
4922 (Right.isAccessSpecifier(/*ColonRequired=*/false) ||
4923 Right.is(Keywords.kw_internal))) {
4924 return true;
4925 }
4926 // Break between ] and [ but only when there are really 2 attributes.
4927 if (Left.is(TT_AttributeSquare) && Right.is(TT_AttributeSquare) &&
4928 Left.is(tok::r_square) && Right.is(tok::l_square)) {
4929 return true;
4930 }
4931
4932 } else if (Style.isJavaScript()) {
4933 // FIXME: This might apply to other languages and token kinds.
4934 if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous &&
4935 Left.Previous->is(tok::string_literal)) {
4936 return true;
4937 }
4938 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
4939 Left.Previous && Left.Previous->is(tok::equal) &&
4940 Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
4941 tok::kw_const) &&
4942 // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
4943 // above.
4944 !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
4945 // Object literals on the top level of a file are treated as "enum-style".
4946 // Each key/value pair is put on a separate line, instead of bin-packing.
4947 return true;
4948 }
4949 if (Left.is(tok::l_brace) && Line.Level == 0 &&
4950 (Line.startsWith(tok::kw_enum) ||
4951 Line.startsWith(tok::kw_const, tok::kw_enum) ||
4952 Line.startsWith(tok::kw_export, tok::kw_enum) ||
4953 Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) {
4954 // JavaScript top-level enum key/value pairs are put on separate lines
4955 // instead of bin-packing.
4956 return true;
4957 }
4958 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && Left.Previous &&
4959 Left.Previous->is(TT_FatArrow)) {
4960 // JS arrow function (=> {...}).
4961 switch (Style.AllowShortLambdasOnASingleLine) {
4963 return false;
4965 return true;
4967 return !Left.Children.empty();
4969 // allow one-lining inline (e.g. in function call args) and empty arrow
4970 // functions.
4971 return (Left.NestingLevel == 0 && Line.Level == 0) &&
4972 !Left.Children.empty();
4973 }
4974 llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum");
4975 }
4976
4977 if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
4978 !Left.Children.empty()) {
4979 // Support AllowShortFunctionsOnASingleLine for JavaScript.
4980 return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
4981 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
4982 (Left.NestingLevel == 0 && Line.Level == 0 &&
4983 Style.AllowShortFunctionsOnASingleLine &
4985 }
4986 } else if (Style.Language == FormatStyle::LK_Java) {
4987 if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
4988 Right.Next->is(tok::string_literal)) {
4989 return true;
4990 }
4991 } else if (Style.isVerilog()) {
4992 // Break between assignments.
4993 if (Left.is(TT_VerilogAssignComma))
4994 return true;
4995 // Break between ports of different types.
4996 if (Left.is(TT_VerilogTypeComma))
4997 return true;
4998 // Break between ports in a module instantiation and after the parameter
4999 // list.
5000 if (Style.VerilogBreakBetweenInstancePorts &&
5001 (Left.is(TT_VerilogInstancePortComma) ||
5002 (Left.is(tok::r_paren) && Keywords.isVerilogIdentifier(Right) &&
5003 Left.MatchingParen &&
5004 Left.MatchingParen->is(TT_VerilogInstancePortLParen)))) {
5005 return true;
5006 }
5007 // Break after labels. In Verilog labels don't have the 'case' keyword, so
5008 // it is hard to identify them in UnwrappedLineParser.
5009 if (!Keywords.isVerilogBegin(Right) && Keywords.isVerilogEndOfLabel(Left))
5010 return true;
5011 } else if (Style.Language == FormatStyle::LK_Cpp ||
5012 Style.Language == FormatStyle::LK_ObjC ||
5013 Style.Language == FormatStyle::LK_Proto ||
5014 Style.Language == FormatStyle::LK_TableGen ||
5015 Style.Language == FormatStyle::LK_TextProto) {
5016 if (Left.isStringLiteral() && Right.isStringLiteral())
5017 return true;
5018 }
5019
5020 // Basic JSON newline processing.
5021 if (Style.isJson()) {
5022 // Always break after a JSON record opener.
5023 // {
5024 // }
5025 if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace))
5026 return true;
5027 // Always break after a JSON array opener based on BreakArrays.
5028 if ((Left.is(TT_ArrayInitializerLSquare) && Left.is(tok::l_square) &&
5029 Right.isNot(tok::r_square)) ||
5030 Left.is(tok::comma)) {
5031 if (Right.is(tok::l_brace))
5032 return true;
5033 // scan to the right if an we see an object or an array inside
5034 // then break.
5035 for (const auto *Tok = &Right; Tok; Tok = Tok->Next) {
5036 if (Tok->isOneOf(tok::l_brace, tok::l_square))
5037 return true;
5038 if (Tok->isOneOf(tok::r_brace, tok::r_square))
5039 break;
5040 }
5041 return Style.BreakArrays;
5042 }
5043 }
5044
5045 if (Line.startsWith(tok::kw_asm) && Right.is(TT_InlineASMColon) &&
5046 Style.BreakBeforeInlineASMColon == FormatStyle::BBIAS_Always) {
5047 return true;
5048 }
5049
5050 // If the last token before a '}', ']', or ')' is a comma or a trailing
5051 // comment, the intention is to insert a line break after it in order to make
5052 // shuffling around entries easier. Import statements, especially in
5053 // JavaScript, can be an exception to this rule.
5054 if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) {
5055 const FormatToken *BeforeClosingBrace = nullptr;
5056 if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
5057 (Style.isJavaScript() && Left.is(tok::l_paren))) &&
5058 Left.isNot(BK_Block) && Left.MatchingParen) {
5059 BeforeClosingBrace = Left.MatchingParen->Previous;
5060 } else if (Right.MatchingParen &&
5061 (Right.MatchingParen->isOneOf(tok::l_brace,
5062 TT_ArrayInitializerLSquare) ||
5063 (Style.isJavaScript() &&
5064 Right.MatchingParen->is(tok::l_paren)))) {
5065 BeforeClosingBrace = &Left;
5066 }
5067 if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
5068 BeforeClosingBrace->isTrailingComment())) {
5069 return true;
5070 }
5071 }
5072
5073 if (Right.is(tok::comment)) {
5074 return Left.isNot(BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
5075 (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
5076 }
5077 if (Left.isTrailingComment())
5078 return true;
5079 if (Left.IsUnterminatedLiteral)
5080 return true;
5081 if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) &&
5082 Right.Next->is(tok::string_literal)) {
5083 return true;
5084 }
5085 if (Right.is(TT_RequiresClause)) {
5086 switch (Style.RequiresClausePosition) {
5089 return true;
5090 default:
5091 break;
5092 }
5093 }
5094 // Can break after template<> declaration
5095 if (Left.ClosesTemplateDeclaration && Left.MatchingParen &&
5096 Left.MatchingParen->NestingLevel == 0) {
5097 // Put concepts on the next line e.g.
5098 // template<typename T>
5099 // concept ...
5100 if (Right.is(tok::kw_concept))
5101 return Style.BreakBeforeConceptDeclarations == FormatStyle::BBCDS_Always;
5102 return Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes;
5103 }
5104 if (Left.ClosesRequiresClause && Right.isNot(tok::semi)) {
5105 switch (Style.RequiresClausePosition) {
5108 return true;
5109 default:
5110 break;
5111 }
5112 }
5113 if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
5114 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
5115 (Left.is(TT_CtorInitializerComma) ||
5116 Right.is(TT_CtorInitializerColon))) {
5117 return true;
5118 }
5119
5120 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5121 Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma)) {
5122 return true;
5123 }
5124 }
5125 if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
5126 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
5127 Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) {
5128 return true;
5129 }
5130 if (Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly) {
5131 if ((Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon ||
5132 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) &&
5133 Right.is(TT_CtorInitializerColon)) {
5134 return true;
5135 }
5136
5137 if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5138 Left.is(TT_CtorInitializerColon)) {
5139 return true;
5140 }
5141 }
5142 // Break only if we have multiple inheritance.
5143 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
5144 Right.is(TT_InheritanceComma)) {
5145 return true;
5146 }
5147 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
5148 Left.is(TT_InheritanceComma)) {
5149 return true;
5150 }
5151 if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\"")) {
5152 // Multiline raw string literals are special wrt. line breaks. The author
5153 // has made a deliberate choice and might have aligned the contents of the
5154 // string literal accordingly. Thus, we try keep existing line breaks.
5155 return Right.IsMultiline && Right.NewlinesBefore > 0;
5156 }
5157 if ((Left.is(tok::l_brace) || (Left.is(tok::less) && Left.Previous &&
5158 Left.Previous->is(tok::equal))) &&
5159 Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
5160 // Don't put enums or option definitions onto single lines in protocol
5161 // buffers.
5162 return true;
5163 }
5164 if (Right.is(TT_InlineASMBrace))
5165 return Right.HasUnescapedNewline;
5166
5167 if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
5168 auto *FirstNonComment = Line.getFirstNonComment();
5169 bool AccessSpecifier =
5170 FirstNonComment &&
5171 FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,
5172 tok::kw_private, tok::kw_protected);
5173
5174 if (Style.BraceWrapping.AfterEnum) {
5175 if (Line.startsWith(tok::kw_enum) ||
5176 Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
5177 return true;
5178 }
5179 // Ensure BraceWrapping for `public enum A {`.
5180 if (AccessSpecifier && FirstNonComment->Next &&
5181 FirstNonComment->Next->is(tok::kw_enum)) {
5182 return true;
5183 }
5184 }
5185
5186 // Ensure BraceWrapping for `public interface A {`.
5187 if (Style.BraceWrapping.AfterClass &&
5188 ((AccessSpecifier && FirstNonComment->Next &&
5189 FirstNonComment->Next->is(Keywords.kw_interface)) ||
5190 Line.startsWith(Keywords.kw_interface))) {
5191 return true;
5192 }
5193
5194 // Don't attempt to interpret struct return types as structs.
5195 if (Right.isNot(TT_FunctionLBrace)) {
5196 return (Line.startsWith(tok::kw_class) &&
5197 Style.BraceWrapping.AfterClass) ||
5198 (Line.startsWith(tok::kw_struct) &&
5199 Style.BraceWrapping.AfterStruct);
5200 }
5201 }
5202
5203 if (Left.is(TT_ObjCBlockLBrace) &&
5204 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
5205 return true;
5206 }
5207
5208 // Ensure wrapping after __attribute__((XX)) and @interface etc.
5209 if (Left.is(TT_AttributeRParen) && Right.is(TT_ObjCDecl))
5210 return true;
5211
5212 if (Left.is(TT_LambdaLBrace)) {
5213 if (IsFunctionArgument(Left) &&
5214 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) {
5215 return false;
5216 }
5217
5218 if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
5219 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
5220 (!Left.Children.empty() &&
5221 Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) {
5222 return true;
5223 }
5224 }
5225
5226 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
5227 Left.isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser)) {
5228 return true;
5229 }
5230
5231 // Put multiple Java annotation on a new line.
5232 if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
5233 Left.is(TT_LeadingJavaAnnotation) &&
5234 Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
5235 (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
5236 return true;
5237 }
5238
5239 if (Right.is(TT_ProtoExtensionLSquare))
5240 return true;
5241
5242 // In text proto instances if a submessage contains at least 2 entries and at
5243 // least one of them is a submessage, like A { ... B { ... } ... },
5244 // put all of the entries of A on separate lines by forcing the selector of
5245 // the submessage B to be put on a newline.
5246 //
5247 // Example: these can stay on one line:
5248 // a { scalar_1: 1 scalar_2: 2 }
5249 // a { b { key: value } }
5250 //
5251 // and these entries need to be on a new line even if putting them all in one
5252 // line is under the column limit:
5253 // a {
5254 // scalar: 1
5255 // b { key: value }
5256 // }
5257 //
5258 // We enforce this by breaking before a submessage field that has previous
5259 // siblings, *and* breaking before a field that follows a submessage field.
5260 //
5261 // Be careful to exclude the case [proto.ext] { ... } since the `]` is
5262 // the TT_SelectorName there, but we don't want to break inside the brackets.
5263 //
5264 // Another edge case is @submessage { key: value }, which is a common
5265 // substitution placeholder. In this case we want to keep `@` and `submessage`
5266 // together.
5267 //
5268 // We ensure elsewhere that extensions are always on their own line.
5269 if ((Style.Language == FormatStyle::LK_Proto ||
5270 Style.Language == FormatStyle::LK_TextProto) &&
5271 Right.is(TT_SelectorName) && Right.isNot(tok::r_square) && Right.Next) {
5272 // Keep `@submessage` together in:
5273 // @submessage { key: value }
5274 if (Left.is(tok::at))
5275 return false;
5276 // Look for the scope opener after selector in cases like:
5277 // selector { ...
5278 // selector: { ...
5279 // selector: @base { ...
5280 FormatToken *LBrace = Right.Next;
5281 if (LBrace && LBrace->is(tok::colon)) {
5282 LBrace = LBrace->Next;
5283 if (LBrace && LBrace->is(tok::at)) {
5284 LBrace = LBrace->Next;
5285 if (LBrace)
5286 LBrace = LBrace->Next;
5287 }
5288 }
5289 if (LBrace &&
5290 // The scope opener is one of {, [, <:
5291 // selector { ... }
5292 // selector [ ... ]
5293 // selector < ... >
5294 //
5295 // In case of selector { ... }, the l_brace is TT_DictLiteral.
5296 // In case of an empty selector {}, the l_brace is not TT_DictLiteral,
5297 // so we check for immediately following r_brace.
5298 ((LBrace->is(tok::l_brace) &&
5299 (LBrace->is(TT_DictLiteral) ||
5300 (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
5301 LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
5302 // If Left.ParameterCount is 0, then this submessage entry is not the
5303 // first in its parent submessage, and we want to break before this entry.
5304 // If Left.ParameterCount is greater than 0, then its parent submessage
5305 // might contain 1 or more entries and we want to break before this entry
5306 // if it contains at least 2 entries. We deal with this case later by
5307 // detecting and breaking before the next entry in the parent submessage.
5308 if (Left.ParameterCount == 0)
5309 return true;
5310 // However, if this submessage is the first entry in its parent
5311 // submessage, Left.ParameterCount might be 1 in some cases.
5312 // We deal with this case later by detecting an entry
5313 // following a closing paren of this submessage.
5314 }
5315
5316 // If this is an entry immediately following a submessage, it will be
5317 // preceded by a closing paren of that submessage, like in:
5318 // left---. .---right
5319 // v v
5320 // sub: { ... } key: value
5321 // If there was a comment between `}` an `key` above, then `key` would be
5322 // put on a new line anyways.
5323 if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
5324 return true;
5325 }
5326
5327 return false;
5328}
5329
5330bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
5331 const FormatToken &Right) const {
5332 const FormatToken &Left = *Right.Previous;
5333 // Language-specific stuff.
5334 if (Style.isCSharp()) {
5335 if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
5336 Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon)) {
5337 return false;
5338 }
5339 // Only break after commas for generic type constraints.
5340 if (Line.First->is(TT_CSharpGenericTypeConstraint))
5341 return Left.is(TT_CSharpGenericTypeConstraintComma);
5342 // Keep nullable operators attached to their identifiers.
5343 if (Right.is(TT_CSharpNullable))
5344 return false;
5345 } else if (Style.Language == FormatStyle::LK_Java) {
5346 if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5347 Keywords.kw_implements)) {
5348 return false;
5349 }
5350 if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
5351 Keywords.kw_implements)) {
5352 return true;
5353 }
5354 } else if (Style.isJavaScript()) {
5355 const FormatToken *NonComment = Right.getPreviousNonComment();
5356 if (NonComment &&
5357 NonComment->isOneOf(
5358 tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
5359 tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
5360 tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
5361 Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
5362 Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
5363 Keywords.kw_await)) {
5364 return false; // Otherwise automatic semicolon insertion would trigger.
5365 }
5366 if (Right.NestingLevel == 0 &&
5367 (Left.Tok.getIdentifierInfo() ||
5368 Left.isOneOf(tok::r_square, tok::r_paren)) &&
5369 Right.isOneOf(tok::l_square, tok::l_paren)) {
5370 return false; // Otherwise automatic semicolon insertion would trigger.
5371 }
5372 if (NonComment && NonComment->is(tok::identifier) &&
5373 NonComment->TokenText == "asserts") {
5374 return false;
5375 }
5376 if (Left.is(TT_FatArrow) && Right.is(tok::l_brace))
5377 return false;
5378 if (Left.is(TT_JsTypeColon))
5379 return true;
5380 // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
5381 if (Left.is(tok::exclaim) && Right.is(tok::colon))
5382 return false;
5383 // Look for is type annotations like:
5384 // function f(): a is B { ... }
5385 // Do not break before is in these cases.
5386 if (Right.is(Keywords.kw_is)) {
5387 const FormatToken *Next = Right.getNextNonComment();
5388 // If `is` is followed by a colon, it's likely that it's a dict key, so
5389 // ignore it for this check.
5390 // For example this is common in Polymer:
5391 // Polymer({
5392 // is: 'name',
5393 // ...
5394 // });
5395 if (!Next || Next->isNot(tok::colon))
5396 return false;
5397 }
5398 if (Left.is(Keywords.kw_in))
5399 return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
5400 if (Right.is(Keywords.kw_in))
5401 return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
5402 if (Right.is(Keywords.kw_as))
5403 return false; // must not break before as in 'x as type' casts
5404 if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
5405 // extends and infer can appear as keywords in conditional types:
5406 // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
5407 // do not break before them, as the expressions are subject to ASI.
5408 return false;
5409 }
5410 if (Left.is(Keywords.kw_as))
5411 return true;
5412 if (Left.is(TT_NonNullAssertion))
5413 return true;
5414 if (Left.is(Keywords.kw_declare) &&
5415 Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
5416 Keywords.kw_function, tok::kw_class, tok::kw_enum,
5417 Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
5418 Keywords.kw_let, tok::kw_const)) {
5419 // See grammar for 'declare' statements at:
5420 // https://github.com/Microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md#A.10
5421 return false;
5422 }
5423 if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
5424 Right.isOneOf(tok::identifier, tok::string_literal)) {
5425 return false; // must not break in "module foo { ...}"
5426 }
5427 if (Right.is(TT_TemplateString) && Right.closesScope())
5428 return false;
5429 // Don't split tagged template literal so there is a break between the tag
5430 // identifier and template string.
5431 if (Left.is(tok::identifier) && Right.is(TT_TemplateString))
5432 return false;
5433 if (Left.is(TT_TemplateString) && Left.opensScope())
5434 return true;
5435 }
5436
5437 if (Left.is(tok::at))
5438 return false;
5439 if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
5440 return false;
5441 if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
5442 return Right.isNot(tok::l_paren);
5443 if (Right.is(TT_PointerOrReference)) {
5444 return Line.IsMultiVariableDeclStmt ||
5445 (getTokenPointerOrReferenceAlignment(Right) ==
5447 (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
5448 }
5449 if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
5450 Right.is(tok::kw_operator)) {
5451 return true;
5452 }
5453 if (Left.is(TT_PointerOrReference))
5454 return false;
5455 if (Right.isTrailingComment()) {
5456 // We rely on MustBreakBefore being set correctly here as we should not
5457 // change the "binding" behavior of a comment.
5458 // The first comment in a braced lists is always interpreted as belonging to
5459 // the first list element. Otherwise, it should be placed outside of the
5460 // list.
5461 return Left.is(BK_BracedInit) ||
5462 (Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 &&
5463 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
5464 }
5465 if (Left.is(tok::question) && Right.is(tok::colon))
5466 return false;
5467 if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
5468 return Style.BreakBeforeTernaryOperators;
5469 if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
5470 return !Style.BreakBeforeTernaryOperators;
5471 if (Left.is(TT_InheritanceColon))
5472 return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
5473 if (Right.is(TT_InheritanceColon))
5474 return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
5475 if (Right.is(TT_ObjCMethodExpr) && Right.isNot(tok::r_square) &&
5476 Left.isNot(TT_SelectorName)) {
5477 return true;
5478 }
5479
5480 if (Right.is(tok::colon) &&
5481 !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) {
5482 return false;
5483 }
5484 if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
5485 if (Style.Language == FormatStyle::LK_Proto ||
5486 Style.Language == FormatStyle::LK_TextProto) {
5487 if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
5488 return false;
5489 // Prevent cases like:
5490 //
5491 // submessage:
5492 // { key: valueeeeeeeeeeee }
5493 //
5494 // when the snippet does not fit into one line.
5495 // Prefer:
5496 //
5497 // submessage: {
5498 // key: valueeeeeeeeeeee
5499 // }
5500 //
5501 // instead, even if it is longer by one line.
5502 //
5503 // Note that this allows the "{" to go over the column limit
5504 // when the column limit is just between ":" and "{", but that does
5505 // not happen too often and alternative formattings in this case are
5506 // not much better.
5507 //
5508 // The code covers the cases:
5509 //
5510 // submessage: { ... }
5511 // submessage: < ... >
5512 // repeated: [ ... ]
5513 if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
5514 Right.is(TT_DictLiteral)) ||
5515 Right.is(TT_ArrayInitializerLSquare)) {
5516 return false;
5517 }
5518 }
5519 return true;
5520 }
5521 if (Right.is(tok::r_square) && Right.MatchingParen &&
5522 Right.MatchingParen->is(TT_ProtoExtensionLSquare)) {
5523 return false;
5524 }
5525 if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
5526 Right.Next->is(TT_ObjCMethodExpr))) {
5527 return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
5528 }
5529 if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
5530 return true;
5531 if (Right.is(tok::kw_concept))
5532 return Style.BreakBeforeConceptDeclarations != FormatStyle::BBCDS_Never;
5533 if (Right.is(TT_RequiresClause))
5534 return true;
5535 if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen))
5536 return true;
5537 if (Left.ClosesRequiresClause)
5538 return true;
5539 if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
5540 TT_OverloadedOperator)) {
5541 return false;
5542 }
5543 if (Left.is(TT_RangeBasedForLoopColon))
5544 return true;
5545 if (Right.is(TT_RangeBasedForLoopColon))
5546 return false;
5547 if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
5548 return true;
5549 if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
5550 (Left.is(tok::less) && Right.is(tok::less))) {
5551 return false;
5552 }
5553 if (Right.is(TT_BinaryOperator) &&
5554 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
5555 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
5556 Right.getPrecedence() != prec::Assignment)) {
5557 return true;
5558 }
5559 if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
5560 Left.is(tok::kw_operator)) {
5561 return false;
5562 }
5563 if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
5564 Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) {
5565 return false;
5566 }
5567 if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
5568 !Style.Cpp11BracedListStyle) {
5569 return false;
5570 }
5571 if (Left.is(TT_AttributeLParen) ||
5572 (Left.is(tok::l_paren) && Left.is(TT_TypeDeclarationParen))) {
5573 return false;
5574 }
5575 if (Left.is(tok::l_paren) && Left.Previous &&
5576 (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) {
5577 return false;
5578 }
5579 if (Right.is(TT_ImplicitStringLiteral))
5580 return false;
5581
5582 if (Right.is(TT_TemplateCloser))
5583 return false;
5584 if (Right.is(tok::r_square) && Right.MatchingParen &&
5585 Right.MatchingParen->is(TT_LambdaLSquare)) {
5586 return false;
5587 }
5588
5589 // We only break before r_brace if there was a corresponding break before
5590 // the l_brace, which is tracked by BreakBeforeClosingBrace.
5591 if (Right.is(tok::r_brace)) {
5592 return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) ||
5593 (Right.isBlockIndentedInitRBrace(Style)));
5594 }
5595
5596 // We only break before r_paren if we're in a block indented context.
5597 if (Right.is(tok::r_paren)) {
5598 if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
5599 !Right.MatchingParen) {
5600 return false;
5601 }
5602 auto Next = Right.Next;
5603 if (Next && Next->is(tok::r_paren))
5604 Next = Next->Next;
5605 if (Next && Next->is(tok::l_paren))
5606 return false;
5607 const FormatToken *Previous = Right.MatchingParen->Previous;
5608 return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
5609 }
5610
5611 // Allow breaking after a trailing annotation, e.g. after a method
5612 // declaration.
5613 if (Left.is(TT_TrailingAnnotation)) {
5614 return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
5615 tok::less, tok::coloncolon);
5616 }
5617
5618 if (Right.isAttribute())
5619 return true;
5620
5621 if (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))
5622 return Left.isNot(TT_AttributeSquare);
5623
5624 if (Left.is(tok::identifier) && Right.is(tok::string_literal))
5625 return true;
5626
5627 if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
5628 return true;
5629
5630 if (Left.is(TT_CtorInitializerColon)) {
5631 return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
5632 (!Right.isTrailingComment() || Right.NewlinesBefore > 0);
5633 }
5634 if (Right.is(TT_CtorInitializerColon))
5635 return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
5636 if (Left.is(TT_CtorInitializerComma) &&
5637 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
5638 return false;
5639 }
5640 if (Right.is(TT_CtorInitializerComma) &&
5641 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
5642 return true;
5643 }
5644 if (Left.is(TT_InheritanceComma) &&
5645 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
5646 return false;
5647 }
5648 if (Right.is(TT_InheritanceComma) &&
5649 Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) {
5650 return true;
5651 }
5652 if (Left.is(TT_ArrayInitializerLSquare))
5653 return true;
5654 if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
5655 return true;
5656 if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
5657 !Left.isOneOf(tok::arrowstar, tok::lessless) &&
5658 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
5659 (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
5660 Left.getPrecedence() == prec::Assignment)) {
5661 return true;
5662 }
5663 if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) ||
5664 (Left.is(tok::r_square) && Right.is(TT_AttributeSquare))) {
5665 return false;
5666 }
5667
5668 auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
5669 if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
5670 if (isAllmanLambdaBrace(Left))
5671 return !isItAnEmptyLambdaAllowed(Left, ShortLambdaOption);
5672 if (isAllmanLambdaBrace(Right))
5673 return !isItAnEmptyLambdaAllowed(Right, ShortLambdaOption);
5674 }
5675
5676 if (Right.is(tok::kw_noexcept) && Right.is(TT_TrailingAnnotation)) {
5677 switch (Style.AllowBreakBeforeNoexceptSpecifier) {
5679 return false;
5681 return true;
5683 return Right.Next && Right.Next->is(tok::l_paren);
5684 }
5685 }
5686
5687 return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
5688 tok::kw_class, tok::kw_struct, tok::comment) ||
5689 Right.isMemberAccess() ||
5690 Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
5691 tok::colon, tok::l_square, tok::at) ||
5692 (Left.is(tok::r_paren) &&
5693 Right.isOneOf(tok::identifier, tok::kw_const)) ||
5694 (Left.is(tok::l_paren) && Right.isNot(tok::r_paren)) ||
5695 (Left.is(TT_TemplateOpener) && Right.isNot(TT_TemplateCloser));
5696}
5697
5698void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) const {
5699 llvm::errs() << "AnnotatedTokens(L=" << Line.Level << ", P=" << Line.PPLevel
5700 << ", T=" << Line.Type << ", C=" << Line.IsContinuation
5701 << "):\n";
5702 const FormatToken *Tok = Line.First;
5703 while (Tok) {
5704 llvm::errs() << " M=" << Tok->MustBreakBefore
5705 << " C=" << Tok->CanBreakBefore
5706 << " T=" << getTokenTypeName(Tok->getType())
5707 << " S=" << Tok->SpacesRequiredBefore
5708 << " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount
5709 << " BK=" << Tok->getBlockKind() << " P=" << Tok->SplitPenalty
5710 << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength
5711 << " PPK=" << Tok->getPackingKind() << " FakeLParens=";
5712 for (prec::Level LParen : Tok->FakeLParens)
5713 llvm::errs() << LParen << "/";
5714 llvm::errs() << " FakeRParens=" << Tok->FakeRParens;
5715 llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo();
5716 llvm::errs() << " Text='" << Tok->TokenText << "'\n";
5717 if (!Tok->Next)
5718 assert(Tok == Line.Last);
5719 Tok = Tok->Next;
5720 }
5721 llvm::errs() << "----\n";
5722}
5723
5725TokenAnnotator::getTokenReferenceAlignment(const FormatToken &Reference) const {
5726 assert(Reference.isOneOf(tok::amp, tok::ampamp));
5727 switch (Style.ReferenceAlignment) {
5729 return Style.PointerAlignment;
5731 return FormatStyle::PAS_Left;
5736 }
5737 assert(0); //"Unhandled value of ReferenceAlignment"
5738 return Style.PointerAlignment;
5739}
5740
5742TokenAnnotator::getTokenPointerOrReferenceAlignment(
5743 const FormatToken &PointerOrReference) const {
5744 if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
5745 switch (Style.ReferenceAlignment) {
5747 return Style.PointerAlignment;
5749 return FormatStyle::PAS_Left;
5754 }
5755 }
5756 assert(PointerOrReference.is(tok::star));
5757 return Style.PointerAlignment;
5758}
5759
5760} // namespace format
5761} // namespace clang
NodeId Parent
Definition: ASTDiff.cpp:191
MatchType Type
StringRef P
This file contains the declaration of the FormatToken, a wrapper around Token with additional informa...
Defines the SourceManager interface.
bool ColonIsObjCMethodExpr
bool ColonIsDictLiteral
FormatToken * FirstStartOfName
bool InCpp11AttributeSpecifier
bool CaretFound
bool ColonIsForRangeExpr
bool CanBeExpression
unsigned LongestObjCSelectorName
bool VerilogAssignmentFound
bool IsExpression
bool InCSharpAttributeSpecifier
enum clang::format::@1210::AnnotatingParser::Context::@327 ContextType
unsigned BindingStrength
tok::TokenKind ContextKind
FormatToken * FirstObjCSelectorName
bool VerilogMayBeConcatenation
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
#define TRANSFORM_TYPE_TRAIT_DEF(Enum, _)
Definition: Type.h:4853
StateNode * Previous
Parser - This implements a parser for the C family of languages.
Definition: Parser.h:53
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:186
FormatToken * getFirstNonComment() const
bool endsWith(Ts... Tokens) const
true if this line ends with the given tokens in reversed order, ignoring comments.
bool startsWith(Ts... Tokens) const
true if this line starts with the given tokens in order, ignoring comments.
void calculateFormattingInformation(AnnotatedLine &Line) const
void annotate(AnnotatedLine &Line)
void setCommentLineLevels(SmallVectorImpl< AnnotatedLine * > &Lines) const
Adapts the indent levels of comment lines to the indent of the subsequent line.
const char * getTokenTypeName(TokenType Type)
Determines the name of a token type.
Definition: FormatToken.cpp:24
static bool isAllmanLambdaBrace(const FormatToken &Tok)
static bool IsFunctionArgument(const FormatToken &Tok)
static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current, const AnnotatedLine &Line)
static unsigned maxNestingDepth(const AnnotatedLine &Line)
static bool mustBreakAfterAttributes(const FormatToken &Tok, const FormatStyle &Style)
static bool isItAnEmptyLambdaAllowed(const FormatToken &Tok, FormatStyle::ShortLambdaStyle ShortLambdaOption)
static bool isCtorOrDtorName(const FormatToken *Tok)
static bool isAllmanBrace(const FormatToken &Tok)
TokenType
Determines the semantic type of a syntactic token, e.g.
Definition: FormatToken.h:176
static FormatToken * getFunctionName(const AnnotatedLine &Line)
@ LT_CommentAbovePPDirective
@ LT_ArrayOfStructInitializer
bool Ret(InterpState &S, CodePtr &PC, APValue &Result)
Definition: Interp.h:207
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
@ Parameter
The parameter type of a method or function.
@ Result
The result type of a method or function.
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:114
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:55
@ LK_Java
Should be used for Java.
Definition: Format.h:2919
@ LK_Cpp
Should be used for C, C++.
Definition: Format.h:2915
@ LK_ObjC
Should be used for Objective-C, Objective-C++.
Definition: Format.h:2925
@ LK_TableGen
Should be used for TableGen code.
Definition: Format.h:2930
@ LK_Proto
Should be used for Protocol Buffers (https://developers.google.com/protocol-buffers/).
Definition: Format.h:2928
@ LK_TextProto
Should be used for Protocol Buffer messages in text format (https://developers.google....
Definition: Format.h:2933
ShortLambdaStyle
Different styles for merging short lambdas containing at most one statement.
Definition: Format.h:824
@ SLS_All
Merge all lambdas fitting on a single line.
Definition: Format.h:848
@ SLS_Inline
Merge lambda into a single line if the lambda is argument of a function.
Definition: Format.h:842
@ SLS_None
Never merge lambdas into a single line.
Definition: Format.h:826
@ SLS_Empty
Only merge empty lambdas.
Definition: Format.h:834
@ BCIS_AfterColon
Break constructor initializers after the colon and commas.
Definition: Format.h:2034
@ BCIS_BeforeColon
Break constructor initializers before the colon and after the commas.
Definition: Format.h:2019
@ BCIS_BeforeComma
Break constructor initializers before the colon and commas, and align the commas with the colon.
Definition: Format.h:2027
@ BOS_All
Break before operators.
Definition: Format.h:1501
@ BOS_None
Break after operators.
Definition: Format.h:1477
@ BAS_DontAlign
Don't align, instead use ContinuationIndentWidth, e.g.:
Definition: Format.h:78
@ BAS_BlockIndent
Always break after an open bracket, if the parameters don't fit on a single line.
Definition: Format.h:99
@ BBIAS_Always
Always break before inline ASM colon.
Definition: Format.h:1989
@ PPDIS_BeforeHash
Indents directives before the hash.
Definition: Format.h:2577
@ SBS_Never
Never merge blocks into a single line.
Definition: Format.h:648
@ BTDS_Yes
Always break after template declaration.
Definition: Format.h:1001
@ SBPO_Never
Never put a space before opening parentheses.
Definition: Format.h:4023
@ SBPO_Always
Always put a space before opening parentheses, except when it's prohibited by the syntax rules (in fu...
Definition: Format.h:4069
@ PCIS_NextLineOnly
Put all constructor initializers on the next line if they fit.
Definition: Format.h:3237
@ PCIS_Never
Always put each constructor initializer on its own line.
Definition: Format.h:3190
@ PCIS_CurrentLine
Put all constructor initializers on the current line if they fit.
Definition: Format.h:3208
@ BILS_AfterColon
Break inheritance list after the colon and commas.
Definition: Format.h:2138
@ BILS_AfterComma
Break inheritance list only after the commas.
Definition: Format.h:2145
@ BILS_BeforeComma
Break inheritance list before the colon and commas, and align the commas with the colon.
Definition: Format.h:2130
@ BBNSS_Never
No line break allowed.
Definition: Format.h:607
@ BBNSS_Always
Line breaks are allowed.
Definition: Format.h:630
@ BBNSS_OnlyWithParen
For a simple noexcept there is no line break allowed, but when we have a condition it is.
Definition: Format.h:618
@ RCPS_OwnLine
Always put the requires clause on its own line.
Definition: Format.h:3626
@ RCPS_WithPreceding
Try to put the clause together with the preceding part of a declaration.
Definition: Format.h:3643
@ RCPS_WithFollowing
Try to put the requires clause together with the class or function declaration.
Definition: Format.h:3657
@ LS_Cpp11
Parse and format as C++11.
Definition: Format.h:4467
@ ABS_Leave
Leave the line breaking after attributes as is.
Definition: Format.h:1420
@ ABS_Always
Always break after attributes.
Definition: Format.h:1413
@ BFCS_Both
Add one space on each side of the :
Definition: Format.h:1067
@ BFCS_Before
Add space before the : only.
Definition: Format.h:1078
@ BFCS_After
Add space after the : only (space may be added before if needed for AlignConsecutiveBitFields).
Definition: Format.h:1084
@ SFS_Empty
Only merge empty functions.
Definition: Format.h:724
@ SFS_None
Never merge functions into a single line.
Definition: Format.h:702
@ SFS_InlineOnly
Only merge functions defined inside a class.
Definition: Format.h:716
@ BBCDS_Never
Keep the template declaration line together with concept.
Definition: Format.h:1949
@ BBCDS_Always
Always break before concept, putting it in the line after the template declaration.
Definition: Format.h:1960
@ SAPQ_After
Ensure that there is a space after pointer qualifiers.
Definition: Format.h:3941
@ SAPQ_Both
Ensure that there is a space both before and after pointer qualifiers.
Definition: Format.h:3947
@ SAPQ_Before
Ensure that there is a space before pointer qualifiers.
Definition: Format.h:3935
AttributeBreakingStyle BreakAfterAttributes
Break after a group of C++11 attributes before a function declaration/definition name.
Definition: Format.h:1432
@ AIAS_None
Don't align array initializer columns.
Definition: Format.h:132
@ SIAS_Always
Add spaces after < and before >.
Definition: Format.h:4263
@ SIAS_Leave
Keep a single space after < and before > if any spaces were present.
Definition: Format.h:4266
PointerAlignmentStyle
The &, && and * alignment style.
Definition: Format.h:3286
@ PAS_Left
Align pointer to the left.
Definition: Format.h:3291
@ PAS_Middle
Align pointer in the middle.
Definition: Format.h:3301
@ PAS_Right
Align pointer to the right.
Definition: Format.h:3296
@ RTBS_TopLevelDefinitions
Always break after the return type of top-level definitions.
Definition: Format.h:941
@ RTBS_All
Always break after the return type.
Definition: Format.h:901
@ RTBS_TopLevel
Always break after the return types of top-level functions.
Definition: Format.h:914
@ RTBS_None
Break after return type automatically.
Definition: Format.h:885
@ RTBS_AllDefinitions
Always break after the return type of function definitions.
Definition: Format.h:929
@ RAS_Right
Align reference to the right.
Definition: Format.h:3468
@ RAS_Left
Align reference to the left.
Definition: Format.h:3463
@ RAS_Pointer
Align reference like PointerAlignment.
Definition: Format.h:3458
@ RAS_Middle
Align reference in the middle.
Definition: Format.h:3473
A wrapper around a Token storing information about the whitespace characters preceding it.
Definition: FormatToken.h:261
unsigned NestingLevel
The nesting level of this token, i.e.
Definition: FormatToken.h:475
SmallVector< AnnotatedLine *, 1 > Children
If this token starts a block, this contains all the unwrapped lines in it.
Definition: FormatToken.h:546
unsigned OriginalColumn
The original 0-based column of this token, including expanded tabs.
Definition: FormatToken.h:462
bool isNot(T Kind) const
Definition: FormatToken.h:580
StringRef TokenText
The raw text of the token.
Definition: FormatToken.h:280
bool opensScope() const
Returns whether Tok is ([{ or an opening < of a template or in protos.
Definition: FormatToken.h:655
unsigned Finalized
If true, this token has been fully formatted (indented and potentially re-formatted inside),...
Definition: FormatToken.h:335
FormatToken * Next
The next token in the unwrapped line.
Definition: FormatToken.h:524
unsigned NewlinesBefore
The number of newlines immediately before the Token.
Definition: FormatToken.h:421
bool is(tok::TokenKind Kind) const
Definition: FormatToken.h:561
unsigned TotalLength
The total length of the unwrapped line up to and including this token.
Definition: FormatToken.h:458
bool isOneOf(A K1, B K2) const
Definition: FormatToken.h:573
bool isTrailingComment() const
Definition: FormatToken.h:702
FormatToken * MatchingParen
If this is a bracket, this points to the matching one.
Definition: FormatToken.h:518
FormatToken * Previous
The previous token in the unwrapped line.
Definition: FormatToken.h:521